Skip to content

Preparations for bulk editing of tags #940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Controller/PartListsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function tableAction(Request $request, PartsTableActionHandler $actionHan
$errors = [];

$parts = $actionHandler->idStringToArray($ids);
$redirectResponse = $actionHandler->handleAction($action, $parts, $target ? (int) $target : null, $redirect, $errors);
$redirectResponse = $actionHandler->handleAction($action, $parts, $target ? $target : null, $redirect, $errors);

//Save changes
$this->entityManager->flush();
Expand Down
15 changes: 15 additions & 0 deletions src/Controller/SelectAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use App\Entity\ProjectSystem\Project;
use App\Form\Type\Helper\StructuralEntityChoiceHelper;
use App\Services\Trees\NodesListBuilder;
use App\ApiPlatform\Filter\TagFilter;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -49,6 +50,20 @@ public function __construct(private readonly NodesListBuilder $nodesListBuilder,
{
}

#[Route(path: '/tag', name: 'select_tag')]
public function tag(): Response
{
$tags = [
'text' => 'test',
'value' => 'test',
];
$this->addEmptyNode($tags);
// pseudocode:
// for each part in selection
// use TagFilter to find tags
// dedupe
return $this->json($tags);

#[Route(path: '/category', name: 'select_category')]
public function category(): Response
{
Expand Down
20 changes: 19 additions & 1 deletion src/Services/Parts/PartsTableActionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ public function idStringToArray(string $ids): array
* @return RedirectResponse|null Returns a redirect response if the user should be redirected to another page, otherwise null
* //@param-out list<array{'part': Part, 'message': string|TranslatableInterface}>|array<void> $errors
*/
public function handleAction(string $action, array $selected_parts, ?int $target_id, ?string $redirect_url = null, array &$errors = []): ?RedirectResponse
public function handleAction(string $action, array $selected_parts, ?string $target_id, ?string $redirect_url = null, array &$errors = []): ?RedirectResponse
{
// validate target_id
if (!str_contains($action, 'tag') && $target_id !== null && !is_numeric($target_id))
throw new InvalidArgumentException('$target_id must be an integer for action'. $action.'!');
}

if ($action === 'add_to_project') {
return new RedirectResponse(
$this->urlGenerator->generate('project_add_parts', [
Expand Down Expand Up @@ -130,6 +135,19 @@ public function handleAction(string $action, array $selected_parts, ?int $target
$this->denyAccessUnlessGranted('edit', $part);

switch ($action) {
case "add_tag":
$this->denyAccessUnlessGranted('edit', $part);
$tags = $part->getTags();
// simple append
$part->setTags($tags.','.$target_id);
break;
case "remove_tag":
$this->denyAccessUnlessGranted('edit', $part);
// remove any matching tag at start or end
$tags = preg_replace('/(^'.$target_id.',|,'.$target_id.'$)/', '', $part->getTags());
// remove any matching tags in the middle, retaining one comma, and commit
$part->setTags(str_replace(','.$target_id.',', ',' $tags);
break;
case 'favorite':
$this->denyAccessUnlessGranted('change_favorite', $part);
$part->setFavorite(true);
Expand Down
4 changes: 4 additions & 0 deletions templates/components/datatables.macro.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@

<select class="form-select" name="action" data-controller="elements--select" {{ stimulus_action('elements/datatables/parts', 'updateTargetPicker', 'change') }}
title="{% trans %}part_list.action.action.title{% endtrans %}" required>
<optgroup label="{% trans %}part_list.action.action.group.tags{% endtrans %}">
<option {% if not is_granted('@parts.edit') %}disabled{% endif %} value="add_tag" data-url="{{ path('select_tag') }}">{% trans %}part_list.action.action.add_tag{% endtrans %}</option>
<option {% if not is_granted('@parts.edit') %}disabled{% endif %} value="remove_tag" data-url="{{ path('select_tag') }}">{% trans %}part_list.action.action.remove_tag{% endtrans %}</option>
</optgroup>
<optgroup label="{% trans %}part_list.action.action.group.favorite{% endtrans %}">
<option {% if not is_granted('@parts.change_favorite') %}disabled{% endif %} value="favorite">{% trans %}part_list.action.action.favorite{% endtrans %}</option>
<option {% if not is_granted('@parts.change_favorite') %}disabled{% endif %} value="unfavorite">{% trans %}part_list.action.action.unfavorite{% endtrans %}</option>
Expand Down
Loading