Completed
Push — master ( e6c0c9...d841f8 )
by Jeroen
35:52 queued 19: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\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
        parent::__construct($em);
52
53
        $this->setAdminType(MediaType::class);
54
55
        $this->folder = $folder;
56
        $this->request = $request;
57
    }
58
59
    /**
60
     * Configure the visible columns
61
     */
62
    public function buildFields()
63
    {
64
        $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...
65
        $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...
66
        $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...
67
        $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...
68
    }
69
70
    /**
71
     * Build filters for admin list
72
     */
73
    public function buildFilters()
74
    {
75
        $this->addFilter('name', new ORM\StringFilterType('name'), 'media.adminlist.configurator.filter.name');
76
        $this->addFilter('contentType', new ORM\StringFilterType('contentType'), 'media.adminlist.configurator.filter.type');
77
        $this->addFilter('updatedAt', new ORM\NumberFilterType('updatedAt'), 'media.adminlist.configurator.filter.updated_at');
78
        $this->addFilter('filesize', new ORM\NumberFilterType('filesize'), 'media.adminlist.configurator.filter.filesize');
79
    }
80
81
    /**
82
     * Return the url to list all the items
83
     *
84
     * @return array
85
     */
86
    public function getIndexUrl()
87
    {
88
        return array(
89
            'path' => $this->request->get('_route'),
90
            'params' => array('folderId' => $this->folder->getId()),
91
        );
92
    }
93
94
    /**
95
     * @param object|array $item
96
     *
97
     * @return bool
98
     */
99
    public function canEdit($item)
100
    {
101
        return false;
102
    }
103
104
    /**
105
     * Configure if it's possible to delete the given $item
106
     *
107
     * @param object|array $item
108
     *
109
     * @return bool
110
     */
111
    public function canDelete($item)
112
    {
113
        return false;
114
    }
115
116
    /**
117
     * Add item actions buttons
118
     */
119
    public function buildItemActions()
120
    {
121
        if ($this->request->get('_route') == 'KunstmaanMediaBundle_chooser_show_folder') {
122
            $this->addItemAction(new MediaSelectItemAction());
123
        } else {
124
            $this->addItemAction(new MediaEditItemAction());
125
            $this->addItemAction(new MediaDeleteItemAction($this->request->getRequestUri()));
126
        }
127
    }
128
129
    /**
130
     * Get bundle name
131
     *
132
     * @return string
133
     */
134
    public function getBundleName()
135
    {
136
        return 'KunstmaanMediaBundle';
137
    }
138
139
    /**
140
     * Get entity name
141
     *
142
     * @return string
143
     */
144
    public function getEntityName()
145
    {
146
        return 'Media';
147
    }
148
149
    /**
150
     * @param QueryBuilder $queryBuilder
151
     */
152
    public function adaptQueryBuilder(QueryBuilder $queryBuilder)
153
    {
154
        $queryBuilder->andWhere('b.folder = :folder')
155
            ->setParameter('folder', $this->folder->getId())
156
            ->andWhere('b.deleted = 0')
157
            ->orderBy('b.updatedAt', 'DESC');
158
159
        if ($this->request->get('_route') == 'KunstmaanMediaBundle_chooser_show_folder') {
160
            $type = $this->request->query->get('type');
161
            if ($type) {
162
                switch ($type) {
163
                    case 'file':
164
                        $queryBuilder->andWhere('b.location = :location')
165
                            ->setParameter('location', 'local');
166
167
                        break;
168
                    case 'image':
169
                        $queryBuilder->andWhere('b.contentType LIKE :ctype')
170
                            ->setParameter('ctype', '%image%');
171
172
                        break;
173
                    case RemoteAudioHandler::TYPE:
174
                        $queryBuilder->andWhere('b.contentType = :ctype')
175
                            ->setParameter('ctype', RemoteAudioHandler::CONTENT_TYPE);
176
177
                        break;
178
                    case RemoteSlideHandler::TYPE:
179
                        $queryBuilder->andWhere('b.contentType = :ctype')
180
                            ->setParameter('ctype', RemoteSlideHandler::CONTENT_TYPE);
181
182
                        break;
183
                    case RemoteVideoHandler::TYPE:
184
                        $queryBuilder->andWhere('b.contentType = :ctype')
185
                            ->setParameter('ctype', RemoteVideoHandler::CONTENT_TYPE);
186
187
                        break;
188
                }
189
            }
190
        }
191
    }
192
}
193