Skip to content
Open
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
144 changes: 135 additions & 9 deletions administrator/components/com_categories/src/Model/CategoryModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Joomla\Component\Categories\Administrator\Model;

use Joomla\CMS\Access\Rules;
use Joomla\CMS\Access\Exception\NotAllowedException;
use Joomla\CMS\Association\AssociationServiceInterface;
use Joomla\CMS\Categories\CategoryServiceInterface;
use Joomla\CMS\Component\ComponentHelper;
Expand All @@ -21,10 +21,12 @@
use Joomla\CMS\Language\Associations;
use Joomla\CMS\Language\LanguageHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\Exception\ResourceNotFoundException;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\MVC\Model\AdminModel;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Table\Category;
use Joomla\CMS\Table\Exception\DuplicateEntryException;
use Joomla\CMS\UCM\UCMType;
use Joomla\CMS\Versioning\VersionableModelInterface;
use Joomla\CMS\Versioning\VersionableModelTrait;
Expand Down Expand Up @@ -444,6 +446,10 @@

// Check for an error.
if ($form instanceof \Exception) {
if ($this->shouldUseExceptions()) {

Check failure on line 449 in administrator/components/com_categories/src/Model/CategoryModel.php

View workflow job for this annotation

GitHub Actions / Run PHPstan

Call to deprecated method shouldUseExceptions() of class Joomla\CMS\MVC\Model\BaseModel: 7.0
throw $form;
}

$this->setError($form->getMessage());

return false;
Expand Down Expand Up @@ -548,13 +554,25 @@

// Rebuild the path for the category:
if (!$table->rebuildPath($table->id)) {
if ($this->shouldUseExceptions()) {

Check failure on line 557 in administrator/components/com_categories/src/Model/CategoryModel.php

View workflow job for this annotation

GitHub Actions / Run PHPstan

Call to deprecated method shouldUseExceptions() of class Joomla\CMS\MVC\Model\BaseModel: 7.0
$error = $table->getError(null, false);

Check failure on line 558 in administrator/components/com_categories/src/Model/CategoryModel.php

View workflow job for this annotation

GitHub Actions / Run PHPstan

Ignored error pattern #^Call to deprecated method getError\(\) of class Joomla\\CMS\\Table\\Table\: 3\.1\.4 will be removed in 7\.0 Will be removed without replacement Catch thrown Exceptions instead of getError$# (method.deprecated) in path /__w/joomla-cms/joomla-cms/administrator/components/com_categories/src/Model/CategoryModel.php is expected to occur 14 times, but occurred 24 times.

throw $error instanceof \Throwable ? $error : new \RuntimeException((string) $error);
}

$this->setError($table->getError());

return false;
}

// Rebuild the paths of the category's children:
if (!$table->rebuild($table->id, $table->lft, $table->level, $table->path)) {
if ($this->shouldUseExceptions()) {

Check failure on line 570 in administrator/components/com_categories/src/Model/CategoryModel.php

View workflow job for this annotation

GitHub Actions / Run PHPstan

Call to deprecated method shouldUseExceptions() of class Joomla\CMS\MVC\Model\BaseModel: 7.0
$error = $table->getError(null, false);

throw $error instanceof \Throwable ? $error : new \RuntimeException((string) $error);
}

$this->setError($table->getError());

return false;
Expand Down Expand Up @@ -629,6 +647,12 @@
$table = $this->getTable();

if (!$table->rebuild()) {
if ($this->shouldUseExceptions()) {
$error = $table->getError(null, false);

throw $error instanceof \Throwable ? $error : new \RuntimeException((string) $error);
}

$this->setError($table->getError());

return false;
Expand Down Expand Up @@ -658,6 +682,12 @@
$table = $this->getTable();

if (!$table->saveorder($idArray, $lftArray)) {
if ($this->shouldUseExceptions()) {
$error = $table->getError(null, false);

throw $error instanceof \Throwable ? $error : new \RuntimeException((string) $error);
}

$this->setError($table->getError());

return false;
Expand Down Expand Up @@ -747,14 +777,22 @@
// Check that the parent exists
if ($parentId) {
if (!$this->table->load($parentId)) {
if ($error = $this->table->getError()) {
if ($error = $this->table->getError(null, false)) {
// Fatal error
$this->setError($error);
if ($this->shouldUseExceptions()) {
throw $error instanceof \Throwable ? $error : new \RuntimeException((string) $error);
}

$this->setError($error instanceof \Throwable ? $error->getMessage() : $error);

return false;
}

// Non-fatal error
if ($this->shouldUseExceptions()) {
throw new ResourceNotFoundException(Text::_('JGLOBAL_BATCH_MOVE_PARENT_NOT_FOUND'));
}

$this->setError(Text::_('JGLOBAL_BATCH_MOVE_PARENT_NOT_FOUND'));
$parentId = 0;
}
Expand All @@ -768,6 +806,10 @@

if (!$canCreate) {
// Error since user cannot create in parent category
if ($this->shouldUseExceptions()) {
throw new NotAllowedException(Text::_('COM_CATEGORIES_BATCH_CANNOT_CREATE'));
}

$this->setError(Text::_('COM_CATEGORIES_BATCH_CANNOT_CREATE'));

return false;
Expand All @@ -777,13 +819,23 @@
// If the parent is 0, set it to the ID of the root item in the tree
if (empty($parentId)) {
if (!$parentId = $this->table->getRootId()) {
if ($this->shouldUseExceptions()) {
$error = $this->table->getError(null, false);

throw $error instanceof \Throwable ? $error : new \RuntimeException((string) $error);
}

$this->setError($this->table->getError());

return false;
}

if (!$this->user->authorise('core.create', $extension)) {
// Make sure we can create in root
if ($this->shouldUseExceptions()) {
throw new NotAllowedException(Text::_('COM_CATEGORIES_BATCH_CANNOT_CREATE'));
}

$this->setError(Text::_('COM_CATEGORIES_BATCH_CANNOT_CREATE'));

return false;
Expand All @@ -802,6 +854,10 @@
try {
$count = $db->loadResult();
} catch (\RuntimeException $e) {
if ($this->shouldUseExceptions()) {
throw $e;
}

$this->setError($e->getMessage());

return false;
Expand All @@ -816,14 +872,22 @@

// Check that the row actually exists
if (!$this->table->load($pk)) {
if ($error = $this->table->getError()) {
if ($error = $this->table->getError(null, false)) {
// Fatal error
$this->setError($error);
if ($this->shouldUseExceptions()) {
throw $error instanceof \Throwable ? $error : new \RuntimeException((string) $error);
}

$this->setError($error instanceof \Throwable ? $error->getMessage() : $error);

return false;
}

// Not fatal error
if ($this->shouldUseExceptions()) {
throw new ResourceNotFoundException(Text::sprintf('JGLOBAL_BATCH_MOVE_ROW_NOT_FOUND', $pk));
}

$this->setError(Text::sprintf('JGLOBAL_BATCH_MOVE_ROW_NOT_FOUND', $pk));
continue;
}
Expand Down Expand Up @@ -880,6 +944,12 @@

// Store the row.
if (!$this->table->store()) {
if ($this->shouldUseExceptions()) {
$error = $this->table->getError(null, false);

throw $error instanceof \Throwable ? $error : new \RuntimeException((string) $error);
}

$this->setError($this->table->getError());

return false;
Expand Down Expand Up @@ -912,13 +982,25 @@

// Rebuild the hierarchy.
if (!$this->table->rebuild()) {
if ($this->shouldUseExceptions()) {
$error = $this->table->getError(null, false);

throw $error instanceof \Throwable ? $error : new \RuntimeException((string) $error);
}

$this->setError($this->table->getError());

return false;
}

// Rebuild the tree path.
if (!$this->table->rebuildPath($this->table->id)) {
if ($this->shouldUseExceptions()) {
$error = $this->table->getError(null, false);

throw $error instanceof \Throwable ? $error : new \RuntimeException((string) $error);
}

$this->setError($this->table->getError());

return false;
Expand Down Expand Up @@ -951,14 +1033,22 @@
// Check that the parent exists.
if ($parentId) {
if (!$this->table->load($parentId)) {
if ($error = $this->table->getError()) {
if ($error = $this->table->getError(null, false)) {
// Fatal error.
$this->setError($error);
if ($this->shouldUseExceptions()) {
throw $error instanceof \Throwable ? $error : new \RuntimeException((string) $error);
}

$this->setError($error instanceof \Throwable ? $error->getMessage() : $error);

return false;
}

// Non-fatal error.
if ($this->shouldUseExceptions()) {
throw new ResourceNotFoundException(Text::_('JGLOBAL_BATCH_MOVE_PARENT_NOT_FOUND'));
}

$this->setError(Text::_('JGLOBAL_BATCH_MOVE_PARENT_NOT_FOUND'));
$parentId = 0;
}
Expand All @@ -972,6 +1062,10 @@

if (!$canCreate) {
// Error since user cannot create in parent category
if ($this->shouldUseExceptions()) {
throw new NotAllowedException(Text::_('COM_CATEGORIES_BATCH_CANNOT_CREATE'));
}

$this->setError(Text::_('COM_CATEGORIES_BATCH_CANNOT_CREATE'));

return false;
Expand All @@ -982,6 +1076,10 @@
foreach ($pks as $pk) {
if (!$this->user->authorise('core.edit', $extension . '.category.' . $pk)) {
// Error since user cannot edit this category
if ($this->shouldUseExceptions()) {
throw new NotAllowedException(Text::_('COM_CATEGORIES_BATCH_CANNOT_EDIT'));
}

$this->setError(Text::_('COM_CATEGORIES_BATCH_CANNOT_EDIT'));

return false;
Expand All @@ -998,14 +1096,22 @@
foreach ($pks as $pk) {
// Check that the row actually exists
if (!$this->table->load($pk)) {
if ($error = $this->table->getError()) {
if ($error = $this->table->getError(null, false)) {
// Fatal error
$this->setError($error);
if ($this->shouldUseExceptions()) {
throw $error instanceof \Throwable ? $error : new \RuntimeException((string) $error);
}

$this->setError($error instanceof \Throwable ? $error->getMessage() : $error);

return false;
}

// Not fatal error
if ($this->shouldUseExceptions()) {
throw new ResourceNotFoundException(Text::sprintf('JGLOBAL_BATCH_MOVE_ROW_NOT_FOUND', $pk));
}

$this->setError(Text::sprintf('JGLOBAL_BATCH_MOVE_ROW_NOT_FOUND', $pk));
continue;
}
Expand All @@ -1030,6 +1136,10 @@
try {
$children = array_merge($children, (array) $db->loadColumn());
} catch (\RuntimeException $e) {
if ($this->shouldUseExceptions()) {
throw $e;
}

$this->setError($e->getMessage());

return false;
Expand All @@ -1043,6 +1153,10 @@
];

if ($table->load($conditions)) {
if ($this->shouldUseExceptions()) {
throw new DuplicateEntryException(Text::_('JLIB_DATABASE_ERROR_CATEGORY_UNIQUE_ALIAS'), 'alias');
}

$this->setError(Text::_('JLIB_DATABASE_ERROR_CATEGORY_UNIQUE_ALIAS'));

return false;
Expand All @@ -1051,13 +1165,25 @@

// Store the row.
if (!$this->table->store()) {
if ($this->shouldUseExceptions()) {
$error = $this->table->getError(null, false);

throw $error instanceof \Throwable ? $error : new \RuntimeException((string) $error);
}

$this->setError($this->table->getError());

return false;
}

// Rebuild the tree path.
if (!$this->table->rebuildPath()) {
if ($this->shouldUseExceptions()) {
$error = $this->table->getError(null, false);

throw $error instanceof \Throwable ? $error : new \RuntimeException((string) $error);
}

$this->setError($this->table->getError());

return false;
Expand Down
Loading
Loading