system/modules/pct_customelements/PCT/CustomElements/Core/GroupFactory.php line 40

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\Origin as Origin;
  20. use PCT\CustomElements\Helper\QueryBuilder as QueryBuilder;
  21. use PCT\CustomElements\Core\Cache as Cache;
  22. use PCT\CustomElements\Models\GroupModel as GroupModel;
  23. /**
  24.  * Class file
  25.  * CustomElement
  26.  * Generate CustomElements Groups and their attribute fields
  27.  */
  28. class GroupFactory
  29. {
  30.     /**
  31.      * Create a CustomElement object instance by a model instance
  32.      * @param object    Model or DatabaseResult
  33.      * @return object    \PCT\CustomElements\Core\CustomElement
  34.      */
  35.     public static function create($objModel)
  36.     {
  37.         // if a database result is coming
  38.         if(strlen(strpos(get_class($objModel), 'Database')) > && strlen(strpos(get_class($objModel), 'Result')) > 0)
  39.         {
  40.             $objModel = new GroupModel($objModel,false);
  41.         }
  42.         
  43.         $objReturn = new Group();
  44.         $objReturn->setData($objModel->row());
  45.         $objReturn->setModel($objModel);
  46.         
  47.         // trigger the onFactoryCreate Hook
  48.         \PCT\CustomElements\Core\Hooks::callstatic('onFactoryCreateHook',array($objReturn,$objModel));
  49.         
  50.         return $objReturn;
  51.     }
  52.     
  53.     
  54.     /**
  55.      * Generate the attribute groups by a given id and return the Group object with all its attributes
  56.      * @param integer
  57.      * @return object Group
  58.      */
  59.     public static function findById($intId$arrOptions=array())
  60.     {
  61.         if($intId 1)
  62.         {
  63.             return null;
  64.         }
  65.         
  66.         // look up from cache
  67.         if(Cache::getGroup($intId))
  68.         {
  69.             return Cache::getGroup($intId);
  70.         }
  71.         
  72.         $objModel GroupModel::findByPk($intId);
  73.         if($objModel === null)
  74.         {
  75.             return null;
  76.         }
  77.         
  78.         $objReturn = static::create$objModel );
  79.         
  80.         // add to cache
  81.         Cache::addGroup($objReturn->id$objReturn);
  82.         Cache::addGroup('ce_'.$objReturn->pid.'_'.$objReturn->alias$objReturn);
  83.         
  84.         return $objReturn;
  85.     }
  86.     
  87.     
  88.     /**
  89.      * Fetch the attribute groups by a given id and return the Group object with all its attributes
  90.      * @param integer
  91.      * @return object GroupModel
  92.      */
  93.     public static function fetchById($intId$arrOptions=array())
  94.     {
  95.         return GroupModel::findByPk($intId,$arrOptions);
  96.     }
  97.     
  98.     
  99.     /**
  100.      * Generate the published attribute groups by a given id and return the Group object with all its attributes
  101.      * @param integer
  102.      * @return object Group
  103.      */
  104.     public static function findPublishedById($intId$arrOptions=array())
  105.     {
  106.         if($intId 1)
  107.         {
  108.             return null;
  109.         }
  110.         
  111.         // look up from cache
  112.         if(Cache::getGroup($intId) && (boolean)Cache::getGroup($intId)->published === true)
  113.         {
  114.             return Cache::getGroup($intId);
  115.         }
  116.         
  117.         $objModel GroupModel::findPublishedById($intId$arrOptions);
  118.         if($objModel === null)
  119.         {
  120.             return null;
  121.         }
  122.         
  123.         $objReturn = static::create$objModel );
  124.         
  125.         // add to cache
  126.         Cache::addGroup($objReturn->id$objReturn);
  127.         Cache::addGroup('ce_'.$objReturn->pid.'_'.$objReturn->alias$objReturn);
  128.         
  129.         return $objReturn;
  130.     }
  131.     
  132.     /**
  133.      * Generate all attribute groups by a given id and return as array
  134.      * @param integer
  135.      * @return array
  136.      */
  137.     public static function findByParentId($intPid$arrOptions=array())
  138.     {
  139.         if(Cache::getGroupsByPid($intPid))
  140.         {
  141.             return Cache::getGroupsByPid($intPid);
  142.         }
  143.         
  144.         $objCollection GroupModel::findByPid($intPid$arrOptions);
  145.         if($objCollection === null)
  146.         {
  147.             return array();
  148.         }
  149.         
  150.         $arrReturn = array();
  151.         foreach($objCollection as $objModel)
  152.         {
  153.             $objGroup = static::create$objModel );
  154.             
  155.             $arrReturn[] = $objGroup;
  156.             
  157.             // add to cache
  158.             Cache::addGroup($objGroupn->id$objGroup);
  159.         }
  160.         
  161.         // add the groups to the cache
  162.         Cache::addGroupsByPid($intPid,$arrReturn);
  163.         
  164.         return $arrReturn;
  165.     }
  166.     
  167.     
  168.     /**
  169.      * Generate all published attribute groups by a given id and return as array
  170.      * @param integer
  171.      * @return array
  172.      */
  173.     public static function findPublishedByParentId($intPid$arrOptions=array())
  174.     {
  175.         $arrCache Cache::getData();
  176.         
  177.         // look up from cache
  178.         $strKey 'GroupFactory::'.__FUNCTION__;
  179.         if( isset($arrCache['GROUP'][$strKey][$intPid]) && $arrCache['GROUP'][$strKey][$intPid] !== null)
  180.         {
  181.             return $arrCache['GROUP'][$strKey][$intPid];
  182.         }
  183.         
  184.         $objCollection GroupModel::findPublishedByPid($intPid$arrOptions);
  185.         if($objCollection === null)
  186.         {
  187.             return array();
  188.         }
  189.         
  190.         $arrReturn = array();
  191.         foreach($objCollection as $objModel)
  192.         {
  193.             $objGroup = static::create$objModel );
  194.             
  195.             $arrReturn[] = $objGroup;
  196.             
  197.             // add to cache
  198.             Cache::addGroup($objGroup->id$objGroup);
  199.         }
  200.         
  201.         // add the groups to the cache
  202.         Cache::add('CE_GROUPS_'.$intPid,$arrReturn);
  203.         
  204.         // add to cache
  205.         $arrCache['GROUP'][$strKey][$intPid] = $arrReturn;
  206.         Cache::setData($arrCache);
  207.         
  208.         return $arrReturn;
  209.     }
  210.     
  211.     
  212.     /**
  213.      * Find a group by its alias in a customelement by ID or Alias
  214.      * @param string
  215.      * @param integer|string        Id or Alias of the CE
  216.      * @return object
  217.      */
  218.     public static function findByAliasAndCustomElement($strAlias,$varCE,$arrOptions=array())
  219.     {
  220.         // look up from cache
  221.         if(Cache::getGroup('ce_'.$varCE.'_'.$strAlias))
  222.         {
  223.             return Cache::getGroup('ce_'.$varCE.'_'.$strAlias);
  224.         }
  225.         
  226.         $objCE \PCT\CustomElements\Core\CustomElementFactory::findByIdOrAlias($varCE,$arrOptions);
  227.         if($objCE === null)
  228.         {
  229.             return null;
  230.         }
  231.         
  232.         $objModel GroupModel::findByAliasAndPid($strAlias,$objCE->id,$arrOptions);
  233.         if($objModel === null)
  234.         {
  235.             return null;
  236.         }
  237.         
  238.         $objReturn = static::create$objModel );
  239.         
  240.         // add to cache
  241.         Cache::addGroup($objReturn->id$objReturn);
  242.         Cache::addGroup('ce_'.$objCE->id.'_'.$strAlias$objReturn);
  243.         Cache::addGroup('ce_'.$objCE->alias.'_'.$strAlias$objReturn);
  244.         
  245.         return $objReturn;
  246.     }    
  247. }