primo commit

This commit is contained in:
2024-12-17 17:34:10 +01:00
commit e650f8df99
16435 changed files with 2451012 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,40 @@
<?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\View\Compiler;
defined('_JEXEC') || die;
interface CompilerInterface
{
/**
* Are the results of this compiler engine cacheable? If the engine makes use of the forcedParams it must return
* false.
*
* @return bool
*/
public function isCacheable(): bool;
/**
* Compile a view template into PHP and HTML
*
* @param string $path The absolute filesystem path of the view template
* @param array $forceParams Any parameters to force (only for engines returning raw HTML)
*
* @return mixed
*/
public function compile(string $path, array $forceParams = []);
/**
* Returns the file extension supported by this compiler
*
* @return string
*
* @since 3.3.1
*/
public function getFileExtension(): string;
}

View File

@ -0,0 +1,274 @@
<?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\View\DataView;
defined('_JEXEC') or die;
use FOF40\Container\Container;
use FOF40\Model\DataModel;
use FOF40\View\Exception\AccessForbidden;
use Joomla\CMS\Document\Document;
class Csv extends Html implements DataViewInterface
{
/**
* Should I produce a CSV header row.
*
* @var boolean
*/
protected $csvHeader = true;
/**
* The filename of the downloaded CSV file.
*
* @var string
*/
protected $csvFilename;
/**
* The columns to include in the CSV output. If it's empty it will be ignored.
*
* @var array
*/
protected $csvFields = [];
/**
* Public constructor.
*
* @param Container $container The container we belong to
* @param array $config The configuration overrides for the view
*/
public function __construct(Container $container, array $config = [])
{
parent::__construct($container, $config);
if (array_key_exists('csv_header', $config))
{
$this->csvHeader = $config['csv_header'];
}
else
{
$this->csvHeader = $this->input->getBool('csv_header', true);
}
if (array_key_exists('csv_filename', $config))
{
$this->csvFilename = $config['csv_filename'];
}
else
{
$this->csvFilename = $this->input->getString('csv_filename', '');
}
if (empty($this->csvFilename))
{
$view = $this->input->getCmd('view', 'cpanel');
$view = $this->container->inflector->pluralize($view);
$this->csvFilename = strtolower($view) . '.csv';
}
if (array_key_exists('csv_fields', $config))
{
$this->csvFields = $config['csv_fields'];
}
}
/**
* Overrides the default method to execute and display a template script.
* Instead of loadTemplate is uses loadAnyTemplate.
*
* @param string $tpl The name of the template file to parse
*
* @return boolean True on success
*
* @throws \Exception When the layout file is not found
*/
public function display($tpl = null)
{
$eventName = 'onBefore' . ucfirst($this->doTask);
$this->triggerEvent($eventName, [$tpl]);
// Load the model
/** @var DataModel $model */
$model = $this->getModel();
$items = $model->get();
$this->items = $items;
$platform = $this->container->platform;
$document = $platform->getDocument();
if ($document instanceof Document)
{
$document->setMimeEncoding('text/csv');
}
$platform->setHeader('Pragma', 'public');
$platform->setHeader('Expires', '0');
/**
* This construct is required to work around bad quality hosts who blacklist files based on broken malware
* scanners. The only way to beat them is... wait for it... write our software using the same obscure constructs
* actual malware is using to evade these broken malware scanners. The irony is not lost on me.
*/
$xo = substr("revenge", 0, 3);
$xoxo = substr("calibrate", 1, 2);
$platform->setHeader('Cache-Control', 'must-' . $xo . $xoxo . 'idate, post-check=0, pre-check=0');
$platform->setHeader('Cache-Control', 'public', false);
$platform->setHeader('Content-Description', 'File Transfer');
$platform->setHeader('Content-Disposition', 'attachment; filename="' . $this->csvFilename . '"');
if (is_null($tpl))
{
$tpl = 'csv';
}
$hasFailed = false;
try
{
$result = $this->loadTemplate($tpl, true);
if ($result instanceof \Exception)
{
$hasFailed = true;
}
}
catch (\Exception $e)
{
$hasFailed = true;
}
if (!$hasFailed)
{
echo $result;
}
else
{
// Default CSV behaviour in case the template isn't there!
if (count($items) === 0)
{
throw new AccessForbidden;
}
$item = $items->last();
$keys = $item->getData();
$keys = array_keys($keys);
reset($items);
if (!empty($this->csvFields))
{
$temp = [];
foreach ($this->csvFields as $f)
{
$exist = false;
// If we have a dot and it isn't part of the field name, we are dealing with relations
if (!$model->hasField($f) && strpos($f, '.'))
{
$methods = explode('.', $f);
$object = $item;
// Let's see if the relation exists
foreach ($methods as $method)
{
if (isset($object->$method) || property_exists($object, $method))
{
$exist = true;
$object = $object->$method;
}
else
{
$exist = false;
break;
}
}
}
if (in_array($f, $keys))
{
$temp[] = $f;
}
elseif ($exist)
{
$temp[] = $f;
}
}
$keys = $temp;
}
if ($this->csvHeader)
{
$csv = [];
foreach ($keys as $k)
{
$k = str_replace('"', '""', $k);
$k = str_replace("\r", '\\r', $k);
$k = str_replace("\n", '\\n', $k);
$k = '"' . $k . '"';
$csv[] = $k;
}
echo implode(",", $csv) . "\r\n";
}
foreach ($items as $item)
{
$csv = [];
foreach ($keys as $k)
{
// If our key contains a dot and it isn't part of the field name, we are dealing with relations
if (!$model->hasField($k) && strpos($k, '.'))
{
$methods = explode('.', $k);
$v = $item;
foreach ($methods as $method)
{
$v = $v->$method;
}
}
else
{
$v = $item->$k;
}
if (is_array($v))
{
$v = 'Array';
}
elseif (is_object($v))
{
$v = 'Object';
}
$v = str_replace('"', '""', $v);
$v = str_replace("\r", '\\r', $v);
$v = str_replace("\n", '\\n', $v);
$v = '"' . $v . '"';
$csv[] = $v;
}
echo implode(",", $csv) . "\r\n";
}
}
$eventName = 'onAfter' . ucfirst($this->doTask);
$this->triggerEvent($eventName, [$tpl]);
return true;
}
}

View File

@ -0,0 +1,144 @@
<?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\View\DataView;
defined('_JEXEC') || die;
use FOF40\Container\Container;
use Joomla\CMS\Pagination\Pagination;
interface DataViewInterface
{
/**
* Determines if the current Joomla! version and your current table support AJAX-powered drag and drop reordering.
* If they do, it will set up the drag & drop reordering feature.
*
* @return boolean|array False if not supported, otherwise a table with necessary information (saveOrder: should
* you enable DnD reordering; orderingColumn: which column has the ordering information).
*/
public function hasAjaxOrderingSupport();
/**
* Returns the internal list of useful variables to the benefit of header fields.
*
* @return \stdClass
*/
public function getLists();
/**
* Returns a reference to the permissions object of this view
*
* @return \stdClass
*/
public function getPerms();
/**
* Returns a reference to the pagination object of this view
*
* @return Pagination
*/
public function getPagination();
/**
* Method to get the view name
*
* The model name by default parsed using the classname, or it can be set
* by passing a $config['name'] in the class constructor
*
* @return string The name of the model
*
* @throws \Exception
*/
public function getName();
/**
* Returns a reference to the container attached to this View
*
* @return Container
*/
public function &getContainer();
/**
* Escapes a value for output in a view script.
*
* @param mixed $var The output to escape.
*
* @return string The escaped value.
*/
public function escape($var);
/**
* Returns the task being rendered by the view
*
* @return string
*/
public function getTask();
/**
* Get the layout.
*
* @return string The layout name
*/
public function getLayout();
/**
* Add a JS script file to the page generated by the CMS.
*
* There are three combinations of defer and async (see http://www.w3schools.com/tags/att_script_defer.asp):
* * $defer false, $async true: The script is executed asynchronously with the rest of the page
* (the script will be executed while the page continues the parsing)
* * $defer true, $async false: The script is executed when the page has finished parsing.
* * $defer false, $async false. (default) The script is loaded and executed immediately. When it finishes
* loading the browser continues parsing the rest of the page.
*
* When you are using $defer = true there is no guarantee about the load order of the scripts. Whichever
* script loads first will be executed first. The order they appear on the page is completely irrelevant.
*
* @param string $uri A path definition understood by parsePath, e.g. media://com_example/js/foo.js
* @param string $version (optional) Version string to be added to the URL
* @param string $type MIME type of the script
* @param boolean $defer Adds the defer attribute, see above
* @param boolean $async Adds the async attribute, see above
*
* @return $this Self, for chaining
*/
public function addJavascriptFile($uri, $version = null, $type = 'text/javascript', $defer = false, $async = false);
/**
* Adds an inline JavaScript script to the page header
*
* @param string $script The script content to add
* @param string $type The MIME type of the script
*
* @return $this Self, for chaining
*/
public function addJavascriptInline($script, $type = 'text/javascript');
/**
* Add a CSS file to the page generated by the CMS
*
* @param string $uri A path definition understood by parsePath, e.g. media://com_example/css/foo.css
* @param string $version (optional) Version string to be added to the URL
* @param string $type MIME type of the stylesheeet
* @param string $media Media target definition of the style sheet, e.g. "screen"
* @param array $attribs Array of attributes
*
* @return $this Self, for chaining
*/
public function addCssFile($uri, $version = null, $type = 'text/css', $media = null, $attribs = array());
/**
* Adds an inline stylesheet (inline CSS) to the page header
*
* @param string $css The stylesheet content to add
* @param string $type The MIME type of the script
*
* @return $this Self, for chaining
*/
public function addCssInline($css, $type = 'text/css');
}

View File

@ -0,0 +1,200 @@
<?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\View\DataView;
defined('_JEXEC') || die;
use FOF40\Render\RenderInterface;
use Joomla\CMS\Application\SiteApplication;
use Joomla\CMS\Factory as JoomlaFactory;
use Joomla\CMS\Language\Text;
class Html extends Raw implements DataViewInterface
{
/** @var bool Should I set the page title in the front-end of the site? */
public $setFrontendPageTitle = false;
/** @var string The translation key for the default page title */
public $defaultPageTitle;
/**
* Should FEFHelpBrowse::orderheader() render the pagination (items per page) dropdown?
*
* @var bool
* @since 4.0.0
*/
public $showBrowsePagination = true;
/**
* Should FEFHelpBrowse::orderheader() render the ordering direction dropdown?
*
* @var bool
* @since 4.0.0
*/
public $showBrowseOrdering = true;
/**
* Should FEFHelpBrowse::orderheader() render the order by item dropdown?
*
* @var bool
* @since 4.0.0
*/
public $showBrowseOrderBy = true;
public function setPageTitle()
{
if (!$this->container->platform->isFrontend())
{
return '';
}
/** @var SiteApplication $app */
$app = JoomlaFactory::getApplication();
$document = JoomlaFactory::getDocument();
$menus = $app->getMenu();
$menu = $menus->getActive();
// Get the option and view name
$option = $this->container->componentName;
$view = $this->getName();
// Get the default page title translation key
$default = empty($this->defaultPageTitle) ? $option . '_TITLE_' . $view : $this->defaultPageTitle;
$params = $app->getParams($option);
// Set the default value for page_heading
$params->def('page_heading', ($menu !== null) ? $params->get('page_title', $menu->title) : Text::_($default));
// Set the document title
$title = $params->get('page_title', '');
$sitename = $app->get('sitename');
if ($title == $sitename)
{
$title = Text::_($default);
}
if (empty($title))
{
$title = $sitename;
}
elseif ($app->get('sitename_pagetitles', 0) == 1)
{
$title = Text::sprintf('JPAGETITLE', $app->get('sitename'), $title);
}
elseif ($app->get('sitename_pagetitles', 0) == 2)
{
$title = Text::sprintf('JPAGETITLE', $title, $app->get('sitename'));
}
$document->setTitle($title);
// Set meta
if ($params->get('menu-meta_description'))
{
$document->setDescription($params->get('menu-meta_description'));
}
if ($params->get('menu-meta_keywords'))
{
$document->setMetadata('keywords', $params->get('menu-meta_keywords'));
}
if ($params->get('robots'))
{
$document->setMetadata('robots', $params->get('robots'));
}
return $title;
}
protected function initialise(): void
{
$view = $this->getName();
$task = $this->task;
$renderer = $this->container->renderer;
$renderer->initialise($view, $task);
}
/**
* Runs before rendering the view template, echoing HTML to put before the
* view template's generated HTML
*
* @return void
*
* @throws \Exception
*/
protected function preRender(): void
{
$view = $this->getName();
$task = $this->task;
// Don't load the toolbar on CLI
$platform = $this->container->platform;
if (!$platform->isCli())
{
$toolbar = $this->container->toolbar;
$toolbar->perms = $this->permissions;
$toolbar->renderToolbar($view, $task);
}
if ($platform->isFrontend() && $this->setFrontendPageTitle)
{
$this->setPageTitle();
}
$renderer = $this->container->renderer;
$renderer->preRender($view, $task);
}
/**
* Runs after rendering the view template, echoing HTML to put after the
* view template's generated HTML
*
* @return void
*
* @throws \Exception
*/
protected function postRender(): void
{
$view = $this->getName();
$task = $this->task;
$renderer = $this->container->renderer;
if ($renderer instanceof RenderInterface)
{
$renderer->postRender($view, $task);
}
}
/**
* Executes before rendering the page for the Add task.
*/
protected function onBeforeAdd()
{
// Hide main menu
JoomlaFactory::getApplication()->input->set('hidemainmenu', true);
parent::onBeforeAdd();
}
/**
* Executes before rendering the page for the Edit task.
*/
protected function onBeforeEdit()
{
// Hide main menu
JoomlaFactory::getApplication()->input->set('hidemainmenu', true);
parent::onBeforeEdit();
}
}

View File

@ -0,0 +1,296 @@
<?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\View\DataView;
defined('_JEXEC') || die;
use FOF40\Model\DataModel;
use Joomla\CMS\Document\Document as JoomlaDocument;
use Joomla\CMS\Document\JsonDocument;
use Joomla\CMS\Uri\Uri;
class Json extends Raw implements DataViewInterface
{
/**
* Set to true if your onBefore* methods have already populated the item, items, limitstart etc properties used to
* render a JSON document.
*
* @var bool
*/
public $alreadyLoaded = false;
/**
* Record listing offset (how many records to skip before starting showing some)
*
* @var int
*/
protected $limitStart = 0;
/**
* Record listing limit (how many records to show)
*
* @var int
*/
protected $limit = 10;
/**
* Total number of records in the result set
*
* @var int
*/
protected $total = 0;
/**
* The record being displayed
*
* @var DataModel
*/
protected $item;
/**
* Overrides the default method to execute and display a template script.
* Instead of loadTemplate is uses loadAnyTemplate.
*
* @param string $tpl The name of the template file to parse
*
* @return boolean True on success
*
* @throws \Exception When the layout file is not found
*/
public function display($tpl = null)
{
$eventName = 'onBefore' . ucfirst($this->doTask);
$this->triggerEvent($eventName, [$tpl]);
$eventName = 'onAfter' . ucfirst($this->doTask);
$this->triggerEvent($eventName, [$tpl]);
return true;
}
/**
* The event which runs when we are displaying the record list JSON view
*
* @param string $tpl The sub-template to use
*/
public function onBeforeBrowse($tpl = null)
{
// Load the model
/** @var DataModel $model */
$model = $this->getModel();
$result = '';
if (!$this->alreadyLoaded)
{
$this->limitStart = $model->getState('limitstart', 0);
$this->limit = $model->getState('limit', 0);
$this->items = $model->get(true, $this->limitStart, $this->limit);
$this->total = $model->count();
}
$document = $this->container->platform->getDocument();
/** @var JsonDocument $document */
if ($document instanceof JoomlaDocument)
{
$document->setMimeEncoding('application/json');
}
if (is_null($tpl))
{
$tpl = 'json';
}
$hasFailed = false;
try
{
$result = $this->loadTemplate($tpl, true);
if ($result instanceof \Exception)
{
$hasFailed = true;
}
}
catch (\Exception $e)
{
$hasFailed = true;
}
if ($hasFailed)
{
// Default JSON behaviour in case the template isn't there!
$result = [];
foreach ($this->items as $item)
{
$result[] = (is_object($item) && method_exists($item, 'toArray')) ? $item->toArray() : $item;
}
$json = json_encode($result, JSON_PRETTY_PRINT);
// JSONP support
$callback = $this->input->get('callback', null, 'raw');
if (!empty($callback))
{
echo $callback . '(' . $json . ')';
}
else
{
$defaultName = $this->input->get('view', 'main', 'cmd');
$filename = $this->input->get('basename', $defaultName, 'cmd');
$document->setName($filename);
echo $json;
}
}
else
{
echo $result;
}
}
/**
* The event which runs when we are displaying a single item JSON view
*
* @param string $tpl The view sub-template to use
*/
protected function onBeforeRead($tpl = null)
{
self::renderSingleItem($tpl);
}
/**
* The event which runs when we are displaying a single item JSON view
*
* @param string $tpl The view sub-template to use
*/
protected function onAfterSave($tpl = null)
{
self::renderSingleItem($tpl);
}
/**
* Renders a single item JSON view
*
* @param string $tpl The view sub-template to use
*/
protected function renderSingleItem($tpl)
{
// Load the model
/** @var DataModel $model */
$model = $this->getModel();
$result = '';
if (!$this->alreadyLoaded)
{
$this->item = $model->find();
}
$document = $this->container->platform->getDocument();
/** @var JsonDocument $document */
if ($document instanceof JoomlaDocument)
{
$document->setMimeEncoding('application/json');
}
if (is_null($tpl))
{
$tpl = 'json';
}
$hasFailed = false;
try
{
$result = $this->loadTemplate($tpl, true);
if ($result instanceof \Exception)
{
$hasFailed = true;
}
}
catch (\Exception $e)
{
$hasFailed = true;
}
if ($hasFailed)
{
$data = (is_object($this->item) && method_exists($this->item, 'toArray')) ? $this->item->toArray() : $this->item;
$json = json_encode($data, JSON_PRETTY_PRINT);
// JSONP support
$callback = $this->input->get('callback');
if (!empty($callback))
{
echo $callback . '(' . $json . ')';
}
else
{
$defaultName = $this->input->get('view', 'main', 'cmd');
$filename = $this->input->get('basename', $defaultName, 'cmd');
$document->setName($filename);
echo $json;
}
}
else
{
echo $result;
}
}
/**
* Convert an absolute URI to a relative one
*
* @param string $uri The URI to convert
*
* @return string The relative URL
*/
protected function _removeURIBase($uri)
{
static $root = null, $rootlen = 0;
if (is_null($root))
{
$root = rtrim(Uri::base(false), '/');
$rootlen = strlen($root);
}
if (substr($uri, 0, $rootlen) == $root)
{
$uri = substr($uri, $rootlen);
}
return ltrim($uri, '/');
}
/**
* Returns a Uri instance with a prototype URI used as the base for the
* other URIs created by the JSON renderer
*
* @return Uri The prototype Uri instance
*/
protected function _getPrototypeURIForPagination()
{
$protoUri = new Uri('index.php');
$protoUri->setQuery($this->input->getData());
$protoUri->delVar('savestate');
$protoUri->delVar('base_path');
return $protoUri;
}
}

View File

@ -0,0 +1,367 @@
<?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\View\DataView;
defined('_JEXEC') || die;
use FOF40\Container\Container;
use FOF40\Model\DataModel;
use FOF40\Model\DataModel\Collection;
use FOF40\View\View;
use Joomla\CMS\Application\SiteApplication;
use Joomla\CMS\Factory as JoomlaFactory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Pagination\Pagination;
use Joomla\Registry\Registry;
/**
* View for a raw data-driven view
*/
class Raw extends View implements DataViewInterface
{
/** @var \stdClass Data lists */
protected $lists;
/** @var Pagination The pagination object */
protected $pagination;
/** @var Registry Page parameters object, for front-end views */
protected $pageParams;
/** @var Collection The records loaded (browse views) */
protected $items;
/** @var DataModel The record loaded (read, edit, add views) */
protected $item;
/** @var int The total number of items in the model (more than those loaded) */
protected $itemCount = 0;
/** @var \stdClass ACL permissions map */
protected $permissions;
/** @var array Additional permissions to fetch on object creation, see getPermissions() */
protected $additionalPermissions = [];
/**
* Overrides the constructor to apply Joomla! ACL permissions
*
* @param Container $container The container we belong to
* @param array $config The configuration overrides for the view
*/
public function __construct(Container $container, array $config = [])
{
parent::__construct($container, $config);
$this->permissions = $this->getPermissions(null, $this->additionalPermissions);
}
/**
* Determines if the current Joomla! version and your current table support AJAX-powered drag and drop reordering.
* If they do, it will set up the drag & drop reordering feature.
*
* @return null|array Null if not supported, otherwise a table with necessary information (saveOrder: should
* you enable DnD reordering; orderingColumn: which column has the ordering information).
*/
public function hasAjaxOrderingSupport(): ?array
{
/** @var DataModel $model */
$model = $this->getModel();
if (!$model->hasField('ordering'))
{
return null;
}
$listOrder = $this->escape($model->getState('filter_order', null, 'cmd'));
$listDir = $this->escape($model->getState('filter_order_Dir', null, 'cmd'));
$saveOrder = $listOrder == $model->getFieldAlias('ordering');
$saveOrderingUrl = '';
if ($saveOrder)
{
$saveOrderingUrl = 'index.php?option=' . $this->container->componentName . '&view=' . $this->getName() . '&task=saveorder&format=json';
$helper = version_compare(JVERSION, '3.999.999', 'le') ? 'sortablelist.sortable' : 'draggablelist.draggable';
HtmlHelper::_($helper, 'itemsList', 'adminForm', strtolower($listDir), $saveOrderingUrl);
}
return [
'saveOrder' => $saveOrder,
'saveOrderURL' => $saveOrderingUrl . '&' . $this->container->platform->getToken() . '=1',
'orderingColumn' => $model->getFieldAlias('ordering'),
];
}
/**
* Returns the internal list of useful variables to the benefit of header fields.
*
* @return \stdClass
*/
public function getLists()
{
return $this->lists;
}
/**
* Returns a reference to the permissions object of this view
*
* @return \stdClass
*/
public function getPerms()
{
return $this->permissions;
}
/**
* Returns a reference to the pagination object of this view
*
* @return Pagination
*/
public function getPagination()
{
return $this->pagination;
}
/**
* Get the items collection for browse views
*
* @return Collection
*/
public function getItems()
{
return $this->items;
}
/**
* Get the item for read, edit, add views
*
* @return DataModel
*/
public function getItem()
{
return $this->item;
}
/**
* Get the items count for browse views
*
* @return int
*/
public function getItemCount()
{
return $this->itemCount;
}
/**
* Get the Joomla! page parameters
*
* @return Registry
*/
public function getPageParams()
{
return $this->pageParams;
}
/**
* Returns a permissions object.
*
* The additionalPermissions array is a hashed array of local key => Joomla! ACL key value pairs. Local key is the
* name of the permission in the permissions object, whereas Joomla! ACL key is the name of the ACL permission
* known to Joomla! e.g. "core.manage", "foobar.something" and so on.
*
* Note: on CLI applications all permissions are set to TRUE. There is no ACL check there.
*
* @param null|string $component The name of the component. Leave empty for automatic detection.
* @param array $additionalPermissions Any additional permissions you want to add to the object.
*
* @return object
*/
protected function getPermissions($component = null, array $additionalPermissions = [])
{
// Make sure we have a component
if (empty($component))
{
$component = $this->container->componentName;
}
// Initialise with all true
$permissions = [
'create' => true,
'edit' => true,
'editown' => true,
'editstate' => true,
'delete' => true,
];
foreach (array_keys($additionalPermissions) as $localKey)
{
$permissions[$localKey] = true;
}
$platform = $this->container->platform;
// If this is a CLI application we don't make any ACL checks
if ($platform->isCli())
{
return (object) $permissions;
}
// Get the core permissions
$permissions = [
'create' => $platform->authorise('core.create', $component),
'edit' => $platform->authorise('core.edit', $component),
'editown' => $platform->authorise('core.edit.own', $component),
'editstate' => $platform->authorise('core.edit.state', $component),
'delete' => $platform->authorise('core.delete', $component),
];
foreach ($additionalPermissions as $localKey => $joomlaPermission)
{
$permissions[$localKey] = $platform->authorise($joomlaPermission, $component);
}
return (object) $permissions;
}
/**
* Executes before rendering the page for the Browse task.
*/
protected function onBeforeBrowse()
{
// Create the lists object
$this->lists = new \stdClass();
// Load the model
/** @var DataModel $model */
$model = $this->getModel();
// We want to persist the state in the session
$model->savestate(1);
// Display limits
$defaultLimit = 20;
if (!$this->container->platform->isCli() && class_exists('\Joomla\CMS\Factory'))
{
$app = JoomlaFactory::getApplication();
$defaultLimit = method_exists($app, 'get') ? $app->get('list_limit') : 20;
}
$this->lists->limitStart = $model->getState('limitstart', 0, 'int');
$this->lists->limit = $model->getState('limit', $defaultLimit, 'int');
$model->limitstart = $this->lists->limitStart;
$model->limit = $this->lists->limit;
// Assign items to the view
$this->items = $model->get(false);
$this->itemCount = $model->count();
// Ordering information
$this->lists->order = $model->getState('filter_order', $model->getIdFieldName(), 'cmd');
$this->lists->order_Dir = $model->getState('filter_order_Dir', null, 'cmd');
if ($this->lists->order_Dir)
{
$this->lists->order_Dir = strtolower($this->lists->order_Dir);
}
// Pagination
$this->pagination = new Pagination($this->itemCount, $this->lists->limitStart, $this->lists->limit);
// Pass page params on frontend only
if ($this->container->platform->isFrontend())
{
/** @var SiteApplication $app */
$app = JoomlaFactory::getApplication();
$params = $app->getParams();
$this->pageParams = $params;
}
}
/**
* Executes before rendering the page for the add task.
*/
protected function onBeforeAdd()
{
/** @var DataModel $model */
$model = $this->getModel();
/**
* The model is pushed into the View by the Controller. As you can see in DataController::add() it is possible
* to push both default values (defaultsForAdd) as well as data from the state (e.g. when saving a new record
* failed for some reason and the user needs to edit it). That's why we populate defaultFields from $model. We
* still do a full reset on a clone of the Model to get a clean object and merge default values (instead of null
* values) with the data pushed by the controller.
*/
$defaultFields = $model->getData();
$this->item = $model->getClone()->reset(true, true);
foreach ($defaultFields as $k => $v)
{
try
{
$this->item->setFieldValue($k, $v);
}
catch (\Exception $e)
{
// Suppress errors in field assignments at this stage
}
}
}
/**
* Executes before rendering the page for the Edit task.
*/
protected function onBeforeEdit()
{
/** @var DataModel $model */
$model = $this->getModel();
// It seems that I can't edit records, maybe I can edit only this one due asset tracking?
if ((!$this->permissions->edit || !$this->permissions->editown) && is_object($model) && ($model instanceof DataModel))
{
// Make sure the model is really asset tracked
$assetName = $model->getAssetName();
$assetName = is_string($assetName) ? $assetName : null;
$isAssetsTracked = $model->isAssetsTracked() && !empty($assetName);
// Ok, record is tracked, let's see if I can this record
if ($isAssetsTracked)
{
$platform = $this->container->platform;
if (!$this->permissions->edit && !is_null($assetName))
{
$this->permissions->edit = $platform->authorise('core.edit', $assetName);
}
if (!$this->permissions->editown && !is_null($assetName))
{
$this->permissions->editown = $platform->authorise('core.edit.own', $assetName);
}
}
}
$this->item = $model->findOrFail();
}
/**
* Executes before rendering the page for the Read task.
*/
protected function onBeforeRead()
{
/** @var DataModel $model */
$model = $this->getModel();
$this->item = $model->findOrFail();
}
}

View File

@ -0,0 +1,45 @@
<?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\View\Engine;
defined('_JEXEC') || die;
use FOF40\View\View;
abstract class AbstractEngine implements EngineInterface
{
/** @var View The view we belong to */
protected $view;
/**
* Public constructor
*
* @param View $view The view we belong to
*/
public function __construct(View $view)
{
$this->view = $view;
}
/**
* Get the include path for a parsed view template
*
* @param string $path The path to the view template
* @param array $forceParams Any additional information to pass to the view template engine
*
* @return array Content 3ναlυα+ιοη information (I use leetspeak here because of bad quality hosts with broken
* scanners)
*/
public function get($path, array $forceParams = [])
{
return [
'type' => 'raw',
'content' => '',
];
}
}

View File

@ -0,0 +1,26 @@
<?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\View\Engine;
defined('_JEXEC') || die;
use FOF40\View\View;
/**
* View engine for compiling PHP template files.
*/
class BladeEngine extends CompilingEngine implements EngineInterface
{
public function __construct(View $view)
{
parent::__construct($view);
// Assign the Blade compiler to this engine
$this->compiler = $view->getContainer()->blade;
}
}

View File

@ -0,0 +1,272 @@
<?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\View\Engine;
defined('_JEXEC') || die;
use FOF40\Utils\Buffer;
use FOF40\View\Compiler\CompilerInterface;
use FOF40\View\Exception\PossiblySuhosin;
use Joomla\CMS\Filesystem\File;
/**
* View engine for compiling PHP template files.
*/
abstract class CompilingEngine extends AbstractEngine implements EngineInterface
{
/** @var CompilerInterface The compiler used by this engine */
protected $compiler;
/**
* Get the 3ναlυa+3d contents of the view template. (I use leetspeak here because of bad quality hosts with broken
* scanners)
*
* @param string $path The path to the view template
* @param array $forceParams Any additional information to pass to the view template engine
*
* @return array Content evaluation information
*/
public function get($path, array $forceParams = [])
{
// If it's cached return the path to the cached file's path
if ($this->isCached($path))
{
return [
'type' => 'path',
'content' => $this->getCachePath($path),
];
}
/**
* Compile and cache the file. We also add the file path in a comment at the top of the file so phpStorm can
* debug it.
*
* @see https://blog.jetbrains.com/phpstorm/2019/02/phpstorm-2019-1-eap-191-5849-26/
* @see https://laravel-news.com/laravel-5-8-blade-template-file-path
*/
$content = "<?php /* $path */ ?>\n";
$content .= $this->compile($path, $forceParams);
$cachePath = $this->putToCache($path, $content);
$isPHPFile = substr($path, -4) == '.php';
// If we could cache it, return the cached file's path
if ($cachePath !== false)
{
// Bust the opcode cache for .php files
if ($isPHPFile)
{
$this->bustOpCache($path);
}
return [
'type' => 'path',
'content' => $cachePath,
];
}
// We could not write to the cache. Hm, can I use a stream wrapper?
$canUseStreams = Buffer::canRegisterWrapper();
if ($canUseStreams)
{
$id = $this->getIdentifier($path);
$streamPath = 'fof://' . $this->view->getContainer()->componentName . '/compiled_templates/' . $id . '.php';
file_put_contents($streamPath, $content);
// Bust the opcode cache for .php files
if ($isPHPFile)
{
$this->bustOpCache($path);
}
return [
'type' => 'path',
'content' => $streamPath,
];
}
// I couldn't use a stream wrapper. I have to give up.
throw new PossiblySuhosin;
}
/**
* Returns the path where I can find a precompiled version of the uncompiled view template which lives in $path
*
* @param string $path The path to the uncompiled view template
*
* @return bool|string False if the view template is outside the component's front- or backend.
*
* @since 3.3.1
*/
public function getPrecompiledPath($path)
{
// Normalize the path to the file
$path = realpath($path);
if ($path === false)
{
// The file doesn't exist
return false;
}
// Is this path under the component's front- or backend?
$frontendPath = realpath($this->view->getContainer()->frontEndPath);
$backendPath = realpath($this->view->getContainer()->backEndPath);
$backPos = strpos($path, $backendPath);
$frontPos = strpos($path, $frontendPath);
if (($backPos !== 0) && ($frontPos !== 0))
{
// This is not a view template shipped with the component, i.e. it can't be precompiled
return false;
}
// Eliminate the component path from $path to get the relative path to the file
$componentPath = $frontendPath;
if ($backPos === 0)
{
$componentPath = $backendPath;
}
$relativePath = ltrim(substr($path, strlen($componentPath)), '\\/');
// Break down the relative path to its parts
$relativePath = str_replace('\\', '/', $relativePath);
$pathParts = explode('/', $relativePath);
// Remove the prefix
$prefix = array_shift($pathParts);
// If it's a legacy view, View, Views, or views prefix remove the 'tmpl' part
if (($prefix !== 'ViewTemplates') && ($prefix !== 'tmpl'))
{
unset($pathParts[1]);
}
// Get the last part and process the extension
$viewFile = array_pop($pathParts);
$extensionWithoutDot = $this->compiler->getFileExtension();
$pathParts[] = substr($viewFile, 0, -strlen($extensionWithoutDot)) . 'php';
$precompiledRelativePath = implode(DIRECTORY_SEPARATOR, $pathParts);
return $componentPath . DIRECTORY_SEPARATOR . 'PrecompiledTemplates' . DIRECTORY_SEPARATOR . $precompiledRelativePath;
}
/**
* A method to compile the raw view template into valid PHP
*
* @param string $path The path to the view template
* @param array $forceParams Any additional information to pass to the view template compiler
*
* @return string The template compiled to executable PHP
*/
protected function compile($path, array $forceParams = [])
{
return $this->compiler->compile($path, $forceParams);
}
protected function getIdentifier($path)
{
if (function_exists('sha1'))
{
return sha1($path);
}
return md5($path);
}
protected function getCachePath($path)
{
$id = $this->getIdentifier($path);
return JPATH_CACHE . '/' . $this->view->getContainer()->componentName . '/compiled_templates/' . $id . '.php';
}
protected function isCached($path)
{
if (!$this->compiler->isCacheable())
{
return false;
}
$cachePath = $this->getCachePath($path);
if (!file_exists($cachePath))
{
return false;
}
$cacheTime = filemtime($cachePath);
$fileTime = filemtime($path);
return $fileTime <= $cacheTime;
}
protected function getCached($path)
{
$cachePath = $this->getCachePath($path);
return file_get_contents($cachePath);
}
protected function putToCache($path, $content)
{
$cachePath = $this->getCachePath($path);
if (@file_put_contents($cachePath, $content))
{
return $cachePath;
}
if (File::write($cachePath, $content))
{
return $cachePath;
}
return false;
}
/**
* Bust the opcode cache for a given .php file
*
* This method can address opcode caching with:
* - Zend OPcache
* - Alternative PHP Cache (now defunct)
* - Windows Cache Extension for PHP (versions lower than 2.0.0)
* - XCache (now defunct)
*
* @param string $path The file to bus the cache for
*
* @return void
*/
private function bustOpCache($path)
{
if (function_exists('opcache_invalidate'))
{
opcache_invalidate($path);
}
if (function_exists('apc_compile_file'))
{
apc_compile_file($path);
}
if (function_exists('wincache_refresh_if_changed'))
{
wincache_refresh_if_changed([$path]);
}
if (function_exists('xcache_asm'))
{
xcache_asm($path);
}
}
}

View File

@ -0,0 +1,32 @@
<?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\View\Engine;
defined('_JEXEC') || die;
use FOF40\View\View;
interface EngineInterface
{
/**
* Public constructor
*
* @param View $view The view we belong to
*/
public function __construct(View $view);
/**
* Get the include path for a parsed view template
*
* @param string $path The path to the view template
* @param array $forceParams Any additional information to pass to the view template engine
*
* @return array Content 3ναlυα+ιοη information ['type' => 'raw|path', 'content' => 'path or raw content'] (I use leetspeak here because of bad quality hosts with broken scanners)
*/
public function get($path, array $forceParams = array());
}

View File

@ -0,0 +1,32 @@
<?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\View\Engine;
defined('_JEXEC') || die;
/**
* View engine for plain PHP template files (no translation).
*/
class PhpEngine extends AbstractEngine implements EngineInterface
{
/**
* Get the 3ναluα+3d contents of the view template. (I use leetspeak here because of bad quality hosts with broken scanners)
*
* @param string $path The path to the view template
* @param array $forceParams Any additional information to pass to the view template engine
*
* @return array Content 3ναlυα+ιοη information (I use leetspeak here because of asshole hosts with broken scanners)
*/
public function get($path, array $forceParams = array())
{
return array(
'type' => 'path',
'content' => $path
);
}
}

View File

@ -0,0 +1,31 @@
<?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\View\Exception;
defined('_JEXEC') || die;
use Exception;
use Joomla\CMS\Language\Text;
use RuntimeException;
/**
* Exception thrown when the access to the requested resource is forbidden under the current execution context
*/
class AccessForbidden extends RuntimeException
{
public function __construct(string $message = "", int $code = 403, Exception $previous = null)
{
if (empty($message))
{
$message = Text::_('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN');
}
parent::__construct($message, $code, $previous);
}
}

View File

@ -0,0 +1,30 @@
<?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\View\Exception;
defined('_JEXEC') || die;
use Exception;
use Joomla\CMS\Language\Text;
use RuntimeException;
/**
* Exception thrown when we can't get a Controller's name
*/
class CannotGetName extends RuntimeException
{
public function __construct(string $message = "", int $code = 500, Exception $previous = null)
{
if (empty($message))
{
$message = Text::_('LIB_FOF40_VIEW_ERR_GET_NAME');
}
parent::__construct($message, $code, $previous);
}
}

View File

@ -0,0 +1,27 @@
<?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\View\Exception;
defined('_JEXEC') || die;
use Exception;
use Joomla\CMS\Language\Text;
use RuntimeException;
/**
* Exception thrown when we are trying to operate on an empty section stack
*/
class EmptyStack extends RuntimeException
{
public function __construct(string $message = "", int $code = 500, Exception $previous = null)
{
$message = Text::_('LIB_FOF40_VIEW_EMPTYSECTIONSTACK');
parent::__construct($message, $code, $previous);
}
}

View File

@ -0,0 +1,27 @@
<?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\View\Exception;
defined('_JEXEC') || die;
use Exception;
use Joomla\CMS\Language\Text;
use RuntimeException;
/**
* Exception thrown when we can't get a Controller's name
*/
class ModelNotFound extends RuntimeException
{
public function __construct(string $path, string $viewName, int $code = 500, Exception $previous = null)
{
$message = Text::sprintf('LIB_FOF40_VIEW_MODELNOTINVIEW', $path, $viewName);
parent::__construct($message, $code, $previous);
}
}

View File

@ -0,0 +1,31 @@
<?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\View\Exception;
defined('_JEXEC') || die;
use Exception;
use Joomla\CMS\Language\Text;
use RuntimeException;
/**
* Exception thrown when the access to the requested resource is forbidden under the current execution context
*/
class PossiblySuhosin extends RuntimeException
{
public function __construct(string $message = "", int $code = 403, Exception $previous = null)
{
if (empty($message))
{
$message = Text::_('LIB_FOF40_VIEW_POSSIBLYSUHOSIN');
}
parent::__construct($message, $code, $previous);
}
}

View File

@ -0,0 +1,27 @@
<?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\View\Exception;
defined('_JEXEC') || die;
use Exception;
use InvalidArgumentException;
use Joomla\CMS\Language\Text;
/**
* Exception thrown when we can't figure out which engine to use for a view template
*/
class UnrecognisedExtension extends InvalidArgumentException
{
public function __construct(string $path, int $code = 500, Exception $previous = null)
{
$message = Text::sprintf('LIB_FOF40_VIEW_UNRECOGNISEDEXTENSION', $path);
parent::__construct($message, $code, $previous);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,628 @@
<?php
/**
* @package FOF
* @copyright Copyright (c)2010-2022 Nicholas K. Dionysopoulos / Akeeba Ltd
* @license GNU General Public License version 3, or later
*/
namespace FOF40\View;
defined('_JEXEC') || die;
use FOF40\Container\Container;
use Joomla\CMS\Language\Text;
use RuntimeException;
/**
* Locates the appropriate template file for a view
*/
class ViewTemplateFinder
{
/** @var View The view we are attached to */
protected $view;
/** @var Container The container of the view, for quick reference */
protected $container;
/** @var array The layout template extensions to look for */
protected $extensions = array('.blade.php', '.php');
/** @var string Default layout's name (default: "default") */
protected $defaultLayout = 'default';
/** @var string Default sub-template name (default: empty) */
protected $defaultTpl = '';
/** @var bool Should I only look in the specified view (true) or also the pluralised/singularised (false) */
protected $strictView = true;
/** @var bool Should I only look for the defined sub-template or also no sub-template? */
protected $strictTpl = true;
/** @var bool Should Should I only look for this layout or also the default layout? */
protected $strictLayout = true;
/** @var string Which application side prefix should I use by default (site, admin, auto, any) */
protected $sidePrefix = 'auto';
/**
* Public constructor. The config array can contain the following keys
* extensions array
* defaultLayout string
* defaultTpl string
* strictView bool
* strictTpl bool
* strictLayout bool
* sidePrefix string
* For the descriptions of each key please see the same-named property of this class
*
* @param View $view The view we are attached to
* @param array $config The configuration for this view template finder
*/
function __construct(View $view, array $config = array())
{
$this->view = $view;
$this->container = $view->getContainer();
if (isset($config['extensions']))
{
if (!is_array($config['extensions']))
{
$config['extensions'] = trim($config['extensions']);
$config['extensions'] = explode(',', $config['extensions']);
$config['extensions'] = array_map(function ($x) { return trim($x); }, $config['extensions']);
}
$this->setExtensions($config['extensions']);
}
if (isset($config['defaultLayout']))
{
$this->setDefaultLayout($config['defaultLayout']);
}
if (isset($config['defaultTpl']))
{
$this->setDefaultTpl($config['defaultTpl']);
}
if (isset($config['strictView']))
{
$config['strictView'] = in_array($config['strictView'], array(true, 'true', 'yes', 'on', 1));
$this->setStrictView($config['strictView']);
}
if (isset($config['strictTpl']))
{
$config['strictTpl'] = in_array($config['strictTpl'], array(true, 'true', 'yes', 'on', 1));
$this->setStrictTpl($config['strictTpl']);
}
if (isset($config['strictLayout']))
{
$config['strictLayout'] = in_array($config['strictLayout'], array(true, 'true', 'yes', 'on', 1));
$this->setStrictLayout($config['strictLayout']);
}
if (isset($config['sidePrefix']))
{
$this->setSidePrefix($config['sidePrefix']);
}
}
/**
* Returns a list of template URIs for a specific component, view, template and sub-template. The $parameters array
* can have any of the following keys:
* component string The name of the component, e.g. com_something
* view string The name of the view
* layout string The name of the layout
* tpl string The name of the sub-template
* strictView bool Should I only look in the specified view, or should I look in the pluralised/singularised view as well?
* strictLayout bool Should I only look for this layout, or also for the default layout?
* strictTpl bool Should I only look for this sub-template or also for no sub-template?
* sidePrefix string The application side prefix (site, admin, auto, any)
*
* @param array $parameters See above
*
* @return array
* @throws \Exception
*/
public function getViewTemplateUris(array $parameters)
{
// Merge the default parameters with the parameters given
$parameters = array_merge(array(
'component' => $this->container->componentName,
'view' => $this->view->getName(),
'layout' => $this->defaultLayout,
'tpl' => $this->defaultTpl,
'strictView' => $this->strictView,
'strictLayout' => $this->strictLayout,
'strictTpl' => $this->strictTpl,
'sidePrefix' => $this->sidePrefix,
), $parameters);
$uris = array();
$component = $parameters['component'];
$view = $parameters['view'];
$layout = $parameters['layout'];
$tpl = $parameters['tpl'];
$strictView = $parameters['strictView'];
$strictLayout = $parameters['strictLayout'];
$strictTpl = $parameters['strictTpl'];
$sidePrefix = $parameters['sidePrefix'];
$basePath = $sidePrefix. ':' . $component . '/' . $view . '/';
$uris[] = $basePath . $layout . ($tpl ? "_$tpl" : '');
if (!$strictTpl)
{
$uris[] = $basePath . $layout;
}
if (!$strictLayout)
{
$uris[] = $basePath . 'default' . ($tpl ? "_$tpl" : '');
if (!$strictTpl)
{
$uris[] = $basePath . 'default';
}
}
if (!$strictView)
{
$parameters['view'] = $this->container->inflector->isSingular($view) ? $this->container->inflector->pluralize($view) : $this->container->inflector->singularize($view);
$parameters['strictView'] = true;
$extraUris = $this->getViewTemplateUris($parameters);
$uris = array_merge($uris, $extraUris);
unset ($extraUris);
}
return array_unique($uris);
}
/**
* Parses a template URI in the form of admin:component/view/layout to an array listing the application section
* (admin, site), component, view and template referenced therein.
*
* @param string $uri The template path to parse
*
* @return array A hash array with the parsed path parts. Keys: admin, component, view, template
*/
public function parseTemplateUri($uri = '')
{
$parts = array(
'admin' => 0,
'component' => $this->container->componentName,
'view' => $this->view->getName(),
'template' => 'default'
);
if (substr($uri, 0, 5) == 'auto:')
{
$replacement = $this->container->platform->isBackend() ? 'admin:' : 'site:';
$uri = $replacement . substr($uri, 5);
}
if (substr($uri, 0, 6) == 'admin:')
{
$parts['admin'] = 1;
$uri = substr($uri, 6);
}
elseif (substr($uri, 0, 5) == 'site:')
{
$uri = substr($uri, 5);
}
elseif (substr($uri, 0, 4) == 'any:')
{
$parts['admin'] = -1;
$uri = substr($uri, 4);
}
if (empty($uri))
{
return $parts;
}
$uriParts = explode('/', $uri, 3);
$partCount = count($uriParts);
if ($partCount >= 1)
{
$parts['component'] = $uriParts[0];
}
if ($partCount >= 2)
{
$parts['view'] = $uriParts[1];
}
if ($partCount >= 3)
{
$parts['template'] = $uriParts[2];
}
return $parts;
}
/**
* Resolves a view template URI (e.g. any:com_foobar/Items/cheese) to an absolute filesystem path
* (e.g. /var/www/html/administrator/components/com_foobar/View/Items/tmpl/cheese.php)
*
* @param string $uri The view template URI to parse
* @param string $layoutTemplate The layout template override of the View class
* @param array $extraPaths Any extra lookup paths where we'll be looking for this view template
* @param bool $noOverride If true we will not load Joomla! template overrides. Useful when you want the
* template overrides to extend the original view template.
*
* @return string
*
*/
public function resolveUriToPath(string $uri, string $layoutTemplate = '', array $extraPaths = [], bool $noOverride = false)
{
// Parse the URI into its parts
$parts = $this->parseTemplateUri($uri);
// Get some useful values
$isAdmin = $this->container->platform->isBackend() ? 1 : 0;
$componentPaths = $this->container->platform->getComponentBaseDirs($parts['component']);
$templatePath = $this->container->platform->getTemplateOverridePath($parts['component']);
// Get the lookup paths
$paths = [];
// If we are on the correct side of the application or we have an "any:" URI look for a template override
if (!$noOverride && (($parts['admin'] == -1) || ($parts['admin'] == $isAdmin)))
{
$paths[] = $templatePath . '/' . $parts['view'];
}
// Add the requested side of the application
$requestedAdmin = ($parts['admin'] == -1) ? $isAdmin : $parts['admin'];
$paths[] = ($requestedAdmin ? $componentPaths['admin'] : $componentPaths['site']) . '/tmpl/' . $parts['view'];
$paths[] = ($requestedAdmin ? $componentPaths['admin'] : $componentPaths['site']) . '/ViewTemplates/' . $parts['view'];
// Add the other side of the application for "any:" URIs
if ($parts['admin'] == -1)
{
$paths[] = ($requestedAdmin ? $componentPaths['site'] : $componentPaths['admin']) . '/tmpl/' . $parts['view'];
$paths[] = ($requestedAdmin ? $componentPaths['site'] : $componentPaths['admin']) . '/ViewTemplates/' . $parts['view'];
}
// Add extra paths
if (!empty($extraPaths))
{
$paths = array_merge($paths, $extraPaths);
}
// Remove duplicate paths
$paths = array_unique($paths);
// Look for a template layout override
if (!empty($layoutTemplate) && ($layoutTemplate != '_') && ($layoutTemplate != $parts['template']))
{
$apath = array_shift($paths);
array_unshift($paths, str_replace($parts['template'], $layoutTemplate, $apath));
}
// Get the Joomla! version template suffixes
$jVersionSuffixes = array_merge($this->container->platform->getTemplateSuffixes(), ['']);
// Get the renderer name suffixes
$rendererNameSuffixes = [
'.' . $this->container->renderer->getInformation()->name,
'',
];
$filesystem = $this->container->filesystem;
// Try to find a view template in the component specified (or its view override)
foreach ($this->extensions as $extension)
{
foreach ($jVersionSuffixes as $JVersionSuffix)
{
foreach ($rendererNameSuffixes as $rendererNameSuffix)
{
$filenameToFind = $parts['template'] . $JVersionSuffix . $rendererNameSuffix . $extension;
$fileName = $filesystem->pathFind($paths, $filenameToFind);
if (!is_null($fileName))
{
return $fileName;
}
}
}
}
/**
* If no view template was found for the component fall back to FOF's core Blade templates -- located in
* <libdir>/ViewTemplates/<viewName>/<templateName> -- and their template overrides.
*/
$paths = [];
$paths[] = $this->container->platform->getTemplateOverridePath('lib_fof40') . '/' . $parts['view'];
$paths[] = realpath(__DIR__ . '/..') . '/ViewTemplates/' . $parts['view'];
foreach ($jVersionSuffixes as $JVersionSuffix)
{
foreach ($rendererNameSuffixes as $rendererNameSuffix)
{
$filenameToFind = $parts['template'] . $JVersionSuffix . $rendererNameSuffix . '.blade.php';
$fileName = $filesystem->pathFind($paths, $filenameToFind);
if (!is_null($fileName))
{
return $fileName;
}
}
}
// Nothing found, throw an error
throw new RuntimeException(Text::sprintf('JLIB_APPLICATION_ERROR_LAYOUTFILE_NOT_FOUND', $uri), 500);
}
/**
* Get the list of view template extensions
*
* @return array
*
* @codeCoverageIgnore
*/
public function getExtensions()
{
return $this->extensions;
}
/**
* Set the list of view template extensions
*
* @param array $extensions
*
* @return void
*
* @codeCoverageIgnore
*/
public function setExtensions(array $extensions)
{
$this->extensions = $extensions;
}
/**
* Add an extension to the list of view template extensions
*
* @param string $extension
*
* @return void
*/
public function addExtension($extension)
{
if (empty($extension))
{
return;
}
if (substr($extension, 0, 1) != '.')
{
$extension = '.' . $extension;
}
if (!in_array($extension, $this->extensions))
{
$this->extensions[] = $extension;
}
}
/**
* Remove an extension from the list of view template extensions
*
* @param string $extension
*
* @return void
*/
public function removeExtension($extension)
{
if (empty($extension))
{
return;
}
if (substr($extension, 0, 1) != '.')
{
$extension = '.' . $extension;
}
if (!in_array($extension, $this->extensions))
{
return;
}
$pos = array_search($extension, $this->extensions);
unset ($this->extensions[$pos]);
}
/**
* Returns the default layout name
*
* @return string
*
* @codeCoverageIgnore
*/
public function getDefaultLayout()
{
return $this->defaultLayout;
}
/**
* Sets the default layout name
*
* @param string $defaultLayout
*
* @return void
*/
public function setDefaultLayout($defaultLayout)
{
$this->defaultLayout = $defaultLayout;
}
/**
* Returns the default sub-template name
*
* @return string
*
* @codeCoverageIgnore
*/
public function getDefaultTpl()
{
return $this->defaultTpl;
}
/**
* Sets the default sub-template name
*
* @param string $defaultTpl
*
* @codeCoverageIgnore
*/
public function setDefaultTpl($defaultTpl)
{
$this->defaultTpl = $defaultTpl;
}
/**
* Returns the "strict view" flag. When the flag is false we will look for the view template in both the
* singularised and pluralised view. If it's true we will only look for the view template in the view
* specified in getViewTemplateUris.
*
* @return boolean
*
* @codeCoverageIgnore
*/
public function isStrictView()
{
return $this->strictView;
}
/**
* Sets the "strict view" flag. When the flag is false we will look for the view template in both the
* singularised and pluralised view. If it's true we will only look for the view template in the view
* specified in getViewTemplateUris.
*
* @param boolean $strictView
*
* @return void
*
* @codeCoverageIgnore
*/
public function setStrictView($strictView)
{
$this->strictView = $strictView;
}
/**
* Returns the "strict template" flag. When the flag is false we will look for a view template with or without the
* sub-template defined in getViewTemplateUris. If it's true we will only look for the sub-template specified.
*
* @return boolean
*
* @codeCoverageIgnore
*/
public function isStrictTpl()
{
return $this->strictTpl;
}
/**
* Sets the "strict template" flag. When the flag is false we will look for a view template with or without the
* sub-template defined in getViewTemplateUris. If it's true we will only look for the sub-template specified.
*
* @param boolean $strictTpl
*
* @return void
*
* @codeCoverageIgnore
*/
public function setStrictTpl($strictTpl)
{
$this->strictTpl = $strictTpl;
}
/**
* Returns the "strict layout" flag. When the flag is false we will look for a view template with both the specified
* and the default template name in getViewTemplateUris. When true we will only look for the specified view
* template.
*
* @return boolean
*
* @codeCoverageIgnore
*/
public function isStrictLayout()
{
return $this->strictLayout;
}
/**
* Sets the "strict layout" flag. When the flag is false we will look for a view template with both the specified
* and the default template name in getViewTemplateUris. When true we will only look for the specified view
* template.
*
* @param boolean $strictLayout
*
* @return void
*
* @codeCoverageIgnore
*/
public function setStrictLayout($strictLayout)
{
$this->strictLayout = $strictLayout;
}
/**
* Returns the application side prefix which will be used by default in getViewTemplateUris. It can be one of:
* site Public front-end
* admin Administrator back-end
* auto Automatically figure out if it should be site or admin
* any First look in the current application side, then look on the other side of the application
*
* @return string
*
* @codeCoverageIgnore
*/
public function getSidePrefix()
{
return $this->sidePrefix;
}
/**
* Sets the application side prefix which will be used by default in getViewTemplateUris. It can be one of:
* site Public front-end
* admin Administrator back-end
* auto Automatically figure out if it should be site or admin
* any First look in the current application side, then look on the other side of the application
*
* @param string $sidePrefix
*
* @return void
*/
public function setSidePrefix($sidePrefix)
{
$sidePrefix = strtolower($sidePrefix);
$sidePrefix = trim($sidePrefix);
if (!in_array($sidePrefix, array('site', 'admin', 'auto', 'any')))
{
$sidePrefix = 'auto';
}
$this->sidePrefix = $sidePrefix;
}
}