src/AdminBundle/Admin/Shop/GoodsAdmin.php line 21

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Admin\Shop;
  3. use CoreBundle\Entity\User;
  4. use AdminBundle\Form\Type\ContentType;
  5. use CoreBundle\Entity\Shop\Goods;
  6. use CoreBundle\Entity\Shop\GoodsContent;
  7. use Sonata\AdminBundle\Admin\AbstractAdmin;
  8. use Sonata\AdminBundle\Datagrid\ListMapper;
  9. use Sonata\AdminBundle\Form\FormMapper;
  10. use Sonata\AdminBundle\Form\Type\ModelType;
  11. use Sonata\AdminBundle\Route\RouteCollectionInterface;
  12. use Sonata\MediaBundle\Form\Type\MediaType;
  13. use Symfony\Component\Finder\Exception\AccessDeniedException;
  14. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  15. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  16. use Symfony\Component\Security\Core\Security;
  17. class GoodsAdmin 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('delete');
  30.     }
  31.     /**
  32.      * @param object $object
  33.      */
  34.     public function prePersist($object): void
  35.     {
  36.         /** @var User $User */
  37.         $User $this->security->getUser();
  38.         if(!$User->getDealer() && !$object->getDealer()) {
  39.             throw new AccessDeniedException('User without dealer');
  40.         }
  41.         /** @var Goods $object */
  42.         $object->setDealer($User->getDealer());
  43.         $object->setState((int) $object->getState());
  44.         /** @var GoodsContent $content */
  45.         foreach ($object->getContent() as $content) {
  46.             $content->setGoods($object);
  47.         }
  48.     }
  49.     /**
  50.      * @param FormMapper $formMapper
  51.      */
  52.     protected function configureFormFields(FormMapper $formMapper): void
  53.     {
  54.         $formMapper
  55.             ->tab('Основная информация')
  56.             ->with(' ', ['class' => 'col-lg-6 without-box-heder'])
  57.             ->add('state'CheckboxType::class, ['label' => 'Показывать на сайте''required' => false])
  58.             ->add('group'ModelType::class, [
  59.                 'required' => true,
  60.                 'label' => 'Группа товаров',
  61.                 'btn_add' => false,
  62.             ])
  63.             ->add('image'MediaType::class, [
  64.                 'label' => 'Изображение',
  65.                 'provider' => 'sonata.media.provider.image',
  66.                 'context'  => 'shop'
  67.             ])
  68.             ->add('price',NumberType::class, ['label' => 'Цена''required' => true])
  69.             ->add('content'ContentType::class, ['label' => false], [
  70.                 'edit' => 'inline',
  71.                 'sortable' => 'position',
  72.             ])
  73.             ->end()
  74.             ->end()
  75.         ;
  76.     }
  77.     /**
  78.      * @param ListMapper $listMapper
  79.      */
  80.     protected function configureListFields(ListMapper $listMapper): void
  81.     {
  82.         $listMapper->addIdentifier('id')
  83.             ->add('title',null, ['label' => 'Название'])
  84.             ->add('price',null, ['label' => 'Цена'])
  85.             ->add('dealer',null, ['label' => 'Дилер'])
  86.             ->add('state','choice', ['label' => 'Показывать на сайте''editable' => true'choices' => [
  87.                 => 'Да',
  88.                 => 'Нет',
  89.             ]])
  90.             ->add('_action''actions', [
  91.                 'label' => 'Действия',
  92.                 'actions' => [
  93.                     'edit' => [],
  94.                 ]
  95.             ])
  96.         ;
  97.     }
  98. }