first commit
This commit is contained in:
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.API
|
||||
* @subpackage com_newsfeeds
|
||||
*
|
||||
* @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\Newsfeeds\Api\Controller;
|
||||
|
||||
use Joomla\CMS\MVC\Controller\ApiController;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* The feeds controller
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
class FeedsController extends ApiController
|
||||
{
|
||||
/**
|
||||
* The content type of the item.
|
||||
*
|
||||
* @var string
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected $contentType = 'newsfeeds';
|
||||
|
||||
/**
|
||||
* The default view for the display method.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.0
|
||||
*/
|
||||
protected $default_view = 'feeds';
|
||||
}
|
||||
@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Joomla! Content Management System
|
||||
*
|
||||
* @copyright (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Component\Newsfeeds\Api\Serializer;
|
||||
|
||||
use Joomla\CMS\Router\Route;
|
||||
use Joomla\CMS\Serializer\JoomlaSerializer;
|
||||
use Joomla\CMS\Tag\TagApiSerializerTrait;
|
||||
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 NewsfeedSerializer extends JoomlaSerializer
|
||||
{
|
||||
use TagApiSerializerTrait;
|
||||
|
||||
/**
|
||||
* 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/newsfeeds/feeds/' . $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/newfeeds/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);
|
||||
}
|
||||
}
|
||||
182
api/components/com_newsfeeds/src/View/Feeds/JsonapiView.php
Normal file
182
api/components/com_newsfeeds/src/View/Feeds/JsonapiView.php
Normal file
@ -0,0 +1,182 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.API
|
||||
* @subpackage com_newsfeeds
|
||||
*
|
||||
* @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\Newsfeeds\Api\View\Feeds;
|
||||
|
||||
use Joomla\CMS\Language\Multilanguage;
|
||||
use Joomla\CMS\MVC\View\JsonApiView as BaseApiView;
|
||||
use Joomla\Component\Newsfeeds\Api\Serializer\NewsfeedSerializer;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* The feeds 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',
|
||||
'category',
|
||||
'name',
|
||||
'alias',
|
||||
'link',
|
||||
'published',
|
||||
'numarticles',
|
||||
'cache_time',
|
||||
'checked_out',
|
||||
'checked_out_time',
|
||||
'ordering',
|
||||
'rtl',
|
||||
'access',
|
||||
'language',
|
||||
'params',
|
||||
'created',
|
||||
'created_by',
|
||||
'created_by_alias',
|
||||
'modified',
|
||||
'modified_by',
|
||||
'metakey',
|
||||
'metadesc',
|
||||
'metadata',
|
||||
'publish_up',
|
||||
'publish_down',
|
||||
'description',
|
||||
'version',
|
||||
'hits',
|
||||
'images',
|
||||
'tags',
|
||||
];
|
||||
|
||||
/**
|
||||
* The fields to render items in the documents
|
||||
*
|
||||
* @var array
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected $fieldsToRenderList = [
|
||||
'id',
|
||||
'name',
|
||||
'alias',
|
||||
'checked_out',
|
||||
'checked_out_time',
|
||||
'category',
|
||||
'numarticles',
|
||||
'cache_time',
|
||||
'created_by',
|
||||
'published',
|
||||
'access',
|
||||
'ordering',
|
||||
'language',
|
||||
'publish_up',
|
||||
'publish_down',
|
||||
'language_title',
|
||||
'language_image',
|
||||
'editor',
|
||||
'access_level',
|
||||
'category_title',
|
||||
];
|
||||
|
||||
/**
|
||||
* The relationships the item has
|
||||
*
|
||||
* @var array
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected $relationship = [
|
||||
'category',
|
||||
'created_by',
|
||||
'modified_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 NewsfeedSerializer($config['contentType']);
|
||||
}
|
||||
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute and display a template script.
|
||||
*
|
||||
* @param object $item Item
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function displayItem($item = null)
|
||||
{
|
||||
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 (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);
|
||||
$tagsNames = $item->tagsHelper->getTagNames($tagsIds);
|
||||
|
||||
$item->tags = array_combine($tagsIds, $tagsNames);
|
||||
} else {
|
||||
$item->tags = [];
|
||||
}
|
||||
|
||||
return parent::prepareItem($item);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user