-
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
bc3c021
commit fae4788
Showing
3 changed files
with
91 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace App\Twig; | ||
|
||
use App\Entity\Book; | ||
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 FieldGuesser extends AbstractController | ||
{ | ||
use DefaultActionTrait; | ||
use ValidatableComponentTrait; | ||
use ComponentToolsTrait; | ||
|
||
#[LiveProp()] | ||
public Book $book; | ||
|
||
public ?string $flashMessage = null; | ||
|
||
#[LiveAction] | ||
public function accept(EntityManagerInterface $entityManager): void | ||
{ | ||
$this->book->setSerie($this->guessSerie()); | ||
$this->book->setSerieIndex((float) $this->guessIndex()); | ||
$this->book->setMainAuthor($this->guessAuthor()); | ||
$entityManager->flush(); | ||
$this->dispatchBrowserEvent('manager:flush'); | ||
|
||
$this->flashMessage = 'Saved'; | ||
} | ||
|
||
public function guessSerie(): string | ||
{ | ||
$author = $this->book->getMainAuthor(); | ||
$parts = explode(' - ', $author); | ||
if (3 === count($parts)) { | ||
return $parts[1]; | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
public function guessIndex(): string | ||
{ | ||
$author = $this->book->getMainAuthor(); | ||
$parts = explode(' - ', $author); | ||
if (3 === count($parts)) { | ||
return $parts[2]; | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
public function guessAuthor(): string | ||
{ | ||
$author = $this->book->getMainAuthor(); | ||
$parts = explode(' - ', $author); | ||
if (3 === count($parts)) { | ||
return $parts[0]; | ||
} | ||
|
||
return ''; | ||
} | ||
} |
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,18 @@ | ||
<span {{ attributes.defaults(stimulus_controller('inline-edit-book')) }}> | ||
{% if this.guessAuthor!='' %} | ||
<form> | ||
<button | ||
data-action="live#action" | ||
data-action-name="prevent|accept" | ||
class="btn btn-sm btn-outline-success" | ||
> | ||
Author: {{ this.guessAuthor }} // | ||
Serie:{{ this.guessSerie }} // | ||
Index: {{ this.guessIndex }} | ||
|
||
|
||
<i class="bi bi-check"></i> | ||
</button> | ||
</form> | ||
{% endif %} | ||
</span> |