Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
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\Configurator\ChangeableLimitInterface;
9
use Kunstmaan\AdminListBundle\AdminList\FilterType\ORM;
10
use Kunstmaan\AdminListBundle\Traits\ChangeableLimitTrait;
11
use Kunstmaan\MediaBundle\AdminList\ItemAction\MediaDeleteItemAction;
12
use Kunstmaan\MediaBundle\AdminList\ItemAction\MediaEditItemAction;
13
use Kunstmaan\MediaBundle\AdminList\ItemAction\MediaSelectItemAction;
14
use Kunstmaan\MediaBundle\Entity\Folder;
15
use Kunstmaan\MediaBundle\Form\Type\MediaType;
16
use Kunstmaan\MediaBundle\Helper\MediaManager;
17
use Kunstmaan\MediaBundle\Helper\RemoteAudio\RemoteAudioHandler;
18
use Kunstmaan\MediaBundle\Helper\RemoteSlide\RemoteSlideHandler;
19
use Kunstmaan\MediaBundle\Helper\RemoteVideo\RemoteVideoHandler;
20
use Symfony\Component\HttpFoundation\Request;
21
22
/**
23
 * The admin list configurator for the Media entity
24
 */
25
class MediaAdminListConfigurator extends AbstractDoctrineORMAdminListConfigurator implements ChangeableLimitInterface
26
{
27
    use ChangeableLimitTrait;
28
29
    /**
30
     * @var Folder
31
     */
32
    private $folder;
33
34
    /**
35
     * @var Request
36
     */
37
    private $request;
38
39
    /**
40
     * @param EntityManager $em The entity manager
41
     * @param MediaManager $mediaManager The media manager
42
     * @param Folder $folder The current folder
43
     * @param Request $request The request object
44
     */
45
    public function __construct(
46
        EntityManager $em,
47
        MediaManager $mediaManager,
48
        Folder $folder,
49
        Request $request
50
    )
51
    {
52
        parent::__construct($em);
53
54
        $this->setAdminType(MediaType::class);
55
56
        $this->folder = $folder;
57
        $this->request = $request;
58
    }
59
60
    /**
61
     * Configure the visible columns
62
     */
63
    public function buildFields()
64
    {
65
        $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...
66
        $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...
67
        $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...
68
        $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...
69
    }
70
71
    /**
72
     * Build filters for admin list
73
     */
74
    public function buildFilters()
75
    {
76
        $this->addFilter('name', new ORM\StringFilterType('name'), 'media.adminlist.configurator.filter.name');
77
        $this->addFilter('contentType', new ORM\StringFilterType('contentType'), 'media.adminlist.configurator.filter.type');
78
        $this->addFilter('updatedAt', new ORM\NumberFilterType('updatedAt'), 'media.adminlist.configurator.filter.updated_at');
79
        $this->addFilter('filesize', new ORM\NumberFilterType('filesize'), 'media.adminlist.configurator.filter.filesize');
80
    }
81
82
    /**
83
     * Return the url to list all the items
84
     *
85
     * @return array
86
     */
87
    public function getIndexUrl()
88
    {
89
        return array(
90
            'path' => $this->request->get('_route'),
91
            'params' => array('folderId' => $this->folder->getId())
92
        );
93
    }
94
95
    /**
96
     * @param object|array $item
97
     *
98
     * @return bool
99
     */
100
    public function canEdit($item)
101
    {
102
        return false;
103
    }
104
105
    /**
106
     * Configure if it's possible to delete the given $item
107
     *
108
     * @param object|array $item
109
     *
110
     * @return bool
111
     */
112
    public function canDelete($item)
113
    {
114
        return false;
115
    }
116
117
    /**
118
     * @return int
119
     */
120
    public function getLimit()
121
    {
122
        return $this->limit;
123
    }
124
125
    /**
126
     * @param int $limit
127
     * @return MediaAdminListConfigurator
128
     */
129
    protected function setLimit($limit)
130
    {
131
        $this->limit = $limit;
132
        return $this;
133
    }
134
135
136
137
    /**
138
     * Add item actions buttons
139
     */
140
    public function buildItemActions()
141
    {
142
        if ($this->request->get('_route') == 'KunstmaanMediaBundle_chooser_show_folder') {
143
            $this->addItemAction(new MediaSelectItemAction());
144
        } else {
145
            $this->addItemAction(new MediaEditItemAction());
146
            $this->addItemAction(new MediaDeleteItemAction($this->request->getRequestUri()));
147
        }
148
    }
149
150
    /**
151
     * Get bundle name
152
     *
153
     * @return string
154
     */
155
    public function getBundleName()
156
    {
157
        return 'KunstmaanMediaBundle';
158
    }
159
160
    /**
161
     * Get entity name
162
     *
163
     * @return string
164
     */
165
    public function getEntityName()
166
    {
167
        return 'Media';
168
    }
169
170
    /**
171
     * @param QueryBuilder $queryBuilder
172
     */
173
    public function adaptQueryBuilder(QueryBuilder $queryBuilder)
174
    {
175
        $queryBuilder->andWhere('b.folder = :folder')
176
            ->setParameter('folder', $this->folder->getId())
177
            ->andWhere('b.deleted = 0')
178
            ->orderBy('b.updatedAt', 'DESC');
179
180
        if ($this->request->get('_route') == 'KunstmaanMediaBundle_chooser_show_folder') {
181
            $type = $this->request->query->get('type');
182
            if ($type) {
183
                switch ($type) {
184
                    case 'file':
185
                        $queryBuilder->andWhere('b.location = :location')
186
                            ->setParameter('location', 'local');
187
                        break;
188
                    case 'image':
189
                        $queryBuilder->andWhere('b.contentType LIKE :ctype')
190
                            ->setParameter('ctype', '%image%');
191
                        break;
192
                    case RemoteAudioHandler::TYPE:
193
                        $queryBuilder->andWhere('b.contentType = :ctype')
194
                            ->setParameter('ctype', RemoteAudioHandler::CONTENT_TYPE);
195
                        break;
196
                    case RemoteSlideHandler::TYPE:
197
                        $queryBuilder->andWhere('b.contentType = :ctype')
198
                            ->setParameter('ctype', RemoteSlideHandler::CONTENT_TYPE);
199
                        break;
200
                    case RemoteVideoHandler::TYPE:
201
                        $queryBuilder->andWhere('b.contentType = :ctype')
202
                            ->setParameter('ctype', RemoteVideoHandler::CONTENT_TYPE);
203
                        break;
204
                }
205
            }
206
        }
207
    }
208
}
209