TransfertAdmin.php 3.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

namespace App\Admin;

use App\Admin\FluxAdmin;
use App\Entity\User;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType as SChoiceType;
15
use Symfony\Component\Security\Core\Security;
16 17 18 19
use Symfony\Component\Translation\TranslatorInterface;

class TransfertAdmin extends FluxAdmin
{
20
    protected $security;
21 22 23 24 25
    protected $datagridValues = [
        '_sort_order' => 'DESC',
        '_sort_by' => 'createdAt',
    ];

26
    public function setSecurity(Security $security)
27
    {
28
        $this->security = $security;
29 30
    }

31 32 33 34 35
    /**
    * {@inheritdoc}
    */
    public function createQuery($context = 'list')
    {
36
        $user = $this->security->getUser();
37 38 39 40
        $query = parent::createQuery($context);
        $query->andWhere($query->getRootAliases()[0].".parenttype = :type")
                ->setParameter('type', 'transfert');
        ;
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        if ($user->isGranted('ROLE_GESTION_GROUPE') || $user->isGranted('ROLE_CONTACT')) {
            if (empty($user->getGroupesgere())) {
                $query->andWhere('false');
            } else {
                $groupe = $user->getGroupesgere();
                $em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getEntityManager();
                $connection = $em->getConnection();
                $statement = $connection->prepare('SELECT f.id FROM flux f WHERE f.groupe_id = '.$groupe->getId().' OR (f.type = \'prestataire_siege\' AND f.prestataire_id IN (SELECT p.id FROM prestataire p WHERE p.groupe_id = '.$groupe->getId().'))');
                $statement->execute();
                $ids = $statement->fetchAll();
                $query
                    ->andWhere($query->expr()->in($query->getRootAliases()[0].'.id', ':ids'))
                    ->setParameter('ids', $ids)
                ;
            }
        }
        if ($user->isGranted('ROLE_COMPTOIR')) {
            if (empty($user->getComptoirsgere())) {
                $query->andWhere('false');
            } else {
                $comptoir = $user->getComptoirsgere();
                $em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getEntityManager();
                $connection = $em->getConnection();
                $statement = $connection->prepare('SELECT f.id FROM flux f WHERE f.comptoir_id = '.$comptoir->getId().' OR f.user_id = '.$user->getId());
                $statement->execute();
                $ids = $statement->fetchAll();
                $query
                    ->andWhere($query->expr()->in($query->getRootAliases()[0].'.id', ':ids'))
                    ->setParameter('ids', $ids)
                ;
            }
        }
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
        return $query;
    }

    /**
    * {@inheritdoc}
    */
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
    {
        $datagridMapper
            ->add('type', null, array(
                'label' => 'Type de transfert',
                'advanced_filter' => false,
                'show_filter' => true
            ))
            ->add('operateur', null, array(
                'label' => 'Operateur',
                'advanced_filter' => false,
                'show_filter' => true
            ))
        ;
    }
}