1 | <?php namespace App\Controller\Admin; |
||
2 | |||
3 | use App\Entity\Book; |
||
4 | use App\Entity\BookRevision; |
||
5 | use App\Form\Type\BookIsbnType; |
||
6 | use App\Form\Type\BookLinkType; |
||
7 | use App\Persistence\BookRepository; |
||
8 | use App\Persistence\TextRepository; |
||
9 | use App\Service\ContentService; |
||
10 | use Doctrine\ORM\EntityManagerInterface; |
||
11 | use EasyCorp\Bundle\EasyAdminBundle\Config\Action; |
||
12 | use EasyCorp\Bundle\EasyAdminBundle\Config\Actions; |
||
13 | use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; |
||
14 | use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext; |
||
15 | use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField; |
||
16 | use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField; |
||
17 | use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField; |
||
18 | use EasyCorp\Bundle\EasyAdminBundle\Field\CodeEditorField; |
||
19 | use EasyCorp\Bundle\EasyAdminBundle\Field\Field; |
||
20 | use EasyCorp\Bundle\EasyAdminBundle\Field\FormField; |
||
21 | use EasyCorp\Bundle\EasyAdminBundle\Field\IdField; |
||
22 | use EasyCorp\Bundle\EasyAdminBundle\Field\SlugField; |
||
23 | use Symfony\Component\HttpFoundation\Request; |
||
24 | |||
25 | class BookCrudController extends CrudController { |
||
26 | |||
27 | const ACTION_UPDATE_COVER = 'updateCover'; |
||
28 | |||
29 | protected static $entityClass = Book::class; |
||
30 | |||
31 | private $textRepository; |
||
32 | |||
33 | public function __construct(TextRepository $textRepository) { |
||
34 | $this->textRepository = $textRepository; |
||
35 | } |
||
36 | |||
37 | public function configureActions(Actions $actions): Actions { |
||
38 | $actions->disable(Action::NEW, Action::DELETE); |
||
39 | |||
40 | $updateCover = Action::new(self::ACTION_UPDATE_COVER, 'Корица от БМ', 'fa fa-image') |
||
41 | ->linkToCrudAction(self::ACTION_UPDATE_COVER) |
||
42 | ->displayIf(function(Book $book): bool { |
||
43 | return $book->getBibliomanId() !== null; |
||
44 | }); |
||
45 | $viewInFrontend = $this->viewInFrontendAction('book_show', function(Book $book): array { |
||
46 | return ['id' => $book->getId()]; |
||
47 | }); |
||
48 | $actions->add(Crud::PAGE_INDEX, $viewInFrontend); |
||
49 | $actions->add(Crud::PAGE_INDEX, $updateCover); |
||
50 | return $actions; |
||
51 | } |
||
52 | |||
53 | public function configureFields(string $pageName): iterable { |
||
54 | yield FormField::addTab('General attributes'); |
||
55 | yield IdField::new('id')->hideOnForm(); |
||
56 | yield IdField::new('bibliomanId')->setTemplatePath('Admin/Book/list_biblioman.html.twig'); |
||
57 | yield Field::new('title')->setColumns(6); |
||
58 | yield SlugField::new('slug')->setTargetFieldName('title')->setColumns(6)->hideOnIndex(); |
||
59 | yield Field::new('titleAuthor')->hideOnForm(); |
||
60 | yield $this->associationFieldWithManyItems('authors')->hideOnIndex(); |
||
61 | yield Field::new('year')->hideOnIndex(); |
||
62 | yield AssociationField::new('lang')->setColumns(6)->hideOnIndex(); |
||
63 | yield AssociationField::new('origLang')->setColumns(6)->hideOnIndex(); |
||
64 | yield ChoiceField::new('type')->setChoices($this->getTranslation()->getBookTypeChoices()); |
||
65 | |||
66 | yield FormField::addTab('Extra attributes'); |
||
67 | yield Field::new('subtitle')->setColumns(6)->hideOnIndex(); |
||
68 | yield Field::new('titleExtra')->setColumns(6)->hideOnIndex(); |
||
69 | yield Field::new('origTitle')->hideOnIndex(); |
||
70 | yield $this->associationFieldWithManyItems('sequence')->setColumns(6)->hideOnIndex(); |
||
71 | yield Field::new('seqnr')->setColumns(6)->hideOnIndex(); |
||
72 | yield $this->associationFieldWithManyItems('category')->hideOnIndex(); |
||
73 | yield $this->collectionField('isbns', BookIsbnType::class)->hideOnIndex(); |
||
74 | yield $this->collectionField('links', BookLinkType::class, 'Site Links')->hideOnIndex(); |
||
75 | |||
76 | yield FormField::addTab('Textual content'); |
||
77 | yield CodeEditorField::new('rawTemplate', 'Template')->hideOnIndex(); |
||
78 | yield CodeEditorField::new('annotation')->hideOnIndex(); |
||
79 | yield CodeEditorField::new('extraInfo')->hideOnIndex(); |
||
80 | yield ChoiceField::new('formats') |
||
81 | ->setChoices(array_combine(Book::FORMATS, Book::FORMATS)) |
||
82 | ->allowMultipleChoices() |
||
83 | ->renderExpanded() |
||
84 | ->setFormTypeOptions(['choice_translation_domain' => false]) |
||
85 | ->hideOnIndex(); |
||
86 | yield Field::new('revisionComment')->setHelp('help.book.revisionComment')->hideOnIndex(); |
||
87 | yield Field::new('removedNotice')->hideOnIndex(); |
||
88 | |||
89 | if ($pageName === Action::EDIT) { |
||
90 | $coverHtml = $this->generateCoverElementsForEdit($this->getContext()->getEntity()->getInstance()); |
||
91 | if (!empty($coverHtml)) { |
||
92 | yield FormField::addTab('Cover'); |
||
93 | yield BooleanField::new('hasCover', false)->setDisabled()->setHelp($coverHtml); |
||
94 | } |
||
95 | } |
||
96 | } |
||
97 | |||
98 | protected function generateCoverElementsForEdit(Book $book): string { |
||
99 | $html = ''; |
||
100 | if ($book->hasCover()) { |
||
101 | $html .= '<img src="/' . ContentService::getCover($book->getId(), 300) . '">'; |
||
102 | } |
||
103 | if ($book->getBibliomanId()) { |
||
104 | $updateCoverUrl = $this->adminUrlGenerator() |
||
105 | ->setAction(self::ACTION_UPDATE_COVER) |
||
106 | ->setEntityId($book->getId()) |
||
107 | ->generateUrl(); |
||
108 | $html .= '<br><br><a href="'.$updateCoverUrl.'" class="btn btn-primary update_cover_link" title="Копиране на корицата от Библиоман"><span class="fa fa-image"></span> Корица от БМ</a>'; |
||
109 | } |
||
110 | return $html; |
||
111 | } |
||
112 | |||
113 | public function updateCover(AdminContext $context, Request $request, BookRepository $bookRepository) { |
||
114 | $book = $context->getEntity()->getInstance();/* @var $book Book */ |
||
115 | |||
116 | if (!$book) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
117 | throw $this->createNotFoundException('Unable to locate a book'); |
||
118 | } |
||
119 | |||
120 | if ($request->isMethod(Request::METHOD_POST)) { |
||
121 | ContentService::copyCoverFromBiblioman($book); |
||
122 | $book->setHasCover(true); |
||
123 | $bookRepository->save($book); |
||
124 | |||
125 | $this->addFlash('success', "Корицата на „{$book}“ беше обновена."); |
||
126 | |||
127 | return $this->redirectToIndex($context); |
||
128 | } |
||
129 | return $this->render('Admin/Book/update_cover.html.twig', [ |
||
130 | 'book' => $book, |
||
131 | ]); |
||
132 | } |
||
133 | |||
134 | public function updateEntity(EntityManagerInterface $entityManager, $entityInstance): void { |
||
135 | $this->preUpdate($entityInstance); |
||
136 | parent::updateEntity($entityManager, $entityInstance); |
||
137 | $this->postUpdate($entityInstance); |
||
138 | } |
||
139 | |||
140 | private function preUpdate(Book $book) { |
||
141 | if ($book->textsNeedUpdate()) { |
||
142 | $texts = $this->textRepository->findByIds($book->getTextIdsFromTemplate()); |
||
143 | $book->setTexts($texts); |
||
144 | } |
||
145 | if ($book->getRevisionComment()) { |
||
146 | $revision = new BookRevision; |
||
147 | $revision->setComment($book->getRevisionComment()); |
||
148 | $revision->setBook($book); |
||
149 | $revision->setDate(new \DateTime); |
||
150 | $revision->setFirst(false); |
||
151 | $book->addRevision($revision); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | private function postUpdate(Book $book) { |
||
156 | $book->persistAnnotation(); |
||
157 | $book->persistExtraInfo(); |
||
158 | } |
||
159 | } |
||
160 |