From d26f70dc924a5df97748a15967e1d408406b3afe Mon Sep 17 00:00:00 2001 From: Sergio Mendolia Date: Fri, 13 Oct 2023 14:10:53 +0200 Subject: [PATCH] edit author or tags --- src/Entity/Book.php | 11 ++- src/Form/BookFilterType.php | 4 +- src/Repository/BookRepository.php | 13 +-- src/Twig/InlineEditGroup.php | 84 +++++++++++++++++++ .../components/InlineEditGroup.html.twig | 38 +++++++++ templates/group/index.html.twig | 15 ++-- 6 files changed, 142 insertions(+), 23 deletions(-) create mode 100644 src/Twig/InlineEditGroup.php create mode 100644 templates/components/InlineEditGroup.html.twig diff --git a/src/Entity/Book.php b/src/Entity/Book.php index 48a531f5..8948e2b3 100644 --- a/src/Entity/Book.php +++ b/src/Entity/Book.php @@ -328,8 +328,9 @@ public function addTag(string $tag): static if (null === $this->tags) { $this->tags = []; } - if (!in_array($tag, $this->tags, true)) { - $this->tags[] = $tag; + + if (!in_array($tag, $this->tags, true) && '' !== $tag) { + $this->tags[] = ucwords(strtolower($tag), self::UCWORDS_SEPARATORS); } $this->tags = array_values($this->tags); @@ -338,14 +339,12 @@ public function addTag(string $tag): static public function removeTag(string $tag): static { - if (null === $this->tags) { - $this->tags = []; - } foreach ($this->tags as $key => $value) { - if ($value === $tag) { + if (strtolower($value) === strtolower($tag)) { unset($this->tags[$key]); } } + $this->tags = array_values($this->tags); return $this; diff --git a/src/Form/BookFilterType.php b/src/Form/BookFilterType.php index 852b5812..d5c3c0ae 100644 --- a/src/Form/BookFilterType.php +++ b/src/Form/BookFilterType.php @@ -123,8 +123,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void if ($tag === 'no_tags') { $orModule->add('book.tags = \'[]\''); } else { - $orModule->add('JSON_CONTAINS(book.tags, :tag'.$key.')=1'); - $qb->setParameter('tag'.$key, json_encode([$tag])); + $orModule->add('JSON_CONTAINS(lower(book.tags), :tag'.$key.')=1'); + $qb->setParameter('tag'.$key, json_encode([strtolower($tag)])); } } $qb->andWhere($orModule); diff --git a/src/Repository/BookRepository.php b/src/Repository/BookRepository.php index c7cabf28..427e5259 100644 --- a/src/Repository/BookRepository.php +++ b/src/Repository/BookRepository.php @@ -139,17 +139,18 @@ private function convertResults(mixed $intermediateResults): array $results = []; foreach ($intermediateResults as $result) { foreach ($result['item'] as $item) { + $key = ucwords(strtolower($item), Book::UCWORDS_SEPARATORS); if (!array_key_exists($item, $results)) { - $results[$item] = [ - 'item' => ucwords(strtolower($item), Book::UCWORDS_SEPARATORS), + $results[$key] = [ + 'item' => $key, 'bookCount' => 0, 'booksFinished' => 0, ]; } - $results[$item] = [ - 'item' => ucwords(strtolower($item), Book::UCWORDS_SEPARATORS), - 'bookCount' => $result['bookCount'] + $results[$item]['bookCount'], - 'booksFinished' => $result['booksFinished'] + $results[$item]['booksFinished'], + $results[$key] = [ + 'item' => $key, + 'bookCount' => $result['bookCount'] + $results[$key]['bookCount'], + 'booksFinished' => $result['booksFinished'] + $results[$key]['booksFinished'], ]; } } diff --git a/src/Twig/InlineEditGroup.php b/src/Twig/InlineEditGroup.php new file mode 100644 index 00000000..07292e4c --- /dev/null +++ b/src/Twig/InlineEditGroup.php @@ -0,0 +1,84 @@ +isEditing = true; + } + + /** + * @throws \JsonException + */ + #[LiveAction] + public function save(BookRepository $bookRepository, EntityManagerInterface $entityManager): void + { + + $qb = $bookRepository->createQueryBuilder('book') + ->select('book'); + $qb->andWhere('JSON_CONTAINS(lower(book.'.$this->field.'), :value)=1'); + $qb->setParameter('value', json_encode([strtolower($this->existingValue)], JSON_THROW_ON_ERROR)); + + $books = $qb->getQuery()->getResult(); + + foreach ($books as $book) { + switch ($this->field) { + case 'authors': + $book->removeAuthor($this->existingValue); + if($this->fieldValue !== ''){ + $book->addAuthor($this->fieldValue); + } + break; + case 'tags': + $book->removeTag($this->existingValue); + if($this->fieldValue !== ''){ + $book->addTag($this->fieldValue); + } + break; + default: + throw new \RuntimeException('Field not implemented for group edition'); + } + } + $entityManager->flush(); + + $this->dispatchBrowserEvent('manager:flush'); + $this->isEditing = false; + + $this->flashMessage = ' book updated'; + } +} diff --git a/templates/components/InlineEditGroup.html.twig b/templates/components/InlineEditGroup.html.twig new file mode 100644 index 00000000..a8821e3a --- /dev/null +++ b/templates/components/InlineEditGroup.html.twig @@ -0,0 +1,38 @@ + +{% if isEditing and is_granted('ROLE_ADMIN') %} +
+ + +
+{% elseif is_granted('ROLE_ADMIN') %} + + + + {% if flashMessage %} + + {% endif %} +{% endif %} + +
diff --git a/templates/group/index.html.twig b/templates/group/index.html.twig index 571c5586..a87eabde 100644 --- a/templates/group/index.html.twig +++ b/templates/group/index.html.twig @@ -16,23 +16,20 @@ {{ knp_pagination_render(pagination) }} - -
- -
- + {{ knp_pagination_render(pagination) }} {% endblock %}