diff --git a/bundles/AdminBundle/Controller/Admin/Document/DocumentController.php b/bundles/AdminBundle/Controller/Admin/Document/DocumentController.php index a5594670d34..a687b093055 100644 --- a/bundles/AdminBundle/Controller/Admin/Document/DocumentController.php +++ b/bundles/AdminBundle/Controller/Admin/Document/DocumentController.php @@ -40,6 +40,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface; use Symfony\Component\HttpKernel\Event\ControllerEvent; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Routing\Annotation\Route; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; @@ -742,24 +743,21 @@ public function docTypesAction(Request $request) * * @param Request $request * + * @throws BadRequestHttpException If type is invalid + * * @return JsonResponse */ public function getDocTypesAction(Request $request) { $list = new Document\DocType\Listing(); - if ($request->get('type')) { - $type = $request->get('type'); - if (Document\Service::isValidType($type)) { - $list->setFilter(function ($row) use ($type) { - if ($row['type'] == $type) { - return true; - } - - return false; - }); + if ($type = $request->get('type')) { + if (!Document\Service::isValidType($type)) { + throw new BadRequestHttpException('Invalid type: ' . $type); } + $list->setFilter(function (Document\DocType $docType) use ($type) { + return $docType->getType() === $type; + }); } - $list->load(); $docTypes = []; foreach ($list->getDocTypes() as $type) { diff --git a/bundles/AdminBundle/Controller/Admin/ElementController.php b/bundles/AdminBundle/Controller/Admin/ElementController.php index b795622cc6c..2d2fc145e4c 100644 --- a/bundles/AdminBundle/Controller/Admin/ElementController.php +++ b/bundles/AdminBundle/Controller/Admin/ElementController.php @@ -786,21 +786,16 @@ public function getPredefinedPropertiesAction(Request $request) $type = $request->get('elementType'); $allowedTypes = ['asset', 'document', 'object']; - if (in_array($type, $allowedTypes)) { + if (in_array($type, $allowedTypes, true)) { $list = new Model\Property\Predefined\Listing(); - $list->setFilter(function ($row) use ($type) { - if (is_array($row['ctype'])) { - $row['ctype'] = implode(',', $row['ctype']); - } - if (strpos($row['ctype'], $type) !== false) { - return true; + $list->setFilter(function (Model\Property\Predefined $predefined) use ($type) { + if (!str_contains($predefined->getCtype(), $type)) { + return false; } - return false; + return true; }); - $list->load(); - foreach ($list->getProperties() as $type) { $properties[] = $type->getObjectVars(); } diff --git a/bundles/AdminBundle/Controller/Admin/SettingsController.php b/bundles/AdminBundle/Controller/Admin/SettingsController.php index 497e7222c6a..b706d5eb1bd 100644 --- a/bundles/AdminBundle/Controller/Admin/SettingsController.php +++ b/bundles/AdminBundle/Controller/Admin/SettingsController.php @@ -210,14 +210,12 @@ public function metadataAction(Request $request) } } else { // get list of types - $list = new Metadata\Predefined\Listing(); - if ($request->get('filter')) { - $filter = $request->get('filter'); - $list->setFilter(function ($row) use ($filter) { - foreach ($row as $value) { - if (strpos($value, $filter) !== false) { + if ($filter = $request->get('filter')) { + $list->setFilter(function (Metadata\Predefined $predefined) use ($filter) { + foreach ($predefined->getObjectVars() as $value) { + if (stripos($value, $filter) !== false) { return true; } } @@ -226,15 +224,11 @@ public function metadataAction(Request $request) }); } - $list->load(); - $properties = []; - if (is_array($list->getDefinitions())) { - foreach ($list->getDefinitions() as $metadata) { - $data = $metadata->getObjectVars(); - $data['writeable'] = $metadata->isWriteable(); - $properties[] = $data; - } + foreach ($list->getDefinitions() as $metadata) { + $data = $metadata->getObjectVars(); + $data['writeable'] = $metadata->isWriteable(); + $properties[] = $data; } return $this->adminJson(['data' => $properties, 'success' => true, 'total' => $list->getTotalCount()]); @@ -333,15 +327,14 @@ public function propertiesAction(Request $request) // get list of types $list = new Property\Predefined\Listing(); - if ($request->get('filter')) { - $filter = $request->get('filter'); - $list->setFilter(function ($row) use ($filter) { - foreach ($row as $value) { + if ($filter = $request->get('filter')) { + $list->setFilter(function (Property\Predefined $predefined) use ($filter) { + foreach ($predefined->getObjectVars() as $value) { if ($value) { $cellValues = is_array($value) ? $value : [$value]; foreach ($cellValues as $cellValue) { - if (strpos($cellValue, $filter) !== false) { + if (stripos($cellValue, $filter) !== false) { return true; } } @@ -352,15 +345,11 @@ public function propertiesAction(Request $request) }); } - $list->load(); - $properties = []; - if (is_array($list->getProperties())) { - foreach ($list->getProperties() as $property) { - $data = $property->getObjectVars(); - $data['writeable'] = $property->isWriteable(); - $properties[] = $data; - } + foreach ($list->getProperties() as $property) { + $data = $property->getObjectVars(); + $data['writeable'] = $property->isWriteable(); + $properties[] = $data; } return $this->adminJson(['data' => $properties, 'success' => true, 'total' => $list->getTotalCount()]); @@ -885,15 +874,13 @@ public function staticroutesAction(Request $request) $list = new Staticroute\Listing(); - if ($request->get('filter')) { - $filter = $request->get('filter'); - $list->setFilter(function ($staticRoute) use ($filter) { - $vars = $staticRoute->getObjectVars(); - foreach ($vars as $value) { - if (! is_scalar($value)) { + if ($filter = $request->get('filter')) { + $list->setFilter(function (Staticroute $staticRoute) use ($filter) { + foreach ($staticRoute->getObjectVars() as $value) { + if (!is_scalar($value)) { continue; } - if (strpos((string)$value, $filter) !== false) { + if (stripos((string)$value, $filter) !== false) { return true; } } @@ -902,10 +889,7 @@ public function staticroutesAction(Request $request) }); } - $list->load(); - $routes = []; - /** @var Staticroute $routeFromList */ foreach ($list->getRoutes() as $routeFromList) { $route = $routeFromList->getObjectVars(); $route['writeable'] = $routeFromList->isWriteable(); @@ -1157,22 +1141,18 @@ public function thumbnailAdapterCheckAction(Request $request) /** * @Route("/thumbnail-tree", name="pimcore_admin_settings_thumbnailtree", methods={"GET", "POST"}) * - * @param Request $request - * * @return JsonResponse */ - public function thumbnailTreeAction(Request $request) + public function thumbnailTreeAction() { $this->checkPermission('thumbnails'); $thumbnails = []; $list = new Asset\Image\Thumbnail\Config\Listing(); - $items = $list->getThumbnails(); $groups = []; - /** @var Asset\Image\Thumbnail\Config $item */ - foreach ($items as $item) { + foreach ($list->getThumbnails() as $item) { if ($item->getGroup()) { if (empty($groups[$item->getGroup()])) { $groups[$item->getGroup()] = [ @@ -1217,22 +1197,18 @@ public function thumbnailTreeAction(Request $request) /** * @Route("/thumbnail-downloadable", name="pimcore_admin_settings_thumbnaildownloadable", methods={"GET"}) * - * @param Request $request - * * @return JsonResponse */ - public function thumbnailDownloadableAction(Request $request) + public function thumbnailDownloadableAction() { $thumbnails = []; $list = new Asset\Image\Thumbnail\Config\Listing(); - $list->setFilter(function (array $config) { - return array_key_exists('downloadable', $config) ? $config['downloadable'] : false; + $list->setFilter(function (Asset\Image\Thumbnail\Config $config) { + return $config->isDownloadable(); }); - $items = $list->getThumbnails(); - /** @var Asset\Image\Thumbnail\Config $item */ - foreach ($items as $item) { + foreach ($list->getThumbnails() as $item) { $thumbnails[] = [ 'id' => $item->getName(), 'text' => $item->getName(), @@ -1391,22 +1367,18 @@ public function videoThumbnailAdapterCheckAction(Request $request) /** * @Route("/video-thumbnail-tree", name="pimcore_admin_settings_videothumbnailtree", methods={"GET", "POST"}) * - * @param Request $request - * * @return JsonResponse */ - public function videoThumbnailTreeAction(Request $request) + public function videoThumbnailTreeAction() { $this->checkPermission('thumbnails'); $thumbnails = []; $list = new Asset\Video\Thumbnail\Config\Listing(); - $items = $list->getThumbnails(); $groups = []; - /** @var Asset\Video\Thumbnail\Config $item */ - foreach ($items as $item) { + foreach ($list->getThumbnails() as $item) { if ($item->getGroup()) { if (!$groups[$item->getGroup()]) { $groups[$item->getGroup()] = [ diff --git a/lib/Model/Listing/JsonListing.php b/lib/Model/Listing/JsonListing.php index a111b6e76e6..47aef68dcb8 100644 --- a/lib/Model/Listing/JsonListing.php +++ b/lib/Model/Listing/JsonListing.php @@ -20,17 +20,17 @@ abstract class JsonListing extends AbstractModel { /** - * @var mixed + * @var callable|null */ protected $filter; /** - * @var mixed + * @var callable|null */ protected $order; /** - * @return mixed + * @return callable|null */ public function getFilter() { @@ -38,7 +38,7 @@ public function getFilter() } /** - * @param mixed $filter + * @param callable|null $filter */ public function setFilter($filter) { @@ -46,7 +46,7 @@ public function setFilter($filter) } /** - * @return mixed + * @return callable|null */ public function getOrder() { @@ -54,7 +54,7 @@ public function getOrder() } /** - * @param mixed $order + * @param callable|null $order */ public function setOrder($order) { diff --git a/models/Asset/Image/Thumbnail/Config/Listing/Dao.php b/models/Asset/Image/Thumbnail/Config/Listing/Dao.php index 861c2af8bf7..00f28ac9292 100644 --- a/models/Asset/Image/Thumbnail/Config/Listing/Dao.php +++ b/models/Asset/Image/Thumbnail/Config/Listing/Dao.php @@ -34,6 +34,12 @@ public function loadList() foreach ($this->loadIdList() as $name) { $configs[] = Config::getByName($name); } + if ($this->model->getFilter()) { + $configs = array_filter($configs, $this->model->getFilter()); + } + if ($this->model->getOrder()) { + usort($configs, $this->model->getOrder()); + } $this->model->setThumbnails($configs); @@ -45,6 +51,6 @@ public function loadList() */ public function getTotalCount() { - return count($this->loadIdList()); + return count($this->loadList()); } } diff --git a/models/Document/DocType/Listing/Dao.php b/models/Document/DocType/Listing/Dao.php index e76e1ee1a8a..101bb481aaf 100644 --- a/models/Document/DocType/Listing/Dao.php +++ b/models/Document/DocType/Listing/Dao.php @@ -33,6 +33,12 @@ public function loadList() foreach ($this->loadIdList() as $id) { $docTypes[] = Model\Document\DocType::getById($id); } + if ($this->model->getFilter()) { + $docTypes = array_filter($docTypes, $this->model->getFilter()); + } + if ($this->model->getOrder()) { + usort($docTypes, $this->model->getOrder()); + } $this->model->setDocTypes($docTypes); @@ -44,8 +50,6 @@ public function loadList() */ public function getTotalCount() { - $amount = count($this->loadIdList()); - - return $amount; + return count($this->loadList()); } } diff --git a/models/Metadata/Predefined/Listing/Dao.php b/models/Metadata/Predefined/Listing/Dao.php index 6ce9d5ee530..14486dfe80c 100644 --- a/models/Metadata/Predefined/Listing/Dao.php +++ b/models/Metadata/Predefined/Listing/Dao.php @@ -36,6 +36,12 @@ public function loadList() foreach ($this->loadIdList() as $id) { $properties[] = Model\Metadata\Predefined::getById($id); } + if ($this->model->getFilter()) { + $properties = array_filter($properties, $this->model->getFilter()); + } + if ($this->model->getOrder()) { + usort($properties, $this->model->getOrder()); + } $this->model->setDefinitions($properties); diff --git a/models/Property/Predefined/Listing/Dao.php b/models/Property/Predefined/Listing/Dao.php index aac44c32f13..b77d0d037d4 100644 --- a/models/Property/Predefined/Listing/Dao.php +++ b/models/Property/Predefined/Listing/Dao.php @@ -37,6 +37,12 @@ public function loadList() foreach ($this->loadIdList() as $id) { $properties[] = Model\Property\Predefined::getById($id); } + if ($this->model->getFilter()) { + $properties = array_filter($properties, $this->model->getFilter()); + } + if ($this->model->getOrder()) { + usort($properties, $this->model->getOrder()); + } $this->model->setProperties($properties); diff --git a/models/Staticroute/Listing/Dao.php b/models/Staticroute/Listing/Dao.php index a7fb8422593..aa8a624cad7 100644 --- a/models/Staticroute/Listing/Dao.php +++ b/models/Staticroute/Listing/Dao.php @@ -50,8 +50,6 @@ public function loadList() */ public function getTotalCount() { - $amount = count($this->loadIdList()); - - return $amount; + return count($this->loadList()); } }