PiedWeb /
CMS
| 1 | <?php |
||
| 2 | |||
| 3 | namespace PiedWeb\CMSBundle\Extension\Admin; |
||
| 4 | |||
| 5 | use Sonata\AdminBundle\Admin\AbstractAdmin; |
||
| 6 | use Sonata\AdminBundle\Datagrid\DatagridMapper; |
||
| 7 | use Sonata\AdminBundle\Datagrid\ListMapper; |
||
| 8 | use Sonata\AdminBundle\Form\FormMapper; |
||
| 9 | use Sonata\AdminBundle\Object\Metadata; //use Sonata\BlockBundle\Meta\Metadata; |
||
| 10 | use Symfony\Component\Form\Extension\Core\Type\FileType; |
||
| 11 | use Symfony\Component\Form\Extension\Core\Type\TextType; |
||
| 12 | |||
| 13 | class MediaAdmin extends AbstractAdmin |
||
| 14 | { |
||
| 15 | use AdminTrait; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 16 | |||
| 17 | protected $datagridValues = [ |
||
| 18 | '_page' => 1, |
||
| 19 | '_sort_order' => 'DESC', |
||
| 20 | '_sort_by' => 'updatedAt', |
||
| 21 | ]; |
||
| 22 | |||
| 23 | private $liipImage; |
||
| 24 | private $relatedPages; |
||
| 25 | |||
| 26 | public function setLiipImage($liipImage) |
||
| 27 | { |
||
| 28 | $this->liipImage = $liipImage; |
||
| 29 | } |
||
| 30 | |||
| 31 | protected function configureFormFields(FormMapper $formMapper) |
||
| 32 | { |
||
| 33 | $media = $this->getSubject(); |
||
| 34 | |||
| 35 | $formMapper->with('Media', ['class' => 'col-md-6']) |
||
| 36 | |||
| 37 | ->add('mediaFile', FileType::class, [ |
||
| 38 | 'label' => 'admin.media.mediaFile.label', |
||
| 39 | 'required' => $this->getSubject() && $this->getSubject()->getMedia() ? false : true, |
||
| 40 | ]) |
||
| 41 | ->add('name', TextType::class, [ |
||
| 42 | 'required' => $this->getSubject() && $this->getSubject()->getMedia() ? true : false, |
||
| 43 | 'help_html' => true, |
||
| 44 | 'help' => 'admin.media.name.help', |
||
| 45 | 'label' => 'admin.media.name.label', |
||
| 46 | 'attr' => ['ismedia' => 1, 'class' => 'col-md-6'], |
||
| 47 | ]) |
||
| 48 | ->add('slugForce', TextType::class, [ |
||
| 49 | 'label' => 'admin.page.slug.label', |
||
| 50 | 'help_html' => true, |
||
| 51 | 'required' => false, |
||
| 52 | 'help' => $this->getSubject() && $this->getSubject()->getSlug() |
||
| 53 | ? '<span class="btn btn-link" onclick="toggleDisabled()" id="disabledLinkSlug"> |
||
| 54 | <i class="fa fa-unlock"></i></span> |
||
| 55 | <script>function toggleDisabled() { |
||
| 56 | $(".slug_disabled").first().removeAttr("disabled"); |
||
| 57 | $(".slug_disabled").first().focus(); |
||
| 58 | $("#disabledLinkSlug").first().remove(); |
||
| 59 | }</script>' |
||
| 60 | .'<small>Changer le slug change l\'URL de l\'image et peut créer des erreurs.</small>' |
||
| 61 | : 'admin.page.slug.help', |
||
| 62 | 'attr' => [ |
||
| 63 | 'class' => 'slug_disabled', |
||
| 64 | ($this->getSubject() ? ($this->getSubject()->getSlug() ? 'disabled' : 't') : 't') => '', |
||
| 65 | ], |
||
| 66 | ]) |
||
| 67 | ->end(); |
||
| 68 | |||
| 69 | $formMapper->with('i18n', ['class' => 'col-md-6']); |
||
| 70 | |||
| 71 | $formMapper->add('names', null, [ |
||
| 72 | 'required' => false, |
||
| 73 | 'help_html' => true, 'help' => 'admin.media.names.help', |
||
| 74 | 'label' => 'admin.media.names.label', |
||
| 75 | 'attr' => ['ismedia' => 1, 'class' => 'col-md-6'], |
||
| 76 | ]); |
||
| 77 | |||
| 78 | $formMapper->end(); |
||
| 79 | |||
| 80 | if ($media && $media->getMedia()) { |
||
| 81 | $formMapper->with('admin.media.preview.label', [ |
||
| 82 | 'class' => 'col-md-12', |
||
| 83 | 'description' => $this->showMediaPreview(), |
||
| 84 | //'empty_message' => false, // to uncomment when sonataAdmin 3.62 is released |
||
| 85 | ])->end(); |
||
| 86 | |||
| 87 | if ($this->issetRelatedPages()) { |
||
| 88 | $formMapper->with('admin.media.related.label', [ |
||
| 89 | 'class' => 'col-md-12', |
||
| 90 | 'description' => $this->showRelatedPages(), |
||
| 91 | //'empty_message' => false, /// to uncomment when sonataAdmin 3.62 is released |
||
| 92 | ])->end(); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | protected function showMediaPreview(): string |
||
| 98 | { |
||
| 99 | $media = $this->getSubject(); |
||
| 100 | |||
| 101 | $template = false !== strpos($media->getMimeType(), 'image/') ? |
||
| 102 | '@pwcAdmin/media_show.preview_image.html.twig' |
||
| 103 | : '@pwcAdmin/media_show.preview.html.twig'; |
||
| 104 | |||
| 105 | return $this->twig->render( |
||
| 106 | $template, |
||
| 107 | [ |
||
| 108 | 'media' => $media, |
||
| 109 | ] |
||
| 110 | ); |
||
| 111 | } |
||
| 112 | |||
| 113 | protected function issetRelatedPages(): bool |
||
| 114 | { |
||
| 115 | $relatedPages = $this->getRelatedPages(); |
||
| 116 | |||
| 117 | if (! empty($relatedPages['content']) |
||
| 118 | || $relatedPages['gallery']->count() > 0 |
||
| 119 | || $relatedPages['mainImage']->count() > 0 |
||
| 120 | ) { |
||
| 121 | return true; |
||
| 122 | } else { |
||
| 123 | return false; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | protected function getRelatedPages(): ?array |
||
| 128 | { |
||
| 129 | if (null !== $this->relatedPages) { |
||
| 130 | return $this->relatedPages; |
||
| 131 | } |
||
| 132 | |||
| 133 | $media = $this->getSubject(); |
||
| 134 | |||
| 135 | $pages = $this->em |
||
| 136 | ->getRepository($this->pageClass) |
||
| 137 | ->getPagesUsingMedia($this->liipImage->getBrowserPath($media->getFullPath(), 'default')); |
||
| 138 | |||
| 139 | $this->relatedPages = [ |
||
| 140 | 'content' => $pages, |
||
| 141 | 'gallery' => $media->getPageHasMedias(), |
||
| 142 | 'mainImage' => $media->getMainImagePages(), |
||
| 143 | ]; |
||
| 144 | |||
| 145 | return $this->relatedPages; |
||
| 146 | } |
||
| 147 | |||
| 148 | protected function showRelatedPages(): string |
||
| 149 | { |
||
| 150 | return $this->twig->render( |
||
| 151 | '@pwcAdmin/media_show.relatedPages.html.twig', |
||
| 152 | $this->getRelatedPages() |
||
| 153 | ); |
||
| 154 | } |
||
| 155 | |||
| 156 | protected function configureDatagridFilters(DatagridMapper $datagridMapper) |
||
| 157 | { |
||
| 158 | $datagridMapper->add('name', null, [ |
||
| 159 | 'label' => 'admin.media.name.label', |
||
| 160 | ]); |
||
| 161 | $datagridMapper->add('names', null, [ |
||
| 162 | 'label' => 'admin.media.names.label', |
||
| 163 | ]); |
||
| 164 | } |
||
| 165 | |||
| 166 | protected function configureListFields(ListMapper $listMapper) |
||
| 167 | { |
||
| 168 | $this->setMosaicDefaultListMode(); |
||
| 169 | |||
| 170 | $listMapper->add('name', null, [ |
||
| 171 | 'label' => 'admin.media.name.label', |
||
| 172 | ]); |
||
| 173 | $listMapper->add('createdAt', null, [ |
||
| 174 | 'label' => 'admin.media.createdAt.label', |
||
| 175 | 'format' => 'd/m/y', |
||
| 176 | ]); |
||
| 177 | $listMapper->add('mainColor', null, [ |
||
| 178 | 'label' => 'admin.media.mainColor.label', |
||
| 179 | ]); |
||
| 180 | $listMapper->add('_action', null, [ |
||
| 181 | 'actions' => [ |
||
| 182 | 'edit' => [], |
||
| 183 | 'delete' => [], |
||
| 184 | ], |
||
| 185 | ]); |
||
| 186 | } |
||
| 187 | |||
| 188 | public function getObjectMetadata($media) |
||
| 189 | { |
||
| 190 | if (false !== strpos($media->getMimeType(), 'image/')) { |
||
| 191 | $fullPath = '/'.$media->getRelativeDir().'/'.$media->getMedia(); |
||
| 192 | $thumb = $this->liipImage->getBrowserPath($fullPath, 'thumb'); |
||
| 193 | } else { |
||
| 194 | $thumb = self::$thumb; |
||
| 195 | } |
||
| 196 | |||
| 197 | return new Metadata($media->getName(), null, $thumb); |
||
| 198 | } |
||
| 199 | } |
||
| 200 |