src/AdminBundle/Admin/References/VehicleGroupAdmin.php line 20

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Admin\References;
  3. use Exception;
  4. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  5. use AdminBundle\Admin\BaseAdmin;
  6. use CoreBundle\Model\Vehicles\VehicleType;
  7. use Sonata\AdminBundle\Datagrid\ListMapper;
  8. use Sonata\AdminBundle\Form\FormMapper;
  9. use Sonata\AdminBundle\Route\RouteCollection;
  10. use Sonata\AdminBundle\Route\RouteCollectionInterface;
  11. use Sonata\MediaBundle\Form\Type\MediaType;
  12. use Symfony\Component\Finder\Exception\AccessDeniedException;
  13. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  14. use Symfony\Component\Form\Extension\Core\Type\TextType;
  15. class VehicleGroupAdmin extends BaseAdmin
  16. {
  17.     /**
  18.      * @param RouteCollection $collection
  19.      */
  20.     protected function configureRoutes(RouteCollectionInterface $collection): void
  21.     {
  22.         $collection->remove('view');
  23.     }
  24.     public function prePersist($object): void
  25.     {
  26.         $this->prePare($object);
  27.     }
  28.     public function preUpdate($object): void
  29.     {
  30.         $this->prePare($object);
  31.         parent::preUpdate($object);
  32.     }
  33.     /**
  34.      * @param CarGroup $object
  35.      */
  36.     private function prePare($object) {
  37.         $user $this->getUser();
  38.         $dealer $user->getDealer();
  39.         if(!$dealer) {
  40.             throw new AccessDeniedException();
  41.         }
  42.         $object->setDealer($dealer);
  43.     }
  44.     /**
  45.      * @param FormMapper $formMapper
  46.      * @throws Exception
  47.      */
  48.     protected function configureFormFields(FormMapper $formMapper): void
  49.     {
  50.         $this->checkByRole(['ROLE_SUPER_ADMIN','ROLE_CONTENT_MANAGER','ROLE_DC_MANAGER']);
  51.         $User $this->getUser();
  52.         if(!$Dealer $User->getDealer()) {
  53.             throw new AccessDeniedException('User without dealer');
  54.         }
  55.         $formMapper
  56.             ->tab('Основная информация')
  57.                 ->with('Контент', ['class' => 'col-lg-6'])
  58.                     ->add('position'null, ['label' => 'Порядок вывода'])
  59.                     ->add('preview'MediaType::class, [
  60.                         'label' => 'Превью',
  61.                         'provider' => 'sonata.media.provider.image',
  62.                         'context'  => 'dc_site'
  63.                     ])
  64.                     ->add('vehicle_type'ChoiceType::class, [
  65.                         'label' => 'Тип техники',
  66.                         'choices' => array_flip(VehicleType::getTypes())
  67.                     ])
  68.                     ->add('inner_name'TextType::class, ['label' => 'Техническое название'], [])
  69.                     ->add('title_ru'TextType::class, ['label' => 'Название RU'], [])
  70.                     ->add('title_ua'TextType::class, ['label' => 'Название UA'], [])
  71.                     ->add('url'TextType::class, ['label' => 'url'], [])
  72.                 ->end()
  73.             ->end();
  74.     }
  75.     /**
  76.      * @param string $context
  77.      * @return ProxyQueryInterface
  78.      */
  79.     public function configureQuery($context 'list'): ProxyQueryInterface
  80.     {
  81.         $User $this->getUser();
  82.         $query parent::configureQuery($context);
  83.         $dealer $User->getDealer();
  84.         if(!$dealer) {
  85.             throw new AccessDeniedException();
  86.         }
  87.         $query->andWhere(
  88.             $query->expr()->eq($query->getRootAliases()[0].'.dealer'':dealer')
  89.         );
  90.         $query->setParameter('dealer'$dealer->getId());
  91.         return $query;
  92.     }
  93.     /**
  94.      * @param ListMapper $listMapper
  95.      */
  96.     protected function configureListFields(ListMapper $listMapper): void
  97.     {
  98.         $this->checkByRole(['ROLE_SUPER_ADMIN','ROLE_CONTENT_MANAGER','ROLE_DC_MANAGER']);
  99.         $listMapper->addIdentifier('id')
  100.             ->add('inner_name',null, ['label' => 'Техническое название'])
  101.             ->add('title_ru',null, ['label' => 'Название'])
  102.             ->add('vehicle_type','choice', ['label' => 'Тип техники''editable' => false'choices' => VehicleType::getTypes()])
  103.             ->add('_action','actions',[
  104.                 'label' => 'Действия',
  105.                 'actions' => [
  106.                     'edit' => [],
  107.                     'delete' => [],
  108.                 ]
  109.             ])
  110.         ;
  111.     }
  112. }