Skip to content

Commit

Permalink
edit author or tags
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioMendolia committed Oct 13, 2023
1 parent e11f7aa commit d26f70d
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 23 deletions.
11 changes: 5 additions & 6 deletions src/Entity/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Form/BookFilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 7 additions & 6 deletions src/Repository/BookRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
}
}
Expand Down
84 changes: 84 additions & 0 deletions src/Twig/InlineEditGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace App\Twig;

use App\Entity\Book;
use App\Repository\BookRepository;
use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\ComponentToolsTrait;
use Symfony\UX\LiveComponent\DefaultActionTrait;
use Symfony\UX\LiveComponent\ValidatableComponentTrait;

#[AsLiveComponent()]
class InlineEditGroup extends AbstractController
{
use DefaultActionTrait;
use ValidatableComponentTrait;
use ComponentToolsTrait;

#[LiveProp()]
public string $existingValue;

#[LiveProp(writable: true)]
public string $fieldValue;

#[LiveProp()]
public bool $isEditing = false;

#[LiveProp()]
public string $field;

public ?string $flashMessage = null;


#[LiveAction]
public function activateEditing(): void
{
$this->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';
}
}
38 changes: 38 additions & 0 deletions templates/components/InlineEditGroup.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<span {{ attributes.defaults(stimulus_controller('inline-edit-group')) }}>
{% if isEditing and is_granted('ROLE_ADMIN') %}
<form class="input-group">
<select
class="form-select form-select-sm"
id="books_{{ field }}"
data-model="fieldValue"
{{ stimulus_controller('symfony/ux-autocomplete/autocomplete', {
url: path('app_autocomplete_group', {type: field}),
}) }}
>
<option value="{{ fieldValue }}" selected>{{ fieldValue }}</option>
</select>
<button
data-action="live#action"
data-action-name="prevent|save"
class="btn btn-sm btn-outline-danger"
>
<i class="bi bi-journal-check"></i> Rename {{ field }}
</button>
</form>
{% elseif is_granted('ROLE_ADMIN') %}

<button
data-action="live#action"
data-action-name="activateEditing"
class="btn btn-sm btn-link d-inline-block"
title="Click to edit!"
>
<i class="bi bi-journal-check"></i>
</button>

{% if flashMessage %}
<i class="bi bi-check alert-remove"></i>
{% endif %}
{% endif %}

</span>
15 changes: 6 additions & 9 deletions templates/group/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,20 @@
</form>

{{ knp_pagination_render(pagination) }}

<div style="column-count: 3">
<ul class="list-group list-group-flush">
<div class="row">
{% for item in pagination %}
{% set status = ((item.booksFinished/(item.bookCount+0.00001))*100)|round %}
{% set class = status==100?'success':(status>0?'warning':'secondary') %}
<li class="list-group-item list-group-item-{{ class }} p-0 border-1" >
<div class="bg-{{ class }}-subtle col-md-4 p-2 border-3 border-{{ class }}">
{% set params = { (type): item.item } %}
<a href="{{ filter_book_url(params) }}" class="d-block p-3">
<a href="{{ filter_book_url(params) }}" class="text-decoration-none">
{{ item.item }}
<span class="badge bg-{{ class }} rounded-pill">{{ item.booksFinished }}/{{ item.bookCount }}</span>
</a>
</li>
{{ component('InlineEditGroup', {'existingValue':item.item,'fieldValue':item.item, 'field':type}) }}
</div>
{% endfor %}
</ul>
</div>

</div>
{{ knp_pagination_render(pagination) }}

{% endblock %}

0 comments on commit d26f70d

Please sign in to comment.