src/AdminBundle/Admin/DCAutoSite/SalesContractAdmin.php line 21

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Admin\DCAutoSite;
  3. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  4. use AdminBundle\Admin\BaseAdmin;
  5. use DcSiteBundle\Entity\SalesContract;
  6. use DcSiteBundle\Model\File;
  7. use Sonata\AdminBundle\Datagrid\ListMapper;
  8. use Sonata\AdminBundle\Form\FormMapper;
  9. use Sonata\MediaBundle\Form\Type\MediaType;
  10. use Symfony\Component\Finder\Exception\AccessDeniedException;
  11. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  12. use Symfony\Component\Form\Extension\Core\Type\DateType;
  13. /**
  14.  * Class SalesContractAdmin
  15.  * @package AdminBundle\Admin\DCAutoSite
  16.  */
  17. class SalesContractAdmin extends BaseAdmin
  18. {
  19.     /**
  20.      * @param SalesContract $object
  21.      */
  22.     public function prePersist($object): void
  23.     {
  24.         $user $this->getUser();
  25.         if(!$user->getDealer() && !$object->getDealer()) {
  26.             throw new AccessDeniedException('User without dealer');
  27.         }
  28.         $object->setDealer($user->getDealer());
  29.     }
  30.     /**
  31.      * @param string $context
  32.      * @return ProxyQueryInterface
  33.      */
  34.     public function configureQuery($context 'list'): ProxyQueryInterface
  35.     {
  36.         $user $this->security->getUser();
  37.         $query parent::configureQuery($context);
  38.         $dealer $user->getDealer();
  39.         if(!$dealer) {
  40.             throw new AccessDeniedException();
  41.         }
  42.         $query->andWhere($query->getRootAliases()[0].'.dealer = :dealer');
  43.         $query->setParameter('dealer'$dealer->getId());
  44.         return $query;
  45.     }
  46.     /**
  47.      * @param FormMapper $form
  48.      */
  49.     public function configureFormFields(FormMapper $form): void
  50.     {
  51.         $form
  52.             ->tab('Информация')
  53.             ->with(' ', ['class' => 'col-lg-6 without-box-heder'])
  54.             ->add('file'MediaType::class, [
  55.                 'label' => 'Файл договору',
  56.                 'provider' => 'sonata.media.provider.file',
  57.                 'context'  => 'dc_file'
  58.             ])
  59.             ->add('title_ua'null, [
  60.                 'label' => 'Название UA',
  61.             ])
  62.             ->add('title_ru'null, [
  63.                 'label' => 'Название RU',
  64.             ])
  65.             ->add('begin_date'DateType::class, [
  66.                 'label' => 'Редакція договору від',
  67.                 'widget' => 'single_text',
  68.             ])
  69.             ->add('type'ChoiceType::class, [
  70.                 'label' => 'Тип документа',
  71.                 'choices' => array_flip(File::getTypes()),
  72.             ])
  73.             ->end()
  74.             ->end()
  75.             ;
  76.     }
  77.     /**
  78.      * @param ListMapper $list
  79.      */
  80.     public function configureListFields(ListMapper $list): void
  81.     {
  82.         $list->addIdentifier('id')
  83.             ->add('title_ua'null, ['label' => 'Назва договору'])
  84.             ->add('begin_date'null, [
  85.                 'label' => 'Редакція договору від',
  86.                 'format' => 'd.m.Y',
  87.             ])
  88.             ->add('_action''actions', [
  89.                 'label' => 'Действия',
  90.                 'actions' => [
  91.                     'edit' => [],
  92.                 ]
  93.             ])
  94.             ;
  95.     }
  96. }