primo commit
This commit is contained in:
		| @ -0,0 +1,132 @@ | ||||
| <?php | ||||
|  | ||||
| /** | ||||
|  * @package     Joomla.API | ||||
|  * @subpackage  com_contenthistory | ||||
|  * | ||||
|  * @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\Contenthistory\Api\Controller; | ||||
|  | ||||
| use Joomla\CMS\Language\Text; | ||||
| use Joomla\CMS\MVC\Controller\ApiController; | ||||
| use Joomla\CMS\MVC\Controller\Exception; | ||||
| use Joomla\Component\Contenthistory\Administrator\Model\HistoryModel; | ||||
|  | ||||
| // phpcs:disable PSR1.Files.SideEffects | ||||
| \defined('_JEXEC') or die; | ||||
| // phpcs:enable PSR1.Files.SideEffects | ||||
|  | ||||
| /** | ||||
|  * The history controller | ||||
|  * | ||||
|  * @since  4.0.0 | ||||
|  */ | ||||
| class HistoryController extends ApiController | ||||
| { | ||||
|     /** | ||||
|      * The content type of the item. | ||||
|      * | ||||
|      * @var    string | ||||
|      * @since  4.0.0 | ||||
|      */ | ||||
|     protected $contentType = 'history'; | ||||
|  | ||||
|     /** | ||||
|      * The default view for the display method. | ||||
|      * | ||||
|      * @var    string | ||||
|      * @since  3.0 | ||||
|      */ | ||||
|     protected $default_view = 'history'; | ||||
|  | ||||
|     /** | ||||
|      * Basic display of a list view | ||||
|      * | ||||
|      * @return  static  A \JControllerLegacy object to support chaining. | ||||
|      * | ||||
|      * @since   4.0.0 | ||||
|      */ | ||||
|     public function displayList() | ||||
|     { | ||||
|         $this->modelState->set('type_alias', $this->getTypeAliasFromInput()); | ||||
|         $this->modelState->set('type_id', $this->getTypeIdFromInput()); | ||||
|         $this->modelState->set('item_id', $this->getTypeAliasFromInput() . '.' . $this->getItemIdFromInput()); | ||||
|         $this->modelState->set('list.ordering', 'h.save_date'); | ||||
|         $this->modelState->set('list.direction', 'DESC'); | ||||
|  | ||||
|         return parent::displayList(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Method to edit an existing record. | ||||
|      * | ||||
|      * @return  static  A \JControllerLegacy object to support chaining. | ||||
|      * | ||||
|      * @since   4.0.0 | ||||
|      */ | ||||
|     public function keep() | ||||
|     { | ||||
|         /** @var HistoryModel $model */ | ||||
|         $model = $this->getModel($this->contentType); | ||||
|  | ||||
|         if (!$model) { | ||||
|             throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE')); | ||||
|         } | ||||
|  | ||||
|         $recordId = $this->input->getInt('id'); | ||||
|  | ||||
|         if (!$recordId) { | ||||
|             throw new Exception\ResourceNotFound(Text::_('JLIB_APPLICATION_ERROR_RECORD'), 404); | ||||
|         } | ||||
|  | ||||
|         $cid = [$recordId]; | ||||
|  | ||||
|         if (!$model->keep($cid)) { | ||||
|             throw new Exception\Save(Text::plural('COM_CONTENTHISTORY_N_ITEMS_KEEP_TOGGLE', \count($cid))); | ||||
|         } | ||||
|  | ||||
|         return $this; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get item id from input | ||||
|      * | ||||
|      * @return string | ||||
|      * | ||||
|      * @since 4.0.0 | ||||
|      */ | ||||
|     private function getItemIdFromInput() | ||||
|     { | ||||
|         return $this->input->exists('id') ? | ||||
|             $this->input->get('id') : $this->input->post->get('id'); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get type id from input | ||||
|      * | ||||
|      * @return string | ||||
|      * | ||||
|      * @since 4.0.0 | ||||
|      */ | ||||
|     private function getTypeIdFromInput() | ||||
|     { | ||||
|         return $this->input->exists('type_id') ? | ||||
|             $this->input->get('type_id') : $this->input->post->get('type_id'); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get type alias from input | ||||
|      * | ||||
|      * @return string | ||||
|      * | ||||
|      * @since 4.0.0 | ||||
|      */ | ||||
|     private function getTypeAliasFromInput() | ||||
|     { | ||||
|         return $this->input->exists('type_alias') ? | ||||
|             $this->input->get('type_alias') : $this->input->post->get('type_alias'); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,64 @@ | ||||
| <?php | ||||
|  | ||||
| /** | ||||
|  * @package     Joomla.API | ||||
|  * @subpackage  com_contenthistory | ||||
|  * | ||||
|  * @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\Contenthistory\Api\View\History; | ||||
|  | ||||
| use Joomla\CMS\MVC\View\JsonApiView as BaseApiView; | ||||
|  | ||||
| // phpcs:disable PSR1.Files.SideEffects | ||||
| \defined('_JEXEC') or die; | ||||
| // phpcs:enable PSR1.Files.SideEffects | ||||
|  | ||||
| /** | ||||
|  * The history view | ||||
|  * | ||||
|  * @since  4.0.0 | ||||
|  */ | ||||
| class JsonapiView extends BaseApiView | ||||
| { | ||||
|     /** | ||||
|      * The fields to render items in the documents | ||||
|      * | ||||
|      * @var  array | ||||
|      * @since  4.0.0 | ||||
|      */ | ||||
|     protected $fieldsToRenderList = [ | ||||
|         'id', | ||||
|         'ucm_item_id', | ||||
|         'ucm_type_id', | ||||
|         'version_note', | ||||
|         'save_date', | ||||
|         'editor_user_id', | ||||
|         'character_count', | ||||
|         'sha1_hash', | ||||
|         'version_data', | ||||
|         'keep_forever', | ||||
|         'editor', | ||||
|     ]; | ||||
|  | ||||
|     /** | ||||
|      * Prepare item before render. | ||||
|      * | ||||
|      * @param   object  $item  The model item | ||||
|      * | ||||
|      * @return  object | ||||
|      * | ||||
|      * @since   4.0.0 | ||||
|      */ | ||||
|     protected function prepareItem($item) | ||||
|     { | ||||
|         $item->id = $item->version_id; | ||||
|         unset($item->version_id); | ||||
|  | ||||
|         $item->version_data = (array) json_decode($item->version_data, true); | ||||
|  | ||||
|         return parent::prepareItem($item); | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user