first commit
This commit is contained in:
119
api/components/com_content/src/Controller/ArticlesController.php
Normal file
119
api/components/com_content/src/Controller/ArticlesController.php
Normal file
@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.API
|
||||
* @subpackage com_content
|
||||
*
|
||||
* @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\Content\Api\Controller;
|
||||
|
||||
use Joomla\CMS\Filter\InputFilter;
|
||||
use Joomla\CMS\MVC\Controller\ApiController;
|
||||
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* The article controller
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
class ArticlesController extends ApiController
|
||||
{
|
||||
/**
|
||||
* The content type of the item.
|
||||
*
|
||||
* @var string
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected $contentType = 'articles';
|
||||
|
||||
/**
|
||||
* The default view for the display method.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.0
|
||||
*/
|
||||
protected $default_view = 'articles';
|
||||
|
||||
/**
|
||||
* Article list view amended to add filtering of data
|
||||
*
|
||||
* @return static A BaseController object to support chaining.
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function displayList()
|
||||
{
|
||||
$apiFilterInfo = $this->input->get('filter', [], 'array');
|
||||
$filter = InputFilter::getInstance();
|
||||
|
||||
if (\array_key_exists('author', $apiFilterInfo)) {
|
||||
$this->modelState->set('filter.author_id', $filter->clean($apiFilterInfo['author'], 'INT'));
|
||||
}
|
||||
|
||||
if (\array_key_exists('category', $apiFilterInfo)) {
|
||||
$this->modelState->set('filter.category_id', $filter->clean($apiFilterInfo['category'], 'INT'));
|
||||
}
|
||||
|
||||
if (\array_key_exists('search', $apiFilterInfo)) {
|
||||
$this->modelState->set('filter.search', $filter->clean($apiFilterInfo['search'], 'STRING'));
|
||||
}
|
||||
|
||||
if (\array_key_exists('state', $apiFilterInfo)) {
|
||||
$this->modelState->set('filter.published', $filter->clean($apiFilterInfo['state'], 'INT'));
|
||||
}
|
||||
|
||||
if (\array_key_exists('featured', $apiFilterInfo)) {
|
||||
$this->modelState->set('filter.featured', $filter->clean($apiFilterInfo['featured'], 'INT'));
|
||||
}
|
||||
|
||||
if (\array_key_exists('tag', $apiFilterInfo)) {
|
||||
$this->modelState->set('filter.tag', $filter->clean($apiFilterInfo['tag'], 'INT'));
|
||||
}
|
||||
|
||||
if (\array_key_exists('language', $apiFilterInfo)) {
|
||||
$this->modelState->set('filter.language', $filter->clean($apiFilterInfo['language'], 'STRING'));
|
||||
}
|
||||
|
||||
$apiListInfo = $this->input->get('list', [], 'array');
|
||||
|
||||
if (\array_key_exists('ordering', $apiListInfo)) {
|
||||
$this->modelState->set('list.ordering', $filter->clean($apiListInfo['ordering'], 'STRING'));
|
||||
}
|
||||
|
||||
if (\array_key_exists('direction', $apiListInfo)) {
|
||||
$this->modelState->set('list.direction', $filter->clean($apiListInfo['direction'], 'STRING'));
|
||||
}
|
||||
|
||||
return parent::displayList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
foreach (FieldsHelper::getFields('com_content.article') as $field) {
|
||||
if (isset($data[$field->name])) {
|
||||
!isset($data['com_fields']) && $data['com_fields'] = [];
|
||||
|
||||
$data['com_fields'][$field->name] = $data[$field->name];
|
||||
unset($data[$field->name]);
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
42
api/components/com_content/src/Helper/ContentHelper.php
Normal file
42
api/components/com_content/src/Helper/ContentHelper.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Api
|
||||
* @subpackage com_content
|
||||
*
|
||||
* @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\Component\Content\Api\Helper;
|
||||
|
||||
use Joomla\CMS\Uri\Uri;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Content api helper.
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
class ContentHelper
|
||||
{
|
||||
/**
|
||||
* Fully Qualified Domain name for the image url
|
||||
*
|
||||
* @param string $uri The uri to resolve
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function resolve(string $uri): string
|
||||
{
|
||||
// Check if external URL.
|
||||
if (stripos($uri, 'http') !== 0) {
|
||||
return Uri::root() . $uri;
|
||||
}
|
||||
|
||||
return $uri;
|
||||
}
|
||||
}
|
||||
112
api/components/com_content/src/Serializer/ContentSerializer.php
Normal file
112
api/components/com_content/src/Serializer/ContentSerializer.php
Normal file
@ -0,0 +1,112 @@
|
||||
<?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\Component\Content\Api\Serializer;
|
||||
|
||||
use Joomla\CMS\Router\Route;
|
||||
use Joomla\CMS\Serializer\JoomlaSerializer;
|
||||
use Joomla\CMS\Uri\Uri;
|
||||
use Tobscure\JsonApi\Collection;
|
||||
use Tobscure\JsonApi\Relationship;
|
||||
use Tobscure\JsonApi\Resource;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Temporary serializer
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
class ContentSerializer extends JoomlaSerializer
|
||||
{
|
||||
/**
|
||||
* Build content relationships by associations
|
||||
*
|
||||
* @param \stdClass $model Item model
|
||||
*
|
||||
* @return Relationship
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function languageAssociations($model)
|
||||
{
|
||||
$resources = [];
|
||||
|
||||
// @todo: This can't be hardcoded in the future?
|
||||
$serializer = new JoomlaSerializer($this->type);
|
||||
|
||||
foreach ($model->associations as $association) {
|
||||
$resources[] = (new Resource($association, $serializer))
|
||||
->addLink('self', Route::link('site', Uri::root() . 'api/index.php/v1/content/articles/' . $association->id));
|
||||
}
|
||||
|
||||
$collection = new Collection($resources, $serializer);
|
||||
|
||||
return new Relationship($collection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build category relationship
|
||||
*
|
||||
* @param \stdClass $model Item model
|
||||
*
|
||||
* @return Relationship
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function category($model)
|
||||
{
|
||||
$serializer = new JoomlaSerializer('categories');
|
||||
|
||||
$resource = (new Resource($model->catid, $serializer))
|
||||
->addLink('self', Route::link('site', Uri::root() . 'api/index.php/v1/content/categories/' . $model->catid));
|
||||
|
||||
return new Relationship($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build category relationship
|
||||
*
|
||||
* @param \stdClass $model Item model
|
||||
*
|
||||
* @return Relationship
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function createdBy($model)
|
||||
{
|
||||
$serializer = new JoomlaSerializer('users');
|
||||
|
||||
$resource = (new Resource($model->created_by, $serializer))
|
||||
->addLink('self', Route::link('site', Uri::root() . 'api/index.php/v1/users/' . $model->created_by));
|
||||
|
||||
return new Relationship($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build editor relationship
|
||||
*
|
||||
* @param \stdClass $model Item model
|
||||
*
|
||||
* @return Relationship
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function modifiedBy($model)
|
||||
{
|
||||
$serializer = new JoomlaSerializer('users');
|
||||
|
||||
$resource = (new Resource($model->modified_by, $serializer))
|
||||
->addLink('self', Route::link('site', Uri::root() . 'api/index.php/v1/users/' . $model->modified_by));
|
||||
|
||||
return new Relationship($resource);
|
||||
}
|
||||
}
|
||||
250
api/components/com_content/src/View/Articles/JsonapiView.php
Normal file
250
api/components/com_content/src/View/Articles/JsonapiView.php
Normal file
@ -0,0 +1,250 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.API
|
||||
* @subpackage com_content
|
||||
*
|
||||
* @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\Content\Api\View\Articles;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Helper\TagsHelper;
|
||||
use Joomla\CMS\Language\Multilanguage;
|
||||
use Joomla\CMS\MVC\View\JsonApiView as BaseApiView;
|
||||
use Joomla\CMS\Plugin\PluginHelper;
|
||||
use Joomla\Component\Content\Api\Helper\ContentHelper;
|
||||
use Joomla\Component\Content\Api\Serializer\ContentSerializer;
|
||||
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
|
||||
use Joomla\Registry\Registry;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* The article 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',
|
||||
'typeAlias',
|
||||
'asset_id',
|
||||
'title',
|
||||
'text',
|
||||
'tags',
|
||||
'language',
|
||||
'state',
|
||||
'category',
|
||||
'images',
|
||||
'metakey',
|
||||
'metadesc',
|
||||
'metadata',
|
||||
'access',
|
||||
'featured',
|
||||
'alias',
|
||||
'note',
|
||||
'publish_up',
|
||||
'publish_down',
|
||||
'urls',
|
||||
'created',
|
||||
'created_by',
|
||||
'created_by_alias',
|
||||
'modified',
|
||||
'modified_by',
|
||||
'hits',
|
||||
'version',
|
||||
'featured_up',
|
||||
'featured_down',
|
||||
];
|
||||
|
||||
/**
|
||||
* The fields to render items in the documents
|
||||
*
|
||||
* @var array
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected $fieldsToRenderList = [
|
||||
'id',
|
||||
'typeAlias',
|
||||
'asset_id',
|
||||
'title',
|
||||
'text',
|
||||
'tags',
|
||||
'language',
|
||||
'state',
|
||||
'category',
|
||||
'images',
|
||||
'metakey',
|
||||
'metadesc',
|
||||
'metadata',
|
||||
'access',
|
||||
'featured',
|
||||
'alias',
|
||||
'note',
|
||||
'publish_up',
|
||||
'publish_down',
|
||||
'urls',
|
||||
'created',
|
||||
'created_by',
|
||||
'created_by_alias',
|
||||
'modified',
|
||||
'hits',
|
||||
'version',
|
||||
'featured_up',
|
||||
'featured_down',
|
||||
];
|
||||
|
||||
/**
|
||||
* The relationships the item has
|
||||
*
|
||||
* @var array
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected $relationship = [
|
||||
'category',
|
||||
'created_by',
|
||||
'tags',
|
||||
];
|
||||
|
||||
/**
|
||||
* 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->serializer = new ContentSerializer($config['contentType']);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
foreach (FieldsHelper::getFields('com_content.article') as $field) {
|
||||
$this->fieldsToRenderList[] = $field->name;
|
||||
}
|
||||
|
||||
return parent::displayList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute and display a template script.
|
||||
*
|
||||
* @param object $item Item
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function displayItem($item = null)
|
||||
{
|
||||
$this->relationship[] = 'modified_by';
|
||||
|
||||
foreach (FieldsHelper::getFields('com_content.article') as $field) {
|
||||
$this->fieldsToRenderItem[] = $field->name;
|
||||
}
|
||||
|
||||
if (Multilanguage::isEnabled()) {
|
||||
$this->fieldsToRenderItem[] = 'languageAssociations';
|
||||
$this->relationship[] = 'languageAssociations';
|
||||
}
|
||||
|
||||
return parent::displayItem();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare item before render.
|
||||
*
|
||||
* @param object $item The model item
|
||||
*
|
||||
* @return object
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected function prepareItem($item)
|
||||
{
|
||||
if (!$item) {
|
||||
return $item;
|
||||
}
|
||||
|
||||
$item->text = $item->introtext . ' ' . $item->fulltext;
|
||||
|
||||
// Process the content plugins.
|
||||
PluginHelper::importPlugin('content');
|
||||
Factory::getApplication()->triggerEvent('onContentPrepare', ['com_content.article', &$item, &$item->params]);
|
||||
|
||||
foreach (FieldsHelper::getFields('com_content.article', $item, true) as $field) {
|
||||
$item->{$field->name} = $field->apivalue ?? $field->rawvalue;
|
||||
}
|
||||
|
||||
if (Multilanguage::isEnabled() && !empty($item->associations)) {
|
||||
$associations = [];
|
||||
|
||||
foreach ($item->associations as $language => $association) {
|
||||
$itemId = explode(':', $association)[0];
|
||||
|
||||
$associations[] = (object) [
|
||||
'id' => $itemId,
|
||||
'language' => $language,
|
||||
];
|
||||
}
|
||||
|
||||
$item->associations = $associations;
|
||||
}
|
||||
|
||||
if (!empty($item->tags->tags)) {
|
||||
$tagsIds = explode(',', $item->tags->tags);
|
||||
$item->tags = $item->tagsHelper->getTags($tagsIds);
|
||||
} else {
|
||||
$item->tags = [];
|
||||
$tags = new TagsHelper();
|
||||
$tagsIds = $tags->getTagIds($item->id, 'com_content.article');
|
||||
|
||||
if (!empty($tagsIds)) {
|
||||
$tagsIds = explode(',', $tagsIds);
|
||||
$item->tags = $tags->getTags($tagsIds);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($item->images)) {
|
||||
$registry = new Registry($item->images);
|
||||
$item->images = $registry->toArray();
|
||||
|
||||
if (!empty($item->images['image_intro'])) {
|
||||
$item->images['image_intro'] = ContentHelper::resolve($item->images['image_intro']);
|
||||
}
|
||||
|
||||
if (!empty($item->images['image_fulltext'])) {
|
||||
$item->images['image_fulltext'] = ContentHelper::resolve($item->images['image_fulltext']);
|
||||
}
|
||||
}
|
||||
|
||||
return parent::prepareItem($item);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user