system/modules/pct_customelements/PCT/CustomElements/Attributes/Image/Image.php line 199

Open in your IDE?
  1. <?php
  2. /**
  3.  * Contao Open Source CMS
  4.  * 
  5.  * Copyright (C) 2005-2013 Leo Feyer
  6.  * 
  7.  * @copyright    Tim Gatzky 2013, Premium Contao Webworks, Premium Contao Themes
  8.  * @author        Tim Gatzky <info@tim-gatzky.de>
  9.  * @package        pct_customelements
  10.  * @attribute    AttributeImage
  11.  * @link        http://contao.org
  12.  */
  13. /**
  14.  * Namespace
  15.  */
  16. namespace PCT\CustomElements\Attributes;
  17. /**
  18.  * Imports
  19.  */
  20. use Contao\Validator;
  21. use PCT\CustomElements\Core\Attribute as Attribute;
  22. use PCT\CustomElements\Helper\ControllerHelper as ControllerHelper;
  23. /**
  24.  * Class file
  25.  * Image
  26.  */
  27. class Image extends Attribute
  28. {
  29.     /**
  30.      * Tell the vault to store how to save the data (binary,blob)
  31.      * Leave empty to varchar
  32.      * @var boolean
  33.      */
  34.     protected $saveDataAs 'binary';
  35.     
  36.     /**
  37.      * Default rendering template
  38.      * @var string
  39.      */
  40.     protected $strTemplate 'customelement_attr_default';
  41.     
  42.         
  43.     /**
  44.      * Create new instance
  45.      * @param array
  46.      */ 
  47.     public function __construct($arrData=array())
  48.     {
  49.         if(count($arrData) > 0)
  50.         {
  51.             $this->setData($arrData);
  52.         }
  53.         
  54.         if( $this->get('template') != '' )
  55.         {
  56.             $this->strTemplate $this->get('template');
  57.         }
  58.     }    
  59.     /**
  60.      * Return the field definition
  61.      * @return array
  62.      */
  63.     public function getFieldDefinition()
  64.     {
  65.         $arrEval $this->getEval();
  66.         
  67.         // always add clear to image fields
  68.         if(!$this->get('eval_tl_class_clr'))
  69.         {
  70.             if( !isset($arrEval['tl_class']) )
  71.             {
  72.                 $arrEval['tl_class'] = '';
  73.             }
  74.             $arrEval['tl_class'] .= ' clr';
  75.         }
  76.         
  77.         $arrReturn = array
  78.         (
  79.             'label'            => array( $this->get('title'),$this->get('description') ),
  80.             'exclude'        => true,
  81.             'inputType'        => 'fileTree',
  82.             'eval'            => array_merge($arrEval,array('filesOnly'=>true'fieldType'=>'radio')),
  83.             'sql'            => "binary(16) NULL",
  84.         );
  85.         
  86.         return $arrReturn;
  87.     }
  88.     
  89.     /**
  90.      * Parse widget callback
  91.      */
  92.     public function parseWidgetCallback($objWidget,$strField,$arrFieldDef,$objDC,$varValue)
  93.     {
  94.         // validate
  95.         if(isset($_POST[$strField]))
  96.         {
  97.             $objWidget->validate();
  98.         }
  99.         
  100.         if($objWidget->hasErrors())
  101.         {
  102.             $objWidget->class 'error';
  103.         }
  104.         
  105.         $strBuffer $objWidget->parse();
  106.         
  107.         // load data container and language file
  108.         ControllerHelper::callstatic('loadDataContainer',array('tl_content'));
  109.         ControllerHelper::callstatic('loadLanguageFile',array('tl_content'));
  110.         
  111.         $options \Contao\StringUtil::deserialize($this->get('options'));
  112.         if(empty($options) || !is_array($options))
  113.         {
  114.             return $strBuffer;
  115.         }
  116.         
  117.         // alt field
  118.         if(in_array('alt'$options))
  119.         {
  120.             $strName $strField.'_alt';
  121.             $arrFieldDef $GLOBALS['TL_DCA']['tl_content']['fields']['alt'];
  122.             $arrFieldDef['eval']['tl_class'] = 'w50';
  123.             $arrFieldDef['saveDataAs'] = 'varchar';
  124.             $this->prepareChildAttribute($arrFieldDef,$strName);
  125.         }
  126.         
  127.         // title field
  128.         if(in_array('title'$options))
  129.         {
  130.             $strName $strField.'_title';
  131.             $arrFieldDef $GLOBALS['TL_DCA']['tl_content']['fields']['imageTitle'];
  132.             if( version_compare(VERSION'3.5','<=') )
  133.             {
  134.                 $arrFieldDef $GLOBALS['TL_DCA']['tl_content']['fields']['title'];
  135.             }
  136.             $arrFieldDef['eval']['tl_class'] = 'w50';
  137.             $arrFieldDef['saveDataAs'] = 'varchar';
  138.             $this->prepareChildAttribute($arrFieldDef,$strName);
  139.         }
  140.         
  141.         // size field 
  142.         if(in_array('size'$options))
  143.         {
  144.             $strName $strField.'_size';
  145.             $arrFieldDef $GLOBALS['TL_DCA']['tl_content']['fields']['size'];
  146.             $arrFieldDef['eval']['tl_class'] = 'w50';
  147.             $arrFieldDef['saveDataAs'] = 'varchar';
  148.             $this->prepareChildAttribute($arrFieldDef,$strName);
  149.         }
  150.         
  151.         // imageUrl field 
  152.         if(in_array('imageUrl'$options))
  153.         {
  154.             $strName $strField.'_imageUrl';
  155.             $arrFieldDef $GLOBALS['TL_DCA']['tl_content']['fields']['imageUrl'];
  156.             $arrFieldDef['eval']['tl_class'] = 'w50 wizard';
  157.             $arrFieldDef['saveDataAs'] = 'varchar';
  158.             $this->prepareChildAttribute($arrFieldDef,$strName);
  159.         }
  160.         
  161.         // fullscreen/new window field 
  162.         if(in_array('fullsize'$options))
  163.         {
  164.             $strName $strField.'_fullsize';
  165.             $arrFieldDef $GLOBALS['TL_DCA']['tl_content']['fields']['fullsize'];
  166.             $arrFieldDef['eval']['tl_class'] = 'w50';
  167.             $arrFieldDef['saveDataAs'] = 'varchar';
  168.             $this->prepareChildAttribute($arrFieldDef,$strName);
  169.         }
  170.         
  171.         // caption field 
  172.         if(in_array('caption'$options))
  173.         {
  174.             $strName $strField.'_caption';
  175.             $arrFieldDef $GLOBALS['TL_DCA']['tl_content']['fields']['caption'];
  176.             $arrFieldDef['eval']['tl_class'] = 'w50';
  177.             $arrFieldDef['saveDataAs'] = 'varchar';
  178.             $this->prepareChildAttribute($arrFieldDef,$strName);
  179.         }
  180.         
  181.         return $strBuffer;
  182.     }
  183.     
  184.     
  185.     /**
  186.      * Render the attribute and return html
  187.      * @param string
  188.      * @param mixed
  189.      * @param object
  190.      * @param object
  191.      * @return array
  192.      */
  193.     public function renderCallback($strField,$varValue,$objTemplate,$objAttribute)
  194.     {
  195.         $varValue \Contao\StringUtil::deserialize($varValue);
  196.         
  197.         $objOrig $this->getActiveRecord();
  198.         $objActiveRecord = new \Contao\ContentModel();
  199.         $objActiveRecord->mergeRow$objOrig->row() );
  200.         $objActiveRecord->__set('strPk',$objActiveRecord->id);
  201.         $singleSRC $varValue;
  202.         if( !Validator::isBinaryUuid($varValue) )
  203.         {
  204.             $singleSRC \Contao\StringUtil::uuidToBin($singleSRC);
  205.         }
  206.         $objActiveRecord->singleSRC $singleSRC;
  207.         $objActiveRecord->customTpl '';
  208.         $objActiveRecord->start '';
  209.         $objActiveRecord->stop '';
  210.         $objActiveRecord->invisible '';
  211.         $objActiveRecord->autogrid 0;
  212.         $objActiveRecord->cssID $objAttribute->get('cssID');
  213.         $objActiveRecord->isCustomElement true;
  214.         $blnOverwriteMeta false;
  215.         // laod option values
  216.         $arrOptionValues array_filter$this->loadOptionValues($strField) );
  217.         
  218.         foreach($arrOptionValues as $k => $v)
  219.         {
  220.             $objActiveRecord->{$k} = $v;
  221.             // meta data fields
  222.             if( \in_array($k, array('title','alt','caption','imageUrl') ) )
  223.             {
  224.                 $blnOverwriteMeta true;
  225.             }
  226.         }
  227.         $objActiveRecord->overwriteMeta $blnOverwriteMeta;
  228.         $objActiveRecord->imageTitle =  $arrOptionValues['title'] ?? '';
  229.         
  230.         // a non ce attribute template is coming in
  231.         if( \property_exists($this,'isCustomTemplate') && $this->isCustomTemplate)
  232.         {
  233.             $objActiveRecord->customTpl $objAttribute->get('template');
  234.         }
  235.         $objMyAttribute = new \Contao\ContentImage($objActiveRecord);
  236.         $objMyAttribute->customTpl '';
  237.         $objMyAttribute->type 'image';
  238.         $objMyAttribute->overwriteMeta $blnOverwriteMeta;
  239.         
  240.         $objActiveRecord->headline '';
  241.         $objMyAttribute->imageUrl $arrOptionValues['imageUrl'] ?? '';
  242.         $objMyAttribute->href $arrOptionValues['imageUrl'] ?? '';
  243.         $objMyAttribute->fullsize $arrOptionValues['fullsize'] ?? '';
  244.         $objMyAttribute->caption $arrOptionValues['caption'] ?? '';
  245.     
  246.         $options \Contao\StringUtil::deserialize($this->get('options'));
  247.         
  248.         if(!empty($options) && is_array($options) && !empty($arrOptionValues['size']))
  249.         {
  250.             $arrSize array_filter(\Contao\StringUtil::deserialize($arrOptionValues['size']),'strlen');
  251.             if(count($arrSize) > 0)
  252.             {
  253.                 $objMyAttribute->size $arrOptionValues['size'];
  254.             }
  255.         }
  256.         
  257.         if(!$objMyAttribute->size)
  258.         {
  259.             $objMyAttribute->size $this->get('size');
  260.         }
  261.         
  262.         $objMyAttribute->space = array();
  263.                 
  264.         // pass to template
  265.         $objTemplate->activeRecord $objActiveRecord;
  266.         $objTemplate->element $objMyAttribute;
  267.         
  268.         // generate the attribute and place html in attribute template
  269.         $objTemplate->value \Contao\Controller::replaceInsertTags$objMyAttribute->generate() ?? '' );
  270.         
  271.         // bypass the CE attribute template when a Contao template is in use
  272.         if( \property_exists($objAttribute,'isCustomTemplate') && $objAttribute->isCustomTemplate)
  273.         {
  274.             return $objTemplate->value;
  275.         }
  276.         
  277.         return $objTemplate->parse();
  278.     }
  279.     
  280.     
  281.     /**
  282.      * Generate wildcard value
  283.      * @param mixed
  284.      * @param object    DatabaseResult
  285.      * @return string
  286.      */
  287.     public function processWildcardValue($varValue,$objAttribute)
  288.     {
  289.         if($objAttribute->get('type') != 'image' || empty($varValue))
  290.         {
  291.             return $varValue;
  292.         }
  293.         
  294.         $size $GLOBALS['PCT_CUSTOMELEMENTS']['defaultWildcardImageSize'];
  295.         $objFile \Contao\FilesModel::findByPk($varValue);
  296.         $image ControllerHelper::callstatic('getImage',array($objFile->path,$size[0],$size[1],$size[2]));
  297.         
  298.         if(strlen($image) < 1)
  299.         {
  300.             return '';
  301.         }
  302.         
  303.         $image \Contao\Image::getHtml($image);
  304.         return $image;
  305.     }
  306.     
  307.     
  308.     /**
  309.      * Return the field definition for an options field
  310.      * @param string
  311.      * @return array
  312.      */
  313.     public function getOptionFieldDefinition($strOption)
  314.     {
  315.         $arrReturn $GLOBALS['TL_DCA']['tl_content']['fields'][$strOption] ?? array();
  316.         $arrReturn['eval']['tl_class'] = 'w50';
  317.         $arrReturn['saveDataAs'] = 'varchar';
  318.         
  319.         if( isset($arrReturn['eval']['datepicker']) || isset($arrReturn['eval']['dcaPicker']) )
  320.         {
  321.             $arrReturn['eval']['tl_class'] .= ' wizard';
  322.         }
  323.         return $arrReturn;
  324.     }
  325. }