src/AdminBundle/Admin/DCAutoSite/PagesAdmin.php line 17

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Admin\DCAutoSite;
  3. use AdminBundle\Admin\BaseAdmin;
  4. use AdminBundle\Form\Type\ContentType;
  5. use CoreBundle\Entity\Post;
  6. use CoreBundle\Entity\User;
  7. use DateTime;
  8. use DcSiteBundle\Entity\PageContent;
  9. use Sonata\AdminBundle\Datagrid\ListMapper;
  10. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  11. use Sonata\AdminBundle\Form\FormMapper;
  12. use Sonata\AdminBundle\Route\RouteCollectionInterface;
  13. use Symfony\Component\Finder\Exception\AccessDeniedException;
  14. class PagesAdmin extends BaseAdmin
  15. {
  16.     /**
  17.      * @param RouteCollectionInterface $collection
  18.      */
  19.     protected function configureRoutes(RouteCollectionInterface $collection): void
  20.     {
  21.         $collection->remove('view');
  22.     }
  23.     public function configureQuery($context 'list'): ProxyQueryInterface
  24.     {
  25.         $query parent::configureQuery($context);
  26.         /** @var User $User */
  27.         $User $this->security->getUser();
  28.         if (!$User->getDealer()) {
  29.             throw new AccessDeniedException('User without dealer');
  30.         }
  31.         $query->andWhere($query->getRootAliases()[0] . '.dealer = ' $User->getDealer()->getId());
  32.         return $query;
  33.     }
  34.     /**
  35.      * @param FormMapper $formMapper
  36.      */
  37.     protected function configureFormFields(FormMapper $formMapper): void
  38.     {
  39.         $formMapper
  40.             ->tab('Основная информация')
  41.             ->with(' ', ['class' => 'col-lg-6 without-box-heder'])
  42.             ->add('url'null, ['label' => 'URL для поиска страницы(system)'])
  43.             ->add('content'ContentType::class, ['label' => false], [
  44.                 'admin_code' => 'admin.dc.page_content',
  45.                 'edit' => 'inline',
  46.                 'sortable' => 'position',
  47.             ])
  48.             ->end()
  49.             ->end();
  50.     }
  51.     /**
  52.      * @param ListMapper $listMapper
  53.      */
  54.     protected function configureListFields(ListMapper $listMapper): void
  55.     {
  56.         $listMapper->addIdentifier('id')
  57.             ->add('dealer'null, ['label' => 'Дилер'])
  58.             ->add('url'null, ['label' => 'Url'])
  59.             ->add('_action''actions', [
  60.                 'label' => 'Действия',
  61.                 'actions' => [
  62.                     'edit' => [],
  63.                 ]
  64.             ]);
  65.     }
  66.     /**
  67.      * @param Post $object
  68.      */
  69.     public function prePersist($object): void
  70.     {
  71.         parent::prePersist($object);
  72.         $User $this->security->getUser();
  73.         $object->setState(1);
  74.         $object->setDateCreate(new DateTime());
  75.         $object->setDealer($User->getDealer());
  76.         /** @var PageContent $content */
  77.         foreach ($object->getContent() as $content) {
  78.             $content->setDescription('');
  79.             $content->setPage($object);
  80.         }
  81.     }
  82. }