Completed
Push — master ( 27e061...d911fa )
by Grégoire
12s
created

GalleryAdmin::preUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Admin;
15
16
use Sonata\AdminBundle\Admin\AbstractAdmin;
17
use Sonata\AdminBundle\Datagrid\DatagridMapper;
18
use Sonata\AdminBundle\Datagrid\ListMapper;
19
use Sonata\AdminBundle\Form\FormMapper;
20
use Sonata\CoreBundle\Form\Type\CollectionType;
21
use Sonata\MediaBundle\Provider\Pool;
22
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
23
24
class GalleryAdmin extends AbstractAdmin
25
{
26
    /**
27
     * @var Pool
28
     */
29
    protected $pool;
30
31
    /**
32
     * @param string $code
33
     * @param string $class
34
     * @param string $baseControllerName
35
     * @param Pool   $pool
36
     */
37
    public function __construct($code, $class, $baseControllerName, Pool $pool)
38
    {
39
        parent::__construct($code, $class, $baseControllerName);
40
41
        $this->pool = $pool;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function prePersist($gallery): void
48
    {
49
        $parameters = $this->getPersistentParameters();
50
51
        $gallery->setContext($parameters['context']);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getPersistentParameters()
58
    {
59
        $parameters = parent::getPersistentParameters();
60
61
        if (!$this->hasRequest()) {
62
            return $parameters;
63
        }
64
65
        return array_merge($parameters, [
66
            'context' => $this->getRequest()->get('context', $this->pool->getDefaultContext()),
67
        ]);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getNewInstance()
74
    {
75
        $gallery = parent::getNewInstance();
76
77
        if ($this->hasRequest()) {
78
            $gallery->setContext($this->getRequest()->get('context'));
79
        }
80
81
        return $gallery;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    protected function configureFormFields(FormMapper $formMapper): void
88
    {
89
        // define group zoning
90
        $formMapper
91
            ->with('Gallery', ['class' => 'col-md-9'])->end()
92
            ->with('Options', ['class' => 'col-md-3'])->end()
93
        ;
94
95
        $context = $this->getPersistentParameter('context');
96
97
        if (!$context) {
98
            $context = $this->pool->getDefaultContext();
99
        }
100
101
        $formats = [];
102
        foreach ((array) $this->pool->getFormatNamesByContext($context) as $name => $options) {
103
            $formats[$name] = $name;
104
        }
105
106
        $contexts = [];
107
        foreach ((array) $this->pool->getContexts() as $contextItem => $format) {
108
            $contexts[$contextItem] = $contextItem;
109
        }
110
111
        $formMapper
112
            ->with('Options')
113
                ->add('context', ChoiceType::class, ['choices' => $contexts])
114
                ->add('enabled', null, ['required' => false])
115
                ->add('name')
116
                ->ifTrue($formats)
0 ignored issues
show
Documentation introduced by
$formats is of type array, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
                    ->add('defaultFormat', ChoiceType::class, ['choices' => $formats])
118
                ->ifEnd()
119
            ->end()
120
            ->with('Gallery')
121
                ->add('galleryItems', CollectionType::class, ['by_reference' => false], [
122
                    'edit' => 'inline',
123
                    'inline' => 'table',
124
                    'sortable' => 'position',
125
                    'link_parameters' => ['context' => $context],
126
                    'admin_code' => 'sonata.media.admin.gallery_item',
127
                ])
128
            ->end()
129
        ;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    protected function configureListFields(ListMapper $listMapper): void
136
    {
137
        $listMapper
138
            ->addIdentifier('name')
139
            ->add('enabled', 'boolean', ['editable' => true])
140
            ->add('context', 'trans', ['catalogue' => 'SonataMediaBundle'])
141
            ->add('defaultFormat', 'trans', ['catalogue' => 'SonataMediaBundle'])
142
        ;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
149
    {
150
        $datagridMapper
151
            ->add('name')
152
            ->add('enabled')
153
            ->add('context', null, [
154
                'show_filter' => false,
155
            ])
156
        ;
157
    }
158
}
159