src/AdminBundle/Admin/DCAutoSite/VacancyAdmin.php line 20

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Admin\DCAutoSite;
  3. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  4. use AdminBundle\Form\Type\ContentType;
  5. use CoreBundle\Entity\User;
  6. use DcSiteBundle\Entity\Vacancy;
  7. use DcSiteBundle\Entity\VacancyContent;
  8. use Sonata\AdminBundle\Admin\AbstractAdmin;
  9. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  10. use Sonata\AdminBundle\Datagrid\ListMapper;
  11. use Sonata\AdminBundle\Form\FormMapper;
  12. use Sonata\AdminBundle\Route\RouteCollectionInterface;
  13. use Sonata\MediaBundle\Form\Type\MediaType;
  14. use Symfony\Component\Finder\Exception\AccessDeniedException;
  15. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  16. use Symfony\Component\Security\Core\Security;
  17. class VacancyAdmin extends AbstractAdmin
  18. {
  19.     protected Security $security;
  20.     public function setContainerServices(Security $security): void
  21.     {
  22.         $this->security $security;
  23.     }
  24.     /**
  25.      * @param RouteCollectionInterface $collection
  26.      */
  27.     protected function configureRoutes(RouteCollectionInterface $collection): void
  28.     {
  29.         $collection->remove('view');
  30.     }
  31.     /**
  32.      * @param DatagridMapper $datagridMapper
  33.      */
  34.     protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
  35.     {
  36.         $datagridMapper->add('id');
  37.     }
  38.     /**
  39.      * @param Vacancy $object
  40.      */
  41.     public function prePersist($object): void
  42.     {
  43.         /** @var User $User */
  44.         $User $this->security->getUser();
  45.         if(!$User->getDealer() && !$object->getDealer()) {
  46.             throw new AccessDeniedException('User without dealer');
  47.         }
  48.         $object->setDealer($User->getDealer());
  49.         $object->setHot((int) $object->getHot());
  50.         $contents $object->getContent();
  51.         /** @var VacancyContent $content */
  52.         foreach ($contents as $content) {
  53.             $content->setVacancy($object);
  54.         }
  55.     }
  56.     /**
  57.      * @param FormMapper $formMapper
  58.      */
  59.     protected function configureFormFields(FormMapper $formMapper): void
  60.     {
  61.         $formMapper
  62.             ->tab('Основная информация')
  63.             ->with(' ', ['class' => 'col-lg-6 without-box-heder'])
  64.             ->add('image'MediaType::class, [
  65.                 'label' => 'Изображение',
  66.                 'required' => false,
  67.                 'provider' => 'sonata.media.provider.image',
  68.                 'context'  => 'dc_site'
  69.             ])
  70.             ->add('state'CheckboxType::class, ['label' => 'Показывать на сайте''required' => false])
  71.             ->add('hot'CheckboxType::class, [
  72.                 'label' => 'Горячая?',
  73.                 'required' => false,
  74.             ])
  75.             ->add('content'ContentType::class, ['label' => false], [
  76.                 'edit' => 'inline',
  77.                 'sortable' => 'position',
  78.             ])
  79.             ->end()
  80.             ->end()
  81.         ;
  82.     }
  83.     /**
  84.      * @param string $context
  85.      * @return ProxyQueryInterface
  86.      */
  87.     public function configureQuery($context 'list'): ProxyQueryInterface
  88.     {
  89.         $query parent::configureQuery($context);
  90.         /** @var User $User */
  91.         $User $this->security->getUser();
  92.         if(!$User->getDealer()) {
  93.             throw new AccessDeniedException('User without dealer');
  94.         }
  95.         $query->andWhere(
  96.             $query->expr()->eq($query->getRootAliases()[0] . '.dealer'$User->getDealer()->getId()));
  97.         return $query;
  98.     }
  99.     /**
  100.      * @param ListMapper $listMapper
  101.      */
  102.     protected function configureListFields(ListMapper $listMapper): void
  103.     {
  104.         $listMapper->addIdentifier('id')
  105.             ->add('title',null, ['label' => 'Заголовок'])
  106.             ->add('dealer',null, ['label' => 'Дилер'])
  107.             ->add('state','choice', ['label' => 'Показывать на сайте''choices' => [
  108.                 => 'Да',
  109.                 => 'Нет',
  110.             ]])
  111.             ->add('_action''actions', [
  112.                 'label' => 'Действия',
  113.                 'actions' => [
  114.                     'edit' => [],
  115.                 ]
  116.             ])
  117.         ;
  118.     }
  119. }