52
0
plg_extension_componentbuil.../componentbuilderheaderscompiler.php

319 lines
9.2 KiB
PHP
Raw Normal View History

2021-10-14 14:31:41 +00:00
<?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>
2021-10-14 14:31:59 +00:00
* @copyright Copyright (C) 2015 - 2021 Vast Development Method. All rights reserved.
2021-10-14 14:31:41 +00:00
* @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
2021-10-14 14:31:59 +00:00
* @since 1.0.4
2021-10-14 14:31:41 +00:00
*/
class PlgExtensionComponentbuilderHeadersCompiler extends CMSPlugin
{
/**
2021-10-14 14:31:59 +00:00
* Global switch to see if a file has custom headers.
2021-10-14 14:31:41 +00:00
*
* @var boolean
* @since 1.0.0
*/
2021-10-14 14:31:59 +00:00
protected $loadHeaders = false;
2021-10-14 14:31:41 +00:00
/**
2021-10-14 14:31:59 +00:00
* The files active with headers
2021-10-14 14:31:41 +00:00
*
* @var array
* @since 1.0.0
*/
2021-10-14 14:31:59 +00:00
protected $activeFiles = array();
2021-10-14 14:31:41 +00:00
/**
* The Targets
*
* @var array
2021-10-14 14:31:59 +00:00
* @since 1.0.4
2021-10-14 14:31:41 +00:00
*/
2021-10-14 14:31:46 +00:00
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'
)
2021-10-14 14:31:41 +00:00
),
2021-10-14 14:31:46 +00:00
'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'
)
2021-10-14 14:31:53 +00:00
),
'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'
)
2021-10-14 14:31:59 +00:00
),
'joomla_component_headers' =>
array(
'add_admin_component' => array(
'field' => 'admin_component',
'context' => 'admin.component',
'view' => 'admin'
),
'add_site_component' => array(
'field' => 'site_component',
'context' => 'site.component',
'view' => 'site'
),
'add_admin_helper' => array(
'field' => 'admin_helper',
'context' => 'admin.helper',
'view' => 'admin'
),
'add_site_helper' => array(
'field' => 'site_helper',
'context' => 'site.helper',
'view' => 'site'
)
2021-10-14 14:31:41 +00:00
)
);
/**
* 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)
2021-10-14 14:31:46 +00:00
&& 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
2021-10-14 14:31:59 +00:00
$this->loadHeaders = true;
2021-10-14 14:31:46 +00:00
// setup the view key name
$view_name = ComponentbuilderHelper::safeString(
$view->{$event['view']}
);
2021-10-14 14:31:59 +00:00
// load the headers
$this->activeFiles[$view_name][$event['context']]
2021-10-14 14:31:46 +00:00
= $view->params['admin_view_headers'][$event['field']];
}
}
}
}
/**
* Event Triggered in the compiler [on Before Model Custom View Data]
*
* @return void
*
2021-10-14 14:31:59 +00:00
* @since 1.2
2021-10-14 14:31:46 +00:00
*/
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;
2021-10-14 14:31:53 +00:00
// target site views
2021-10-14 14:31:46 +00:00
if (ComponentbuilderHelper::checkArray($view->params)
&& isset($view->params['site_view_headers'])
2021-10-14 14:31:41 +00:00
&& ComponentbuilderHelper::checkArray(
2021-10-14 14:31:46 +00:00
$view->params['site_view_headers']
2021-10-14 14:31:41 +00:00
))
{
2021-10-14 14:31:46 +00:00
foreach ($this->targets['site_view_headers'] as $target => $event)
2021-10-14 14:31:41 +00:00
{
2021-10-14 14:31:46 +00:00
if (isset($view->params['site_view_headers'][$target])
&& $view->params['site_view_headers'][$target] == 1)
2021-10-14 14:31:41 +00:00
{
// activate the load of the privacy plugin
2021-10-14 14:31:59 +00:00
$this->loadHeaders = true;
2021-10-14 14:31:41 +00:00
// setup the view key name
$view_name = ComponentbuilderHelper::safeString(
$view->{$event['view']}
);
2021-10-14 14:31:59 +00:00
// load the headers
$this->activeFiles[$view_name][$event['context']]
2021-10-14 14:31:46 +00:00
= $view->params['site_view_headers'][$event['field']];
2021-10-14 14:31:41 +00:00
}
}
}
2021-10-14 14:31:59 +00:00
// target custom admin views
2021-10-14 14:31:53 +00:00
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
2021-10-14 14:31:59 +00:00
$this->loadHeaders = true;
2021-10-14 14:31:53 +00:00
// setup the view key name
$view_name = ComponentbuilderHelper::safeString(
$view->{$event['view']}
);
2021-10-14 14:31:59 +00:00
// load the headers
$this->activeFiles[$view_name][$event['context']]
2021-10-14 14:31:53 +00:00
= $view->params['custom_admin_view_headers'][$event['field']];
}
}
}
2021-10-14 14:31:41 +00:00
}
/**
2021-10-14 14:31:59 +00:00
* Event Triggered in the compiler [on Before Model Component Data]
*
* @return void
*
* @since 1.4
*/
public function jcb_ce_onBeforeModelComponentData(&$context, &$component)
{
// add the privacy
$component->params = (isset($component->params)
&& ComponentbuilderHelper::checkJson($component->params)) ? json_decode(
$component->params, true
) : $component->params;
if (ComponentbuilderHelper::checkArray($component->params)
&& isset($component->params['joomla_component_headers'])
&& ComponentbuilderHelper::checkArray(
$component->params['joomla_component_headers']
))
{
foreach ($this->targets['joomla_component_headers'] as $target => $event)
{
if (isset($component->params['joomla_component_headers'][$target])
&& $component->params['joomla_component_headers'][$target] == 1)
{
// activate the load of the privacy plugin
$this->loadHeaders = true;
// load the headers
$this->activeFiles[$event['view']][$event['context']]
= $component->params['joomla_component_headers'][$event['field']];
}
}
}
}
/**
* Event Triggered in the compiler [on set Class Header]
2021-10-14 14:31:41 +00:00
*
* @return void
*
* @since 1.0
*/
public function jcb_ce_setClassHeader(&$context, &$event_context,
&$view_name,
&$headers
) {
2021-10-14 14:31:59 +00:00
if ($this->loadHeaders && isset($this->activeFiles[$view_name])
&& isset($this->activeFiles[$view_name][$event_context]))
2021-10-14 14:31:41 +00:00
{
// 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(
2021-10-14 14:31:59 +00:00
$this->activeFiles[$view_name][$event_context], $header
2021-10-14 14:31:41 +00:00
) !== false)
{
// since it is set, remove it here
unset($headers[$n]);
}
}
// now add the custom headers
2021-10-14 14:31:59 +00:00
// yes they are below the defaults, unless overridden and removed above
$headers[] = $this->activeFiles[$view_name][$event_context];
2021-10-14 14:31:41 +00:00
}
}
}