system/modules/pct_themer/PCT/Themer.php line 296

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 2016, Premium Contao Themes
  8.  * @author        Tim Gatzky <info@tim-gatzky.de>
  9.  * @package        pct_themer
  10.  */
  11. /**
  12.  * Namespace
  13.  */
  14. namespace PCT;
  15. /**
  16.  * Class file
  17.  * Themer
  18.  *
  19.  * Provide various methods to apply theme data
  20.  */
  21. class Themer
  22. {
  23.     /**
  24.      * Theme Demo name
  25.      * @var string
  26.      */
  27.     protected $strTheme '';
  28.     
  29.     /**
  30.      * Root page id
  31.      * @var integer
  32.      */
  33.     protected $intRoot 0;
  34.     
  35.     /**
  36.      * Root page object
  37.      * @var object
  38.      */
  39.     protected $objRoot;
  40.     
  41.     
  42.     /**
  43.      * Set the theme name
  44.      * @var string
  45.      */
  46.     public function setName($strValue)
  47.     {
  48.         $this->strTheme $strValue;
  49.     }
  50.     
  51.     
  52.     /**
  53.      * Return the theme name
  54.      * @return string
  55.      */
  56.     public function getName()
  57.     {
  58.         return $this->strTheme;
  59.     }
  60.     
  61.     
  62.     /**
  63.      * Set root page object
  64.      * @var object
  65.      */
  66.     public function setRootPage($objValue)
  67.     {
  68.         $this->objRoot $objValue;
  69.         $this->intRoot $objValue->id;
  70.     }
  71.     
  72.     
  73.     /**
  74.      * Return the root page id
  75.      * @return object
  76.      */
  77.     public function getRootPage()
  78.     {
  79.         return $this->objRoot;
  80.     }
  81.     /**
  82.      * Run the import for a root page
  83.      * @param integer    Root page id
  84.      * @return boolean
  85.      */
  86.     public function import($intRoot)
  87.     {
  88.         $obj = new \PCT\Themer\Import;
  89.         
  90.         // set the root page
  91.         $objRoot \Contao\PageModel::findByPk($intRoot);
  92.         $this->setRootPage($objRoot);
  93.         
  94.         // set the name
  95.         $strTheme $GLOBALS['PCT_THEMER']['THEMES'][$objRoot->pct_theme]['label'] ?: $objRoot->pct_theme;
  96.         $this->setName($strTheme);
  97.         
  98.         // run
  99.         return $obj->run($intRoot);
  100.     }
  101.     
  102.     
  103.     /**
  104.      * Build the query string
  105.      * @param array
  106.      * @return string
  107.      */
  108.     protected function buildInsert($strTable,$arrFields,$arrValues,$blnRowPerRow=true)
  109.     {
  110.         if(strlen($strTable) < || count($arrFields) < || count($arrValues) < 1)
  111.         {
  112.             return '';
  113.         }
  114.         
  115.         $strReturn ='INSERT INTO `'.$strTable.'`';
  116.         
  117.         $fields = array();
  118.         foreach($arrFields as $f)
  119.         {
  120.             $fields[] = '`'.$f.'`';
  121.         }
  122.         $strReturn .= '('.implode(','$fields).')';
  123.         
  124.         $strReturn .= ' VALUES ';
  125.         $values = array();
  126.         foreach($arrValues as $v)
  127.         {
  128.             $values[] = '`'.$v.'`';
  129.         }
  130.         $strReturn .= '('.implode(','$values).')';
  131.         
  132.         $strReturn .= '; ';
  133.         
  134.         return $strReturn;
  135.     }
  136.     
  137.     
  138.     /**
  139.      * Merge two arrays into one
  140.      * @param array        The new array to merge
  141.      * @param array        The array to merge into
  142.      */
  143.     public function mergeData($arrInput$arrHaystack=array())
  144.     {
  145.         if(count($arrInput) < 1)
  146.         {
  147.             return $arrHaystack;
  148.         }
  149.         
  150.         $diff array_diff_key($arrInput,$arrHaystack);
  151.         
  152.         // append the differences
  153.         if(is_array($diff) && count($diff) > 0)
  154.         {
  155.             $arrHaystack += $diff;
  156.         }
  157.         
  158.         // append existing keys
  159.         foreach($arrInput as $k => $v)
  160.         {
  161.             if(!is_array($v))
  162.             {
  163.                 $v = array($v);
  164.             }
  165.             
  166.             if(array_key_exists($k$arrHaystack))
  167.             {
  168.                 $arrHaystack[$k] = array_merge($arrHaystack[$k], $v);
  169.             }
  170.         }
  171.         
  172.         return $arrHaystack;
  173.     }
  174.     
  175. // ! -- Page, PageLayout
  176.     
  177.     /**
  178.      * Add theme files to the page
  179.      * @param object
  180.      * @param object
  181.      * called from: generatePage Hook
  182.      */
  183.     public function addThemeFiles($objTemplate)
  184.     {
  185.         if ( TL_MODE != 'FE' || \strpos($objTemplate->getName(), 'fe_page') === false 
  186.         {
  187.             return;
  188.         }
  189.         
  190.         global $objPage;
  191.         
  192.         $objRoot \Contao\PageModel::findByPk($objPage->rootId);
  193.         if(strlen($objRoot->pct_theme) < || !is_array($GLOBALS['PCT_THEMER']['THEMES'][$objRoot->pct_theme]))
  194.         {
  195.             return;
  196.         }
  197.         
  198.         //-- apply css
  199.         
  200.         if( \is_array($GLOBALS['PCT_THEMER']['THEMES'][$objRoot->pct_theme]['css']) === false )
  201.         {
  202.             $GLOBALS['PCT_THEMER']['THEMES'][$objRoot->pct_theme]['css'] = array();
  203.         }
  204.         if( !empty($GLOBALS['PCT_THEMER']['THEMES'][$objRoot->pct_theme]['css']) )
  205.         {
  206.             // combine more than one file
  207.             if(count($GLOBALS['PCT_THEMER']['THEMES'][$objRoot->pct_theme]['css']) > )
  208.             {
  209.                 $objCombiner = new \Contao\Combiner();
  210.                 foreach($GLOBALS['PCT_THEMER']['THEMES'][$objRoot->pct_theme]['css'] as $f)
  211.                 {
  212.                     $f str_replace('|static'''$f);
  213.                     $file PCT_THEMER_PATH.'/assets/'.$objRoot->pct_theme.'/css/'.$f;
  214.                     if( \file_exists(TL_ROOT.'/'.$file) )
  215.                     {
  216.                         $objCombiner->add($file);
  217.                     }
  218.                 }
  219.                 $objTemplate->pct_layout_css trim($objCombiner->getCombinedFile());
  220.             }
  221.             // single file
  222.             else
  223.             {
  224.                 $f str_replace('|static'''implode(''$GLOBALS['PCT_THEMER']['THEMES'][$objRoot->pct_theme]['css']));
  225.                 $file PCT_THEMER_PATH.'/assets/'.$objRoot->pct_theme.'/css/'.$f;
  226.                 if( \file_exists(TL_ROOT.'/'.$file) )
  227.                 {
  228.                     $objTemplate->pct_layout_css $file;
  229.                 }
  230.             }
  231.         }
  232.         
  233.         //-- apply scripts
  234.         
  235.         if( \is_array($GLOBALS['PCT_THEMER']['THEMES'][$objRoot->pct_theme]['scripts']) === false )
  236.         {
  237.             $GLOBALS['PCT_THEMER']['THEMES'][$objRoot->pct_theme]['scripts'] = array();
  238.         }
  239.         // combine more than one file
  240.         if( !empty($GLOBALS['PCT_THEMER']['THEMES'][$objRoot->pct_theme]['scripts']) )
  241.         {
  242.             if( count($GLOBALS['PCT_THEMER']['THEMES'][$objRoot->pct_theme]['scripts']) > )
  243.             {
  244.                 $objCombiner = new \Contao\Combiner(); 
  245.                 foreach($GLOBALS['PCT_THEMER']['THEMES'][$objRoot->pct_theme]['scripts'] as $f)
  246.                 {
  247.                     $f str_replace('|static'''$f);
  248.                     $file PCT_THEMER_PATH.'/assets/'.$objRoot->pct_theme.'/scripts/'.$f;
  249.                     if( \file_exists(TL_ROOT.'/'.$file) )
  250.                     {
  251.                         $objCombiner->add($file);
  252.                     }
  253.                 }
  254.                 $objTemplate->pct_layout_js trim($objCombiner->getCombinedFile());
  255.             }
  256.             // single file
  257.             else
  258.             {
  259.                 $f str_replace('|static'''implode(''$GLOBALS['PCT_THEMER']['THEMES'][$objRoot->pct_theme]['scripts']));
  260.                 $file PCT_THEMER_PATH.'/assets/'.$objRoot->pct_theme.'/scripts/'.$f;
  261.                 if( \file_exists(TL_ROOT.'/'.$file) )
  262.                 {
  263.                     $objTemplate->pct_layout_js $file;
  264.                 }
  265.             }
  266.         }
  267.     }
  268.     
  269.     
  270.     /**
  271.      * Add theme fonts to the page layout
  272.      * @param object
  273.      * @param object
  274.      * called from: getPageLayout Hook
  275.      */
  276.     public function addThemeFonts($objPage$objLayout)
  277.     {
  278.         $objRoot \Contao\PageModel::findByPk($objPage->rootId);
  279.         if(strlen($objRoot->pct_theme) < || !is_array($GLOBALS['PCT_THEMER']['THEMES'][$objRoot->pct_theme]))
  280.         {
  281.             return;
  282.         }
  283.         
  284.         if(count($GLOBALS['PCT_THEMER']['THEMES'][$objRoot->pct_theme]['googlefonts']) > || is_array($GLOBALS['PCT_THEMER']['THEMES'][$objRoot->pct_theme]['googlefonts']))
  285.         {
  286.             $arrFonts explode('|'$objLayout->webfonts);
  287.             $arrFonts array_uniquearray_merge($arrFonts,$GLOBALS['PCT_THEMER']['THEMES'][$objRoot->pct_theme]['googlefonts']) );
  288.             $objLayout->webfonts implode('|'$arrFonts);
  289.         }
  290.     }
  291. }