52
0
plg_extension_componentbuil.../componentbuilderheaderscompiler.php

486 lines
14 KiB
PHP
Raw Normal View History

2021-10-14 14:31:41 +00:00
<?php
2021-12-03 02:46:17 +00:00
/**
* @package Joomla.Component.Builder
*
* @created 30th April, 2015
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder-Pro>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
2021-10-14 14:31:41 +00:00
*/
// 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-12-03 02:46:17 +00:00
* @since 1.0.7
2021-10-14 14:31:41 +00:00
*/
class PlgExtensionComponentbuilderHeadersCompiler extends CMSPlugin
{
2021-12-03 02:46:17 +00:00
/**
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
2021-12-03 02:46:17 +00:00
/**
* The compiler placeholders values
*
* @var array
* @since 1.0.6
*/
protected $placeholders = array();
/**
* The powers to include in project
*
* @var array
* @since 1.0.6
*/
protected $linkedPowers = array();
2021-10-14 14:31:41 +00:00
/**
* The Targets
*
* @var array
2021-10-14 14:32:05 +00:00
* @since 1.0.5
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_model' => array(
'field' => 'admin_view_model',
'context' => 'admin.view.model',
'view' => 'name_single'
),
2021-10-14 14:32:05 +00:00
'add_admin_view' => array(
'field' => 'admin_view',
'context' => 'admin.view',
'view' => 'name_single'
),
2021-12-03 02:46:17 +00:00
'add_admin_view_html' => array(
'field' => 'admin_view_html',
'context' => 'admin.view.html',
'view' => 'name_single'
),
'add_site_admin_view_html' => array(
'field' => 'site_admin_view_html',
'context' => 'site.admin.view.html',
'view' => 'name_single'
),
2021-10-14 14:32:05 +00:00
'add_admin_view_controller' => array(
'field' => 'admin_view_controller',
'context' => 'admin.view.controller',
'view' => 'name_single'
),
2021-10-14 14:31:46 +00:00
'add_admin_views_model' => array(
'field' => 'admin_views_model',
'context' => 'admin.views.model',
'view' => 'name_list'
2021-10-14 14:32:05 +00:00
),
'add_admin_views' => array(
'field' => 'admin_views',
'context' => 'admin.views',
'view' => 'name_list'
),
2021-12-03 02:46:17 +00:00
'add_admin_views_html' => array(
'field' => 'admin_views_html',
'context' => 'admin.views.html',
'view' => 'name_list'
),
2021-10-14 14:32:05 +00:00
'add_admin_views_controller' => array(
'field' => 'admin_views_controller',
'context' => 'admin.views.controller',
'view' => 'name_list'
2021-10-14 14:31:46 +00:00
)
2021-10-14 14:31:41 +00:00
),
2021-10-14 14:31:46 +00:00
'site_view_headers' =>
array(
2021-10-14 14:32:05 +00:00
'add_site_view_model' => array(
'field' => 'site_view_model',
'context' => 'site.view.model',
2021-10-14 14:31:46 +00:00
'view' => 'code'
),
2021-10-14 14:32:05 +00:00
'add_site_view' => array(
'field' => 'site_view',
'context' => 'site.view',
2021-10-14 14:31:46 +00:00
'view' => 'code'
),
2021-12-03 02:46:17 +00:00
'add_site_view_html' => array(
'field' => 'site_view_html',
'context' => 'site.view.html',
'view' => 'code'
),
2021-10-14 14:32:05 +00:00
'add_site_view_controller' => array(
'field' => 'site_view_controller',
'context' => 'site.view.controller',
2021-10-14 14:31:46 +00:00
'view' => 'code'
),
'add_site_views_model' => array(
'field' => 'site_views_model',
'context' => 'site.views.model',
'view' => 'code'
2021-10-14 14:32:05 +00:00
),
'add_site_views' => array(
'field' => 'site_views',
'context' => 'site.views',
'view' => 'code'
),
2021-12-03 02:46:17 +00:00
'add_site_views_html' => array(
'field' => 'site_views_html',
'context' => 'site.views.html',
'view' => 'code'
),
2021-10-14 14:32:05 +00:00
'add_site_views_controller' => array(
'field' => 'site_views_controller',
'context' => 'site.views.controller',
'view' => 'code'
2021-10-14 14:31:46 +00:00
)
2021-10-14 14:31:53 +00:00
),
'custom_admin_view_headers' =>
array(
2021-10-14 14:32:05 +00:00
'add_custom_admin_view_model' => array(
'field' => 'custom_admin_view_model',
'context' => 'custom.admin.view.model',
2021-10-14 14:31:53 +00:00
'view' => 'code'
),
2021-10-14 14:32:05 +00:00
'add_custom_admin_view' => array(
'field' => 'custom_admin_view',
'context' => 'custom.admin.view',
2021-10-14 14:31:53 +00:00
'view' => 'code'
),
2021-12-03 02:46:17 +00:00
'add_custom_admin_view_html' => array(
'field' => 'custom_admin_view_html',
'context' => 'custom.admin.view.html',
'view' => 'code'
),
2021-10-14 14:32:05 +00:00
'add_custom_admin_view_controller' => array(
'field' => 'custom_admin_view_controller',
'context' => 'custom.admin.view.controller',
2021-10-14 14:31:53 +00:00
'view' => 'code'
),
'add_custom_admin_views_model' => array(
'field' => 'custom_admin_views_model',
'context' => 'custom.admin.views.model',
'view' => 'code'
2021-10-14 14:32:05 +00:00
),
'add_custom_admin_views' => array(
'field' => 'custom_admin_views',
'context' => 'custom.admin.views',
'view' => 'code'
),
2021-12-03 02:46:17 +00:00
'add_custom_admin_views_html' => array(
'field' => 'custom_admin_views_html',
'context' => 'custom.admin.views.html',
'view' => 'code'
),
2021-10-14 14:32:05 +00:00
'add_custom_admin_views_controller' => array(
'field' => 'custom_admin_views_controller',
'context' => 'custom.admin.views.controller',
'view' => 'code'
2021-10-14 14:31:53 +00:00
)
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
) {
2021-12-03 02:46:17 +00:00
// add the headers
2021-10-14 14:31:41 +00:00
$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)
{
2021-12-03 02:46:17 +00:00
// get the header string if set
$this->getHeaderString($view, $event, 'admin_view_headers');
2021-10-14 14:31:46 +00:00
}
}
}
}
/**
* 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
) {
2021-12-03 02:46:17 +00:00
// add the headers
2021-10-14 14:31:46 +00:00
$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
{
2021-12-03 02:46:17 +00:00
// get the header string if set
$this->getHeaderString($view, $event, 'site_view_headers');
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)
{
2021-12-03 02:46:17 +00:00
// get the header string if set
$this->getHeaderString($view, $event, 'custom_admin_view_headers');
2021-10-14 14:31:53 +00:00
}
}
}
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)
{
2021-12-03 02:46:17 +00:00
// activate the load of the headers
2021-10-14 14:31:59 +00:00
$this->loadHeaders = true;
// load the headers
$this->activeFiles[$event['view']][$event['context']]
= $component->params['joomla_component_headers'][$event['field']];
}
}
}
}
2021-12-03 02:46:17 +00:00
/**
* get the header string
*
* @return void
*
* @since 1.0
*/
protected function getHeaderString(&$view, &$get, $key, $view_name = null)
{
// we first check if the value is set
if (isset($view->params[$key][$get['field']]) || isset($view->params[$key]['power_' . $get['field']]))
{
// activate the load of the headers
$this->loadHeaders = true;
// setup the view key name
if (!$view_name)
{
$view_name = ComponentbuilderHelper::safeString(
$view->{$get['view']}
);
}
// we start this field if not already
if (!isset($this->activeFiles[$view_name][$get['context']]))
{
$this->activeFiles[$view_name][$get['context']] = '';
}
// start little bucket
$bucket = array();
// load the headers if text
if (isset($view->params[$key][$get['field']]) && ComponentbuilderHelper::checkString($view->params[$key][$get['field']]))
{
$bucket[] = $view->params[$key][$get['field']];
}
// load the headers if power
if (isset($view->params[$key]['power_' . $get['field']]) && ComponentbuilderHelper::checkArray($view->params[$key]['power_' . $get['field']], true)
&& ($use = $this->getPowers($view->params[$key]['power_' . $get['field']])) !== false)
{
$bucket[] = 'use ' . implode(';' . PHP_EOL . 'use ', $use) . ';';
}
// we start this field if not already
if (ComponentbuilderHelper::checkArray($bucket, true))
{
$this->activeFiles[$view_name][$get['context']] .= implode(PHP_EOL, $bucket);
}
}
}
/**
* get the powers header use strings
*
* @return string
*
* @since 1.0.6
*/
protected function getPowers($ids)
{
// if none are found
$namspaces = false;
// add to active powers
if (($namspaces = ComponentbuilderHelper::getVars('power', $ids, 'id', 'namespace')) !== false)
{
foreach ($ids as $id)
{
$this->linkedPowers[$id] = $id;
}
// convert the dots to namespace
$namspaces = array_map( function($namespace) {
return ComponentbuilderHelper::safeNamespace(
str_replace(
array_keys($this->placeholders),
array_values($this->placeholders),
str_replace('.', '\\', $namespace)
)
);
}, $namspaces );
}
return $namspaces;
}
2021-10-14 14:31:59 +00:00
/**
* 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
2021-12-03 02:46:17 +00:00
if (empty($header) || (isset($this->activeFiles[$view_name][$event_context]) && strpos(
2021-10-14 14:31:59 +00:00
$this->activeFiles[$view_name][$event_context], $header
2021-12-03 02:46:17 +00:00
) !== false))
2021-10-14 14:31:41 +00:00
{
// 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
}
}
2021-12-03 02:46:17 +00:00
/**
* Event Triggered in the compiler [on Before Get Component Data]
*
* @return void
*
* @since 1.0.6
*/
public function jcb_ce_onBeforeGetComponentData(&$context, $compiler)
{
// get placeholders from the compiler
$this->placeholders = $compiler->globalPlaceholders;
}
/**
* Event Triggered in the compiler [on After Get Component Data]
*
* @return void
*
* @since 1.0.6
*/
public function jcb_ce_onAfterGetComponentData(&$context, $compiler)
{
// add the powers to the component
if (ComponentbuilderHelper::checkArray($this->linkedPowers, true))
{
$compiler->linkedPowers = $this->linkedPowers;
}
}
2021-10-14 14:31:41 +00:00
}