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

namespace App\Admin;

use App\Entity\Comptoir;
use App\Entity\Prestataire;
use App\Entity\Siege;
Julien Jorry committed
8
use App\Entity\Rubrique;
9
use FOS\CKEditorBundle\Form\Type\CKEditorType;
Julien Jorry committed
10 11 12 13 14
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;
Julien Jorry committed
15 16 17
use Sonata\MediaBundle\Form\Type\MediaType;
use Sonata\MediaBundle\Provider\Pool;
use Sonata\MediaBundle\Provider\MediaProviderInterface;
Julien Jorry committed
18 19 20 21 22
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;
Julien Jorry committed
23
use Symfony\Component\Security\Core\Security;
Julien Jorry committed
24

25 26 27
/**
 * Administration des rubriques
 *
Julien Jorry committed
28
 * KOHINOS : Outil de gestion de Monnaie Locale Complémentaire
29 30
 * @author Julien Jorry <julien.jorry@gmail.com>
 */
Julien Jorry committed
31 32
class RubriqueAdmin extends AbstractAdmin
{
Julien Jorry committed
33
    protected $security;
Julien Jorry committed
34 35
    protected $datagridValues = [
        '_sort_order' => 'ASC',
Julien Jorry committed
36
        '_sort_by' => 'id',
Julien Jorry committed
37 38
    ];

Julien Jorry committed
39 40 41 42 43 44
    public function configure()
    {
        parent::configure();
    }

    public function setSecurity(Security $security)
Julien Jorry committed
45
    {
Julien Jorry committed
46
        $this->security = $security;
Julien Jorry committed
47 48
    }

Julien Jorry committed
49 50 51 52 53 54 55 56 57 58 59 60 61
    /**
    * {@inheritdoc}
    */
    public function createQuery($context = 'list')
    {
        $query = parent::createQuery($context);
        $query
            ->leftJoin($query->getRootAliases()[0] .'.media', 'm')
            ->addSelect('m')
        ;
        return $query;
    }

Julien Jorry committed
62 63 64 65 66
    /**
    * {@inheritdoc}
    */
    protected function configureFormFields(FormMapper $formMapper)
    {
Julien Jorry committed
67
        $rubrique = $this->getSubject();
Julien Jorry committed
68
        $user = $this->security->getUser();
69
        $prestataires = $this->getConfigurationPool()->getContainer()->get('doctrine')->getRepository(Prestataire::class)->findBy(array('enabled' => true), array('raison'=> 'ASC'));
Julien Jorry committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83

        // get the current Image instance
        $imageHelp = null;
        if (!empty($rubrique) && !empty($rubrique->getMedia())) {
            $image = $rubrique->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" />';
            }
        }

Julien Jorry committed
84 85
        $groupe = $this->getSubject();
        $formMapper
Julien Jorry committed
86
            ->with("Contenu", ['class' => 'col-md-6'])
Julien Jorry committed
87 88 89 90
                ->add('name', TextType::class, array(
                    'label' => 'Nom :',
                    'required' => true
                ))
91 92 93 94
                ->add('content', CKEditorType::class, array(
                    'label' => 'Description :',
                    'required' => false,
                ))
Julien Jorry committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
                ->add('enabled', CheckboxType::class, array(
                    'label' => 'Activé ?',
                    'required' => false,
                    'label_attr' => array('class' => 'checkbox-inline')
                ))
            ->end()
            ->with('Image', ['class' => 'col-md-6'])
                ->add('media', MediaType::class, array(
                    'provider' => 'sonata.media.provider.image',
                    'context' => 'rubrique',
                    // 'help' => $imageHelp,
                    'label' => false,
                    'required' => false
                ))
            ->end()
            ->with('Prestataires', ['class' => 'col-md-6'])
Julien Jorry committed
111
                ->add('prestataires', CollectionType::class, array(
Julien Jorry committed
112
                    'label' => false,
Julien Jorry committed
113 114 115
                    'entry_type' => EntityType::class,
                    'entry_options' => array(
                        'class' => Prestataire::class,
Julien Jorry committed
116
                        'choices' =>  $prestataires,
Julien Jorry committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130
                        // 'choice_label' => 'name',
                        'placeholder' => 'Choisir un prestataire',
                        'required' => false,
                        'label' => false),
                    'by_reference' => false,
                    'allow_add' => true,
                    'allow_delete' => true
                ))
            ->end()
        ;
    }

    protected function configureRoutes(RouteCollection $collection)
    {
Julien Jorry committed
131
        // $collection->remove('delete');
Julien Jorry committed
132 133
    }

Julien Jorry committed
134 135 136 137 138 139 140 141
    // public function getBatchActions()
    // {
    //     $actions = parent::getBatchActions();
    //     unset($actions['delete']);

    //     return $actions;
    // }

Julien Jorry committed
142 143 144 145 146 147 148
    /**
    * {@inheritdoc}
    */
    protected function configureListFields(ListMapper $listMapper)
    {
        unset($this->listModes['mosaic']);
        $listMapper
Julien Jorry committed
149 150
            ->addIdentifier('file', null, array('label' => 'Icône', 'template' => '@SonataAdmin/Image/preview_image_o.html.twig'))
            ->add('name', null, array('editable' => true, 'truncate' => array('length' => 80), 'label' => 'Nom du groupe'))
151
            ->addIdentifier('content', 'html', array('truncate' => array('length' => 80), 'label' => 'Description'))
Julien Jorry committed
152 153 154 155
            ->add('getPrestatairesCount', null, array(
                    'label' => 'Nombre de prestas',
                    'sortable' => false,
            ))
Julien Jorry committed
156
            ->addIdentifier('enabled', null, array('label' => 'Activé', 'datatype' => 'App.Groupeprestataire', 'template' => '@SonataAdmin/Boolean/editable_boolean.html.twig'))
Julien Jorry committed
157 158 159 160 161 162 163
            // You may also specify the actions you want to be displayed in the list
            ->add('_action', null, [
                'actions' => [
                    'edit' => [],
                    'delete' => [],
                ]
            ])
Julien Jorry committed
164 165 166
        ;
    }
}