<?php
namespace FoxHabbit\BasisBundle\Model\DataObject;
class CssBackground extends \Pimcore\Model\DataObject\Concrete {
private $disabled = false;
private $thumbnail = null;
public function setDisabled( $disabled) {
$this->disabled = ! ! $disabled;
}
public function setThumbnail( $thumbnail) {
if( is_array( $thumbnail) && count( $thumbnail)) {
$this->thumbnail = $thumbnail;
} else if( is_string( $thumbnail) || $thumbnail === null || $thumbnail === false) {
$this->thumbnail = $thumbnail;
}
}
public function getClassString( $addClass = array()) {
if( is_string( $addClass)) {
$addClass = explode(' ', $addClass);
} else if( ! is_array( $addClass)) {
$addClass = array();
}
if( ! $this->disabled) {
return implode( ' ', array_merge( $addClass, explode( ' ', $this->getBgClass().' '.$this->getContentClass())));
} else {
return implode( ' ', array_merge( $addClass, explode( ' ', $this->getContentClass())));
}
}
public function getBgClassString( $addClass = array()) {
if( is_string( $addClass)) {
$addClass = explode(' ', $addClass);
} else if( ! is_array( $addClass)) {
$addClass = array();
}
if( ! $this->disabled) {
return implode( ' ', array_merge( $addClass, explode( ' ', $this->getBgClass())));
} else {
return implode( ' ', $addClass);
}
}
public function getContentClassString( $addClass = array()) {
if( is_string( $addClass)) {
$addClass = explode(' ', $addClass);
} else if( ! is_array( $addClass)) {
$addClass = array();
}
return implode( ' ', array_merge( $addClass, explode( ' ', $this->getContentClass())));
}
private function getImagePath( $addImage, $generateThumbnail=true) {
$image = null;
if( $addImage instanceof \Pimcore\Model\DataObject\Data\Hotspotimage) {
if( $generateThumbnail && is_string( $this->thumbnail)) {
$image = $addImage->getThumbnail( $this->thumbnail);
} else {
$image = $addImage->getThumbnail();
}
} else if( $addImage instanceof \Pimcore\Model\Asset\Image\Thumbnail) {
if( $addImage->getPath()) {
$image = $addImage->getPath();
}
} else if( $addImage instanceof \Pimcore\Model\Asset\Image) {
if( $addImage->getFullPath()) {
if( $generateThumbnail && is_string( $this->thumbnail) && ! $addImage->isAnimated()) {
$image = $this->getImagePath( $addImage->getThumbnail( $this->thumbnail), false);
} else {
$image = $addImage->getFullPath();
}
}
} else if( $addImage instanceof \Pimcore\Model\Document\Editable\Image) {
if( $addImage->getSrc()) {
if( $generateThumbnail && $this->thumbnail) {
// TODO: GENERATE THUMBNAIL
$image = $addImage->getSrc();
} else {
$image = $addImage->getSrc();
}
}
} else if( $addImage instanceof \Pimcore\Model\Document\Editable\Href) {
if( $addImage->getFullPath()) {
if( $generateThumbnail && $this->thumbnail) {
// TODO: GENERATE THUMBNAIL
$image = $this->getImagePath( $addImage->getElement(), true);
} else {
$image = $addImage->getFullPath();
}
}
} else {
//$image = 'addImage class_not_handled:'.get_class( $addImage);
}
return $image;
}
public function getStyleString( $addImage = null, $addYoutube = null, $addStyles = array()) {
if( $this->disabled) {
return '';
}
$prefixStyleProperties = array( 'background-size');
$prefixStyleVendors = array( '-webkit-', '-moz-', '-o-');
$style = array();
if( is_array( $addStyles)) {
foreach( $addStyles as $k=>$v) {
$style[$k] = $v;
}
}
if( $color = $this->getColor()) {
$style['background-color'] = $color;
}
$image = null;
if( $this->getAllowImage() && is_object( $addImage)) {
$image = $this->getImagePath( $addImage, $this->thumbnail);
} else if( is_object( $addImage)) {
//$image = 'CAN NOT ADD IMAGE';
} else if( $addImage) {
//$image = 'Image is not an object';
}
if( ! $image && is_object( $this->getImage())) {
$image = $this->getImagePath( $this->getImage(), $this->thumbnail);
} else if( ! $image) {
//$image = 'NO IMAGE';
}
if( $image) {
$style['background-image'] = 'url('.$image.')';
// First set default styles for the background image
$style['background-repeat'] = 'no-repeat';
$style['background-position'] = 'center center';
//$style['-webkit-background-size'] = 'cover';
//$style['-moz-background-size'] = 'cover';
//$style['-o-background-size'] = 'cover';
$style['background-size'] = 'cover';
// Set styles defind in the object
if( strlen( $this->getBgSize()) && $this->getBgSize() != '') {
//$style['-webkit-background-size'] = $this->getBgSize();
//$style['-moz-background-size'] = $this->getBgSize();
//$style['-o-background-size'] = $this->getBgSize();
$style['background-size'] = $this->getBgSize();
}
if( strlen( $this->getBgPosition()) && $this->getBgPosition() != '') {
$style['background-position'] = $this->getBgPosition();
}
if( strlen( $this->getBgRepeat()) && $this->getBgRepeat() != '') {
$style['background-repeat'] = $this->getBgRepeat();
}
if( strlen( $this->getBgAttachment()) && $this->getBgAttachment() != '') {
$style['background-attachment'] = $this->getBgAttachment();
}
}
foreach( $this->getProperties() as $prop) {
if( $prop->getType() === 'text') {
$style[$prop->getName()] = $prop->getData();
}
}
$ret = '';
foreach( $style as $k=>$v) {
if( in_array( $k, $prefixStyleProperties)) {
foreach($prefixStyleVendors as $prefix) {
if( ! array_key_exists( $prefix.$k, $style)) {
$ret .= $prefix.$k.':'.$v.';';
}
}
}
$ret .= $k.':'.$v.';';
}
return $ret;
}
public function canAddImage() {
return $this->getAllowImage();
}
public function canAddYouTube() {
return $this->getAllowYoutube();
}
}