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

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