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

[4.0] Set data directly into state rather than faking a proxy into the input #27696

Merged
merged 1 commit into from
Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class CategoriesController extends ApiController
*/
public function displayItem($id = null)
{
$this->input->set('model_state', ['filter.extension' => $this->getExtensionFromInput()]);
$this->modelState->set('filter.extension', $this->getExtensionFromInput());

return parent::displayItem($id);
}
Expand All @@ -60,7 +60,7 @@ public function displayItem($id = null)
*/
public function displayList()
{
$this->input->set('model_state', ['filter.extension' => $this->getExtensionFromInput()]);
$this->modelState->set('filter.extension', $this->getExtensionFromInput());

return parent::displayList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,11 @@ class HistoryController extends ApiController
*/
public function displayList()
{
$this->input->set('model_state', [
'type_alias' => $this->getTypeAliasFromInput(),
'type_id' => $this->getTypeIdFromInput(),
'item_id' => $this->getItemIdFromInput(),
'list.ordering' => 'h.save_date',
'list.direction' => 'DESC',
]
);
$this->modelState->set('type_alias', $this->getTypeAliasFromInput());
$this->modelState->set('type_id', $this->getTypeIdFromInput());
$this->modelState->set('item_id', $this->getItemIdFromInput());
$this->modelState->set('list.ordering', 'h.save_date');
$this->modelState->set('list.direction', 'DESC');

return parent::displayList();
}
Expand Down
4 changes: 2 additions & 2 deletions api/components/com_fields/Controller/FieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class FieldsController extends ApiController
*/
public function displayItem($id = null)
{
$this->input->set('model_state', ['filter.context' => $this->getContextFromInput()]);
$this->modelState->set('filter.context', $this->getContextFromInput());

return parent::displayItem($id);
}
Expand All @@ -61,7 +61,7 @@ public function displayItem($id = null)
*/
public function displayList()
{
$this->input->set('model_state', ['filter.context' => $this->getContextFromInput()]);
$this->modelState->set('filter.context', $this->getContextFromInput());

return parent::displayList();
}
Expand Down
4 changes: 2 additions & 2 deletions api/components/com_fields/Controller/GroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class GroupsController extends ApiController
*/
public function displayItem($id = null)
{
$this->input->set('model_state', ['filter.context' => $this->getContextFromInput()]);
$this->modelState->set('filter.context', $this->getContextFromInput());

return parent::displayItem($id);
}
Expand All @@ -61,7 +61,7 @@ public function displayItem($id = null)
*/
public function displayList()
{
$this->input->set('model_state', ['filter.context' => $this->getContextFromInput()]);
$this->modelState->set('filter.context', $this->getContextFromInput());

return parent::displayList();
}
Expand Down
21 changes: 6 additions & 15 deletions api/components/com_languages/Controller/OverridesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ class OverridesController extends ApiController
*/
public function displayItem($id = null)
{
$this->input->set('model_state', [
'filter.language' => $this->getLanguageFromInput(),
'filter.client' => $this->getClientFromInput(),
]
);
$this->modelState->set('filter.language', $this->getLanguageFromInput());
$this->modelState->set('filter.client', $this->getClientFromInput());

return parent::displayItem($id);
}
Expand All @@ -70,11 +67,8 @@ public function displayItem($id = null)
*/
public function displayList()
{
$this->input->set('model_state', [
'filter.language' => $this->getLanguageFromInput(),
'filter.client' => $this->getClientFromInput(),
]
);
$this->modelState->set('filter.language', $this->getLanguageFromInput());
$this->modelState->set('filter.client', $this->getClientFromInput());

return parent::displayList();
}
Expand Down Expand Up @@ -168,11 +162,8 @@ public function delete($id = null)

$this->input->set('model', $this->contentType);

$this->input->set('model_state', [
'filter.language' => $this->getLanguageFromInput(),
'filter.client' => $this->getClientFromInput(),
]
);
$this->modelState->set('filter.language', $this->getLanguageFromInput());
$this->modelState->set('filter.client', $this->getClientFromInput());

parent::delete($id);
}
Expand Down
4 changes: 2 additions & 2 deletions api/components/com_menus/Controller/ItemsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ItemsController extends ApiController
*/
public function displayItem($id = null)
{
$this->input->set('model_state', ['filter.client_id' => $this->getClientIdFromInput()]);
$this->modelState->set('filter.client_id', $this->getClientIdFromInput());

return parent::displayItem($id);
}
Expand All @@ -65,7 +65,7 @@ public function displayItem($id = null)
*/
public function displayList()
{
$this->input->set('model_state', ['filter.client_id' => $this->getClientIdFromInput()]);
$this->modelState->set('filter.client_id', $this->getClientIdFromInput());

return parent::displayList();
}
Expand Down
4 changes: 2 additions & 2 deletions api/components/com_menus/Controller/MenusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class MenusController extends ApiController
*/
public function displayItem($id = null)
{
$this->input->set('model_state', ['client_id' => $this->getClientIdFromInput()]);
$this->modelState->set('filter.client_id', $this->getClientIdFromInput());

return parent::displayItem($id);
}
Expand All @@ -61,7 +61,7 @@ public function displayItem($id = null)
*/
public function displayList()
{
$this->input->set('model_state', ['client_id' => $this->getClientIdFromInput()]);
$this->modelState->set('filter.client_id', $this->getClientIdFromInput());

return parent::displayList();
}
Expand Down
4 changes: 2 additions & 2 deletions api/components/com_modules/Controller/ModulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ModulesController extends ApiController
*/
public function displayItem($id = null)
{
$this->input->set('model_state', ['client_id' => $this->getClientIdFromInput()]);
$this->modelState->set('filter.client_id', $this->getClientIdFromInput());

return parent::displayItem($id);
}
Expand All @@ -64,7 +64,7 @@ public function displayItem($id = null)
*/
public function displayList()
{
$this->input->set('model_state', ['client_id' => $this->getClientIdFromInput()]);
$this->modelState->set('filter.client_id', $this->getClientIdFromInput());

return parent::displayList();
}
Expand Down
4 changes: 2 additions & 2 deletions api/components/com_templates/Controller/StylesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class StylesController extends ApiController
*/
public function displayItem($id = null)
{
$this->input->set('model_state', ['client_id' => $this->getClientIdFromInput()]);
$this->modelState->set('client_id', $this->getClientIdFromInput());

return parent::displayItem($id);
}
Expand All @@ -61,7 +61,7 @@ public function displayItem($id = null)
*/
public function displayList()
{
$this->input->set('model_state', ['client_id' => $this->getClientIdFromInput()]);
$this->modelState->set('client_id', $this->getClientIdFromInput());

return parent::displayList();
}
Expand Down
48 changes: 15 additions & 33 deletions libraries/src/MVC/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Joomla\CMS\MVC\Factory\MvcFactoryInterface;
use Joomla\CMS\MVC\Model\ListModel;
use Joomla\CMS\MVC\View\JsonApiView;
use Joomla\CMS\Object\CMSObject;
use Joomla\Input\Input;
use Joomla\String\Inflector;
use Tobscure\JsonApi\Exception\InvalidParameterException;
Expand Down Expand Up @@ -71,6 +72,13 @@ class ApiController extends BaseController
*/
protected $itemsPerPage = 20;

/**
* The model state to inject
*
* @var CMSObject
*/
protected $modelState;

/**
* Constructor.
*
Expand All @@ -86,6 +94,8 @@ class ApiController extends BaseController
*/
public function __construct($config = array(), MvcFactoryInterface $factory = null, $app = null, $input = null)
{
$this->modelState = new CMSObject;

parent::__construct($config, $factory, $app, $input);

// Guess the option as com_NameOfController
Expand Down Expand Up @@ -151,23 +161,14 @@ public function displayItem($id = null)

$modelName = $this->input->get('model', Inflector::singularize($this->contentType));

// Create the model, ignoring request data so we can safely set the state in the request, without it being
// reinitialised on the first getState call
$model = $this->getModel($modelName, '', ['ignore_request' => true]);
// Create the model, ignoring request data so we can safely set the state in the request from the controller
$model = $this->getModel($modelName, '', ['ignore_request' => true, 'state' => $this->modelState]);

if (!$model)
{
throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'));
}

if ($modelState = $this->input->get('model_state', false, 'array'))
{
foreach ($modelState as $property => $value)
{
$model->setState($property, $value);
}
}

try
{
$modelName = $model->getName();
Expand Down Expand Up @@ -199,20 +200,17 @@ public function displayList()
{
// Assemble pagination information (using recommended JsonApi pagination notation for offset strategy)
$paginationInfo = $this->input->get('page', [], 'array');
$internalPaginationMapping = [];

if (\array_key_exists('offset', $paginationInfo))
{
$this->input->set('limitstart', $paginationInfo['offset']);
$this->modelState->set($this->context . '.limitstart', $paginationInfo['offset']);
}

if (\array_key_exists('limit', $paginationInfo))
{
$internalPaginationMapping['limit'] = $paginationInfo['limit'];
$this->modelState->set($this->context . '.list.limit', $paginationInfo['limit']);
}

$this->input->set('list', $internalPaginationMapping);

$viewType = $this->app->getDocument()->getType();
$viewName = $this->input->get('view', $this->default_view);
$viewLayout = $this->input->get('layout', 'default', 'string');
Expand All @@ -235,21 +233,13 @@ public function displayList()
$modelName = $this->input->get('model', $this->contentType);

/** @var ListModel $model */
$model = $this->getModel($modelName, '', ['ignore_request' => true]);
$model = $this->getModel($modelName, '', ['ignore_request' => true, 'state' => $this->modelState]);

if (!$model)
{
throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'));
}

if ($modelState = $this->input->get('model_state', false, 'array'))
{
foreach ($modelState as $property => $value)
{
$model->setState($property, $value);
}
}

// Push the model into the view (as default)
$view->setModel($model, true);

Expand Down Expand Up @@ -305,14 +295,6 @@ public function delete($id = null)
throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'));
}

if ($modelState = $this->input->get('model_state', false, 'array'))
{
foreach ($modelState as $property => $value)
{
$model->setState($property, $value);
}
}

// Remove the item.
if (!$model->delete($id))
{
Expand Down