src/Admin/AbstractAdmin.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Admin;
  3. use App\Entity\User;
  4. use App\Utils\DateUtils;
  5. use DateTime;
  6. use Sonata\AdminBundle\Datagrid\DatagridInterface;
  7. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  8. use Sonata\AdminBundle\Admin\AbstractAdmin as BaseAbstractAdmin;
  9. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  10. use Sonata\AdminBundle\Filter\Model\FilterData;
  11. use Sonata\AdminBundle\Form\FormMapper;
  12. use Sonata\DoctrineORMAdminBundle\Filter\CallbackFilter;
  13. use Sonata\DoctrineORMAdminBundle\Filter\ChoiceFilter;
  14. use Sonata\DoctrineORMAdminBundle\Filter\DateRangeFilter;
  15. use Sonata\Form\Type\DateRangePickerType;
  16. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  17. use Symfony\Component\Security\Core\Security;
  18. use Symfony\Component\Security\Core\User\UserInterface;
  19. class AbstractAdmin extends BaseAbstractAdmin
  20. {
  21.     private Security $security;
  22.     public function __construct(?string $code null, ?string $class null, ?string $baseControllerName nullSecurity $security null)
  23.     {
  24.         parent::__construct($code$class$baseControllerName);
  25.         $this->security $security;
  26.     }
  27.     /**
  28.      * {@inheritdoc}
  29.      */
  30.     protected function configure(): void
  31.     {
  32.         if (is_object($this->getUser()) && $this->getUser()->hasRole(User::ROLE_ADMIN)) {
  33.             // disable softdeleted filter for admin
  34.             $filters $this->getModelManager()->getEntityManager($this->getClass())->getFilters();
  35.             if (array_key_exists('softdeleteable'$filters->getEnabledFilters())) {
  36.                 $filters->disable('softdeleteable');
  37.             }
  38.         }
  39.     }
  40.     protected function getUser(): ?UserInterface
  41.     {
  42.         $token $this->getSecurity()->getToken();
  43.         if ($token) {
  44.             return $token->getUser();
  45.         }
  46.         return null;
  47.     }
  48.     public function getSecurity(): Security
  49.     {
  50.         return $this->security;
  51.     }
  52.     public function setSecurity($security)
  53.     {
  54.         $this->security $security;
  55.     }
  56.     /**
  57.      * {@inheritdoc}
  58.      */
  59.     protected function configureFormFields(FormMapper $form): void
  60.     {
  61.         if (is_null($this->getSubject()) || is_null($this->getSubject()->getId())) {
  62.             $this->configureFormFieldsCreation($form);
  63.         } else {
  64.             $this->configureFormFieldsEdition($form);
  65.         }
  66.     }
  67.     /**
  68.      * Add a filter on a time range
  69.      * @param DatagridMapper $filter
  70.      * @param string $fieldName // name of the entity field
  71.      */
  72.     protected function addTimeRangeFilterOnField(DatagridMapper $filterstring $fieldName)
  73.     {
  74.         $filter->add($fieldNameDateRangeFilter::class, [
  75.             'advanced_filter' => false,
  76.             'show_filter' => true,
  77.             'field_type' => DateRangePickerType::class,
  78.         ]);
  79.     }
  80.     /**
  81.      * Add a filter on a period for a given field
  82.      * @param DatagridMapper $filter
  83.      * @param string $fieldName // name of the entity field
  84.      * @param null $fullFieldName
  85.      */
  86.     protected function addPeriodFilterOnField(DatagridMapper $filterstring $fieldName$fullFieldName nullstring $label null)
  87.     {
  88.         //filter on a period
  89.         $filter
  90.             ->add('period'CallbackFilter::class, array(
  91.                 'show_filter' => true,
  92.                 'advanced_filter' => false,
  93.                 'label' => !is_null($label) ? $label 'Période',
  94.                 'callback' => function (ProxyQueryInterface $querystring $aliasstring $fieldFilterData $data) use ($fieldName$fullFieldName) {
  95.                     if (!$data->hasValue()) {
  96.                         return false;
  97.                     }
  98.                     $startAndEndDates DateUtils::getFirstAndLastDayOfPeriod($data->getValue());
  99.                     $startDate $startAndEndDates[0];
  100.                     $endDate $startAndEndDates[1];
  101.                     // $queryBuilder->join($alias . '.prospectsOnStore', 'pos');
  102.                     if (is_null($fullFieldName)) {
  103.                         $fullFieldName $alias '.' $fieldName;
  104.                     }
  105.                     $query->andWhere($fullFieldName ' >= :startDate and ' $fullFieldName ' <= :endDate');
  106.                     $query->setParameter('startDate'$startDate);
  107.                     $query->setParameter('endDate'$endDate);
  108.                     return true;
  109.                 },
  110.                 'field_type' => ChoiceType::class,
  111.                 'field_options' => array(
  112.                     'choices' => [
  113.                         'Aujourd\'hui' => DateUtils::PERIOD_TODAY,
  114.                         'Semaine en cours' => DateUtils::PERIOD_THIS_WEEK,
  115.                         'Mois en cours' => DateUtils::PERIOD_THIS_MONTH,
  116.                         'Année en cours' => DateUtils::PERIOD_THIS_YEAR,
  117.                         'Semaine dernière' => DateUtils::PERIOD_LAST_WEEK,
  118.                         'Mois dernier' => DateUtils::PERIOD_LAST_MONTH,
  119.                         'Année dernière' => DateUtils::PERIOD_LAST_YEAR,
  120.                     ],
  121.                 ),
  122.             ));
  123.     }
  124.     /**
  125.      * Add a filter on a period for a given field
  126.      * @param DatagridMapper $filter
  127.      * @param string $fieldName // name of the entity field
  128.      * @param null $label
  129.      * @param null $fullFieldName
  130.      */
  131.     protected function addPreviousMonthsFilterOnField(
  132.         DatagridMapper $filter,
  133.         string         $fieldName,
  134.                        $label null,
  135.                        $fullFieldName null
  136.     )
  137.     {
  138.         $now = new DateTime();
  139.         $choices = [];
  140.         for ($m 0$m 24$m++) {
  141.             $choices[$now->format('m/y')] = $now->format('Y-m-01');
  142.             $now->modify('-1 months');
  143.         }
  144.         $filter
  145.             ->add('month'CallbackFilter::class, array(
  146.                 'show_filter' => true,
  147.                 'advanced_filter' => false,
  148.                 'label' => is_null($label) ? 'Mois' $label,
  149.                 'callback' => function (ProxyQueryInterface $querystring $aliasstring $fieldFilterData $data) use ($fieldName$fullFieldName) {
  150.                     if (!$data->hasValue()) {
  151.                         return false;
  152.                     }
  153.                     $now DateUtils::getFirstDayOfMonthMidnightFromString($data->getValue());
  154.                     $startDate = clone $now;
  155.                     //$startDate->modify('-'.$data->getValue().' months');
  156.                     $endDate = clone $startDate;
  157.                     $endDate DateUtils::getLastDayOfMonthMidnight($endDate);
  158.                     // $queryBuilder->join($alias . '.prospectsOnStore', 'pos');
  159.                     if (is_null($fullFieldName)) {
  160.                         $fullFieldName $alias '.' $fieldName;
  161.                     }
  162.                     $query->andWhere($fullFieldName ' >= :startDate and ' $fullFieldName ' <= :endDate');
  163.                     $query->setParameter('startDate'$startDate);
  164.                     $query->setParameter('endDate'$endDate);
  165.                     return true;
  166.                 },
  167.                 'field_type' => ChoiceType::class,
  168.                 'field_options' => array(
  169.                     'choices' => $choices,
  170.                 ),
  171.             ));
  172.     }
  173.     /**
  174.      * Add a filter on acquisitionMode
  175.      * @param DatagridMapper $filter
  176.      * @param string $fieldName // name of the entity field
  177.      */
  178.     protected function addAcquisitionModeFilterOnField(DatagridMapper $filterstring $fieldName)
  179.     {
  180.         $choices = [];
  181.         foreach ($this->getAllModeAcquisition() as $m) {
  182.             $m strtolower($m[1]);
  183.             $choices[$m] = $m;
  184.         }
  185.         asort($choices);
  186.         $filter
  187.             ->add($fieldNameChoiceFilter::class, array(
  188.                 'show_filter' => true,
  189.                 'advanced_filter' => false,
  190.                 'label' => 'Mode d\'acquisition',
  191.                 'field_type' => ChoiceType::class,
  192.                 'field_options' => array(
  193.                     'choices' => $choices,
  194.                 ),
  195.             ));
  196.     }
  197.     private function getAllModeAcquisition()
  198.     {
  199.         return $this->getRepository()->createQueryBuilder('p')
  200.             ->select('DISTINCT(p.acquisitionMode)')
  201.             ->getQuery()->getArrayResult();
  202.     }
  203.     protected function getRepository()
  204.     {
  205.         return $this->getModelManager()->getEntityManager($this->getClass())->getRepository($this->getClass());
  206.     }
  207.     /**
  208.      * Add a filter on acquisitionBase
  209.      * @param DatagridMapper $filter
  210.      * @param $fieldName // name of the entity field
  211.      */
  212.     protected function addAcquisitionBaseFilterOnField(DatagridMapper $filter$fieldName)
  213.     {
  214.         $choices = [];
  215.         foreach ($this->getAllBaseAcquisition() as $m) {
  216.             $m strtolower($m[1]);
  217.             $choices[$m] = $m;
  218.         }
  219.         $filter
  220.             ->add($fieldNameChoiceFilter::class, array(
  221.                 'show_filter' => true,
  222.                 'advanced_filter' => false,
  223.                 'label' => 'Base d\'acquisition',
  224.                 'field_type' => ChoiceType::class,
  225.                 'field_options' => [
  226.                     'choices' => $choices,
  227.                 ]
  228.             ));
  229.     }
  230.     private function getAllBaseAcquisition()
  231.     {
  232.         return $this->getRepository()->createQueryBuilder('p')
  233.             ->select('DISTINCT(p.acquisitionBase)')
  234.             ->getQuery()->getArrayResult();
  235.     }
  236.     /**
  237.      * Add a filter on admin type (partner or customer)
  238.      * @param DatagridMapper $filter
  239.      * @param $fieldName // name of the entity field
  240.      */
  241.     protected function addAdminTypeFilterOnField(DatagridMapper $filter$fieldName)
  242.     {
  243.         $choices = [
  244.             'Indépendants' => 'freelancer',
  245.             'Partenaire' => 'partner',
  246.         ];
  247.         $filter
  248.             ->add($fieldNameChoiceFilter::class, array(
  249.                 'show_filter' => true,
  250.                 'advanced_filter' => false,
  251.                 'label' => 'Type de client',
  252.                 'field_type' => ChoiceType::class,
  253.                 'field_options' => array(
  254.                     'choices' => $choices,
  255.                 ),
  256.             ));
  257.     }
  258.     protected function configureDefaultSortValues(array &$sortValues): void
  259.     {
  260.         // reverse order (default = 'ASC')
  261.         $sortValues[DatagridInterface::SORT_ORDER] = 'DESC';
  262.         // name of the ordered field (default = the model's id field, if any)
  263.         $sortValues[DatagridInterface::SORT_BY] = 'id';
  264.     }
  265. }