src/AdminBundle/Admin/SubAutoSite/PagesServicesAdmin.php line 18

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Admin\SubAutoSite;
  3. use CoreBundle\Entity\User;
  4. use AdminBundle\Form\Type\ContentType;
  5. use ImporterBundle\Entity\PageContent;
  6. use ImporterBundle\Entity\PagesServices;
  7. use Sonata\AdminBundle\Datagrid\ListMapper;
  8. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  9. use Sonata\AdminBundle\Form\FormMapper;
  10. use Sonata\AdminBundle\Route\RouteCollection;
  11. use Sonata\AdminBundle\Route\RouteCollectionInterface;
  12. use Symfony\Component\Finder\Exception\AccessDeniedException;
  13. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  14. class PagesServicesAdmin extends BaseImporterAdmin
  15. {
  16.     /**
  17.      * @return string
  18.      */
  19.     public function getImageContext() {
  20.         return 'default';
  21.     }
  22.     /**
  23.      * @param RouteCollectionInterface $collection
  24.      */
  25.     protected function configureRoutes(RouteCollectionInterface $collection): void
  26.     {
  27.         $collection->remove('view');
  28.     }
  29.     /**
  30.      * @param PagesServices $object
  31.      */
  32.     public function prePersist($object): void
  33.     {
  34.         /** @var User $User */
  35.         $User $this->security->getUser();
  36.         if(!$User->getSubDealer() && !$object->getDealer()) {
  37.             throw new AccessDeniedException('User without sub dealer');
  38.         }
  39.         /** @var PageContent $content */
  40.         foreach ($object->getContent() as $content) {
  41.             $content->setPage($object);
  42.         }
  43.         $object->setDealer($User->getSubDealer());
  44.         $object->setState((int) $object->getState());
  45.         $object->setCreator($User);
  46.     }
  47.     /**
  48.      * @param FormMapper $formMapper
  49.      */
  50.     protected function configureFormFields(FormMapper $formMapper): void
  51.     {
  52.         $formMapper
  53.             ->tab('Основная информация')
  54.             ->with(' ', ['class' => 'col-lg-6 without-box-heder'])
  55.             ->add('name_ua'null, ['label' => 'Название раздела (UA)'])
  56.             ->add('name_ru'null, ['label' => 'Название раздела (RU)'])
  57.             ->add('state'CheckboxType::class, ['label' => 'Показывать на сайте''required' => false])
  58.             ->add('url'null, ['label' => 'URL'])
  59.             ->add('content'ContentType::class, [
  60.                 'label' => false,
  61.             ], [
  62.                 'edit' => 'inline',
  63.                 'sortable' => 'position',
  64.             ])
  65.             ->end()
  66.             ->end()
  67.         ;
  68.     }
  69.     /**
  70.      * @param string $context
  71.      * @return ProxyQueryInterface
  72.      */
  73.     public function configureQuery($context 'list'): ProxyQueryInterface
  74.     {
  75.         $user $this->security->getUser();
  76.         $dealer $user->getSubDealer();
  77.         $query parent::configureQuery($context);
  78.         $query->andWhere(
  79.             $query->expr()->eq($query->getRootAliases()[0] . '.dealer'':dealer')
  80.         );
  81.         $query->setParameter('dealer'$dealer);
  82.         return $query;
  83.     }
  84.     /**
  85.      * @param ListMapper $listMapper
  86.      */
  87.     protected function configureListFields(ListMapper $listMapper): void
  88.     {
  89.         $listMapper->addIdentifier('id')
  90.             ->add('name_ru',null, ['label' => 'Название раздела'])
  91.             ->add('dealer',null, ['label' => 'Дилер''admin_code' => 'admin.sub.contact'])
  92.             ->add('state','choice', ['label' => 'Показывать на сайте''editable' => true'choices' => [
  93.                 => 'Да',
  94.                 => 'Нет',
  95.             ]])
  96.             ->add('_action''actions', [
  97.                 'label' => 'Действия',
  98.                 'actions' => [
  99.                     'edit' => [],
  100.                 ]
  101.             ])
  102.         ;
  103.     }
  104. }