primo commit
This commit is contained in:
189
api/components/com_menus/src/Controller/ItemsController.php
Normal file
189
api/components/com_menus/src/Controller/ItemsController.php
Normal file
@ -0,0 +1,189 @@
|
||||
<?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();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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');
|
||||
}
|
||||
}
|
||||
84
api/components/com_menus/src/Controller/MenusController.php
Normal file
84
api/components/com_menus/src/Controller/MenusController.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?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\MVC\Controller\ApiController;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* The menus controller
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
class MenusController extends ApiController
|
||||
{
|
||||
/**
|
||||
* The content type of the item.
|
||||
*
|
||||
* @var string
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected $contentType = 'menus';
|
||||
|
||||
/**
|
||||
* The default view for the display method.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.0
|
||||
*/
|
||||
protected $default_view = 'menus';
|
||||
|
||||
/**
|
||||
* 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()
|
||||
{
|
||||
$this->modelState->set('filter.client_id', $this->getClientIdFromInput());
|
||||
|
||||
return parent::displayList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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');
|
||||
}
|
||||
}
|
||||
209
api/components/com_menus/src/View/Items/JsonapiView.php
Normal file
209
api/components/com_menus/src/View/Items/JsonapiView.php
Normal file
@ -0,0 +1,209 @@
|
||||
<?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\View\Items;
|
||||
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\CMS\MVC\View\JsonApiView as BaseApiView;
|
||||
use Joomla\CMS\Serializer\JoomlaSerializer;
|
||||
use Joomla\CMS\Uri\Uri;
|
||||
use Tobscure\JsonApi\Collection;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* The items view
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
class JsonapiView extends BaseApiView
|
||||
{
|
||||
/**
|
||||
* The fields to render item in the documents
|
||||
*
|
||||
* @var array
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected $fieldsToRenderItem = [
|
||||
'id',
|
||||
'parent_id',
|
||||
'level',
|
||||
'lft',
|
||||
'rgt',
|
||||
'alias',
|
||||
'typeAlias',
|
||||
'menutype',
|
||||
'title',
|
||||
'note',
|
||||
'path',
|
||||
'link',
|
||||
'type',
|
||||
'published',
|
||||
'component_id',
|
||||
'checked_out',
|
||||
'checked_out_time',
|
||||
'browserNav',
|
||||
'access',
|
||||
'img',
|
||||
'template_style_id',
|
||||
'params',
|
||||
'home',
|
||||
'language',
|
||||
'client_id',
|
||||
'publish_up',
|
||||
'publish_down',
|
||||
'request',
|
||||
'associations',
|
||||
'menuordering',
|
||||
];
|
||||
|
||||
/**
|
||||
* The fields to render items in the documents
|
||||
*
|
||||
* @var array
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected $fieldsToRenderList = [
|
||||
'id',
|
||||
'menutype',
|
||||
'title',
|
||||
'alias',
|
||||
'note',
|
||||
'path',
|
||||
'link',
|
||||
'type',
|
||||
'parent_id',
|
||||
'level',
|
||||
'a.published',
|
||||
'component_id',
|
||||
'checked_out',
|
||||
'checked_out_time',
|
||||
'browserNav',
|
||||
'access',
|
||||
'img',
|
||||
'template_style_id',
|
||||
'params',
|
||||
'lft',
|
||||
'rgt',
|
||||
'home',
|
||||
'language',
|
||||
'client_id',
|
||||
'enabled',
|
||||
'publish_up',
|
||||
'publish_down',
|
||||
'published',
|
||||
'language_title',
|
||||
'language_image',
|
||||
'language_sef',
|
||||
'editor',
|
||||
'componentname',
|
||||
'access_level',
|
||||
'menutype_id',
|
||||
'menutype_title',
|
||||
'association',
|
||||
'name',
|
||||
];
|
||||
|
||||
/**
|
||||
* Execute and display a list items types.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function displayListTypes()
|
||||
{
|
||||
/** @var \Joomla\Component\Menus\Administrator\Model\MenutypesModel $model */
|
||||
$model = $this->getModel();
|
||||
$items = [];
|
||||
|
||||
foreach ($model->getTypeOptions() as $type => $data) {
|
||||
$groupItems = [];
|
||||
|
||||
foreach ($data as $item) {
|
||||
$item->id = implode('/', $item->request);
|
||||
$item->title = Text::_($item->title);
|
||||
$item->description = Text::_($item->description);
|
||||
$item->group = Text::_($type);
|
||||
|
||||
$groupItems[] = $item;
|
||||
}
|
||||
|
||||
$items = array_merge($items, $groupItems);
|
||||
}
|
||||
|
||||
// Set up links for pagination
|
||||
$currentUrl = Uri::getInstance();
|
||||
$currentPageDefaultInformation = ['offset' => 0, 'limit' => 20];
|
||||
$currentPageQuery = $currentUrl->getVar('page', $currentPageDefaultInformation);
|
||||
|
||||
$offset = $currentPageQuery['offset'];
|
||||
$limit = $currentPageQuery['limit'];
|
||||
$totalItemsCount = \count($items);
|
||||
$totalPagesAvailable = ceil($totalItemsCount / $limit);
|
||||
|
||||
$items = array_splice($items, $offset, $limit);
|
||||
|
||||
$firstPage = clone $currentUrl;
|
||||
$firstPageQuery = $currentPageQuery;
|
||||
$firstPageQuery['offset'] = 0;
|
||||
$firstPage->setVar('page', $firstPageQuery);
|
||||
|
||||
$nextPage = clone $currentUrl;
|
||||
$nextPageQuery = $currentPageQuery;
|
||||
$nextOffset = $currentPageQuery['offset'] + $limit;
|
||||
$nextPageQuery['offset'] = ($nextOffset > ($totalPagesAvailable * $limit)) ? $totalPagesAvailable - $limit : $nextOffset;
|
||||
$nextPage->setVar('page', $nextPageQuery);
|
||||
|
||||
$previousPage = clone $currentUrl;
|
||||
$previousPageQuery = $currentPageQuery;
|
||||
$previousOffset = $currentPageQuery['offset'] - $limit;
|
||||
$previousPageQuery['offset'] = max($previousOffset, 0);
|
||||
$previousPage->setVar('page', $previousPageQuery);
|
||||
|
||||
$lastPage = clone $currentUrl;
|
||||
$lastPageQuery = $currentPageQuery;
|
||||
$lastPageQuery['offset'] = $totalPagesAvailable - $limit;
|
||||
$lastPage->setVar('page', $lastPageQuery);
|
||||
|
||||
$collection = (new Collection($items, new JoomlaSerializer('menutypes')));
|
||||
|
||||
// Set the data into the document and render it
|
||||
$this->getDocument()->addMeta('total-pages', $totalPagesAvailable)
|
||||
->setData($collection)
|
||||
->addLink('self', (string) $currentUrl)
|
||||
->addLink('first', (string) $firstPage)
|
||||
->addLink('next', (string) $nextPage)
|
||||
->addLink('previous', (string) $previousPage)
|
||||
->addLink('last', (string) $lastPage);
|
||||
|
||||
return $this->getDocument()->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare item before render.
|
||||
*
|
||||
* @param object $item The model item
|
||||
*
|
||||
* @return object
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected function prepareItem($item)
|
||||
{
|
||||
if (\is_string($item->params)) {
|
||||
$item->params = json_decode($item->params);
|
||||
}
|
||||
|
||||
return parent::prepareItem($item);
|
||||
}
|
||||
}
|
||||
57
api/components/com_menus/src/View/Menus/JsonapiView.php
Normal file
57
api/components/com_menus/src/View/Menus/JsonapiView.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?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\View\Menus;
|
||||
|
||||
use Joomla\CMS\MVC\View\JsonApiView as BaseApiView;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* The menus view
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
class JsonapiView extends BaseApiView
|
||||
{
|
||||
/**
|
||||
* The fields to render item in the documents
|
||||
*
|
||||
* @var array
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected $fieldsToRenderItem = [
|
||||
'id',
|
||||
'menutype',
|
||||
'title',
|
||||
'description',
|
||||
'client_id',
|
||||
'count_published',
|
||||
'count_unpublished',
|
||||
'count_trashed',
|
||||
];
|
||||
|
||||
/**
|
||||
* The fields to render items in the documents
|
||||
*
|
||||
* @var array
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected $fieldsToRenderList = [
|
||||
'id',
|
||||
'asset_id',
|
||||
'menutype',
|
||||
'title',
|
||||
'description',
|
||||
'client_id',
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user