InformationPopupAdmin.php 3.43 KB
Newer Older
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
<?php

namespace App\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use FOS\CKEditorBundle\Form\Type\CKEditorType;

/**
 * Administration des popups d'information.
 * Ces popup sont à destination des utilisateurs (adhérents uniquement pour l'instant),
 * et s'ouvrent automatiquement au lancement de l'application, 
 * tant que l'utilisateur n'a pas cliqué sur le bouton de confirmation
 * 
 * KOHINOS : Outil de gestion de Monnaie Locale Complémentaire
 */
class InformationPopupAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $form): void
    {
        $form
        ->add('title', TextType::class, [
            'label' => 'Titre',
            'required' => true,
        ])
        ->add('content', CKEditorType::class, [
            'label' => 'Contenu',
            'required' => true,
        ])
        ->add('validationButtonText', TextType::class, [
            'label' => 'Texte du bouton de validation',
            'required' => true,
            'attr' => [
38 39 40
                'placeholder' => 'Par exemple : "J\'ai compris", "J\'ai bien effectué l\'action"...',
            ],
            'help' => 'Le clic sur ce bouton entraînera la fermeture définitive de cette popup pour cet utilisateur, il est donc conseillé d\'y renseigner un texte explicite.'
41 42 43 44 45
        ])
        ->add('closingButtonText', TextType::class, [
            'label' => 'Texte du bouton de fermeture',
            'required' => true,
            'attr' => [
46
                'placeholder' => 'Par exemple : "Fermer pour l\'instant", "Je le ferai plus tard"...',
47 48 49 50 51 52 53 54
            ]
        ])
        ->add('enabled', null, [
            'label' => 'Activé ?',
            'help' => 'Une seule popup d\'information peut être active à la fois. Activer cette popup désactivera toutes les autres.'
        ]);
    }

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
    public function postPersist($informationPopup)
    {
        $this->disableOtherPopups($informationPopup);
    }

    public function preUpdate($informationPopup)
    {
        $this->disableOtherPopups($informationPopup);
    }

    /**
     * Only one popup should be enabled at a time.
     * If enableling a popup in the admin, disable the rest.
     */
    private function disableOtherPopups($informationPopup)
    {
        if (true == $informationPopup->getEnabled()) {
            $em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager();
    
            $qb = $em->createQueryBuilder();
            
            $qb->update('App\Entity\InformationPopup', 'e')
                ->set('e.enabled', ':newValue')
                ->where('e.id != :id')
                ->setParameter('newValue', 0)
                ->setParameter('id', $informationPopup->getId());
        
            $query = $qb->getQuery();
            $query->execute();
        }
    }

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    protected function configureDatagridFilters(DatagridMapper $datagrid): void
    {
        $datagrid
        ->add('title', null, [
            'label' => 'Titre'
        ]);
    }

    protected function configureListFields(ListMapper $list): void
    {
        $list
        ->addIdentifier('title', null, [
            'label' => 'Titre'
        ])
        ->add('enabled', null, [
            'label' => 'Activé ?',
        ]);
    }
}