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

namespace App\Admin;

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;
14
use Symfony\Component\Security\Core\Security;
15
use Symfony\Component\Translation\TranslatorInterface;
16 17 18

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

26 27 28 29 30
    public function setTranslator(TranslatorInterface $translator)
    {
        $this->translator = $translator;
    }

31
    public function setSecurity(Security $security)
32
    {
33
        $this->security = $security;
34 35
    }

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    /**
    * {@inheritdoc}
    */
    protected function configureRoutes(RouteCollection $collection)
    {
        $collection->clearExcept('list');
    }

    /**
    * {@inheritdoc}
    */
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
    {
        $datagridMapper
            ->add('transfert_or_transaction', 'doctrine_orm_callback', array(
51
                'label' => "Type",
52 53 54 55 56 57 58 59 60 61 62 63 64
                'callback' => function ($queryBuilder, $alias, $field, $value) {
                    if (!$value['value']) {
                        return;
                    }
                    $queryBuilder
                        ->where($alias.".parenttype = :type")
                        ->setParameter('type', $value['value']);
                    return true;
                },
                'advanced_filter' => false,
                'show_filter' => true,
                'field_type' => SChoiceType::class,
                'field_options' => array(
65
                    'choices' => array('Transactions' => 'transaction', 'Transferts' => 'transfert', 'Cotisations' => 'cotisation'),
66 67 68 69 70
                    'placeholder' => 'Indifférent',
                    'expanded' => true,
                    'multiple' => false
                )
            ))
71 72 73 74 75
            ->add('type', null, array(
                'label' => 'Type plus précis',
                'advanced_filter' => false,
                'show_filter' => true
            ))
76 77 78 79 80 81 82 83 84 85 86
            ->add('operateur', null, array(
                'label' => 'Operateur',
                'advanced_filter' => false,
                'show_filter' => true
            ))
        ;
    }

    public function getTemplate($name)
    {
        if ($name == 'list') {
87
            return 'block/base_list_with_total.html.twig';
88 89 90 91 92
        }

        return parent::getTemplate($name);
    }

93 94
    public function getTotalLabel()
    {
95
        return $this->translator->trans('Total :');
96 97 98
    }

    public function getTotal()
99 100 101 102
    {
        $datagrid = $this->getDatagrid();
        $datagrid->buildPager();

103 104 105 106 107 108 109 110 111 112
        $query2 = clone $datagrid->getQuery();
        $query2
            ->select('SUM( ' . $query2->getRootAlias() . '.montant) as total')
            ->setFirstResult(null)
            ->setMaxResults(null);


        $result2 = $query2->execute(array(), \Doctrine\ORM\Query::HYDRATE_SINGLE_SCALAR);

        $query = clone $datagrid->getQuery();
113 114
        $query
            ->select('SUM( ' . $query->getRootAlias() . '.montant) as total')
115
            ->andWhere($query->getRootAlias().".parenttype = :type")
116 117 118 119 120 121 122
            ->setParameter('type', 'transaction')
            ->setFirstResult(null)
            ->setMaxResults(null);


        $result = $query->execute(array(), \Doctrine\ORM\Query::HYDRATE_SINGLE_SCALAR);

123
        return $result2.' (Transactions: '.$result.')';
124 125 126 127 128 129 130 131 132 133
    }

    /**
    * {@inheritdoc}
    */
    protected function configureListFields(ListMapper $listMapper)
    {
        unset($this->listModes['mosaic']);
        $listMapper
            ->addIdentifier('createdAt', null, array('label' => 'Date'))
134
            ->addIdentifier('type', null, array('label' => 'Type'))
135
            ->addIdentifier('operateur', User::class, array('label' => 'Operateur'))
136
            ->addIdentifier('expediteur', null, array('label' => 'Expediteur'))
137 138 139 140 141 142
            ->addIdentifier('destinataire', null, array('label' => 'Destinataire'))
            ->addIdentifier('montant', null, array('label' => 'Montant'))
            ->addIdentifier('reference', null, array('label' => 'Reference'))
        ;
    }
}