-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e11f7aa
commit d26f70d
Showing
6 changed files
with
142 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters