ImportAdmin.php 4.24 KB
Newer Older
Damien Moulard 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 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
<?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\Form\Type\Filter\ChoiceType;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Translation\TranslatorInterface;

/**
 * Import de données
 *
 * KOHINOS : Outil de gestion de Monnaie Locale Complémentaire
 * @author Julien Jorry <julien.jorry@gmail.com>
 */
class ImportAdmin extends AbstractAdmin
{
    protected $baseRoutePattern = 'importdata';
    protected $baseRouteName = 'importdata';
    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' => 32
    ];

    /**
    * {@inheritdoc}
    */
    protected function configureRoutes(RouteCollection $collection)
    {
        $collection->clearExcept(array('list', 'create', 'show'));
    }

    /**
    * {@inheritdoc}
    */
    public function createQuery($context = 'list')
    {
        $query = parent::createQuery($context);
        $query
            ->innerJoin($query->getRootAliases()[0] .'.user', 'u')
            ->addSelect('u')
        ;
        return $query;
    }

    public function configureActionButtons($action, $object = null)
    {
        $list = parent::configureActionButtons($action, $object);

        $list['create']['template'] = '@SonataAdmin/CRUD/import_button.html.twig';
        // $list['show']['template'] = '@SonataAdmin/CRUD/import_button.html.twig';

        return $list;
    }

    protected function configureListFields(ListMapper $listMapper): void
    {
        parent::configureListFields($listMapper);
        unset($this->listModes['mosaic']);
        $listMapper
            ->addIdentifier('createdAt', 'datetime', ['label' => 'Date'])
            ->add('user', null, ['label' => 'Utilisateur'])
            ->addIdentifier('type')
            ->add('nbentityadded', null, ['label' => 'Lignes correctes'])
            ->add('nbentityerror', null, ['label' => 'Lignes en erreurs'])
            ->add('media', null, ['label' => 'Fichier'])
            ->add('_action', null, [
                'actions' => [
                    'show' => [],
                ]
            ])
        ;
    }

    protected function configureShowFields(ShowMapper $showMapper): void
    {
        $object = $showMapper->getAdmin()->getSubject();
        $showMapper
            ->with('Import')
                ->add('createdAt')
                ->add('user')
                ->add('type')
                ->add('media')
                ->add('nbentityadded', null, ['label' => 'Lignes correctes'])
                ->add('nbentityerror', null, ['label' => 'Lignes en erreurs'])
            ->end();
        if (!empty(json_decode($object->getSuccess()))) {
            $showMapper
                ->with('Correct', [
                        'box_class'   => 'box box-solid box-success',
                        // 'description' => 'Lorem ipsum',
                    ])
                    ->add('success', 'json', ['label' => false])
                ->end()
            ;
        }
        if (!empty(json_decode($object->getWarnings()))) {
            $showMapper
                ->with('Warnings', [
                    'box_class'   => 'box box-solid box-warning',
                    // 'description' => 'Lorem ipsum',
                ])
                ->add('warnings', 'json', ['label' => false])
                ->end()
                ;
        }
        if (!empty(json_decode($object->getErrors()))) {
            $showMapper
                ->with('Erreurs', [
                        'box_class'   => 'box box-solid box-danger',
                        // 'description' => 'Lorem ipsum',
                    ])
                    ->add('errors', 'json', ['label' => false])
                ->end()
            ;
        }
        ;
    }

    public function getBatchActions()
    {
        $actions = parent::getBatchActions();
        unset($actions['delete']);

        return $actions;
    }
}