system/modules/pct_customelements/PCT/CustomElements/Core/AttributeFactory.php line 43

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 Contao\Database;
  20. use Contao\StringUtil;
  21. use PCT\CustomElements\Core\Origin as Origin;
  22. use PCT\CustomElements\Helper\QueryBuilder as QueryBuilder;
  23. use PCT\CustomElements\Helper\ControllerHelper as ControllerHelper;
  24. use PCT\CustomElements\Core\Cache as Cache;
  25. use PCT\CustomElements\Models\AttributeModel as AttributeModel;
  26. /**
  27.  * Class file
  28.  * AttributeFactory
  29.  * Generate attributes
  30.  */
  31. class AttributeFactory
  32. {
  33.     /**
  34.      * Create a CustomElement object instance by a model instance
  35.      * @param object    Model or DatabaseResult
  36.      * @return object    \PCT\CustomElements\Core\CustomElement
  37.      */
  38.     public static function create($objModel)
  39.     {
  40.         // if a database result is coming
  41.         if(strlen(strpos(get_class($objModel), 'Database')) > && strlen(strpos(get_class($objModel), 'Result')) > 0)
  42.         {
  43.             $objModel = new AttributeModel($objModel,false);
  44.         }
  45.         if( !isset($GLOBALS['PCT_CUSTOMELEMENTS']['ATTRIBUTES'][$objModel->type]) )
  46.         {
  47.             return null;
  48.         }
  49.         
  50.         $strClass $GLOBALS['PCT_CUSTOMELEMENTS']['ATTRIBUTES'][$objModel->type]['class'];
  51.         if(strlen($strClass) < 1)
  52.         {
  53.             return null;
  54.         }
  55.         
  56.         if(!class_exists($strClass))
  57.         {
  58.             \PCT\CustomElements\Loader\AttributeLoader::load($objModel->type);
  59.         }
  60.         
  61.         try
  62.         {
  63.             if(!class_exists($strClass))
  64.             {
  65.                 throw new \Exception('Cannot load Attribute class or class not found: '.$strClass);
  66.             }
  67.             
  68.         }
  69.         catch(\Exception $e)
  70.         {
  71.             throw new \Exception($e->getMessage());
  72.         }
  73.         
  74.         $objReturn = new $strClass$objModel->row() );
  75.         $objReturn->setModel($objModel);
  76.         
  77.         // trigger the onFactoryCreate Hook
  78.         \PCT\CustomElements\Core\Hooks::callstatic('onFactoryCreateHook',array($objReturn,$objModel));
  79.         
  80.         return $objReturn;
  81.     }
  82.     
  83.     
  84.     /**
  85.      * Find all active attributes and return as array
  86.      * @return array
  87.      */
  88.     public static function findAll()
  89.     {
  90.         $objCollection AttributeModel::findAll();
  91.         if($objCollection === null)
  92.         {
  93.             return array();
  94.         }
  95.         
  96.         $objRegistry \Contao\Model\Registry::getInstance();
  97.         
  98.         $arrReturn = array();
  99.         foreach($objCollection as $objModel)
  100.         {
  101.             $objAttribute = static::create$objModel );
  102.             if($objAttribute === null)
  103.             {
  104.                 continue;
  105.             }
  106.             
  107.             $arrReturn[] = $objAttribute;
  108.             
  109.             // add to cache
  110.             Cache::addAttribute($objAttribute->id,$objAttribute);
  111.             Cache::addAttribute($objAttribute->uuid,$objAttribute);
  112.         }
  113.         
  114.         return $arrReturn;
  115.     }
  116.     
  117.     
  118.     /**
  119.      * Find a CustomElement instance by an ID
  120.      * @param id
  121.      * @return object    Filter instance
  122.      */
  123.     public static function findById($intId$arrOptions=array())
  124.     {
  125.         // look up from Cache
  126.         if(Cache::getAttribute($intId))
  127.         {
  128.             return Cache::getAttribute($intId);
  129.         }
  130.         
  131.         $objModel AttributeModel::findByPk($intId,$arrOptions);
  132.         if($objModel === null)
  133.         {
  134.             return null;
  135.         }
  136.         
  137.         $objReturn = static::create$objModel );
  138.         if($objReturn === null)
  139.         {
  140.             return null;
  141.         }
  142.         
  143.         // add to cache
  144.         Cache::addAttribute($objReturn->id$objReturn);
  145.         Cache::addAttribute($objReturn->uuid$objReturn);
  146.         return $objReturn;
  147.     }
  148.     
  149.     
  150.     /**
  151.      * Fetch an attribute by its id
  152.      * @param integer
  153.      * @return object    \PCT\CustomElements\Models\AttributeModel
  154.      */
  155.     public static function fetchById($intId$arrOptions=array())
  156.     {
  157.         return AttributeModel::findByPk($intId$arrOptions);    
  158.     }
  159.     
  160.     
  161.     /**
  162.      * Generate the published attribute by a given id return the Attribute object
  163.      * @param integer
  164.      * @return object Attribute
  165.      */
  166.     public static function findPublishedById($intId$arrOptions=array())
  167.     {
  168.         // look up from Cache
  169.         if(Cache::getAttribute($intId))
  170.         {
  171.             $objReturn Cache::getAttribute($intId);
  172.             if((boolean)$objReturn->get('published') === false )
  173.             {
  174.                 return null;
  175.             }
  176.             return $objReturn;
  177.         }
  178.             
  179.         $objModel AttributeModel::findPublishedById($intId,$arrOptions);
  180.         if($objModel === null)
  181.         {
  182.             return null;
  183.         }
  184.         
  185.         $objReturn = static::create$objModel );
  186.         if($objReturn === null)
  187.         {
  188.             return null;
  189.         }
  190.         
  191.         // add to cache
  192.         Cache::addAttribute($objReturn->id$objReturn);
  193.         Cache::addAttribute($objReturn->uuid$objReturn);
  194.         
  195.         return $objReturn;
  196. }
  197.     
  198.     
  199.     /**
  200.      * Fetch the published attribute by a given id return the database result object
  201.      * @param integer
  202.      * @return object Attribute
  203.      */
  204.     public static function fetchPublishedById($intId$arrOptions=array())
  205.     {
  206.         return AttributeModel::findPublishedById($intId$arrOptions);
  207.     }
  208.     
  209.     
  210.     /**
  211.      * Fetch the attributes by a given set of ids return the database result object
  212.      * @param integer
  213.      * @return object Attribute
  214.      */
  215.     public static function fetchMultipleById($arrIds$arrOptions=array())
  216.     {
  217.         if (!isset($arrOptions['order']))
  218.         {
  219.             $arrOptions['order'] = "tl_pct_customelement_attribute.sorting";
  220.         }
  221.         
  222.         return AttributeModel::findMultipleByIds($arrIds,$arrOptions);
  223.     }
  224.     
  225.     
  226.     /**
  227.      * Generate the attribute by a given id return the Attribute object
  228.      * @param integer
  229.      * @return object Attribute
  230.      */
  231.     public static function findByUuid($strUuid$arrOptions=array())
  232.     {
  233.         // look up from Cache
  234.         if(Cache::getAttribute($strUuid))
  235.         {
  236.             return Cache::getAttribute($strUuid);
  237.         }
  238.         
  239.         $objModel AttributeModel::findByUuid($strUuid,$arrOptions);
  240.         if($objModel === null)
  241.         {
  242.             return null;
  243.         }
  244.         
  245.         $objReturn = static::create$objModel );
  246.         if($objReturn === null)
  247.         {
  248.             return null;
  249.         }
  250.         
  251.         // add to cache
  252.         Cache::addAttribute($objReturn->id$objReturn);
  253.         Cache::addAttribute($strUuid$objReturn);
  254.         
  255.         return $objReturn;
  256.     }
  257.     
  258.     
  259.     /**
  260.      * Generate the attribute by a given alias return the Attribute object
  261.      * @param integer
  262.      * @return object Attribute
  263.      */
  264.     public static function findByAlias($strAlias$arrOptions=array())
  265.     {
  266.         $objModel AttributeModel::findByAlias($strAlias,$arrOptions);
  267.         if($objModel === null)
  268.         {
  269.             return null;
  270.         }
  271.         
  272.         $objReturn = static::create$objModel );
  273.         if($objReturn === null)
  274.         {
  275.             return null;
  276.         }
  277.         
  278.         // add to cache
  279.         Cache::addAttribute($objReturn->id$objReturn);
  280.         Cache::addAttribute($objReturn->uuid$objReturn);
  281.         
  282.         return $objReturn;
  283.     }
  284.     
  285.     /**
  286.      * Fetch the attribute by a given alias return the Attribute object
  287.      * @param integer
  288.      * @return object Attribute
  289.      */
  290.     public static function fetchByAlias($strAlias$arrOptions=array())
  291.     {
  292.         return AttributeModel::findByAlias($strAlias,$arrOptions);    
  293.     }
  294.     
  295.     
  296.     /**
  297.      * Find an attribute by its alias in a customelement by ID or Alias
  298.      * @param string
  299.      * @param integer|string        Id or Alias of the CE
  300.      * @return object
  301.      */
  302.     public static function findByAliasAndCustomElement($strAlias,$varCE,$arrOptions=array())
  303.     {
  304.         // look up from cache
  305.         if(Cache::getAttribute('ce_'.$varCE.'_'.$strAlias))
  306.         {
  307.             return Cache::getAttribute('ce_'.$varCE.'_'.$strAlias);
  308.         }
  309.         
  310.         $objCE \PCT\CustomElements\Core\CustomElementFactory::findByIdOrAlias($varCE,$arrOptions);
  311.         if($objCE === null)
  312.         {
  313.             return null;
  314.         }
  315.         // handle copies
  316.         $isCopy false;
  317.         if(strlen(strpos($strAlias'#')) > 0)
  318.         {
  319.             $arr explode('#'$strAlias);
  320.             $strAlias $arr[0];
  321.             $isCopy true;
  322.         }
  323.         
  324.         $strCEwhere "SELECT id FROM tl_pct_customelement WHERE id=".$objCE->id;
  325.         
  326.         $strQuery "tl_pct_customelement_attribute.alias=? AND pid IN(SELECT id FROM tl_pct_customelement_group WHERE pid IN(".$strCEwhere.") GROUP BY id)";
  327.         
  328.         $objCollection AttributeModel::findByCustomSql($strQuery,array($strAlias));
  329.         if($objCollection === null)
  330.         {
  331.             return null;
  332.         }
  333.         
  334.         $arrModels $objCollection->getModels();
  335.         
  336.         $objReturn = static::create$arrModels[0] );
  337.         
  338.         // add to cache
  339.         Cache::addAttribute($objReturn->id$objReturn);
  340.         Cache::addAttribute($objReturn->uuid$objReturn);
  341.         Cache::addAttribute('ce_'.$objCE->id.'_'.$strAlias$objReturn);
  342.         
  343.         return $objReturn;
  344.     }
  345.     
  346.     
  347.     /**
  348.      * Find an attribute by its uuid in a customelement by ID or Alias
  349.      * @param string
  350.      * @param integer|string        Id or Alias of the CE
  351.      * @return object
  352.      */
  353.     public static function findByUuidAndCustomElement($strUuid,$varCE,$arrOptions=array())
  354.     {
  355.         // look up from cache
  356.         if(Cache::getAttribute('ce_'.$varCE.'_'.$strUuid))
  357.         {
  358.             return Cache::getAttribute('ce_'.$varCE.'_'.$strUuid);
  359.         }
  360.         
  361.         $objCE \PCT\CustomElements\Core\CustomElementFactory::findByIdOrAlias($varCE,$arrOptions);
  362.         if($objCE === null)
  363.         {
  364.             return null;
  365.         }
  366.         $strCEwhere "SELECT id FROM tl_pct_customelement WHERE id=".$objCE->id;
  367.         
  368.         $strQuery "tl_pct_customelement_attribute.uuid=? AND pid IN(SELECT id FROM tl_pct_customelement_group WHERE pid IN(".$strCEwhere.") GROUP BY id)";
  369.         
  370.         $objCollection AttributeModel::findByCustomSql($strQuery,array($strUuid));
  371.         if($objCollection === null)
  372.         {
  373.             return null;
  374.         }
  375.         
  376.         $arrModels $objCollection->getModels();
  377.         
  378.         $objReturn = static::create$arrModels[0] );
  379.         
  380.         // add to cache
  381.         Cache::addAttribute($objReturn->id$objReturn);
  382.         Cache::addAttribute($objReturn->uuid$objReturn);
  383.         Cache::addAttribute('ce_'.$objCE->id.'_'.$strUuid$objReturn);
  384.         Cache::addAttribute('ce_'.$objCE->alias.'_'.$strUuid$objReturn);
  385.         
  386.         return $objReturn;
  387.     }
  388.     
  389.     
  390.     /**
  391.      * Generate all attributes in a group and return them as array
  392.      * @param integer
  393.      * @return array
  394.      */
  395.     public static function findByParentId($intPid$arrOptions=array())
  396.     {
  397.         $arrCache Cache::getData();
  398.         
  399.         // look up from cache
  400.         $strKey 'AttributeFactory::'.__FUNCTION__;
  401.         if(isset($arrCache['ATTRIBUTE'][$strKey][$intPid]) && $arrCache['ATTRIBUTE'][$strKey][$intPid] !== null)
  402.         {
  403.             return $arrCache['ATTRIBUTE'][$strKey][$intPid];
  404.         }
  405.         
  406.         
  407.         $objCollection AttributeModel::findByPid($intPid,$arrOptions);
  408.         if($objCollection === null)
  409.         {
  410.             return null;
  411.         }
  412.         
  413.         $arrReturn = array();
  414.         foreach($objCollection as $objModel)
  415.         {
  416.             $objAttribute = static::create$objModel );
  417.             if($objAttribute === null)
  418.             {
  419.                 continue;
  420.             }
  421.             
  422.             $arrReturn[] = $objAttribute;
  423.             
  424.             // add to cache
  425.             Cache::addAttribute($objAttribute->id$objAttribute);
  426.             Cache::addAttribute($objAttribute->uuid$objAttribute);
  427.             Cache::addAttribute('group_'.$intPid.'_'.$objAttribute->alias$objAttribute);
  428.         }
  429.         
  430.         // add to cache
  431.         $arrCache['ATTRIBUTE'][$strKey][$intPid] = $arrReturn;
  432.         Cache::setData($arrCache);
  433.         
  434.         return $arrReturn;
  435.     }
  436.     
  437.     
  438.     /**
  439.      * Generate all attributes in a group and return them as array
  440.      * @param integer
  441.      * @return array
  442.      */
  443.     public static function findByAliasAndPid($strAlias$intPid$arrOptions=array())
  444.     {
  445.         // look up from cache
  446.         if(Cache::getAttribute('group_'.$intPid.'_'.$strAlias))
  447.         {
  448.             return Cache::getAttribute('group_'.$intPid.'_'.$strAlias);
  449.         }
  450.         
  451.         $objModel AttributeModel::findByAliasAndPid($strAlias$intPid,$arrOptions);
  452.         if($objModel === null)
  453.         {
  454.             return null;
  455.         }
  456.         
  457.         $objReturn = static::create$objModel );
  458.         if($objReturn === null)
  459.         {
  460.             return null;
  461.         }
  462.         
  463.         // add to cache
  464.         Cache::addAttribute($objReturn->id$objReturn);
  465.         Cache::addAttribute($objReturn->uuid$objReturn);
  466.         Cache::addAttribute('group_'.$intPid.'_'.$objReturn->alias$objReturn);
  467.         return $objReturn;
  468.     }
  469.     
  470.     
  471.     /**
  472.      * Generate all attributes in a group and return them as array
  473.      * @param integer
  474.      * @return array
  475.      */
  476.     public static function findPublishedByParentId($intPid$arrOptions=array())
  477.     {
  478.         $arrCache Cache::getData();
  479.         
  480.         // look up from cache
  481.         $strKey 'AttributeFactory::'.__FUNCTION__;
  482.         if( isset($arrCache['ATTRIBUTE'][$strKey][$intPid]) && $arrCache['ATTRIBUTE'][$strKey][$intPid] !== null)
  483.         {
  484.             return $arrCache['ATTRIBUTE'][$strKey][$intPid];
  485.         }
  486.         
  487.         $objCollection AttributeModel::findPublishedByPid($intPid$arrOptions);
  488.         if($objCollection === null)
  489.         {
  490.             return array();
  491.         }
  492.         
  493.         $arrReturn = array();
  494.         foreach($objCollection as $objModel)
  495.         {
  496.             $objAttribute = static::create$objModel );
  497.             if($objAttribute === null)
  498.             {
  499.                 continue;
  500.             }
  501.             
  502.             $arrReturn[] = $objAttribute;
  503.             
  504.             // add to cache
  505.             Cache::addAttribute($objAttribute->id$objAttribute);
  506.             Cache::addAttribute($objAttribute->uuid$objAttribute);
  507.             Cache::addAttribute('ce_'.$intPid.'_'.$objAttribute->alias$objAttribute);
  508.         }
  509.         
  510.         // add to cache
  511.         $arrCache['ATTRIBUTE'][$strKey][$intPid] = $arrReturn;
  512.         Cache::setData($arrCache);
  513.         
  514.         return $arrReturn;
  515.     }
  516.     
  517.     
  518.     /**
  519.      * Return the attribute (mainly a copy) by its alias and origin information
  520.      * @param string
  521.      * @param integer
  522.      * @param string
  523.      * @return object
  524.      */
  525.     public static function findByAliasAndOrigin($strAlias,$intPid,$strTable)
  526.     {
  527.         $arrData = array();
  528.         
  529.         $objDatabase Database::getInstance();
  530.         if(Cache::getWizard($intPid,$strTable))
  531.         {
  532.             $arrData StringUtil::deserialize(Cache::getWizard($intPid,$strTable));
  533.         }
  534.         else if( $objDatabase->tableExists('tl_pct_customelement_vault') )
  535.         {
  536.             // check if there is a wizzard data for this record
  537.             $objWizard \Contao\Database::getInstance()->prepare("SELECT * FROM tl_pct_customelement_vault WHERE pid=? AND source=? AND type=?")->limit(1)->execute($intPid,$strTable,'wizard');
  538.             if($objWizard->numRows 1)
  539.             {
  540.                 return null;
  541.             }
  542.         
  543.             $arrData StringUtil::deserialize($objWizard->data_blob);
  544.         }
  545.         
  546.         if(empty($arrData))
  547.         {
  548.             return null;
  549.         }
  550.         
  551.         foreach($arrData as $k => $data)
  552.         {
  553.             if(!is_array($data)) {continue;}
  554.             // work on copies only
  555.             if(!$data['isCopy'] || empty($data['attributes'])) {continue;}
  556.             foreach($data['attributes'] as $attribute)
  557.             {
  558.                 if($attribute['alias'] == $strAlias)
  559.                 {
  560.                     // generate parent attribute
  561.                     $objAttribute AttributeFactory::findById($attribute['id']);
  562.                     // set uuid to copy uuid
  563.                     $objAttribute->set('uuid',$attribute['uuid']);
  564.                 }
  565.             }
  566.         }
  567.         
  568.         return $objAttribute;
  569.     }
  570.     
  571.     
  572.     /**
  573.      * Return the attribute by its uuid located in a wizard. Finds clones
  574.      * @param string
  575.      * @param integer
  576.      * @param string
  577.      * @return object|null
  578.      */
  579.     public static function findCloneByUuidAndOrigin($strUuid,$intPid,$strTable,$intAttribute=0)
  580.     {
  581.         $arrWizard \PCT\CustomElements\Core\Vault::getWizardData($intPid,$strTable,$intAttribute);
  582.         if( isset($arrWizard['clones']) && is_array($arrWizard['clones']))
  583.         {
  584.             foreach($arrWizard['clones'] as $attr_id => $arrUuids)
  585.             {
  586.                 if(empty($arrUuids) || !is_array($arrUuids))
  587.                 {
  588.                     continue;
  589.                 }
  590.                 
  591.                 if(in_array($strUuid$arrUuids))
  592.                 {
  593.                     return \PCT\CustomElements\Core\AttributeFactory::findById($attr_id);
  594.                 }
  595.             }
  596.         }
  597.         return null;
  598.     }
  599.     
  600.     
  601.     /**
  602.      * Find attributes by the custom element
  603.      * @param integer
  604.      * @return array
  605.      */
  606.     public static function findMultipleByCustomElement($intId$arrOptions=array())
  607.     {
  608.         $arrCache Cache::getData();
  609.         
  610.         $strKey 'AttributeFactory::'.__FUNCTION__;
  611.         if( isset($arrCache['ATTRIBUTE'][$strKey][$intId]) && $arrCache['ATTRIBUTE'][$strKey][$intId] !== null)
  612.         {
  613.             return $arrCache['ATTRIBUTE'][$strKey][$intId];
  614.         }
  615.         
  616.         $strQuery "tl_pct_customelement_attribute.pid IN(SELECT id FROM tl_pct_customelement_group WHERE pid=".$intId.")";
  617.         $objCollection AttributeModel::findByCustomSql($strQuery,array(),$arrOptions);
  618.         if($objCollection === null)
  619.         {
  620.             return array();
  621.         }
  622.         
  623.         $arrReturn = array();
  624.         foreach($objCollection as $objModel)
  625.         {
  626.             $arrReturn[] = static::create$objModel );
  627.         }
  628.         
  629.         // add to cache
  630.         $arrCache['ATTRIBUTE'][$strKey][$intId] = $arrReturn;
  631.         Cache::setData($arrCache);
  632.         
  633.         return $arrReturn;
  634.     }
  635.     
  636.     
  637.     /**
  638.      * Find attributes by the custom element
  639.      * @param integer
  640.      * @return object    Database Result
  641.      */
  642.     public static function fetchMultipleByCustomElement($intId$arrOptions=array())
  643.     {
  644.         $strQuery "tl_pct_customelement_attribute.pid IN(SELECT id FROM tl_pct_customelement_group WHERE pid=".$intId.")";
  645.         return AttributeModel::findByCustomSql($strQuery,array(),$arrOptions);
  646.     }
  647.     
  648.     
  649.     /**
  650.      * Find attributes by the custom element
  651.      * @param integer
  652.      * @return array
  653.      */
  654.     public static function findMultiplePublishedByCustomElement($intId$arrOptions=array())
  655.     {
  656.         $arrCache Cache::getData();
  657.         
  658.         $strKey 'AttributeFactory::'.__FUNCTION__;
  659.         if($arrCache['ATTRIBUTE'][$strKey][$intId] !== null)
  660.         {
  661.             return $arrCache['ATTRIBUTE'][$strKey][$intId];
  662.         }
  663.         
  664.         $strQuery "tl_pct_customelement_attribute.pid IN(SELECT id FROM tl_pct_customelement_group WHERE pid=".$intId.") AND tl_pct_customelement_attribute.published=1";
  665.         $objCollection AttributeModel::findByCustomSql($strQuery,array(),$arrOptions);
  666.         if($objCollection === null)
  667.         {
  668.             return array();
  669.         }
  670.         
  671.         $arrReturn = array();
  672.         foreach($objCollection as $objModel)
  673.         {
  674.             $arrReturn[] = static::create$objModel );
  675.         }
  676.         
  677.         // add to cache
  678.         $arrCache['ATTRIBUTE'][$strKey][$intId] = $arrReturn;
  679.         Cache::setData($arrCache);
  680.         
  681.         return $arrReturn;
  682.     }
  683.     
  684.     
  685.     /**
  686.      * Find attributes by the custom element
  687.      * @param integer
  688.      * @return object    Database Result
  689.      */
  690.     public static function fetchMultiplePublishedByCustomElement($intId$arrOptions=array())
  691.     {
  692.         $strQuery "tl_pct_customelement_attribute.pid IN(SELECT id FROM tl_pct_customelement_group WHERE pid=".$intId.") AND tl_pct_customelement_attribute.published=1";
  693.         return AttributeModel::findByCustomSql($strQuery,array(),$arrOptions);
  694.     }
  695.         
  696.     
  697.     /**
  698.      * Return the class defintion for an attribute by its type name
  699.      * @param string
  700.      * @return string
  701.      */
  702.     public static function getAttributeClassByType($strName)
  703.     {
  704.         return $GLOBALS['PCT_CUSTOMELEMENTS']['ATTRIBUTES'][$strName]['class'];
  705.     }
  706.     
  707.     
  708.     /**
  709.      * Return the system columns as array
  710.      * @return array
  711.      */
  712.     public static function getSystemColumns()
  713.     {
  714.         return array('id','pid','tstamp','sorting');
  715.     }
  716.     
  717.     
  718.     /**
  719.      * Return a unique generic name
  720.      * @param object DataContainer
  721.      * @param string
  722.      * @param integer
  723.      * @return object DataContainer
  724.      */
  725.     public static function generateUuid($objDC$intLength=15$strCharSet='1234567890abcdefghijklmnopqrstuvwxyz')
  726.     {
  727.         $objDatabase \Contao\Database::getInstance();
  728.         
  729.         $strTable $objDC->table;
  730.         if(isset($objDC->ref_table))
  731.         {
  732.             $strTable $objDC->ref_table;
  733.         }
  734.         
  735.         $objUuid $objDatabase->prepare("SELECT id,uuid FROM ".$strTable." WHERE id=?")->limit(1)->execute($objDC->id);
  736.         if($objUuid->numRows && strlen($objUuid->uuid) > 0)
  737.         {
  738.             return $objDC;
  739.         }
  740.         
  741.         $strUuid = static::generatePassword($intLength,$strCharSet);
  742.         
  743.         $objSiblings $objDatabase->prepare("SELECT id FROM ".$strTable." WHERE uuid=? AND id!=?")->limit(1)->execute($strUuid,$objDC->id);
  744.         // generate a new uuid this one is taken
  745.         if($objSiblings->numRows 0)
  746.         {
  747.             return static::generateUuid($objDC,$intLength+1);
  748.         }
  749.         
  750.         return $strUuid;
  751.     }
  752.     
  753.     /**
  754.      * Generate a random string
  755.      * @param integer
  756.      * @return string
  757.      */
  758.     public static function generatePassword($intLength=8,$strCharSet='')
  759.     {
  760.         if(!$strCharSet)
  761.         {
  762.             $strCharSet "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  763.         }
  764.         
  765.         $max strlen($strCharSet);
  766.         $rand rand(0,$max);
  767.         
  768.         $return '';
  769.         for($i 0$i <= $intLength-1$i++)
  770.         {
  771.             $rand rand(0,$max);
  772.             $return .= substr($strCharSet$rand,1);
  773.         }
  774.         
  775.         return $return;
  776.     }
  777.     
  778.         
  779.     /**
  780.      * Get the attribute id in the session registry by the generic name
  781.      * @param string
  782.      * @param integer
  783.      * @param string
  784.      * @return integer
  785.      */
  786.     public static function getAttributeIdFromSession($strName,$intElement=0,$strTable='')
  787.     {
  788.         $objSession \Contao\System::getContainer()->get('session');
  789.         $arrSession =  $objSession->get('pct_customelements');
  790.         $arrRegistry $arrSession['registry'];
  791.         
  792.         // use the global as fallback if session fails
  793.         if(!$arrSession['registry'])
  794.         {
  795.             $arrRegistry $GLOBALS['PCT_CUSTOMELEMENTS_REGISTRY'];
  796.         }
  797.         
  798.         if(!$strTable)
  799.         {
  800.             $strTable \Contao\Input::get('table');
  801.         }
  802.         
  803.         if($intElement 1)
  804.         {
  805.             $intElement \Contao\Input::get('id');
  806.         }
  807.         
  808.         if(empty($arrRegistry[$strTable]))
  809.         {
  810.             return null;
  811.         }
  812.                 
  813.         return $arrRegistry[$strTable][$intElement][$strName]['id'];        
  814.     }
  815. }