webservices for com_weblinks

This commit is contained in:
a-samoylov 2019-08-21 01:08:04 +03:00
parent 2be3dec52a
commit 5a781b487c
8 changed files with 338 additions and 0 deletions

View File

@ -63,5 +63,10 @@
##BACKEND_LANGUAGE_FILES##
</languages>
</administration>
<api>
<files folder="api/components/com_weblinks">
##API_COMPONENT_FILES##
</files>
</api>
</extension>

View File

@ -22,6 +22,7 @@
<file type="plugin" id="weblinks" group="search">plg_search_weblinks.zip</file>
<file type="plugin" id="weblinks" group="system">plg_system_weblinks.zip</file>
<file type="plugin" id="weblinks" group="editors-xtd">plg_editors-xtd_weblink.zip</file>
<file type="plugin" id="weblinks" group="webservices">plg_webservices.zip</file>
</files>
<languages folder="language">
<language tag="en-GB">en-GB/en-GB.pkg_weblinks.sys.ini</language>

View File

@ -0,0 +1,78 @@
<?php
/**
* @package Joomla.API
* @subpackage com_weblinks
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Weblinks\Api\Controller;
defined('_JEXEC') or die;
use Joomla\CMS\Access\Exception\NotAllowed;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Form\Form;
use Joomla\CMS\MVC\Controller\ApiController;
use Joomla\CMS\MVC\Factory\LegacyFactory;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\MVC\View\JsonApiView;
use Joomla\CMS\MVC\Controller\Exception\ResourceNotFound;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
use Joomla\Input\Input;
use Joomla\String\Inflector;
use Tobscure\JsonApi\Exception\InvalidParameterException;
/**
* The weblinks controller
*
* @since 4.0.0
*/
class WeblinksController extends ApiController
{
/**
* The content type of the item.
*
* @var string
* @since 4.0.0
*/
protected $contentType = 'weblinks';
/**
* The default view for the display method.
*
* @var string
* @since 3.0
*/
protected $default_view = 'weblinks';
/**
* Method to save a record.
*
* @param integer $recordKey The primary key of the item (if exists)
*
* @return integer The record ID on success, false on failure
*
* @since 4.0.0
*/
protected function save($recordKey = null)
{
$data = (array) json_decode($this->input->json->getRaw(), true);
foreach (FieldsHelper::getFields('com_weblinks.weblink') 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]);
}
}
$this->input->set('data', $data);
return parent::save($recordKey);
}
}

View File

@ -0,0 +1,147 @@
<?php
/**
* @package Joomla.API
* @subpackage com_redirect
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Weblinks\Api\View\Weblinks;
defined('_JEXEC') or die;
use Joomla\CMS\MVC\View\JsonApiView as BaseApiView;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
/**
* The weblinks 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',
'catid',
'title',
'alias',
'url',
'description',
'hits',
'state',
'checked_out',
'checked_out_time',
'ordering',
'access',
'params',
'language',
'created',
'created_by',
'created_by_alias',
'modified',
'modified_by',
'metakey',
'metadesc',
'metadata',
'featured',
'xreference',
'publish_up',
'publish_down',
'version',
'images',
'tags'
];
/**
* The fields to render items in the documents
*
* @var array
* @since 4.0.0
*/
protected $fieldsToRenderList = [
'id',
'title',
'alias',
'checked_out',
'checked_out_time',
'catid',
'created',
'created_by',
'hits',
'state',
'access',
'ordering',
'language',
'publish_up',
'publish_down',
'language_title',
'language_image',
'editor',
'access_level',
'category_title'
];
/**
* 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_weblinks.weblink') 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)
{
foreach (FieldsHelper::getFields('com_weblinks.weblink') as $field)
{
$this->fieldsToRenderItem[] = $field->name;
}
return parent::displayItem();
}
/**
* Prepare item before render.
*
* @param object $item The model item
*
* @return object
*
* @since 4.0.0
*/
protected function prepareItem($item)
{
foreach (FieldsHelper::getFields('com_weblinks.weblink', $item, true) as $field)
{
$item->{$field->name} = isset($field->apivalue) ? $field->apivalue : $field->rawvalue;
}
return parent::prepareItem($item);
}
}

View File

@ -0,0 +1,7 @@
; Joomla! Project
; Copyright (C) 2005 - 2019 Open Source Matters. All rights reserved.
; License GNU General Public License version 2 or later; see LICENSE.txt, see LICENSE.php
; Note : All ini files need to be saved as UTF-8
PLG_WEBSERVICES_WEBLINKS="Web Services - Weblinks"
PLG_WEBSERVICES_WEBLINKS_XML_DESCRIPTION="Used to add weblinks routes to the API for your website."

View File

@ -0,0 +1,7 @@
; Joomla! Project
; Copyright (C) 2005 - 2019 Open Source Matters. All rights reserved.
; License GNU General Public License version 2 or later; see LICENSE.txt, see LICENSE.php
; Note : All ini files need to be saved as UTF-8
PLG_WEBSERVICES_WEBLINKS="Web Services - Weblinks"
PLG_WEBSERVICES_WEBLINKS_XML_DESCRIPTION="Used to add weblinks routes to the API for your website."

View File

@ -0,0 +1,75 @@
<?php
/**
* @package Joomla.Plugin
* @subpackage Webservices.Weblinks
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Router\ApiRouter;
/**
* Web Services adapter for com_weblinks.
*
* @since 4.0.0
*/
class PlgWebservicesWeblinks extends CMSPlugin
{
/**
* Load the language file on instantiation.
*
* @var boolean
* @since 4.0.0
*/
protected $autoloadLanguage = true;
/**
* Registers com_weblinks's API's routes in the application
*
* @param ApiRouter &$router The API Routing object
*
* @return void
*
* @since 4.0.0
*/
public function onBeforeApiRoute(&$router)
{
$router->createCRUDRoutes('v1/weblinks', 'weblinks', ['component' => 'com_weblinks']);
$router->createCRUDRoutes(
'v1/weblinks/categories',
'categories',
['component' => 'com_categories', 'extension' => 'com_weblinks']
);
$this->createFieldsRoutes($router);
}
/**
* Create fields routes
*
* @param ApiRouter &$router The API Routing object
*
* @return void
*
* @since 4.0.0
*/
private function createFieldsRoutes(&$router)
{
$router->createCRUDRoutes(
'v1/fields/weblinks',
'fields',
['component' => 'com_fields', 'context' => 'com_weblinks.weblink']
);
$router->createCRUDRoutes(
'v1/fields/groups/weblinks',
'groups',
['component' => 'com_fields', 'context' => 'com_weblinks.weblink']
);
}
}

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<extension version="3.1" type="plugin" group="webservices" method="upgrade">
<name>plg_webservices_weblinks</name>
<author>Joomla! Project</author>
<creationDate>August 2017</creationDate>
<copyright>(C) 2005 - 2019 Open Source Matters. All rights reserved.</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<authorEmail>admin@joomla.org</authorEmail>
<authorUrl>www.joomla.org</authorUrl>
<version>4.0.0</version>
<description>PLG_WEBSERVICES_WEBLINKS_XML_DESCRIPTION</description>
<files>
##FILES##
</files>
<languages folder="language">
##LANGUAGE_FILES##
</languages>
</extension>