src/AdminBundle/Admin/References/EngineAdmin.php line 17

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Admin\References;
  3. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  4. use Exception;
  5. use AdminBundle\Admin\BaseAdmin;
  6. use DcSiteBundle\Entity\Engine;
  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 Symfony\Component\Finder\Exception\AccessDeniedException;
  12. class EngineAdmin extends BaseAdmin
  13. {
  14.     /**
  15.      * @param RouteCollection $collection
  16.      */
  17.     protected function configureRoutes(RouteCollectionInterface $collection): void
  18.     {
  19.         $collection->remove('view');
  20.     }
  21.     public function prePersist($object): void
  22.     {
  23.         $this->prePare($object);
  24.     }
  25.     public function preUpdate($object): void
  26.     {
  27.         $this->prePare($object);
  28.     }
  29.     /**
  30.      * @param Engine $object
  31.      */
  32.     private function prePare($object) {
  33.         $user $this->getUser();
  34.         $dealer $user->getDealer();
  35.         if(!$dealer) {
  36.             throw new AccessDeniedException();
  37.         }
  38.         $object->setDealer($dealer);
  39.     }
  40.     /**
  41.      * @param string $context
  42.      * @return ProxyQueryInterface
  43.      */
  44.     public function configureQuery($context 'list'): ProxyQueryInterface
  45.     {
  46.         $user $this->getUser();
  47.         $query parent::configureQuery($context);
  48.         $dealer $user->getDealer();
  49.         if(!$dealer) {
  50.             throw new AccessDeniedException();
  51.         }
  52.         $query->andWhere(
  53.             $query->expr()->eq($query->getRootAliases()[0].'.dealer',':dealer')
  54.         );
  55.         $query->setParameter('dealer'$dealer->getId());
  56.         return $query;
  57.     }
  58.     /**
  59.      * @param FormMapper $formMapper
  60.      * @throws Exception
  61.      */
  62.     protected function configureFormFields(FormMapper $formMapper): void
  63.     {
  64.         $User $this->getUser();
  65.         if(!$Dealer $User->getDealer()) {
  66.             throw new AccessDeniedException('User without dealer');
  67.         }
  68.         $formMapper
  69.             ->with('Контент', ['class' => 'col-lg-6'])
  70.                 ->add('name'null, ['label' => 'Полное название'])
  71.                 ->add('capacity'null, ['label' => 'Объем двигателя'])
  72.             ->end();
  73.     }
  74.     /**
  75.      * @param ListMapper $listMapper
  76.      */
  77.     protected function configureListFields(ListMapper $listMapper): void
  78.     {
  79.         $listMapper->addIdentifier('id')
  80.             ->add('name',null, ['label' => 'Название'])
  81.             ->add('capacity',null, ['label' => 'Объем'])
  82.             ->add('_action','actions',[
  83.                 'label' => 'Действия',
  84.                 'actions' => [
  85.                     'edit' => [],
  86.                     'delete' => [],
  87.                 ]
  88.             ])
  89.         ;
  90.     }
  91. }