src/AdminBundle/Admin/DCAutoSite/SpecialOffersAdmin.php line 30

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Admin\DCAutoSite;
  3. use CoreBundle\Entity\User;
  4. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  5. use AdminBundle\Form\Type\ContentType;
  6. use CoreBundle\Entity\SpecialOffers;
  7. use CoreBundle\Entity\SpecialOffersContent;
  8. use Sonata\AdminBundle\Admin\AbstractAdmin;
  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\Form\Validator\ErrorElement;
  14. use Sonata\MediaBundle\Form\Type\MediaType;
  15. use Symfony\Component\Finder\Exception\AccessDeniedException;
  16. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  17. use Symfony\Component\Form\Extension\Core\Type\DateType;
  18. use Symfony\Component\Form\FormError;
  19. use Symfony\Component\Security\Core\Security;
  20. use Symfony\Component\Yaml\Yaml;
  21. /**
  22.  * Class GoodsAdmin
  23.  * @package AdminBundle\Admin\Shop
  24.  */
  25. class SpecialOffersAdmin extends AbstractAdmin
  26. {
  27.     /**
  28.      * @var string
  29.      */
  30.     private $projectDir;
  31.     private Security $security;
  32.     public function __construct($code$class$baseControllerNamestring $projectDirSecurity $security)
  33.     {
  34.         parent::__construct($code$class$baseControllerName);
  35.         $this->projectDir $projectDir;
  36.         $this->security $security;
  37.     }
  38.     /**
  39.      * @param RouteCollectionInterface $collection
  40.      */
  41.     protected function configureRoutes(RouteCollectionInterface $collection): void
  42.     {
  43.         $collection->remove('delete');
  44.     }
  45.     /**
  46.      * @param object $object
  47.      */
  48.     public function prePersist($object): void
  49.     {
  50.         /** @var User $User */
  51.         $User $this->security->getUser();
  52.         if (!$User->getDealer() && !$object->getDealer()) {
  53.             throw new AccessDeniedException('User without dealer');
  54.         }
  55.         /** @var SpecialOffers $object */
  56.         $object->setDealer($User->getDealer());
  57.         $object->setState((int)$object->getState());
  58.         /** @var SpecialOffersContent $content */
  59.         foreach ($object->getContent() as $content) {
  60.             $content->setSpecialOffers($object);
  61.         }
  62.     }
  63.     /**
  64.      * @param FormMapper $formMapper
  65.      */
  66.     protected function configureFormFields(FormMapper $formMapper): void
  67.     {
  68.         $formMapper
  69.             ->tab('Основная информация')
  70.             ->with(' ', ['class' => 'col-lg-6 without-box-heder'])
  71.             ->add('state'CheckboxType::class, ['label' => 'Показывать на сайте''required' => false])
  72.             ->add('title'null, ['label' => 'Название'])
  73.             ->add('url'null, ['label' => 'URL'])
  74.             ->add('date_start'DateType::class, ['widget' => 'single_text''label' => 'Дата начала'])
  75.             ->add('date_end'DateType::class, ['widget' => 'single_text''label' => 'Дата конца'])
  76.             ->add('min_pay'null, ['label' => 'Минимальный платеж'])
  77.             ->add('rate'null, ['label' => 'Процентная ставка'])
  78.             ->add('period'null, ['label' => 'Период лизингу, мес. (максимальный)'])
  79.             ->add('image'MediaType::class, [
  80.                 'label' => 'Иконка',
  81.                 'provider' => 'sonata.media.provider.image',
  82.                 'context' => 'dc_site'
  83.             ])
  84.             ->add('content'ContentType::class, ['label' => false], [
  85.                 'edit' => 'inline',
  86.                 'sortable' => 'position',
  87.             ])
  88.             ->end()
  89.             ->end();
  90.     }
  91.     /**
  92.      * @param string $context
  93.      * @return ProxyQueryInterface
  94.      */
  95.     public function configureQuery($context 'list'): ProxyQueryInterface
  96.     {
  97.         $user $this->security->getUser();
  98.         $query parent::configureQuery($context);
  99.         $dealer $user->getDealer();
  100.         if (!$dealer) {
  101.             throw new AccessDeniedException();
  102.         }
  103.         $query->andWhere($query->getRootAliases()[0] . '.dealer = :dealer');
  104.         $query->setParameter('dealer'$dealer->getId());
  105.         return $query;
  106.     }
  107.     /**
  108.      * @param ListMapper $listMapper
  109.      */
  110.     protected function configureListFields(ListMapper $listMapper): void
  111.     {
  112.         $listMapper->addIdentifier('id')
  113.             ->add('title'null, ['label' => 'Название'])
  114.             ->add('date_start''datetime', ['label' => 'Старт предложения''format' => 'd.m.Y'])
  115.             ->add('date_end''datetime', ['label' => 'Конец предложения''format' => 'd.m.Y'])
  116.             ->add('state''choice', ['label' => 'Показывать на сайте''editable' => true'choices' => [
  117.                 => 'Да',
  118.                 => 'Нет',
  119.             ]])
  120.             ->add('_action''actions', [
  121.                 'label' => 'Действия',
  122.                 'actions' => [
  123.                     'edit' => [],
  124.                 ]
  125.             ]);
  126.     }
  127.     /**
  128.      * @param ErrorElement $errorElement
  129.      */
  130.     public function validate(ErrorElement $errorElement$object)
  131.     {
  132.         parent::validate($errorElement$object);
  133.         $configPath $this->projectDir '/config/packages/sonata_media.yaml';
  134.         $config Yaml::parseFile($configPath);
  135.         $allowedMimeTypes $config['sonata_media']['providers']['image']['allowed_mime_types'];
  136.         /** @var SpecialOffersContent $content */
  137.         foreach ($object->getContent() as $content) {
  138.             $contentType $content->getImage()->getContentType();
  139.             if (!in_array($contentType$allowedMimeTypes)) {
  140.                 $this->getForm()->addError(new FormError('Невірний формат зображення в контенті (' $contentType ') не підтримується.'));
  141.             }
  142.         }
  143.         $contentType $object->getImage()->getContentType();
  144.         if (!in_array($contentType$allowedMimeTypes)) {
  145.             $this->getForm()->addError(new FormError('Невірний формат зображення в іконці (' $contentType ') не підтримується.'));
  146.         }
  147.     }
  148. }