Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

Configurator/AbstractPageAdminListConfigurator.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\AdminListBundle\AdminList\Configurator;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Query\QueryBuilder;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\EntityRepository;
9
use Kunstmaan\AdminBundle\Entity\EntityInterface;
10
use Kunstmaan\AdminListBundle\AdminList\FilterType\DBAL\BooleanFilterType;
11
use Kunstmaan\AdminListBundle\AdminList\FilterType\DBAL\DateTimeFilterType;
12
use Kunstmaan\AdminListBundle\AdminList\FilterType\DBAL\StringFilterType;
13
14
/**
15
 * An abstract admin list configurator that can be used for pages.
16
 */
17
abstract class AbstractPageAdminListConfigurator extends AbstractDoctrineDBALAdminListConfigurator
18
{
19
    /**
20
     * @var EntityManagerInterface
21
     */
22
    private $em;
23
24
    /**
25
     * @var string $locale
26
     */
27
    private $locale;
28
29
    /**
30
     * @var array
31
     */
32
    private $nodeIds = [];
33
34
    /**
35
     * @var array
36
     */
37
    private $nodeTranslationIds = [];
38
39
    /**
40
     * AbstractPageAdminListConfigurator constructor.
41
     *
42
     * @param EntityManagerInterface $em
43
     * @param string                 $locale
44
     */
45
    public function __construct(EntityManagerInterface $em, $locale)
46
    {
47
        parent::__construct($em->getConnection());
48
        $this->em = $em;
49
        $this->locale = $locale;
50
        $this->setListTemplate('KunstmaanAdminListBundle:Page:list.html.twig');
51
    }
52
53
    /**
54
     * Configure the visible columns
55
     */
56
    public function buildFields()
57
    {
58
        $this->addField('title', 'Title', true, 'KunstmaanAdminListBundle:Page:list-title.html.twig');
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...
59
        $this->addField('online', 'Online', true, 'KunstmaanNodeBundle:Admin:online.html.twig');
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...
60
        $this->addField('updated', 'Updated at', 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...
61
    }
62
63
    /**
64
     * Build filters for admin list
65
     */
66
    public function buildFilters()
67
    {
68
        $this->addFilter('title', new StringFilterType('title'), 'Title');
69
        $this->addFilter('online', new BooleanFilterType('online', 't'), 'Online');
70
        $this->addFilter('updated', new DateTimeFilterType('updated', 'v'), 'Updated at');
71
    }
72
73
    /**
74
     * Get the edit url for the given $item
75
     *
76
     * @param array $item
77
     *
78
     * @return array
0 ignored issues
show
Consider making the return type a bit more specific; maybe use array<string,string|array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
79
     */
80
    public function getEditUrlFor($item)
81
    {
82
        return [
83
            'path' => 'KunstmaanNodeBundle_nodes_edit',
84
            'params' => ['id' => $item['node_id']],
85
86
        ];
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92
    public function getDeleteUrlFor($item)
93
    {
94
        return [
95
            'path' => 'KunstmaanNodeBundle_nodes_delete',
96
            'params' => ['id' => $item['node_id']],
97
        ];
98
    }
99
100
    /**
101
     * Get the fully qualified class name
102
     *
103
     * @return string
104
     */
105
    public function getPageClass()
106
    {
107
        return $this->em->getClassMetadata($this->getRepositoryName())->getName();
108
    }
109
110
    /**
111
     * @param QueryBuilder $queryBuilder
112
     * @param array        $params
113
     */
114
    public function adaptQueryBuilder(QueryBuilder $queryBuilder, array $params = [])
115
    {
116
        $qbQuery = clone $queryBuilder;
117
118
        $qbQuery
119
            ->select('b.id, b.node_id')
120
            ->from('kuma_node_translations', 'b')
121
            ->innerJoin('b', 'kuma_nodes', 'n', 'b.node_id = n.id')
122
            ->where('n.deleted = 0')
123
            ->andWhere('n.ref_entity_name = :class')
124
            ->setParameter('class', $this->getPageClass())
125
            ->addOrderBy('b.updated', 'DESC');
126
127
        // Clone query for next step with same start query.
128
        $qbHelper = clone $qbQuery;
129
        // Get the node translations having current locale.
130
        $this->getCurrentLocaleResults($qbQuery);
131
        // Get the node translations for the other locales, excluding current locale
132
        $this->getOtherLocalesResults($qbHelper);
133
134
        // Make the final query.
135
        $queryBuilder
136
            ->select('b.*')
137
            ->from('kuma_node_translations', 'b')
138
            ->innerJoin('b', 'kuma_nodes', 'n', 'b.node_id = n.id')
139
            ->andWhere('b.id IN (:ids)')
140
            ->setParameter('ids', $this->nodeTranslationIds, Connection::PARAM_STR_ARRAY)
141
            ->orderBy('b.updated', 'DESC');
142
    }
143
144
    /**
145
     * @param QueryBuilder $qb
146
     */
147
    private function getCurrentLocaleResults(QueryBuilder $qb)
148
    {
149
        $results = $qb
150
            ->andWhere('b.lang = :lang')
151
            ->setParameter('lang', $this->locale)
152
            ->execute()
153
            ->fetchAll();
154
155
        foreach ($results as $result) {
156
            $this->nodeIds[] = $result['node_id'];
157
            $this->nodeTranslationIds[] = $result['id'];
158
        }
159
    }
160
161
    /**
162
     * @param QueryBuilder $qb
163
     */
164
    private function getOtherLocalesResults(QueryBuilder $qb)
165
    {
166
        $qb
167
            ->andWhere('b.lang != :lang')
168
            ->setParameter('lang', $this->locale);
169
170
        if (!empty($this->nodeIds)) {
171
            $qb
172
                ->andWhere('b.node_id NOT IN (:ids)')
173
                ->setParameter('ids', $this->nodeIds, Connection::PARAM_STR_ARRAY);
174
        }
175
176
        $results = $qb
177
            ->groupBy('b.node_id')
178
            ->execute()
179
            ->fetchAll();
180
181
        foreach ($results as $result) {
182
            $this->nodeTranslationIds[] = $result['id'];
183
        }
184
    }
185
186
    /**
187
     * Return default repository name.
188
     *
189
     * @return string
190
     */
191
    public function getRepositoryName()
192
    {
193
        return sprintf('%s:%s\%s', $this->getBundleName(), 'Pages', $this->getEntityName());
194
    }
195
196
    /**
197
     * @return EntityInterface
198
     */
199
    abstract public function getOverviewPageClass();
200
201
    /**
202
     * Returns the overviewpage.
203
     */
204
    public function getOverviewPage()
205
    {
206
        /** @var EntityRepository $repository */
207
        $repository = $this->em->getRepository($this->getOverviewPageClass());
208
209
        $overviewPage = $repository->createQueryBuilder('o')
210
            ->orderBy('o.id', 'DESC')
211
            ->setMaxResults(1)
212
            ->getQuery()
213
            ->getOneOrNullResult();
214
215
        return $overviewPage;
216
    }
217
218
    /**
219
     * @return string
220
     */
221
    abstract public function getReadableName();
222
}
223