Skip to content

Commit

Permalink
feat(NodeType): Added sortingAttributesByWeight boolean option for …
Browse files Browse the repository at this point in the history
…NodeType
  • Loading branch information
ambroisemaupate committed Jun 4, 2024
1 parent a4c49ff commit becf83e
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 18 deletions.
33 changes: 33 additions & 0 deletions lib/RoadizCoreBundle/migrations/Version20240604143759.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 Version20240604143759 extends AbstractMigration
{
public function getDescription(): string
{
return 'Added attributable_by_weight to node_types and color index on attributes table.';
}

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

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP INDEX IDX_319B9E70665648E9 ON attributes');
$this->addSql('ALTER TABLE node_types DROP attributable_by_weight');
}
}
1 change: 1 addition & 0 deletions lib/RoadizCoreBundle/src/Entity/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
ORM\Index(columns: ["type"]),
ORM\Index(columns: ["searchable"]),
ORM\Index(columns: ["weight"]),
ORM\Index(columns: ["color"]),
ORM\Index(columns: ["group_id"]),
ORM\HasLifecycleCallbacks,
UniqueEntity(fields: ["code"]),
Expand Down
18 changes: 18 additions & 0 deletions lib/RoadizCoreBundle/src/Entity/NodeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ class NodeType extends AbstractEntity implements NodeTypeInterface
Serializer\Type("boolean")
]
private bool $attributable = false;
#[
ORM\Column(name: "attributable_by_weight", type: "boolean", nullable: false, options: ["default" => false]),
Serializer\Groups(["node_type"]),
SymfonySerializer\Groups(["node_type"]),
Serializer\Type("boolean")
]
private bool $sortingAttributesByWeight = false;
/**
* Define if this node-type produces nodes that will be
* viewable from a Controller.
Expand Down Expand Up @@ -550,4 +557,15 @@ public function setAttributable(bool $attributable): NodeType
$this->attributable = $attributable;
return $this;
}

public function isSortingAttributesByWeight(): bool
{
return $this->sortingAttributesByWeight;
}

public function setSortingAttributesByWeight(bool $sortingAttributesByWeight): NodeType
{
$this->sortingAttributesByWeight = $sortingAttributesByWeight;
return $this;
}
}
5 changes: 5 additions & 0 deletions lib/RoadizCoreBundle/src/Model/AttributeValueTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ trait AttributeValueTrait
ApiFilter(BaseFilter\SearchFilter::class, properties: [
"attribute.id" => "exact",
"attribute.code" => "exact",
"attribute.color" => "exact",
"attribute.type" => "exact",
"attribute.group" => "exact",
"attribute.group.canonicalName" => "exact",
Expand All @@ -29,6 +30,10 @@ trait AttributeValueTrait
"attribute.visible",
"attribute.searchable"
]),
ApiFilter(BaseFilter\ExistsFilter::class, properties: [
"attribute.color",
"attribute.group"
]),
ApiFilter(BaseFilter\OrderFilter::class, properties: [
"attribute.weight" => "DESC",
])
Expand Down
21 changes: 14 additions & 7 deletions lib/RoadizCoreBundle/src/Repository/AttributeValueRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ public function __construct(

/**
* @param AttributableInterface $attributable
*
* @return array
* @param bool $orderByWeight
* @return array<AttributeValue>
*/
public function findByAttributable(
AttributableInterface $attributable
AttributableInterface $attributable,
bool $orderByWeight = false
): array {
$qb = $this->createQueryBuilder('av');
return $qb->addSelect('avt')
$qb = $qb->addSelect('avt')
->addSelect('a')
->addSelect('at')
->addSelect('ad')
Expand All @@ -44,12 +45,18 @@ public function findByAttributable(
->leftJoin('a.group', 'ag')
->leftJoin('ag.attributeGroupTranslations', 'agt')
->andWhere($qb->expr()->eq('av.node', ':attributable'))
->addOrderBy('av.position', 'ASC')
->setParameters([
'attributable' => $attributable,
])
->setCacheable(true)
->getQuery()
->setCacheable(true);

if ($orderByWeight) {
$qb->addOrderBy('a.weight', 'DESC');
} else {
$qb->addOrderBy('av.position', 'ASC');
}

return $qb->getQuery()
->getResult();
}

Expand Down
11 changes: 10 additions & 1 deletion lib/Rozier/src/Controllers/Nodes/NodesAttributesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,15 @@ public function editAction(Request $request, int $nodeId, int $translationId): R
);

$this->assignation['attribute_value_translation_forms'] = [];
$attributeValues = $node->getAttributeValues();
$nodeType = $node->getNodeType();
$orderByWeight = false;
if ($nodeType instanceof NodeType) {
$orderByWeight = $nodeType->isSortingAttributesByWeight();
}
$attributeValues = $this->em()->getRepository(AttributeValue::class)->findByAttributable(
$node,
$orderByWeight
);
/** @var AttributeValue $attributeValue */
foreach ($attributeValues as $attributeValue) {
$name = $node->getNodeName() . '_attribute_' . $attributeValue->getId();
Expand Down Expand Up @@ -151,6 +159,7 @@ public function editAction(Request $request, int $nodeId, int $translationId): R

$this->assignation['source'] = $nodeSource;
$this->assignation['translation'] = $translation;
$this->assignation['order_by_weight'] = $orderByWeight;
$availableTranslations = $this->em()
->getRepository(Translation::class)
->findAvailableTranslationsForNode($node);
Expand Down
5 changes: 5 additions & 0 deletions lib/Rozier/src/Forms/NodeTypeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'required' => false,
'help' => 'enables_node_attributes_for_this_type',
])
->add('sortingAttributesByWeight', CheckboxType::class, [
'label' => 'sortingAttributesByWeight',
'required' => false,
'help' => 'sort_attributes_by_weight_for_this_type',
])
->add('reachable', CheckboxType::class, [
'label' => 'reachable',
'required' => false,
Expand Down
6 changes: 4 additions & 2 deletions lib/Rozier/src/Resources/app/less/attributes/attributes.less
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

.attribute-value-col {
cursor: move;
.uk-sortable {
.attribute-value-col {
cursor: move;
}
}
8 changes: 8 additions & 0 deletions lib/Rozier/src/Resources/translations/messages.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -4868,6 +4868,14 @@
<source>enables_node_attributes_for_this_type</source>
<target state="translated">Allows nodes of this type to hold attributes in addition of their data fields.</target>
</trans-unit>
<trans-unit xml:space="preserve" id="sortingAttributesByWeight">
<source>sortingAttributesByWeight</source>
<target state="translated">Sort attributes by weight</target>
</trans-unit>
<trans-unit xml:space="preserve" id="sort_attributes_by_weight_for_this_type">
<source>sort_attributes_by_weight_for_this_type</source>
<target state="translated">If node-type is attributable, this option enforce attribute sorting by weight instead of manual position.</target>
</trans-unit>
</body>
</file>
</xliff>
10 changes: 9 additions & 1 deletion lib/Rozier/src/Resources/translations/messages.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -4868,6 +4868,14 @@
<source>enables_node_attributes_for_this_type</source>
<target state="translated">Permets aux nœuds de ce type d'avoir des attributs en plus de leurs données.</target>
</trans-unit>
</body>
<trans-unit xml:space="preserve" id="sortingAttributesByWeight">
<source>sortingAttributesByWeight</source>
<target state="translated">Trier les attributs par poids</target>
</trans-unit>
<trans-unit xml:space="preserve" id="sort_attributes_by_weight_for_this_type">
<source>sort_attributes_by_weight_for_this_type</source>
<target state="translated">Si le type de nœud accepte les attributs, cette option force le tri des attributs par poids plutôt que par position manuelle.</target>
</trans-unit>
</body>
</file>
</xliff>
3 changes: 3 additions & 0 deletions lib/Rozier/src/Resources/translations/messages.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,9 @@

<trans-unit xml:space="preserve" id="attributable"><source>attributable</source></trans-unit>
<trans-unit xml:space="preserve" id="enables_node_attributes_for_this_type"><source>enables_node_attributes_for_this_type</source></trans-unit>

<trans-unit xml:space="preserve" id="sortingAttributesByWeight"><source>sortingAttributesByWeight</source></trans-unit>
<trans-unit xml:space="preserve" id="sort_attributes_by_weight_for_this_type"><source>sort_attributes_by_weight_for_this_type</source></trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
</a>
</td>
<td>{{ item.attributeTranslations.first.label -}}</td>
<td class="mobile-hidden">
{%- if item.group -%}
{{- item.group.name -}}
{%- endif -%}
</td>
<td>{{- item.typeLabel|trans -}}</td>
<td class="mobile-hidden">
{%- if item.attributeValues|length > 0 -%}
Expand All @@ -16,11 +21,6 @@
</span>
{%- endif -%}
</td>
<td class="mobile-hidden">
{%- if item.group -%}
{{- item.group.name -}}
{%- endif -%}
</td>
<td class="mobile-hidden">
{{- item.weight -}}
</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
} only %}
{% endif %}
</th>
<th class="mobile-hidden">{% trans %}attributes.group{% endtrans %}</th>
<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 %}
Expand Down
10 changes: 9 additions & 1 deletion lib/Rozier/src/Resources/views/attributes/groups/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,26 @@
<thead>
<tr>
<th>
{% trans %}attribute_groups.name{% endtrans %}
{% trans %}attribute_group.form.canonicalName{% endtrans %}
{% include '@RoadizRozier/includes/column_ordering.html.twig' with {
'field': 'name',
'filters': filters,
} only %}
</th>
<th>
{% trans %}attribute_groups.name{% endtrans %}
</th>
<th class="table-actions-row table-actions-row-3">{% trans %}actions{% endtrans %}</th>
</tr>
</thead>
<tbody>
{% for item in items %}
<tr>
<td>
<a href="{{ path('attributeGroupsEditPage', { id: item.id }) }}">
{{- item.canonicalName -}}
</a>
</td>
<td>
<a href="{{ path('attributeGroupsEditPage', { id: item.id }) }}">
{{- item.name -}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
<article class="content content-settings-list">
<div class="content-table-cont uk-margin">
<table class="attribute-value-forms content-table settings uk-table attributes">
{% if not order_by_weight %}
<tbody class="uk-sortable" data-uk-sortable="{animation:0, dragCustomClass:'setting-row rz-node-type-field-dragged', handleClass:'attribute-value-col'}">
{% else %}
<tbody>
{% endif %}
{% for attribute_value_translation_form in attribute_value_translation_forms %}
{% set attributeValue = attribute_value_translation_form.vars.data.attributeValue %}
<tr class="setting-row" data-position="{{ attributeValue.position }}" data-id="{{ attributeValue.id }}">
Expand Down

0 comments on commit becf83e

Please sign in to comment.