PrestataireoldAdmin.php 6.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php

namespace App\Admin;

use App\Admin\UserAdmin;
use App\Entity\Geoloc;
use App\Entity\Prestataire;
use App\Entity\Usergroup;
use FOS\UserBundle\Model\UserManagerInterface;
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\MediaBundle\Form\Type\MediaType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

21
class PrestataireoldAdmin extends UserAdmin
22
{
23 24
    protected $baseRouteName = 'prestataireo';
    protected $baseRoutePattern = 'prestataireo';
25 26 27 28 29 30 31 32 33 34 35 36

    public function configure()
    {
        parent::configure();
    }

    /**
    * {@inheritdoc}
    */
    protected function configureFormFields(FormMapper $formMapper): void
    {
        // Initialize prestataire
37
        $user = $this->getSubject();
38 39 40 41
        $groupe = $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(Usergroup::class)->findOneByName('Prestataire');
        $user->setEnabled(true);
        $user->addGroup($groupe);
        $user->addRole('ROLE_PRESTATAIRE');
42
        $presta = new Prestataire();
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
        $presta->setGeoloc(new Geoloc());
        $user->setPrestataire($presta);

        // get the current Image instance
        $imageHelp = null;
        if (!empty($user->getPrestataire()) && !empty($user->getPrestataire()->getMedia())) {
            $image = $user->getPrestataire()->getMedia();
            if ($image && ($webPath = $image->getWebPath())) {
                // get the container so the full path to the image can be set
                $container = $this->getConfigurationPool()->getContainer();
                $fullPath = $container->get('request_stack')->getCurrentRequest()->getBasePath().'/'.$webPath;
                // add a 'help' option containing the preview's img tag
                $imageHelp = '<img src="'.$fullPath.'" class="admin-preview" />';
            }
        }

        $formMapper
            ->tab('Prestataire')
                ->with('General', ['class' => 'col-md-7'])
62
                    ->add('prestataire.raison', TextType::class, array(
63 64 65
                        'label' => 'Raison :',
                        'required' => true
                    ))
66
                    ->add('prestataire.statut', TextType::class, array(
67 68 69
                        'label' => 'Statut :',
                        'required' => false
                    ))
70
                    ->add('prestataire.siret', TextType::class, array(
71 72 73
                        'label' => 'SIRET :',
                        'required' => true
                    ))
74
                    ->add('prestataire.iban', TextType::class, array(
75 76 77 78 79
                        'label' => 'IBAN :',
                        'required' => true
                    ))
                ->end()
                ->with('Responsable', ['class' => 'col-md-5'])
80
                    ->add('prestataire.metier', TextType::class, array(
81 82 83
                        'label' => 'Métier :',
                        'required' => true
                    ))
84
                    ->add('prestataire.responsable', TextType::class, array(
85 86 87 88 89
                        'label' => 'Responsable :',
                        'required' => false
                    ))
                ->end()
                ->with('Addresse', ['class' => 'col-md-7'])
90
                    ->add('prestataire.geoloc.adresse', TextType::class, array(
91 92 93
                        'label' => 'Addresse :',
                        'required' => false
                    ))
94
                    ->add('prestataire.geoloc.cpostal', TextType::class, array(
95 96 97
                        'label' => 'Code postal :',
                        'required' => false
                    ))
98
                    ->add('prestataire.geoloc.ville', TextType::class, array(
99 100 101
                        'label' => 'Ville :',
                        'required' => false
                    ))
102
                    ->add('prestataire.geoloc.lat', TextType::class, array(
103 104 105
                        'label' => 'Latitude :',
                        'required' => false
                    ))
106
                    ->add('prestataire.geoloc.lon', TextType::class, array(
107 108 109 110 111
                        'label' => 'Longitude :',
                        'required' => false
                    ))
                ->end()
                ->with('Image', ['class' => 'col-md-5'])
112
                    ->add('prestataire.media', MediaType::class, array(
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
                        'provider' => 'sonata.media.provider.image',
                        'context' => 'prestataire',
                        'help' => $imageHelp
                    ))
                ->end()
            ->end()
        ;
        parent::configureFormFields($formMapper);
    }

    /**
    * {@inheritdoc}
    */
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
    {
        parent::configureDatagridFilters($datagridMapper);
        $datagridMapper
130 131
            ->add('prestataire.raison')
            ->add('prestataire.statut')
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
        ;
    }

    /**
     * @param UserManagerInterface $userManager
     */
    public function setUserManager(UserManagerInterface $userManager): void
    {
        $this->userManager = $userManager;
    }

    /**
     * @return UserManagerInterface
     */
    public function getUserManager()
    {
        return $this->userManager;
    }

    protected function configureListFields(ListMapper $listMapper): void
    {
        unset($this->listModes['mosaic']);
        $listMapper
155 156 157 158 159
            ->addIdentifier('username')
            ->addIdentifier('email')
            ->addIdentifier('prestataire.raison')
            ->addIdentifier('enabled', null, array('label' => 'Activé', 'datatype' => 'App.User', 'template' => '@SonataAdmin/Boolean/editable_boolean.html.twig'))
            ->addIdentifier('createdAt')
160 161
        ;

162 163 164 165 166
        if ($this->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
            $listMapper
                ->addIdentifier('impersonating', 'string', ['template' => '@SonataUser/Admin/Field/impersonating.html.twig'])
            ;
        }
167 168 169 170 171 172 173 174 175 176 177 178 179
    }

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

    /**
    * {@inheritdoc}
    */
    public function createQuery($context = 'list')
    {
        $query = parent::createQuery($context);
180
        $query->andWhere($query->getRootAliases()[0] . '.prestataire IS NOT NULL');
181 182 183
        return $query;
    }
}