Skip to content

Commit

Permalink
Guess fields from calibre
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioMendolia committed Sep 4, 2023
1 parent bc3c021 commit fae4788
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
71 changes: 71 additions & 0 deletions src/Twig/FieldGuesser.php
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 '';
}
}
3 changes: 2 additions & 1 deletion templates/book/_list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
<div>
{{ component('InlineEditBook', {'book':book, 'field':'serie'}) }}

{{ component('InlineEditBook', {'book':book, 'field':'serieIndex'}) }}
<div class="w-25"> {{ component('InlineEditBook', {'book':book, 'field':'serieIndex'}) }}</div>
{{ component('FieldGuesser',{'book':book}) }}
</div>
</td>
<td>
Expand Down
18 changes: 18 additions & 0 deletions templates/components/FieldGuesser.html.twig
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>

0 comments on commit fae4788

Please sign in to comment.