system/modules/pct_customelements/PCT/CustomElements/Core/CustomElementFactory.php line 48

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.  * @link        http://contao.org
  11.  */
  12. /**
  13.  * Namespace
  14.  */
  15. namespace PCT\CustomElements\Core;
  16. /**
  17.  * Imports
  18.  */
  19. use PCT\CustomElements\Core\CustomElement;
  20. use PCT\CustomElements\Core\CustomElements as CustomElements;
  21. use PCT\CustomElements\Core\Cache as Cache;
  22. use PCT\CustomElements\Models\CustomElementModel as CustomElementModel;
  23. /**
  24.  * Class file
  25.  * CustomElementFactory
  26.  */
  27. class CustomElementFactory
  28. {
  29.     /**
  30.      * Create a CustomElement object instance by a model instance
  31.      * @param object    Model or DatabaseResult
  32.      * @return object    \PCT\CustomElements\Core\CustomElement
  33.      */
  34.     public static function create($objModel)
  35.     {
  36.         // if a database result is coming
  37.         if(strlen(strpos(get_class($objModel), 'Database')) > && strlen(strpos(get_class($objModel), 'Result')) > 0)
  38.         {
  39.             $objModel = new CustomElementModel($objModel,false);
  40.         }
  41.         
  42.         $objReturn = new CustomElement();
  43.         $objReturn->setData($objModel->row());
  44.         $objReturn->setModel($objModel);
  45.         
  46.         // trigger the onFactoryCreate Hook
  47.         \PCT\CustomElements\Core\Hooks::callstatic('onFactoryCreateHook',array($objReturn,$objModel));
  48.         
  49.         return $objReturn;
  50.     }
  51.     
  52.     
  53.     /**
  54.      * Find a CustomElement instance by an ID
  55.      * @param id
  56.      * @return object    Filter instance
  57.      */
  58.     public static function findById($intId)
  59.     {
  60.         if(Cache::getCustomElement($intId))
  61.         {
  62.             return Cache::getCustomElement($intId);
  63.         }
  64.         
  65.         $objModel CustomElementModel::findByPk($intId);
  66.         if($objModel === null)
  67.         {
  68.             return null;
  69.         }
  70.         
  71.         $objReturn = static::create$objModel );
  72.         
  73.         // add to cache
  74.         Cache::addCustomElement($objModel->id,$objReturn);
  75.         Cache::addCustomElement($objModel->alias,$objReturn);
  76.         
  77.         return $objReturn;
  78.     }
  79.     
  80.     
  81.     /**
  82.      * Return the custom element model by its ID
  83.      * @param integer
  84.      * @return object CustomElement
  85.      */
  86.     public static function fetchById($intId,$arrOptions=array())
  87.     {
  88.         return CustomElementModel::findByPk($intId,$arrOptions);
  89.     }
  90.     
  91.     
  92.     /**
  93.      * Return the CustomElement either by its alias or id
  94.      * @param mixed
  95.      * @return object
  96.      */
  97.     public static function findByIdOrAlias($varCE,$arrOptions)
  98.     {
  99.         if(is_string($varCE) && !is_numeric($varCE))
  100.         {
  101.             return static::findById($varCE,$arrOptions);
  102.         }
  103.         return static::findByAlias($varCE,$arrOptions);
  104.     }
  105.     
  106.     
  107.     /**
  108.      * Return a CustomElement by its alias
  109.      * @param string
  110.      * @return object CustomElement
  111.      */
  112.     public static function findByAlias($strAlias,$arrOptions=array())
  113.     {
  114.         if(Cache::getCustomElement($strAlias))
  115.         {
  116.             return Cache::getCustomElement($strAlias);
  117.         }
  118.         
  119.         $objModel CustomElementModel::findByAlias($strAlias$arrOptions);
  120.         if($objModel === null)
  121.         {
  122.             return null;
  123.         }
  124.         $objReturn = static::create$objModel );
  125.         
  126.         // add to cache
  127.         Cache::addCustomElement($objModel->id,$objReturn);
  128.         Cache::addCustomElement($objModel->alias,$objReturn);
  129.         
  130.         return $objReturn;
  131.     }
  132.     
  133.     
  134.     /**
  135.      * Return the database resqult by the alias
  136.      * @param string
  137.      * @return object CustomElement
  138.      */
  139.     public static function fetchByAlias($strAlias,$arrOptions=array())
  140.     {
  141.         return CustomElementModel::findByAlias($strAlias$arrOptions);
  142.     }
  143.     
  144.     
  145.     /**
  146.      * Return a customelement by an attribute id
  147.      * @param integer    Attribute id
  148.      * @return object
  149.      */
  150.     public static function findByAttributeId($intAttribute,$arrOptions=array())
  151.     {
  152.         $strQuery 'tl_pct_customelement.id=(SELECT pid FROM tl_pct_customelement_group WHERE id=(SELECT pid FROM tl_pct_customelement_attribute WHERE id=?) )';
  153.         $arrWildcards = array($intAttribute);
  154.         
  155.         $objModel CustomElementModel::findByCustomSql($strQuery,$arrWildcards,$arrOptions);
  156.         if($objModel === null)
  157.         {
  158.             return null;
  159.         }
  160.         
  161.         $objReturn = static::create$objModel );
  162.         
  163.         // add to cache
  164.         Cache::addCustomElement($objModel->id,$objReturn);
  165.         Cache::addCustomElement($objModel->alias,$objReturn);
  166.         
  167.         return $objReturn;
  168.     }
  169.     
  170.     
  171.     /**
  172.      * Find all active custom elements and return as array
  173.      * @return array
  174.      */
  175.     public static function findAll()
  176.     {
  177.         $objCollection CustomElementModel::findAll();
  178.         if($objCollection === null)
  179.         {
  180.             return array();
  181.         }
  182.         
  183.         $arrReturn = array();
  184.         foreach($objCollection as $objModel)
  185.         {
  186.             $arrReturn[] = static::create$objModel );
  187.             
  188.             // add to cache
  189.             Cache::addCustomElement($objModel->id,$objReturn);
  190.             Cache::addCustomElement($objModel->alias,$objReturn);
  191.         }
  192.         
  193.         return $arrReturn;
  194.     }
  195.     
  196.     /**
  197.      * Return true if the custom element has been changed by a user e.g. after saving
  198.      * @param integer     Timestamp
  199.      * @param string
  200.      * @return boolean 
  201.      */
  202.     public static function hasChangedByTimestamp($intTstamp,$strAlias='')
  203.     {
  204.         if(strlen($strAlias) < 1)
  205.         {
  206.             if(\Contao\Input::get('act') != 'edit')
  207.             {
  208.                 return false;
  209.             }
  210.             
  211.             $objActiveRecord null;
  212.             $strTable \Contao\Input::get('table');
  213.             $strModel \Contao\Model::getClassFromTable($strTable);
  214.             if(class_exists($strModel))
  215.             {
  216.                 $objActiveRecord $strModel::findByPk(\Contao\Input::get('id'));
  217.             }
  218.             
  219.             if($objActiveRecord === null)
  220.             {
  221.                 $objActiveRecord \Contao\Database::getInstance()->prepare("SELECT * FROM ".$strTable." WHERE id=?")->limit(1)->execute(\Contao\Input::get('id'));
  222.             }
  223.             
  224.             // get the selection field for the current table
  225.             $selectionfield CustomElements::getSelectionField($strTable);
  226.             
  227.             // check if it is a custom element we are in
  228.             if(!static::isCustomElement($objActiveRecord->{$selectionfield},$strTable))
  229.             {
  230.                return false;
  231.             }
  232.             
  233.             $strAlias $objActiveRecord->{$selectionfield};
  234.         }
  235.     
  236.         $objResult \PCT\CustomElements\Models\CustomElementModel::findByAlias($strAlias);
  237.         
  238.         return ($objResult->tstamp != $intTstamp true false);
  239.     }
  240.     
  241.     
  242.     /**
  243.      * Return true if the name/alias is a custom element
  244.      * @param string
  245.      * @param string
  246.      * @return boolean
  247.      */
  248.     public static function isCustomElement($strAlias)
  249.     {
  250.         return (\PCT\CustomElements\Models\CustomElementModel::findByAlias($strAlias) !== null true false);
  251.     }
  252. }