src/AdminBundle/Admin/SubAutoSite/ActionsAdmin.php line 16

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Admin\SubAutoSite;
  3. use CoreBundle\Entity\User;
  4. use CoreBundle\Entity\Vehicles\Vehicle;
  5. use ImporterBundle\Model\Post;
  6. use Sonata\AdminBundle\Datagrid\ListMapper;
  7. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  8. use Sonata\AdminBundle\Form\FormMapper;
  9. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  10. use Symfony\Component\Finder\Exception\AccessDeniedException;
  11. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  12. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  13. use Symfony\Component\Form\Extension\Core\Type\DateType;
  14. class ActionsAdmin extends NewsAdmin
  15. {
  16.     protected $baseRouteName 'admin_vendor_adminbundle_importer_actionsadmin';
  17.     protected $baseRoutePattern 'importer/action';
  18.     public function configureQuery($context 'list'): ProxyQueryInterface
  19.     {
  20.         $query parent::configureQuery($context);
  21.         /** @var User $User */
  22.         $User $this->security->getUser();
  23.         $query->where($query->getRootAliases()[0].'.post_type = 'Post::POST_TYPE_ACTIONS);
  24.         if(!$User->getSubDealer()) {
  25.             throw new AccessDeniedException('User without sub dealer');
  26.         }
  27.         $query->andWhere($query->getRootAliases()[0].'.dealer = '.$User->getSubDealer()->getId());
  28.         return $query;
  29.     }
  30.     /**
  31.      * @param FormMapper $formMapper
  32.      */
  33.     protected function configureFormFields(FormMapper $formMapper): void
  34.     {
  35.         $dealerOfSubDealer $this-security->getUser()->getSubDealer()->getBrand()->getDealer()->first();
  36.         $carsQuery $this->em
  37.             ->getRepository(Vehicle::class)
  38.             ->createQueryBuilder('c')
  39.             ->where('c.dealer = :dealer')->setParameter('dealer'$dealerOfSubDealer)
  40.             ->andWhere('c.is_used = 0')
  41.         ;
  42.         $formMapper
  43.             ->tab('Основная информация')
  44.                 ->with(' ', ['class' => 'col-lg-6 without-box-heder'])
  45.                     ->add('state'CheckboxType::class, ['label' => 'Показывать на сайте''required' => false])
  46.                     ->add('action_chapter'ChoiceType::class, ['label' => 'Раздел''choices' => [
  47.                         'Акции сервиса' => Post::ACTION_TYPE_SERVICE,
  48.                         'Акции авто' => Post::ACTION_TYPE_CAR
  49.                     ]])
  50.                     ->add('date_start'DateType::class, [
  51.                         'label' => 'Начало акции',
  52.                         'widget' => 'single_text'
  53.                     ])
  54.                     ->add('date_end'DateType::class, [
  55.                         'label' => 'Конец акции',
  56.                         'widget' => 'single_text'
  57.                     ])
  58.                     ->add('vehicles'EntityType::class, [
  59.                         'class' => Vehicle::class,
  60.                         'label' => 'Техника',
  61.                         'multiple' => true,
  62.                         'required' => false,
  63.                         'query_builder' => $carsQuery,
  64.                     ], [ 'admin_code' => 'admin.vehicles.passenger_vehicle'])
  65.                 ->end()
  66.             ->end()
  67.         ;
  68.         parent::configureFormFields($formMapper);
  69.     }
  70.     /**
  71.      * @param ListMapper $listMapper
  72.      */
  73.     protected function configureListFields(ListMapper $listMapper): void
  74.     {
  75.         parent::configureListFields($listMapper);
  76.         $listMapper->remove('_action');
  77.         $listMapper->add('date_end'null, ['label' => 'Дата окончания акции''format' => 'd-m-Y']);
  78.         $listMapper->add('action_chapter','choice', ['label' => 'Раздел''choices' => [
  79.                 Post::ACTION_TYPE_CAR => 'Акции авто',
  80.                 Post::ACTION_TYPE_SERVICE => 'Акции сервиса',
  81.             ]])
  82.             ->add('_action''actions', [
  83.                 'label' => 'Действия',
  84.                 'actions' => [
  85.                     'edit' => [],
  86.                 ]
  87.             ])
  88.         ;
  89.     }
  90.     /**
  91.      * @param \CoreBundle\Entity\Post $object
  92.      */
  93.     public function prePersist($object): void
  94.     {
  95.         parent::prePersist($object);
  96.         $object->setPostType(Post::POST_TYPE_ACTIONS);
  97.     }
  98. }