system/modules/pct_theme_settings/PCT/ThemeSettings/ContaoCallbacks.php line 34

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 2019
  8.  * @author  Tim Gatzky <info@tim-gatzky.de>
  9.  * @package  pct_theme_settings
  10.  * @link  http://contao.org
  11.  */
  12. /**
  13.  * Namespace
  14.  */
  15. namespace PCT\ThemeSettings;
  16. use Contao\StringUtil;
  17. /**
  18.  * Class file
  19.  * ContaoCallbacks
  20.  *
  21.  * Provide various callbacks to contao hooks
  22.  */
  23. class ContaoCallbacks
  24. {
  25.     /**
  26.      * Run scripts on Contao init
  27.      * @ called from initializeSystem Hook
  28.      */
  29.     public function initializeSystemCallback()
  30.     {
  31.         // append themify-icons.css to Iconpicker
  32.         if( \file_exists(TL_ROOT.'/files/cto_layout/css/themify-icons.css') )
  33.         {
  34.             $arrFiles \Contao\StringUtil::deserialize\Contao\Config::get('iconStylesheets') ) ?: array();
  35.             $arrFiles[] = \Contao\StringUtil::binToUuid\Contao\Dbafs::addResource('files/cto_layout/css/themify-icons.css')->uuid );
  36.             \Contao\Config::set('iconStylesheets',$arrFiles);
  37.             
  38.             $arrClasses \explode(','\Contao\Config::get('customIconClasses') ?: '');
  39.             $arrClasses[] = '.ti-';
  40.             $arrClasses[] = '.ti-themify';
  41.             \Contao\Config::set('customIconClasses'\implode(',',array_unique($arrClasses)));
  42.         }
  43.     }
  44.     /**
  45.      * Add the theme setting related information to the article template
  46.      * @param object The article template object
  47.      * @param array  The db row information
  48.      * @param object The class object
  49.      *
  50.      * called from compileArticle Hook
  51.      */
  52.     public function compileArticleCallback($objTemplate,$arrData,$objArticle)
  53.     {
  54.         \PCT\ThemeSettings\Core::addToTemplate($objTemplate,$objArticle);
  55.     }
  56.     
  57.     
  58.     /**
  59.      * Inject theme classes in content elements
  60.      * @param object
  61.      * @param string
  62.      * @param objElement
  63.      * @return string
  64.      */
  65.     public function getContentElementCallback($objRow$strBuffer$objElement)
  66.     {
  67.         if(TL_MODE != 'FE')
  68.         {
  69.             return $strBuffer;
  70.         }
  71.         
  72.         // handle include elements
  73.         //if( $objElement->type == 'alias' )
  74.         //{
  75.         //    $objModel = \Contao\ContentModel::findByPk($objElement->cteAlias);
  76.         //    if ($objModel === null)
  77.         //    {
  78.         //        return $strBuffer;
  79.         //    }
  80.         //    
  81.         //    $strClass = \Contao\ContentElement::findClass($objModel->type);
  82.         //    if( class_exists($strClass) === false)
  83.         //    {
  84.         //        return $strBuffer;
  85.         //    }
  86.         //    
  87.         //    $objOrigElement = new $strClass($objModel);
  88.         //    
  89.         //    // run recursive here to get all the autogrid features for the original element
  90.         //    return $this->getContentElementCallback($objModel,$strBuffer,$objOrigElement);
  91.         //}
  92.         
  93.         $blnGenerate false;
  94.         // set the ce_headline_seo template for headlines when seo is active and no custom template is set
  95.         if($objElement->type == 'headline' && $objRow->seo  && empty($objRow->customTpl))
  96.         {
  97.             $objElement->customTpl $GLOBALS['PCT_THEME_SETTINGS']['templateByType']['headline_seo'];
  98.             $blnGenerate true;
  99.         }
  100.         else if($objElement->type == 'text' && $objRow->seo && empty($objRow->customTpl))
  101.         {
  102.             $objElement->customTpl $GLOBALS['PCT_THEME_SETTINGS']['templateByType']['text_seo'];
  103.             $blnGenerate true;
  104.         }
  105.         if($blnGenerate === true)
  106.         {
  107.             $strClass \Contao\ContentElement::findClass($objRow->type);
  108.             if( class_exists($strClass) === false)
  109.             {
  110.                 return $strBuffer;
  111.             }
  112.             // set new template
  113.             $objRow->customTpl $objElement->customTpl;
  114.             // @var object
  115.             $objElement = new $strClass($objRow);
  116.             // regenerate the element with the new template
  117.             $strBuffer $objElement->generate();
  118.         }
  119.     
  120.         return $strBuffer;
  121.     }
  122.     /**
  123.     * Handle form fields widgets
  124.     * @param widget
  125.     * @return widget
  126.     */
  127.     public function loadFormFieldCallback($objWidget)
  128.     {
  129.         if( empty($objWidget->visibility_css) === false )
  130.         {
  131.             $objWidget->class ' '.$objWidget->visibility_css;
  132.         }
  133.         return $objWidget;
  134.     }
  135.     /**
  136.      * Collect include elements for further usage
  137.      * @param object
  138.      * @param boolean
  139.      * @return boolean
  140.      */
  141.     public function isVisibleElementCallback($objRow$blnReturn)
  142.     {
  143.         if( TL_MODE != 'FE' )
  144.         {
  145.             return $blnReturn;
  146.         }
  147.         $objModel null;
  148.         // include module
  149.         if( $objRow->type == 'module' )
  150.         {
  151.             $objModel \Contao\ModuleModel::findByPk($objRow->module);
  152.         }
  153.         // include form
  154.         else if( $objRow->type == 'form' )
  155.         {
  156.             $objModel \Contao\FormModel::findByPk($objRow->form);
  157.         }
  158.         // include content element
  159.         else if( $objRow->type == 'alias' )
  160.         {
  161.             $objModel \Contao\FormModel::findByPk($objRow->cteAlias);
  162.         }
  163.         // include article
  164.         else if( $objRow->type == 'article' )
  165.         {
  166.             $objModel \Contao\ArticleModel::findByPk($objRow->articleAlias);
  167.         }
  168.         // frontend module
  169.         if( $objRow->getTable() == 'tl_module' )
  170.         {
  171.             $arrCssId \Contao\StringUtil::deserialize($objRow->cssID);
  172.             if( empty($objRow->visibility_css) === false )
  173.             {
  174.                 $arrCssId[1] .= ' '.$objRow->visibility_css;
  175.             }
  176.             $objRow->__set('cssID',$arrCssId);
  177.         }
  178.         if( $objModel !== null )
  179.         {
  180.             $objModel->__set('origID',$objRow->id);
  181.             $objModel->__set('isInclude',true);
  182.         }
  183.         
  184.         return $blnReturn;
  185.     }
  186.     /**
  187.      * Add information to the template object
  188.      * @param object
  189.      */
  190.     public function parseTemplateCallback($objTemplate)
  191.     {
  192.         $objRow = clone($objTemplate);
  193.         if( (int)$objTemplate->origID )
  194.         {
  195.             $objRow \Contao\ContentModel::findByPk$objTemplate->origID );
  196.         }
  197.         
  198.         $arrClasses explode(' '$objTemplate->class);
  199.         if( !isset($GLOBALS['PCT_THEME_SETTINGS']['cssByType'][$objRow->type]) )
  200.         {
  201.             $GLOBALS['PCT_THEME_SETTINGS']['cssByType'][$objRow->type] = array();
  202.         }
  203.         // collect css classes depending on the element type
  204.         $arrFields array_merge($GLOBALS['PCT_THEME_SETTINGS']['cssByType'][$objRow->type] ?: array(),$GLOBALS['PCT_THEME_SETTINGS']['cssByType']['*'] );
  205.         foreach($arrFields as $field)
  206.         {
  207.             $arrClasses[] = $objRow->{$field};
  208.         }
  209.     
  210.         $arrClasses array_uniquearray_filter($arrClasses) );
  211.         
  212.         // add to Contaos $this->class
  213.         if( empty($arrClasses) === false)
  214.         {
  215.             $objTemplate->class implode(' '$arrClasses);
  216.         }
  217.     }
  218.     
  219.     // !Inserttags
  220.     /**
  221.      * Replace insert tags
  222.      * @param string
  223.      * @return boolean||mixed
  224.      *
  225.      * called from replaceInsertTags Hook
  226.      */
  227.     public function replaceInsertTagsCallback($strElement)
  228.     {
  229.         $arrElements explode('::'$strElement);
  230.         
  231.         // label-
  232.         if( strlen(strpos($arrElements[0], 'label-')) > )
  233.         {
  234.             $arr $arrElements;
  235.             $arrElements[0] = 'label-';
  236.             $arrElements[1] = str_replace('label-','',$arr[0]);
  237.             $arrElements[2] = $arr[1];
  238.             unset($arr);
  239.         }
  240.         
  241.         // color-
  242.         if( strlen(strpos($arrElements[0], 'color-')) > )
  243.         {
  244.             $arr $arrElements;
  245.             $arrElements[0] = 'color-';
  246.             $arrElements[1] = str_replace('color-','',$arr[0]);
  247.             $arrElements[2] = $arr[1];
  248.             unset($arr);
  249.         }
  250.         
  251.         switch($arrElements[0])
  252.         {
  253.             // color-
  254.             case 'color-':
  255.                 return '<span class="color-'.$arrElements[1].'">'.$arrElements[2].'</span>';
  256.                 break;
  257.             // label-
  258.             case 'label-':
  259.                 return '<span class="label-'.$arrElements[1].'">'.$arrElements[2].'</span>';
  260.                 break;
  261.             default:
  262.                 break;
  263.         }
  264.         return false;
  265.     }
  266.     // !News
  267.     /**
  268.      * Enable manual sorting for tl_news
  269.      * @param string
  270.      * @called from loadDataContainer Hook
  271.      */
  272.     public function enableManualSorting($strTable)
  273.     {
  274.         if($strTable != 'tl_news' || TL_MODE != 'BE')
  275.         {
  276.             return;
  277.         }
  278.         $objArchive null;
  279.         $intArchive 0;
  280.         if(!\Contao\Input::get('act'))
  281.         {
  282.             $intArchive \Contao\Input::get('id');
  283.         }
  284.         else if(in_array(\Contao\Input::get('act'),array('cut','copyAll')) || in_array(\Contao\Input::get('mode'),array('cut','cutAll')) )
  285.             {
  286.                 $objActiveRecord \Contao\NewsModel::findByPk\Contao\Input::get('id') );
  287.                 $intArchive $objActiveRecord->pid;
  288.             }
  289.         #$objArchive = $objArchive = \Contao\NewsArchiveModel::findByPk($intArchive);
  290.         $objArchive \Contao\Database::getInstance()->prepare("SELECT * FROM tl_news_archive WHERE id=?")->limit(1)->execute($intArchive);
  291.         
  292.         if($objArchive->manualSorting)
  293.         {
  294.             $GLOBALS['TL_DCA']['tl_news']['list']['sorting']['fields'] = array('sorting');
  295.             // add the sorting field as backend sorting option
  296.             $GLOBALS['TL_DCA']['tl_news']['fields']['sorting']['sorting'] = true;
  297.             // bypass the permission check on cut, cutAll to allow manual sorting
  298.             if( \in_array(\Contao\Input::get('act'),array('cut','cutAll')) )
  299.             {
  300.                 if(is_array($GLOBALS['TL_DCA']['tl_news']['config']['onload_callback']))
  301.                 {
  302.                     foreach($GLOBALS['TL_DCA']['tl_news']['config']['onload_callback'] as $i => $callback)
  303.                     {
  304.                         if(!is_array($callback) || empty($callback))
  305.                         {
  306.                             continue;
  307.                         }
  308.                         if($callback[0] == 'tl_news' && $callback[1] == 'checkPermission')
  309.                         {
  310.                             unset($GLOBALS['TL_DCA']['tl_news']['config']['onload_callback'][$i]);
  311.                         }
  312.                     }
  313.                 }
  314.             }
  315.         }
  316.     }
  317.     /**
  318.      * Apply manual sorting for news
  319.      * @param array
  320.      * @param boolean
  321.      * @param integer
  322.      * @param integer
  323.      * @return object||null||false
  324.      * called from $GLOBALS['TL_HOOKS']['newsListFetchItems']
  325.      */
  326.     public function newsListFetchItemsCallback($arrArchives$blnFeatured$intLimit$intOffset$objModule)
  327.     {
  328.         if( $objModule->news_order != 'sorting' )
  329.         {
  330.             return false;
  331.         }
  332.         
  333.         $strTable \Contao\NewsModel::getTable();
  334.         $arrOptions = array( 'order' => 'sorting' );
  335.         // news list filtering
  336.         // fetch portfoliofilters related to this list
  337.         $objFilterModules \Contao\ModuleModel::findBy( array('news_readerModule','news_sysfilter'), array($objModule->id,1) );
  338.         if( $objFilterModules !== null )
  339.         {
  340.             $arrIds = static::filterNews($arrArchives$blnFeatured$intLimit$intOffset$arrOptions);
  341.             if( !empty($arrIds) )
  342.             {
  343.                 $arrOptions['column'] = array($strTable.'.id IN('.implode(','$arrIds).')');
  344.             }
  345.         }
  346.         
  347.         return \Contao\NewsModel::findPublishedByPids($arrArchives$blnFeatured$intLimit$intOffset$arrOptions);
  348.     }
  349.     /**
  350.      * Calculate the number of news items after applying the filter
  351.      * @param array
  352.      * @param boolean
  353.      * @param object
  354.      * @return object||integer||boolean(false)
  355.      *
  356.       * called from $GLOBALS['TL_HOOKS']['newsListCountItems']
  357.      */
  358.     public function newsListCountItemsCallback($arrArchives$blnFeatured$objModule)
  359.     {
  360.         // fetch portfoliofilters related to this list
  361.         $objFilterModules \Contao\ModuleModel::findBy( array('news_readerModule','news_sysfilter'), array($objModule->id,1) );
  362.         
  363.         if( $objFilterModules === null || \Contao\Input::get('filter') == '' )
  364.         {
  365.             return false;
  366.         }
  367.         
  368.         $arrIds = static::filterNews($arrArchives$blnFeatured00, array());
  369.         
  370.         return count($arrIds);
  371.     }
  372.     
  373.     
  374.     /**
  375.      * Filter news entries
  376.      * @param array
  377.      * @param boolean
  378.      * @param integer
  379.      * @param integer
  380.      * @return array
  381.      */
  382.     public static function filterNews($arrArchives$blnFeatured$intLimit$intOffset$arrOptions)
  383.     {
  384.         if( \Contao\Input::get('filter') == '' )
  385.         {
  386.             return array();
  387.         }
  388.         
  389.         $strTable \Contao\NewsModel::getTable();
  390.         
  391.         // active url filters
  392.         $arrFilters array_filterexplode(','\Contao\Input::get('filter') ) );
  393.         
  394.         $arrOptions['column'] = array($strTable.'.addFilter=1');
  395.         
  396.         // fetch all entries 
  397.         $objEntries \Contao\NewsModel::findPublishedByPids($arrArchives,$blnFeatured,$intLimit,$intOffset,$arrOptions);
  398.         if($objEntries === null)
  399.         {
  400.             return array();
  401.         }
  402.         
  403.         $arrIds = array();
  404.         foreach($objEntries as $objModel)
  405.         {
  406.             $filter StringUtil::deserialize($objModel->filters);
  407.             
  408.             if( empty( array_intersect($filter$arrFilters) ) )
  409.             {
  410.                 continue;
  411.             }
  412.             
  413.             $arrIds[] = $objModel->id;
  414.         }
  415.         
  416.         return $arrIds;
  417.     }
  418. }