Completed
Pull Request — 5.0 (#2104)
by Kevin
13:45 queued 04:21
created

AdminList/MediaAdminListConfigurator.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\MediaBundle\AdminList;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\QueryBuilder;
7
use Kunstmaan\AdminListBundle\AdminList\Configurator\AbstractDoctrineORMAdminListConfigurator;
8
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM;
9
use Kunstmaan\MediaBundle\AdminList\ItemAction\MediaDeleteItemAction;
10
use Kunstmaan\MediaBundle\AdminList\ItemAction\MediaEditItemAction;
11
use Kunstmaan\MediaBundle\AdminList\ItemAction\MediaSelectItemAction;
12
use Kunstmaan\MediaBundle\Entity\Folder;
13
use Kunstmaan\MediaBundle\Form\Type\MediaType;
14
use Kunstmaan\MediaBundle\Helper\MediaManager;
15
use Kunstmaan\MediaBundle\Helper\RemoteAudio\RemoteAudioHandler;
16
use Kunstmaan\MediaBundle\Helper\RemoteSlide\RemoteSlideHandler;
17
use Kunstmaan\MediaBundle\Helper\RemoteVideo\RemoteVideoHandler;
18
use Symfony\Component\HttpFoundation\Request;
19
20
/**
21
 * The admin list configurator for the Media entity
22
 */
23
class MediaAdminListConfigurator extends AbstractDoctrineORMAdminListConfigurator
24
{
25
    /**
26
     * @var Folder
27
     */
28
    private $folder;
29
30
    /**
31
     * @var Request
32
     */
33
    private $request;
34
35
    /**
36
     * @var int $limit
37
     */
38
    private $limit;
39
40
    /**
41
     * @param EntityManager $em The entity manager
42
     * @param MediaManager $mediaManager The media manager
43
     * @param Folder $folder The current folder
44
     * @param Request $request The request object
45
     */
46
    public function __construct(
47
        EntityManager $em,
48
        MediaManager $mediaManager,
49
        Folder $folder,
50
        Request $request
51
    )
52
    {
53
        parent::__construct($em);
54
55
        $this->setAdminType(MediaType::class);
56
57
        $this->folder = $folder;
58
        $this->request = $request;
59
60
        $this->limit = 24;
61
    }
62
63
    /**
64
     * Configure the visible columns
65
     */
66
    public function buildFields()
67
    {
68
        $this->addField('name', 'media.adminlist.configurator.name', true);
0 ignored issues
show
true is of type boolean, but the function expects a string.

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...
69
        $this->addField('contentType', 'media.adminlist.configurator.type', true);
0 ignored issues
show
true is of type boolean, but the function expects a string.

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...
70
        $this->addField('updatedAt', 'media.adminlist.configurator.date', true);
0 ignored issues
show
true is of type boolean, but the function expects a string.

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...
71
        $this->addField('filesize', 'media.adminlist.configurator.filesize', true);
0 ignored issues
show
true is of type boolean, but the function expects a string.

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...
72
    }
73
74
    /**
75
     * Build filters for admin list
76
     */
77
    public function buildFilters()
78
    {
79
        $this->addFilter('name', new ORM\StringFilterType('name'), 'media.adminlist.configurator.filter.name');
80
        $this->addFilter('contentType', new ORM\StringFilterType('contentType'), 'media.adminlist.configurator.filter.type');
81
        $this->addFilter('updatedAt', new ORM\NumberFilterType('updatedAt'), 'media.adminlist.configurator.filter.updated_at');
82
        $this->addFilter('filesize', new ORM\NumberFilterType('filesize'), 'media.adminlist.configurator.filter.filesize');
83
    }
84
85
    /**
86
     * Return the url to list all the items
87
     *
88
     * @return array
89
     */
90
    public function getIndexUrl()
91
    {
92
        return array(
93
            'path' => $this->request->get('_route'),
94
            'params' => array('folderId' => $this->folder->getId())
95
        );
96
    }
97
98
    /**
99
     * @param object|array $item
100
     *
101
     * @return bool
102
     */
103
    public function canEdit($item)
104
    {
105
        return false;
106
    }
107
108
    /**
109
     * Configure if it's possible to delete the given $item
110
     *
111
     * @param object|array $item
112
     *
113
     * @return bool
114
     */
115
    public function canDelete($item)
116
    {
117
        return false;
118
    }
119
120
    /**
121
     * @return int
122
     */
123
    public function getLimit()
124
    {
125
        return $this->limit;
126
    }
127
128
    /**
129
     * @param int $limit
130
     * @return MediaAdminListConfigurator
131
     */
132
    protected function setLimit($limit)
133
    {
134
        $this->limit = $limit;
135
        return $this;
136
    }
137
138
139
140
    /**
141
     * Add item actions buttons
142
     */
143
    public function buildItemActions()
144
    {
145
        if ($this->request->get('_route') == 'KunstmaanMediaBundle_chooser_show_folder') {
146
            $this->addItemAction(new MediaSelectItemAction());
147
        } else {
148
            $this->addItemAction(new MediaEditItemAction());
149
            $this->addItemAction(new MediaDeleteItemAction($this->request->getRequestUri()));
150
        }
151
    }
152
153
    /**
154
     * Get bundle name
155
     *
156
     * @return string
157
     */
158
    public function getBundleName()
159
    {
160
        return 'KunstmaanMediaBundle';
161
    }
162
163
    /**
164
     * Get entity name
165
     *
166
     * @return string
167
     */
168
    public function getEntityName()
169
    {
170
        return 'Media';
171
    }
172
173
    /**
174
     * @param QueryBuilder $queryBuilder
175
     */
176
    public function adaptQueryBuilder(QueryBuilder $queryBuilder)
177
    {
178
        $queryBuilder->andWhere('b.folder = :folder')
179
            ->setParameter('folder', $this->folder->getId())
180
            ->andWhere('b.deleted = 0')
181
            ->orderBy('b.updatedAt', 'DESC');
182
183
        if ($this->request->get('_route') == 'KunstmaanMediaBundle_chooser_show_folder') {
184
            $type = $this->request->query->get('type');
185
            if ($type) {
186
                switch ($type) {
187
                    case 'file':
188
                        $queryBuilder->andWhere('b.location = :location')
189
                            ->setParameter('location', 'local');
190
                        break;
191
                    case 'image':
192
                        $queryBuilder->andWhere('b.contentType LIKE :ctype')
193
                            ->setParameter('ctype', '%image%');
194
                        break;
195
                    case RemoteAudioHandler::TYPE:
196
                        $queryBuilder->andWhere('b.contentType = :ctype')
197
                            ->setParameter('ctype', RemoteAudioHandler::CONTENT_TYPE);
198
                        break;
199
                    case RemoteSlideHandler::TYPE:
200
                        $queryBuilder->andWhere('b.contentType = :ctype')
201
                            ->setParameter('ctype', RemoteSlideHandler::CONTENT_TYPE);
202
                        break;
203
                    case RemoteVideoHandler::TYPE:
204
                        $queryBuilder->andWhere('b.contentType = :ctype')
205
                            ->setParameter('ctype', RemoteVideoHandler::CONTENT_TYPE);
206
                        break;
207
                }
208
            }
209
        }
210
    }
211
}
212