Skip to content

Commit

Permalink
feat(Attributes): Added indexes on attributes values position, fallba…
Browse files Browse the repository at this point in the history
…ck value on default translation during Normalization
  • Loading branch information
ambroisemaupate committed Aug 29, 2023
1 parent 957024b commit 453708f
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 20 deletions.
2 changes: 1 addition & 1 deletion config/api_resources/web_response.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions lib/RoadizCoreBundle/migrations/Version20230829082257.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20230829082257 extends AbstractMigration
{
public function getDescription(): string
{
return 'Indexes for attribute_values table.';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->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');
}
}
27 changes: 16 additions & 11 deletions lib/RoadizCoreBundle/src/Entity/AttributeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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"
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions lib/RoadizCoreBundle/src/Model/AttributeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};
}
}
33 changes: 27 additions & 6 deletions lib/RoadizCoreBundle/src/Model/AttributeValueTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,21 @@ trait AttributeValueTrait
),
Serializer\Groups(["attribute", "node", "nodes_sources"]),
Serializer\Type("ArrayCollection<RZ\Roadiz\CoreBundle\Model\AttributeValueTranslationInterface>"),
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
{
Expand All @@ -60,7 +69,7 @@ public function getAttribute(): ?AttributeInterface
/**
* @param AttributeInterface $attribute
*
* @return mixed
* @return static
*/
public function setAttribute(AttributeInterface $attribute)
{
Expand All @@ -87,7 +96,7 @@ public function getAttributeValueTranslations(): Collection
/**
* @param Collection $attributeValueTranslations
*
* @return mixed
* @return static
*/
public function setAttributeValueTranslations(Collection $attributeValueTranslations)
{
Expand All @@ -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
{
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function setAttributeValue(AttributeValueInterface $attributeValue)
}

/**
* @return AttributeInterface
* @return AttributeInterface|null
*/
public function getAttribute(): ?AttributeInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
2 changes: 2 additions & 0 deletions lib/Rozier/src/Resources/views/attributes/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'filters': filters,
} only %}
</th>
<th>{% trans %}attributes.form.type{% endtrans %}</th>
<th>{% trans %}attributes.group{% endtrans %}</th>
<th class="table-actions-row table-actions-row-3">{% trans %}actions{% endtrans %}</th>
</tr>
Expand All @@ -50,6 +51,7 @@
{{ item.attributeTranslations.first.label|default(item.code) -}}
</a>
</td>
<td>{{- item.typeLabel|trans -}}</td>
<td>{%- if item.group -%}
{{- item.group.name -}}
{%- endif -%}</td>
Expand Down

0 comments on commit 453708f

Please sign in to comment.