* @github Joomla Component Builder * @copyright Copyright (C) 2015 - 2020 Vast Development Method. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ // No direct access to this file defined('_JEXEC') or die('Restricted access'); use Joomla\CMS\Application\CMSApplication; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\Registry\Registry; JLoader::register('ComponentbuilderHelper', JPATH_ADMINISTRATOR . '/components/com_componentbuilder/helpers/componentbuilder.php'); /** * Extension - Componentbuilder Headers Compiler plugin. * * @package ComponentbuilderHeadersCompiler * @since 1.0.3 */ class PlgExtensionComponentbuilderHeadersCompiler extends CMSPlugin { /** * Global switch to see if component have need class headers. * * @var boolean * @since 1.0.0 */ protected $loadClassHeaders = false; /** * The Views active with default ordering options * * @var array * @since 1.0.0 */ protected $activeViews = array(); /** * The Targets * * @var array * @since 1.0.3 */ protected $targets = array( 'admin_view_headers' => array( 'add_admin_view_controller' => array( 'field' => 'admin_view_controller', 'context' => 'admin.view.controller', 'view' => 'name_single' ), 'add_admin_views_controller' => array( 'field' => 'admin_views_controller', 'context' => 'admin.views.controller', 'view' => 'name_list' ), 'add_admin_view_model' => array( 'field' => 'admin_view_model', 'context' => 'admin.view.model', 'view' => 'name_single' ), 'add_admin_views_model' => array( 'field' => 'admin_views_model', 'context' => 'admin.views.model', 'view' => 'name_list' ) ), 'site_view_headers' => array( 'add_site_view_controller' => array( 'field' => 'site_view_controller', 'context' => 'site.view.controller', 'view' => 'code' ), 'add_site_views_controller' => array( 'field' => 'site_views_controller', 'context' => 'site.views.controller', 'view' => 'code' ), 'add_site_view_model' => array( 'field' => 'site_view_model', 'context' => 'site.view.model', 'view' => 'code' ), 'add_site_views_model' => array( 'field' => 'site_views_model', 'context' => 'site.views.model', 'view' => 'code' ) ), 'custom_admin_view_headers' => array( 'add_custom_admin_view_controller' => array( 'field' => 'custom_admin_view_controller', 'context' => 'custom.admin.view.controller', 'view' => 'code' ), 'add_custom_admin_views_controller' => array( 'field' => 'custom_admin_views_controller', 'context' => 'custom.admin.views.controller', 'view' => 'code' ), 'add_custom_admin_view_model' => array( 'field' => 'custom_admin_view_model', 'context' => 'custom.admin.view.model', 'view' => 'code' ), 'add_custom_admin_views_model' => array( 'field' => 'custom_admin_views_model', 'context' => 'custom.admin.views.model', 'view' => 'code' ) ) ); /** * Event Triggered in the compiler [on Before Model View Data] * * @return void * * @since 1.0 */ public function jcb_ce_onBeforeModelViewData(&$context, &$view, &$placeholders ) { // add the privacy $view->params = (isset($view->params) && ComponentbuilderHelper::checkJson($view->params)) ? json_decode( $view->params, true ) : $view->params; if (ComponentbuilderHelper::checkArray($view->params) && isset($view->params['admin_view_headers']) && ComponentbuilderHelper::checkArray( $view->params['admin_view_headers'] )) { foreach ($this->targets['admin_view_headers'] as $target => $event) { if (isset($view->params['admin_view_headers'][$target]) && $view->params['admin_view_headers'][$target] == 1) { // activate the load of the privacy plugin $this->loadClassHeaders = true; // setup the view key name $view_name = ComponentbuilderHelper::safeString( $view->{$event['view']} ); // load the admin view details $this->activeViews[$view_name][$event['context']] = $view->params['admin_view_headers'][$event['field']]; } } } } /** * Event Triggered in the compiler [on Before Model Custom View Data] * * @return void * * @since 1.0 */ public function jcb_ce_onBeforeModelCustomViewData(&$context, &$view, &$id, &$table ) { // add the privacy $view->params = (isset($view->params) && ComponentbuilderHelper::checkJson($view->params)) ? json_decode( $view->params, true ) : $view->params; // target site views if (ComponentbuilderHelper::checkArray($view->params) && isset($view->params['site_view_headers']) && ComponentbuilderHelper::checkArray( $view->params['site_view_headers'] )) { foreach ($this->targets['site_view_headers'] as $target => $event) { if (isset($view->params['site_view_headers'][$target]) && $view->params['site_view_headers'][$target] == 1) { // activate the load of the privacy plugin $this->loadClassHeaders = true; // setup the view key name $view_name = ComponentbuilderHelper::safeString( $view->{$event['view']} ); // load the admin view details $this->activeViews[$view_name][$event['context']] = $view->params['site_view_headers'][$event['field']]; } } } // target custom admin veiws if (ComponentbuilderHelper::checkArray($view->params) && isset($view->params['custom_admin_view_headers']) && ComponentbuilderHelper::checkArray( $view->params['custom_admin_view_headers'] )) { foreach ($this->targets['custom_admin_view_headers'] as $target => $event) { if (isset($view->params['custom_admin_view_headers'][$target]) && $view->params['custom_admin_view_headers'][$target] == 1) { // activate the load of the privacy plugin $this->loadClassHeaders = true; // setup the view key name $view_name = ComponentbuilderHelper::safeString( $view->{$event['view']} ); // load the admin view details $this->activeViews[$view_name][$event['context']] = $view->params['custom_admin_view_headers'][$event['field']]; } } } } /** * Event Triggered in the compiler [on set ClassHeader] * * @return void * * @since 1.0 */ public function jcb_ce_setClassHeader(&$context, &$event_context, &$view_name, &$headers ) { if ($this->loadClassHeaders && isset($this->activeViews[$view_name]) && isset($this->activeViews[$view_name][$event_context])) { // we have default headers we actually need foreach ($headers as $n => $header) { // we check if this header is also set from the user if (strpos( $this->activeViews[$view_name][$event_context], $header ) !== false) { // since it is set, remove it here unset($headers[$n]); } } // now add the custom headers // yes they are below the defaults, unless overridden and remove above $headers[] = $this->activeViews[$view_name][$event_context]; } } }