CoreFiles by mySites.guru
ItemsController.php290 lines, 8,562 bytes, from Joomla_5.4.11-Stable-Full_Package.zip
Raw History Compare with 5.4.10
md5
5fb1f16c5b184a806e67915234ef843a
sha1
4be2c21a54282e146d9626bdae856c68895da09b
sha256
cbbf13b8d42b23b8cc322db829e4e927dc5a19601daedd04eb9c7fb4f32f98a6
<?php

/**
 * @package     Joomla.API
 * @subpackage  com_menus
 *
 * @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\Component\Menus\Api\Controller;

use Joomla\CMS\Access\Exception\NotAllowed;
use Joomla\CMS\Filter\InputFilter;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\ApiController;
use Joomla\CMS\MVC\Model\ListModel;
use Joomla\Component\Menus\Api\View\Items\JsonapiView;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
 * The items controller
 *
 * @since  4.0.0
 */
class ItemsController extends ApiController
{
    /**
     * The content type of the item.
     *
     * @var    string
     * @since  4.0.0
     */
    protected $contentType = 'items';

    /**
     * The default view for the display method.
     *
     * @var    string
     * @since  3.0
     */
    protected $default_view = 'items';

    /**
     * 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)
    {
        $this->modelState->set('filter.client_id', $this->getClientIdFromInput());

        return parent::displayItem($id);
    }

    /**
     * Basic display of a list view
     *
     * @return  static  A \JControllerLegacy object to support chaining.
     *
     * @since   4.0.0
     */
    public function displayList()
    {
        $apiFilterInfo = $this->input->get('filter', [], 'array');
        $filter        = InputFilter::getInstance();

        if (\array_key_exists('menutype', $apiFilterInfo)) {
            $this->modelState->set('filter.menutype', $filter->clean($apiFilterInfo['menutype'], 'STRING'));
        }

        $this->modelState->set('filter.client_id', $this->getClientIdFromInput());

        return parent::displayList();
    }

    /**
     * Method to add a new record.
     *
     * @return  void
     *
     * @since   4.0.0
     * @throws  NotAllowed
     * @throws  \RuntimeException
     */
    public function add()
    {
        $data = $this->input->get('data', json_decode($this->input->json->getRaw(), true), 'array');

        if (isset($data['menutype'])) {
            $this->input->set('menutype', $data['menutype']);
            $this->input->set('com_menus.items.menutype', $data['menutype']);
        }

        isset($data['type']) && $this->input->set('type', $data['type']);
        isset($data['parent_id']) && $this->input->set('parent_id', $data['parent_id']);
        isset($data['link']) && $this->input->set('link', $data['link']);

        $this->input->set('id', '0');

        parent::add();
    }

    /**
     * Method to edit an existing record.
     *
     * @return  static  A \JControllerLegacy object to support chaining.
     *
     * @since   4.0.0
     */
    public function edit()
    {
        $data = $this->input->get('data', json_decode($this->input->json->getRaw(), true), 'array');

        if (isset($data['menutype'])) {
            $this->input->set('menutype', $data['menutype']);
            $this->input->set('com_menus.items.menutype', $data['menutype']);
        }

        isset($data['type']) && $this->input->set('type', $data['type']);
        isset($data['parent_id']) && $this->input->set('parent_id', $data['parent_id']);
        isset($data['link']) && $this->input->set('link', $data['link']);

        return parent::edit();
    }

    /**
     * Method to check if it's allowed to add a new menu item.
     *
     * Like in the administrator, create permission is checked on the menu type of the new item and the protected
     * administrator menu "main" can not be used.
     *
     * @param   array  $data  An array of input data.
     *
     * @return  boolean
     *
     * @since   5.4.11
     */
    protected function allowAdd($data = [])
    {
        return $this->authoriseMenuType('core.create', $this->input->getCmd('menutype', $data['menutype'] ?? ''));
    }

    /**
     * Method to check if it's allowed to edit an existing menu item.
     *
     * Like in the administrator, edit permission is checked on the menu type of the item and items of the protected
     * administrator menu "main" can not be edited. Moving an item to another menu requires create permission on
     * the target menu.
     *
     * @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   5.4.11
     */
    protected function allowEdit($data = [], $key = 'id')
    {
        $table = $this->getModel('item', '', ['ignore_request' => true])->getTable();

        if (!isset($data[$key]) || !$table->load((int) $data[$key])) {
            return false;
        }

        if (!$this->authoriseMenuType('core.edit', (string) $table->menutype)) {
            return false;
        }

        // Moving the item to another menu
        $newMenuType = $this->input->getCmd('menutype', '');

        if ($newMenuType !== '' && $newMenuType !== $table->menutype) {
            return $this->authoriseMenuType('core.create', $newMenuType);
        }

        return true;
    }

    /**
     * Method to check if it's allowed to delete a menu item.
     *
     * Like in the administrator, delete permission is checked on the menu type of the item and items of the
     * protected administrator menu "main" can not be deleted.
     *
     * @return  boolean
     *
     * @since   5.4.11
     */
    protected function allowDelete(): bool
    {
        $table = $this->getModel('item', '', ['ignore_request' => true])->getTable();

        if (!$table->load($this->input->get('id', 0, 'int'))) {
            return false;
        }

        return $this->authoriseMenuType('core.delete', (string) $table->menutype);
    }

    /**
     * Check a permission on the asset of a menu type. The protected administrator menu "main" is always denied.
     *
     * @param   string  $action    The action to check, e.g. core.edit
     * @param   string  $menuType  The menu type
     *
     * @return  boolean
     *
     * @since   5.4.11
     */
    private function authoriseMenuType(string $action, string $menuType): bool
    {
        // Protected menu type, do not allow any change
        if ($menuType === '' || $menuType === 'main') {
            return false;
        }

        $menuTypeTable = $this->getModel('item', '', ['ignore_request' => true])->getTable('MenuType');
        $menuTypeTable->load(['menutype' => $menuType]);

        if (!$menuTypeTable->id) {
            return false;
        }

        return $this->app->getIdentity()->authorise($action, 'com_menus.menu.' . (int) $menuTypeTable->id);
    }

    /**
     * Return menu items types
     *
     * @return  static  A \JControllerLegacy object to support chaining.
     *
     * @since   4.0.0
     */
    public function getTypes()
    {
        $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());
        }

        /** @var ListModel $model */
        $model = $this->getModel('menutypes', '', ['ignore_request' => true]);

        if (!$model) {
            throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'));
        }

        $model->setState('client_id', $this->getClientIdFromInput());

        $view->setModel($model, true);

        $view->document = $this->app->getDocument();

        $view->displayListTypes();

        return $this;
    }

    /**
     * Get client id from input
     *
     * @return string
     *
     * @since 4.0.0
     */
    private function getClientIdFromInput()
    {
        return $this->input->exists('client_id') ?
            $this->input->get('client_id') : $this->input->post->get('client_id');
    }
}

No other release has this exact content. See every version of this file.