diff --git a/config/api_resources/web_response.yml b/config/api_resources/web_response.yml index cf9513c9..8e9ddb30 100644 --- a/config/api_resources/web_response.yml +++ b/config/api_resources/web_response.yml @@ -25,7 +25,7 @@ RZ\Roadiz\CoreBundle\Api\Model\WebResponse: - tag_base - translation_base - document_display - - node_attributes + #- node_attributes - document_display_sources openapiContext: summary: Get a resource by its path wrapped in a WebResponse object diff --git a/lib/RoadizCoreBundle/migrations/Version20230829082257.php b/lib/RoadizCoreBundle/migrations/Version20230829082257.php new file mode 100644 index 00000000..ac1f45c8 --- /dev/null +++ b/lib/RoadizCoreBundle/migrations/Version20230829082257.php @@ -0,0 +1,33 @@ +addSql('CREATE INDEX idx_attribute_value_node_position ON attribute_values (node_id, position)'); + $this->addSql('CREATE INDEX idx_attribute_value_position ON attribute_values (position)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('DROP INDEX idx_attribute_value_node_position ON attribute_values'); + $this->addSql('DROP INDEX idx_attribute_value_position ON attribute_values'); + } +} diff --git a/lib/RoadizCoreBundle/src/Entity/AttributeValue.php b/lib/RoadizCoreBundle/src/Entity/AttributeValue.php index 3c47d2dd..ec2869d0 100644 --- a/lib/RoadizCoreBundle/src/Entity/AttributeValue.php +++ b/lib/RoadizCoreBundle/src/Entity/AttributeValue.php @@ -22,8 +22,13 @@ ORM\Entity(repositoryClass: AttributeValueRepository::class), ORM\Table(name: "attribute_values"), ORM\Index(columns: ["attribute_id", "node_id"]), + ORM\Index(columns: ["node_id", "position"], name: "idx_attribute_value_node_position"), + ORM\Index(columns: ["position"], name: "idx_attribute_value_position"), ORM\HasLifecycleCallbacks, - ApiFilter(PropertyFilter::class) + ApiFilter(PropertyFilter::class), + ApiFilter(BaseFilter\OrderFilter::class, properties: [ + "position", + ]), ] class AttributeValue extends AbstractPositioned implements AttributeValueInterface { @@ -41,7 +46,9 @@ class AttributeValue extends AbstractPositioned implements AttributeValueInterfa ApiFilter(BaseFilter\SearchFilter::class, properties: [ "node" => "exact", "node.id" => "exact", - "node.nodeName" => "exact" + "node.nodeName" => "exact", + "node.nodeType" => "exact", + "node.nodeType.name" => "exact" ]), ApiFilter(BaseFilter\BooleanFilter::class, properties: [ "node.visible" @@ -78,7 +85,7 @@ public function getAttributable(): ?AttributableInterface */ public function setAttributable(?AttributableInterface $attributable) { - if (null === $attributable || $attributable instanceof Node) { + if ($attributable instanceof Node) { $this->node = $attributable; return $this; } @@ -115,14 +122,12 @@ public function __clone() if ($this->id) { $this->id = null; $attributeValueTranslations = $this->getAttributeValueTranslations(); - if ($attributeValueTranslations !== null) { - $this->attributeValueTranslations = new ArrayCollection(); - /** @var AttributeValueTranslationInterface $attributeValueTranslation */ - foreach ($attributeValueTranslations as $attributeValueTranslation) { - $cloneAttributeValueTranslation = clone $attributeValueTranslation; - $cloneAttributeValueTranslation->setAttributeValue($this); - $this->attributeValueTranslations->add($cloneAttributeValueTranslation); - } + $this->attributeValueTranslations = new ArrayCollection(); + /** @var AttributeValueTranslationInterface $attributeValueTranslation */ + foreach ($attributeValueTranslations as $attributeValueTranslation) { + $cloneAttributeValueTranslation = clone $attributeValueTranslation; + $cloneAttributeValueTranslation->setAttributeValue($this); + $this->attributeValueTranslations->add($cloneAttributeValueTranslation); } } } diff --git a/lib/RoadizCoreBundle/src/Model/AttributeTrait.php b/lib/RoadizCoreBundle/src/Model/AttributeTrait.php index c6b46a7e..2305a8bf 100644 --- a/lib/RoadizCoreBundle/src/Model/AttributeTrait.php +++ b/lib/RoadizCoreBundle/src/Model/AttributeTrait.php @@ -346,4 +346,21 @@ public function isCountry(): bool { return $this->getType() === AttributeInterface::COUNTRY_T; } + + public function getTypeLabel(): string + { + return match ($this->getType()) { + AttributeInterface::DATETIME_T => 'attributes.form.type.datetime', + AttributeInterface::BOOLEAN_T => 'attributes.form.type.boolean', + AttributeInterface::INTEGER_T => 'attributes.form.type.integer', + AttributeInterface::DECIMAL_T => 'attributes.form.type.decimal', + AttributeInterface::PERCENT_T => 'attributes.form.type.percent', + AttributeInterface::EMAIL_T => 'attributes.form.type.email', + AttributeInterface::COLOUR_T => 'attributes.form.type.colour', + AttributeInterface::ENUM_T => 'attributes.form.type.enum', + AttributeInterface::DATE_T => 'attributes.form.type.date', + AttributeInterface::COUNTRY_T => 'attributes.form.type.country', + default => 'attributes.form.type.string', + }; + } } diff --git a/lib/RoadizCoreBundle/src/Model/AttributeValueTrait.php b/lib/RoadizCoreBundle/src/Model/AttributeValueTrait.php index 51e2919d..bf1dff3c 100644 --- a/lib/RoadizCoreBundle/src/Model/AttributeValueTrait.php +++ b/lib/RoadizCoreBundle/src/Model/AttributeValueTrait.php @@ -45,12 +45,21 @@ trait AttributeValueTrait ), Serializer\Groups(["attribute", "node", "nodes_sources"]), Serializer\Type("ArrayCollection"), - Serializer\Accessor(getter: "getAttributeValueTranslations", setter: "setAttributeValueTranslations") + Serializer\Accessor(getter: "getAttributeValueTranslations", setter: "setAttributeValueTranslations"), + ApiFilter(BaseFilter\SearchFilter::class, properties: [ + "attributeValueTranslations.value" => "partial", + ]), + ApiFilter(BaseFilter\RangeFilter::class, properties: [ + "attributeValueTranslations.value", + ]), + ApiFilter(BaseFilter\ExistsFilter::class, properties: [ + "attributeValueTranslations.value", + ]), ] protected Collection $attributeValueTranslations; /** - * @return AttributeInterface + * @return AttributeInterface|null */ public function getAttribute(): ?AttributeInterface { @@ -60,7 +69,7 @@ public function getAttribute(): ?AttributeInterface /** * @param AttributeInterface $attribute * - * @return mixed + * @return static */ public function setAttribute(AttributeInterface $attribute) { @@ -87,7 +96,7 @@ public function getAttributeValueTranslations(): Collection /** * @param Collection $attributeValueTranslations * - * @return mixed + * @return static */ public function setAttributeValueTranslations(Collection $attributeValueTranslations) { @@ -96,13 +105,13 @@ public function setAttributeValueTranslations(Collection $attributeValueTranslat foreach ($this->attributeValueTranslations as $attributeValueTranslation) { $attributeValueTranslation->setAttributeValue($this); } - return true; + return $this; } /** * @param TranslationInterface $translation * - * @return AttributeValueTranslationInterface + * @return AttributeValueTranslationInterface|null */ public function getAttributeValueTranslation(TranslationInterface $translation): ?AttributeValueTranslationInterface { @@ -115,4 +124,16 @@ public function getAttributeValueTranslation(TranslationInterface $translation): }) ->first() ?: null; } + + /** + * @return AttributeValueTranslationInterface|null + */ + public function getAttributeValueDefaultTranslation(): ?AttributeValueTranslationInterface + { + return $this->getAttributeValueTranslations() + ->filter(function (AttributeValueTranslationInterface $attributeValueTranslation) { + return $attributeValueTranslation->getTranslation()?->isDefaultTranslation() ?? false; + }) + ->first() ?: null; + } } diff --git a/lib/RoadizCoreBundle/src/Model/AttributeValueTranslationTrait.php b/lib/RoadizCoreBundle/src/Model/AttributeValueTranslationTrait.php index f139fc3b..0e038563 100644 --- a/lib/RoadizCoreBundle/src/Model/AttributeValueTranslationTrait.php +++ b/lib/RoadizCoreBundle/src/Model/AttributeValueTranslationTrait.php @@ -123,7 +123,7 @@ public function setAttributeValue(AttributeValueInterface $attributeValue) } /** - * @return AttributeInterface + * @return AttributeInterface|null */ public function getAttribute(): ?AttributeInterface { diff --git a/lib/RoadizCoreBundle/src/Serializer/Normalizer/AttributeValueNormalizer.php b/lib/RoadizCoreBundle/src/Serializer/Normalizer/AttributeValueNormalizer.php index 543d865d..86ca6cea 100644 --- a/lib/RoadizCoreBundle/src/Serializer/Normalizer/AttributeValueNormalizer.php +++ b/lib/RoadizCoreBundle/src/Serializer/Normalizer/AttributeValueNormalizer.php @@ -35,11 +35,20 @@ public function normalize($object, $format = null, array $context = []) if (isset($context['translation']) && $context['translation'] instanceof TranslationInterface) { $translatedData = $object->getAttributeValueTranslation($context['translation']); $data['label'] = $object->getAttribute()->getLabelOrCode($context['translation']); - if ($translatedData instanceof AttributeValueTranslationInterface) { + if ( + $translatedData instanceof AttributeValueTranslationInterface && + $translatedData->getValue() !== null + ) { $data['value'] = $translatedData->getValue(); + } else { + $data['value'] = $object->getAttributeValueDefaultTranslation()?->getValue(); } } + if ($data['value'] instanceof \DateTimeInterface) { + $data['value'] = $data['value']->format(\DateTimeInterface::ATOM); + } + if (\in_array('attribute_documents', $serializationGroups, true)) { $documentsContext = $context; $documentsContext['groups'] = ['document_display']; diff --git a/lib/Rozier/src/Resources/views/attributes/list.html.twig b/lib/Rozier/src/Resources/views/attributes/list.html.twig index 372020ab..3214f864 100644 --- a/lib/Rozier/src/Resources/views/attributes/list.html.twig +++ b/lib/Rozier/src/Resources/views/attributes/list.html.twig @@ -37,6 +37,7 @@ 'filters': filters, } only %} + {% trans %}attributes.form.type{% endtrans %} {% trans %}attributes.group{% endtrans %} {% trans %}actions{% endtrans %} @@ -50,6 +51,7 @@ {{ item.attributeTranslations.first.label|default(item.code) -}} + {{- item.typeLabel|trans -}} {%- if item.group -%} {{- item.group.name -}} {%- endif -%}