From 8bd2b8dfb6af68a4cf5242316c3afba98820da22 Mon Sep 17 00:00:00 2001 From: Blackbit Date: Wed, 11 Jan 2023 15:29:17 +0100 Subject: [PATCH] [Data Objects] Add select mode to many-to-one relation field (#13676) * add display mode to many-to-one relation field * add display mode to many-to-one relation field * fix PhpStan error * better describing option labels for "display mode" configuration * simplify type / subtype / class filtering * simplify type / subtype / class filtering * simplify code * refactoring * work with Data object instead of its settings as array * work with Data object instead of its settings as array * add fullpath to visible fields * add fullpath to visible fields * fix PhpStan errors * fix PhpStan errors - use Relation trait already in AbstractRelations as all Relation field types use it anyway, either directly or via inheritance * add getVisibleFields() to many-to-one-relation. currently hard-coded to "fullpath". soon we will implement configuration of visible fields also for many-to-one relation fields * support path formatter for select display mode for many-to-one-relation fields * fix path formatter usage * fix set value from search popup * respect path formatter for select field mode * Remove interfaces which already get provided by parent class Co-authored-by: mcop1 <89011527+mcop1@users.noreply.github.com> * fix PhpStan * fix PhpStan * fix PhpStan * fix PhpStan * change translations to YAML * fix PhpStan * fix PhpStan * fix PhpStan * fix PhpStan * fix PhpStan * fix PhpStan Co-authored-by: mcop1 <89011527+mcop1@users.noreply.github.com> --- .../object/classes/data/manyToOneRelation.js | 13 ++ .../tags/advancedManyToManyObjectRelation.js | 6 +- .../object/tags/manyToManyObjectRelation.js | 6 +- .../pimcore/object/tags/manyToOneRelation.js | 128 ++++++++++++++++-- .../Admin/DataObject/DataObjectController.php | 121 +++++++++++++---- bundles/CoreBundle/translations/admin.en.yaml | 2 + .../Data/ManyToManyObjectRelation.php | 3 +- .../Data/ManyToManyRelation.php | 6 +- .../Data/ManyToOneRelation.php | 8 +- .../Data/Relations/AbstractRelations.php | 1 + .../Relations/AllowAssetRelationTrait.php | 26 +--- .../Relations/AllowDocumentRelationTrait.php | 4 +- .../Relations/AllowObjectRelationTrait.php | 12 +- phpstan-baseline.neon | 10 -- 14 files changed, 259 insertions(+), 87 deletions(-) diff --git a/bundles/AdminBundle/public/js/pimcore/object/classes/data/manyToOneRelation.js b/bundles/AdminBundle/public/js/pimcore/object/classes/data/manyToOneRelation.js index 4b122c1a48c..c1e767ea85d 100644 --- a/bundles/AdminBundle/public/js/pimcore/object/classes/data/manyToOneRelation.js +++ b/bundles/AdminBundle/public/js/pimcore/object/classes/data/manyToOneRelation.js @@ -346,6 +346,19 @@ pimcore.object.classes.data.manyToOneRelation = Class.create(pimcore.object.clas width: 400 }) ] + }, + { + xtype: "combo", + fieldLabel: t("display_mode"), + name: "displayMode", + value: this.datax.displayMode ?? 'grid', + labelWidth: 140, + forceSelection: true, + width: 400, + store: [ + ['grid', t('display_mode_display')], + ['combo', t('display_mode_inline_search')], + ] } diff --git a/bundles/AdminBundle/public/js/pimcore/object/tags/advancedManyToManyObjectRelation.js b/bundles/AdminBundle/public/js/pimcore/object/tags/advancedManyToManyObjectRelation.js index 28ae7b76914..ab332c9fa27 100644 --- a/bundles/AdminBundle/public/js/pimcore/object/tags/advancedManyToManyObjectRelation.js +++ b/bundles/AdminBundle/public/js/pimcore/object/tags/advancedManyToManyObjectRelation.js @@ -100,9 +100,9 @@ pimcore.object.tags.advancedManyToManyObjectRelation = Class.create(pimcore.obje url: Routing.generate('pimcore_admin_dataobject_dataobject_relation_objects_list'), extraParams: { fieldConfig: JSON.stringify(this.fieldConfig), - data: this.data.map(function(element) { - return element.id; - }).join(','), + data: JSON.stringify(this.data.map(function(element) { + return {id: element.id, type: element.type}; + })), }, reader: { type: 'json', diff --git a/bundles/AdminBundle/public/js/pimcore/object/tags/manyToManyObjectRelation.js b/bundles/AdminBundle/public/js/pimcore/object/tags/manyToManyObjectRelation.js index be597ae121e..2a84c28df64 100644 --- a/bundles/AdminBundle/public/js/pimcore/object/tags/manyToManyObjectRelation.js +++ b/bundles/AdminBundle/public/js/pimcore/object/tags/manyToManyObjectRelation.js @@ -70,9 +70,9 @@ pimcore.object.tags.manyToManyObjectRelation = Class.create(pimcore.object.tags. url: Routing.generate('pimcore_admin_dataobject_dataobject_relation_objects_list'), extraParams: { fieldConfig: JSON.stringify(this.fieldConfig), - data: this.data.map(function(element) { - return element.id; - }).join(','), + data: JSON.stringify(this.data.map(function(element) { + return {id: element.id, type: element.type}; + })), }, reader: { type: 'json', diff --git a/bundles/AdminBundle/public/js/pimcore/object/tags/manyToOneRelation.js b/bundles/AdminBundle/public/js/pimcore/object/tags/manyToOneRelation.js index d3a50db59d4..4e6170de875 100644 --- a/bundles/AdminBundle/public/js/pimcore/object/tags/manyToOneRelation.js +++ b/bundles/AdminBundle/public/js/pimcore/object/tags/manyToOneRelation.js @@ -37,6 +37,52 @@ pimcore.object.tags.manyToOneRelation = Class.create(pimcore.object.tags.abstrac } return true; }.bind(this)); + + this.fieldConfig.visibleFields = "fullpath"; + + let storeConfig = { + data: this.data, + listeners: { + add: function () { + this.dataChanged = true; + }.bind(this), + remove: function () { + this.dataChanged = true; + }.bind(this), + clear: function () { + this.dataChanged = true; + }.bind(this) + }, + }; + + if (this.fieldConfig.displayMode == 'combo') { + storeConfig.proxy = { + type: 'ajax', + url: Routing.generate('pimcore_admin_dataobject_dataobject_relation_objects_list'), + extraParams: { + fieldConfig: JSON.stringify(this.fieldConfig), + data: JSON.stringify( + (this.data.id && this.data.type) ? [{id: this.data.id, type: this.data.type}] : [] + ) + }, + reader: { + type: 'json', + rootProperty: 'options', + successProperty: 'success', + messageProperty: 'message' + } + }; + storeConfig.fields = ['id', 'label']; + storeConfig.autoLoad = true; + storeConfig.listeners = { + beforeload: function(store) { + store.getProxy().setExtraParam('unsavedChanges', this.object ? this.object.getSaveData().data : {}); + store.getProxy().setExtraParam('context', JSON.stringify(this.getContext())); + }.bind(this) + }; + } + + this.store = new Ext.data.JsonStore(storeConfig); }, @@ -77,9 +123,7 @@ pimcore.object.tags.manyToOneRelation = Class.create(pimcore.object.tags.abstrac getLayoutEdit: function () { - var href = { - name: this.fieldConfig.name - }; + var href = {}; var labelWidth = this.fieldConfig.labelWidth ? this.fieldConfig.labelWidth : 100; @@ -95,10 +139,40 @@ pimcore.object.tags.manyToOneRelation = Class.create(pimcore.object.tags.abstrac href.width = 300; } - href.cls = 'pimcore_droptarget_display_edit'; + if (this.fieldConfig.displayMode == 'combo') { + Object.assign(href, { + store: this.store, + autoLoadOnValue: true, + labelWidth: labelWidth, + forceSelection: true, + height: 'auto', + value: this.data.id, + typeAhead: true, + filterPickList: true, + triggerAction: "all", + displayField: "label", + valueField: "id", + listeners: { + change: function (comboBox, newValue) { + if (newValue) { + let record = this.store.getById(newValue); + if (record) { + this.dataChanged = true; + this.data.id = record.get('id'); + this.data.type = record.get('type'); + } + } + }.bind(this) + } + }); + + this.component = Ext.create('Ext.form.field.ComboBox', href); + } else { + href.cls = 'pimcore_droptarget_display_edit'; + href.fieldBodyCls = 'pimcore_droptarget_display x-form-trigger-wrap'; + this.component = new Ext.form.field.Display(href); + } - href.fieldBodyCls = 'pimcore_droptarget_display x-form-trigger-wrap'; - this.component = new Ext.form.field.Display(href); if (this.data.published === false) { this.component.addCls("strikeThrough"); } @@ -122,7 +196,7 @@ pimcore.object.tags.manyToOneRelation = Class.create(pimcore.object.tags.abstrac el.getEl().on("contextmenu", this.onContextMenu.bind(this)); - el.getEl().on('dblclick', function(){ + el.getEl().on('dblclick', function () { var subtype = this.data.subtype; if (this.data.type === "object" && this.data.subtype !== "folder" && this.data.subtype !== null) { subtype = "object"; @@ -270,7 +344,18 @@ pimcore.object.tags.manyToOneRelation = Class.create(pimcore.object.tags.abstrac this.data.subtype = data["type"]; this.data.path = data["fullpath"]; this.dataChanged = true; - this.component.setValue(data["fullpath"]); + if (this.fieldConfig.displayMode == 'combo') { + if (!this.component.getStore().getById(data.id)) { + this.component.getStore().getProxy().setExtraParam('data', JSON.stringify([{id: this.data.id, type: this.data.type}])); + this.component.getStore().on('load', function(){ + this.component.setValue(this.data.id); + }.bind(this), this, { single: true }); + this.component.getStore().load(); + } + this.component.setValue(this.data.id); + } else { + this.component.setValue(data["fullpath"]); + } this.requestNicePathData(); } } catch (e) { @@ -308,7 +393,18 @@ pimcore.object.tags.manyToOneRelation = Class.create(pimcore.object.tags.abstrac if (data.published === false) { this.component.addCls("strikeThrough"); } - this.component.setValue(data.path); + if (this.fieldConfig.displayMode == 'combo') { + if (!this.component.getStore().getById(data.id)) { + this.component.getStore().getProxy().setExtraParam('data', JSON.stringify([{id: data.id, type: data.elementType}])); + this.component.getStore().on('load', function(){ + this.component.setValue(data.id); + }.bind(this), this, { single: true }); + this.component.getStore().load(); + } + this.component.setValue(data.id); + } else { + this.component.setValue(data.path); + } this.requestNicePathData(); return true; @@ -429,7 +525,13 @@ pimcore.object.tags.manyToOneRelation = Class.create(pimcore.object.tags.abstrac if (data.published === false) { this.component.addCls("strikeThrough"); } - this.component.setValue(data.fullpath); + + if (this.fieldConfig.displayMode == 'combo') { + this.component.setValue(data.id); + } else { + this.component.setValue(data.fullpath); + } + this.requestNicePathData(); }, @@ -554,7 +656,11 @@ pimcore.object.tags.manyToOneRelation = Class.create(pimcore.object.tags.abstrac this.component.removeCls("grid_nicepath_requested"); if (typeof responseData[target["nicePathKey"]] !== "undefined") { - this.component.setValue(responseData[target["nicePathKey"]]); + if (this.fieldConfig.displayMode == 'combo') { + this.component.setValue(target["id"]); + } else { + this.component.setValue(responseData[target["nicePathKey"]]); + } } }.bind(this, target) diff --git a/bundles/AdminBundle/src/Controller/Admin/DataObject/DataObjectController.php b/bundles/AdminBundle/src/Controller/Admin/DataObject/DataObjectController.php index ff1862ab763..d9509244810 100644 --- a/bundles/AdminBundle/src/Controller/Admin/DataObject/DataObjectController.php +++ b/bundles/AdminBundle/src/Controller/Admin/DataObject/DataObjectController.php @@ -557,64 +557,139 @@ static function (Task $task) { */ public function optionsAction(Request $request): JsonResponse { - $fieldConfig = json_decode($request->get('fieldConfig'), true); - - $options = []; - $classes = []; - if (count($fieldConfig['classes']) > 0) { - foreach ($fieldConfig['classes'] as $classData) { - $classes[] = $classData['classes']; - } + $fieldConfigData = json_decode($request->get('fieldConfig'), true); + $loader = \Pimcore::getContainer()->get('pimcore.implementation_loader.object.data'); + if (!$loader->supports($fieldConfigData['fieldtype'])) { + return new JsonResponse([]); } - $visibleFields = is_array($fieldConfig['visibleFields']) ? $fieldConfig['visibleFields'] : explode(',', $fieldConfig['visibleFields']); + /** @var AbstractRelations $fieldConfig */ + $fieldConfig = $loader->build($fieldConfigData['fieldtype']); + $fieldConfig->setValues($fieldConfigData); + + $visibleFields = null; + if(method_exists($fieldConfig, 'getVisibleFields')) { + $visibleFields = is_array($fieldConfig->getVisibleFields()) ? $fieldConfig->getVisibleFields() : explode(',', $fieldConfig->getVisibleFields()); + } if (!$visibleFields) { $visibleFields = ['id', 'fullpath', 'classname']; } $searchRequest = $request; - $searchRequest->request->set('type', 'object'); - $searchRequest->request->set('subtype', 'object,variant'); + + $allowedTypes = []; + $subTypes = []; + $classes = []; + $allowClasses = true; + + if ($fieldConfig->getAssetsAllowed()) { + $allowedTypes[] = 'asset'; + $allowClasses = false; + + foreach ($fieldConfig->getAssetTypes() as $subType) { + $subTypes[] = $subType['assetTypes']; + } + } + + if (method_exists($fieldConfig, 'getDocumentsAllowed') && $fieldConfig->getDocumentsAllowed()) { + $allowedTypes[] = 'document'; + $allowClasses = false; + + foreach ($fieldConfig->getDocumentTypes() as $subType) { + $subTypes[] = $subType['documentTypes']; + } + } + + if (method_exists($fieldConfig, 'getObjectsAllowed') && $fieldConfig->getObjectsAllowed()) { + $allowedTypes[] = 'object'; + + if ( $allowClasses ) { + $subTypes = array_merge($subTypes, ['object', 'variant']); + + foreach ($fieldConfig->getClasses() as $classData) { + $classes[] = $classData['classes']; + } + } + } + + $searchRequest->request->set('type', implode(',', $allowedTypes)); + $searchRequest->request->set('subtype', implode(',', $subTypes)); $searchRequest->request->set('class', implode(',', $classes)); $searchRequest->request->set('fields', $visibleFields); $searchRequest->attributes->set('unsavedChanges', $request->get('unsavedChanges', '')); $res = $this->forward(SearchController::class.'::findAction', ['request' => $searchRequest]); $objects = json_decode($res->getContent(), true)['data']; - if ($request->get('data')) { - foreach (explode(',', $request->get('data')) as $preSelectedElementId) { - $objects[] = ['id' => $preSelectedElementId]; + if($request->get('data')) { + foreach(json_decode($request->get('data'), true) as $preSelectedElement) { + if (isset($preSelectedElement['id'], $preSelectedElement['type'])) { + $objects[] = ['id' => $preSelectedElement['id'], 'type' => $preSelectedElement['type']]; + } } } + $options = []; foreach ($objects as $objectData) { $option = [ 'id' => $objectData['id'], + 'type' => $objectData['type'] ]; $visibleFieldValues = []; foreach ($visibleFields as $visibleField) { + if ($visibleField === 'fullpath' && $fieldConfig instanceof DataObject\ClassDefinition\PathFormatterAwareInterface) { + $object = Element\Service::getElementById($objectData['type'], $objectData['id']); + if (!$object instanceof Element\ElementInterface) { + continue; + } + + $formatter = $fieldConfig->getPathFormatterClass(); + + if (null !== $formatter) { + $pathFormatter = DataObject\ClassDefinition\Helper\PathFormatterResolver::resolvePathFormatter( + $fieldConfig->getPathFormatterClass() + ); + + if ($pathFormatter instanceof DataObject\ClassDefinition\PathFormatterInterface) { + $formattedPath = $pathFormatter->formatPath( + [], + $object, + [$objectData], + [ + 'fd' => $fieldConfig, + 'context' => [] + ] + )[0] ?? null; + if($formattedPath) { + $objectData['fullpath'] = $formattedPath; + } + } + } + } + if (isset($objectData[$visibleField])) { $visibleFieldValues[] = $objectData[$visibleField]; } else { + $object = Element\Service::getElementById($objectData['type'], $objectData['id']); + if (!$object instanceof Element\ElementInterface) { + continue; + } + $inheritValues = DataObject\Concrete::getGetInheritedValues(); $fallbackValues = DataObject\Localizedfield::getGetFallbackValues(); DataObject\Concrete::setGetInheritedValues(true); DataObject\Localizedfield::setGetFallbackValues(true); - $object = DataObject\Concrete::getById($objectData['id']); - if (!$object instanceof DataObject\Concrete) { - continue; - } - $getter = 'get'.ucfirst($visibleField); - $visibleFieldValue = $object->$getter(); - if (count($classes) > 1 && $visibleField == 'key') { - $visibleFieldValue .= ' ('.$object->getClassName().')'; + if(method_exists($object, $getter)) { + $visibleFieldValue = $object->$getter(); + if ($visibleField === 'key' && $object instanceof DataObject\Concrete && count($classes) > 1) { + $visibleFieldValue .= ' ('.$object->getClassName().')'; + } + $visibleFieldValues[] = $visibleFieldValue; } - $visibleFieldValues[] = $visibleFieldValue; DataObject\Concrete::setGetInheritedValues($inheritValues); DataObject\Localizedfield::setGetFallbackValues($fallbackValues); diff --git a/bundles/CoreBundle/translations/admin.en.yaml b/bundles/CoreBundle/translations/admin.en.yaml index 1a988899251..1b89423680e 100644 --- a/bundles/CoreBundle/translations/admin.en.yaml +++ b/bundles/CoreBundle/translations/admin.en.yaml @@ -1084,4 +1084,6 @@ delete_data_from_it: This will also delete all data from it. display_mode: Display Mode display_mode_grid: Grid view display_mode_combo: Tag field +display_mode_display: Display path reference +display_mode_inline_search: Inline Search batch_confirmation: You are about to update %s dataObjects. Do you want to continue ? diff --git a/models/DataObject/ClassDefinition/Data/ManyToManyObjectRelation.php b/models/DataObject/ClassDefinition/Data/ManyToManyObjectRelation.php index 6997719e1a3..dcc824e8b58 100644 --- a/models/DataObject/ClassDefinition/Data/ManyToManyObjectRelation.php +++ b/models/DataObject/ClassDefinition/Data/ManyToManyObjectRelation.php @@ -25,9 +25,8 @@ use Pimcore\Model\Element; use Pimcore\Normalizer\NormalizerInterface; -class ManyToManyObjectRelation extends AbstractRelations implements QueryResourcePersistenceAwareInterface, OptimizedAdminLoadingInterface, TypeDeclarationSupportInterface, VarExporterInterface, NormalizerInterface, IdRewriterInterface, PreGetDataInterface, PreSetDataInterface, LayoutDefinitionEnrichmentInterface +class ManyToManyObjectRelation extends AbstractRelations implements QueryResourcePersistenceAwareInterface, OptimizedAdminLoadingInterface, VarExporterInterface, NormalizerInterface, PreGetDataInterface, PreSetDataInterface, LayoutDefinitionEnrichmentInterface { - use DataObject\ClassDefinition\Data\Extension\Relation; use DataObject\ClassDefinition\Data\Relations\AllowObjectRelationTrait; use DataObject\ClassDefinition\Data\Relations\ManyToManyRelationTrait; use DataObject\ClassDefinition\Data\Extension\RelationFilterConditionParser; diff --git a/models/DataObject/ClassDefinition/Data/ManyToManyRelation.php b/models/DataObject/ClassDefinition/Data/ManyToManyRelation.php index e83a0580adb..86884ff0020 100644 --- a/models/DataObject/ClassDefinition/Data/ManyToManyRelation.php +++ b/models/DataObject/ClassDefinition/Data/ManyToManyRelation.php @@ -27,9 +27,8 @@ use Pimcore\Model\Element; use Pimcore\Normalizer\NormalizerInterface; -class ManyToManyRelation extends AbstractRelations implements QueryResourcePersistenceAwareInterface, OptimizedAdminLoadingInterface, TypeDeclarationSupportInterface, VarExporterInterface, NormalizerInterface, IdRewriterInterface, PreGetDataInterface, PreSetDataInterface +class ManyToManyRelation extends AbstractRelations implements QueryResourcePersistenceAwareInterface, OptimizedAdminLoadingInterface, VarExporterInterface, NormalizerInterface, PreGetDataInterface, PreSetDataInterface { - use Model\DataObject\ClassDefinition\Data\Extension\Relation; use Extension\QueryColumnType; use DataObject\ClassDefinition\Data\Relations\AllowObjectRelationTrait; use DataObject\ClassDefinition\Data\Relations\AllowAssetRelationTrait; @@ -172,6 +171,9 @@ public function setAssetsAllowed(bool $assetsAllowed): static return $this; } + /** + * @return array + */ public function getAssetTypes(): array { return $this->assetTypes; diff --git a/models/DataObject/ClassDefinition/Data/ManyToOneRelation.php b/models/DataObject/ClassDefinition/Data/ManyToOneRelation.php index 55ad2964a0a..9ff03f24a18 100644 --- a/models/DataObject/ClassDefinition/Data/ManyToOneRelation.php +++ b/models/DataObject/ClassDefinition/Data/ManyToOneRelation.php @@ -28,9 +28,8 @@ use Pimcore\Model\Element; use Pimcore\Normalizer\NormalizerInterface; -class ManyToOneRelation extends AbstractRelations implements QueryResourcePersistenceAwareInterface, TypeDeclarationSupportInterface, VarExporterInterface, NormalizerInterface, IdRewriterInterface, PreGetDataInterface, PreSetDataInterface +class ManyToOneRelation extends AbstractRelations implements QueryResourcePersistenceAwareInterface, VarExporterInterface, NormalizerInterface, PreGetDataInterface, PreSetDataInterface { - use Model\DataObject\ClassDefinition\Data\Extension\Relation; use Extension\QueryColumnType; use DataObject\ClassDefinition\Data\Relations\AllowObjectRelationTrait; use DataObject\ClassDefinition\Data\Relations\AllowAssetRelationTrait; @@ -619,4 +618,9 @@ public function getFilterConditionExt(mixed $value, string $operator, array $par return $this->getRelationFilterCondition($value, $operator, $name); } + + public function getVisibleFields(): ?string + { + return 'fullpath'; + } } diff --git a/models/DataObject/ClassDefinition/Data/Relations/AbstractRelations.php b/models/DataObject/ClassDefinition/Data/Relations/AbstractRelations.php index d3e84afeedb..e00b0db1427 100644 --- a/models/DataObject/ClassDefinition/Data/Relations/AbstractRelations.php +++ b/models/DataObject/ClassDefinition/Data/Relations/AbstractRelations.php @@ -35,6 +35,7 @@ abstract class AbstractRelations extends Data implements Data\IdRewriterInterface { use DataObject\Traits\ContextPersistenceTrait; + use Data\Extension\Relation; const RELATION_ID_SEPARATOR = '$$'; diff --git a/models/DataObject/ClassDefinition/Data/Relations/AllowAssetRelationTrait.php b/models/DataObject/ClassDefinition/Data/Relations/AllowAssetRelationTrait.php index 7a00082b01a..319f97d2352 100644 --- a/models/DataObject/ClassDefinition/Data/Relations/AllowAssetRelationTrait.php +++ b/models/DataObject/ClassDefinition/Data/Relations/AllowAssetRelationTrait.php @@ -35,37 +35,19 @@ trait AllowAssetRelationTrait */ protected function allowAssetRelation(Asset $asset): bool { - if (!$asset instanceof Asset || $asset->getId() <= 0) { + if ($asset->getId() <= 0) { return false; } $allowedAssetTypes = $this->getAssetTypes(); - $allowedTypes = []; $allowed = true; if (!$this->getAssetsAllowed()) { $allowed = false; - } elseif ($this->getAssetsAllowed() && is_array($allowedAssetTypes) && count($allowedAssetTypes) > 0) { + } elseif (count($allowedAssetTypes) > 0) { //check for allowed asset types - foreach ($allowedAssetTypes as $t) { - if (is_array($t) && array_key_exists('assetTypes', $t)) { - $t = $t['assetTypes']; - } + $allowedTypes = array_column($allowedAssetTypes, 'assetTypes'); - if ($t) { - if (is_string($t)) { - $allowedTypes[] = $t; - } elseif (is_array($t)) { - if (isset($t['assetTypes'])) { - $allowedTypes[] = $t['assetTypes']; - } else { - $allowedTypes[] = $t; - } - } - } - } - if (!in_array($asset->getType(), $allowedTypes)) { - $allowed = false; - } + $allowed = in_array($asset->getType(), $allowedTypes, true); } else { //don't check if no allowed asset types set } diff --git a/models/DataObject/ClassDefinition/Data/Relations/AllowDocumentRelationTrait.php b/models/DataObject/ClassDefinition/Data/Relations/AllowDocumentRelationTrait.php index 100f8899064..22d5e3e657f 100644 --- a/models/DataObject/ClassDefinition/Data/Relations/AllowDocumentRelationTrait.php +++ b/models/DataObject/ClassDefinition/Data/Relations/AllowDocumentRelationTrait.php @@ -44,7 +44,7 @@ protected function allowDocumentRelation(Document $document): bool $allowed = true; if (!$this->getDocumentsAllowed()) { $allowed = false; - } elseif ($this->getDocumentsAllowed() && is_array($allowedDocumentTypes) && count($allowedDocumentTypes) > 0) { + } elseif (count($allowedDocumentTypes) > 0) { //check for allowed asset types $allowedTypes = []; foreach ($allowedDocumentTypes as $t) { @@ -53,7 +53,7 @@ protected function allowDocumentRelation(Document $document): bool } } - if (!in_array($document->getType(), $allowedTypes) && count($allowedTypes)) { + if (!in_array($document->getType(), $allowedTypes, true) && count($allowedTypes) > 0) { $allowed = false; } } else { diff --git a/models/DataObject/ClassDefinition/Data/Relations/AllowObjectRelationTrait.php b/models/DataObject/ClassDefinition/Data/Relations/AllowObjectRelationTrait.php index e242924449b..78ef4e1e82f 100644 --- a/models/DataObject/ClassDefinition/Data/Relations/AllowObjectRelationTrait.php +++ b/models/DataObject/ClassDefinition/Data/Relations/AllowObjectRelationTrait.php @@ -35,7 +35,7 @@ trait AllowObjectRelationTrait */ protected function allowObjectRelation(DataObject\AbstractObject $object): bool { - if (!$object instanceof DataObject\AbstractObject || $object->getId() <= 0) { + if ($object->getId() <= 0) { return false; } @@ -44,18 +44,16 @@ protected function allowObjectRelation(DataObject\AbstractObject $object): bool if (!$this->getObjectsAllowed()) { $allowed = false; } elseif (count($allowedClasses) > 0) { - $allowedClassnames = []; - foreach ($allowedClasses as $c) { - $allowedClassnames[] = $c['classes']; - } + $allowedClassnames = array_column($allowedClasses, 'classes'); + //check for allowed classes if ($object instanceof DataObject\Concrete) { $classname = $object->getClassName(); - if (!in_array($classname, $allowedClassnames)) { + if (!in_array($classname, $allowedClassnames, true)) { $allowed = false; } } elseif ($object instanceof DataObject\Folder) { - if (!in_array('folder', $allowedClassnames)) { + if (!in_array('folder', $allowedClassnames, true)) { $allowed = false; } } else { diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index efa838d2b95..d603ad5dcfa 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -139,13 +139,3 @@ parameters: message: "#^Negated boolean expression is always true\\.$#" count: 1 path: models/Asset/Video/ImageThumbnail.php - - - - message: "#^Left side of && is always true\\.$#" - count: 2 - path: models/DataObject/ClassDefinition/Data/ManyToManyRelation.php - - - - message: "#^Left side of && is always true\\.$#" - count: 2 - path: models/DataObject/ClassDefinition/Data/ManyToOneRelation.php