system/modules/pct_customelements/PCT/CustomElements/Core/TemplateAttribute.php line 128

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 2014, Premium Contao Webworks, Premium Contao Themes
  8.  * @author        Tim Gatzky <info@tim-gatzky.de>
  9.  * @package        pct_customelements
  10.  * @link        http://contao.org
  11.  */
  12. /**
  13.  * Namespace
  14.  */
  15. namespace PCT\CustomElements\Core;
  16. /**
  17.  * Class file
  18.  * TemplateAttribute
  19.  */
  20. class TemplateAttribute
  21. {
  22.     /**
  23.      * Initialize and attach an attribute object
  24.      * @param object    CustomElement Attribute
  25.      */
  26.     public function __construct($objAttribute=null)
  27.     {
  28.         if(!is_object($objAttribute) || empty($objAttribute) || is_null($objAttribute))
  29.         {
  30.             return null;
  31.         }
  32.         $this->attribute $objAttribute;
  33.     }
  34.     
  35.     
  36.     /**
  37.      * Return the value of the attribute
  38.      * @return mixed|null
  39.      */
  40.     public function value() 
  41.     { 
  42.         // Store processed values
  43.         if(!isset($this->value) && isset($this->attribute)) 
  44.         {
  45.             $this->value $this->attribute->load();
  46.         }
  47.         
  48.         // load value hook
  49.         if(isset($this->attribute))
  50.         {
  51.             $this->value \PCT\CustomElements\Core\Hooks::callstatic('loadValueHook',array($this->value,$this->attribute));
  52.         }
  53.         if( !isset($this->value) )
  54.         {
  55.             return null;
  56.         }
  57.         
  58.         // check if arrays might be empty
  59.         if(is_array($this->value))
  60.         {
  61.             $this->value array_filter($this->value);
  62.             if(empty($this->value))
  63.             {
  64.                 $this->value null;
  65.             }
  66.         }
  67.         
  68.         return $this->value;
  69.     }
  70.     
  71.     
  72.     /**
  73.      * Returns the label of a value or the name of the attribute
  74.      * @param string    A certain value to search
  75.      * @return string
  76.      */
  77.     public function label($varValue='')
  78.     {
  79.         if(!isset($this->attribute))
  80.         {
  81.             return '';
  82.         }
  83.         
  84.         if(strlen($varValue) < 1)
  85.         {
  86.             $varValue $this->value();
  87.         }
  88.         
  89.         $strReturn $this->attribute->get('title');
  90.         $arrOptions \Contao\StringUtil::deserialize($this->attribute->get('options'));
  91.         if(!is_array($arrOptions))
  92.         {
  93.             $arrOptions explode(','$arrOptions);
  94.         }
  95.         if( empty($arrOptions) && strlen($varValue) < 1)
  96.         {
  97.             return $this->attribute->get('title');
  98.         }
  99.         
  100.         foreach($arrOptions as $option)
  101.         {
  102.             if( \is_string($option) && $option == $varValue )
  103.             {
  104.                 $strReturn $option;
  105.                 break;
  106.             }
  107.             if( \is_array($option) && isset($option['value']) && $option['value'] == $varValue)
  108.             {
  109.                 $strReturn $option['label'];
  110.                 break;
  111.             }
  112.         }
  113.         
  114.         return $strReturn;
  115.     }
  116.     
  117.     
  118.     /**
  119.      * Render the attribute and return html string
  120.      * @return string
  121.      */
  122.     public function render($strTemplate=''
  123.     {
  124.         if(!isset($this->attribute))
  125.         {
  126.             return '';
  127.         }
  128.         
  129.         if(strlen($strTemplate) > 0)
  130.         {
  131.             // reset the html variable
  132.             unset($this->html);
  133.             // set new template
  134.             $this->attribute->set('template',$strTemplate);
  135.         }
  136.         
  137.         // Store processed values
  138.         if(!isset($this->html))
  139.         {
  140.             $this->html $this->attribute->render();
  141.         }
  142.         
  143.         return $this->html;
  144.     }
  145.     
  146.     
  147.     /**
  148.      * Shortcut to render()
  149.      */
  150.     public function html($strTemplate='') {return $this->render($strTemplate);}
  151.     
  152.     
  153.     /**
  154.      * Return an options value
  155.      * @param string    Name of the option key
  156.      * @return mixed
  157.      */
  158.     public function optionvalue($strOption)    
  159.     {
  160.         // Store processed values
  161.         if(!isset($this->options[$strOption]) && isset($this->attribute))
  162.         {
  163.             $this->options = array($strOption => $this->attribute->loadOptionValue($strOption));
  164.         }
  165.         return $this->options[$strOption];
  166.     }
  167.     
  168.     
  169.     /**
  170.      * Shortcut to optionvalue
  171.      */
  172.     public function option($strOption) {return $this->optionvalue($strOption);}
  173.     
  174.     
  175.     /**
  176.      * Return the attribute object
  177.      * @return object
  178.      */
  179.     public function attribute() {return $this->attribute;}
  180.     
  181.     
  182.     /**
  183.      * Translate a value
  184.      * @param string
  185.      * @param string
  186.      */
  187.     public function translate($varValue,$strLanguage='')
  188.     {
  189.         if(!isset($this->attribute))
  190.         {
  191.             return $varValue;
  192.         }
  193.         
  194.         return $this->attribute->getTranslatedValue($varValue,$strLanguage);
  195.     }
  196.     
  197.     
  198.     /**
  199.      * Generate an responsive image
  200.      * @param string
  201.      * @return string
  202.      */
  203.     public function picture($strTemplate='picture_default')
  204.     {
  205.         if(!class_exists('\Contao\Picture') || version_compare(VERSION'3.4','<'))
  206.         {
  207.             return '';
  208.         }
  209.         
  210.         if(!$this->picture)
  211.         {
  212.             $objTmp = new \FrontendTemplate('ce_image');
  213.             
  214.             $arrSize array_filter\Contao\StringUtil::deserialize$this->optionvalue('size')) ?: array() );
  215.             $arrSizeFromAttribute array_filter\Contao\StringUtil::deserialize($this->attribute()->get('size')) ?: array() );
  216.             if(count($arrSize) < && count($arrSizeFromAttribute) > 0)
  217.             {
  218.                 $arrSize $arrSizeFromAttribute;
  219.             }
  220.             
  221.             $arrData['singleSRC'] = \FilesModel::findByUuid$this->value() )->path;
  222.             $arrData['size'] = $arrSize;
  223.             $arrData['alt'] = $this->optionvalue('alt');
  224.             $arrData['title'] = $this->optionvalue('title');
  225.             
  226.             \Contao\Controller::addImageToTemplate($objTmp,$arrData);
  227.             
  228.             $objTmp->insert($strTemplate,$objTmp->picture);
  229.         }
  230.     }
  231.     
  232.     
  233.     /**
  234.      * Generate an image and return path to the file as string
  235.      * @param array        Size array
  236.      * @return string
  237.      */
  238.     public function generate($arrSize=array())
  239.     {
  240.         $objAttribute $this->attribute();
  241.         if(!$objAttribute)
  242.         {
  243.             return '';
  244.         }
  245.         
  246.         switch($objAttribute->get('type'))
  247.         {
  248.             case 'image':
  249.                 $path \FilesModel::findByUuid$this->value() )->path;
  250.                 if(strlen($path) > 0)
  251.                 {
  252.                     // no parameter
  253.                     if(count($arrSize) < 1)
  254.                     {
  255.                         $arrSize \Contao\StringUtil::deserialize($this->optionvalue('size')) ?: array();
  256.                         if(!is_array($arrSize))
  257.                         {
  258.                             $arrSize explode(','$arrSize);
  259.                         }
  260.                         $arrSizeFromAttribute \Contao\StringUtil::deserialize($objAttribute->get('size')) ?: array();
  261.                         if(!is_array($arrSizeFromAttribute))
  262.                         {
  263.                             $arrSizeFromAttribute explode(','$arrSizeFromAttribute);
  264.                         }
  265.                         
  266.                         if(count(array_filter($arrSize,'strlen')) < 1)
  267.                         {
  268.                             $arrSize $arrSizeFromAttribute;
  269.                         }
  270.                     }
  271.                     
  272.                     return \Contao\Image::get($path,$arrSize[0],$arrSize[1],$arrSize[2]);
  273.                 }
  274.                 break;
  275.             default:
  276.                 return $this->render(); 
  277.                 break;
  278.         }
  279.         
  280.         return '';
  281.     }
  282.     
  283. }
  284.