Skip to content

Commit

Permalink
feat(Attributes): Added Attribute weight field to sort filtered lists.
Browse files Browse the repository at this point in the history
  • Loading branch information
ambroisemaupate committed Jun 3, 2024
1 parent 1c929fc commit cbcc6e6
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 4 deletions.
33 changes: 33 additions & 0 deletions lib/RoadizCoreBundle/migrations/Version20240603210209.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 Version20240603210209 extends AbstractMigration
{
public function getDescription(): string
{
return 'Added weight column to attributes table.';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE attributes ADD weight INT DEFAULT 0 NOT NULL');
$this->addSql('CREATE INDEX IDX_319B9E707CD5541 ON attributes (weight)');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP INDEX IDX_319B9E707CD5541 ON attributes');
$this->addSql('ALTER TABLE attributes DROP weight');
}
}
36 changes: 33 additions & 3 deletions lib/RoadizCoreBundle/src/Entity/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@

namespace RZ\Roadiz\CoreBundle\Entity;

use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Metadata\ApiFilter;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use RZ\Roadiz\Core\AbstractEntities\AbstractEntity;
use RZ\Roadiz\CoreBundle\Model\AttributeInterface;
use RZ\Roadiz\CoreBundle\Model\AttributeTrait;
use RZ\Roadiz\CoreBundle\Model\RealmInterface;
use RZ\Roadiz\CoreBundle\Repository\AttributeRepository;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation as SymfonySerializer;
use RZ\Roadiz\CoreBundle\Model\AttributeInterface;
use RZ\Roadiz\CoreBundle\Model\AttributeTrait;
use RZ\Roadiz\Core\AbstractEntities\AbstractEntity;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Range;

/**
* @package RZ\Roadiz\CoreBundle\Entity
Expand All @@ -25,6 +29,7 @@
ORM\Index(columns: ["code"]),
ORM\Index(columns: ["type"]),
ORM\Index(columns: ["searchable"]),
ORM\Index(columns: ["weight"]),
ORM\Index(columns: ["group_id"]),
ORM\HasLifecycleCallbacks,
UniqueEntity(fields: ["code"]),
Expand Down Expand Up @@ -62,6 +67,20 @@ class Attribute extends AbstractEntity implements AttributeInterface
#[Serializer\Exclude]
private ?RealmInterface $defaultRealm = null;

/**
* @var int Absolute weight for sorting attributes in filtered lists.
*/
#[
ORM\Column(type: "integer", nullable: false, options: ["default" => 0]),
Serializer\Type("integer"),
Serializer\Groups(["attribute", "node", "nodes_sources"]),
SymfonySerializer\Groups(["attribute", "node", "nodes_sources"]),
ApiFilter(OrderFilter::class),
Range(min: 0, max: 9999),
NotNull,
]
protected int $weight = 0;

public function __construct()
{
$this->attributeTranslations = new ArrayCollection();
Expand Down Expand Up @@ -100,6 +119,17 @@ public function setDefaultRealm(?RealmInterface $defaultRealm): Attribute
return $this;
}

public function getWeight(): int
{
return $this->weight;
}

public function setWeight(?int $weight): Attribute
{
$this->weight = $weight ?? 0;
return $this;
}

/**
* @return Collection<int, Document>
*/
Expand Down
7 changes: 7 additions & 0 deletions lib/RoadizCoreBundle/src/Form/AttributeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -59,6 +60,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'required' => false,
'help' => 'attributes.form_help.searchable'
])
->add('weight', NumberType::class, [
'label' => 'attributes.weight',
'required' => false,
'scale' => 1,
'help' => 'attributes.form_help.weight'
])
->add('defaultRealm', RealmChoiceType::class, [
'label' => 'attributes.defaultRealm',
'help' => 'attributes.defaultRealm.help',
Expand Down
2 changes: 2 additions & 0 deletions lib/RoadizCoreBundle/src/Model/AttributeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public function getType(): int;
*/
public function getColor(): ?string;

public function getWeight(): int;

/**
* @param string|null $color
*/
Expand Down
3 changes: 3 additions & 0 deletions lib/RoadizCoreBundle/src/Model/AttributeValueTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ trait AttributeValueTrait
ApiFilter(BaseFilter\BooleanFilter::class, properties: [
"attribute.visible",
"attribute.searchable"
]),
ApiFilter(BaseFilter\OrderFilter::class, properties: [
"attribute.weight" => "DESC",
])
]
protected AttributeInterface $attribute;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function normalize(mixed $object, ?string $format = null, array $context
$data['type'] = $object->getType();
$data['code'] = $object->getAttribute()->getCode();
$data['color'] = $object->getAttribute()->getColor();
$data['weight'] = $object->getAttribute()->getWeight();

if (isset($context['translation']) && $context['translation'] instanceof TranslationInterface) {
$translatedData = $object->getAttributeValueTranslation($context['translation']);
Expand Down
11 changes: 11 additions & 0 deletions lib/RoadizRozierBundle/translations/attributes/messages.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,17 @@
<target state="translated">Usage count</target>
<note></note>
</trans-unit>

<trans-unit xml:space="preserve" id="attributes.weight">
<source>attributes.weight</source>
<target state="translated">Weight</target>
<note></note>
</trans-unit>
<trans-unit xml:space="preserve" id="attributes.form_help.weight">
<source>attributes.form_help.weight</source>
<target state="translated">Absolute weight to sort attribute in filtered lists.</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions lib/RoadizRozierBundle/translations/attributes/messages.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,16 @@
<target state="translated">Nombre d'utilisations</target>
<note></note>
</trans-unit>
<trans-unit xml:space="preserve" id="attributes.weight">
<source>attributes.weight</source>
<target state="translated">Poids</target>
<note></note>
</trans-unit>
<trans-unit xml:space="preserve" id="attributes.form_help.weight">
<source>attributes.form_help.weight</source>
<target state="translated">Poids absolu pour faciliter le tri des listes d'attributs filtrées.</target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
10 changes: 10 additions & 0 deletions lib/RoadizRozierBundle/translations/attributes/messages.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,16 @@
<target></target>
<note></note>
</trans-unit>
<trans-unit xml:space="preserve" id="attributes.weight">
<source>attributes.weight</source>
<target></target>
<note></note>
</trans-unit>
<trans-unit xml:space="preserve" id="attributes.form_help.weight">
<source>attributes.form_help.weight</source>
<target></target>
<note></note>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ protected function getFormType(): string
*/
protected function getDefaultOrder(Request $request): array
{
return ['code' => 'ASC'];
return [
'weight' => 'DESC',
'code' => 'ASC',
];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
{{- item.group.name -}}
{%- endif -%}
</td>
<td class="mobile-hidden">
{{- item.weight -}}
</td>
<td class="table-actions-row">
{% apply spaceless %}
{% if hasBulkActions %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
<th>{% trans %}attributes.form.type{% endtrans %}</th>
<th class="mobile-hidden"></th>
<th class="mobile-hidden">{% trans %}attributes.group{% endtrans %}</th>
<th class="mobile-hidden">
{% trans %}attributes.weight{% endtrans %}
{% if not no_sort %}
{% include '@RoadizRozier/includes/column_ordering.html.twig' with {
'field': 'weight',
'filters': filters,
} only %}
{% endif %}
</th>
<th class="table-actions-row table-actions-row-3">
{% if hasBulkActions %}
<div class="bulk-selection">
Expand Down

0 comments on commit cbcc6e6

Please sign in to comment.