system/modules/pct_customelements/PCT/CustomElements/Core/SystemIntegration.php line 170

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 2015, 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\Message;
  20. use PCT\CustomElements\Core\Cache as Cache;
  21. use Contao\Controller;
  22. use Contao\System;
  23. /**
  24.  * Class file
  25.  * SystemIntegration
  26.  */
  27. class SystemIntegration
  28. {
  29.     /**
  30.      * Initialize
  31.      */
  32.     public function initSystem()
  33.     {
  34.         $blnInstallTool false;
  35.         if(strlen(strpos(\Contao\Environment::get('scriptName'), '/contao/install.php')) > || strlen(strpos(\Contao\Environment::get('requestUri'), '/contao/install')) > 0)
  36.         {
  37.             $blnInstallTool true;
  38.         }
  39.         
  40.         $objDatabase \Contao\Database::getInstance();
  41.         $objRegistry \Contao\Model\Registry::getInstance();
  42.         $arrCache Cache::getData();
  43.         
  44.         if(!$objDatabase->tableExists('tl_pct_customelement'))
  45.         {
  46.             return;
  47.         }
  48.         
  49.         // register backend module keys
  50.         $bundles array_keys(\Contao\System::getContainer()->getParameter('kernel.bundles'));
  51.         if( TL_MODE == 'BE' && (\in_array('news',$bundles) || \in_array('ContaoNewsBundle',$bundles)) )
  52.         {
  53.             $GLOBALS['BE_MOD']['content']['news']['contentelementset'] = array('PCT\CustomElements\Backend\PageContentElementSet','run'); 
  54.         }
  55.         if( TL_MODE == 'BE' && \in_array('calendar'$bundles) )
  56.         {
  57.             $GLOBALS['BE_MOD']['content']['calendar']['contentelementset'] = array('PCT\CustomElements\Backend\PageContentElementSet','run'); 
  58.         }
  59.         
  60.         //-- Cache CEs
  61.         $objCustomElements $objDatabase->prepare("SELECT * FROM tl_pct_customelement WHERE alias!='' AND tstamp > 0 ORDER BY title")->execute();
  62.                 
  63.         if($objCustomElements->numRows 0)
  64.         {
  65.             while($objCustomElements->next())
  66.             {
  67.                 $strAlias $objCustomElements->alias;
  68.                 if($objCustomElements->isCTE)
  69.                 {
  70.                     // use default class
  71.                     if(!isset($GLOBALS['TL_CTE']['pct_customelements_node'][$strAlias]))
  72.                     {
  73.                         $GLOBALS['TL_CTE']['pct_customelements_node'][$strAlias] = $GLOBALS['PCT_CUSTOMELEMENTS']['TL_CTE'];
  74.                     }
  75.                     // set name
  76.                     if( !isset($GLOBALS['TL_LANG']['CTE'][$strAlias]) )
  77.                     {
  78.                         $GLOBALS['TL_LANG']['CTE'][$strAlias] = array($objCustomElements->title,$strAlias);
  79.                     }
  80.                     // store the alias
  81.                     $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['ACTIVE']['tl_content']['alias'][] = $strAlias;
  82.                 }
  83.                 if($objCustomElements->isFMD)
  84.                 {
  85.                     // use default class
  86.                     if(  !isset($GLOBALS['FE_MOD']['pct_customelements_node'][$strAlias]) )
  87.                     {
  88.                         $GLOBALS['FE_MOD']['pct_customelements_node'][$strAlias] = $GLOBALS['PCT_CUSTOMELEMENTS']['TL_FMD'];
  89.                     }
  90.                     if( !isset($GLOBALS['TL_LANG']['FMD'][$strAlias]) )
  91.                     {
  92.                         $GLOBALS['TL_LANG']['FMD'][$strAlias] = array();
  93.                     }
  94.                     // set name
  95.                     $GLOBALS['TL_LANG']['FMD'][$strAlias] = array($GLOBALS['TL_LANG']['FMD'][$strAlias] ?: $objCustomElements->title,$strAlias);
  96.                     
  97.                     // store the alias
  98.                     $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['ACTIVE']['tl_module']['alias'][] = $strAlias;
  99.                 }
  100.                 
  101.                 $objCE \PCT\CustomElements\Core\CustomElementFactory::create($objCustomElements);
  102.                 
  103.                 // add CE to the cache
  104.                 Cache::addCustomElement($objCE->id,$objCE);
  105.                 Cache::addCustomElement($objCE->alias,$objCE);
  106.                 
  107.                 // set Wrapper elements
  108.                 if($objCustomElements->isWrapper == 'start')
  109.                 {
  110.                     $GLOBALS['PCT_CUSTOMELEMENTS_WRAPPERS']['start'][] = $strAlias;
  111.                 }
  112.                 if($objCustomElements->isWrapper == 'stop')
  113.                 {
  114.                     $GLOBALS['PCT_CUSTOMELEMENTS_WRAPPERS']['stop'][] = $strAlias;
  115.                 }
  116.                 if($objCustomElements->isWrapper == 'divider')
  117.                 {
  118.                     $GLOBALS['PCT_CUSTOMELEMENTS_WRAPPERS']['separator'][] = $strAlias;
  119.                 }
  120.                 
  121.                 // reset the group name to avoid "Meine Inhaltselemente (-CE-Title-)"
  122.                 if( in_array($objCustomElements->isWrapper, array('start','stop')) && \Contao\Input::get('act') == '' )
  123.                 {
  124.                     $GLOBALS['TL_LANG']['CTE']['pct_customelements_node'] = '';
  125.                 }
  126.                 
  127.             }
  128.         }
  129.         
  130.         if($objCustomElements->numRows 0)
  131.         {
  132.             //-- Cache groups
  133.             $objGroups $objDatabase->prepare("SELECT * FROM tl_pct_customelement_group WHERE tstamp > 0 AND pid IN(".implode(',',$objCustomElements->fetchEach('id')).")")->execute();
  134.             if($objGroups->numRows 0)
  135.             {
  136.                 while($objGroups->next())
  137.                 {
  138.                     $objGroup \PCT\CustomElements\Core\GroupFactory::create($objGroups);
  139.                     if($objGroup === null)
  140.                     {
  141.                         continue;
  142.                     }
  143.                     
  144.                     $objCE \PCT\CustomElements\Core\CustomElementFactory::findById($objGroup->pid);
  145.                     
  146.                     Cache::addGroup($objGroup->id,$objGroup);
  147.                     Cache::addGroup('ce_'.$objCE->id.'_'.$objGroup->alias,$objGroup);
  148.                     Cache::addGroup('ce_'.$objCE->alias.'_'.$objGroup->alias,$objGroup);
  149.                 }
  150.             }
  151.         
  152.             if($objGroups->numRows 0)
  153.             {
  154.                 //-- Cache attributes
  155.                 $objAttributes $objDatabase->prepare("SELECT * FROM tl_pct_customelement_attribute WHERE tstamp > 0 AND pid IN(".implode(',',$objGroups->fetchEach('id')).")")->execute();
  156.                 if($objAttributes->numRows 0)
  157.                 {
  158.                     while($objAttributes->next())
  159.                     {
  160.                         $objAttribute \PCT\CustomElements\Core\AttributeFactory::create($objAttributes);
  161.                         if($objAttribute === null)
  162.                         {
  163.                             continue;
  164.                         }
  165.                         $objGroup \PCT\CustomElements\Core\GroupFactory::findById($objAttributes->pid);
  166.                         $objCE \PCT\CustomElements\Core\CustomElementFactory::findById($objGroup->pid);
  167.                         Cache::addAttribute($objAttribute->id,$objAttribute);
  168.                         Cache::addAttribute('ce_'.$objCE->id.'_'.$objAttribute->alias$objAttribute );
  169.                         if( isset($objAttribute->uuid) && !empty($objAttribute->uuid) )
  170.                         {
  171.                             Cache::addAttribute($objAttribute->uuid,$objAttribute);
  172.                             Cache::addAttribute('ce_'.$objCE->id.'_'.$objAttribute->uuid$objAttribute );
  173.                             Cache::addAttribute('ce_'.$objCE->alias.'_'.$objAttribute->uuid$objAttribute );
  174.                         }    
  175.                     }
  176.                 }
  177.             }
  178.         }
  179.         
  180.         //-- Cache wizards
  181.         #if(TL_MODE == 'FE' || $GLOBALS['PCT_CUSTOMELEMENTS']['cacheWizardsInBackend'])
  182.         #{
  183.         #    $blnCache = true;
  184.         #    
  185.         #    // do not run caching when not in edit modes
  186.         #    if(TL_MODE == 'BE' && !in_array(\Contao\Input::get('act'), array('edit','editAll','overrideAll')) )
  187.         #    {
  188.         #        $blnCache = false;
  189.         #    }
  190.         #    
  191.         #    if($blnCache)
  192.         #    {
  193.         #        global $objPage;
  194.         #        $objDatabase = \Contao\Database::getInstance();
  195.         #        $objWizards = null;
  196.         #
  197.         #        $arrSource = array_unique(array_diff($GLOBALS['PCT_CUSTOMELEMENTS']['allowedTables'],array('tl_page','tl_article','tl_news_archive')));
  198.         #        if(TL_MODE == 'BE' && \Contao\Input::get('table'))
  199.         #        {
  200.         #            $arrSource = array(\Contao\Input::get('table'));
  201.         #        }
  202.         #        
  203.         #        $objStmt = $objDatabase->prepare("
  204.         #            SELECT * FROM tl_pct_customelement_vault 
  205.         #            WHERE type=? 
  206.         #                AND data_blob IS NOT NULL 
  207.         #                AND ".$objDatabase->findInSet('source',$arrSource).
  208.         #                ((int)\Contao\Input::get('id') > 0 && TL_MODE == 'BE' ? " AND pid=".(int)\Contao\Input::get('id') : "")
  209.         #        );
  210.         #        
  211.         #        if($GLOBALS['PCT_CUSTOMELEMENTS']['cacheLimit']['wizard'] > 0)
  212.         #        {
  213.         #            $objStmt->limit($GLOBALS['PCT_CUSTOMELEMENTS']['cacheLimit']['wizard']);
  214.         #        }
  215.         #        
  216.         #        $objWizards = $objStmt->execute('wizard');
  217.         #        if($objWizards->numRows > 0)
  218.         #        {
  219.         #            while($objWizards->next())
  220.         #            {
  221.         #                Cache::addWizard($objWizards->pid,$objWizards->source,$objWizards->attr_id,$objWizards->data_blob);
  222.         #            }
  223.         #        }
  224.         #    }
  225.         #}
  226.     }
  227.     
  228.     
  229.     /**
  230.      * Store the Ids of the generated customelements
  231.      * @param object
  232.      * @param boolean
  233.      * @return boolean
  234.      */
  235.     public function onIsVisibleElement($objElement$blnIsVisible)
  236.     {
  237.         if(!$blnIsVisible)
  238.         {
  239.             return $blnIsVisible;
  240.         }
  241.         
  242.         $strTable $objElement->getTable();
  243.         $strType $objElement->type;
  244.         
  245.         // include module
  246.         if($strType == 'module' && $strTable == 'tl_content')
  247.         {
  248.             $objModuleModel \Contao\ModuleModel::findByPk($objElement->module);
  249.             return $this->onIsVisibleElement($objModuleModel$blnIsVisible);
  250.         }
  251.         
  252.         if(!is_array($GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['ACTIVE'][$strTable]['alias']))
  253.         {
  254.             $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['ACTIVE'][$strTable]['alias'] = array();
  255.         }
  256.         
  257.         // remember the id of the element to laod the wizard data later
  258.         if(in_array($strType$GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['ACTIVE'][$strTable]['alias']))
  259.         {
  260.             $GLOBALS['PCT_CUSTOMELEMENTS']['CACHE']['ACTIVE'][$strTable]['ids'][] = $objElement->id;
  261.         }
  262.         
  263.         return $blnIsVisible;
  264.     }
  265.     /**
  266.      * Checks if the deprected vault sill contains extries and displays a backend info.
  267.      */
  268.     public function showVaultMigrationAlert()
  269.     {
  270.         if( TL_MODE != 'BE' )
  271.         {
  272.             return;
  273.         }
  274.         $objDatabase \Contao\Database::getInstance();
  275.         if( !$objDatabase->tableExists('tl_pct_customelement_vault') )
  276.         {
  277.             unset( $GLOBALS['PCT_CUSTOMELEMENTS']['MAINTENANCE']['resolveVault'] );
  278.             return;
  279.         }
  280.         
  281.         $objUser System::importStatic('Contao\BackendUser''User');
  282.         if( $objUser->id === null )
  283.         {
  284.             return;
  285.         }
  286.         $objResult $objDatabase->prepare("SELECT COUNT(*) as count FROM tl_pct_customelement_vault WHERE ".$objDatabase->findInSet('source',$GLOBALS['PCT_CUSTOMELEMENTS']['allowedTables']))->execute();
  287.         if( $objResult->count )
  288.         {
  289.             return;
  290.         }
  291.         
  292.         System::loadLanguageFile('default');
  293.         Message::addError('<a href="'.Controller::addToUrl('do=maintenance').'">'.$GLOBALS['TL_LANG']['PCT_CUSTOMELEMENTS']['migrateVaultAlert'].'</a>');
  294.     }
  295. }