src/AdminBundle/Admin/ProjectAdmin.php line 15

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Admin;
  3. use AdminBundle\Form\Type\ContentType;
  4. use DcSiteBundle\Entity\Project;
  5. use DcSiteBundle\Entity\ProjectContent;
  6. use Sonata\AdminBundle\Datagrid\ListMapper;
  7. use Sonata\AdminBundle\Form\FormMapper;
  8. use Sonata\AdminBundle\Route\RouteCollectionInterface;
  9. use Sonata\AdminBundle\Form\Type\ModelListType;
  10. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  11. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  12. class ProjectAdmin extends BaseAdmin
  13. {
  14.     protected function configureRoutes(RouteCollectionInterface $collection): void
  15.     {
  16.     }
  17.     /**
  18.      * @param Project $object
  19.      */
  20.     public function prePersist($object): void
  21.     {
  22.         $this->preSave($object);
  23.         $object->setUrl($this->slugify($object->getUrl()));
  24.         $User $this->security->getUser();
  25.         if(!$User->getDealer() && !$object->getDealer()) {
  26.             throw new AccessDeniedException('User without dealer');
  27.         }
  28.         /** @var ProjectContent $content */
  29.         foreach ($object->getContent() as $content) {
  30.             $content->setProject($object);
  31.         }
  32.         $object->setDealer($User->getDealer());
  33.     }
  34.     public function preUpdate($object): void
  35.     {
  36.         $this->preSave($object);
  37.         $object->setUrl($this->slugify($object->getUrl()));
  38.         parent::preUpdate($object);
  39.     }
  40.     private function preSave($object): void
  41.     {
  42.         if (empty($object->getUrl())) {
  43.             /** @var ProjectContent $content */
  44.             if (count($object->getContent()) > 0) {
  45.                 $content $object->getContent()[0];
  46.                 $object->setUrl(strtolower($content->getH1()));
  47.             }
  48.         }
  49.     }
  50.     protected function configureBatchActions($actions): array
  51.     {
  52.         return [];
  53.     }
  54.     protected function configureFormFields(FormMapper $formMapper): void
  55.     {
  56.         $formMapper
  57.             ->tab('Контент')
  58.             ->with('Контент', ['class' => 'col-lg-12'])
  59.             ->add('url'null, ['label' => 'URL'])
  60.             ->add('content'ContentType::class, ['label' => false], [
  61.                 'edit' => 'inline',
  62.                 'sortable' => 'position',
  63.                 'admin_code' => 'admin.vehicles.special_projects_content',
  64.             ])
  65.             ->end()
  66.             ->end()
  67.             ->tab('Галерея')
  68.             ->add('is_first_video',ChoiceType::class, ['label' => 'Що перше показувати''choices' => array_flip([
  69.                 => 'Відео',
  70.                 => 'Фото',
  71.                 null => 'Фото',
  72.             ])])
  73.             ->add('gallery'ModelListType::class, [
  74.                 'label' => 'Галерея',
  75.                 'btn_list' => false,
  76.                 'required' => false,
  77.             ], [
  78.                 'edit' => 'inline',
  79.                 'inline' => 'table',
  80.                 'sortable' => 'position',
  81.                 'link_parameters' => [
  82.                     'context' => 'dc_car_gallery',
  83.                     'provider' => 'sonata.media.provider.image'
  84.                 ],
  85.                 'admin_code' => 'sonata.media.admin.gallery',
  86.             ])
  87.             ->add('video'ModelListType::class, [
  88.                 'label' => 'Галерея видео',
  89.                 'btn_list' => false,
  90.                 'required' => false,
  91.             ], [
  92.                 'edit' => 'inline',
  93.                 'inline' => 'table',
  94.                 'sortable' => 'position',
  95.                 'link_parameters' => [
  96.                     'context' => 'dc_car_video',
  97.                     'provider' => 'sonata.media.provider.youtube'
  98.                 ],
  99.                 'admin_code' => 'sonata.media.admin.gallery',
  100.             ])
  101.             ->end()
  102.             ->end()
  103.         ;
  104.     }
  105.     /**
  106.      * @param ListMapper $listMapper
  107.      */
  108.     protected function configureListFields(ListMapper $listMapper): void
  109.     {
  110.         $listMapper->addIdentifier('id')
  111.             ->add('title',null, ['label' => 'Назва проекту'])
  112.             ->add('_action''actions', [
  113.                 'label' => 'Действия',
  114.                 'actions' => [
  115.                     'edit' => [],
  116.                     'delete' => [],
  117.                 ]
  118.             ])
  119.         ;
  120.     }
  121. }