SolidoumeAdmin.php 10.4 KB
Newer Older
Julien Jorry committed
1 2 3 4 5 6 7
<?php

namespace App\Admin;

use App\Entity\Adherent;
use App\Entity\SolidoumeParameter;
use App\Entity\User;
8
use App\Utils\LogEntryTrait;
Julien Jorry committed
9 10 11 12 13 14
use Knp\Menu\ItemInterface;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
15
use Sonata\AdminBundle\Show\ShowMapper;
Julien Jorry committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
use Sonata\Form\Type\DateTimeRangePickerType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
use Symfony\Component\Validator\Constraints\NotBlank;

/**
 * Administration des participations à Solidoume.
 *
 * KOHINOS : Outil de gestion de Monnaie Locale Complémentaire
 *
 * @author Julien Jorry <julien.jorry@gmail.com>
 */
class SolidoumeAdmin extends AbstractAdmin
{
36 37
    use LogEntryTrait;

Julien Jorry committed
38 39 40 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    protected $baseRouteName = 'solidoume';
    protected $baseRoutePattern = 'solidoume';
    protected $security;

    protected $datagridValues = [
        // reverse order (default = 'ASC')
        '_sort_order' => 'DESC',
        // name of the ordered field (default = the model's id field, if any)
        '_sort_by' => 'createdAt',
        '_page' => 1,
        '_per_page' => 100,
    ];

    /**
     * {@inheritdoc}
     */
    protected function configureFormFields(FormMapper $formMapper)
    {
        $item = $this->getSubject();
        $user = $this->getConfigurationPool()->getContainer()->get('security.token_storage')->getToken() ? $this->getConfigurationPool()->getContainer()->get('security.token_storage')->getToken()->getUser() : null;
        $em = $this->getConfigurationPool()->getContainer()->get('doctrine');
        $adherents = $em->getRepository(Adherent::class)->findOrderByName();
        $cotisationMaximum = $em->getRepository(SolidoumeParameter::class)->getValueOf('maximum');
        $cotisationMinimum = $em->getRepository(SolidoumeParameter::class)->getValueOf('minimum');

        $formMapper
            ->add('user', HiddenType::class, [
                'data' => $user,
                'data_class' => null,
                'entity_class' => User::class,
                'em' => $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager(),
            ])
            ->add('adherent', EntityType::class, [
                'class' => Adherent::class,
                'choices' => $adherents,
                'placeholder' => 'Choisir un adherent',
                'choice_label' => function ($adherent) {
                    return $adherent->getUser() ? ($adherent->getUser()->getLastname() . ' ' . $adherent->getUser()->getFirstname() . ' (' . $adherent->getUser()->getEmail() . ')') : $adherent->__toString();
                },
                'required' => false,
                'label' => false,
            ])
            ->add('amount', MoneyType::class, [
                'label' => 'Montant en euro(s) entre ' . $cotisationMinimum . ' et ' . $cotisationMaximum,
                'required' => true,
                'scale' => 2,
                'constraints' => [
                    new NotBlank(),
                    new LessThanOrEqual(['value' => $cotisationMaximum]),
                    new GreaterThanOrEqual(['value' => $cotisationMinimum]),
                ],
            ])
            ->add('paiementDate', ChoiceType::class, [
                'label' => 'Date de prélèvement',
                'required' => false,
                'choices' => range(1, 28),
                'choice_label' => function ($choice) {
                    return $choice;
                },
            ])
            ->add('isRecurrent', CheckboxType::class, [
                'label' => 'Paiement récurrent (tous les mois) ?',
                'required' => false,
                'label_attr' => ['class' => 'checkbox-inline'],
            ])
            ->add('isDonation', CheckboxType::class, [
                'label' => 'Est-ce une donation (sans participation au programme) ?',
                'required' => false,
                'label_attr' => ['class' => 'checkbox-inline'],
            ])
            ->add('enabled', CheckboxType::class, [
                'label' => 'Activé ?',
                'required' => false,
                'label_attr' => ['class' => 'checkbox-inline'],
            ])
        ;
    }

    /**
     * {@inheritdoc}
     */
    protected function configureListFields(ListMapper $listMapper)
    {
        unset($this->listModes['mosaic']);
        $listMapper
            ->add('user', null, ['label' => 'Utilisateur'])
            ->add('adherent', null, ['label' => 'Adhérent'])
            ->add('amount', null, ['label' => 'Montant'])
            ->add('paiementDate', null, ['label' => 'Date de prélèvement'])
            ->add('isDonation', null, ['label' => 'Donation'])
            ->add('isRecurrent', null, ['label' => 'Récurrent'])
            ->add('createdAt', 'date', [
                'pattern' => 'dd/MM/YYYY HH:mm',
                'label' => 'Création',
            ])
            ->add('updatedAt', 'date', [
                'pattern' => 'dd/MM/YYYY HH:mm',
                'label' => 'Mis à jour',
            ])
            ->add('enabled', null, ['label' => 'Activé ?'])
            ->add('comments', null, [
                'label' => 'Commentaires',
                'editable' => true,
            ])
142 143 144 145 146 147 148 149 150 151 152 153
            // ->add('lastMonthPayed', null, [
            //     'label' => 'Dernier prélèvement',
            //     'template' => '@kohinos/bundles/SonataAdminBundle/CRUD/list_solidoume_lastmonthpayed.html.twig',
            // ])
            ->add('allPrelevement', null, [
                    'label' => 'Prélèvements',
                    'template' => '@kohinos/bundles/SonataAdminBundle/CRUD/list_solidoume_prelevements.html.twig',
                ])
                ->add('allRedistribution', null, [
                    'label' => 'Redistributions',
                    'template' => '@kohinos/bundles/SonataAdminBundle/CRUD/list_solidoume_redistributions.html.twig',
                ])
Julien Jorry committed
154 155
            ->add('_action', null, [
                'actions' => [
156
                    'show' => [],
Julien Jorry committed
157 158 159 160 161
                    'edit' => [],
                ],
            ]);
    }

162 163 164
    /**
     * {@inheritdoc}
     */
Julien Jorry committed
165 166 167 168 169 170
    public function configure()
    {
        parent::configure();
        $this->setTemplate('list', '@kohinos/bundles/SonataAdminBundle/CRUD/solidoume_list.html.twig');
    }

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    /**
     * {@inheritdoc}
     */
    protected function configureShowFields(ShowMapper $showMapper): void
    {
        $object = $showMapper->getAdmin()->getSubject();
        $showMapper
            ->add('createdAt', null, ['label' => 'Date d\'adhésion'])
            ->add('user', null, ['label' => 'Utilisateur'])
            ->add('adherent', null, ['label' => 'Adhérent'])
            ->add('amount', null, ['label' => 'Montant'])
            ->add('paiementDate', null, ['label' => 'Date de prélèvement'])
            ->add('isDonation', null, ['label' => 'Donation'])
            ->add('isRecurrent', null, ['label' => 'Récurrent'])
            ->add('enabled', null, ['label' => 'Activé ?'])
            ->add('comments', null, ['label' => 'Commentaires'])
            ->add('allPrelevement', null, [
                'label' => 'Prélèvements',
                'template' => '@kohinos/bundles/SonataAdminBundle/CRUD/show_solidoume_prelevements.html.twig',
            ])
191 192 193 194 195
            ->add('allRedistribution', null, [
                'label' => 'Redistributions',
                'template' => '@kohinos/bundles/SonataAdminBundle/CRUD/show_solidoume_redistributions.html.twig',
            ])
        ->end()
196
        ;
197
        $this->addLogEntries($showMapper, 'col-md-12');
198 199 200 201 202
    }

    /**
     * {@inheritdoc}
     */
Julien Jorry committed
203 204 205 206 207 208 209 210 211
    protected function configureSideMenu(ItemInterface $menu, string $action, AdminInterface $childAdmin = null)
    {
        if (!$childAdmin && !in_array($action, ['list'])) {
            return;
        }

        $menu->addChild('Paramètres', [
            'uri' => $this->getConfigurationPool()->getContainer()->get('router')->generate('solidoumeparam_list', [], UrlGeneratorInterface::ABSOLUTE_URL),
        ])->setAttribute('icon', 'fa fa-cogs');
212 213 214 215

        $menu->addChild('Redistributions', [
            'uri' => $this->getConfigurationPool()->getContainer()->get('router')->generate('solidoumeparam_redistribution', [], UrlGeneratorInterface::ABSOLUTE_URL),
        ])->setAttribute('icon', 'fa fa-list');
Julien Jorry committed
216 217
    }

218 219 220
    /**
     * {@inheritdoc}
     */
Julien Jorry committed
221 222 223 224 225 226 227 228
    public function getBatchActions()
    {
        $actions = parent::getBatchActions();
        unset($actions['delete']);

        return $actions;
    }

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    public function getExportFields()
    {
        return [
            'Id' => 'id',
            'Adhérent' => 'adherent.fullname',
            'Adhérent email' => 'adherent.email',
            'Date de prélèvement' => 'paiementDate',
            'Montant' => 'amount',
            'Activé ?' => 'enabled',
            'Récurrent ?' => 'isRecurrent',
            'Don ?' => 'isDonation',
            'Crée le' => 'createdAt',
            'Mise à jour le' => 'updatedAt',
            'Commentaires' => 'comments',
        ];
    }

Julien Jorry committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    /**
     * {@inheritdoc}
     */
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
    {
        $datagridMapper
            ->add('enabled', null, [
                'label' => 'Activé',
                'show_filter' => true,
                'advanced_filter' => false,
            ])
            ->add('isRecurrent', null, [
                'label' => 'Récurrent ?',
                'advanced_filter' => false,
            ])
            ->add('isDonation', null, [
                'label' => 'Donation ?',
                'advanced_filter' => false,
            ])
            ->add('createdAt', 'doctrine_orm_datetime_range', [
                'field_type' => DateTimeRangePickerType::class,
                'label' => 'Date de création',
            ])
            ->add('updatedAt', 'doctrine_orm_datetime_range', [
                'field_type' => DateTimeRangePickerType::class,
                'label' => 'Date de mise à jour',
            ])
        ;
    }
}