system/modules/pct_autogrid/PCT/AutoGrid/ContaoCallbacks.php line 172

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_autogrid
  10.  * @link        http://contao.org
  11.  */
  12. /**
  13.  * Namespace
  14.  */
  15. namespace PCT\AutoGrid;
  16. /**
  17.  * Imports
  18.  */
  19. use PCT\AutoGrid\Core as AutoGrid;
  20. /**
  21.  * Class file
  22.  * ContaoCallbacks
  23.  */
  24. class ContaoCallbacks
  25. {
  26.     /**
  27.      * Autogrid Wrapper template
  28.      * @var string
  29.      */
  30.     public $strAutoGridTemplate 'autogrid';
  31.     /**
  32.      * Handle content elements
  33.      * @param object
  34.      * @param string
  35.      * @param object
  36.      * @return string
  37.      */
  38.     public function getContentElementCallback($objRow$strBuffer$objElement)
  39.     {
  40.         if(TL_MODE != 'FE')
  41.         {
  42.             return $strBuffer;
  43.         }
  44.         else if(in_array($objRow->type$GLOBALS['PCT_AUTOGRID']['wrapperElements']))
  45.         {
  46.             $preg preg_match('/class="(.*?)\"/'$strBuffer,$result);
  47.             if($preg && strpos($strBuffer'ce_'.$objElement->type))
  48.             {
  49.                 $classes array_diffexplode(' '$result[1]), array('ce_'.$objElement->type) );
  50.                 $strBuffer str_replace($result[1], implode(' '$classes), $strBuffer);
  51.             }
  52.             return $strBuffer;
  53.         }
  54.         if( (boolean)$objRow->autogrid === false )
  55.         {
  56.             return $strBuffer;
  57.         }
  58.         // wrap the element in the AutoGrid wrapper
  59.         // @var object
  60.         $objTemplate = new \Contao\FrontendTemplate$this->strAutoGridTemplate );
  61.         $objTemplate->setData$objRow->row() );
  62.         $objTemplate->html $strBuffer;
  63.         // add autogrid template variable Template->autogrid
  64.         AutoGrid::addToTemplate($objTemplate);
  65.         
  66.         return $objTemplate->parse();
  67.     }
  68.     /**
  69.      * Collect include elements for further usage
  70.      * @param object
  71.      * @param boolean
  72.      * @return boolean
  73.      */
  74.     public function isVisibleElementCallback($objRow$blnReturn)
  75.     {
  76.         if( TL_MODE != 'FE' )
  77.         {
  78.             return $blnReturn;
  79.         }
  80.         $objModel null;
  81.         // include module
  82.         if( $objRow->type == 'module' )
  83.         {
  84.             $objModel \Contao\ModuleModel::findByPk($objRow->module);
  85.         }
  86.         // include form
  87.         else if( $objRow->type == 'form' )
  88.         {
  89.             $objModel \Contao\FormModel::findByPk($objRow->form);
  90.         }
  91.         // include content element
  92.         else if( $objRow->type == 'alias' )
  93.         {
  94.             $objModel \Contao\FormModel::findByPk($objRow->cteAlias);
  95.         }
  96.         // include article
  97.         else if( $objRow->type == 'article' )
  98.         {
  99.             $objModel \Contao\ArticleModel::findByPk($objRow->articleAlias);
  100.         }
  101.         if( $objModel !== null )
  102.         {
  103.             $objModel->__set('origID',$objRow->id);
  104.             $objModel->__set('isInclude',true);
  105.         }
  106.         
  107.         return $blnReturn;
  108.     }
  109.     
  110.     /**
  111.      * Add information to the template object
  112.      * @param object
  113.      */
  114.     public function parseTemplateCallback($objTemplate)
  115.     {
  116.         if( TL_MODE != 'FE' )
  117.         {
  118.             return;
  119.         }
  120.         $objRow = clone($objTemplate);
  121.         if( (int)$objTemplate->origID )
  122.         {
  123.             $objRow \Contao\ContentModel::findByPk$objTemplate->origID );
  124.         }
  125.         
  126.         $arrClasses explode(' '$objTemplate->class);
  127.         // remove the ce_TYPE class
  128.         if(in_array($objRow->type$GLOBALS['PCT_AUTOGRID']['wrapperElements']))
  129.         {
  130.             unset( $arrClassesarray_search('ce_'.$objRow->type,$arrClasses) ] );
  131.         }
  132.         if( !isset($GLOBALS['PCT_AUTOGRID']['cssByType'][$objRow->type]) )
  133.         {
  134.             $GLOBALS['PCT_AUTOGRID']['cssByType'][$objRow->type] = array();
  135.         }
  136.         // collect css classes depending on the element type
  137.         $arrFields array_merge($GLOBALS['PCT_AUTOGRID']['cssByType'][$objRow->type] ?: array(),$GLOBALS['PCT_AUTOGRID']['cssByType']['*'] );
  138.         foreach($arrFields as $field)
  139.         {
  140.             $arrClasses[] = $objRow->{$field};
  141.         }
  142.     
  143.         $arrClasses array_uniquearray_filter($arrClasses) );
  144.         
  145.         // add to Contaos $this->class
  146.         if( empty($arrClasses) === false)
  147.         {
  148.             $objTemplate->class implode(' '$arrClasses);
  149.         }
  150.         if( empty($objRow->AutoGrid) === true )
  151.         {
  152.             // add AutoGrid to template
  153.             AutoGrid::addToTemplate($objTemplate,$objRow);
  154.         }
  155.     }
  156.     /**
  157.     * Handle form fields widgets
  158.     * @param widget
  159.     * @return widget
  160.     */
  161.     public function parseWidgetCallback($strBuffer,$objWidget)
  162.     {
  163.         if (TL_MODE != 'FE'
  164.         {
  165.             return $strBuffer;
  166.         } 
  167.         elseif ( (boolean)$objWidget->autogrid === false 
  168.         {
  169.             return $strBuffer;
  170.         }
  171.         $objForm \Contao\FormModel::findByPk($objWidget->pid);
  172.         if(version_compare(VERSION'4','<') && !$objForm->tableless)
  173.         {
  174.             return $strBuffer;
  175.         }
  176.         
  177.         $objRow \FormFieldModel::findByPk($objWidget->id);
  178.         
  179.         // @var object
  180.         $objTemplate = new \Contao\FrontendTemplate$this->strAutoGridTemplate );
  181.         $objTemplate->setData$objRow->row() );
  182.         $objTemplate->html $strBuffer;
  183.         // add autogrid template variable Template->AutoGrid
  184.         AutoGrid::addToTemplate($objTemplate);
  185.         
  186.         return $objTemplate->parse();
  187.     }
  188.     /**
  189.      * Validate the user grid layout setting
  190.      * @param string
  191.      * @param mixed
  192.      * @param object
  193.      * 
  194.      * called from addCustomRegexp Hook
  195.      */
  196.     public function addCustomRegexpCallback($strRegexp$varValue$objWidget)
  197.     {
  198.         if($strRegexp != 'grid')
  199.         {
  200.             return true;
  201.         }
  202.         $objDC $objWidget->__get('dataContainer');
  203.         
  204.         $strClass \Contao\Model::getClassFromTable($objDC->table);
  205.         if( $objDC->activeRecord === null )
  206.         {
  207.             $objDC->activeRecord $strClass::findByPk($objDC->id);
  208.         }
  209.         
  210.         $arrPreset \PCT\AutoGrid\GridPreset::getGridPreset$objDC->activeRecord->autogrid_grid );
  211.         $intColumns = (int)$arrPreset['columns'] ?: 0;
  212.         
  213.         // validate against max columns
  214.         if( $intColumns )
  215.         {
  216.             $values explode(' '$varValue);
  217.             $count count($values);
  218.             // too many columns
  219.             if( $count != $intColumns )
  220.             {
  221.                 $objWidget->addErrorsprintf($GLOBALS['TL_LANG']['XPT']['grid_rgxp']['column_count'], $count$intColumns) );
  222.                 return true;
  223.             }
  224.             // check the units are %
  225.             foreach($values as $i => $value)
  226.             {
  227.                 // no percent orlast character is not percent
  228.                 if( strlen(strpos($value,'%')) < || \substr($value,-1) != '%' )
  229.                 {
  230.                     $objWidget->addError$GLOBALS['TL_LANG']['XPT']['grid_rgxp']['wrong_or_missing_unit'] );
  231.                     return true;
  232.                 }
  233.                 // no numeric value
  234.                 if( $value == '%' && strlen($value) == )
  235.                 {
  236.                     $objWidget->addErrorsprintf($GLOBALS['TL_LANG']['XPT']['grid_rgxp']['no_numeric_value'], $i+1) );
  237.                     return true;
  238.                 }
  239.                 // has alpha values
  240.                 $v str_replace('%','',$value);
  241.                 if( !is_integer($v) && !is_numeric($v) )
  242.                 {
  243.                     $objWidget->addErrorsprintf($GLOBALS['TL_LANG']['XPT']['grid_rgxp']['invalid_value'], $i+1) );
  244.                     return true;
  245.                 }
  246.                 
  247.                 // must be higher than 0
  248.                 if( $v <= )
  249.                 {
  250.                     $objWidget->addErrorsprintf($GLOBALS['TL_LANG']['XPT']['grid_rgxp']['higher_than_zero'], $i+1) );
  251.                     return true;
  252.                 }
  253.             }
  254.         }
  255.         return false;
  256.     }
  257. }