Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webservices #407

Open
wants to merge 4 commits into
base: 4.0-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/administrator/components/com_weblinks/weblinks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,10 @@
##BACKEND_LANGUAGE_FILES##
</languages>
</administration>
<api>
<files folder="api/components/com_weblinks">
##API_COMPONENT_FILES##
</files>
</api>
</extension>

1 change: 1 addition & 0 deletions src/administrator/manifests/packages/pkg_weblinks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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_weblinks.zip</file>
</files>
<languages folder="language">
<language tag="en-GB">en-GB/en-GB.pkg_weblinks.sys.ini</language>
Expand Down
78 changes: 78 additions & 0 deletions src/api/components/com_weblinks/Controller/WeblinksController.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
147 changes: 147 additions & 0 deletions src/api/components/com_weblinks/View/Weblinks/JsonapiView.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
; Joomla! Project
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the file name should be src/plugins/webservices/weblinks/en-GB/en-GB.plg_webservices_weblinks.ini not .sys at the end ;)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, thanks :)

; 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."
Original file line number Diff line number Diff line change
@@ -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."
75 changes: 75 additions & 0 deletions src/plugins/webservices/weblinks/weblinks.php
Original file line number Diff line number Diff line change
@@ -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']
);
}
}
18 changes: 18 additions & 0 deletions src/plugins/webservices/weblinks/weblinks.xml
Original file line number Diff line number Diff line change
@@ -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>[email protected]</authorEmail>
<authorUrl>www.joomla.org</authorUrl>
<version>4.0.0</version>
<description>PLG_WEBSERVICES_WEBLINKS_XML_DESCRIPTION</description>
<files>
##FILES##
</files>
<languages folder="administrator/language">
##LANGUAGE_FILES##
</languages>
</extension>