system/modules/pct_customelements/PCT/CustomElements/Backend/BackendIntegration.php line 44

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\Backend;
  16. /**
  17.  * Imports
  18.  */
  19. use PCT\CustomElements\Core\CustomElements as CustomElements;
  20. use PCT\CustomElements\Core\CustomElementFactory as CustomElementFactory;
  21. use PCT\CustomElements\Core\PluginFactory as PluginFactory;
  22. use PCT\CustomElements\Core\Cache as Cache;
  23. use Contao\BackendUser;
  24. /**
  25.  * Class file
  26.  * BackendIntegration
  27.  * Provide methods to generate the backend interface for the custom elements
  28.  */
  29. class BackendIntegration extends \Contao\Backend
  30. {
  31.     /**
  32.      * Current widget
  33.      * @param object
  34.      */
  35.     protected $objWidget;
  36.     
  37.     /**
  38.      * Import the back end user object
  39.      */
  40.     public function __construct()
  41.     {
  42.         parent::__construct();
  43.         $this->import(BackendUser::class, 'User');
  44.     }
  45.     /**
  46.      * Generate the backend view for the current custom element
  47.      * @param DataContainer
  48.      * @return DataContainer
  49.      *
  50.      * called from onload_callback HOOK
  51.      */
  52.     public function buildBackendView($objDC)
  53.     {
  54.         if(\Contao\Input::get('act') != 'edit')
  55.         {
  56.             return $objDC;
  57.         }
  58.         
  59.         $objActiveRecord $objDC->activeRecord;
  60.         $strModel \Contao\Model::getClassFromTable($objDC->table);
  61.         if($objActiveRecord === null && class_exists($strModel))
  62.         {
  63.             $objActiveRecord $strModel::findByPk($objDC->id);
  64.         }
  65.         else if($objActiveRecord === null && !class_exists($strModel))
  66.         {
  67.             $objActiveRecord \Contao\Database::getInstance()->prepare("SELECT * FROM ".$objDC->table." WHERE id=?")->limit(1)->execute($objDC->id);
  68.         }
  69.         
  70.         // get the selection field for the current table
  71.         $selectionfield CustomElements::getSelectionField($objDC->table);
  72.         
  73.         if(strlen($selectionfield) < 1)
  74.         {
  75.             return;
  76.         }
  77.         
  78.         // return if field is already been created
  79.         if(in_array($GLOBALS['TL_DCA'][$objDC->table]['fields'][$selectionfield]['inputType'],array('pct_customelements','pct_customelement')) )
  80.         {
  81.             return;
  82.         }
  83.         
  84.         $objCustomElement CustomElementFactory::findByAlias($objActiveRecord->{$selectionfield});
  85.         if(!$objCustomElement)
  86.         {
  87.             return;
  88.         }
  89.         
  90.         // return if no permission
  91.         if(!$objCustomElement->hasAccess())
  92.         {
  93.             return;
  94.         }
  95.         // check if CE is really enabled for being a content element or module
  96.         if( ($objDC->table == 'tl_content' && !$objCustomElement->isCTE) || ($objDC->table == 'tl_module' && !$objCustomElement->isFMD) )
  97.         {
  98.             return;
  99.         }
  100.         $strType $objActiveRecord->{$selectionfield};
  101.                 
  102.         // Prepare the CustomElementWidget as wrapper for all the fields etc
  103.         $strGenericCustomElementField 'customelement_widget_'.$objDC->id;
  104.         $GLOBALS['TL_DCA'][$objDC->table]['fields'][$strGenericCustomElementField] = array
  105.         (
  106.             'label'         => array('',''),
  107.             'exclude'        => false,
  108.             'inputType'    => 'pct_customelement',
  109.             'input_field_callback'  => array(get_class($this),'generateGenericCustomElementField'),
  110.         );
  111.         
  112.         // search for a placeholder
  113.         if(strlen(strpos($GLOBALS['TL_DCA'][$objDC->table]['palettes']['default'],'###CUSTOMELEMENT_WIDGET###')) > 0)
  114.         {
  115.             $strPalette str_replace('###CUSTOMELEMENT_WIDGET###'$strGenericCustomElementField$GLOBALS['TL_DCA'][$objDC->table]['palettes']['default']);
  116.         }
  117.         else
  118.         {
  119.             $objDcaHelper \PCT\CustomElements\Helper\DcaHelper::getInstance()->setTable($objDC->table);
  120.             
  121.             // get current default palettes
  122.             $arrPalettes $objDcaHelper->getPalettesAsArray('default');
  123.             
  124.             // build default palettes
  125.             $arrPalettes['custom_legend'] = array($strGenericCustomElementField);
  126.             $arrPalettes['protected_legend:hide'] = array('protected');
  127.             if(version_compare(VERSION'3.4','>='))
  128.             {
  129.                 $arrPalettes['template_legend:hide'] = array('customTpl');
  130.             }
  131.             $arrPalettes['expert_legend:hide'] = array('guests','cssID','space');
  132.             $arrPalettes['invisible_legend:hide'] = array('invisible','start','stop');
  133.             
  134.             // set palettes
  135.             $strPalette $objDcaHelper->generatePalettes($arrPalettes);
  136.         }
  137.         
  138.         // set the palette
  139.         $GLOBALS['TL_DCA'][$objDC->table]['palettes'][$strType] = $strPalette;
  140.         
  141.         // handle tables with no type selction
  142.         if(count($GLOBALS['TL_DCA'][$objDC->table]['palettes']) < 2)
  143.         {
  144.             $GLOBALS['TL_DCA'][$objDC->table]['palettes']['default'] = $strPalette;
  145.         }
  146.         
  147.         // custom legend
  148.         $GLOBALS['TL_LANG'][$objDC->table]['custom_legend'] = sprintf($GLOBALS['TL_LANG']['PCT_CUSTOMELEMENTS']['custom_legend'],$objCustomElement->get('title'));
  149.         
  150.         return $objDC;
  151.     }
  152.     
  153.     
  154.     /**
  155.      * Add the customelement templates to the custom template selection
  156.      * @param object
  157.      * @return array
  158.      */
  159.     public function getTemplates($objDC=null)
  160.     {
  161.         if( $objDC->activeRecord === null )
  162.         {
  163.             $strModel \Contao\Model::getClassFromTable($objDC->table);
  164.             if(class_exists($strModel))
  165.             {
  166.                 $objDC->activeRecord $strModel::findByPk($objDC->id);
  167.             }
  168.             else
  169.             {
  170.                 $objDC->activeRecord \Contao\Database::getInstance()->prepare("SELECT * FROM ".$objDC->table." WHERE id=?")->limit(1)->execute($objDC->id);
  171.             }
  172.         }
  173.         $strSelectionField CustomElements::getSelectionField($objDC->table);
  174.         $objCustomElemnts = new CustomElements;
  175.         $arrCustomElements $objCustomElemnts->getCustomElements($objDC->table);
  176.         if( \in_array($objDC->activeRecord->{$strSelectionField}, $arrCustomElements) === false )
  177.         {
  178.             return \Contao\Controller::getTemplateGroup('ce_' $objDC->activeRecord->type '_', array(), 'ce_' $objDC->activeRecord->type);
  179.         }
  180.         
  181.         $objCE CustomElementFactory::findByAlias($objDC->activeRecord->{$strSelectionField});
  182.         $arrTemplates $this->getTemplateGroup($objCE->template, array(), $objCE->template);
  183.         
  184.         if( \version_compare(VERSION,'4.9','>=') || (boolean)$GLOBALS['TL_DCA'][$objDC->table]['fields']['customTpl']['eval']['includeBlankOption'] === false )
  185.         {
  186.             unset( $arrTemplates[$objCE->template] );
  187.         }
  188.         
  189.         $arrContaos = array();
  190.         if($objDC->table == 'tl_content')
  191.         {
  192.             $arrContaos $this->getTemplateGroup('ce_');
  193.         }
  194.         else if($objDC->table == 'tl_module')
  195.         {
  196.             $arrContaos $this->getTemplateGroup('mod_');
  197.         }
  198.         return array_merge($arrTemplates,$arrContaos);
  199.     }
  200.     
  201.     /**
  202.      * Return the default palette for a table
  203.      * @param string
  204.      * @return string
  205.      */
  206.     protected function getDefaultPalette($strTable)
  207.     {
  208.         $this->loadDataContainer($strTable);
  209.         return $GLOBALS['TL_DCA'][$strTable]['palettes']['default'];
  210.     }
  211.     /**
  212.      * Create the widget and return it as object
  213.      * @param object DataContainer
  214.      * @return object
  215.      */
  216.     public function prepareGenericCustomElementWidget($objDC)
  217.     {
  218.         $objActiveRecord $objDC->activeRecord;
  219.         $strModel \Contao\Model::getClassFromTable($objDC->table);
  220.         if($objActiveRecord === null && class_exists($strModel))
  221.         {
  222.             $objActiveRecord $strModel::findByPk($objDC->id);
  223.         }
  224.         else if($objActiveRecord === null && !class_exists($strModel))
  225.         {
  226.             $objActiveRecord \Contao\Database::getInstance()->prepare("SELECT * FROM ".$objDC->table." WHERE id=?")->limit(1)->execute($objDC->id);
  227.         }
  228.         
  229.         $strGenericCustomElementField 'generic_customelement_'.$objDC->id;
  230.         $objWidget = new $GLOBALS['BE_FFL']['pct_customelement']
  231.         (
  232.             array
  233.             (
  234.                 'strId'    => $strGenericCustomElementField,
  235.                 'strTable' => $objDC->table,
  236.                 'strField' => $strGenericCustomElementField,
  237.                 'strName'  => $strGenericCustomElementField,
  238.                 'varValue' => '',
  239.                 'objDataContainer' => $objDC,
  240.                 'objActiveRecord' => $objActiveRecord,
  241.             ),
  242.             $objDC
  243.         );
  244.         
  245.         return $objWidget;
  246.     }
  247.     /**
  248.      * Generate the widget and return as string
  249.      * @param object DataContainer
  250.      * @return string
  251.      */
  252.     public function generateGenericCustomElementField($objDC)
  253.     {
  254.         $objWidget $this->prepareGenericCustomElementWidget($objDC);
  255.         $strBuffer $objWidget->generate();
  256.         
  257.         return $strBuffer;
  258.     }
  259.     
  260.     
  261.     /**
  262.      * Generate backend wildcard
  263.      * @param integer    Id of the custom element to be displayed
  264.      * @param integer    Id of the contao element carrying the custom element
  265.      * @param string    The source table e.g. tl_content or tl_module
  266.      */
  267.     public function generateBackendWildcard($intCustomElement,$intElement,$strTable)
  268.     {
  269.         $objDatabase \Contao\Database::getInstance();
  270.         
  271.         // check if element is an include element
  272.         if($strTable == 'tl_content')
  273.         {
  274.             $objElement \Contao\ContentModel::findByPk($intElement);
  275.             if($objElement->type == 'alias')
  276.             {
  277.                 $intElement $objElement->cteAlias;
  278.             }
  279.             // type module
  280.             else if($objElement->type == 'module')
  281.             {
  282.                 $strTable 'tl_module';
  283.                 $intElement $objElement->id;
  284.             }
  285.         }
  286.         else if($strTable == 'tl_module')
  287.         {
  288.             $objElement \Contao\ModuleModel::findByPk($intElement);
  289.         }
  290.         
  291.         // fetch the customelement
  292.         $objCustomElement CustomElementFactory::findById($intCustomElement);
  293.         if($objCustomElement === null)
  294.         {
  295.             return;
  296.         }
  297.         
  298.         // fetch attributes to be displayed in the wildcard
  299.         $objAttributes \PCT\CustomElements\Core\AttributeFactory::findMultipleByCustomElement($intCustomElement);
  300.         $objVault \PCT\CustomElements\Core\Vault::getInstance();
  301.         
  302.         $strReturn '<div class="customelement_wildcard">';
  303.         $strReturn .= '<p class="type">### CUSTOM CONTENT ELEMENT ###</p>';
  304.         $strReturn .= '<p class="title '.($strTable == 'tl_module' 'include_module' '').'">### '.$objCustomElement->get('title').' ###</p>';
  305.         
  306.         if($objAttributes !== null)
  307.         {
  308.             $strReturn .= '<ul class="attributes">';
  309.             foreach($objAttributes as $objAttribute)
  310.             {
  311.                 if( !$objAttribute->published || !$objAttribute->be_visible )
  312.                 {
  313.                     continue;
  314.                 }
  315.                 
  316.                 $value $objVault::getValue($objAttribute->get('uuid'),$intElement,$strTable,array('saveDataAs'=>$objAttribute->get('saveDataAs')));
  317.                 
  318.                 if(!$objAttribute->getOrigin())
  319.                 {
  320.                     $objOrigin \PCT\CustomElements\Core\Origin::getInstance();
  321.                     $objOrigin->set('intPid',$intElement);
  322.                     $objOrigin->set('strTable',$strTable);
  323.                     $objAttribute->setOrigin($objOrigin);
  324.                 }
  325.                 
  326.                 // trigger HOOK: allow other extensions to manipulate the wildcard value
  327.                 $valueFromHook \PCT\CustomElements\Core\Hooks::callstatic('processWildcardValue',array($value,$objAttribute,$intElement,$strTable));
  328.                 if(isset($valueFromHook) && !empty($valueFromHook))
  329.                 {
  330.                     $value $valueFromHook;
  331.                 }
  332.                 
  333.                 // implode arrays
  334.                 if(is_array($value))
  335.                 {
  336.                     $value implode(','$value);
  337.                 }
  338.                 
  339.                 $strReturn .= '<li class="sibling"><span class="title cell first">'.$objAttribute->get('title').'</span><span class="value cell last">'.$value.'</span></li>';
  340.             }
  341.             $strReturn .= '</ul>';
  342.         }
  343.         
  344.         $strReturn .= '</div>';
  345.         
  346.         // add the stylesheet
  347.         $this->loadAssets();
  348.         
  349.         // trigger HOOK: allow other extensions to manipulate the wildcard output
  350.         $strReturn \PCT\CustomElements\Core\Hooks::callstatic('generateWildcardHook',array($strReturn,$objAttributes,$this));
  351.         
  352.         return $strReturn;
  353.     }
  354.     
  355.     
  356.     /**
  357.      * Load stylesheets and scripts
  358.      */
  359.     public function loadAssets()
  360.     {
  361.         if(TL_MODE != 'BE')
  362.         {
  363.             return;
  364.         }
  365.         $GLOBALS['TL_CSS'][] = PCT_CUSTOMELEMENTS_PATH.'/assets/css/styles.css';
  366.         // load contao fonts
  367.         $GLOBALS['TL_CONFIG']['loadGoogleFonts'] = true;
  368.         
  369.         // load font-awesome
  370.         if(\Contao\Config::get('fontaweseome') == 'cdn')
  371.         {
  372.             $GLOBALS['TL_CSS'][] = '//maxcdn.bootstrapcdn.com/font-awesome/'.PCT_CUSTOMELEMENTS_FONTAWESOME_VERSION.'/css/font-awesome.min.css';
  373.         }
  374.         else
  375.         {
  376.             $GLOBALS['TL_CSS'][] = PCT_CUSTOMELEMENTS_PATH.'/assets/font-awesome/'.PCT_CUSTOMELEMENTS_FONTAWESOME_VERSION.'/css/font-awesome.min.css';
  377.         }
  378.         
  379.         // load js
  380.         $GLOBALS['TL_JAVASCRIPT'][] = PCT_CUSTOMELEMENTS_PATH.'/assets/js/CustomElements.js';
  381.     }
  382.     
  383.     
  384.     /**
  385.      * Inject javascript templates in the backend page
  386.      * @param object
  387.      * 
  388.      * Called from [parseTemplate] Hook
  389.       */
  390.     public function injectJavascriptInBackendPage($objTemplate)
  391.     {
  392.         if($objTemplate->getName() == 'be_main')
  393.         {
  394.             $objJsMooFxSlide = new \Contao\BackendTemplate('be_js_moo_fxslide');
  395.             $objTemplate->javascripts .= $objJsMooFxSlide->parse();
  396.         }
  397.     }    
  398.     
  399.     
  400.     /**
  401.      * Inject the versionnumber in the backend page
  402.      * @param object
  403.      * 
  404.      * Called from [parseTemplate] Hook
  405.      */
  406.     public function injectVersionnumberInBackendPage($objTemplate)
  407.     {
  408.         if($objTemplate->getName() == 'be_main' && \Contao\Input::get('do') == 'pct_customelements')
  409.         {
  410.             // show customcatalog version number
  411.             $objTemplate->headline .= '<span class="version">'.PCT_CUSTOMELEMENTS_VERSION.'</span>';
  412.         }
  413.     }
  414. }