Файловый менеджер - Редактировать - /home/harasnat/www/mf/MVC.tar
Назад
Controller/FormController.php 0000644 00000070724 15062121162 0012355 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2009 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Controller; use Doctrine\Inflector\InflectorFactory; use Joomla\CMS\Application\CMSApplication; use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Form\FormFactoryAwareInterface; use Joomla\CMS\Form\FormFactoryAwareTrait; use Joomla\CMS\Form\FormFactoryInterface; use Joomla\CMS\Language\Text; use Joomla\CMS\MVC\Factory\MVCFactoryInterface; use Joomla\CMS\MVC\Model\BaseDatabaseModel; use Joomla\CMS\Router\Route; use Joomla\CMS\Uri\Uri; use Joomla\Input\Input; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Controller tailored to suit most form-based admin operations. * * @since 1.6 * @todo Add ability to set redirect manually to better cope with frontend usage. */ class FormController extends BaseController implements FormFactoryAwareInterface { use FormFactoryAwareTrait; /** * The context for storing internal data, e.g. record. * * @var string * @since 1.6 */ protected $context; /** * The URL option for the component. * * @var string * @since 1.6 */ protected $option; /** * The URL view item variable. * * @var string * @since 1.6 */ protected $view_item; /** * The URL view list variable. * * @var string * @since 1.6 */ protected $view_list; /** * The prefix to use with controller messages. * * @var string * @since 1.6 */ protected $text_prefix; /** * Constructor. * * @param array $config An optional associative array of configuration settings. * Recognized key values include 'name', 'default_task', 'model_path', and * 'view_path' (this list is not meant to be comprehensive). * @param ?MVCFactoryInterface $factory The factory. * @param ?CMSApplication $app The Application for the dispatcher * @param ?Input $input Input * @param ?FormFactoryInterface $formFactory The form factory. * * @since 3.0 */ public function __construct( $config = [], MVCFactoryInterface $factory = null, ?CMSApplication $app = null, ?Input $input = null, FormFactoryInterface $formFactory = null ) { parent::__construct($config, $factory, $app, $input); // Guess the option as com_NameOfController if (empty($this->option)) { $this->option = ComponentHelper::getComponentName($this, $this->getName()); } // Guess the \Text message prefix. Defaults to the option. if (empty($this->text_prefix)) { $this->text_prefix = strtoupper($this->option); } // Guess the context as the suffix, eg: OptionControllerContent. if (empty($this->context)) { $r = null; $match = 'Controller'; // If there is a namespace append a backslash if (strpos(\get_class($this), '\\')) { $match .= '\\\\'; } if (!preg_match('/(.*)' . $match . '(.*)/i', \get_class($this), $r)) { throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_GET_NAME', __METHOD__), 500); } // Remove the backslashes and the suffix controller $this->context = str_replace(['\\', 'controller'], '', strtolower($r[2])); } // Guess the item view as the context. if (empty($this->view_item)) { $this->view_item = $this->context; } // Guess the list view as the plural of the item view. if (empty($this->view_list)) { $this->view_list = InflectorFactory::create()->build()->pluralize($this->view_item); } $this->setFormFactory($formFactory); // Apply, Save & New, and Save As copy should be standard on forms. $this->registerTask('apply', 'save'); $this->registerTask('save2menu', 'save'); $this->registerTask('save2new', 'save'); $this->registerTask('save2copy', 'save'); $this->registerTask('editAssociations', 'save'); } /** * Method to add a new record. * * @return boolean True if the record can be added, false if not. * * @since 1.6 */ public function add() { $context = "$this->option.edit.$this->context"; // Access check. if (!$this->allowAdd()) { // Set the internal error and also the redirect error. $this->setMessage(Text::_('JLIB_APPLICATION_ERROR_CREATE_RECORD_NOT_PERMITTED'), 'error'); $this->setRedirect( Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false ) ); return false; } // Clear the record edit information from the session. $this->app->setUserState($context . '.data', null); // Redirect to the edit screen. $this->setRedirect( Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend(), false ) ); return true; } /** * Method to check if you can add a new record. * * Extended classes can override this if necessary. * * @param array $data An array of input data. * * @return boolean * * @since 1.6 */ protected function allowAdd($data = []) { $user = $this->app->getIdentity(); return $user->authorise('core.create', $this->option) || \count($user->getAuthorisedCategories($this->option, 'core.create')); } /** * Method to check if you can edit an existing record. * * Extended classes can override this if necessary. * * @param array $data An array of input data. * @param string $key The name of the key for the primary key; default is id. * * @return boolean * * @since 1.6 */ protected function allowEdit($data = [], $key = 'id') { return $this->app->getIdentity()->authorise('core.edit', $this->option); } /** * Method to check if you can save a new or existing record. * * Extended classes can override this if necessary. * * @param array $data An array of input data. * @param string $key The name of the key for the primary key. * * @return boolean * * @since 1.6 */ protected function allowSave($data, $key = 'id') { $recordId = $data[$key] ?? '0'; if ($recordId) { return $this->allowEdit($data, $key); } else { return $this->allowAdd($data); } } /** * Method to run batch operations. * * @param BaseDatabaseModel $model The model of the component being processed. * * @return boolean True if successful, false otherwise and internal error is set. * * @since 1.7 */ public function batch($model) { $vars = $this->input->post->get('batch', [], 'array'); $cid = (array) $this->input->post->get('cid', [], 'int'); // Remove zero values resulting from input filter $cid = array_filter($cid); // Build an array of item contexts to check $contexts = []; $option = $this->extension ?? $this->option; foreach ($cid as $id) { // If we're coming from com_categories, we need to use extension vs. option $contexts[$id] = $option . '.' . $this->context . '.' . $id; } // Attempt to run the batch operation. if ($model->batch($vars, $cid, $contexts)) { $this->setMessage(Text::_('JLIB_APPLICATION_SUCCESS_BATCH')); return true; } else { $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_BATCH_FAILED', $model->getError()), 'warning'); return false; } } /** * Method to cancel an edit. * * @param string $key The name of the primary key of the URL variable. * * @return boolean True if access level checks pass, false otherwise. * * @since 1.6 */ public function cancel($key = null) { $this->checkToken(); $model = $this->getModel(); $table = $model->getTable(); $context = "$this->option.edit.$this->context"; if (empty($key)) { $key = $table->getKeyName(); } $recordId = $this->input->getInt($key); // Attempt to check-in the current record. if ($recordId && $table->hasField('checked_out') && $model->checkin($recordId) === false) { // Check-in failed, go back to the record and display a notice. $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()), 'error'); $this->setRedirect( Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $key), false ) ); return false; } // Clean the session data and redirect. $this->releaseEditId($context, $recordId); $this->app->setUserState($context . '.data', null); $url = 'index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(); // Check if there is a return value $return = $this->input->get('return', null, 'base64'); if (!\is_null($return) && Uri::isInternal(base64_decode($return))) { $url = base64_decode($return); } // Redirect to the list screen. $this->setRedirect(Route::_($url, false)); return true; } /** * Method to edit an existing record. * * @param string $key The name of the primary key of the URL variable. * @param string $urlVar The name of the URL variable if different from the primary key * (sometimes required to avoid router collisions). * * @return boolean True if access level check and checkout passes, false otherwise. * * @since 1.6 */ public function edit($key = null, $urlVar = null) { // Do not cache the response to this, its a redirect, and mod_expires and google chrome browser bugs cache it forever! $this->app->allowCache(false); $model = $this->getModel(); $table = $model->getTable(); $cid = (array) $this->input->post->get('cid', [], 'int'); $context = "$this->option.edit.$this->context"; // Determine the name of the primary key for the data. if (empty($key)) { $key = $table->getKeyName(); } // To avoid data collisions the urlVar may be different from the primary key. if (empty($urlVar)) { $urlVar = $key; } // Get the previous record id (if any) and the current record id. $recordId = (int) (\count($cid) ? $cid[0] : $this->input->getInt($urlVar)); $checkin = $table->hasField('checked_out'); // Access check. if (!$this->allowEdit([$key => $recordId], $key)) { $this->setMessage(Text::_('JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'), 'error'); $this->setRedirect( Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false ) ); return false; } // Attempt to check-out the new record for editing and redirect. if ($checkin && !$model->checkout($recordId)) { // Check-out failed, display a notice but allow the user to see the record. $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKOUT_FAILED', $model->getError()), 'error'); $this->setRedirect( Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar), false ) ); return false; } else { // Check-out succeeded, push the new record id into the session. $this->holdEditId($context, $recordId); $this->app->setUserState($context . '.data', null); $this->setRedirect( Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar), false ) ); return true; } } /** * Method to get a model object, loading it if required. * * @param string $name The model name. Optional. * @param string $prefix The class prefix. Optional. * @param array $config Configuration array for model. Optional. * * @return BaseDatabaseModel The model. * * @since 1.6 */ public function getModel($name = '', $prefix = '', $config = ['ignore_request' => true]) { if (empty($name)) { $name = $this->context; } return parent::getModel($name, $prefix, $config); } /** * Gets the URL arguments to append to an item redirect. * * @param integer $recordId The primary key id for the item. * @param string $urlVar The name of the URL variable for the id. * * @return string The arguments to append to the redirect URL. * * @since 1.6 */ protected function getRedirectToItemAppend($recordId = null, $urlVar = 'id') { $append = ''; // Setup redirect info. if ($tmpl = $this->input->get('tmpl', '', 'string')) { $append .= '&tmpl=' . $tmpl; } if ($layout = $this->input->get('layout', 'edit', 'string')) { $append .= '&layout=' . $layout; } if ($forcedLanguage = $this->input->get('forcedLanguage', '', 'cmd')) { $append .= '&forcedLanguage=' . $forcedLanguage; } if ($recordId) { $append .= '&' . $urlVar . '=' . $recordId; } $return = $this->input->get('return', null, 'base64'); if ($return) { $append .= '&return=' . $return; } return $append; } /** * Gets the URL arguments to append to a list redirect. * * @return string The arguments to append to the redirect URL. * * @since 1.6 */ protected function getRedirectToListAppend() { $append = ''; // Setup redirect info. if ($tmpl = $this->input->get('tmpl', '', 'string')) { $append .= '&tmpl=' . $tmpl; } if ($forcedLanguage = $this->input->get('forcedLanguage', '', 'cmd')) { $append .= '&forcedLanguage=' . $forcedLanguage; } return $append; } /** * Function that allows child controller access to model data * after the data has been saved. * * @param BaseDatabaseModel $model The data model object. * @param array $validData The validated data. * * @return void * * @since 1.6 */ protected function postSaveHook(BaseDatabaseModel $model, $validData = []) { } /** * Method to save a record. * * @param string $key The name of the primary key of the URL variable. * @param string $urlVar The name of the URL variable if different from the primary key (sometimes required to avoid router collisions). * * @return boolean True if successful, false otherwise. * * @since 1.6 */ public function save($key = null, $urlVar = null) { // Check for request forgeries. $this->checkToken(); $app = $this->app; $model = $this->getModel(); $table = $model->getTable(); $data = $this->input->post->get('jform', [], 'array'); $checkin = $table->hasField('checked_out'); $context = "$this->option.edit.$this->context"; $task = $this->getTask(); // Determine the name of the primary key for the data. if (empty($key)) { $key = $table->getKeyName(); } // To avoid data collisions the urlVar may be different from the primary key. if (empty($urlVar)) { $urlVar = $key; } $recordId = $this->input->getInt($urlVar); // Populate the row id from the session. $data[$key] = $recordId; // The save2copy task needs to be handled slightly differently. if ($task === 'save2copy') { // Check-in the original row. if ($checkin && $model->checkin($data[$key]) === false) { // Check-in failed. Go back to the item and display a notice. $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()), 'error'); $this->setRedirect( Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar), false ) ); return false; } // Reset the ID, the multilingual associations and then treat the request as for Apply. $data[$key] = 0; $data['associations'] = []; $task = 'apply'; } // Access check. if (!$this->allowSave($data, $key)) { $this->setMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error'); $this->setRedirect( Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false ) ); return false; } // Validate the posted data. // Sometimes the form needs some posted data, such as for plugins and modules. $form = $model->getForm($data, false); if (!$form) { $app->enqueueMessage($model->getError(), 'error'); return false; } // Send an object which can be modified through the plugin event $objData = (object) $data; $app->triggerEvent( 'onContentNormaliseRequestData', [$this->option . '.' . $this->context, $objData, $form] ); $data = (array) $objData; // Test whether the data is valid. $validData = $model->validate($form, $data); // Check for validation errors. if ($validData === false) { // Get the validation messages. $errors = $model->getErrors(); // Push up to three validation messages out to the user. for ($i = 0, $n = \count($errors); $i < $n && $i < 3; $i++) { if ($errors[$i] instanceof \Exception) { $app->enqueueMessage($errors[$i]->getMessage(), 'warning'); } else { $app->enqueueMessage($errors[$i], 'warning'); } } /** * We need the filtered value of calendar fields because the UTC normalisation is * done in the filter and on output. This would apply the Timezone offset on * reload. We set the calendar values we save to the processed date. */ $filteredData = $form->filter($data); foreach ($form->getFieldset() as $field) { if ($field->type === 'Calendar') { $fieldName = $field->fieldname; if (isset($filteredData[$fieldName])) { $data[$fieldName] = $filteredData[$fieldName]; } } } // Save the data in the session. $app->setUserState($context . '.data', $data); // Redirect back to the edit screen. $this->setRedirect( Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar), false ) ); return false; } if (!isset($validData['tags'])) { $validData['tags'] = []; } // Attempt to save the data. if (!$model->save($validData)) { // Save the data in the session. $app->setUserState($context . '.data', $validData); // Redirect back to the edit screen. $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()), 'error'); $this->setRedirect( Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar), false ) ); return false; } // Save succeeded, so check-in the record. if ($checkin && $model->checkin($validData[$key]) === false) { // Save the data in the session. $app->setUserState($context . '.data', $validData); // Check-in failed, so go back to the record and display a notice. $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()), 'error'); $this->setRedirect( Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar), false ) ); return false; } $langKey = $this->text_prefix . ($recordId === 0 && $app->isClient('site') ? '_SUBMIT' : '') . '_SAVE_SUCCESS'; $prefix = $this->app->getLanguage()->hasKey($langKey) ? $this->text_prefix : 'JLIB_APPLICATION'; $this->setMessage(Text::_($prefix . ($recordId === 0 && $app->isClient('site') ? '_SUBMIT' : '') . '_SAVE_SUCCESS')); // Redirect the user and adjust session state based on the chosen task. switch ($task) { case 'apply': // Set the record data in the session. $recordId = $model->getState($model->getName() . '.id'); $this->holdEditId($context, $recordId); $app->setUserState($context . '.data', null); $model->checkout($recordId); // Redirect back to the edit screen. $this->setRedirect( Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar), false ) ); break; case 'save2new': // Clear the record id and data from the session. $this->releaseEditId($context, $recordId); $app->setUserState($context . '.data', null); // Redirect back to the edit screen. $this->setRedirect( Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend(null, $urlVar), false ) ); break; default: // Clear the record id and data from the session. $this->releaseEditId($context, $recordId); $app->setUserState($context . '.data', null); $url = 'index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(); // Check if there is a return value $return = $this->input->get('return', null, 'base64'); if (!\is_null($return) && Uri::isInternal(base64_decode($return))) { $url = base64_decode($return); } // Redirect to the list screen. $this->setRedirect(Route::_($url, false)); break; } // Invoke the postSave method to allow for the child class to access the model. $this->postSaveHook($model, $validData); return true; } /** * Method to reload a record. * * @param string $key The name of the primary key of the URL variable. * @param string $urlVar The name of the URL variable if different from the primary key (sometimes required to avoid router collisions). * * @return void * * @since 3.7.4 */ public function reload($key = null, $urlVar = null) { // Check for request forgeries. $this->checkToken(); $app = $this->app; $model = $this->getModel(); $data = $this->input->post->get('jform', [], 'array'); // Determine the name of the primary key for the data. if (empty($key)) { $key = $model->getTable()->getKeyName(); } // To avoid data collisions the urlVar may be different from the primary key. if (empty($urlVar)) { $urlVar = $key; } $recordId = $this->input->getInt($urlVar); // Populate the row id from the session. $data[$key] = $recordId; // Check if it is allowed to edit or create the data if (($recordId && !$this->allowEdit($data, $key)) || (!$recordId && !$this->allowAdd($data))) { $this->setRedirect( Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false ) ); $this->redirect(); } // The redirect url $redirectUrl = Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_item . $this->getRedirectToItemAppend($recordId, $urlVar), false ); /** @var \Joomla\CMS\Form\Form $form */ $form = $model->getForm($data, false); /** * We need the filtered value of calendar fields because the UTC normalisation is * done in the filter and on output. This would apply the Timezone offset on * reload. We set the calendar values we save to the processed date. */ $filteredData = $form->filter($data); foreach ($form->getFieldset() as $field) { if ($field->type === 'Calendar') { $fieldName = $field->fieldname; if ($field->group) { if (isset($filteredData[$field->group][$fieldName])) { $data[$field->group][$fieldName] = $filteredData[$field->group][$fieldName]; } } else { if (isset($filteredData[$fieldName])) { $data[$fieldName] = $filteredData[$fieldName]; } } } } // Save the data in the session. $app->setUserState($this->option . '.edit.' . $this->context . '.data', $data); $this->setRedirect($redirectUrl); $this->redirect(); } /** * Load item to edit associations in com_associations * * @return void * * @since 3.9.0 * * @deprecated 4.3 will be removed in 6.0 * It is handled by regular save method now. */ public function editAssociations() { // Initialise variables. $app = $this->app; $input = $app->getInput(); $model = $this->getModel(); $data = $input->get('jform', [], 'array'); $model->editAssociations($data); } } Controller/ApiController.php 0000644 00000037411 15062121162 0012157 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Controller; use Joomla\CMS\Access\Exception\NotAllowed; use Joomla\CMS\Application\CMSApplication; use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Form\Form; use Joomla\CMS\Language\Text; use Joomla\CMS\MVC\Factory\MVCFactoryInterface; use Joomla\CMS\MVC\Model\ListModel; use Joomla\CMS\MVC\View\JsonApiView; use Joomla\CMS\Object\CMSObject; use Joomla\Input\Input; use Joomla\String\Inflector; use Tobscure\JsonApi\Exception\InvalidParameterException; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Base class for a Joomla API Controller * * Controller (controllers are where you put all the actual code) Provides basic * functionality, such as rendering views (aka displaying templates). * * @since 4.0.0 */ class ApiController extends BaseController { /** * The content type of the item. * * @var string * @since 4.0.0 */ protected $contentType; /** * The URL option for the component. * * @var string * @since 4.0.0 */ protected $option; /** * The prefix to use with controller messages. * * @var string * @since 4.0.0 */ protected $text_prefix; /** * The context for storing internal data, e.g. record. * * @var string * @since 4.0.0 */ protected $context; /** * Items on a page * * @var integer */ protected $itemsPerPage = 20; /** * The model state to inject * * @var CMSObject */ protected $modelState; /** * Constructor. * * @param array $config An optional associative array of configuration settings. * Recognized key values include 'name', 'default_task', 'model_path', and * 'view_path' (this list is not meant to be comprehensive). * @param ?MVCFactoryInterface $factory The factory. * @param ?CMSApplication $app The Application for the dispatcher * @param ?Input $input Input * * @since 4.0.0 * @throws \Exception */ public function __construct($config = [], MVCFactoryInterface $factory = null, ?CMSApplication $app = null, ?Input $input = null) { $this->modelState = new CMSObject(); parent::__construct($config, $factory, $app, $input); // Guess the option as com_NameOfController if (empty($this->option)) { $this->option = ComponentHelper::getComponentName($this, $this->getName()); } // Guess the \Text message prefix. Defaults to the option. if (empty($this->text_prefix)) { $this->text_prefix = strtoupper($this->option); } // Guess the context as the suffix, eg: OptionControllerContent. if (empty($this->context)) { $r = null; if (!preg_match('/(.*)Controller(.*)/i', \get_class($this), $r)) { throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_GET_NAME', __METHOD__), 500); } $this->context = str_replace('\\', '', strtolower($r[2])); } } /** * Basic display of an item view * * @param integer $id The primary key to display. Leave empty if you want to retrieve data from the request * * @return static A \JControllerLegacy object to support chaining. * * @since 4.0.0 */ public function displayItem($id = null) { if ($id === null) { $id = $this->input->get('id', 0, 'int'); } $viewType = $this->app->getDocument()->getType(); $viewName = $this->input->get('view', $this->default_view); $viewLayout = $this->input->get('layout', 'default', 'string'); try { /** @var JsonApiView $view */ $view = $this->getView( $viewName, $viewType, '', ['base_path' => $this->basePath, 'layout' => $viewLayout, 'contentType' => $this->contentType] ); } catch (\Exception $e) { throw new \RuntimeException($e->getMessage()); } $modelName = $this->input->get('model', Inflector::singularize($this->contentType)); // Create the model, ignoring request data so we can safely set the state in the request from the controller $model = $this->getModel($modelName, '', ['ignore_request' => true, 'state' => $this->modelState]); if (!$model) { throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE')); } try { $modelName = $model->getName(); } catch (\Exception $e) { throw new \RuntimeException($e->getMessage()); } $model->setState($modelName . '.id', $id); // Push the model into the view (as default) $view->setModel($model, true); $view->document = $this->app->getDocument(); $view->displayItem(); return $this; } /** * Basic display of a list view * * @return static A \JControllerLegacy object to support chaining. * * @since 4.0.0 */ public function displayList() { // Assemble pagination information (using recommended JsonApi pagination notation for offset strategy) $paginationInfo = $this->input->get('page', [], 'array'); $limit = null; $offset = null; if (\array_key_exists('offset', $paginationInfo)) { $offset = $paginationInfo['offset']; $this->modelState->set($this->context . '.limitstart', $offset); } if (\array_key_exists('limit', $paginationInfo)) { $limit = $paginationInfo['limit']; $this->modelState->set($this->context . '.list.limit', $limit); } $viewType = $this->app->getDocument()->getType(); $viewName = $this->input->get('view', $this->default_view); $viewLayout = $this->input->get('layout', 'default', 'string'); try { /** @var JsonApiView $view */ $view = $this->getView( $viewName, $viewType, '', ['base_path' => $this->basePath, 'layout' => $viewLayout, 'contentType' => $this->contentType] ); } catch (\Exception $e) { throw new \RuntimeException($e->getMessage()); } $modelName = $this->input->get('model', $this->contentType); /** @var ListModel $model */ $model = $this->getModel($modelName, '', ['ignore_request' => true, 'state' => $this->modelState]); if (!$model) { throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE')); } // Push the model into the view (as default) $view->setModel($model, true); if ($offset) { $model->setState('list.start', $offset); } /** * Sanity check we don't have too much data being requested as regularly in html we automatically set it back to * the last page of data. If there isn't a limit start then set */ if ($limit) { $model->setState('list.limit', $limit); } else { $model->setState('list.limit', $this->itemsPerPage); } if (!is_null($offset) && $offset > $model->getTotal()) { throw new Exception\ResourceNotFound(); } $view->document = $this->app->getDocument(); $view->displayList(); return $this; } /** * Removes an item. * * @param integer $id The primary key to delete item. * * @return void * * @since 4.0.0 */ public function delete($id = null) { if (!$this->app->getIdentity()->authorise('core.delete', $this->option)) { throw new NotAllowed('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED', 403); } if ($id === null) { $id = $this->input->get('id', 0, 'int'); } $modelName = $this->input->get('model', Inflector::singularize($this->contentType)); /** @var \Joomla\CMS\MVC\Model\AdminModel $model */ $model = $this->getModel($modelName, '', ['ignore_request' => true]); if (!$model) { throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE')); } // Remove the item. if (!$model->delete($id)) { if ($model->getError() !== false) { throw new \RuntimeException($model->getError(), 500); } throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_DELETE'), 500); } $this->app->setHeader('status', 204); } /** * Method to add a new record. * * @return void * * @since 4.0.0 * @throws NotAllowed * @throws \RuntimeException */ public function add() { // Access check. if (!$this->allowAdd()) { throw new NotAllowed('JLIB_APPLICATION_ERROR_CREATE_RECORD_NOT_PERMITTED', 403); } $recordId = $this->save(); $this->displayItem($recordId); } /** * Method to edit an existing record. * * @return static A \JControllerLegacy object to support chaining. * * @since 4.0.0 */ public function edit() { /** @var \Joomla\CMS\MVC\Model\AdminModel $model */ $model = $this->getModel(Inflector::singularize($this->contentType)); if (!$model) { throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE')); } try { $table = $model->getTable(); } catch (\Exception $e) { throw new \RuntimeException($e->getMessage()); } $recordId = $this->input->getInt('id'); if (!$recordId) { throw new Exception\ResourceNotFound(Text::_('JLIB_APPLICATION_ERROR_RECORD'), 404); } $key = $table->getKeyName(); // Access check. if (!$this->allowEdit([$key => $recordId], $key)) { throw new NotAllowed('JLIB_APPLICATION_ERROR_CREATE_RECORD_NOT_PERMITTED', 403); } // Attempt to check-out the new record for editing and redirect. if ($table->hasField('checked_out') && !$model->checkout($recordId)) { // Check-out failed, display a notice but allow the user to see the record. throw new Exception\CheckinCheckout(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKOUT_FAILED', $model->getError())); } $this->save($recordId); $this->displayItem($recordId); return $this; } /** * Method to save a record. * * @param integer $recordKey The primary key of the item (if exists) * * @return integer The record ID on success, false on failure * * @since 4.0.0 */ protected function save($recordKey = null) { /** @var \Joomla\CMS\MVC\Model\AdminModel $model */ $model = $this->getModel(Inflector::singularize($this->contentType)); if (!$model) { throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE')); } try { $table = $model->getTable(); } catch (\Exception $e) { throw new \RuntimeException($e->getMessage()); } $key = $table->getKeyName(); $data = $this->input->get('data', json_decode($this->input->json->getRaw(), true), 'array'); $checkin = property_exists($table, $table->getColumnAlias('checked_out')); $data[$key] = $recordKey; if ($this->input->getMethod() === 'PATCH') { if ($recordKey && $table->load($recordKey)) { $fields = $table->getFields(); foreach ($fields as $field) { if (array_key_exists($field->Field, $data)) { continue; } $data[$field->Field] = $table->{$field->Field}; } } } $data = $this->preprocessSaveData($data); // @todo: Not the cleanest thing ever but it works... Form::addFormPath(JPATH_COMPONENT_ADMINISTRATOR . '/forms'); // Needs to be set because com_fields needs the data in jform to determine the assigned catid $this->input->set('jform', $data); // Validate the posted data. $form = $model->getForm($data, false); if (!$form) { throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_FORM_CREATE')); } // Test whether the data is valid. $validData = $model->validate($form, $data); // Check for validation errors. if ($validData === false) { $errors = $model->getErrors(); $messages = []; // Push up to three validation messages out to the user. for ($i = 0, $n = \count($errors); $i < $n && $i < 3; $i++) { if ($errors[$i] instanceof \Exception) { $messages[] = "{$errors[$i]->getMessage()}"; } else { $messages[] = "{$errors[$i]}"; } } throw new InvalidParameterException(implode("\n", $messages)); } if (!isset($validData['tags'])) { $validData['tags'] = []; } // Attempt to save the data. if (!$model->save($validData)) { throw new Exception\Save(Text::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError())); } try { $modelName = $model->getName(); } catch (\Exception $e) { throw new \RuntimeException($e->getMessage()); } // Ensure we have the record ID in case we created a new article $recordId = $model->getState($modelName . '.id'); if ($recordId === null) { throw new Exception\CheckinCheckout(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError())); } // Save succeeded, so check-in the record. if ($checkin && $model->checkin($recordId) === false) { throw new Exception\CheckinCheckout(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError())); } return $recordId; } /** * Method to check if you can edit an existing record. * * Extended classes can override this if necessary. * * @param array $data An array of input data. * @param string $key The name of the key for the primary key; default is id. * * @return boolean * * @since 4.0.0 */ protected function allowEdit($data = [], $key = 'id') { return $this->app->getIdentity()->authorise('core.edit', $this->option); } /** * Method to check if you can add a new record. * * Extended classes can override this if necessary. * * @param array $data An array of input data. * * @return boolean * * @since 4.0.0 */ protected function allowAdd($data = []) { $user = $this->app->getIdentity(); return $user->authorise('core.create', $this->option) || \count($user->getAuthorisedCategories($this->option, 'core.create')); } /** * Method to allow extended classes to manipulate the data to be saved for an extension. * * @param array $data An array of input data. * * @return array * * @since 4.0.0 */ protected function preprocessSaveData(array $data): array { return $data; } } Controller/Exception/ResourceNotFound.php 0000644 00000001000 15062121162 0014565 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Controller\Exception; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Exception class defining a resource not found exception * * @since 4.0.0 */ class ResourceNotFound extends \RuntimeException { } Controller/Exception/Save.php 0000644 00000000722 15062121162 0012231 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Controller\Exception; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Exception saving data * * @since 4.0.0 */ class Save extends \RuntimeException { } Controller/Exception/CheckinCheckout.php 0000644 00000000742 15062121162 0014367 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Controller\Exception; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Checkin/checkout Exception * * @since 4.0.0 */ class CheckinCheckout extends \RuntimeException { } Controller/Exception/SendEmail.php 0000644 00000000726 15062121162 0013200 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Controller\Exception; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Send email Exception * * @since 4.0.0 */ class SendEmail extends \RuntimeException { } Controller/AdminController.php 0000644 00000033620 15062121162 0012474 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2010 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Controller; use Joomla\CMS\Application\CMSApplication; use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Language\Text; use Joomla\CMS\MVC\Factory\MVCFactoryInterface; use Joomla\CMS\MVC\Model\BaseDatabaseModel; use Joomla\CMS\MVC\Model\WorkflowModelInterface; use Joomla\CMS\Router\Route; use Joomla\Input\Input; use Joomla\Utilities\ArrayHelper; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Base class for a Joomla Administrator Controller * * Controller (controllers are where you put all the actual code) Provides basic * functionality, such as rendering views (aka displaying templates). * * @since 1.6 */ class AdminController extends BaseController { /** * The URL option for the component. * * @var string * @since 1.6 */ protected $option; /** * The prefix to use with controller messages. * * @var string * @since 1.6 */ protected $text_prefix; /** * The URL view list variable. * * @var string * @since 1.6 */ protected $view_list; /** * Constructor. * * @param array $config An optional associative array of configuration settings. * Recognized key values include 'name', 'default_task', 'model_path', and * 'view_path' (this list is not meant to be comprehensive). * @param ?MVCFactoryInterface $factory The factory. * @param ?CMSApplication $app The Application for the dispatcher * @param ?Input $input The Input object for the request * * @since 3.0 */ public function __construct($config = [], MVCFactoryInterface $factory = null, ?CMSApplication $app = null, ?Input $input = null) { parent::__construct($config, $factory, $app, $input); // Define standard task mappings. // Value = 0 $this->registerTask('unpublish', 'publish'); // Value = 2 $this->registerTask('archive', 'publish'); // Value = -2 $this->registerTask('trash', 'publish'); // Value = -3 $this->registerTask('report', 'publish'); $this->registerTask('orderup', 'reorder'); $this->registerTask('orderdown', 'reorder'); // Guess the option as com_NameOfController. if (empty($this->option)) { $this->option = ComponentHelper::getComponentName($this, $this->getName()); } // Guess the \Text message prefix. Defaults to the option. if (empty($this->text_prefix)) { $this->text_prefix = strtoupper($this->option); } // Guess the list view as the suffix, eg: OptionControllerSuffix. if (empty($this->view_list)) { $reflect = new \ReflectionClass($this); $r = [0 => '', 1 => '', 2 => $reflect->getShortName()]; if ($reflect->getNamespaceName()) { $r[2] = str_replace('Controller', '', $r[2]); } elseif (!preg_match('/(.*)Controller(.*)/i', $reflect->getShortName(), $r)) { throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_GET_NAME', __METHOD__), 500); } $this->view_list = strtolower($r[2]); } } /** * Removes an item. * * @return void * * @since 1.6 */ public function delete() { // Check for request forgeries $this->checkToken(); // Get items to remove from the request. $cid = (array) $this->input->get('cid', [], 'int'); // Remove zero values resulting from input filter $cid = array_filter($cid); if (empty($cid)) { $this->app->getLogger()->warning(Text::_($this->text_prefix . '_NO_ITEM_SELECTED'), ['category' => 'jerror']); } else { // Get the model. $model = $this->getModel(); // Remove the items. if ($model->delete($cid)) { $this->setMessage(Text::plural($this->text_prefix . '_N_ITEMS_DELETED', \count($cid))); } else { $this->setMessage($model->getError(), 'error'); } // Invoke the postDelete method to allow for the child class to access the model. $this->postDeleteHook($model, $cid); } $this->setRedirect( Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false ) ); } /** * Function that allows child controller access to model data * after the item has been deleted. * * @param BaseDatabaseModel $model The data model object. * @param integer[] $id An array of deleted IDs. * * @return void * * @since 3.1 */ protected function postDeleteHook(BaseDatabaseModel $model, $id = null) { } /** * Method to publish a list of items * * @return void * * @since 1.6 */ public function publish() { // Check for request forgeries $this->checkToken(); // Get items to publish from the request. $cid = (array) $this->input->get('cid', [], 'int'); $data = ['publish' => 1, 'unpublish' => 0, 'archive' => 2, 'trash' => -2, 'report' => -3]; $task = $this->getTask(); $value = ArrayHelper::getValue($data, $task, 0, 'int'); // Remove zero values resulting from input filter $cid = array_filter($cid); if (empty($cid)) { $this->app->getLogger()->warning(Text::_($this->text_prefix . '_NO_ITEM_SELECTED'), ['category' => 'jerror']); } else { // Get the model. $model = $this->getModel(); // Publish the items. try { $model->publish($cid, $value); $errors = $model->getErrors(); $ntext = null; if ($value === 1) { if ($errors) { $this->app->enqueueMessage(Text::plural($this->text_prefix . '_N_ITEMS_FAILED_PUBLISHING', \count($cid)), 'error'); } else { $ntext = $this->text_prefix . '_N_ITEMS_PUBLISHED'; } } elseif ($value === 0) { $ntext = $this->text_prefix . '_N_ITEMS_UNPUBLISHED'; } elseif ($value === 2) { $ntext = $this->text_prefix . '_N_ITEMS_ARCHIVED'; } else { $ntext = $this->text_prefix . '_N_ITEMS_TRASHED'; } if (\count($cid)) { $this->setMessage(Text::plural($ntext, \count($cid))); } } catch (\Exception $e) { $this->setMessage($e->getMessage(), 'error'); } } $this->setRedirect( Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false ) ); } /** * Changes the order of one or more records. * * @return boolean True on success * * @since 1.6 */ public function reorder() { // Check for request forgeries. $this->checkToken(); $ids = (array) $this->input->post->get('cid', [], 'int'); $inc = $this->getTask() === 'orderup' ? -1 : 1; // Remove zero values resulting from input filter $ids = array_filter($ids); $model = $this->getModel(); $return = $model->reorder($ids, $inc); $redirect = Route::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false); if ($return === false) { // Reorder failed. $message = Text::sprintf('JLIB_APPLICATION_ERROR_REORDER_FAILED', $model->getError()); $this->setRedirect($redirect, $message, 'error'); return false; } else { // Reorder succeeded. $message = Text::_('JLIB_APPLICATION_SUCCESS_ITEM_REORDERED'); $this->setRedirect($redirect, $message); return true; } } /** * Method to save the submitted ordering values for records. * * @return boolean True on success * * @since 1.6 */ public function saveorder() { // Check for request forgeries. $this->checkToken(); // Get the input $pks = (array) $this->input->post->get('cid', [], 'int'); $order = (array) $this->input->post->get('order', [], 'int'); // Remove zero PKs and corresponding order values resulting from input filter for PK foreach ($pks as $i => $pk) { if ($pk === 0) { unset($pks[$i]); unset($order[$i]); } } // Get the model $model = $this->getModel(); // Save the ordering $return = $model->saveorder($pks, $order); $redirect = Route::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false); if ($return === false) { // Reorder failed $message = Text::sprintf('JLIB_APPLICATION_ERROR_REORDER_FAILED', $model->getError()); $this->setRedirect($redirect, $message, 'error'); return false; } else { // Reorder succeeded. $this->setMessage(Text::_('JLIB_APPLICATION_SUCCESS_ORDERING_SAVED')); $this->setRedirect($redirect); return true; } } /** * Check in of one or more records. * * @return boolean True on success * * @since 1.6 */ public function checkin() { // Check for request forgeries. $this->checkToken(); $ids = (array) $this->input->post->get('cid', [], 'int'); // Remove zero values resulting from input filter $ids = array_filter($ids); $model = $this->getModel(); $return = $model->checkin($ids); if ($return === false) { // Checkin failed. $message = Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()); $this->setRedirect( Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false ), $message, 'error' ); return false; } else { // Checkin succeeded. $message = Text::plural($this->text_prefix . '_N_ITEMS_CHECKED_IN', \count($ids)); $this->setRedirect( Route::_( 'index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false ), $message ); return true; } } /** * Method to save the submitted ordering values for records via AJAX. * * @return void * * @since 3.0 */ public function saveOrderAjax() { // Check for request forgeries. $this->checkToken(); // Get the input $pks = (array) $this->input->post->get('cid', [], 'int'); $order = (array) $this->input->post->get('order', [], 'int'); // Remove zero PKs and corresponding order values resulting from input filter for PK foreach ($pks as $i => $pk) { if ($pk === 0) { unset($pks[$i]); unset($order[$i]); } } // Get the model $model = $this->getModel(); // Save the ordering $return = $model->saveorder($pks, $order); if ($return) { echo '1'; } // Close the application $this->app->close(); } /** * Method to run Transition by id of item. * * @return boolean Indicates whether the transition was successful. * * @since 4.0.0 */ public function runTransition() { // Check for request forgeries $this->checkToken(); // Get the input $pks = (array) $this->input->post->get('cid', [], 'int'); // Remove zero values resulting from input filter $pks = array_filter($pks); if (!\count($pks)) { return false; } $transitionId = (int) $this->input->post->getInt('transition_id'); // Get the model $model = $this->getModel(); if (!$model instanceof WorkflowModelInterface) { return false; } $return = $model->executeTransition($pks, $transitionId); $redirect = Route::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(), false); if ($return === false) { // Transition change failed. $message = Text::sprintf('JLIB_APPLICATION_ERROR_RUN_TRANSITION', $model->getError()); $this->setRedirect($redirect, $message, 'error'); return false; } // Transition change succeeded. $message = Text::_('JLIB_APPLICATION_SUCCESS_RUN_TRANSITION'); $this->setRedirect($redirect, $message); return true; } /** * Gets the URL arguments to append to a list redirect. * * @return string The arguments to append to the redirect URL. * * @since 4.0.0 */ protected function getRedirectToListAppend() { return ''; } } Controller/ControllerInterface.php 0000644 00000001415 15062121162 0013341 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2005 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Controller; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Joomla Platform CMS Interface * * @since 4.0.0 */ interface ControllerInterface { /** * Execute a controller task. * * @param string $task The task to perform. * * @return mixed The value returned by the called method. * * @since 4.0.0 * @throws \InvalidArgumentException * @throws \RuntimeException */ public function execute($task); } Controller/BaseController.php 0000644 00000102611 15062121162 0012313 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2006 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Controller; use Joomla\CMS\Application\CMSApplication; use Joomla\CMS\Cache\Exception\CacheExceptionInterface; use Joomla\CMS\Document\DocumentAwareInterface; use Joomla\CMS\Factory; use Joomla\CMS\Filesystem\Path; use Joomla\CMS\Filter\InputFilter; use Joomla\CMS\Language\LanguageAwareInterface; use Joomla\CMS\Language\Text; use Joomla\CMS\Log\Log; use Joomla\CMS\MVC\Factory\LegacyFactory; use Joomla\CMS\MVC\Factory\MVCFactoryInterface; use Joomla\CMS\MVC\Model\BaseDatabaseModel; use Joomla\CMS\MVC\Model\BaseModel; use Joomla\CMS\MVC\View\ViewInterface; use Joomla\CMS\Session\Session; use Joomla\CMS\Uri\Uri; use Joomla\CMS\User\CurrentUserInterface; use Joomla\Event\DispatcherAwareInterface; use Joomla\Event\DispatcherAwareTrait; use Joomla\Event\DispatcherInterface; use Joomla\Input\Input; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Base class for a Joomla Controller * * Controller (Controllers are where you put all the actual code.) Provides basic * functionality, such as rendering views (aka displaying templates). * * @since 2.5.5 */ class BaseController implements ControllerInterface, DispatcherAwareInterface { use DispatcherAwareTrait; /** * The base path of the controller * * @var string * @since 3.0 */ protected $basePath; /** * The default view for the display method. * * @var string * @since 3.0 */ protected $default_view; /** * The mapped task that was performed. * * @var string * @since 3.0 */ protected $doTask; /** * Redirect message. * * @var string * @since 3.0 */ protected $message; /** * Redirect message type. * * @var string * @since 3.0 */ protected $messageType; /** * Array of class methods * * @var array * @since 3.0 */ protected $methods; /** * The name of the controller * * @var string * @since 3.0 */ protected $name; /** * The prefix of the models * * @var string * @since 3.0 */ protected $model_prefix; /** * The set of search directories for resources (views). * * @var array * @since 3.0 */ protected $paths; /** * URL for redirection. * * @var string * @since 3.0 */ protected $redirect; /** * Current or most recently performed task. * * @var string * @since 3.0 */ protected $task; /** * Array of class methods to call for a given task. * * @var array * @since 3.0 */ protected $taskMap; /** * Hold a JInput object for easier access to the input variables. * * @var Input * @since 3.0 */ protected $input; /** * The factory. * * @var MVCFactoryInterface * @since 3.10.0 */ protected $factory; /** * Instance container. * * @var static * @since 3.0 */ protected static $instance; /** * Instance container containing the views. * * @var ViewInterface[] * @since 3.4 */ protected static $views; /** * The Application * * @var CMSApplication|null * @since 4.0.0 */ protected $app; /** * Adds to the stack of model paths in LIFO order. * * @param mixed $path The directory (string), or list of directories (array) to add. * @param string $prefix A prefix for models * * @return void * * @since 3.0 * * @deprecated 4.3 will be removed in 6.0 * Will be removed without replacement. Get the model through the MVCFactory instead */ public static function addModelPath($path, $prefix = '') { BaseModel::addIncludePath($path, $prefix); } /** * Create the filename for a resource. * * @param string $type The resource type to create the filename for. * @param array $parts An associative array of filename information. Optional. * * @return string The filename. * * @since 3.0 */ public static function createFileName($type, $parts = []) { $filename = ''; switch ($type) { case 'controller': if (!empty($parts['format'])) { if ($parts['format'] === 'html') { $parts['format'] = ''; } else { $parts['format'] = '.' . $parts['format']; } } else { $parts['format'] = ''; } $filename = strtolower($parts['name'] . $parts['format'] . '.php'); break; case 'view': if (!empty($parts['type'])) { $parts['type'] = '.' . $parts['type']; } else { $parts['type'] = ''; } $filename = strtolower($parts['name'] . '/view' . $parts['type'] . '.php'); break; } return $filename; } /** * Method to get a singleton controller instance. * * @param string $prefix The prefix for the controller. * @param array $config An array of optional constructor options. * * @return static * * @since 3.0 * * @deprecated 4.3 will be removed in 6.0 * Get the controller through the MVCFactory instead * Example: Factory::getApplication()->bootComponent($option)->getMVCFactory()->createController(...); * * @throws \Exception if the controller cannot be loaded. */ public static function getInstance($prefix, $config = []) { if (\is_object(self::$instance)) { return self::$instance; } @trigger_error( sprintf( '%1$s::getInstance() is deprecated. Load it through the MVC factory.', self::class ), E_USER_DEPRECATED ); $app = Factory::getApplication(); $input = $app->getInput(); // Get the environment configuration. $basePath = \array_key_exists('base_path', $config) ? $config['base_path'] : JPATH_COMPONENT; $format = $input->getWord('format'); $command = $input->get('task', 'display'); // Check for array format. $filter = InputFilter::getInstance(); if (\is_array($command)) { $keys = array_keys($command); $command = $filter->clean(array_pop($keys), 'cmd'); } else { $command = $filter->clean($command, 'cmd'); } // Check for a controller.task command. if (strpos($command, '.') !== false) { // Explode the controller.task command. list($type, $task) = explode('.', $command); // Define the controller filename and path. $file = self::createFileName('controller', ['name' => $type, 'format' => $format]); $path = $basePath . '/controllers/' . $file; $backuppath = $basePath . '/controller/' . $file; // Reset the task without the controller context. $input->set('task', $task); } else { // Base controller. $type = ''; // Define the controller filename and path. $file = self::createFileName('controller', ['name' => 'controller', 'format' => $format]); $path = $basePath . '/' . $file; $backupfile = self::createFileName('controller', ['name' => 'controller']); $backuppath = $basePath . '/' . $backupfile; } // Get the controller class name. $class = ucfirst($prefix) . 'Controller' . ucfirst($type); // Include the class if not present. if (!class_exists($class)) { // If the controller file path exists, include it. if (is_file($path)) { require_once $path; } elseif (isset($backuppath) && is_file($backuppath)) { require_once $backuppath; } else { throw new \InvalidArgumentException(Text::sprintf('JLIB_APPLICATION_ERROR_INVALID_CONTROLLER', $type, $format)); } } // Instantiate the class. if (!class_exists($class)) { throw new \InvalidArgumentException(Text::sprintf('JLIB_APPLICATION_ERROR_INVALID_CONTROLLER_CLASS', $class)); } // Check for a possible service from the container otherwise manually instantiate the class if (Factory::getContainer()->has($class)) { self::$instance = Factory::getContainer()->get($class); } else { self::$instance = new $class($config, null, $app, $input); } return self::$instance; } /** * Constructor. * * @param array $config An optional associative array of configuration settings. * Recognized key values include 'name', 'default_task', 'model_path', and * 'view_path' (this list is not meant to be comprehensive). * @param ?MVCFactoryInterface $factory The factory. * @param ?CMSApplication $app The Application for the dispatcher * @param ?Input $input Input * * @since 3.0 */ public function __construct($config = [], MVCFactoryInterface $factory = null, ?CMSApplication $app = null, ?Input $input = null) { $this->methods = []; $this->message = null; $this->messageType = 'message'; $this->paths = []; $this->redirect = null; $this->taskMap = []; $this->app = $app ?: Factory::getApplication(); $this->input = $input ?: $this->app->getInput(); if (\defined('JDEBUG') && JDEBUG) { Log::addLogger(['text_file' => 'jcontroller.log.php'], Log::ALL, ['controller']); } // Determine the methods to exclude from the base class. $xMethods = get_class_methods('\\Joomla\\CMS\\MVC\\Controller\\BaseController'); // Get the public methods in this class using reflection. $r = new \ReflectionClass($this); $rMethods = $r->getMethods(\ReflectionMethod::IS_PUBLIC); foreach ($rMethods as $rMethod) { $mName = $rMethod->getName(); // Add default display method if not explicitly declared. if ($mName === 'display' || !\in_array($mName, $xMethods)) { $this->methods[] = strtolower($mName); // Auto register the methods as tasks. $this->taskMap[strtolower($mName)] = $mName; } } // Set the view name if (empty($this->name)) { if (\array_key_exists('name', $config)) { $this->name = $config['name']; } else { $this->name = $this->getName(); } } // Set a base path for use by the controller if (\array_key_exists('base_path', $config)) { $this->basePath = $config['base_path']; } else { $this->basePath = JPATH_COMPONENT; } // If the default task is set, register it as such if (\array_key_exists('default_task', $config)) { $this->registerDefaultTask($config['default_task']); } else { $this->registerDefaultTask('display'); } // Set the models prefix if (empty($this->model_prefix)) { if (\array_key_exists('model_prefix', $config)) { // User-defined prefix $this->model_prefix = $config['model_prefix']; } else { $this->model_prefix = ucfirst($this->name) . 'Model'; } } // Set the default model search path if (\array_key_exists('model_path', $config)) { // User-defined dirs $this->addModelPath($config['model_path'], $this->model_prefix); } else { $this->addModelPath($this->basePath . '/models', $this->model_prefix); } // Set the default view search path if (\array_key_exists('view_path', $config)) { // User-defined dirs $this->setPath('view', $config['view_path']); } else { $this->setPath('view', $this->basePath . '/views'); } // Set the default view. if (\array_key_exists('default_view', $config)) { $this->default_view = $config['default_view']; } elseif (empty($this->default_view)) { $this->default_view = $this->getName(); } $this->factory = $factory ?: new LegacyFactory(); } /** * Adds to the search path for templates and resources. * * @param string $type The path type (e.g. 'model', 'view'). * @param mixed $path The directory string or stream array to search. * * @return static A \JControllerLegacy object to support chaining. * * @since 3.0 */ protected function addPath($type, $path) { if (!isset($this->paths[$type])) { $this->paths[$type] = []; } // Loop through the path directories foreach ((array) $path as $dir) { // No surrounding spaces allowed! $dir = rtrim(Path::check($dir), '/') . '/'; // Add to the top of the search dirs array_unshift($this->paths[$type], $dir); } return $this; } /** * Add one or more view paths to the controller's stack, in LIFO order. * * @param mixed $path The directory (string) or list of directories (array) to add. * * @return static This object to support chaining. * * @since 3.0 */ public function addViewPath($path) { return $this->addPath('view', $path); } /** * Method to check whether an ID is in the edit list. * * @param string $context The context for the session storage. * @param integer $id The ID of the record to add to the edit list. * * @return boolean True if the ID is in the edit list. * * @since 3.0 */ protected function checkEditId($context, $id) { if ($id) { $values = (array) $this->app->getUserState($context . '.id'); $result = \in_array((int) $id, $values); if (\defined('JDEBUG') && JDEBUG) { $this->app->getLogger()->info( sprintf( 'Checking edit ID %s.%s: %d %s', $context, $id, (int) $result, str_replace("\n", ' ', print_r($values, 1)) ), ['category' => 'controller'] ); } return $result; } // No id for a new item. return true; } /** * Method to load and return a model object. * * @param string $name The name of the model. * @param string $prefix Optional model prefix. * @param array $config Configuration array for the model. Optional. * * @return BaseDatabaseModel|boolean Model object on success; otherwise false on failure. * * @since 3.0 */ protected function createModel($name, $prefix = '', $config = []) { $model = $this->factory->createModel($name, $prefix, $config); if ($model === null) { return false; } if ($model instanceof CurrentUserInterface && $this->app->getIdentity()) { $model->setCurrentUser($this->app->getIdentity()); } return $model; } /** * Method to load and return a view object. This method first looks in the * current template directory for a match and, failing that, uses a default * set path to load the view class file. * * Note the "name, prefix, type" order of parameters, which differs from the * "name, type, prefix" order used in related public methods. * * @param string $name The name of the view. * @param string $prefix Optional prefix for the view class name. * @param string $type The type of view. * @param array $config Configuration array for the view. Optional. * * @return ViewInterface|null View object on success; null or error result on failure. * * @since 3.0 * @throws \Exception */ protected function createView($name, $prefix = '', $type = '', $config = []) { $config['paths'] = $this->paths['view']; $view = $this->factory->createView($name, $prefix, $type, $config); if ($view instanceof CurrentUserInterface && $this->app->getIdentity()) { $view->setCurrentUser($this->app->getIdentity()); } if ($view instanceof LanguageAwareInterface && $this->app->getLanguage()) { $view->setLanguage($this->app->getLanguage()); } return $view; } /** * Typical view method for MVC based architecture * * This function is provide as a default implementation, in most cases * you will need to override it in your own controllers. * * @param boolean $cachable If true, the view output will be cached * @param array $urlparams An array of safe url parameters and their variable types, for valid values see {@link InputFilter::clean()}. * * @return static A \JControllerLegacy object to support chaining. * * @since 3.0 * @throws \Exception */ public function display($cachable = false, $urlparams = []) { $document = $this->app->getDocument(); $viewType = $document->getType(); $viewName = $this->input->get('view', $this->default_view); $viewLayout = $this->input->get('layout', 'default', 'string'); $view = $this->getView($viewName, $viewType, '', ['base_path' => $this->basePath, 'layout' => $viewLayout]); // Get/Create the model if ($model = $this->getModel($viewName, '', ['base_path' => $this->basePath])) { // Push the model into the view (as default) $view->setModel($model, true); } if ($view instanceof DocumentAwareInterface && $document) { $view->setDocument($this->app->getDocument()); } else { @trigger_error( 'View should implement document aware interface.', E_USER_DEPRECATED ); $view->document = $document; } // Display the view if ($cachable && $viewType !== 'feed' && $this->app->get('caching') >= 1) { $option = $this->input->get('option'); if (\is_array($urlparams)) { if (!empty($this->app->registeredurlparams)) { $registeredurlparams = $this->app->registeredurlparams; } else { $registeredurlparams = new \stdClass(); } foreach ($urlparams as $key => $value) { // Add your safe URL parameters with variable type as value {@see InputFilter::clean()}. $registeredurlparams->$key = $value; } $this->app->registeredurlparams = $registeredurlparams; } try { /** @var \Joomla\CMS\Cache\Controller\ViewController $cache */ $cache = Factory::getCache($option, 'view'); $cache->get($view, 'display'); } catch (CacheExceptionInterface $exception) { $view->display(); } } else { $view->display(); } return $this; } /** * Execute a task by triggering a method in the derived class. * * @param string $task The task to perform. If no matching task is found, the '__default' task is executed, if defined. * * @return mixed The value returned by the called method. * * @since 3.0 * @throws \Exception */ public function execute($task) { $this->task = $task; $task = strtolower((string) $task); if (isset($this->taskMap[$task])) { $doTask = $this->taskMap[$task]; } elseif (isset($this->taskMap['__default'])) { $doTask = $this->taskMap['__default']; } else { throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_TASK_NOT_FOUND', $task), 404); } // Record the actual task being fired $this->doTask = $doTask; return $this->$doTask(); } /** * Method to get a model object, loading it if required. * * @param string $name The model name. Optional. * @param string $prefix The class prefix. Optional. * @param array $config Configuration array for model. Optional. * * @return BaseDatabaseModel|boolean Model object on success; otherwise false on failure. * * @since 3.0 */ public function getModel($name = '', $prefix = '', $config = []) { if (empty($name)) { $name = $this->getName(); } if (!$prefix) { if ($this->factory instanceof LegacyFactory) { $prefix = $this->model_prefix; } elseif (!empty($config['base_path']) && strpos(Path::clean($config['base_path']), JPATH_ADMINISTRATOR) === 0) { // When the frontend uses an administrator model $prefix = 'Administrator'; } else { $prefix = $this->app->getName(); } } if ($model = $this->createModel($name, $prefix, $config)) { // Task is a reserved state $model->setState('task', $this->task); // We don't have the concept on a menu tree in the api app, so skip setting it's information and // return early if ($this->app->isClient('api')) { return $model; } // Let's get the application object and set menu information if it's available $menu = $this->app->getMenu(); if (\is_object($menu) && $item = $menu->getActive()) { $params = $menu->getParams($item->id); // Set default state data $model->setState('parameters.menu', $params); } } return $model; } /** * Method to get the controller name * * The dispatcher name is set 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 dispatcher * * @since 3.0 * @throws \Exception */ public function getName() { if (empty($this->name)) { $r = null; if (!preg_match('/(.*)Controller/i', \get_class($this), $r)) { throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_GET_NAME', __METHOD__), 500); } $this->name = strtolower($r[1]); } return $this->name; } /** * Get the last task that is being performed or was most recently performed. * * @return string The task that is being performed or was most recently performed. * * @since 3.0 */ public function getTask() { return $this->task; } /** * Gets the available tasks in the controller. * * @return array Array[i] of task names. * * @since 3.0 */ public function getTasks() { return $this->methods; } /** * Method to get a reference to the current view and load it if necessary. * * @param string $name The view name. Optional, defaults to the controller name. * @param string $type The view type. Optional. * @param string $prefix The class prefix. Optional. * @param array $config Configuration array for view. Optional. * * @return ViewInterface Reference to the view or an error. * * @since 3.0 * @throws \Exception */ public function getView($name = '', $type = '', $prefix = '', $config = []) { // @note We use self so we only access stuff in this class rather than in all classes. if (!isset(self::$views)) { self::$views = []; } if (empty($name)) { $name = $this->getName(); } if (!$prefix) { if ($this->factory instanceof LegacyFactory) { $prefix = $this->getName() . 'View'; } elseif (!empty($config['base_path']) && strpos(Path::clean($config['base_path']), JPATH_ADMINISTRATOR) === 0) { // When the front uses an administrator view $prefix = 'Administrator'; } else { $prefix = $this->app->getName(); } } if (empty(self::$views[$name][$type][$prefix])) { if ($view = $this->createView($name, $prefix, $type, $config)) { self::$views[$name][$type][$prefix] = &$view; } else { throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_VIEW_NOT_FOUND', $name, $type, $prefix), 404); } } return self::$views[$name][$type][$prefix]; } /** * Method to add a record ID to the edit list. * * @param string $context The context for the session storage. * @param integer $id The ID of the record to add to the edit list. * * @return void * * @since 3.0 */ protected function holdEditId($context, $id) { $values = (array) $this->app->getUserState($context . '.id'); // Add the id to the list if non-zero. if (!empty($id)) { $values[] = (int) $id; $values = array_unique($values); $this->app->setUserState($context . '.id', $values); if (\defined('JDEBUG') && JDEBUG) { $this->app->getLogger()->info( sprintf( 'Holding edit ID %s.%s %s', $context, $id, str_replace("\n", ' ', print_r($values, 1)) ), ['category' => 'controller'] ); } } } /** * Redirects the browser or returns false if no redirect is set. * * @return boolean False if no redirect exists. * * @since 3.0 */ public function redirect() { if ($this->redirect) { // Enqueue the redirect message $this->app->enqueueMessage($this->message, $this->messageType); // Execute the redirect $this->app->redirect($this->redirect); } return false; } /** * Register the default task to perform if a mapping is not found. * * @param string $method The name of the method in the derived class to perform if a named task is not found. * * @return static A \JControllerLegacy object to support chaining. * * @since 3.0 */ public function registerDefaultTask($method) { $this->registerTask('__default', $method); return $this; } /** * Register (map) a task to a method in the class. * * @param string $task The task. * @param string $method The name of the method in the derived class to perform for this task. * * @return static A \JControllerLegacy object to support chaining. * * @since 3.0 */ public function registerTask($task, $method) { if (\in_array(strtolower($method), $this->methods)) { $this->taskMap[strtolower($task)] = $method; } return $this; } /** * Unregister (unmap) a task in the class. * * @param string $task The task. * * @return static This object to support chaining. * * @since 3.0 */ public function unregisterTask($task) { unset($this->taskMap[strtolower($task)]); return $this; } /** * Method to check whether an ID is in the edit list. * * @param string $context The context for the session storage. * @param integer $id The ID of the record to add to the edit list. * * @return void * * @since 3.0 */ protected function releaseEditId($context, $id) { $values = (array) $this->app->getUserState($context . '.id'); // Do a strict search of the edit list values. $index = array_search((int) $id, $values, true); if (\is_int($index)) { unset($values[$index]); $this->app->setUserState($context . '.id', $values); if (\defined('JDEBUG') && JDEBUG) { $this->app->getLogger()->info( sprintf( 'Releasing edit ID %s.%s %s', $context, $id, str_replace("\n", ' ', print_r($values, 1)) ), ['category' => 'controller'] ); } } } /** * Sets the internal message that is passed with a redirect * * @param string $text Message to display on redirect. * @param string $type Message type. Optional, defaults to 'message'. * * @return string Previous message * * @since 3.0 */ public function setMessage($text, $type = 'message') { $previous = $this->message; $this->message = $text; $this->messageType = $type; return $previous; } /** * Sets an entire array of search paths for resources. * * @param string $type The type of path to set, typically 'view' or 'model'. * @param string $path The new set of search paths. If null or false, resets to the current directory only. * * @return void * * @since 3.0 */ protected function setPath($type, $path) { // Clear out the prior search dirs $this->paths[$type] = []; // Actually add the user-specified directories $this->addPath($type, $path); } /** * Checks for a form token in the request. * * Use in conjunction with HTMLHelper::_('form.token') or Session::getFormToken. * * @param string $method The request method in which to look for the token key. * @param boolean $redirect Whether to implicitly redirect user to the referrer page on failure or simply return false. * * @return boolean True if found and valid, otherwise return false or redirect to referrer page. * * @since 3.7.0 * @see Session::checkToken() */ public function checkToken($method = 'post', $redirect = true) { $valid = Session::checkToken($method); if (!$valid && $redirect) { $referrer = $this->input->server->getString('HTTP_REFERER'); if (\is_null($referrer) || !Uri::isInternal($referrer)) { $referrer = 'index.php'; } $this->app->enqueueMessage(Text::_('JINVALID_TOKEN_NOTICE'), 'warning'); $this->app->redirect($referrer); } return $valid; } /** * Set a URL for browser redirection. * * @param string $url URL to redirect to. * @param string $msg Message to display on redirect. Optional, defaults to value set internally by controller, if any. * @param string $type Message type. Optional, defaults to 'message' or the type set by a previous call to setMessage. * * @return static This object to support chaining. * * @since 3.0 */ public function setRedirect($url, $msg = null, $type = null) { $this->redirect = $url; if ($msg !== null) { // Controller may have set this directly $this->message = $msg; } // Ensure the type is not overwritten by a previous call to setMessage. if (empty($type)) { if (empty($this->messageType)) { $this->messageType = 'message'; } } else { // If the type is explicitly set, set it. $this->messageType = $type; } return $this; } /** * Get the event dispatcher. * * The override was made to keep a backward compatibility for legacy component. * TODO: Remove the override in 6.0 * * @return DispatcherInterface * * @since 4.4.0 * @throws \UnexpectedValueException May be thrown if the dispatcher has not been set. */ public function getDispatcher() { if (!$this->dispatcher) { @trigger_error( sprintf('Dispatcher for %s should be set through MVC factory. It will throw an exception in 6.0', __CLASS__), E_USER_DEPRECATED ); return $this->app->getDispatcher(); } return $this->dispatcher; } } View/JsonApiView.php 0000644 00000020520 15062121162 0010360 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\View; use Joomla\CMS\Document\JsonapiDocument; use Joomla\CMS\Factory; use Joomla\CMS\MVC\View\Event\OnGetApiFields; use Joomla\CMS\Router\Exception\RouteNotFoundException; use Joomla\CMS\Serializer\JoomlaSerializer; use Joomla\CMS\Uri\Uri; use Tobscure\JsonApi\AbstractSerializer; use Tobscure\JsonApi\Collection; use Tobscure\JsonApi\Resource; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; // phpcs:enable PSR1.Files.SideEffects /** * Base class for a Joomla Json List View * * Class holding methods for displaying presentation data. * * @since 4.0.0 */ abstract class JsonApiView extends JsonView { /** * The active document object (Redeclared for typehinting) * * @var JsonapiDocument * @since 3.0 */ public $document; /** * The content type * * @var string */ protected $type; /** * Item relationship * * @var array * * @since 4.0.0 */ protected $relationship = []; /** * Serializer data * * @var AbstractSerializer * @since 4.0.0 */ protected $serializer; /** * The fields to render item in the documents * * @var array * @since 4.0.0 */ protected $fieldsToRenderItem = []; /** * The fields to render items in the documents * * @var array * @since 4.0.0 */ protected $fieldsToRenderList = []; /** * Constructor. * * @param array $config A named configuration array for object construction. * contentType: the name (optional) of the content type to use for the serialization * * @since 4.0.0 */ public function __construct($config = []) { if (\array_key_exists('contentType', $config)) { $this->type = $config['contentType']; } if ($this->serializer === null) { $this->serializer = new JoomlaSerializer($this->type); } parent::__construct($config); } /** * Execute and display a template script. * * @param array|null $items Array of items * * @return string * * @since 4.0.0 */ public function displayList(array $items = null) { /** @var \Joomla\CMS\MVC\Model\ListModel $model */ $model = $this->getModel(); // Get page query $currentUrl = Uri::getInstance(); $currentPageDefaultInformation = ['offset' => 0, 'limit' => 20]; $currentPageQuery = $currentUrl->getVar('page', $currentPageDefaultInformation); if ($items === null) { $items = []; foreach ($model->getItems() as $item) { $items[] = $this->prepareItem($item); } } $pagination = $model->getPagination(); // Check for errors. if (\count($errors = $this->get('Errors'))) { throw new GenericDataException(implode("\n", $errors), 500); } if ($this->type === null) { throw new \RuntimeException('Content type missing'); } // Set up links for pagination $totalItemsCount = ($pagination->pagesTotal * $pagination->limit); $this->getDocument()->addMeta('total-pages', $pagination->pagesTotal) ->addLink('self', (string) $currentUrl); // Check for first and previous pages if ($pagination->limitstart > 0) { $firstPage = clone $currentUrl; $firstPageQuery = $currentPageQuery; $firstPageQuery['offset'] = 0; $firstPage->setVar('page', $firstPageQuery); $previousPage = clone $currentUrl; $previousPageQuery = $currentPageQuery; $previousOffset = $currentPageQuery['offset'] - $pagination->limit; $previousPageQuery['offset'] = $previousOffset >= 0 ? $previousOffset : 0; $previousPage->setVar('page', $previousPageQuery); $this->getDocument()->addLink('first', $this->queryEncode((string) $firstPage)) ->addLink('previous', $this->queryEncode((string) $previousPage)); } // Check for next and last pages if ($pagination->limitstart + $pagination->limit < $totalItemsCount) { $nextPage = clone $currentUrl; $nextPageQuery = $currentPageQuery; $nextOffset = $currentPageQuery['offset'] + $pagination->limit; $nextPageQuery['offset'] = ($nextOffset > ($pagination->pagesTotal * $pagination->limit)) ? $pagination->pagesTotal - $pagination->limit : $nextOffset; $nextPage->setVar('page', $nextPageQuery); $lastPage = clone $currentUrl; $lastPageQuery = $currentPageQuery; $lastPageQuery['offset'] = ($pagination->pagesTotal - 1) * $pagination->limit; $lastPage->setVar('page', $lastPageQuery); $this->getDocument()->addLink('next', $this->queryEncode((string) $nextPage)) ->addLink('last', $this->queryEncode((string) $lastPage)); } $eventData = ['type' => OnGetApiFields::LIST, 'fields' => $this->fieldsToRenderList, 'context' => $this->type]; $event = new OnGetApiFields('onApiGetFields', $eventData); /** @var OnGetApiFields $eventResult */ $eventResult = Factory::getApplication()->getDispatcher()->dispatch('onApiGetFields', $event); $collection = (new Collection($items, $this->serializer)) ->fields([$this->type => $eventResult->getAllPropertiesToRender()]); if (!empty($this->relationship)) { $collection->with($this->relationship); } // Set the data into the document and render it $this->getDocument()->setData($collection); return $this->getDocument()->render(); } /** * Execute and display a template script. * * @param object $item Item * * @return string * * @since 4.0.0 */ public function displayItem($item = null) { if ($item === null) { /** @var \Joomla\CMS\MVC\Model\AdminModel $model */ $model = $this->getModel(); $item = $this->prepareItem($model->getItem()); } if ($item->id === null) { throw new RouteNotFoundException('Item does not exist'); } // Check for errors. if (\count($errors = $this->get('Errors'))) { throw new GenericDataException(implode("\n", $errors), 500); } if ($this->type === null) { throw new \RuntimeException('Content type missing'); } $eventData = [ 'type' => OnGetApiFields::ITEM, 'fields' => $this->fieldsToRenderItem, 'relations' => $this->relationship, 'context' => $this->type, ]; $event = new OnGetApiFields('onApiGetFields', $eventData); /** @var OnGetApiFields $eventResult */ $eventResult = Factory::getApplication()->getDispatcher()->dispatch('onApiGetFields', $event); $element = (new Resource($item, $this->serializer)) ->fields([$this->type => $eventResult->getAllPropertiesToRender()]); if (!empty($this->relationship)) { $element->with($eventResult->getAllRelationsToRender()); } $this->getDocument()->setData($element); $this->getDocument()->addLink('self', Uri::current()); return $this->getDocument()->render(); } /** * Prepare item before render. * * @param object $item The model item * * @return object * * @since 4.0.0 */ protected function prepareItem($item) { return $item; } /** * Encode square brackets in the URI query, according to JSON API specification. * * @param string $query The URI query * * @return string * * @since 4.0.0 */ protected function queryEncode($query) { return str_replace(['[', ']'], ['%5B', '%5D'], $query); } } View/FormView.php 0000644 00000014564 15062121162 0007733 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2016 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\View; use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Factory; use Joomla\CMS\Language\Text; use Joomla\CMS\Object\CMSObject; use Joomla\CMS\Table\TableInterface; use Joomla\CMS\Toolbar\ToolbarHelper; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Base class for a Joomla Form View * * Class holding methods for displaying presentation data. * * @since 2.5.5 */ class FormView extends HtmlView { /** * The \JForm object * * @var \Joomla\CMS\Form\Form */ protected $form; /** * The active item * * @var object */ protected $item; /** * The item primary key name * * @var string */ protected $keyName; /** * The model state * * @var object */ protected $state; /** * The actions the user is authorised to perform * * @var CMSObject */ protected $canDo; /** * The toolbar title * * @var string */ protected $toolbarTitle; /** * The toolbar icon * * @var string */ protected $toolbarIcon; /** * The preview link * * @var string */ protected $previewLink; /** * The help link * * @var string */ protected $helpLink; /** * Constructor * * @param array $config An optional associative array of configuration settings. */ public function __construct(array $config) { parent::__construct($config); if (isset($config['help_link'])) { $this->helpLink = $config['help_link']; } if (isset($config['toolbar_icon'])) { $this->toolbarIcon = $config['toolbar_icon']; } else { $this->toolbarIcon = 'pencil-2 ' . $this->getName() . '-add'; } // Set default value for $canDo to avoid fatal error if child class doesn't set value for this property $this->canDo = new CMSObject(); } /** * Execute and display a template script. * * @param string $tpl The name of the template file to parse; automatically searches through the template paths. * * @return void * * @throws \Exception */ public function display($tpl = null) { // Prepare view data $this->initializeView(); // Check for errors. if (\count($errors = $this->get('Errors'))) { throw new GenericDataException(implode("\n", $errors), 500); } // Build toolbar $this->addToolbar(); parent::display($tpl); } /** * Prepare view data * * @return void */ protected function initializeView() { $this->form = $this->get('Form'); $this->item = $this->get('Item'); $this->state = $this->get('State'); $table = $this->get('Table'); $this->keyName = $table instanceof TableInterface ? $table->getKeyName() : 'id'; $action = empty($this->item->{$this->keyName}) ? '_NEW' : '_EDIT'; // Set default toolbar title $this->toolbarTitle = Text::_(strtoupper($this->option . '_MANAGER_' . $this->getName() . $action)); } /** * Add the page title and toolbar. * * @return void * * @since 1.6 */ protected function addToolbar() { Factory::getApplication()->getInput()->set('hidemainmenu', true); $user = Factory::getUser(); $userId = $user->id; $isNew = empty($this->item->{$this->keyName}); $viewName = $this->getName(); $checkedOut = $this->getModel()->isCheckedOut($this->item); $canDo = $this->canDo; ToolbarHelper::title( $this->toolbarTitle, $this->toolbarIcon ); // For new records, check the create permission. if ($isNew && $canDo->get('core.create')) { ToolbarHelper::saveGroup( [ ['apply', $viewName . '.apply'], ['save', $viewName . '.save'], ['save2new', $viewName . '.save2new'], ], 'btn-success' ); ToolbarHelper::cancel($viewName . '.cancel'); } else { // Since it's an existing record, check the edit permission, or fall back to edit own if the owner. if (property_exists($this->item, 'created_by')) { $itemEditable = $canDo->get('core.edit') || ($canDo->get('core.edit.own') && $this->item->created_by == $userId); } else { $itemEditable = $canDo->get('core.edit'); } $toolbarButtons = []; // Can't save the record if it's checked out and editable if (!$checkedOut && $itemEditable) { $toolbarButtons[] = ['apply', $viewName . '.apply']; $toolbarButtons[] = ['save', $viewName . '.save']; // We can save this record, but check the create permission to see if we can return to make a new one. if ($canDo->get('core.create')) { $toolbarButtons[] = ['save2new', $viewName . '.save2new']; } } // If checked out, we can still save if ($canDo->get('core.create')) { $toolbarButtons[] = ['save2copy', $viewName . '.save2copy']; } ToolbarHelper::saveGroup( $toolbarButtons, 'btn-success' ); if (ComponentHelper::isEnabled('com_contenthistory') && $this->state->params->get('save_history', 0) && $itemEditable) { ToolbarHelper::versions($this->option . '.' . $viewName, $this->item->id); } if (!$isNew && $this->previewLink) { ToolbarHelper::preview($this->previewLink, Text::_('JGLOBAL_PREVIEW'), 'eye', 80, 90); } ToolbarHelper::cancel($viewName . '.cancel', 'JTOOLBAR_CLOSE'); } ToolbarHelper::divider(); if ($this->helpLink) { ToolbarHelper::help($this->helpLink); } } } View/CategoryView.php 0000644 00000022334 15062121162 0010577 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2013 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\View; use Joomla\CMS\Categories\CategoryNode; use Joomla\CMS\Factory; use Joomla\CMS\Helper\TagsHelper; use Joomla\CMS\Language\Text; use Joomla\CMS\Plugin\PluginHelper; use Joomla\CMS\Router\Route; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Base HTML View class for the a Category list * * @since 3.2 */ class CategoryView extends HtmlView { /** * State data * * @var \Joomla\Registry\Registry * @since 3.2 */ protected $state; /** * Category items data * * @var array * @since 3.2 */ protected $items; /** * The category model object for this category * * @var CategoryNode * @since 3.2 */ protected $category; /** * The list of other categories for this extension. * * @var array * @since 3.2 */ protected $categories; /** * Pagination object * * @var \Joomla\CMS\Pagination\Pagination * @since 3.2 */ protected $pagination; /** * Child objects * * @var array * @since 3.2 */ protected $children; /** * The name of the extension for the category * * @var string * @since 3.2 */ protected $extension; /** * The name of the view to link individual items to * * @var string * @since 3.2 */ protected $viewName; /** * Default title to use for page title * * @var string * @since 3.2 */ protected $defaultPageTitle; /** * Whether to run the standard Joomla plugin events. * Off by default for b/c * * @var boolean * @since 3.5 */ protected $runPlugins = false; /** * The flag to mark if the active menu item is linked to the category being displayed * * @var bool * @since 4.0.0 */ protected $menuItemMatchCategory = false; /** * Method with common display elements used in category list displays * * @return void * * @since 3.2 */ public function commonCategoryDisplay() { $app = Factory::getApplication(); $user = Factory::getUser(); $params = $app->getParams(); // Get some data from the models $model = $this->getModel(); $paramsModel = $model->getState('params'); $paramsModel->set('check_access_rights', 0); $model->setState('params', $paramsModel); $state = $this->get('State'); $category = $this->get('Category'); $children = $this->get('Children'); $parent = $this->get('Parent'); if ($category == false) { throw new \InvalidArgumentException(Text::_('JGLOBAL_CATEGORY_NOT_FOUND'), 404); } if ($parent == false) { throw new \InvalidArgumentException(Text::_('JGLOBAL_CATEGORY_NOT_FOUND'), 404); } // Check whether category access level allows access. $groups = $user->getAuthorisedViewLevels(); if (!\in_array($category->access, $groups)) { throw new \RuntimeException(Text::_('JERROR_ALERTNOAUTHOR'), 403); } $items = $this->get('Items'); $pagination = $this->get('Pagination'); // Check for errors. if (\count($errors = $this->get('Errors'))) { throw new GenericDataException(implode("\n", $errors), 500); } // Setup the category parameters. $cparams = $category->getParams(); $category->params = clone $params; $category->params->merge($cparams); $children = [$category->id => $children]; // Escape strings for HTML output $this->pageclass_sfx = htmlspecialchars($params->get('pageclass_sfx', '')); if ($this->runPlugins) { PluginHelper::importPlugin('content'); foreach ($items as $itemElement) { $itemElement = (object) $itemElement; $itemElement->event = new \stdClass(); // For some plugins. !empty($itemElement->description) ? $itemElement->text = $itemElement->description : $itemElement->text = ''; Factory::getApplication()->triggerEvent('onContentPrepare', [$this->extension . '.category', &$itemElement, &$itemElement->params, 0]); $results = Factory::getApplication()->triggerEvent( 'onContentAfterTitle', [$this->extension . '.category', &$itemElement, &$itemElement->core_params, 0] ); $itemElement->event->afterDisplayTitle = trim(implode("\n", $results)); $results = Factory::getApplication()->triggerEvent( 'onContentBeforeDisplay', [$this->extension . '.category', &$itemElement, &$itemElement->core_params, 0] ); $itemElement->event->beforeDisplayContent = trim(implode("\n", $results)); $results = Factory::getApplication()->triggerEvent( 'onContentAfterDisplay', [$this->extension . '.category', &$itemElement, &$itemElement->core_params, 0] ); $itemElement->event->afterDisplayContent = trim(implode("\n", $results)); if ($itemElement->text) { $itemElement->description = $itemElement->text; } } } $maxLevel = $params->get('maxLevel', -1) < 0 ? PHP_INT_MAX : $params->get('maxLevel', PHP_INT_MAX); $this->maxLevel = &$maxLevel; $this->state = &$state; $this->items = &$items; $this->category = &$category; $this->children = &$children; $this->params = &$params; $this->parent = &$parent; $this->pagination = &$pagination; $this->user = &$user; // Check for layout override only if this is not the active menu item // If it is the active menu item, then the view and category id will match $active = $app->getMenu()->getActive(); if ( $active && $active->component == $this->extension && isset($active->query['view'], $active->query['id']) && $active->query['view'] === 'category' && $active->query['id'] == $this->category->id ) { if (isset($active->query['layout'])) { $this->setLayout($active->query['layout']); } $this->menuItemMatchCategory = true; } elseif ($layout = $category->params->get('category_layout')) { $this->setLayout($layout); } $this->category->tags = new TagsHelper(); $this->category->tags->getItemTags($this->extension . '.category', $this->category->id); } /** * Execute and display a template script. * * @param string $tpl The name of the template file to parse; automatically searches through the template paths. * * @return void * * @since 3.2 * @throws \Exception */ public function display($tpl = null) { $this->prepareDocument(); parent::display($tpl); } /** * Method to prepares the document * * @return void * * @since 3.2 */ protected function prepareDocument() { $app = Factory::getApplication(); $this->pathway = $app->getPathway(); // Because the application sets a default page title, we need to get it from the menu item itself $this->menu = $app->getMenu()->getActive(); if ($this->menu) { $this->params->def('page_heading', $this->params->get('page_title', $this->menu->title)); } else { $this->params->def('page_heading', Text::_($this->defaultPageTitle)); } $this->setDocumentTitle($this->params->get('page_title', '')); if ($this->params->get('menu-meta_description')) { $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } } /** * Method to add an alternative feed link to a category layout. * * @return void * * @since 3.2 */ protected function addFeed() { if ($this->params->get('show_feed_link', 1) == 1) { $link = '&format=feed&limitstart='; $attribs = ['type' => 'application/rss+xml', 'title' => htmlspecialchars($this->getDocument()->getTitle())]; $this->getDocument()->addHeadLink(Route::_($link . '&type=rss'), 'alternate', 'rel', $attribs); $attribs = ['type' => 'application/atom+xml', 'title' => htmlspecialchars($this->getDocument()->getTitle())]; $this->getDocument()->addHeadLink(Route::_($link . '&type=atom'), 'alternate', 'rel', $attribs); } } } View/ViewInterface.php 0000644 00000002015 15062121162 0010714 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\View; use Joomla\CMS\MVC\Model\BaseDatabaseModel; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Joomla Platform CMS Interface * * @since 4.0.0 */ interface ViewInterface { /** * Execute and display a template script. * * @param string $tpl The name of the template file to parse; automatically searches through the template paths. * * @return void * * @since 4.0.0 */ public function display($tpl = null); /** * Method to get the model object * * @param string $name The name of the model (optional) * * @return BaseDatabaseModel The model object * * @since 3.0 */ public function getModel($name = null); } View/AbstractView.php 0000644 00000022266 15062121162 0010571 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2005 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\View; use Joomla\CMS\Document\Document; use Joomla\CMS\Document\DocumentAwareInterface; use Joomla\CMS\Factory; use Joomla\CMS\Language\LanguageAwareInterface; use Joomla\CMS\Language\LanguageAwareTrait; use Joomla\CMS\MVC\Model\BaseDatabaseModel; use Joomla\CMS\Object\CMSObject; use Joomla\Event\DispatcherAwareInterface; use Joomla\Event\DispatcherAwareTrait; use Joomla\Event\DispatcherInterface; use Joomla\Event\EventInterface; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Base class for a Joomla View * * Class holding methods for displaying presentation data. * * @since 2.5.5 */ abstract class AbstractView extends CMSObject implements ViewInterface, DispatcherAwareInterface, DocumentAwareInterface, LanguageAwareInterface { use DispatcherAwareTrait; use LanguageAwareTrait; /** * The active document object * * @var Document * @since 3.0 * * @deprecated 4.4.0 will be removed in 6.0 * Use $this->getDocument() instead */ public $document; /** * The URL option for the component. It is usually passed by controller while it creates the view * * @var string * @since 3.0 */ protected $option = null; /** * The name of the view * * @var string * @since 3.0 */ protected $_name = null; /** * Registered models * * @var array * @since 3.0 */ protected $_models = []; /** * The default model * * @var string * @since 3.0 */ protected $_defaultModel = null; /** * Constructor * * @param array $config A named configuration array for object construction. * name: the name (optional) of the view (defaults to the view class name suffix). * charset: the character set to use for display * escape: the name (optional) of the function to use for escaping strings * base_path: the parent path (optional) of the views directory (defaults to the component folder) * template_plath: the path (optional) of the layout directory (defaults to base_path + /views/ + view name * helper_path: the path (optional) of the helper files (defaults to base_path + /helpers/) * layout: the layout (optional) to use to display the view * * @since 3.0 */ public function __construct($config = []) { // Set the view name if (empty($this->_name)) { if (\array_key_exists('name', $config)) { $this->_name = $config['name']; } else { $this->_name = $this->getName(); } } // Set the component name if passed if (!empty($config['option'])) { $this->option = $config['option']; } } /** * Execute and display a template script. * * @param string $tpl The name of the template file to parse; automatically searches through the template paths. * * @return void * * @since 3.0 */ abstract public function display($tpl = null); /** * Method to get data from a registered model or a property of the view * * @param string $property The name of the method to call on the model or the property to get * @param string $default The name of the model to reference or the default value [optional] * * @return mixed The return value of the method * * @since 3.0 */ public function get($property, $default = null) { // If $model is null we use the default model if ($default === null) { $model = $this->_defaultModel; } else { $model = strtolower($default); } // First check to make sure the model requested exists if (isset($this->_models[$model])) { // Model exists, let's build the method name $method = 'get' . ucfirst($property); // Does the method exist? if (method_exists($this->_models[$model], $method)) { // The method exists, let's call it and return what we get return $this->_models[$model]->$method(); } } // Degrade to CMSObject::get return parent::get($property, $default); } /** * Method to get the model object * * @param string $name The name of the model (optional) * * @return BaseDatabaseModel The model object * * @since 3.0 */ public function getModel($name = null) { if ($name === null) { $name = $this->_defaultModel; } return $this->_models[strtolower($name)]; } /** * Method to add a model to the view. We support a multiple model single * view system by which models are referenced by classname. A caveat to the * classname referencing is that any classname prepended by \JModel will be * referenced by the name without \JModel, eg. \JModelCategory is just * Category. * * @param BaseDatabaseModel $model The model to add to the view. * @param boolean $default Is this the default model? * * @return BaseDatabaseModel The added model. * * @since 3.0 */ public function setModel($model, $default = false) { $name = strtolower($model->getName()); $this->_models[$name] = $model; if ($default) { $this->_defaultModel = $name; } return $model; } /** * 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 * * @since 3.0 * @throws \Exception */ public function getName() { if (empty($this->_name)) { $reflection = new \ReflectionClass($this); if ($viewNamespace = $reflection->getNamespaceName()) { $pos = strrpos($viewNamespace, '\\'); if ($pos !== false) { $this->_name = strtolower(substr($viewNamespace, $pos + 1)); } } else { $className = \get_class($this); $viewPos = strpos($className, 'View'); if ($viewPos != false) { $this->_name = strtolower(substr($className, $viewPos + 4)); } } if (empty($this->_name)) { throw new \Exception(sprintf($this->text('JLIB_APPLICATION_ERROR_GET_NAME'), __METHOD__), 500); } } return $this->_name; } /** * Get the Document. * * @return Document * * @since 4.4.0 * @throws \UnexpectedValueException May be thrown if the document has not been set. */ protected function getDocument(): Document { if ($this->document) { return $this->document; } throw new \UnexpectedValueException('Document not set in ' . __CLASS__); } /** * Set the document to use. * * @param Document $document The document to use * * @return void * * @since 4.4.0 */ public function setDocument(Document $document): void { $this->document = $document; } /** * Get the event dispatcher. * * The override was made to keep a backward compatibility for legacy component. * TODO: Remove the override in 6.0 * * @return DispatcherInterface * * @since 4.4.0 * @throws \UnexpectedValueException May be thrown if the dispatcher has not been set. */ public function getDispatcher() { if (!$this->dispatcher) { @trigger_error( sprintf('Dispatcher for %s should be set through MVC factory. It will throw an exception in 6.0', __CLASS__), E_USER_DEPRECATED ); return Factory::getContainer()->get(DispatcherInterface::class); } return $this->dispatcher; } /** * Dispatches the given event on the internal dispatcher, does a fallback to the global one. * * @param EventInterface $event The event * * @return void * * @since 4.1.0 * * @deprecated 4.4 will be removed in 6.0. Use $this->getDispatcher() directly. */ protected function dispatchEvent(EventInterface $event) { $this->getDispatcher()->dispatch($event->getName(), $event); @trigger_error( sprintf( 'Method %s is deprecated and will be removed in 6.0. Use getDispatcher()->dispatch() directly.', __METHOD__ ), E_USER_DEPRECATED ); } } View/GenericDataException.php 0000644 00000001006 15062121162 0012205 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2016 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\View; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Exception class defining an error getting data from a model into a view * * @since 4.0.0 */ class GenericDataException extends \RuntimeException { } View/CategoriesView.php 0000644 00000006366 15062121162 0011116 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2013 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\View; use Joomla\CMS\Factory; use Joomla\CMS\Language\Text; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Categories view base class. * * @since 3.2 */ class CategoriesView extends HtmlView { /** * State data * * @var \Joomla\Registry\Registry * @since 3.2 */ protected $state; /** * Category items data * * @var array * @since 3.2 */ protected $items; /** * Language key for default page heading * * @var string * @since 3.2 */ protected $pageHeading; /** * Execute and display a template script. * * @param string $tpl The name of the template file to parse; automatically searches through the template paths. * * @return void|boolean * * @since 3.2 * @throws \Exception */ public function display($tpl = null) { $state = $this->get('State'); $items = $this->get('Items'); $parent = $this->get('Parent'); $app = Factory::getApplication(); // Check for errors. if (\count($errors = $this->get('Errors'))) { $app->enqueueMessage($errors, 'error'); return false; } if ($items === false) { $app->enqueueMessage(Text::_('JGLOBAL_CATEGORY_NOT_FOUND'), 'error'); return false; } if ($parent == false) { $app->enqueueMessage(Text::_('JGLOBAL_CATEGORY_NOT_FOUND'), 'error'); return false; } $params = &$state->params; $items = [$parent->id => $items]; // Escape strings for HTML output $this->pageclass_sfx = htmlspecialchars($params->get('pageclass_sfx', ''), ENT_COMPAT, 'UTF-8'); $this->maxLevelcat = $params->get('maxLevelcat', -1) < 0 ? PHP_INT_MAX : $params->get('maxLevelcat', PHP_INT_MAX); $this->params = &$params; $this->parent = &$parent; $this->items = &$items; $this->prepareDocument(); parent::display($tpl); } /** * Prepares the document * * @return void * * @since 3.2 */ protected function prepareDocument() { // Because the application sets a default page title, we need to get it from the menu item itself $menu = Factory::getApplication()->getMenu()->getActive(); if ($menu) { $this->params->def('page_heading', $this->params->get('page_title', $menu->title)); } else { $this->params->def('page_heading', Text::_($this->pageHeading)); } $this->setDocumentTitle($this->params->get('page_title', '')); if ($this->params->get('menu-meta_description')) { $this->getDocument()->setDescription($this->params->get('menu-meta_description')); } if ($this->params->get('robots')) { $this->getDocument()->setMetaData('robots', $this->params->get('robots')); } } } View/JsonView.php 0000644 00000005757 15062121162 0007745 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\View; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Base class for a Joomla Json View * * Class holding methods for displaying presentation data. * * @since 4.0.0 */ class JsonView extends AbstractView { /** * The base path of the view * * @var string * @since 4.0.0 */ protected $_basePath = null; /** * Charset to use in escaping mechanisms; defaults to urf8 (UTF-8) * * @var string * @since 4.0.0 */ protected $_charset = 'UTF-8'; /** * The output of the view. * * @var array * @since 4.0.0 */ protected $_output = []; /** * Constructor * * @param array $config A named configuration array for object construction. * name: the name (optional) of the view (defaults to the view class name suffix). * charset: the character set to use for display * escape: the name (optional) of the function to use for escaping strings * base_path: the parent path (optional) of the views directory (defaults to the component folder) * template_plath: the path (optional) of the layout directory (defaults to base_path + /views/ + view name * helper_path: the path (optional) of the helper files (defaults to base_path + /helpers/) * layout: the layout (optional) to use to display the view * * @since 4.0.0 */ public function __construct($config = []) { parent::__construct($config); // Set the charset (used by the variable escaping functions) if (\array_key_exists('charset', $config)) { @trigger_error( 'Setting a custom charset for escaping is deprecated. Override \JViewLegacy::escape() instead.', E_USER_DEPRECATED ); $this->_charset = $config['charset']; } // Set a base path for use by the view if (\array_key_exists('base_path', $config)) { $this->_basePath = $config['base_path']; } else { $this->_basePath = JPATH_COMPONENT; } } /** * Execute and display a template script. * * @param string $tpl The name of the template file to parse; automatically searches through the template paths. * * @return void * * @since 4.0.0 */ public function display($tpl = null) { // Serializing the output $result = json_encode($this->_output); // Pushing output to the document $this->getDocument()->setBuffer($result); } } View/HtmlView.php 0000644 00000043147 15062121162 0007733 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2006 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\View; use Joomla\CMS\Application\ApplicationHelper; use Joomla\CMS\Event\AbstractEvent; use Joomla\CMS\Factory; use Joomla\CMS\Filesystem\Path; use Joomla\CMS\Language\Text; use Joomla\CMS\Uri\Uri; use Joomla\CMS\User\CurrentUserInterface; use Joomla\CMS\User\CurrentUserTrait; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Base class for a Joomla Html View * * Class holding methods for displaying presentation data. * * @since 2.5.5 */ class HtmlView extends AbstractView implements CurrentUserInterface { use CurrentUserTrait; /** * The base path of the view * * @var string * @since 3.0 */ protected $_basePath = null; /** * Layout name * * @var string * @since 3.0 */ protected $_layout = 'default'; /** * Layout extension * * @var string * @since 3.0 */ protected $_layoutExt = 'php'; /** * Layout template * * @var string * @since 3.0 */ protected $_layoutTemplate = '_'; /** * The set of search directories for resources (templates) * * @var array * @since 3.0 */ protected $_path = ['template' => [], 'helper' => []]; /** * The name of the default template source file. * * @var string * @since 3.0 */ protected $_template = null; /** * The output of the template script. * * @var string * @since 3.0 */ protected $_output = null; /** * Charset to use in escaping mechanisms; defaults to urf8 (UTF-8) * * @var string * @since 3.0 */ protected $_charset = 'UTF-8'; /** * Constructor * * @param array $config A named configuration array for object construction. * name: the name (optional) of the view (defaults to the view class name suffix). * charset: the character set to use for display * escape: the name (optional) of the function to use for escaping strings * base_path: the parent path (optional) of the views directory (defaults to the component folder) * template_plath: the path (optional) of the layout directory (defaults to base_path + /views/ + view name * helper_path: the path (optional) of the helper files (defaults to base_path + /helpers/) * layout: the layout (optional) to use to display the view * * @since 3.0 */ public function __construct($config = []) { parent::__construct($config); // Set the charset (used by the variable escaping functions) if (\array_key_exists('charset', $config)) { @trigger_error( 'Setting a custom charset for escaping is deprecated. Override \JViewLegacy::escape() instead.', E_USER_DEPRECATED ); $this->_charset = $config['charset']; } // Set a base path for use by the view if (\array_key_exists('base_path', $config)) { $this->_basePath = $config['base_path']; } else { $this->_basePath = JPATH_COMPONENT; } // Set the default template search path if (\array_key_exists('template_path', $config)) { // User-defined dirs $this->_setPath('template', $config['template_path']); } elseif (is_dir($this->_basePath . '/tmpl/' . $this->getName())) { $this->_setPath('template', $this->_basePath . '/tmpl/' . $this->getName()); } elseif (is_dir($this->_basePath . '/View/' . $this->getName() . '/tmpl')) { $this->_setPath('template', $this->_basePath . '/View/' . $this->getName() . '/tmpl'); } elseif (is_dir($this->_basePath . '/view/' . $this->getName() . '/tmpl')) { $this->_setPath('template', $this->_basePath . '/view/' . $this->getName() . '/tmpl'); } elseif (is_dir($this->_basePath . '/views/' . $this->getName() . '/tmpl')) { $this->_setPath('template', $this->_basePath . '/views/' . $this->getName() . '/tmpl'); } else { $this->_setPath('template', $this->_basePath . '/views/' . $this->getName()); } // Set the default helper search path if (\array_key_exists('helper_path', $config)) { // User-defined dirs $this->_setPath('helper', $config['helper_path']); } else { $this->_setPath('helper', $this->_basePath . '/helpers'); } // Set the layout if (\array_key_exists('layout', $config)) { $this->setLayout($config['layout']); } else { $this->setLayout('default'); } $this->baseurl = Uri::base(true); } /** * Execute and display a template script. * * @param string $tpl The name of the template file to parse; automatically searches through the template paths. * * @return void * * @throws \Exception * @see \JViewLegacy::loadTemplate() * @since 3.0 */ public function display($tpl = null) { $app = Factory::getApplication(); if ($this->option) { $component = $this->option; } else { $component = ApplicationHelper::getComponentName(); } $context = $component . '.' . $this->getName(); $app->getDispatcher()->dispatch( 'onBeforeDisplay', AbstractEvent::create( 'onBeforeDisplay', [ 'eventClass' => 'Joomla\CMS\Event\View\DisplayEvent', 'subject' => $this, 'extension' => $context, ] ) ); $result = $this->loadTemplate($tpl); $eventResult = $app->getDispatcher()->dispatch( 'onAfterDisplay', AbstractEvent::create( 'onAfterDisplay', [ 'eventClass' => 'Joomla\CMS\Event\View\DisplayEvent', 'subject' => $this, 'extension' => $context, 'source' => $result, ] ) ); $eventResult->getArgument('used', false); echo $result; } /** * Escapes a value for output in a view script. * * If escaping mechanism is htmlspecialchars, use * {@link $_charset} setting. * * @param mixed $var The output to escape. * * @return mixed The escaped value. * * @note the ENT_COMPAT flag was replaced by ENT_QUOTES in Joomla 4.0 to also escape single quotes * * @since 3.0 */ public function escape($var) { if ($var === null) { return ''; } return htmlspecialchars($var, ENT_QUOTES, $this->_charset); } /** * Get the layout. * * @return string The layout name * * @since 3.0 */ public function getLayout() { return $this->_layout; } /** * Get the layout template. * * @return string The layout template name * * @since 3.0 */ public function getLayoutTemplate() { return $this->_layoutTemplate; } /** * Sets the layout name to use * * @param string $layout The layout name or a string in format <template>:<layout file> * * @return string Previous value. * * @since 3.0 */ public function setLayout($layout) { $previous = $this->_layout; if (strpos($layout, ':') === false) { $this->_layout = $layout; } else { // Convert parameter to array based on : $temp = explode(':', $layout); $this->_layout = $temp[1]; // Set layout template $this->_layoutTemplate = $temp[0]; } return $previous; } /** * Allows a different extension for the layout files to be used * * @param string $value The extension. * * @return string Previous value * * @since 3.0 */ public function setLayoutExt($value) { $previous = $this->_layoutExt; if ($value = preg_replace('#[^A-Za-z0-9]#', '', trim($value))) { $this->_layoutExt = $value; } return $previous; } /** * Adds to the stack of view script paths in LIFO order. * * @param mixed $path A directory path or an array of paths. * * @return void * * @since 3.0 */ public function addTemplatePath($path) { $this->_addPath('template', $path); } /** * Adds to the stack of helper script paths in LIFO order. * * @param mixed $path A directory path or an array of paths. * * @return void * * @since 3.0 */ public function addHelperPath($path) { $this->_addPath('helper', $path); } /** * Load a template file -- first look in the templates folder for an override * * @param string $tpl The name of the template source file; automatically searches the template paths and compiles as needed. * * @return string The output of the template script. * * @since 3.0 * @throws \Exception */ public function loadTemplate($tpl = null) { // Clear prior output $this->_output = null; $template = Factory::getApplication()->getTemplate(true); $layout = $this->getLayout(); $layoutTemplate = $this->getLayoutTemplate(); // Create the template file name based on the layout $file = isset($tpl) ? $layout . '_' . $tpl : $layout; // Clean the file name $file = preg_replace('/[^A-Z0-9_\.-]/i', '', $file); $tpl = isset($tpl) ? preg_replace('/[^A-Z0-9_\.-]/i', '', $tpl) : $tpl; try { // Load the language file for the template $lang = $this->getLanguage(); } catch (\UnexpectedValueException $e) { $lang = Factory::getApplication()->getLanguage(); } $lang->load('tpl_' . $template->template, JPATH_BASE) || $lang->load('tpl_' . $template->parent, JPATH_THEMES . '/' . $template->parent) || $lang->load('tpl_' . $template->template, JPATH_THEMES . '/' . $template->template); // Change the template folder if alternative layout is in different template if (isset($layoutTemplate) && $layoutTemplate !== '_' && $layoutTemplate != $template->template) { $this->_path['template'] = str_replace( JPATH_THEMES . DIRECTORY_SEPARATOR . $template->template . DIRECTORY_SEPARATOR, JPATH_THEMES . DIRECTORY_SEPARATOR . $layoutTemplate . DIRECTORY_SEPARATOR, $this->_path['template'] ); } // Load the template script $filetofind = $this->_createFileName('template', ['name' => $file]); $this->_template = Path::find($this->_path['template'], $filetofind); // If alternate layout can't be found, fall back to default layout if ($this->_template == false) { $filetofind = $this->_createFileName('', ['name' => 'default' . (isset($tpl) ? '_' . $tpl : $tpl)]); $this->_template = Path::find($this->_path['template'], $filetofind); } if ($this->_template != false) { // Unset so as not to introduce into template scope unset($tpl, $file); // Never allow a 'this' property if (isset($this->this)) { unset($this->this); } // Start capturing output into a buffer ob_start(); // Include the requested template filename in the local scope // (this will execute the view logic). include $this->_template; // Done with the requested template; get the buffer and // clear it. $this->_output = ob_get_contents(); ob_end_clean(); return $this->_output; } throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_LAYOUTFILE_NOT_FOUND', $file), 500); } /** * Load a helper file * * @param string $hlp The name of the helper source file automatically searches the helper paths and compiles as needed. * * @return void * * @since 3.0 */ public function loadHelper($hlp = null) { // Clean the file name $file = preg_replace('/[^A-Z0-9_\.-]/i', '', $hlp); // Load the template script $helper = Path::find($this->_path['helper'], $this->_createFileName('helper', ['name' => $file])); if ($helper != false) { // Include the requested template filename in the local scope include_once $helper; } } /** * Sets an entire array of search paths for templates or resources. * * @param string $type The type of path to set, typically 'template'. * @param mixed $path The new search path, or an array of search paths. If null or false, resets to the current directory only. * * @return void * * @since 3.0 */ protected function _setPath($type, $path) { if ($this->option) { $component = $this->option; } else { $component = ApplicationHelper::getComponentName(); } $app = Factory::getApplication(); // Clear out the prior search dirs $this->_path[$type] = []; // Actually add the user-specified directories $this->_addPath($type, $path); // Get the active template object $template = $app->getTemplate(true); // Always add the fallback directories as last resort switch (strtolower($type)) { case 'template': // Set the alternative template search dir if (isset($app)) { if ($component) { $component = preg_replace('/[^A-Z0-9_\.-]/i', '', $component); } $name = $this->getName(); if (!empty($template->parent)) { // Parent template's overrides $this->_addPath('template', JPATH_THEMES . '/' . $template->parent . '/html/' . $component . '/' . $name); // Child template's overrides $this->_addPath('template', JPATH_THEMES . '/' . $template->template . '/html/' . $component . '/' . $name); break; } $this->_addPath('template', JPATH_THEMES . '/' . $template->template . '/html/' . $component . '/' . $name); } break; } } /** * Adds to the search path for templates and resources. * * @param string $type The type of path to add. * @param mixed $path The directory or stream, or an array of either, to search. * * @return void * * @since 3.0 */ protected function _addPath($type, $path) { // Loop through the path directories foreach ((array) $path as $dir) { // Clean up the path $dir = Path::clean($dir); // Add trailing separators as needed if (substr($dir, -1) !== DIRECTORY_SEPARATOR) { // Directory $dir .= DIRECTORY_SEPARATOR; } // Add to the top of the search dirs array_unshift($this->_path[$type], $dir); } } /** * Create the filename for a resource * * @param string $type The resource type to create the filename for * @param array $parts An associative array of filename information * * @return string The filename * * @since 3.0 */ protected function _createFileName($type, $parts = []) { switch ($type) { case 'template': $filename = strtolower($parts['name']) . '.' . $this->_layoutExt; break; default: $filename = strtolower($parts['name']) . '.php'; break; } return $filename; } /** * Returns the form object * * @return mixed A \JForm object on success, false on failure * * @since 3.2 */ public function getForm() { if (!\is_object($this->form)) { $this->form = $this->get('Form'); } return $this->form; } /** * Sets the document title according to Global Configuration options * * @param string $title The page title * * @return void * * @since 3.6 */ public function setDocumentTitle($title) { $app = Factory::getApplication(); // Check for empty title and add site name if param is set if (empty($title)) { $title = $app->get('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')); } $this->getDocument()->setTitle($title); } } View/ListView.php 0000644 00000016242 15062121162 0007736 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2016 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\View; use Doctrine\Inflector\InflectorFactory; use Joomla\CMS\Factory; use Joomla\CMS\Language\Text; use Joomla\CMS\Layout\FileLayout; use Joomla\CMS\Object\CMSObject; use Joomla\CMS\Toolbar\Toolbar; use Joomla\CMS\Toolbar\ToolbarHelper; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Base class for a Joomla List View * * Class holding methods for displaying presentation data. * * @since 2.5.5 */ class ListView extends HtmlView { /** * An array of items * * @var array */ protected $items; /** * The pagination object * * @var \Joomla\CMS\Pagination\Pagination */ protected $pagination; /** * The model state * * @var CMSObject */ protected $state; /** * The actions the user is authorised to perform * * @var CMSObject */ protected $canDo; /** * Form object for search filters * * @var \Joomla\CMS\Form\Form */ public $filterForm; /** * The active search filters * * @var array */ public $activeFilters; /** * The sidebar markup * * @var string */ protected $sidebar; /** * The toolbar title * * @var string */ protected $toolbarTitle; /** * The toolbar icon * * @var string */ protected $toolbarIcon; /** * The flag which determine whether we want to show batch button * * @var boolean */ protected $supportsBatch = true; /** * The help link for the view * * @var string */ protected $helpLink; /** * Constructor * * @param array $config An optional associative array of configuration settings. */ public function __construct(array $config) { parent::__construct($config); // Set class properties from config data passed in constructor if (isset($config['toolbar_title'])) { $this->toolbarTitle = $config['toolbar_title']; } else { $this->toolbarTitle = strtoupper($this->option . '_MANAGER_' . $this->getName()); } if (isset($config['toolbar_icon'])) { $this->toolbarIcon = $config['toolbar_icon']; } else { $this->toolbarIcon = strtolower($this->getName()); } if (isset($config['supports_batch'])) { $this->supportsBatch = $config['supports_batch']; } if (isset($config['help_link'])) { $this->helpLink = $config['help_link']; } // Set default value for $canDo to avoid fatal error if child class doesn't set value for this property $this->canDo = new CMSObject(); } /** * Execute and display a template script. * * @param string $tpl The name of the template file to parse; automatically searches through the template paths. * * @return void * * @throws \Exception */ public function display($tpl = null) { // Prepare view data $this->initializeView(); // Check for errors. if (\count($errors = $this->get('Errors'))) { throw new GenericDataException(implode("\n", $errors), 500); } // Build toolbar $this->addToolbar(); parent::display($tpl); } /** * Prepare view data * * @return void */ protected function initializeView() { $componentName = substr($this->option, 4); $helperClass = ucfirst($componentName . 'Helper'); // Include the component helpers. \JLoader::register($helperClass, JPATH_COMPONENT . '/helpers/' . $componentName . '.php'); if ($this->getLayout() !== 'modal') { if (\is_callable($helperClass . '::addSubmenu')) { \call_user_func([$helperClass, 'addSubmenu'], $this->getName()); } $this->sidebar = \JHtmlSidebar::render(); } $this->items = $this->get('Items'); $this->pagination = $this->get('Pagination'); $this->state = $this->get('State'); $this->filterForm = $this->get('FilterForm'); $this->activeFilters = $this->get('ActiveFilters'); } /** * Add the page title and toolbar. * * @return void * * @since 1.6 */ protected function addToolbar() { $canDo = $this->canDo; $user = Factory::getUser(); // Get the toolbar object instance $bar = Toolbar::getInstance('toolbar'); $viewName = $this->getName(); $singularViewName = InflectorFactory::create()->build()->singularize($viewName); ToolbarHelper::title(Text::_($this->toolbarTitle), $this->toolbarIcon); if ($canDo->get('core.create')) { ToolbarHelper::addNew($singularViewName . '.add'); } if (($canDo->get('core.edit')) || ($canDo->get('core.edit.own'))) { ToolbarHelper::editList($singularViewName . '.edit'); } if ($canDo->get('core.edit.state')) { ToolbarHelper::publish($viewName . '.publish', 'JTOOLBAR_PUBLISH', true); ToolbarHelper::unpublish($viewName . '.unpublish', 'JTOOLBAR_UNPUBLISH', true); if (isset($this->items[0]->featured)) { ToolbarHelper::custom($viewName . '.featured', 'featured', '', 'JFEATURE', true); ToolbarHelper::custom($viewName . '.unfeatured', 'unfeatured', '', 'JUNFEATURE', true); } ToolbarHelper::archiveList($viewName . '.archive'); ToolbarHelper::checkin($viewName . '.checkin'); } // Add a batch button if ( $this->supportsBatch && $user->authorise('core.create', $this->option) && $user->authorise('core.edit', $this->option) && $user->authorise('core.edit.state', $this->option) ) { $title = Text::_('JTOOLBAR_BATCH'); // Instantiate a new LayoutFile instance and render the popup button $layout = new FileLayout('joomla.toolbar.popup'); $dhtml = $layout->render(['title' => $title]); $bar->appendButton('Custom', $dhtml, 'batch'); } if ( $canDo->get('core.delete') && ( $this->state->get('filter.state') == -2 || $this->state->get('filter.published') == -2 ) ) { ToolbarHelper::deleteList('JGLOBAL_CONFIRM_DELETE', $viewName . '.delete', 'JTOOLBAR_EMPTY_TRASH'); } elseif ($canDo->get('core.edit.state')) { ToolbarHelper::trash($viewName . '.trash'); } if ($user->authorise('core.admin', $this->option) || $user->authorise('core.options', $this->option)) { ToolbarHelper::preferences($this->option); } if ($this->helpLink) { ToolbarHelper::help($this->helpLink); } } } View/Event/OnGetApiFields.php 0000644 00000012321 15062121162 0012040 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2020 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\View\Event; use Joomla\CMS\Event\AbstractImmutableEvent; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; // phpcs:enable PSR1.Files.SideEffects /** * Event for getting extra API Fields and Relations to render with an entity * * @since 4.0.0 */ final class OnGetApiFields extends AbstractImmutableEvent { /** * List of types of view supported * * @since 4.0.0 */ public const LIST = 'list'; /** * List of types of view supported * * @since 4.0.0 */ public const ITEM = 'item'; /** * List of names of properties that will be rendered as relations * * @var string[] * @since 4.0.0 */ private $extraRelations = []; /** * List of names of properties that will be rendered as data * * @var string[] * @since 4.0.0 */ private $extraAttributes = []; /** * Constructor. * * Mandatory arguments: * type string The type of the field. Should be a constant from static::VIEW_TYPE * fields fields The list of fields that will be rendered in the API. * * @param string $name The event name. * @param array $arguments The event arguments. * * @throws \BadMethodCallException */ public function __construct($name, array $arguments = []) { if (!\array_key_exists('type', $arguments)) { throw new \BadMethodCallException("Argument 'type' is required for event $name"); } if (!\array_key_exists('fields', $arguments)) { throw new \BadMethodCallException("Argument 'fields' is required for event $name"); } if (!\array_key_exists('context', $arguments)) { throw new \BadMethodCallException("Argument 'context' is required for event $name"); } parent::__construct($name, $arguments); } /** * Setter for the type argument * * @param integer $value The constant from VIEW_TYPE * * @return mixed * * @throws \BadMethodCallException if the argument is not of the expected type */ protected function setType($value) { if (!in_array($value, [static::ITEM, static::LIST])) { throw new \BadMethodCallException("Argument 'type' of event {$this->name} must be a valid value"); } return $value; } /** * Setter for the fields argument * * @param mixed $value The value to set * * @return array * * @throws \BadMethodCallException if the argument is not a non-empty array */ protected function setFields($value) { if (!\is_array($value) || is_array($value) && empty($value)) { throw new \BadMethodCallException("Argument 'fields' of event {$this->name} must be be an array and not empty"); } return $value; } /** * Setter for the relations argument * * @param mixed $value The value to set * * @return array * * @throws \BadMethodCallException if the argument is not a non-empty array */ protected function setRelations($value) { if (!\is_array($value)) { throw new \BadMethodCallException("Argument 'relations' of event {$this->name} must be be an array"); } return $value; } /** * Allows the user to add names of properties that will be interpreted as relations * Note that if there is an existing data property it will also be displayed as well * as the relation due to the internal implementation (this behaviour is not part of this API * however and should not be guaranteed) * * @param string[] $fields The array of additional fields to add to the data of the attribute * * @return void */ public function addFields(array $fields): void { $this->extraAttributes = array_merge($this->extraAttributes, $fields); } /** * Allows the user to add names of properties that will be interpreted as relations * Note that if there is an existing data property it will also be displayed as well * as the relation due to the internal implementation (this behaviour is not part of this API * however and should not be guaranteed) * * @param string[] $fields The array of additional fields to add as relations * * @return void */ public function addRelations(array $fields): void { $this->extraRelations = array_merge($this->extraRelations, $fields); } /** * Get properties to render. * * @return array */ public function getAllPropertiesToRender(): array { return array_merge($this->getArgument('fields'), $this->extraAttributes); } /** * Get properties to render. * * @return array */ public function getAllRelationsToRender(): array { return array_merge($this->getArgument('relations'), $this->extraRelations); } } View/CategoryFeedView.php 0000644 00000011472 15062121162 0011364 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2013 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\View; use Joomla\CMS\Document\Feed\FeedItem; use Joomla\CMS\Factory; use Joomla\CMS\Helper\RouteHelper; use Joomla\CMS\Language\Text; use Joomla\CMS\Router\Route; use Joomla\CMS\UCM\UCMType; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Base feed View class for a category * * @since 3.2 */ class CategoryFeedView extends HtmlView { /** * Execute and display a template script. * * @param string $tpl The name of the template file to parse; automatically searches through the template paths. * * @return void * * @since 3.2 * @throws \Exception */ public function display($tpl = null) { $app = Factory::getApplication(); $document = $this->getDocument(); $extension = $app->getInput()->getString('option'); $contentType = $extension . '.' . $this->viewName; $ucmType = new UCMType(); $ucmRow = $ucmType->getTypeByAlias($contentType); $ucmMapCommon = json_decode($ucmRow->field_mappings)->common; $createdField = null; $titleField = null; if (\is_object($ucmMapCommon)) { $createdField = $ucmMapCommon->core_created_time; $titleField = $ucmMapCommon->core_title; } elseif (\is_array($ucmMapCommon)) { $createdField = $ucmMapCommon[0]->core_created_time; $titleField = $ucmMapCommon[0]->core_title; } $document->link = Route::_(RouteHelper::getCategoryRoute($app->getInput()->getInt('id'), $language = 0, $extension)); $app->getInput()->set('limit', $app->get('feed_limit')); $siteEmail = $app->get('mailfrom'); $fromName = $app->get('fromname'); $feedEmail = $app->get('feed_email', 'none'); $document->editor = $fromName; if ($feedEmail !== 'none') { $document->editorEmail = $siteEmail; } // Get some data from the model $items = $this->get('Items'); $category = $this->get('Category'); // Don't display feed if category id missing or non existent if ($category == false || $category->alias === 'root') { throw new \Exception(Text::_('JGLOBAL_CATEGORY_NOT_FOUND'), 404); } foreach ($items as $item) { $this->reconcileNames($item); // Strip html from feed item title if ($titleField) { $title = $this->escape($item->$titleField); $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8'); } else { $title = ''; } // URL link to article $router = new RouteHelper(); $link = Route::_($router->getRoute($item->id, $contentType, null, null, $item->catid)); // Strip HTML from feed item description text. $description = $item->description; $author = $item->created_by_alias ?: $item->author; $categoryTitle = isset($item->category_title) ? $item->category_title : $category->title; if ($createdField) { $date = isset($item->$createdField) ? date('r', strtotime($item->$createdField)) : ''; } else { $date = ''; } // Load individual item creator class. $feeditem = new FeedItem(); $feeditem->title = $title; $feeditem->link = $link; $feeditem->description = $description; $feeditem->date = $date; $feeditem->category = $categoryTitle; $feeditem->author = $author; // We don't have the author email so we have to use site in both cases. if ($feedEmail === 'site') { $feeditem->authorEmail = $siteEmail; } elseif ($feedEmail === 'author') { $feeditem->authorEmail = $item->author_email; } // Loads item information into RSS array $document->addItem($feeditem); } } /** * Method to reconcile non standard names from components to usage in this class. * Typically overridden in the component feed view class. * * @param object $item The item for a feed, an element of the $items array. * * @return void * * @since 3.2 */ protected function reconcileNames($item) { if (!property_exists($item, 'title') && property_exists($item, 'name')) { $item->title = $item->name; } } } Model/DatabaseModelInterface.php 0000644 00000001226 15062121162 0012620 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Model; use Joomla\Database\DatabaseInterface; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Interface for a database model. * * @since 4.0.0 */ interface DatabaseModelInterface { /** * Method to get the database driver object. * * @return DatabaseInterface * * @since 4.0.0 */ public function getDbo(); } Model/AdminModel.php 0000644 00000146344 15062121162 0010336 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2010 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Model; use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Event\Model\BeforeBatchEvent; use Joomla\CMS\Factory; use Joomla\CMS\Form\FormFactoryInterface; use Joomla\CMS\Language\Associations; use Joomla\CMS\Language\LanguageHelper; use Joomla\CMS\Language\Text; use Joomla\CMS\Log\Log; use Joomla\CMS\MVC\Factory\MVCFactoryInterface; use Joomla\CMS\Object\CMSObject; use Joomla\CMS\Plugin\PluginHelper; use Joomla\CMS\Router\Route; use Joomla\CMS\Table\Table; use Joomla\CMS\Table\TableInterface; use Joomla\CMS\Tag\TaggableTableInterface; use Joomla\CMS\UCM\UCMType; use Joomla\Database\ParameterType; use Joomla\Registry\Registry; use Joomla\String\StringHelper; use Joomla\Utilities\ArrayHelper; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Prototype admin model. * * @since 1.6 */ abstract class AdminModel extends FormModel { /** * The type alias for this content type (for example, 'com_content.article'). * * @var string * @since 3.8.6 */ public $typeAlias; /** * The prefix to use with controller messages. * * @var string * @since 1.6 */ protected $text_prefix = null; /** * The event to trigger after deleting the data. * * @var string * @since 1.6 */ protected $event_after_delete = null; /** * The event to trigger after saving the data. * * @var string * @since 1.6 */ protected $event_after_save = null; /** * The event to trigger before deleting the data. * * @var string * @since 1.6 */ protected $event_before_delete = null; /** * The event to trigger before saving the data. * * @var string * @since 1.6 */ protected $event_before_save = null; /** * The event to trigger before changing the published state of the data. * * @var string * @since 4.0.0 */ protected $event_before_change_state = null; /** * The event to trigger after changing the published state of the data. * * @var string * @since 1.6 */ protected $event_change_state = null; /** * The event to trigger before batch. * * @var string * @since 4.0.0 */ protected $event_before_batch = null; /** * Batch copy/move command. If set to false, * the batch copy/move command is not supported * * @var string * @since 3.4 */ protected $batch_copymove = 'category_id'; /** * Allowed batch commands * * @var array * @since 3.4 */ protected $batch_commands = [ 'assetgroup_id' => 'batchAccess', 'language_id' => 'batchLanguage', 'tag' => 'batchTag', ]; /** * The context used for the associations table * * @var string * @since 3.4.4 */ protected $associationsContext = null; /** * A flag to indicate if member variables for batch actions (and saveorder) have been initialized * * @var ?bool * @since 3.8.2 */ protected $batchSet = null; /** * The user performing the actions (re-usable in batch methods & saveorder(), initialized via initBatch()) * * @var object * @since 3.8.2 */ protected $user = null; /** * A JTable instance (of appropriate type) to manage the DB records (re-usable in batch methods & saveorder(), initialized via initBatch()) * * @var Table * @since 3.8.2 */ protected $table = null; /** * The class name of the JTable instance managing the DB records (re-usable in batch methods & saveorder(), initialized via initBatch()) * * @var string * @since 3.8.2 */ protected $tableClassName = null; /** * UCM Type corresponding to the current model class (re-usable in batch action methods, initialized via initBatch()) * * @var object * @since 3.8.2 */ protected $contentType = null; /** * DB data of UCM Type corresponding to the current model class (re-usable in batch action methods, initialized via initBatch()) * * @var object * @since 3.8.2 */ protected $type = null; /** * Constructor. * * @param array $config An array of configuration options (name, state, dbo, table_path, ignore_request). * @param ?MVCFactoryInterface $factory The factory. * @param ?FormFactoryInterface $formFactory The form factory. * * @since 1.6 * @throws \Exception */ public function __construct($config = [], MVCFactoryInterface $factory = null, FormFactoryInterface $formFactory = null) { parent::__construct($config, $factory, $formFactory); if (isset($config['event_after_delete'])) { $this->event_after_delete = $config['event_after_delete']; } elseif (empty($this->event_after_delete)) { $this->event_after_delete = 'onContentAfterDelete'; } if (isset($config['event_after_save'])) { $this->event_after_save = $config['event_after_save']; } elseif (empty($this->event_after_save)) { $this->event_after_save = 'onContentAfterSave'; } if (isset($config['event_before_delete'])) { $this->event_before_delete = $config['event_before_delete']; } elseif (empty($this->event_before_delete)) { $this->event_before_delete = 'onContentBeforeDelete'; } if (isset($config['event_before_save'])) { $this->event_before_save = $config['event_before_save']; } elseif (empty($this->event_before_save)) { $this->event_before_save = 'onContentBeforeSave'; } if (isset($config['event_before_change_state'])) { $this->event_before_change_state = $config['event_before_change_state']; } elseif (empty($this->event_before_change_state)) { $this->event_before_change_state = 'onContentBeforeChangeState'; } if (isset($config['event_change_state'])) { $this->event_change_state = $config['event_change_state']; } elseif (empty($this->event_change_state)) { $this->event_change_state = 'onContentChangeState'; } if (isset($config['event_before_batch'])) { $this->event_before_batch = $config['event_before_batch']; } elseif (empty($this->event_before_batch)) { $this->event_before_batch = 'onBeforeBatch'; } $config['events_map'] = $config['events_map'] ?? []; $this->events_map = array_merge( [ 'delete' => 'content', 'save' => 'content', 'change_state' => 'content', 'validate' => 'content', ], $config['events_map'] ); // Guess the \Text message prefix. Defaults to the option. if (isset($config['text_prefix'])) { $this->text_prefix = strtoupper($config['text_prefix']); } elseif (empty($this->text_prefix)) { $this->text_prefix = strtoupper($this->option); } } /** * Method to perform batch operations on an item or a set of items. * * @param array $commands An array of commands to perform. * @param array $pks An array of item ids. * @param array $contexts An array of item contexts. * * @return boolean Returns true on success, false on failure. * * @since 1.7 */ public function batch($commands, $pks, $contexts) { // Sanitize ids. $pks = array_unique($pks); $pks = ArrayHelper::toInteger($pks); // Remove any values of zero. if (array_search(0, $pks, true)) { unset($pks[array_search(0, $pks, true)]); } if (empty($pks)) { $this->setError(Text::_('JGLOBAL_NO_ITEM_SELECTED')); return false; } $done = false; // Initialize re-usable member properties $this->initBatch(); if ($this->batch_copymove && !empty($commands[$this->batch_copymove])) { $cmd = ArrayHelper::getValue($commands, 'move_copy', 'c'); if ($cmd === 'c') { $result = $this->batchCopy($commands[$this->batch_copymove], $pks, $contexts); if (\is_array($result)) { foreach ($result as $old => $new) { $contexts[$new] = $contexts[$old]; } $pks = array_values($result); } else { return false; } } elseif ($cmd === 'm' && !$this->batchMove($commands[$this->batch_copymove], $pks, $contexts)) { return false; } $done = true; } foreach ($this->batch_commands as $identifier => $command) { if (!empty($commands[$identifier])) { if (!$this->$command($commands[$identifier], $pks, $contexts)) { return false; } $done = true; } } if (!$done) { $this->setError(Text::_('JLIB_APPLICATION_ERROR_INSUFFICIENT_BATCH_INFORMATION')); return false; } // Clear the cache $this->cleanCache(); return true; } /** * Batch access level changes for a group of rows. * * @param integer $value The new value matching an Asset Group ID. * @param array $pks An array of row IDs. * @param array $contexts An array of item contexts. * * @return boolean True if successful, false otherwise and internal error is set. * * @since 1.7 */ protected function batchAccess($value, $pks, $contexts) { // Initialize re-usable member properties, and re-usable local variables $this->initBatch(); foreach ($pks as $pk) { if ($this->user->authorise('core.edit', $contexts[$pk])) { $this->table->reset(); $this->table->load($pk); $this->table->access = (int) $value; $event = new BeforeBatchEvent( $this->event_before_batch, ['src' => $this->table, 'type' => 'access'] ); $this->dispatchEvent($event); // Check the row. if (!$this->table->check()) { $this->setError($this->table->getError()); return false; } if (!$this->table->store()) { $this->setError($this->table->getError()); return false; } } else { $this->setError(Text::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT')); return false; } } // Clean the cache $this->cleanCache(); return true; } /** * Batch copy items to a new category or current. * * @param integer $value The new category. * @param array $pks An array of row IDs. * @param array $contexts An array of item contexts. * * @return array|boolean An array of new IDs on success, boolean false on failure. * * @since 1.7 */ protected function batchCopy($value, $pks, $contexts) { // Initialize re-usable member properties, and re-usable local variables $this->initBatch(); $categoryId = $value; if (!$this->checkCategoryId($categoryId)) { return false; } $newIds = []; $db = $this->getDbo(); // Parent exists so let's proceed while (!empty($pks)) { // Pop the first ID off the stack $pk = array_shift($pks); $this->table->reset(); // Check that the row actually exists if (!$this->table->load($pk)) { if ($error = $this->table->getError()) { // Fatal error $this->setError($error); return false; } else { // Not fatal error $this->setError(Text::sprintf('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk)); continue; } } // Check for asset_id if ($this->table->hasField($this->table->getColumnAlias('asset_id'))) { $oldAssetId = $this->table->asset_id; } $this->generateTitle($categoryId, $this->table); // Reset the ID because we are making a copy $this->table->id = 0; // Unpublish because we are making a copy if (isset($this->table->published)) { $this->table->published = 0; } elseif (isset($this->table->state)) { $this->table->state = 0; } $hitsAlias = $this->table->getColumnAlias('hits'); if (isset($this->table->$hitsAlias)) { $this->table->$hitsAlias = 0; } // New category ID $this->table->catid = $categoryId; $event = new BeforeBatchEvent( $this->event_before_batch, ['src' => $this->table, 'type' => 'copy'] ); $this->dispatchEvent($event); // @todo: Deal with ordering? // $this->table->ordering = 1; // Check the row. if (!$this->table->check()) { $this->setError($this->table->getError()); return false; } // Store the row. if (!$this->table->store()) { $this->setError($this->table->getError()); return false; } // Get the new item ID $newId = $this->table->get('id'); if (!empty($oldAssetId)) { $dbType = strtolower($db->getServerType()); // Copy rules $query = $db->getQuery(true); $query->clear() ->update($db->quoteName('#__assets', 't')); if ($dbType === 'mysql') { $query->set($db->quoteName('t.rules') . ' = ' . $db->quoteName('s.rules')); } else { $query->set($db->quoteName('rules') . ' = ' . $db->quoteName('s.rules')); } $query->join( 'INNER', $db->quoteName('#__assets', 's'), $db->quoteName('s.id') . ' = :oldassetid' ) ->where($db->quoteName('t.id') . ' = :assetid') ->bind(':oldassetid', $oldAssetId, ParameterType::INTEGER) ->bind(':assetid', $this->table->asset_id, ParameterType::INTEGER); $db->setQuery($query)->execute(); } $this->cleanupPostBatchCopy($this->table, $newId, $pk); // Add the new ID to the array $newIds[$pk] = $newId; } // Clean the cache $this->cleanCache(); return $newIds; } /** * Function that can be overridden to do any data cleanup after batch copying data * * @param TableInterface $table The table object containing the newly created item * @param integer $newId The id of the new item * @param integer $oldId The original item id * * @return void * * @since 3.8.12 */ protected function cleanupPostBatchCopy(TableInterface $table, $newId, $oldId) { } /** * Batch language changes for a group of rows. * * @param string $value The new value matching a language. * @param array $pks An array of row IDs. * @param array $contexts An array of item contexts. * * @return boolean True if successful, false otherwise and internal error is set. * * @since 2.5 */ protected function batchLanguage($value, $pks, $contexts) { // Initialize re-usable member properties, and re-usable local variables $this->initBatch(); foreach ($pks as $pk) { if ($this->user->authorise('core.edit', $contexts[$pk])) { $this->table->reset(); $this->table->load($pk); $this->table->language = $value; $event = new BeforeBatchEvent( $this->event_before_batch, ['src' => $this->table, 'type' => 'language'] ); $this->dispatchEvent($event); // Check the row. if (!$this->table->check()) { $this->setError($this->table->getError()); return false; } if (!$this->table->store()) { $this->setError($this->table->getError()); return false; } } else { $this->setError(Text::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT')); return false; } } // Clean the cache $this->cleanCache(); return true; } /** * Batch move items to a new category * * @param integer $value The new category ID. * @param array $pks An array of row IDs. * @param array $contexts An array of item contexts. * * @return boolean True if successful, false otherwise and internal error is set. * * @since 1.7 */ protected function batchMove($value, $pks, $contexts) { // Initialize re-usable member properties, and re-usable local variables $this->initBatch(); $categoryId = (int) $value; if (!$this->checkCategoryId($categoryId)) { return false; } // Parent exists so we proceed foreach ($pks as $pk) { if (!$this->user->authorise('core.edit', $contexts[$pk])) { $this->setError(Text::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT')); return false; } // Check that the row actually exists if (!$this->table->load($pk)) { if ($error = $this->table->getError()) { // Fatal error $this->setError($error); return false; } else { // Not fatal error $this->setError(Text::sprintf('JLIB_APPLICATION_ERROR_BATCH_MOVE_ROW_NOT_FOUND', $pk)); continue; } } // Set the new category ID $this->table->catid = $categoryId; $event = new BeforeBatchEvent( $this->event_before_batch, ['src' => $this->table, 'type' => 'move'] ); $this->dispatchEvent($event); // Check the row. if (!$this->table->check()) { $this->setError($this->table->getError()); return false; } // Store the row. if (!$this->table->store()) { $this->setError($this->table->getError()); return false; } } // Clean the cache $this->cleanCache(); return true; } /** * Batch tag a list of item. * * @param integer $value The value of the new tag. * @param array $pks An array of row IDs. * @param array $contexts An array of item contexts. * * @return boolean True if successful, false otherwise and internal error is set. * * @since 3.1 */ protected function batchTag($value, $pks, $contexts) { // Initialize re-usable member properties, and re-usable local variables $this->initBatch(); $tags = [$value]; foreach ($pks as $pk) { if ($this->user->authorise('core.edit', $contexts[$pk])) { $this->table->reset(); $this->table->load($pk); $setTagsEvent = \Joomla\CMS\Event\AbstractEvent::create( 'onTableSetNewTags', [ 'subject' => $this->table, 'newTags' => $tags, 'replaceTags' => false, ] ); try { $this->table->getDispatcher()->dispatch('onTableSetNewTags', $setTagsEvent); } catch (\RuntimeException $e) { $this->setError($e->getMessage()); return false; } } else { $this->setError(Text::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EDIT')); return false; } } // Clean the cache $this->cleanCache(); return true; } /** * Method to test whether a record can be deleted. * * @param object $record A record object. * * @return boolean True if allowed to delete the record. Defaults to the permission for the component. * * @since 1.6 */ protected function canDelete($record) { return Factory::getUser()->authorise('core.delete', $this->option); } /** * Method to test whether a record can have its state changed. * * @param object $record A record object. * * @return boolean True if allowed to change the state of the record. Defaults to the permission for the component. * * @since 1.6 */ protected function canEditState($record) { return Factory::getUser()->authorise('core.edit.state', $this->option); } /** * Method override to check-in a record or an array of record * * @param mixed $pks The ID of the primary key or an array of IDs * * @return integer|boolean Boolean false if there is an error, otherwise the count of records checked in. * * @since 1.6 */ public function checkin($pks = []) { $pks = (array) $pks; $table = $this->getTable(); $count = 0; if (empty($pks)) { $pks = [(int) $this->getState($this->getName() . '.id')]; } $checkedOutField = $table->getColumnAlias('checked_out'); // Check in all items. foreach ($pks as $pk) { if ($table->load($pk)) { if ($table->{$checkedOutField} > 0) { if (!parent::checkin($pk)) { return false; } $count++; } } else { $this->setError($table->getError()); return false; } } return $count; } /** * Method override to check-out a record. * * @param integer $pk The ID of the primary key. * * @return boolean True if successful, false if an error occurs. * * @since 1.6 */ public function checkout($pk = null) { $pk = (!empty($pk)) ? $pk : (int) $this->getState($this->getName() . '.id'); return parent::checkout($pk); } /** * Method to delete one or more records. * * @param array &$pks An array of record primary keys. * * @return boolean True if successful, false if an error occurs. * * @since 1.6 */ public function delete(&$pks) { $pks = ArrayHelper::toInteger((array) $pks); $table = $this->getTable(); // Include the plugins for the delete events. PluginHelper::importPlugin($this->events_map['delete']); // Iterate the items to delete each one. foreach ($pks as $i => $pk) { if ($table->load($pk)) { if ($this->canDelete($table)) { $context = $this->option . '.' . $this->name; // Trigger the before delete event. $result = Factory::getApplication()->triggerEvent($this->event_before_delete, [$context, $table]); if (\in_array(false, $result, true)) { $this->setError($table->getError()); return false; } // Multilanguage: if associated, delete the item in the _associations table if ($this->associationsContext && Associations::isEnabled()) { $db = $this->getDbo(); $query = $db->getQuery(true) ->select( [ 'COUNT(*) AS ' . $db->quoteName('count'), $db->quoteName('as1.key'), ] ) ->from($db->quoteName('#__associations', 'as1')) ->join('LEFT', $db->quoteName('#__associations', 'as2'), $db->quoteName('as1.key') . ' = ' . $db->quoteName('as2.key')) ->where( [ $db->quoteName('as1.context') . ' = :context', $db->quoteName('as1.id') . ' = :pk', ] ) ->bind(':context', $this->associationsContext) ->bind(':pk', $pk, ParameterType::INTEGER) ->group($db->quoteName('as1.key')); $db->setQuery($query); $row = $db->loadAssoc(); if (!empty($row['count'])) { $query = $db->getQuery(true) ->delete($db->quoteName('#__associations')) ->where( [ $db->quoteName('context') . ' = :context', $db->quoteName('key') . ' = :key', ] ) ->bind(':context', $this->associationsContext) ->bind(':key', $row['key']); if ($row['count'] > 2) { $query->where($db->quoteName('id') . ' = :pk') ->bind(':pk', $pk, ParameterType::INTEGER); } $db->setQuery($query); $db->execute(); } } if (!$table->delete($pk)) { $this->setError($table->getError()); return false; } // Trigger the after event. Factory::getApplication()->triggerEvent($this->event_after_delete, [$context, $table]); } else { // Prune items that you can't change. unset($pks[$i]); $error = $this->getError(); if ($error) { Log::add($error, Log::WARNING, 'jerror'); return false; } else { Log::add(Text::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED'), Log::WARNING, 'jerror'); return false; } } } else { $this->setError($table->getError()); return false; } } // Clear the component's cache $this->cleanCache(); return true; } /** * Method to change the title & alias. * * @param integer $categoryId The id of the category. * @param string $alias The alias. * @param string $title The title. * * @return array Contains the modified title and alias. * * @since 1.7 */ protected function generateNewTitle($categoryId, $alias, $title) { // Alter the title & alias $table = $this->getTable(); $aliasField = $table->getColumnAlias('alias'); $catidField = $table->getColumnAlias('catid'); $titleField = $table->getColumnAlias('title'); while ($table->load([$aliasField => $alias, $catidField => $categoryId])) { if ($title === $table->$titleField) { $title = StringHelper::increment($title); } $alias = StringHelper::increment($alias, 'dash'); } return [$title, $alias]; } /** * Method to get a single record. * * @param integer $pk The id of the primary key. * * @return CMSObject|boolean Object on success, false on failure. * * @since 1.6 */ public function getItem($pk = null) { $pk = (!empty($pk)) ? $pk : (int) $this->getState($this->getName() . '.id'); $table = $this->getTable(); if ($pk > 0) { // Attempt to load the row. $return = $table->load($pk); // Check for a table object error. if ($return === false) { // If there was no underlying error, then the false means there simply was not a row in the db for this $pk. if (!$table->getError()) { $this->setError(Text::_('JLIB_APPLICATION_ERROR_NOT_EXIST')); } else { $this->setError($table->getError()); } return false; } } // Convert to the CMSObject before adding other data. $properties = $table->getProperties(1); $item = ArrayHelper::toObject($properties, CMSObject::class); if (property_exists($item, 'params')) { $registry = new Registry($item->params); $item->params = $registry->toArray(); } return $item; } /** * A protected method to get a set of ordering conditions. * * @param Table $table A Table object. * * @return string[] An array of conditions to add to ordering queries. * * @since 1.6 */ protected function getReorderConditions($table) { return []; } /** * Stock method to auto-populate the model state. * * @return void * * @since 1.6 */ protected function populateState() { $table = $this->getTable(); $key = $table->getKeyName(); // Get the pk of the record from the request. $pk = Factory::getApplication()->getInput()->getInt($key); $this->setState($this->getName() . '.id', $pk); // Load the parameters. $value = ComponentHelper::getParams($this->option); $this->setState('params', $value); } /** * Prepare and sanitise the table data prior to saving. * * @param Table $table A reference to a Table object. * * @return void * * @since 1.6 */ protected function prepareTable($table) { // Derived class will provide its own implementation if required. } /** * Method to change the published state of one or more records. * * @param array &$pks A list of the primary keys to change. * @param integer $value The value of the published state. * * @return boolean True on success. * * @since 1.6 */ public function publish(&$pks, $value = 1) { $user = Factory::getUser(); $table = $this->getTable(); $pks = (array) $pks; $context = $this->option . '.' . $this->name; // Include the plugins for the change of state event. PluginHelper::importPlugin($this->events_map['change_state']); // Access checks. foreach ($pks as $i => $pk) { $table->reset(); if ($table->load($pk)) { if (!$this->canEditState($table)) { // Prune items that you can't change. unset($pks[$i]); Log::add(Text::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'), Log::WARNING, 'jerror'); return false; } // If the table is checked out by another user, drop it and report to the user trying to change its state. if ($table->hasField('checked_out') && $table->checked_out && ($table->checked_out != $user->id)) { Log::add(Text::_('JLIB_APPLICATION_ERROR_CHECKIN_USER_MISMATCH'), Log::WARNING, 'jerror'); // Prune items that you can't change. unset($pks[$i]); return false; } /** * Prune items that are already at the given state. Note: Only models whose table correctly * sets 'published' column alias (if different than published) will benefit from this */ $publishedColumnName = $table->getColumnAlias('published'); if (property_exists($table, $publishedColumnName) && $table->get($publishedColumnName, $value) == $value) { unset($pks[$i]); } } } // Check if there are items to change if (!\count($pks)) { return true; } // Trigger the before change state event. $result = Factory::getApplication()->triggerEvent($this->event_before_change_state, [$context, $pks, $value]); if (\in_array(false, $result, true)) { $this->setError($table->getError()); return false; } // Attempt to change the state of the records. if (!$table->publish($pks, $value, $user->get('id'))) { $this->setError($table->getError()); return false; } // Trigger the change state event. $result = Factory::getApplication()->triggerEvent($this->event_change_state, [$context, $pks, $value]); if (\in_array(false, $result, true)) { $this->setError($table->getError()); return false; } // Clear the component's cache $this->cleanCache(); return true; } /** * Method to adjust the ordering of a row. * * Returns NULL if the user did not have edit * privileges for any of the selected primary keys. * * @param integer $pks The ID of the primary key to move. * @param integer $delta Increment, usually +1 or -1 * * @return boolean|null False on failure or error, true on success, null if the $pk is empty (no items selected). * * @since 1.6 */ public function reorder($pks, $delta = 0) { $table = $this->getTable(); $pks = (array) $pks; $result = true; $allowed = true; foreach ($pks as $i => $pk) { $table->reset(); if ($table->load($pk) && $this->checkout($pk)) { // Access checks. if (!$this->canEditState($table)) { // Prune items that you can't change. unset($pks[$i]); $this->checkin($pk); Log::add(Text::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'), Log::WARNING, 'jerror'); $allowed = false; continue; } $where = $this->getReorderConditions($table); if (!$table->move($delta, $where)) { $this->setError($table->getError()); unset($pks[$i]); $result = false; } $this->checkin($pk); } else { $this->setError($table->getError()); unset($pks[$i]); $result = false; } } if ($allowed === false && empty($pks)) { $result = null; } // Clear the component's cache if ($result == true) { $this->cleanCache(); } return $result; } /** * Method to save the form data. * * @param array $data The form data. * * @return boolean True on success, False on error. * * @since 1.6 */ public function save($data) { $table = $this->getTable(); $context = $this->option . '.' . $this->name; $app = Factory::getApplication(); if (\array_key_exists('tags', $data) && \is_array($data['tags'])) { $table->newTags = $data['tags']; } $key = $table->getKeyName(); $pk = (isset($data[$key])) ? $data[$key] : (int) $this->getState($this->getName() . '.id'); $isNew = true; // Include the plugins for the save events. PluginHelper::importPlugin($this->events_map['save']); // Allow an exception to be thrown. try { // Load the row if saving an existing record. if ($pk > 0) { $table->load($pk); $isNew = false; } // Bind the data. if (!$table->bind($data)) { $this->setError($table->getError()); return false; } // Prepare the row for saving $this->prepareTable($table); // Check the data. if (!$table->check()) { $this->setError($table->getError()); return false; } // Trigger the before save event. $result = $app->triggerEvent($this->event_before_save, [$context, $table, $isNew, $data]); if (\in_array(false, $result, true)) { $this->setError($table->getError()); return false; } // Store the data. if (!$table->store()) { $this->setError($table->getError()); return false; } // Clean the cache. $this->cleanCache(); // Trigger the after save event. $app->triggerEvent($this->event_after_save, [$context, $table, $isNew, $data]); } catch (\Exception $e) { $this->setError($e->getMessage()); return false; } if (isset($table->$key)) { $this->setState($this->getName() . '.id', $table->$key); } $this->setState($this->getName() . '.new', $isNew); if ($this->associationsContext && Associations::isEnabled() && !empty($data['associations'])) { $associations = $data['associations']; // Unset any invalid associations $associations = ArrayHelper::toInteger($associations); // Unset any invalid associations foreach ($associations as $tag => $id) { if (!$id) { unset($associations[$tag]); } } // Show a warning if the item isn't assigned to a language but we have associations. if ($associations && $table->language === '*') { $app->enqueueMessage( Text::_(strtoupper($this->option) . '_ERROR_ALL_LANGUAGE_ASSOCIATED'), 'warning' ); } // Get associationskey for edited item $db = $this->getDbo(); $id = (int) $table->$key; $query = $db->getQuery(true) ->select($db->quoteName('key')) ->from($db->quoteName('#__associations')) ->where($db->quoteName('context') . ' = :context') ->where($db->quoteName('id') . ' = :id') ->bind(':context', $this->associationsContext) ->bind(':id', $id, ParameterType::INTEGER); $db->setQuery($query); $oldKey = $db->loadResult(); if ($associations || $oldKey !== null) { // Deleting old associations for the associated items $query = $db->getQuery(true) ->delete($db->quoteName('#__associations')) ->where($db->quoteName('context') . ' = :context') ->bind(':context', $this->associationsContext); $where = []; if ($associations) { $where[] = $db->quoteName('id') . ' IN (' . implode(',', $query->bindArray(array_values($associations))) . ')'; } if ($oldKey !== null) { $where[] = $db->quoteName('key') . ' = :oldKey'; $query->bind(':oldKey', $oldKey); } $query->extendWhere('AND', $where, 'OR'); $db->setQuery($query); $db->execute(); } // Adding self to the association if ($table->language !== '*') { $associations[$table->language] = (int) $table->$key; } if (\count($associations) > 1) { // Adding new association for these items $key = md5(json_encode($associations)); $query = $db->getQuery(true) ->insert($db->quoteName('#__associations')) ->columns( [ $db->quoteName('id'), $db->quoteName('context'), $db->quoteName('key'), ] ); foreach ($associations as $id) { $query->values( implode( ',', $query->bindArray( [$id, $this->associationsContext, $key], [ParameterType::INTEGER, ParameterType::STRING, ParameterType::STRING] ) ) ); } $db->setQuery($query); $db->execute(); } } if ($app->getInput()->get('task') == 'editAssociations') { return $this->redirectToAssociations($data); } return true; } /** * Saves the manually set order of records. * * @param array $pks An array of primary key ids. * @param integer $order +1 or -1 * * @return boolean Boolean true on success, false on failure * * @since 1.6 */ public function saveorder($pks = [], $order = null) { // Initialize re-usable member properties $this->initBatch(); $conditions = []; if (empty($pks)) { Factory::getApplication()->enqueueMessage(Text::_($this->text_prefix . '_ERROR_NO_ITEMS_SELECTED'), 'error'); return false; } $orderingField = $this->table->getColumnAlias('ordering'); // Update ordering values foreach ($pks as $i => $pk) { $this->table->load((int) $pk); // We don't want to modify tags on reorder, not removing the tagsHelper removes all associated tags if ($this->table instanceof TaggableTableInterface) { $this->table->clearTagsHelper(); } // Access checks. if (!$this->canEditState($this->table)) { // Prune items that you can't change. unset($pks[$i]); Log::add(Text::_('JLIB_APPLICATION_ERROR_EDITSTATE_NOT_PERMITTED'), Log::WARNING, 'jerror'); } elseif ($this->table->$orderingField != $order[$i]) { $this->table->$orderingField = $order[$i]; if (!$this->table->store()) { $this->setError($this->table->getError()); return false; } // Remember to reorder within position and client_id $condition = $this->getReorderConditions($this->table); $found = false; foreach ($conditions as $cond) { if ($cond[1] == $condition) { $found = true; break; } } if (!$found) { $key = $this->table->getKeyName(); $conditions[] = [$this->table->$key, $condition]; } } } // Execute reorder for each category. foreach ($conditions as $cond) { $this->table->load($cond[0]); $this->table->reorder($cond[1]); } // Clear the component's cache $this->cleanCache(); return true; } /** * Method to check the validity of the category ID for batch copy and move * * @param integer $categoryId The category ID to check * * @return boolean * * @since 3.2 */ protected function checkCategoryId($categoryId) { // Check that the category exists if ($categoryId) { $categoryTable = Table::getInstance('Category'); if (!$categoryTable->load($categoryId)) { if ($error = $categoryTable->getError()) { // Fatal error $this->setError($error); return false; } else { $this->setError(Text::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND')); return false; } } } if (empty($categoryId)) { $this->setError(Text::_('JLIB_APPLICATION_ERROR_BATCH_MOVE_CATEGORY_NOT_FOUND')); return false; } // Check that the user has create permission for the component $extension = Factory::getApplication()->getInput()->get('option', ''); $user = Factory::getUser(); if (!$user->authorise('core.create', $extension . '.category.' . $categoryId)) { $this->setError(Text::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_CREATE')); return false; } return true; } /** * A method to preprocess generating a new title in order to allow tables with alternative names * for alias and title to use the batch move and copy methods * * @param integer $categoryId The target category id * @param Table $table The Table within which move or copy is taking place * * @return void * * @since 3.2 */ public function generateTitle($categoryId, $table) { // Alter the title & alias $titleField = $table->getColumnAlias('title'); $aliasField = $table->getColumnAlias('alias'); $data = $this->generateNewTitle($categoryId, $table->$aliasField, $table->$titleField); $table->$titleField = $data['0']; $table->$aliasField = $data['1']; } /** * Method to initialize member variables used by batch methods and other methods like saveorder() * * @return void * * @since 3.8.2 */ public function initBatch() { if ($this->batchSet === null) { $this->batchSet = true; // Get current user $this->user = Factory::getUser(); // Get table $this->table = $this->getTable(); // Get table class name $tc = explode('\\', \get_class($this->table)); $this->tableClassName = end($tc); // Get UCM Type data $this->contentType = new UCMType(); $this->type = $this->contentType->getTypeByTable($this->tableClassName) ?: $this->contentType->getTypeByAlias($this->typeAlias); } } /** * Method to load an item in com_associations. * * @param array $data The form data. * * @return boolean True if successful, false otherwise. * * @since 3.9.0 * * @deprecated 4.3 will be removed in 6.0 * It is handled by regular save method now. */ public function editAssociations($data) { // Save the item return $this->save($data); } /** * Method to load an item in com_associations. * * @param array $data The form data. * * @return boolean True if successful, false otherwise. * * @throws \Exception * @since 3.9.17 */ protected function redirectToAssociations($data) { $app = Factory::getApplication(); $id = $data['id']; // Deal with categories associations if ($this->text_prefix === 'COM_CATEGORIES') { $extension = $app->getInput()->get('extension', 'com_content'); $this->typeAlias = $extension . '.category'; $component = strtolower($this->text_prefix); $view = 'category'; } else { $aliasArray = explode('.', $this->typeAlias); $component = $aliasArray[0]; $view = $aliasArray[1]; $extension = ''; } // Menu item redirect needs admin client $client = $component === 'com_menus' ? '&client_id=0' : ''; if ($id == 0) { $app->enqueueMessage(Text::_('JGLOBAL_ASSOCIATIONS_NEW_ITEM_WARNING'), 'error'); $app->redirect( Route::_('index.php?option=' . $component . '&view=' . $view . $client . '&layout=edit&id=' . $id . $extension, false) ); return false; } if ($data['language'] === '*') { $app->enqueueMessage(Text::_('JGLOBAL_ASSOC_NOT_POSSIBLE'), 'notice'); $app->redirect( Route::_('index.php?option=' . $component . '&view=' . $view . $client . '&layout=edit&id=' . $id . $extension, false) ); return false; } $languages = LanguageHelper::getContentLanguages([0, 1]); $target = ''; /** * If the site contains only 2 languages and an association exists for the item * load directly the associated target item in the side by side view * otherwise select already the target language */ if (count($languages) === 2) { $lang_code = []; foreach ($languages as $language) { $lang_code[] = $language->lang_code; } $refLang = [$data['language']]; $targetLang = array_diff($lang_code, $refLang); $targetLang = implode(',', $targetLang); $targetId = $data['associations'][$targetLang]; if ($targetId) { $target = '&target=' . $targetLang . '%3A' . $targetId . '%3Aedit'; } else { $target = '&target=' . $targetLang . '%3A0%3Aadd'; } } $app->redirect( Route::_( 'index.php?option=com_associations&view=association&layout=edit&itemtype=' . $this->typeAlias . '&task=association.edit&id=' . $id . $target, false ) ); return true; } } Model/BaseDatabaseModel.php 0000644 00000033271 15062121162 0011577 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2006 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Model; use Joomla\CMS\Cache\CacheControllerFactoryAwareInterface; use Joomla\CMS\Cache\CacheControllerFactoryAwareTrait; use Joomla\CMS\Cache\Controller\CallbackController; use Joomla\CMS\Cache\Exception\CacheExceptionInterface; use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Extension\ComponentInterface; use Joomla\CMS\Factory; use Joomla\CMS\Language\Text; use Joomla\CMS\MVC\Factory\LegacyFactory; use Joomla\CMS\MVC\Factory\MVCFactoryAwareTrait; use Joomla\CMS\MVC\Factory\MVCFactoryInterface; use Joomla\CMS\MVC\Factory\MVCFactoryServiceInterface; use Joomla\CMS\Table\Table; use Joomla\CMS\User\CurrentUserInterface; use Joomla\CMS\User\CurrentUserTrait; use Joomla\Database\DatabaseAwareInterface; use Joomla\Database\DatabaseAwareTrait; use Joomla\Database\DatabaseInterface; use Joomla\Database\DatabaseQuery; use Joomla\Database\Exception\DatabaseNotFoundException; use Joomla\Event\DispatcherAwareInterface; use Joomla\Event\DispatcherAwareTrait; use Joomla\Event\DispatcherInterface; use Joomla\Event\Event; use Joomla\Event\EventInterface; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Base class for a database aware Joomla Model * * Acts as a Factory class for application specific objects and provides many supporting API functions. * * @since 2.5.5 */ abstract class BaseDatabaseModel extends BaseModel implements DatabaseModelInterface, DispatcherAwareInterface, CurrentUserInterface, CacheControllerFactoryAwareInterface, DatabaseAwareInterface { use DatabaseAwareTrait; use MVCFactoryAwareTrait; use DispatcherAwareTrait; use CurrentUserTrait; use CacheControllerFactoryAwareTrait; /** * The URL option for the component. * * @var string * @since 3.0 */ protected $option = null; /** * The event to trigger when cleaning cache. * * @var string * @since 3.0 */ protected $event_clean_cache = null; /** * Constructor * * @param array $config An array of configuration options (name, state, dbo, table_path, ignore_request). * @param ?MVCFactoryInterface $factory The factory. * * @since 3.0 * @throws \Exception */ public function __construct($config = [], MVCFactoryInterface $factory = null) { parent::__construct($config); // Guess the option from the class name (Option)Model(View). if (empty($this->option)) { $r = null; if (!preg_match('/(.*)Model/i', \get_class($this), $r)) { throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_GET_NAME', __METHOD__), 500); } $this->option = ComponentHelper::getComponentName($this, $r[1]); } /** * @deprecated 4.3 will be Removed in 6.0 * Database instance is injected through the setter function, * subclasses should not use the db instance in constructor anymore */ $db = \array_key_exists('dbo', $config) ? $config['dbo'] : Factory::getDbo(); if ($db) { @trigger_error(sprintf('Database is not available in constructor in 6.0.'), E_USER_DEPRECATED); $this->setDatabase($db); // Is needed, when models use the deprecated MVC DatabaseAwareTrait, as the trait is overriding the local functions $this->setDbo($db); } // Set the default view search path if (\array_key_exists('table_path', $config)) { $this->addTablePath($config['table_path']); } elseif (\defined('JPATH_COMPONENT_ADMINISTRATOR')) { $this->addTablePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables'); $this->addTablePath(JPATH_COMPONENT_ADMINISTRATOR . '/table'); } // Set the clean cache event if (isset($config['event_clean_cache'])) { $this->event_clean_cache = $config['event_clean_cache']; } elseif (empty($this->event_clean_cache)) { $this->event_clean_cache = 'onContentCleanCache'; } if ($factory) { $this->setMVCFactory($factory); return; } $component = Factory::getApplication()->bootComponent($this->option); if ($component instanceof MVCFactoryServiceInterface) { $this->setMVCFactory($component->getMVCFactory()); } } /** * Gets an array of objects from the results of database query. * * @param DatabaseQuery|string $query The query. * @param integer $limitstart Offset. * @param integer $limit The number of records. * * @return object[] An array of results. * * @since 3.0 * @throws \RuntimeException */ protected function _getList($query, $limitstart = 0, $limit = 0) { if (\is_string($query)) { $query = $this->getDbo()->getQuery(true)->setQuery($query); } $query->setLimit($limit, $limitstart); $this->getDbo()->setQuery($query); return $this->getDbo()->loadObjectList(); } /** * Returns a record count for the query. * * Note: Current implementation of this method assumes that getListQuery() returns a set of unique rows, * thus it uses SELECT COUNT(*) to count the rows. In cases that getListQuery() uses DISTINCT * then either this method must be overridden by a custom implementation at the derived Model Class * or a GROUP BY clause should be used to make the set unique. * * @param DatabaseQuery|string $query The query. * * @return integer Number of rows for query. * * @since 3.0 */ protected function _getListCount($query) { // Use fast COUNT(*) on DatabaseQuery objects if there is no GROUP BY or HAVING clause: if ( $query instanceof DatabaseQuery && $query->type === 'select' && $query->group === null && $query->merge === null && $query->querySet === null && $query->having === null ) { $query = clone $query; $query->clear('select')->clear('order')->clear('limit')->clear('offset')->select('COUNT(*)'); $this->getDbo()->setQuery($query); return (int) $this->getDbo()->loadResult(); } // Otherwise fall back to inefficient way of counting all results. // Remove the limit, offset and order parts if it's a DatabaseQuery object if ($query instanceof DatabaseQuery) { $query = clone $query; $query->clear('limit')->clear('offset')->clear('order'); } $this->getDbo()->setQuery($query); $this->getDbo()->execute(); return (int) $this->getDbo()->getNumRows(); } /** * Method to load and return a table object. * * @param string $name The name of the view * @param string $prefix The class prefix. Optional. * @param array $config Configuration settings to pass to Table::getInstance * * @return Table|boolean Table object or boolean false if failed * * @since 3.0 * @see \JTable::getInstance() */ protected function _createTable($name, $prefix = 'Table', $config = []) { // Make sure we are returning a DBO object if (!\array_key_exists('dbo', $config)) { $config['dbo'] = $this->getDbo(); } $table = $this->getMVCFactory()->createTable($name, $prefix, $config); if ($table instanceof CurrentUserInterface) { $table->setCurrentUser($this->getCurrentUser()); } return $table; } /** * Method to get a table object, load it if necessary. * * @param string $name The table name. Optional. * @param string $prefix The class prefix. Optional. * @param array $options Configuration array for model. Optional. * * @return Table A Table object * * @since 3.0 * @throws \Exception */ public function getTable($name = '', $prefix = '', $options = []) { if (empty($name)) { $name = $this->getName(); } // We need this ugly code to deal with non-namespaced MVC code if (empty($prefix) && $this->getMVCFactory() instanceof LegacyFactory) { $prefix = 'Table'; } if ($table = $this->_createTable($name, $prefix, $options)) { return $table; } throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_TABLE_NAME_NOT_SUPPORTED', $name), 0); } /** * Method to check if the given record is checked out by the current user * * @param \stdClass $item The record to check * * @return bool */ public function isCheckedOut($item) { $table = $this->getTable(); $checkedOutField = $table->getColumnAlias('checked_out'); if (property_exists($item, $checkedOutField) && $item->{$checkedOutField} != $this->getCurrentUser()->id) { return true; } return false; } /** * Clean the cache * * @param string $group The cache group * * @return void * * @since 3.0 */ protected function cleanCache($group = null) { $app = Factory::getApplication(); $options = [ 'defaultgroup' => $group ?: ($this->option ?? $app->getInput()->get('option')), 'cachebase' => $app->get('cache_path', JPATH_CACHE), 'result' => true, ]; try { /** @var CallbackController $cache */ $cache = $this->getCacheControllerFactory()->createCacheController('callback', $options); $cache->clean(); } catch (CacheExceptionInterface $exception) { $options['result'] = false; } // Trigger the onContentCleanCache event. $this->dispatchEvent(new Event($this->event_clean_cache, $options)); } /** * Boots the component with the given name. * * @param string $component The component name, eg. com_content. * * @return ComponentInterface The service container * * @since 4.0.0 */ protected function bootComponent($component): ComponentInterface { return Factory::getApplication()->bootComponent($component); } /** * Get the event dispatcher. * * The override was made to keep a backward compatibility for legacy component. * TODO: Remove the override in 6.0 * * @return DispatcherInterface * * @since 4.4.0 * @throws \UnexpectedValueException May be thrown if the dispatcher has not been set. */ public function getDispatcher() { if (!$this->dispatcher) { @trigger_error( sprintf('Dispatcher for %s should be set through MVC factory. It will throw an exception in 6.0', __CLASS__), E_USER_DEPRECATED ); return Factory::getContainer()->get(DispatcherInterface::class); } return $this->dispatcher; } /** * Dispatches the given event on the internal dispatcher, does a fallback to the global one. * * @param EventInterface $event The event * * @return void * * @since 4.1.0 * * @deprecated 4.4 will be removed in 6.0. Use $this->getDispatcher() directly. */ protected function dispatchEvent(EventInterface $event) { $this->getDispatcher()->dispatch($event->getName(), $event); @trigger_error( sprintf( 'Method %s is deprecated and will be removed in 6.0. Use getDispatcher()->dispatch() directly.', __METHOD__ ), E_USER_DEPRECATED ); } /** * Get the database driver. * * @return DatabaseInterface The database driver. * * @since 4.2.0 * @throws \UnexpectedValueException * * @deprecated 4.3 will be removed in 6.0 * Use getDatabase() instead * Example: $model->getDatabase(); */ public function getDbo() { try { return $this->getDatabase(); } catch (DatabaseNotFoundException $e) { throw new \UnexpectedValueException('Database driver not set in ' . __CLASS__); } } /** * Set the database driver. * * @param ?DatabaseInterface $db The database driver. * * @return void * * @since 4.2.0 * * @deprecated 4.3 will be removed in 6.0 * Use setDatabase() instead * Example: $model->setDatabase($db); */ public function setDbo(DatabaseInterface $db = null) { if ($db === null) { return; } $this->setDatabase($db); } /** * Proxy for _db variable. * * @param string $name The name of the element * * @return mixed The value of the element if set, null otherwise * * @since 4.2.0 * * @deprecated 4.3 will be removed in 6.0 * Use getDatabase() instead of directly accessing _db */ public function __get($name) { if ($name === '_db') { return $this->getDbo(); } // Default the variable if (!isset($this->$name)) { $this->$name = null; } return $this->$name; } } Model/StatefulModelInterface.php 0000644 00000002262 15062121162 0012704 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Model; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Interface for a stateful model. * * @since 4.0.0 */ interface StatefulModelInterface { /** * Method to get model state variables. * * @param string $property Optional parameter name * @param mixed $default Optional default value * * @return mixed The property where specified, the state object where omitted * * @since 4.0.0 */ public function getState($property = null, $default = null); /** * Method to set model state variables. * * @param string $property The name of the property. * @param mixed $value The value of the property to set or null. * * @return mixed The previous value of the property or null if not set. * * @since 4.0.0 */ public function setState($property, $value = null); } Model/StateBehaviorTrait.php 0000644 00000004741 15062121162 0012063 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Model; use Joomla\CMS\Object\CMSObject; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Trait which supports state behavior * * @since 4.0.0 */ trait StateBehaviorTrait { /** * Indicates if the internal state has been set * * @var boolean * @since 4.0.0 */ protected $__state_set = null; /** * A state object * * @var CMSObject * @since 4.0.0 */ protected $state = null; /** * Method to get state variables. * * @param string $property Optional parameter name * @param mixed $default Optional default value * * @return mixed The property where specified, the state object where omitted * * @since 4.0.0 */ public function getState($property = null, $default = null) { if ($this->state === null) { $this->state = new CMSObject(); } if (!$this->__state_set) { // Protected method to auto-populate the state $this->populateState(); // Set the state set flag to true. $this->__state_set = true; } return $property === null ? $this->state : $this->state->get($property, $default); } /** * Method to set state variables. * * @param string $property The name of the property * @param mixed $value The value of the property to set or null * * @return mixed The previous value of the property or null if not set * * @since 4.0.0 */ public function setState($property, $value = null) { if ($this->state === null) { $this->state = new CMSObject(); } return $this->state->set($property, $value); } /** * Method to auto-populate the state. * * This method should only be called once per instantiation and is designed * to be called on the first call to the getState() method unless the * configuration flag to ignore the request is set. * * @return void * * @note Calling getState in this method will result in recursion. * @since 4.0.0 */ protected function populateState() { } } Model/WorkflowModelInterface.php 0000644 00000006555 15062121162 0012740 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2020 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Model; use Joomla\CMS\Form\Form; use Joomla\CMS\Table\Table; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Interface for a workflow model. * * @since 4.0.0 */ interface WorkflowModelInterface { /** * Set Up the workflow * * @param string $extension The option and section separated by. * * @return void * * @since 4.0.0 */ public function setUpWorkflow($extension); /** * Method to allow derived classes to preprocess the form. * * @param Form $form A Form object. * @param mixed $data The data expected for the form. * * @return void * * @see FormField * @since 4.0.0 * @throws \Exception if there is an error in the form event. */ public function workflowPreprocessForm(Form $form, $data); /** * Let plugins access stage change events * * @return void * * @since 4.0.0 */ public function workflowBeforeStageChange(); /** * Preparation of workflow data/plugins * * @return void * * @since 4.0.0 */ public function workflowBeforeSave(); /** * Executing of relevant workflow methods * * @return void * * @since 4.0.0 */ public function workflowAfterSave($data); /** * Batch change workflow stage or current. * * @param integer $oldId The ID of the item copied from * @param integer $newId The ID of the new item * * @return null * * @since 4.0.0 */ public function workflowCleanupBatchMove($oldId, $newId); /** * Runs transition for item. * * @param array $pks Id of items to execute the transition * @param integer $transitionId Id of transition * * @return boolean * * @since 4.0.0 */ public function executeTransition(array $pks, int $transitionId); /** * Method to get state variables. * * @param string $property Optional parameter name * @param mixed $default Optional default value * * @return mixed The property where specified, the state object where omitted * * @since 4.0.0 */ public function getState($property = null, $default = null); /** * Method to get the model 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 * * @since 4.0.0 * @throws \Exception */ public function getName(); /** * Method to get a table object, load it if necessary. * * @param string $name The table name. Optional. * @param string $prefix The class prefix. Optional. * @param array $options Configuration array for model. Optional. * * @return Table A Table object * * @since 3.0 * @throws \Exception */ public function getTable($name = '', $prefix = '', $options = []); } Model/ListModelInterface.php 0000644 00000001221 15062121162 0012022 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Model; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Interface for a list model. * * @since 4.0.0 */ interface ListModelInterface { /** * Method to get an array of data items. * * @return mixed An array of data items * * @since 4.0.0 * * @throws \Exception */ public function getItems(); } Model/FormBehaviorTrait.php 0000644 00000013055 15062121162 0011704 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Model; use Joomla\CMS\Factory; use Joomla\CMS\Form\Form; use Joomla\CMS\Form\FormFactoryInterface; use Joomla\CMS\Form\FormField; use Joomla\CMS\Plugin\PluginHelper; use Joomla\Utilities\ArrayHelper; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Trait which supports form behavior. * * @since 4.0.0 */ trait FormBehaviorTrait { /** * Array of form objects. * * @var Form[] * @since 4.0.0 */ protected $_forms = []; /** * Method to get a form object. * * @param string $name The name of the form. * @param string $source The form source. Can be XML string if file flag is set to false. * @param array $options Optional array of options for the form creation. * @param boolean $clear Optional argument to force load a new form. * @param string $xpath An optional xpath to search for the fields. * * @return Form * * @see Form * @since 4.0.0 * @throws \Exception */ protected function loadForm($name, $source = null, $options = [], $clear = false, $xpath = null) { // Handle the optional arguments. $options['control'] = ArrayHelper::getValue((array) $options, 'control', false); // Create a signature hash. But make sure, that loading the data does not create a new instance $sigoptions = $options; if (isset($sigoptions['load_data'])) { unset($sigoptions['load_data']); } $hash = md5($source . serialize($sigoptions)); // Check if we can use a previously loaded form. if (!$clear && isset($this->_forms[$hash])) { return $this->_forms[$hash]; } // Get the form. Form::addFormPath(JPATH_COMPONENT . '/forms'); Form::addFormPath(JPATH_COMPONENT . '/models/forms'); Form::addFieldPath(JPATH_COMPONENT . '/models/fields'); Form::addFormPath(JPATH_COMPONENT . '/model/form'); Form::addFieldPath(JPATH_COMPONENT . '/model/field'); try { $formFactory = $this->getFormFactory(); } catch (\UnexpectedValueException $e) { $formFactory = Factory::getContainer()->get(FormFactoryInterface::class); } $form = $formFactory->createForm($name, $options); // Load the data. if (substr($source, 0, 1) === '<') { if ($form->load($source, false, $xpath) == false) { throw new \RuntimeException('Form::loadForm could not load form'); } } else { if ($form->loadFile($source, false, $xpath) == false) { throw new \RuntimeException('Form::loadForm could not load file'); } } if (isset($options['load_data']) && $options['load_data']) { // Get the data for the form. $data = $this->loadFormData(); } else { $data = []; } // Allow for additional modification of the form, and events to be triggered. // We pass the data because plugins may require it. $this->preprocessForm($form, $data); // Load the data into the form after the plugins have operated. $form->bind($data); // Store the form for later. $this->_forms[$hash] = $form; return $form; } /** * Method to get the data that should be injected in the form. * * @return array The default data is an empty array. * * @since 4.0.0 */ protected function loadFormData() { return []; } /** * Method to allow derived classes to preprocess the data. * * @param string $context The context identifier. * @param mixed &$data The data to be processed. It gets altered directly. * @param string $group The name of the plugin group to import (defaults to "content"). * * @return void * * @since 4.0.0 */ protected function preprocessData($context, &$data, $group = 'content') { // Get the dispatcher and load the users plugins. PluginHelper::importPlugin($group); // Trigger the data preparation event. Factory::getApplication()->triggerEvent('onContentPrepareData', [$context, &$data]); } /** * Method to allow derived classes to preprocess the form. * * @param Form $form A Form object. * @param mixed $data The data expected for the form. * @param string $group The name of the plugin group to import (defaults to "content"). * * @return void * * @see FormField * @since 4.0.0 * @throws \Exception if there is an error in the form event. */ protected function preprocessForm(Form $form, $data, $group = 'content') { // Import the appropriate plugin group. PluginHelper::importPlugin($group); // Trigger the form preparation event. Factory::getApplication()->triggerEvent('onContentPrepareForm', [$form, $data]); } /** * Get the FormFactoryInterface. * * @return FormFactoryInterface * * @since 4.0.0 * @throws \UnexpectedValueException May be thrown if the FormFactory has not been set. */ abstract public function getFormFactory(): FormFactoryInterface; } Model/FormModel.php 0000644 00000016267 15062121162 0010211 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2009 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Model; use Joomla\CMS\Factory; use Joomla\CMS\Filter\InputFilter; use Joomla\CMS\Form\Form; use Joomla\CMS\Form\FormFactoryAwareInterface; use Joomla\CMS\Form\FormFactoryAwareTrait; use Joomla\CMS\Form\FormFactoryInterface; use Joomla\CMS\Form\FormRule; use Joomla\CMS\Language\Text; use Joomla\CMS\MVC\Factory\MVCFactoryInterface; use Joomla\CMS\Plugin\PluginHelper; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Prototype form model. * * @see Form * @see FormField * @see FormRule * @since 1.6 */ abstract class FormModel extends BaseDatabaseModel implements FormFactoryAwareInterface, FormModelInterface { use FormBehaviorTrait; use FormFactoryAwareTrait; /** * Maps events to plugin groups. * * @var array * @since 3.6 */ protected $events_map = null; /** * Constructor * * @param array $config An array of configuration options (name, state, dbo, table_path, ignore_request). * @param ?MVCFactoryInterface $factory The factory. * @param ?FormFactoryInterface $formFactory The form factory. * * @since 3.6 * @throws \Exception */ public function __construct($config = [], MVCFactoryInterface $factory = null, FormFactoryInterface $formFactory = null) { $config['events_map'] = $config['events_map'] ?? []; $this->events_map = array_merge( ['validate' => 'content'], $config['events_map'] ); parent::__construct($config, $factory); $this->setFormFactory($formFactory); } /** * Method to checkin a row. * * @param integer $pk The numeric id of the primary key. * * @return boolean False on failure or error, true otherwise. * * @since 1.6 */ public function checkin($pk = null) { // Only attempt to check the row in if it exists. if ($pk) { $user = $this->getCurrentUser(); // Get an instance of the row to checkin. $table = $this->getTable(); if (!$table->load($pk)) { $this->setError($table->getError()); return false; } // If there is no checked_out or checked_out_time field, just return true. if (!$table->hasField('checked_out') || !$table->hasField('checked_out_time')) { return true; } $checkedOutField = $table->getColumnAlias('checked_out'); // Check if this is the user having previously checked out the row. if ($table->$checkedOutField > 0 && $table->$checkedOutField != $user->get('id') && !$user->authorise('core.manage', 'com_checkin')) { $this->setError(Text::_('JLIB_APPLICATION_ERROR_CHECKIN_USER_MISMATCH')); return false; } // Attempt to check the row in. if (!$table->checkIn($pk)) { $this->setError($table->getError()); return false; } } return true; } /** * Method to check-out a row for editing. * * @param integer $pk The numeric id of the primary key. * * @return boolean False on failure or error, true otherwise. * * @since 1.6 */ public function checkout($pk = null) { // Only attempt to check the row in if it exists. if ($pk) { // Get an instance of the row to checkout. $table = $this->getTable(); if (!$table->load($pk)) { if ($table->getError() === false) { // There was no error returned, but false indicates that the row did not exist in the db, so probably previously deleted. $this->setError(Text::_('JLIB_APPLICATION_ERROR_NOT_EXIST')); } else { $this->setError($table->getError()); } return false; } // If there is no checked_out or checked_out_time field, just return true. if (!$table->hasField('checked_out') || !$table->hasField('checked_out_time')) { return true; } $user = $this->getCurrentUser(); // When the user is a guest, don't do a checkout if (!$user->id) { return false; } $checkedOutField = $table->getColumnAlias('checked_out'); // Check if this is the user having previously checked out the row. if ($table->$checkedOutField > 0 && $table->$checkedOutField != $user->get('id')) { $this->setError(Text::_('JLIB_APPLICATION_ERROR_CHECKOUT_USER_MISMATCH')); return false; } // Attempt to check the row out. if (!$table->checkOut($user->get('id'), $pk)) { $this->setError($table->getError()); return false; } } return true; } /** * Method to validate the form data. * * @param Form $form The form to validate against. * @param array $data The data to validate. * @param string $group The name of the field group to validate. * * @return array|boolean Array of filtered data if valid, false otherwise. * * @see FormRule * @see InputFilter * @since 1.6 */ public function validate($form, $data, $group = null) { // Include the plugins for the delete events. PluginHelper::importPlugin($this->events_map['validate']); $dispatcher = Factory::getContainer()->get('dispatcher'); if (!empty($dispatcher->getListeners('onUserBeforeDataValidation'))) { @trigger_error( 'The `onUserBeforeDataValidation` event is deprecated and will be removed in 5.0.' . 'Use the `onContentValidateData` event instead.', E_USER_DEPRECATED ); Factory::getApplication()->triggerEvent('onUserBeforeDataValidation', [$form, &$data]); } Factory::getApplication()->triggerEvent('onContentBeforeValidateData', [$form, &$data]); // Filter and validate the form data. $return = $form->process($data, $group); // Check for an error. if ($return instanceof \Exception) { $this->setError($return->getMessage()); return false; } // Check the validation results. if ($return === false) { // Get the validation messages from the form. foreach ($form->getErrors() as $message) { $this->setError($message); } return false; } $data = $return; // Tags B/C break at 3.1.2 if (!isset($data['tags']) && isset($data['metadata']['tags'])) { $data['tags'] = $data['metadata']['tags']; } return $data; } } Model/BaseModel.php 0000644 00000007557 15062121162 0010162 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2006 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Model; use Joomla\CMS\Filesystem\Path; use Joomla\CMS\Language\Text; use Joomla\CMS\Object\CMSObject; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Base class for a Joomla Model * * @since 4.0.0 */ abstract class BaseModel extends CMSObject implements ModelInterface, StatefulModelInterface { use StateBehaviorTrait; use LegacyModelLoaderTrait; /** * The model (base) name * * @var string * @since 4.0.0 */ protected $name; /** * The include paths * * @var array * @since 4.0.0 */ protected static $paths; /** * Constructor * * @param array $config An array of configuration options (name, state, ignore_request). * * @since 4.0.0 * @throws \Exception */ public function __construct($config = []) { // Set the view name if (empty($this->name)) { if (\array_key_exists('name', $config)) { $this->name = $config['name']; } else { $this->name = $this->getName(); } } // Set the model state if (\array_key_exists('state', $config)) { $this->state = $config['state']; } // Set the internal state marker - used to ignore setting state from the request if (!empty($config['ignore_request'])) { $this->__state_set = true; } } /** * Add a directory where \JModelLegacy should search for models. You may * either pass a string or an array of directories. * * @param mixed $path A path or array[sting] of paths to search. * @param string $prefix A prefix for models. * * @return array An array with directory elements. If prefix is equal to '', all directories are returned. * * @since 3.0 * * @deprecated 4.3 will be removed in 6.0 * Will be removed without replacement. Get the model through the MVCFactory + namespace instead * * @see LegacyModelLoaderTrait::getInstance(...) */ public static function addIncludePath($path = '', $prefix = '') { if (!isset(self::$paths)) { self::$paths = []; } if (!isset(self::$paths[$prefix])) { self::$paths[$prefix] = []; } if (!isset(self::$paths[''])) { self::$paths[''] = []; } if (!empty($path)) { foreach ((array) $path as $includePath) { if (!\in_array($includePath, self::$paths[$prefix])) { array_unshift(self::$paths[$prefix], Path::clean($includePath)); } if (!\in_array($includePath, self::$paths[''])) { array_unshift(self::$paths[''], Path::clean($includePath)); } } } return self::$paths[$prefix]; } /** * Method to get the model 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 * * @since 4.0.0 * @throws \Exception */ public function getName() { if (empty($this->name)) { $r = null; if (!preg_match('/Model(.*)/i', \get_class($this), $r)) { throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_GET_NAME', __METHOD__), 500); } $this->name = str_replace(['\\', 'model'], '', strtolower($r[1])); } return $this->name; } } Model/WorkflowBehaviorTrait.php 0000644 00000026236 15062121162 0012620 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2020 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Model; use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Factory; use Joomla\CMS\Filesystem\Path; use Joomla\CMS\Form\Form; use Joomla\CMS\Language\Text; use Joomla\CMS\Plugin\PluginHelper; use Joomla\CMS\Workflow\Workflow; use Joomla\Database\DatabaseDriver; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Trait which supports state behavior * * @since 4.0.0 */ trait WorkflowBehaviorTrait { /** * The name of the component. * * @var string * @since 4.0.0 */ protected $extension = null; /** * The section of the component. * * @var string * @since 4.0.0 */ protected $section = ''; /** * Is workflow for this component enabled? * * @var boolean * @since 4.0.0 */ protected $workflowEnabled = false; /** * The workflow object * * @var Workflow * @since 4.0.0 */ protected $workflow; /** * Set Up the workflow * * @param string $extension The option and section separated by. * * @return void * * @since 4.0.0 */ public function setUpWorkflow($extension) { $parts = explode('.', $extension); $this->extension = array_shift($parts); if (count($parts)) { $this->section = array_shift($parts); } if (method_exists($this, 'getDatabase')) { $db = $this->getDatabase(); } else { @trigger_error('From 6.0 implementing the getDatabase method will be mandatory.', E_USER_DEPRECATED); $db = Factory::getContainer()->get(DatabaseDriver::class); } $this->workflow = new Workflow($extension, Factory::getApplication(), $db); $params = ComponentHelper::getParams($this->extension); $this->workflowEnabled = $params->get('workflow_enabled'); $this->enableWorkflowBatch(); } /** * Add the workflow batch to the command list. Can be overwritten by the child class * * @return void * * @since 4.0.0 */ protected function enableWorkflowBatch() { // Enable batch if ($this->workflowEnabled && property_exists($this, 'batch_commands')) { $this->batch_commands['workflowstage_id'] = 'batchWorkflowStage'; } } /** * Method to allow derived classes to preprocess the form. * * @param Form $form A Form object. * @param mixed $data The data expected for the form. * * @return void * * @since 4.0.0 * @see FormField */ public function workflowPreprocessForm(Form $form, $data) { $this->addTransitionField($form, $data); if (!$this->workflowEnabled) { return; } // Import the workflow plugin group to allow form manipulation. $this->importWorkflowPlugins(); } /** * Let plugins access stage change events * * @return void * * @since 4.0.0 */ public function workflowBeforeStageChange() { if (!$this->workflowEnabled) { return; } $this->importWorkflowPlugins(); } /** * Preparation of workflow data/plugins * * @return void * * @since 4.0.0 */ public function workflowBeforeSave() { if (!$this->workflowEnabled) { return; } $this->importWorkflowPlugins(); } /** * Executing of relevant workflow methods * * @return void * * @since 4.0.0 */ public function workflowAfterSave($data) { // Regardless if workflow is active or not, we have to set the default stage // So we can work with the workflow, when the user activates it later $id = $this->getState($this->getName() . '.id'); $isNew = $this->getState($this->getName() . '.new'); // We save the first stage if ($isNew) { // We have to add the paths, because it could be called outside of the extension context $path = JPATH_BASE . '/components/' . $this->extension; $path = Path::check($path); Form::addFormPath($path . '/forms'); Form::addFormPath($path . '/models/forms'); Form::addFieldPath($path . '/models/fields'); Form::addFormPath($path . '/model/form'); Form::addFieldPath($path . '/model/field'); $form = $this->getForm(); $stage_id = $this->getStageForNewItem($form, $data); $this->workflow->createAssociation($id, $stage_id); } if (!$this->workflowEnabled) { return; } // Execute transition if (!empty($data['transition'])) { $this->executeTransition([$id], $data['transition']); } } /** * Batch change workflow stage or current. * * @param integer $value The workflow stage ID. * @param array $pks An array of row IDs. * @param array $contexts An array of item contexts. * * @return mixed An array of new IDs on success, boolean false on failure. * * @since 4.0.0 */ public function batchWorkflowStage(int $value, array $pks, array $contexts) { $user = Factory::getApplication()->getIdentity(); $workflow = Factory::getApplication()->bootComponent('com_workflow'); if (!$user->authorise('core.admin', $this->option)) { $this->setError(Text::_('JLIB_APPLICATION_ERROR_BATCH_CANNOT_EXECUTE_TRANSITION')); } // Get workflow stage information $stage = $workflow->getMVCFactory()->createTable('Stage', 'Administrator'); if (empty($value) || !$stage->load($value)) { Factory::getApplication()->enqueueMessage(Text::sprintf('JGLOBAL_BATCH_WORKFLOW_STAGE_ROW_NOT_FOUND'), 'error'); return false; } if (empty($pks)) { Factory::getApplication()->enqueueMessage(Text::sprintf('JGLOBAL_BATCH_WORKFLOW_STAGE_ROW_NOT_FOUND'), 'error'); return false; } // Update workflow associations return $this->workflow->updateAssociations($pks, $value); } /** * Batch change workflow stage or current. * * @param integer $oldId The ID of the item copied from * @param integer $newId The ID of the new item * * @return null * * @since 4.0.0 */ public function workflowCleanupBatchMove($oldId, $newId) { // Trigger workflow plugins only if enable (will be triggered from parent class) if ($this->workflowEnabled) { $this->importWorkflowPlugins(); } // We always need an association, so create one $table = $this->getTable(); $table->load($newId); $catKey = $table->getColumnAlias('catid'); $stage_id = $this->workflow->getDefaultStageByCategory($table->$catKey); if (empty($stage_id)) { return; } $this->workflow->createAssociation((int) $newId, (int) $stage_id); } /** * Runs transition for item. * * @param array $pks Id of items to execute the transition * @param integer $transitionId Id of transition * * @return boolean * * @since 4.0.0 */ public function executeTransition(array $pks, int $transitionId) { $result = $this->workflow->executeTransition($pks, $transitionId); if (!$result) { $app = Factory::getApplication(); $app->enqueueMessage(Text::_('COM_CONTENT_ERROR_UPDATE_STAGE', $app::MSG_WARNING)); return false; } return true; } /** * Import the Workflow plugins. * * @param Form $form A Form object. * @param mixed $data The data expected for the form. * * @return void */ protected function importWorkflowPlugins() { PluginHelper::importPlugin('workflow'); } /** * Adds a transition field to the form. Can be overwritten by the child class if not needed * * @param Form $form A Form object. * @param mixed $data The data expected for the form. * * @return void * @since 4.0.0 */ protected function addTransitionField(Form $form, $data) { $extension = $this->extension . ($this->section ? '.' . $this->section : ''); $field = new \SimpleXMLElement('<field></field>'); $field->addAttribute('name', 'transition'); $field->addAttribute('type', $this->workflowEnabled ? 'transition' : 'hidden'); $field->addAttribute('label', 'COM_CONTENT_WORKFLOW_STAGE'); $field->addAttribute('extension', $extension); $form->setField($field); $table = $this->getTable(); $key = $table->getKeyName(); $id = isset($data->$key) ? $data->$key : $form->getValue($key); if ($id) { // Transition field $assoc = $this->workflow->getAssociation($id); if (!empty($assoc->stage_id)) { $form->setFieldAttribute('transition', 'workflow_stage', (int) $assoc->stage_id); } } else { $stage_id = $this->getStageForNewItem($form, $data); if (!empty($stage_id)) { $form->setFieldAttribute('transition', 'workflow_stage', (int) $stage_id); } } } /** * Try to load a workflow stage for newly created items * which does not have a workflow assigned yet. If the category is not the * carrier, overwrite it on your model and deliver your own carrier. * * @param Form $form A Form object. * @param mixed $data The data expected for the form. * * @return boolean|integer An integer, holding the stage ID or false * @since 4.0.0 */ protected function getStageForNewItem(Form $form, $data) { $table = $this->getTable(); $hasKey = $table->hasField('catid'); if (!$hasKey) { return false; } $catKey = $table->getColumnAlias('catid'); $field = $form->getField($catKey); if (!$field) { return false; } $catId = isset(((object) $data)->$catKey) ? ((object) $data)->$catKey : $form->getValue($catKey); // Try to get the category from the html code of the field if (empty($catId)) { $catId = $field->getAttribute('default', null); if (!$catId) { // Choose the first category available $catOptions = $field->options; if ($catOptions && !empty($catOptions[0]->value)) { $catId = (int) $catOptions[0]->value; } } } if (empty($catId)) { return false; } return $this->workflow->getDefaultStageByCategory($catId); } } Model/DatabaseAwareTrait.php 0000644 00000003735 15062121162 0012011 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Model; use Joomla\Database\DatabaseInterface; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Database aware trait. * * @since 4.0.0 * * @deprecated 4.3 will be removed in 6.0 * Use the trait from the database package * Example: \Joomla\Database\DatabaseAwareTrait */ trait DatabaseAwareTrait { /** * The database driver. * * @var DatabaseInterface * @since 4.0.0 * * @deprecated 4.3 will be removed in 6.0 * Use the trait from the database package * Example: \Joomla\Database\DatabaseAwareTrait::$databaseAwareTraitDatabase */ protected $_db; /** * Get the database driver. * * @return DatabaseInterface The database driver. * * @since 4.0.0 * @throws \UnexpectedValueException * * @deprecated 4.3 will be removed in 6.0 * Use the trait from the database package * Example: \Joomla\Database\DatabaseAwareTrait::getDatabase() */ public function getDbo() { if ($this->_db) { return $this->_db; } throw new \UnexpectedValueException('Database driver not set in ' . __CLASS__); } /** * Set the database driver. * * @param ?DatabaseInterface $db The database driver. * * @return void * * @since 4.0.0 * * @deprecated 4.3 will be removed in 6.0 * Use the trait from the database package * Example: \Joomla\Database\DatabaseAwareTrait::setDatabase() */ public function setDbo(DatabaseInterface $db = null) { $this->_db = $db; } } Model/ItemModelInterface.php 0000644 00000001244 15062121162 0012012 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Model; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Interface for an item model. * * @since 4.0.0 */ interface ItemModelInterface { /** * Method to get an item. * * @param integer $pk The id of the item * * @return object * * @since 4.0.0 * @throws \Exception */ public function getItem($pk = null); } Model/Exception/ModelExceptionInterface.php 0000644 00000001346 15062121162 0015013 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Model\Exception; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Interface that all exceptions stemming from the model should implement for processing by the controller. * It is expected that the controller should catch all exceptions that implement this interface and then * make a decision as to whether the exception can be recovered from or not. * * @since 4.4.0 */ interface ModelExceptionInterface extends \Throwable { } Model/ListModel.php 0000644 00000054667 15062121162 0010227 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2009 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Model; use Joomla\CMS\Factory; use Joomla\CMS\Filter\InputFilter; use Joomla\CMS\Form\Form; use Joomla\CMS\Form\FormFactoryAwareInterface; use Joomla\CMS\Form\FormFactoryAwareTrait; use Joomla\CMS\MVC\Factory\MVCFactoryInterface; use Joomla\CMS\Pagination\Pagination; use Joomla\Database\DatabaseQuery; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Model class for handling lists of items. * * @since 1.6 */ class ListModel extends BaseDatabaseModel implements FormFactoryAwareInterface, ListModelInterface { use FormBehaviorTrait; use FormFactoryAwareTrait; /** * Internal memory based cache array of data. * * @var array * @since 1.6 */ protected $cache = []; /** * Context string for the model type. This is used to handle uniqueness * when dealing with the getStoreId() method and caching data structures. * * @var string * @since 1.6 */ protected $context = null; /** * Valid filter fields or ordering. * * @var array * @since 1.6 */ protected $filter_fields = []; /** * An internal cache for the last query used. * * @var DatabaseQuery|string * @since 1.6 */ protected $query = []; /** * The cache ID used when last populating $this->query * * @var null|string * @since 3.10.4 */ protected $lastQueryStoreId = null; /** * Name of the filter form to load * * @var string * @since 3.2 */ protected $filterFormName = null; /** * Associated HTML form * * @var string * @since 3.2 */ protected $htmlFormName = 'adminForm'; /** * A list of filter variables to not merge into the model's state * * @var array * @since 3.4.5 * @deprecated 4.0 will be removed in 6.0 * Use $filterForbiddenList instead */ protected $filterBlacklist = []; /** * A list of forbidden filter variables to not merge into the model's state * * @var array * @since 4.0.0 */ protected $filterForbiddenList = []; /** * A list of forbidden variables to not merge into the model's state * * @var array * @since 3.4.5 * @deprecated 4.0 will be removed in 6.0 * Use $listForbiddenList instead */ protected $listBlacklist = ['select']; /** * A list of forbidden variables to not merge into the model's state * * @var array * @since 4.0.0 */ protected $listForbiddenList = ['select']; /** * Constructor * * @param array $config An array of configuration options (name, state, dbo, table_path, ignore_request). * @param ?MVCFactoryInterface $factory The factory. * * @since 1.6 * @throws \Exception */ public function __construct($config = [], MVCFactoryInterface $factory = null) { parent::__construct($config, $factory); // Add the ordering filtering fields allowed list. if (isset($config['filter_fields'])) { $this->filter_fields = $config['filter_fields']; } // Guess the context as Option.ModelName. if (empty($this->context)) { $this->context = strtolower($this->option . '.' . $this->getName()); } /** * @deprecated 4.0 will be removed in 6.0 * Use $this->filterForbiddenList instead */ if (!empty($this->filterBlacklist)) { $this->filterForbiddenList = array_merge($this->filterBlacklist, $this->filterForbiddenList); } /** * @deprecated 4.0 will be removed in 6.0 * Use $this->listForbiddenList instead */ if (!empty($this->listBlacklist)) { $this->listForbiddenList = array_merge($this->listBlacklist, $this->listForbiddenList); } } /** * Provide a query to be used to evaluate if this is an Empty State, can be overridden in the model to provide granular control. * * @return DatabaseQuery * * @since 4.0.0 */ protected function getEmptyStateQuery() { $query = clone $this->_getListQuery(); if ($query instanceof DatabaseQuery) { $query->clear('bounded') ->clear('group') ->clear('having') ->clear('join') ->clear('values') ->clear('where'); } return $query; } /** * Is this an empty state, I.e: no items of this type regardless of the searched for states. * * @return boolean * * @throws \Exception * * @since 4.0.0 */ public function getIsEmptyState(): bool { return $this->_getListCount($this->getEmptyStateQuery()) === 0; } /** * Method to cache the last query constructed. * * This method ensures that the query is constructed only once for a given state of the model. * * @return DatabaseQuery A DatabaseQuery object * * @since 1.6 */ protected function _getListQuery() { // Compute the current store id. $currentStoreId = $this->getStoreId(); // If the last store id is different from the current, refresh the query. if ($this->lastQueryStoreId !== $currentStoreId || empty($this->query)) { $this->lastQueryStoreId = $currentStoreId; $this->query = $this->getListQuery(); } return $this->query; } /** * Function to get the active filters * * @return array Associative array in the format: array('filter_published' => 0) * * @since 3.2 */ public function getActiveFilters() { $activeFilters = []; if (!empty($this->filter_fields)) { foreach ($this->filter_fields as $filter) { $filterName = 'filter.' . $filter; if (property_exists($this->state, $filterName) && (!empty($this->state->{$filterName}) || is_numeric($this->state->{$filterName}))) { $activeFilters[$filter] = $this->state->get($filterName); } } } return $activeFilters; } /** * Method to get an array of data items. * * @return mixed An array of data items on success, false on failure. * * @since 1.6 */ public function getItems() { // Get a storage key. $store = $this->getStoreId(); // Try to load the data from internal storage. if (isset($this->cache[$store])) { return $this->cache[$store]; } try { // Load the list items and add the items to the internal cache. $this->cache[$store] = $this->_getList($this->_getListQuery(), $this->getStart(), $this->getState('list.limit')); } catch (\RuntimeException $e) { $this->setError($e->getMessage()); return false; } return $this->cache[$store]; } /** * Method to get a DatabaseQuery object for retrieving the data set from a database. * * @return DatabaseQuery|string A DatabaseQuery object to retrieve the data set. * * @since 1.6 */ protected function getListQuery() { return $this->getDbo()->getQuery(true); } /** * Method to get a \JPagination object for the data set. * * @return Pagination A Pagination object for the data set. * * @since 1.6 */ public function getPagination() { // Get a storage key. $store = $this->getStoreId('getPagination'); // Try to load the data from internal storage. if (isset($this->cache[$store])) { return $this->cache[$store]; } $limit = (int) $this->getState('list.limit') - (int) $this->getState('list.links'); // Create the pagination object and add the object to the internal cache. $this->cache[$store] = new Pagination($this->getTotal(), $this->getStart(), $limit); return $this->cache[$store]; } /** * Method to get a store id based on the model configuration state. * * This is necessary because the model is used by the component and * different modules that might need different sets of data or different * ordering requirements. * * @param string $id An identifier string to generate the store id. * * @return string A store id. * * @since 1.6 */ protected function getStoreId($id = '') { // Add the list state to the store id. $id .= ':' . $this->getState('list.start'); $id .= ':' . $this->getState('list.limit'); $id .= ':' . $this->getState('list.ordering'); $id .= ':' . $this->getState('list.direction'); return md5($this->context . ':' . $id); } /** * Method to get the total number of items for the data set. * * @return integer The total number of items available in the data set. * * @since 1.6 */ public function getTotal() { // Get a storage key. $store = $this->getStoreId('getTotal'); // Try to load the data from internal storage. if (isset($this->cache[$store])) { return $this->cache[$store]; } try { // Load the total and add the total to the internal cache. $this->cache[$store] = (int) $this->_getListCount($this->_getListQuery()); } catch (\RuntimeException $e) { $this->setError($e->getMessage()); return false; } return $this->cache[$store]; } /** * Method to get the starting number of items for the data set. * * @return integer The starting number of items available in the data set. * * @since 1.6 */ public function getStart() { $store = $this->getStoreId('getstart'); // Try to load the data from internal storage. if (isset($this->cache[$store])) { return $this->cache[$store]; } $start = $this->getState('list.start'); if ($start > 0) { $limit = $this->getState('list.limit'); $total = $this->getTotal(); if ($start > $total - $limit) { $start = max(0, (int) (ceil($total / $limit) - 1) * $limit); } } // Add the total to the internal cache. $this->cache[$store] = $start; return $this->cache[$store]; } /** * Get the filter form * * @param array $data data * @param boolean $loadData load current data * * @return Form|null The \JForm object or null if the form can't be found * * @since 3.2 */ public function getFilterForm($data = [], $loadData = true) { // Try to locate the filter form automatically. Example: ContentModelArticles => "filter_articles" if (empty($this->filterFormName)) { $classNameParts = explode('Model', \get_called_class()); if (\count($classNameParts) >= 2) { $this->filterFormName = 'filter_' . str_replace('\\', '', strtolower($classNameParts[1])); } } if (empty($this->filterFormName)) { return null; } try { // Get the form. return $this->loadForm($this->context . '.filter', $this->filterFormName, ['control' => '', 'load_data' => $loadData]); } catch (\RuntimeException $e) { } return null; } /** * Method to get the data that should be injected in the form. * * @return mixed The data for the form. * * @since 3.2 */ protected function loadFormData() { // Check the session for previously entered form data. $data = Factory::getApplication()->getUserState($this->context, new \stdClass()); // Pre-fill the list options if (!property_exists($data, 'list')) { $data->list = [ 'direction' => $this->getState('list.direction'), 'limit' => $this->getState('list.limit'), 'ordering' => $this->getState('list.ordering'), 'start' => $this->getState('list.start'), ]; } return $data; } /** * Method to auto-populate the model state. * * This method should only be called once per instantiation and is designed * to be called on the first call to the getState() method unless the model * configuration flag to ignore the request is set. * * Note. Calling getState in this method will result in recursion. * * @param string $ordering An optional ordering field. * @param string $direction An optional direction (asc|desc). * * @return void * * @since 1.6 */ protected function populateState($ordering = null, $direction = null) { // If the context is set, assume that stateful lists are used. if ($this->context) { $app = Factory::getApplication(); $inputFilter = InputFilter::getInstance(); // Receive & set filters if ($filters = $app->getUserStateFromRequest($this->context . '.filter', 'filter', [], 'array')) { foreach ($filters as $name => $value) { // Exclude if forbidden if (!\in_array($name, $this->filterForbiddenList)) { $this->setState('filter.' . $name, $value); } } } $limit = 0; // Receive & set list options if ($list = $app->getUserStateFromRequest($this->context . '.list', 'list', [], 'array')) { foreach ($list as $name => $value) { // Exclude if forbidden if (!\in_array($name, $this->listForbiddenList)) { // Extra validations switch ($name) { case 'fullordering': $orderingParts = explode(' ', $value); if (\count($orderingParts) >= 2) { // Latest part will be considered the direction $fullDirection = end($orderingParts); if (\in_array(strtoupper($fullDirection), ['ASC', 'DESC', ''])) { $this->setState('list.direction', $fullDirection); } else { $this->setState('list.direction', $direction); // Fallback to the default value $value = $ordering . ' ' . $direction; } unset($orderingParts[\count($orderingParts) - 1]); // The rest will be the ordering $fullOrdering = implode(' ', $orderingParts); if (\in_array($fullOrdering, $this->filter_fields)) { $this->setState('list.ordering', $fullOrdering); } else { $this->setState('list.ordering', $ordering); // Fallback to the default value $value = $ordering . ' ' . $direction; } } else { $this->setState('list.ordering', $ordering); $this->setState('list.direction', $direction); // Fallback to the default value $value = $ordering . ' ' . $direction; } break; case 'ordering': if (!\in_array($value, $this->filter_fields)) { $value = $ordering; } break; case 'direction': if ($value && (!\in_array(strtoupper($value), ['ASC', 'DESC', '']))) { $value = $direction; } break; case 'limit': $value = $inputFilter->clean($value, 'int'); $limit = $value; break; case 'select': $explodedValue = explode(',', $value); foreach ($explodedValue as &$field) { $field = $inputFilter->clean($field, 'cmd'); } $value = implode(',', $explodedValue); break; } $this->setState('list.' . $name, $value); } } } else { // Keep B/C for components previous to jform forms for filters // Pre-fill the limits $limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->get('list_limit'), 'uint'); $this->setState('list.limit', $limit); // Check if the ordering field is in the allowed list, otherwise use the incoming value. $value = $app->getUserStateFromRequest($this->context . '.ordercol', 'filter_order', $ordering); if (!\in_array($value, $this->filter_fields)) { $value = $ordering; $app->setUserState($this->context . '.ordercol', $value); } $this->setState('list.ordering', $value); // Check if the ordering direction is valid, otherwise use the incoming value. $value = $app->getUserStateFromRequest($this->context . '.orderdirn', 'filter_order_Dir', $direction); if (!$value || !\in_array(strtoupper($value), ['ASC', 'DESC', ''])) { $value = $direction; $app->setUserState($this->context . '.orderdirn', $value); } $this->setState('list.direction', $value); } // Support old ordering field $oldOrdering = $app->getInput()->get('filter_order'); if (!empty($oldOrdering) && \in_array($oldOrdering, $this->filter_fields)) { $this->setState('list.ordering', $oldOrdering); } // Support old direction field $oldDirection = $app->getInput()->get('filter_order_Dir'); if (!empty($oldDirection) && \in_array(strtoupper($oldDirection), ['ASC', 'DESC', ''])) { $this->setState('list.direction', $oldDirection); } $value = $app->getUserStateFromRequest($this->context . '.limitstart', 'limitstart', 0, 'int'); $limitstart = ($limit != 0 ? (floor($value / $limit) * $limit) : 0); $this->setState('list.start', $limitstart); } else { $this->setState('list.start', 0); $this->setState('list.limit', 0); } } /** * Gets the value of a user state variable and sets it in the session * * This is the same as the method in Application except that this also can optionally * force you back to the first page when a filter has changed * * @param string $key The key of the user state variable. * @param string $request The name of the variable passed in a request. * @param string $default The default value for the variable if not found. Optional. * @param string $type Filter for the variable, for valid values see {@link InputFilter::clean()}. Optional. * @param boolean $resetPage If true, the limitstart in request is set to zero * * @return mixed The request user state. * * @since 1.6 */ public function getUserStateFromRequest($key, $request, $default = null, $type = 'none', $resetPage = true) { $app = Factory::getApplication(); $input = $app->getInput(); $old_state = $app->getUserState($key); $cur_state = $old_state ?? $default; $new_state = $input->get($request, null, $type); // BC for Search Tools which uses different naming if ($new_state === null && strpos($request, 'filter_') === 0) { $name = substr($request, 7); $filters = $app->getInput()->get('filter', [], 'array'); if (isset($filters[$name])) { $new_state = $filters[$name]; } } if ($cur_state != $new_state && $new_state !== null && $resetPage) { $input->set('limitstart', 0); } // Save the new value only if it is set in this request. if ($new_state !== null) { $app->setUserState($key, $new_state); } else { $new_state = $cur_state; } return $new_state; } /** * Parse and transform the search string into a string fit for regex-ing arbitrary strings against * * @param string $search The search string * @param string $regexDelimiter The regex delimiter to use for the quoting * * @return string Search string escaped for regex * * @since 3.4 */ protected function refineSearchStringToRegex($search, $regexDelimiter = '/') { $searchArr = explode('|', trim($search, ' |')); foreach ($searchArr as $key => $searchString) { if (trim($searchString) === '') { unset($searchArr[$key]); continue; } $searchArr[$key] = str_replace(' ', '.*', preg_quote(trim($searchString), $regexDelimiter)); } return implode('|', $searchArr); } } Model/LegacyModelLoaderTrait.php 0000644 00000013412 15062121162 0012632 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Model; use Joomla\CMS\Extension\LegacyComponent; use Joomla\CMS\Factory; use Joomla\CMS\Filesystem\Path; use Joomla\CMS\Language\Text; use Joomla\CMS\Log\Log; use Joomla\CMS\MVC\Factory\MVCFactoryServiceInterface; use Joomla\CMS\Table\Table; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Trait which contains the legacy getInstance functionality * * @since 4.0.0 * * @deprecated 4.3 will be removed in 6.0 * Will be removed without replacement */ trait LegacyModelLoaderTrait { /** * Create the filename for a resource * * @param string $type The resource type to create the filename for. * @param array $parts An associative array of filename information. * * @return string The filename * * @since 3.0 * * @deprecated 4.3 will be removed in 6.0 * Will be removed without replacement */ protected static function _createFileName($type, $parts = []) { return $type === 'model' ? strtolower($parts['name']) . '.php' : ''; } /** * Returns a Model object, always creating it * * @param string $type The model type to instantiate * @param string $prefix Prefix for the model class name. Optional. * @param array $config Configuration array for model. Optional. * * @return self|boolean A \JModelLegacy instance or false on failure * * @since 3.0 * * @deprecated 4.3 will be removed in 6.0 * Will be removed without replacement. Get the model through the MVCFactory instead * Example: Factory::getApplication()->bootComponent('com_xxx')->getMVCFactory()->createModel($type, $prefix, $config); */ public static function getInstance($type, $prefix = '', $config = []) { @trigger_error( sprintf( '%1$s::getInstance() is deprecated. Load it through the MVC factory.', self::class ), E_USER_DEPRECATED ); $type = preg_replace('/[^A-Z0-9_\.-]/i', '', $type); if ($model = self::createModelFromComponent($type, $prefix, $config)) { return $model; } $modelClass = $prefix . ucfirst($type); if (!class_exists($modelClass)) { $path = Path::find(self::addIncludePath(null, $prefix), self::_createFileName('model', ['name' => $type])); if (!$path) { $path = Path::find(self::addIncludePath(null, ''), self::_createFileName('model', ['name' => $type])); } if (!$path) { return false; } require_once $path; if (!class_exists($modelClass)) { Log::add(Text::sprintf('JLIB_APPLICATION_ERROR_MODELCLASS_NOT_FOUND', $modelClass), Log::WARNING, 'jerror'); return false; } } return new $modelClass($config); } /** * Adds to the stack of model table paths in LIFO order. * * @param mixed $path The directory as a string or directories as an array to add. * * @return void * * @since 3.0 * * @deprecated 4.3 will be removed in 6.0 * Will be removed without replacement. Get the model through the MVCFactory instead */ public static function addTablePath($path) { Table::addIncludePath($path); } /** * Returns a Model object by loading the component from the prefix. * * @param string $type The model type to instantiate * @param string $prefix Prefix for the model class name. Optional. * @param array $config Configuration array for model. Optional. * * @return ModelInterface|null A ModelInterface instance or null on failure * * @since 4.0.0 * * @deprecated 4.3 will be removed in 6.0 * Will be removed without replacement */ private static function createModelFromComponent($type, $prefix = '', $config = []): ?ModelInterface { // Do nothing when prefix is not given if (!$prefix) { return null; } // Boot the component $componentName = 'com_' . str_replace('model', '', strtolower($prefix)); $component = Factory::getApplication()->bootComponent($componentName); // When it is a legacy component or not a MVCFactoryService then ignore if ($component instanceof LegacyComponent || !$component instanceof MVCFactoryServiceInterface) { return null; } // Setup the client $client = Factory::getApplication()->getName(); // Detect the client based on the include paths $adminPath = Path::clean(JPATH_ADMINISTRATOR . '/components/' . $componentName); $sitePath = Path::clean(JPATH_SITE . '/components/' . $componentName); foreach (self::addIncludePath() as $path) { if (strpos($path, $adminPath) !== false) { $client = 'Administrator'; break; } if (strpos($path, $sitePath) !== false) { $client = 'Site'; break; } } // Create the model $model = $component->getMVCFactory()->createModel($type, $client, $config); // When the model can't be loaded, then return null if (!$model) { return null; } // Return the model instance return $model; } } Model/FormModelInterface.php 0000644 00000001514 15062121162 0012017 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Model; use Joomla\CMS\Form\Form; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Interface for a form model. * * @since 4.0.0 */ interface FormModelInterface { /** * Method for getting a form. * * @param array $data Data for the form. * @param boolean $loadData True if the form is to load its own data (default case), false if not. * * @return Form * * @since 4.0.0 * * @throws \Exception */ public function getForm($data = [], $loadData = true); } Model/ModelInterface.php 0000644 00000001176 15062121162 0011177 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Model; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Interface for a base model. * * @since 4.0.0 */ interface ModelInterface { /** * Method to get the model name. * * @return string The name of the model * * @since 4.0.0 * @throws \Exception */ public function getName(); } Model/ItemModel.php 0000644 00000002345 15062121162 0010174 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2009 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Model; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Prototype item model. * * @since 1.6 */ abstract class ItemModel extends BaseDatabaseModel implements ItemModelInterface { /** * An item. * * @var array * @since 1.6 */ protected $_item = null; /** * Model context string. * * @var string * @since 1.6 */ protected $_context = 'group.type'; /** * Method to get a store id based on model configuration state. * * This is necessary because the model is used by the component and * different modules that might need different sets of data or different * ordering requirements. * * @param string $id A prefix for the store id. * * @return string A store id. * * @since 1.6 */ protected function getStoreId($id = '') { // Compile the store id. return md5($id); } } Factory/ApiMVCFactory.php 0000644 00000003631 15062121162 0011272 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Factory; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Factory to create MVC objects based on a namespace. Note that in an API Application model and table objects will be * created from their administrator counterparts. * * @since 4.0.0 */ final class ApiMVCFactory extends MVCFactory { /** * Method to load and return a model object. * * @param string $name The name of the model. * @param string $prefix Optional model prefix. * @param array $config Optional configuration array for the model. * * @return \Joomla\CMS\MVC\Model\ModelInterface The model object * * @since 4.0.0 * @throws \Exception */ public function createModel($name, $prefix = '', array $config = []) { $model = parent::createModel($name, $prefix, $config); if (!$model) { $model = parent::createModel($name, 'Administrator', $config); } return $model; } /** * Method to load and return a table object. * * @param string $name The name of the table. * @param string $prefix Optional table prefix. * @param array $config Optional configuration array for the table. * * @return \Joomla\CMS\Table\Table The table object * * @since 4.0.0 * @throws \Exception */ public function createTable($name, $prefix = '', array $config = []) { $table = parent::createTable($name, $prefix, $config); if (!$table) { $table = parent::createTable($name, 'Administrator', $config); } return $table; } } Factory/MVCFactoryInterface.php 0000644 00000005200 15062121162 0012453 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Factory; use Joomla\CMS\Application\CMSApplicationInterface; use Joomla\CMS\MVC\Model\ModelInterface; use Joomla\Input\Input; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Factory to create MVC objects. * * @since 3.10.0 */ interface MVCFactoryInterface { /** * Method to load and return a controller object. * * @param string $name The name of the controller * @param string $prefix The controller prefix * @param array $config The configuration array for the controller * @param CMSApplicationInterface $app The app * @param Input $input The input * * @return \Joomla\CMS\MVC\Controller\ControllerInterface * * @since 4.0.0 * @throws \Exception */ public function createController($name, $prefix, array $config, CMSApplicationInterface $app, Input $input); /** * Method to load and return a model object. * * @param string $name The name of the model. * @param string $prefix Optional model prefix. * @param array $config Optional configuration array for the model. * * @return ModelInterface The model object * * @since 3.10.0 * @throws \Exception */ public function createModel($name, $prefix = '', array $config = []); /** * Method to load and return a view object. * * @param string $name The name of the view. * @param string $prefix Optional view prefix. * @param string $type Optional type of view. * @param array $config Optional configuration array for the view. * * @return \Joomla\CMS\MVC\View\ViewInterface The view object * * @since 3.10.0 * @throws \Exception */ public function createView($name, $prefix = '', $type = '', array $config = []); /** * Method to load and return a table object. * * @param string $name The name of the table. * @param string $prefix Optional table prefix. * @param array $config Optional configuration array for the table. * * @return \Joomla\CMS\Table\Table The table object * * @since 3.10.0 * @throws \Exception */ public function createTable($name, $prefix = '', array $config = []); } Factory/MVCFactoryServiceInterface.php 0000644 00000001355 15062121162 0014003 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2018 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Factory; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; // phpcs:enable PSR1.Files.SideEffects /** * Interface to be implemented by classes depending on a MVC factory. * * @since 4.0.0 */ interface MVCFactoryServiceInterface { /** * Get the factory. * * @return MVCFactoryInterface * * @since 4.0.0 * @throws \UnexpectedValueException May be thrown if the factory has not been set. */ public function getMVCFactory(): MVCFactoryInterface; } Factory/MVCFactoryServiceTrait.php 0000644 00000002416 15062121162 0013165 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2018 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Factory; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; // phpcs:enable PSR1.Files.SideEffects /** * Defines the trait for a MVC factory service class. * * @since 4.0.0 */ trait MVCFactoryServiceTrait { /** * The MVC Factory. * * @var MVCFactoryInterface */ private $mvcFactory; /** * Get the factory. * * @return MVCFactoryInterface * * @since 4.0.0 * @throws \UnexpectedValueException May be thrown if the factory has not been set. */ public function getMVCFactory(): MVCFactoryInterface { if (!$this->mvcFactory) { throw new \UnexpectedValueException('MVC factory not set in ' . __CLASS__); } return $this->mvcFactory; } /** * The MVC Factory. * * @param MVCFactoryInterface $mvcFactory The factory * * @return void * * @since 4.0.0 */ public function setMVCFactory(MVCFactoryInterface $mvcFactory) { $this->mvcFactory = $mvcFactory; } } Factory/LegacyFactory.php 0000644 00000010567 15062121162 0011425 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Factory; use Joomla\CMS\Application\CMSApplicationInterface; use Joomla\CMS\Filesystem\Path; use Joomla\CMS\Language\Text; use Joomla\CMS\MVC\Controller\BaseController; use Joomla\CMS\MVC\Model\BaseDatabaseModel; use Joomla\CMS\MVC\Model\ModelInterface; use Joomla\CMS\Table\Table; use Joomla\Input\Input; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Factory to create MVC objects in legacy mode. * Uses the static getInstance function on the classes itself. Behavior of the old none * namespaced extension set up. * * @since 3.10.0 */ class LegacyFactory implements MVCFactoryInterface { /** * Method to load and return a controller object. * * @param string $name The name of the controller * @param string $prefix The controller prefix * @param array $config The configuration array for the controller * @param CMSApplicationInterface $app The app * @param Input $input The input * * @return \Joomla\CMS\MVC\Controller\ControllerInterface * * @since 4.0.0 * @throws \Exception */ public function createController($name, $prefix, array $config, CMSApplicationInterface $app, Input $input) { throw new \BadFunctionCallException('Legacy controller creation not supported.'); } /** * Method to load and return a model object. * * @param string $name The name of the model. * @param string $prefix Optional model prefix. * @param array $config Optional configuration array for the model. * * @return ModelInterface The model object * * @since 3.10.0 * @throws \Exception */ public function createModel($name, $prefix = '', array $config = []) { // Clean the model name $modelName = preg_replace('/[^A-Z0-9_]/i', '', $name); $classPrefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix); return BaseDatabaseModel::getInstance($modelName, $classPrefix, $config); } /** * Method to load and return a view object. * * @param string $name The name of the view. * @param string $prefix Optional view prefix. * @param string $type Optional type of view. * @param array $config Optional configuration array for the view. * * @return \Joomla\CMS\MVC\View\ViewInterface The view object * * @since 3.10.0 * @throws \Exception */ public function createView($name, $prefix = '', $type = '', array $config = []) { // Clean the view name $viewName = preg_replace('/[^A-Z0-9_]/i', '', $name); $classPrefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix); $viewType = preg_replace('/[^A-Z0-9_]/i', '', $type); // Build the view class name $viewClass = $classPrefix . $viewName; if (!class_exists($viewClass)) { $path = Path::find($config['paths'], BaseController::createFileName('view', ['name' => $viewName, 'type' => $viewType])); if (!$path) { return null; } \JLoader::register($viewClass, $path); if (!class_exists($viewClass)) { throw new \Exception(Text::sprintf('JLIB_APPLICATION_ERROR_VIEW_CLASS_NOT_FOUND', $viewClass, $path), 500); } } return new $viewClass($config); } /** * Method to load and return a table object. * * @param string $name The name of the table. * @param string $prefix Optional table prefix. * @param array $config Optional configuration array for the table. * * @return \Joomla\CMS\Table\Table The table object * * @since 3.10.0 * @throws \Exception */ public function createTable($name, $prefix = 'Table', array $config = []) { // Clean the model name $name = preg_replace('/[^A-Z0-9_]/i', '', $name); $prefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix); return Table::getInstance($name, $prefix, $config); } } Factory/MVCFactory.php 0000644 00000030016 15062121162 0010635 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Factory; use Joomla\CMS\Application\CMSApplicationInterface; use Joomla\CMS\Cache\CacheControllerFactoryAwareInterface; use Joomla\CMS\Cache\CacheControllerFactoryAwareTrait; use Joomla\CMS\Factory; use Joomla\CMS\Form\FormFactoryAwareInterface; use Joomla\CMS\Form\FormFactoryAwareTrait; use Joomla\CMS\Mail\MailerFactoryAwareInterface; use Joomla\CMS\Mail\MailerFactoryAwareTrait; use Joomla\CMS\MVC\Model\ModelInterface; use Joomla\CMS\Router\SiteRouterAwareInterface; use Joomla\CMS\Router\SiteRouterAwareTrait; use Joomla\CMS\User\UserFactoryAwareInterface; use Joomla\CMS\User\UserFactoryAwareTrait; use Joomla\Database\DatabaseAwareInterface; use Joomla\Database\DatabaseAwareTrait; use Joomla\Database\DatabaseInterface; use Joomla\Database\Exception\DatabaseNotFoundException; use Joomla\Event\DispatcherAwareInterface; use Joomla\Event\DispatcherAwareTrait; use Joomla\Input\Input; // phpcs:disable PSR1.Files.SideEffects \defined('JPATH_PLATFORM') or die; // phpcs:enable PSR1.Files.SideEffects /** * Factory to create MVC objects based on a namespace. * * @since 3.10.0 */ class MVCFactory implements MVCFactoryInterface, FormFactoryAwareInterface, SiteRouterAwareInterface, UserFactoryAwareInterface, MailerFactoryAwareInterface { use FormFactoryAwareTrait; use DispatcherAwareTrait; use DatabaseAwareTrait; use SiteRouterAwareTrait; use CacheControllerFactoryAwareTrait; use UserFactoryAwareTrait; use MailerFactoryAwareTrait; /** * The namespace to create the objects from. * * @var string * @since 4.0.0 */ private $namespace; /** * The namespace must be like: * Joomla\Component\Content * * @param string $namespace The namespace * * @since 4.0.0 */ public function __construct($namespace) { $this->namespace = $namespace; } /** * Method to load and return a controller object. * * @param string $name The name of the controller * @param string $prefix The controller prefix * @param array $config The configuration array for the controller * @param CMSApplicationInterface $app The app * @param Input $input The input * * @return \Joomla\CMS\MVC\Controller\ControllerInterface * * @since 3.10.0 * @throws \Exception */ public function createController($name, $prefix, array $config, CMSApplicationInterface $app, Input $input) { // Clean the parameters $name = preg_replace('/[^A-Z0-9_]/i', '', $name); $prefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix); $className = $this->getClassName('Controller\\' . ucfirst($name) . 'Controller', $prefix); if (!$className) { return null; } $controller = new $className($config, $this, $app, $input); $this->setFormFactoryOnObject($controller); $this->setDispatcherOnObject($controller); $this->setRouterOnObject($controller); $this->setCacheControllerOnObject($controller); $this->setUserFactoryOnObject($controller); $this->setMailerFactoryOnObject($controller); return $controller; } /** * Method to load and return a model object. * * @param string $name The name of the model. * @param string $prefix Optional model prefix. * @param array $config Optional configuration array for the model. * * @return ModelInterface The model object * * @since 3.10.0 * @throws \Exception */ public function createModel($name, $prefix = '', array $config = []) { // Clean the parameters $name = preg_replace('/[^A-Z0-9_]/i', '', $name); $prefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix); if (!$prefix) { @trigger_error( sprintf( 'Calling %s() without a prefix is deprecated.', __METHOD__ ), E_USER_DEPRECATED ); $prefix = Factory::getApplication()->getName(); } $className = $this->getClassName('Model\\' . ucfirst($name) . 'Model', $prefix); if (!$className) { return null; } $model = new $className($config, $this); $this->setFormFactoryOnObject($model); $this->setDispatcherOnObject($model); $this->setRouterOnObject($model); $this->setCacheControllerOnObject($model); $this->setUserFactoryOnObject($model); $this->setMailerFactoryOnObject($model); if ($model instanceof DatabaseAwareInterface) { try { $model->setDatabase($this->getDatabase()); } catch (DatabaseNotFoundException $e) { @trigger_error(sprintf('Database must be set, this will not be caught anymore in 5.0.'), E_USER_DEPRECATED); $model->setDatabase(Factory::getContainer()->get(DatabaseInterface::class)); } } return $model; } /** * Method to load and return a view object. * * @param string $name The name of the view. * @param string $prefix Optional view prefix. * @param string $type Optional type of view. * @param array $config Optional configuration array for the view. * * @return \Joomla\CMS\MVC\View\ViewInterface The view object * * @since 3.10.0 * @throws \Exception */ public function createView($name, $prefix = '', $type = '', array $config = []) { // Clean the parameters $name = preg_replace('/[^A-Z0-9_]/i', '', $name); $prefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix); $type = preg_replace('/[^A-Z0-9_]/i', '', $type); if (!$prefix) { @trigger_error( sprintf( 'Calling %s() without a prefix is deprecated.', __METHOD__ ), E_USER_DEPRECATED ); $prefix = Factory::getApplication()->getName(); } $className = $this->getClassName('View\\' . ucfirst($name) . '\\' . ucfirst($type) . 'View', $prefix); if (!$className) { return null; } $view = new $className($config); $this->setFormFactoryOnObject($view); $this->setDispatcherOnObject($view); $this->setRouterOnObject($view); $this->setCacheControllerOnObject($view); $this->setUserFactoryOnObject($view); return $view; } /** * Method to load and return a table object. * * @param string $name The name of the table. * @param string $prefix Optional table prefix. * @param array $config Optional configuration array for the table. * * @return \Joomla\CMS\Table\Table The table object * * @since 3.10.0 * @throws \Exception */ public function createTable($name, $prefix = '', array $config = []) { // Clean the parameters $name = preg_replace('/[^A-Z0-9_]/i', '', $name); $prefix = preg_replace('/[^A-Z0-9_]/i', '', $prefix); if (!$prefix) { @trigger_error( sprintf( 'Calling %s() without a prefix is deprecated.', __METHOD__ ), E_USER_DEPRECATED ); $prefix = Factory::getApplication()->getName(); } $className = $this->getClassName('Table\\' . ucfirst($name) . 'Table', $prefix) ?: $this->getClassName('Table\\' . ucfirst($name) . 'Table', 'Administrator'); if (!$className) { return null; } try { $db = \array_key_exists('dbo', $config) ? $config['dbo'] : $this->getDatabase(); } catch (DatabaseNotFoundException $e) { @trigger_error(sprintf('Database must be set, this will not be caught anymore in 5.0.'), E_USER_DEPRECATED); $db = Factory::getContainer()->get(DatabaseInterface::class); } $table = new $className($db); $this->setUserFactoryOnObject($table); return $table; } /** * Returns a standard classname, if the class doesn't exist null is returned. * * @param string $suffix The suffix * @param string $prefix The prefix * * @return string|null The class name * * @since 3.10.0 */ protected function getClassName(string $suffix, string $prefix) { if (!$prefix) { $prefix = Factory::getApplication(); } $className = trim($this->namespace, '\\') . '\\' . ucfirst($prefix) . '\\' . $suffix; if (!class_exists($className)) { return null; } return $className; } /** * Sets the internal form factory on the given object. * * @param object $object The object * * @return void * * @since 4.0.0 */ private function setFormFactoryOnObject($object) { if (!$object instanceof FormFactoryAwareInterface) { return; } try { $object->setFormFactory($this->getFormFactory()); } catch (\UnexpectedValueException $e) { // Ignore it } } /** * Sets the internal event dispatcher on the given object. * * @param object $object The object * * @return void * * @since 4.2.0 */ private function setDispatcherOnObject($object) { if (!$object instanceof DispatcherAwareInterface) { return; } try { $object->setDispatcher($this->getDispatcher()); } catch (\UnexpectedValueException $e) { // Ignore it } } /** * Sets the internal router on the given object. * * @param object $object The object * * @return void * * @since 4.2.0 */ private function setRouterOnObject($object): void { if (!$object instanceof SiteRouterAwareInterface) { return; } try { $object->setSiteRouter($this->getSiteRouter()); } catch (\UnexpectedValueException $e) { // Ignore it } } /** * Sets the internal cache controller on the given object. * * @param object $object The object * * @return void * * @since 4.2.0 */ private function setCacheControllerOnObject($object): void { if (!$object instanceof CacheControllerFactoryAwareInterface) { return; } try { $object->setCacheControllerFactory($this->getCacheControllerFactory()); } catch (\UnexpectedValueException $e) { // Ignore it } } /** * Sets the internal user factory on the given object. * * @param object $object The object * * @return void * * @since 4.4.0 */ private function setUserFactoryOnObject($object): void { if (!$object instanceof UserFactoryAwareInterface) { return; } try { $object->setUserFactory($this->getUserFactory()); } catch (\UnexpectedValueException $e) { // Ignore it } } /** * Sets the internal mailer factory on the given object. * * @param object $object The object * * @return void * * @since 4.4.0 */ private function setMailerFactoryOnObject($object): void { if (!$object instanceof MailerFactoryAwareInterface) { return; } try { $object->setMailerFactory($this->getMailerFactory()); } catch (\UnexpectedValueException $e) { // Ignore it } } } Factory/MVCFactoryAwareTrait.php 0000644 00000002355 15062121162 0012626 0 ustar 00 <?php /** * Joomla! Content Management System * * @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ namespace Joomla\CMS\MVC\Factory; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; // phpcs:enable PSR1.Files.SideEffects /** * MVCFactory aware trait. * * @since 4.0.0 */ trait MVCFactoryAwareTrait { /** * The mvc factory. * * @var MVCFactoryInterface * @since 4.0.0 */ private $mvcFactory; /** * Returns the MVC factory. * * @return MVCFactoryInterface * * @since 4.0.0 * @throws \UnexpectedValueException */ protected function getMVCFactory(): MVCFactoryInterface { if ($this->mvcFactory) { return $this->mvcFactory; } throw new \UnexpectedValueException('MVC Factory not set in ' . __CLASS__); } /** * Set the MVC factory. * * @param MVCFactoryInterface $mvcFactory The MVC factory * * @return void * * @since 4.0.0 */ public function setMVCFactory(MVCFactoryInterface $mvcFactory) { $this->mvcFactory = $mvcFactory; } }
| ver. 1.4 |
Github
|
.
| PHP 8.1.33 | Генерация страницы: 0 |
proxy
|
phpinfo
|
Настройка