primo commit
This commit is contained in:
@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.API
|
||||
* @subpackage com_config
|
||||
*
|
||||
* @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\Config\Api\Controller;
|
||||
|
||||
use Joomla\CMS\Access\Exception\NotAllowed;
|
||||
use Joomla\CMS\Form\Form;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\CMS\MVC\Controller\ApiController;
|
||||
use Joomla\Component\Config\Administrator\Model\ApplicationModel;
|
||||
use Joomla\Component\Config\Api\View\Application\JsonapiView;
|
||||
use Tobscure\JsonApi\Exception\InvalidParameterException;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* The application controller
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
class ApplicationController extends ApiController
|
||||
{
|
||||
/**
|
||||
* The content type of the item.
|
||||
*
|
||||
* @var string
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected $contentType = 'application';
|
||||
|
||||
/**
|
||||
* The default view for the display method.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.0
|
||||
*/
|
||||
protected $default_view = 'application';
|
||||
|
||||
/**
|
||||
* Basic display of a list view
|
||||
*
|
||||
* @return static A \JControllerLegacy object to support chaining.
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function displayList()
|
||||
{
|
||||
$viewType = $this->app->getDocument()->getType();
|
||||
$viewLayout = $this->input->get('layout', 'default', 'string');
|
||||
|
||||
try {
|
||||
/** @var JsonapiView $view */
|
||||
$view = $this->getView(
|
||||
$this->default_view,
|
||||
$viewType,
|
||||
'',
|
||||
['base_path' => $this->basePath, 'layout' => $viewLayout, 'contentType' => $this->contentType]
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
throw new \RuntimeException($e->getMessage());
|
||||
}
|
||||
|
||||
/** @var ApplicationModel $model */
|
||||
$model = $this->getModel($this->contentType);
|
||||
|
||||
if (!$model) {
|
||||
throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'), 500);
|
||||
}
|
||||
|
||||
// Push the model into the view (as default)
|
||||
$view->setModel($model, true);
|
||||
|
||||
$view->document = $this->app->getDocument();
|
||||
$view->displayList();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to edit an existing record.
|
||||
*
|
||||
* @return static A \JControllerLegacy object to support chaining.
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function edit()
|
||||
{
|
||||
/** @var ApplicationModel $model */
|
||||
$model = $this->getModel($this->contentType);
|
||||
|
||||
if (!$model) {
|
||||
throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'), 500);
|
||||
}
|
||||
|
||||
// Access check.
|
||||
if (!$this->allowEdit()) {
|
||||
throw new NotAllowed('JLIB_APPLICATION_ERROR_CREATE_RECORD_NOT_PERMITTED', 403);
|
||||
}
|
||||
|
||||
$data = json_decode($this->input->json->getRaw(), true);
|
||||
|
||||
// Complete data array if needed
|
||||
$oldData = $model->getData();
|
||||
$data = array_replace($oldData, $data);
|
||||
|
||||
// @todo: Not the cleanest thing ever but it works...
|
||||
Form::addFormPath(JPATH_COMPONENT_ADMINISTRATOR . '/forms');
|
||||
|
||||
// Must load after serving service-requests
|
||||
$form = $model->getForm();
|
||||
|
||||
// Validate the posted data.
|
||||
$validData = $model->validate($form, $data);
|
||||
|
||||
// Check for validation errors.
|
||||
if ($validData === false) {
|
||||
$errors = $model->getErrors();
|
||||
$messages = [];
|
||||
|
||||
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 (!$model->save($validData)) {
|
||||
throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_SERVER'), 500);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
157
api/components/com_config/src/Controller/ComponentController.php
Normal file
157
api/components/com_config/src/Controller/ComponentController.php
Normal file
@ -0,0 +1,157 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.API
|
||||
* @subpackage com_config
|
||||
*
|
||||
* @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\Config\Api\Controller;
|
||||
|
||||
use Joomla\CMS\Access\Exception\NotAllowed;
|
||||
use Joomla\CMS\Component\ComponentHelper;
|
||||
use Joomla\CMS\Extension\ExtensionHelper;
|
||||
use Joomla\CMS\Form\Form;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\CMS\MVC\Controller\ApiController;
|
||||
use Joomla\Component\Config\Administrator\Model\ComponentModel;
|
||||
use Joomla\Component\Config\Api\View\Component\JsonapiView;
|
||||
use Tobscure\JsonApi\Exception\InvalidParameterException;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* The component controller
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
class ComponentController extends ApiController
|
||||
{
|
||||
/**
|
||||
* The content type of the item.
|
||||
*
|
||||
* @var string
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected $contentType = 'component';
|
||||
|
||||
/**
|
||||
* The default view for the display method.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.0
|
||||
*/
|
||||
protected $default_view = 'component';
|
||||
|
||||
/**
|
||||
* Basic display of a list view
|
||||
*
|
||||
* @return static A \JControllerLegacy object to support chaining.
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function displayList()
|
||||
{
|
||||
$viewType = $this->app->getDocument()->getType();
|
||||
$viewLayout = $this->input->get('layout', 'default', 'string');
|
||||
|
||||
try {
|
||||
/** @var JsonapiView $view */
|
||||
$view = $this->getView(
|
||||
$this->default_view,
|
||||
$viewType,
|
||||
'',
|
||||
['base_path' => $this->basePath, 'layout' => $viewLayout, 'contentType' => $this->contentType]
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
throw new \RuntimeException($e->getMessage());
|
||||
}
|
||||
|
||||
/** @var ComponentModel $model */
|
||||
$model = $this->getModel($this->contentType);
|
||||
|
||||
if (!$model) {
|
||||
throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'), 500);
|
||||
}
|
||||
|
||||
// Push the model into the view (as default)
|
||||
$view->setModel($model, true);
|
||||
$view->set('component_name', $this->input->get('component_name'));
|
||||
|
||||
$view->document = $this->app->getDocument();
|
||||
$view->displayList();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to edit an existing record.
|
||||
*
|
||||
* @return static A \JControllerLegacy object to support chaining.
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function edit()
|
||||
{
|
||||
/** @var ComponentModel $model */
|
||||
$model = $this->getModel($this->contentType);
|
||||
|
||||
if (!$model) {
|
||||
throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'), 500);
|
||||
}
|
||||
|
||||
// Access check.
|
||||
if (!$this->allowEdit()) {
|
||||
throw new NotAllowed('JLIB_APPLICATION_ERROR_CREATE_RECORD_NOT_PERMITTED', 403);
|
||||
}
|
||||
|
||||
$option = $this->input->get('component_name');
|
||||
|
||||
// @todo: Not the cleanest thing ever but it works...
|
||||
Form::addFormPath(JPATH_ADMINISTRATOR . '/components/' . $option);
|
||||
|
||||
// Must load after serving service-requests
|
||||
$form = $model->getForm();
|
||||
|
||||
$data = json_decode($this->input->json->getRaw(), true);
|
||||
|
||||
$component = ComponentHelper::getComponent($option);
|
||||
$oldData = $component->getParams()->toArray();
|
||||
$data = array_replace($oldData, $data);
|
||||
|
||||
// Validate the posted data.
|
||||
$validData = $model->validate($form, $data);
|
||||
|
||||
if ($validData === false) {
|
||||
$errors = $model->getErrors();
|
||||
$messages = [];
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
// Attempt to save the configuration.
|
||||
$data = [
|
||||
'params' => $validData,
|
||||
'id' => ExtensionHelper::getExtensionRecord($option, 'component')->extension_id,
|
||||
'option' => $option,
|
||||
];
|
||||
|
||||
if (!$model->save($data)) {
|
||||
throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_SERVER'), 500);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
123
api/components/com_config/src/View/Application/JsonapiView.php
Normal file
123
api/components/com_config/src/View/Application/JsonapiView.php
Normal file
@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.API
|
||||
* @subpackage com_config
|
||||
*
|
||||
* @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\Config\Api\View\Application;
|
||||
|
||||
use Joomla\CMS\Extension\ExtensionHelper;
|
||||
use Joomla\CMS\MVC\View\JsonApiView as BaseApiView;
|
||||
use Joomla\CMS\Serializer\JoomlaSerializer;
|
||||
use Joomla\CMS\Uri\Uri;
|
||||
use Joomla\Component\Config\Administrator\Model\ApplicationModel;
|
||||
use Tobscure\JsonApi\Collection;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* The application view
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
class JsonapiView extends BaseApiView
|
||||
{
|
||||
/**
|
||||
* Execute and display a template script.
|
||||
*
|
||||
* @param ?array $items Array of items
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function displayList(?array $items = null)
|
||||
{
|
||||
/** @var ApplicationModel $model */
|
||||
$model = $this->getModel();
|
||||
$items = [];
|
||||
|
||||
foreach ($model->getData() as $key => $value) {
|
||||
$item = (object) [$key => $value];
|
||||
$items[] = $this->prepareItem($item);
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
$this->getDocument()->addMeta('total-pages', $totalPagesAvailable)
|
||||
->addLink('self', (string) $currentUrl);
|
||||
|
||||
// Check for first and previous pages
|
||||
if ($offset > 0) {
|
||||
$firstPage = clone $currentUrl;
|
||||
$firstPageQuery = $currentPageQuery;
|
||||
$firstPageQuery['offset'] = 0;
|
||||
$firstPage->setVar('page', $firstPageQuery);
|
||||
|
||||
$previousPage = clone $currentUrl;
|
||||
$previousPageQuery = $currentPageQuery;
|
||||
$previousOffset = $currentPageQuery['offset'] - $limit;
|
||||
$previousPageQuery['offset'] = max($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 ($offset + $limit < $totalItemsCount) {
|
||||
$nextPage = clone $currentUrl;
|
||||
$nextPageQuery = $currentPageQuery;
|
||||
$nextOffset = $currentPageQuery['offset'] + $limit;
|
||||
$nextPageQuery['offset'] = ($nextOffset > ($totalPagesAvailable * $limit)) ? $totalPagesAvailable - $limit : $nextOffset;
|
||||
$nextPage->setVar('page', $nextPageQuery);
|
||||
|
||||
$lastPage = clone $currentUrl;
|
||||
$lastPageQuery = $currentPageQuery;
|
||||
$lastPageQuery['offset'] = ($totalPagesAvailable - 1) * $limit;
|
||||
$lastPage->setVar('page', $lastPageQuery);
|
||||
|
||||
$this->getDocument()->addLink('next', $this->queryEncode((string) $nextPage))
|
||||
->addLink('last', $this->queryEncode((string) $lastPage));
|
||||
}
|
||||
|
||||
$collection = (new Collection($items, new JoomlaSerializer($this->type)));
|
||||
|
||||
// Set the data into the document and render it
|
||||
$this->getDocument()->setData($collection);
|
||||
|
||||
return $this->getDocument()->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare item before render.
|
||||
*
|
||||
* @param object $item The model item
|
||||
*
|
||||
* @return object
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected function prepareItem($item)
|
||||
{
|
||||
$item->id = ExtensionHelper::getExtensionRecord('joomla', 'file')->extension_id;
|
||||
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
135
api/components/com_config/src/View/Component/JsonapiView.php
Normal file
135
api/components/com_config/src/View/Component/JsonapiView.php
Normal file
@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.API
|
||||
* @subpackage com_config
|
||||
*
|
||||
* @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\Config\Api\View\Component;
|
||||
|
||||
use Joomla\CMS\Component\ComponentHelper;
|
||||
use Joomla\CMS\Extension\ExtensionHelper;
|
||||
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 component view
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
class JsonapiView extends BaseApiView
|
||||
{
|
||||
/**
|
||||
* Execute and display a template script.
|
||||
*
|
||||
* @param ?array $items Array of items
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function displayList(?array $items = null)
|
||||
{
|
||||
try {
|
||||
$component = ComponentHelper::getComponent($this->get('component_name'));
|
||||
|
||||
if ($component === null || !$component->enabled) {
|
||||
// @todo: exception component unavailable
|
||||
throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_INVALID_COMPONENT_NAME'), 400);
|
||||
}
|
||||
|
||||
$data = $component->getParams()->toObject();
|
||||
} catch (\Exception $e) {
|
||||
throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_SERVER'), 500, $e);
|
||||
}
|
||||
|
||||
$items = [];
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
$item = (object) [$key => $value];
|
||||
$items[] = $this->prepareItem($item);
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
$this->getDocument()->addMeta('total-pages', $totalPagesAvailable)
|
||||
->addLink('self', (string) $currentUrl);
|
||||
|
||||
// Check for first and previous pages
|
||||
if ($offset > 0) {
|
||||
$firstPage = clone $currentUrl;
|
||||
$firstPageQuery = $currentPageQuery;
|
||||
$firstPageQuery['offset'] = 0;
|
||||
$firstPage->setVar('page', $firstPageQuery);
|
||||
|
||||
$previousPage = clone $currentUrl;
|
||||
$previousPageQuery = $currentPageQuery;
|
||||
$previousOffset = $currentPageQuery['offset'] - $limit;
|
||||
$previousPageQuery['offset'] = max($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 ($offset + $limit < $totalItemsCount) {
|
||||
$nextPage = clone $currentUrl;
|
||||
$nextPageQuery = $currentPageQuery;
|
||||
$nextOffset = $currentPageQuery['offset'] + $limit;
|
||||
$nextPageQuery['offset'] = ($nextOffset > ($totalPagesAvailable * $limit)) ? $totalPagesAvailable - $limit : $nextOffset;
|
||||
$nextPage->setVar('page', $nextPageQuery);
|
||||
|
||||
$lastPage = clone $currentUrl;
|
||||
$lastPageQuery = $currentPageQuery;
|
||||
$lastPageQuery['offset'] = ($totalPagesAvailable - 1) * $limit;
|
||||
$lastPage->setVar('page', $lastPageQuery);
|
||||
|
||||
$this->getDocument()->addLink('next', $this->queryEncode((string) $nextPage))
|
||||
->addLink('last', $this->queryEncode((string) $lastPage));
|
||||
}
|
||||
|
||||
$collection = (new Collection($items, new JoomlaSerializer($this->type)));
|
||||
|
||||
// Set the data into the document and render it
|
||||
$this->getDocument()->setData($collection);
|
||||
|
||||
return $this->getDocument()->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare item before render.
|
||||
*
|
||||
* @param object $item The model item
|
||||
*
|
||||
* @return object
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected function prepareItem($item)
|
||||
{
|
||||
$item->id = ExtensionHelper::getExtensionRecord($this->get('component_name'), 'component')->extension_id;
|
||||
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user