GroupeAdmin.php 8.14 KB
Newer Older
Julien Jorry committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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
<?php

namespace App\Admin;

use App\Entity\AccountGroupe;
use App\Entity\Comptoir;
use App\Entity\GlobalParameter;
use App\Entity\Siege;
use App\Enum\CurrencyEnum;
use FOS\CKEditorBundle\Form\Type\CKEditorType;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\DoctrineORMAdminBundle\Filter\CallbackFilter;
use Sonata\FormatterBundle\Form\Type\SimpleFormatterType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

/**
 * Administration des groupes locaux.
 *
 * KOHINOS : Outil de gestion de Monnaie Locale Complémentaire
 *
 * @author Julien Jorry <julien.jorry@gmail.com>
 */
class GroupeAdmin extends AbstractAdmin
{
    protected $datagridValues = [
        '_sort_order' => 'ASC',
        '_sort_by' => 'name',
    ];

    public function configure()
    {
        parent::configure();
        $this->classnameLabel = 'Groupes locaux';
    }

    /**
     * {@inheritdoc}
     */
    protected function configureShowFields(ShowMapper $showMapper)
    {
    }

    /**
     * {@inheritdoc}
     */
    protected function configureFormFields(FormMapper $formMapper)
    {
        $groupe = $this->getSubject();
        $formMapper
            ->with("Création d'un groupe")
                ->add('siege', HiddenType::class, [
                    'data' => $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager()->getRepository(Siege::class)->getTheOne(),
                    'data_class' => null,
                    'entity_class' => Siege::class,
                    'em' => $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager(),
                ])
                ->add('name', TextType::class, [
                    'label' => 'Nom du groupe :',
                    'required' => true,
                ])
                ->add('content', CKEditorType::class, [
                    'label' => 'Description :',
                    'required' => false,
                ])
                // ->add('content', SimpleFormatterType::class, [
                //     'format' => 'richhtml',
                //     'ckeditor_context' => 'default',
                //     'label' => 'Description :',
                //     'required' => false,
                // ])
                ->add('comptoirs', CollectionType::class, [
                    'label' => 'Comptoirs',
                    'entry_type' => EntityType::class,
                    'entry_options' => [
                        'class' => Comptoir::class,
                        'choices' => $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(Comptoir::class)->findAll(),
                        'choice_label' => 'slug',
                        'placeholder' => 'Comptoir',
                        'required' => false,
                        'label' => false, ],
                    'by_reference' => false,
                    'allow_add' => true,
                    'allow_delete' => true,
93 94 95
                ]);
        if (count($groupe->getPrestataires()) > 0) {
            $formMapper
Julien Jorry committed
96 97
                ->add('enabled', CheckboxType::class, [
                    'label' => 'Activé ?',
98
                    'help' => '(impossible de désactiver un groupe local contenant au moins 1 prestataire',
Julien Jorry committed
99
                    'required' => false,
100
                    'disabled' => true,
Julien Jorry committed
101
                    'label_attr' => ['class' => 'checkbox-inline'],
102 103 104 105 106 107 108 109 110 111
                ]);
        } else {
            $formMapper
                ->add('enabled', CheckboxType::class, [
                    'label' => 'Activé ?',
                    'required' => false,
                    'label_attr' => ['class' => 'checkbox-inline'],
                ]);
        }
        $formMapper->end();
Julien Jorry committed
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    }

    /**
     * {@inheritdoc}
     */
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
    {
        $datagridMapper
            ->add('full_text', CallbackFilter::class, [
                'callback' => [$this, 'getFullTextFilter'],
                'field_type' => TextType::class,
                'label' => 'Recherche par nom',
                'show_filter' => true,
                'advanced_filter' => false,
            ])
        ;
    }

    public function getFullTextFilter($queryBuilder, $alias, $field, $value)
    {
        if (!$value['value']) {
            return;
        }

        // Use `andWhere` instead of `where` to prevent overriding existing `where` conditions
        $queryBuilder->andWhere(
            $queryBuilder->expr()->like($alias . '.name', $queryBuilder->expr()->literal('%' . $value['value'] . '%'))
        );

        return true;
    }

    protected function configureRoutes(RouteCollection $collection)
    {
        $collection->remove('delete');
    }

    public function getRouteShowOnFront($object)
    {
        return $this->routeGenerator->generate('show_groupe', ['slug' => $object->getSlug()]);
    }

    /**
     * {@inheritdoc}
     */
    protected function configureListFields(ListMapper $listMapper)
    {
        unset($this->listModes['mosaic']);

        $isWordpressActivated = $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(GlobalParameter::class)->val(GlobalParameter::USE_WORDPRESS);
        if ('false' == $isWordpressActivated) {
            $actions = [
                'show' => ['template' => '@kohinos/bundles/SonataAdminBundle/CRUD/list__action_showonfront.html.twig'],
                'edit' => [],
            ];
        } else {
            $actions = [
                'edit' => [],
            ];
        }

        $listMapper
            ->addIdentifier('name', null, ['label' => 'Nom du groupe'])
            ->add('gestionnaires', null, ['label' => 'Gestionnaires'])
            ->add('content', 'html', ['truncate' => ['length' => 80], 'label' => 'Description'])
177
            ->add('balance', null, ['label' => 'Solde', 'template' => '@kohinos/block/balance.html.twig', 'currency' => 'mlc'])
Julien Jorry committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
            ->add(
                'getPrestatairesCount',
                null,
                [
                    'label' => 'Nombre de prestas',
                    'sortable' => true,
                    'sort_field_mapping' => ['fieldName' => 'id'],
                    'sort_parent_association_mappings' => [],
                ]
            )->add(
                'getAdherentsCount',
                null,
                [
                    'label' => 'Nb adherents',
                    'sortable' => true,
                    'sort_field_mapping' => ['fieldName' => 'id'],
                    'sort_parent_association_mappings' => [],
                ]
            )
            ->add(
                'getComptoirsCount',
                null,
                [
                    'label' => 'Nb comptoirs',
                    'sortable' => true,
                    'sort_field_mapping' => ['fieldName' => 'id'],
                    'sort_parent_association_mappings' => [],
                ]
            )
            ->add('enabled', null, [
208
                'label' => 'Activé',
Julien Jorry committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
            ])
            ->add('_action', null, [
                'actions' => $actions,
            ])
        ;
    }

    public function preUpdate($groupe)
    {
        $this->prePersist($groupe);
    }

    public function prePersist($groupe)
    {
        $em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager();

        $account = $em->getRepository(AccountGroupe::class)->findOneBy(['groupe' => $groupe, 'currency' => CurrencyEnum::CURRENCY_MLC]);
        if (null == $account) {
            $account = new AccountGroupe();
            $account
                ->setCurrency(CurrencyEnum::CURRENCY_MLC)
            ;
            $groupe->addAccount($account);
            $em->persist($account);
        }
        $em->persist($groupe);
        $em->flush();
    }
}