52
0
Fork 0
plg_extension_componentbuil.../componentbuilderheaderscomp...

156 lines
4.1 KiB
PHP

<?php
/**
* @package Joomla.Component.Builder
*
* @created 30th April, 2015
* @author Llewellyn van der Merwe <http://www.joomlacomponentbuilder.com>
* @github Joomla Component Builder <https://github.com/vdm-io/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.0
*/
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 Target
*
* @var string
* @since 1.0.0
*/
protected $target = 'admin_view_headers';
/**
* The Targets
*
* @var array
* @since 1.0.0
*/
protected $targets
= 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'
)
);
/**
* 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[$this->target])
&& ComponentbuilderHelper::checkArray(
$view->params[$this->target]
))
{
foreach ($this->targets as $target => $event)
{
if (isset($view->params[$this->target][$target])
&& $view->params[$this->target][$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[$this->target][$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];
}
}
}