Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

Configurator/AbstractPageAdminListConfigurator.php (1 issue)

Checks whether return doc types can be made more specific.

Documentation Informational

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
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 string $locale
43
     */
44
    public function __construct(EntityManagerInterface $em, $locale)
45
    {
46
        parent::__construct($em->getConnection());
47
        $this->em = $em;
48
        $this->locale = $locale;
49
        $this->setListTemplate('@KunstmaanAdminList/Page/list.html.twig');
50
    }
51
52
    /**
53
     * Configure the visible columns
54
     */
55
    public function buildFields()
56
    {
57
        $this->addField('title', 'Title', true, '@KunstmaanAdminList/Page/list-title.html.twig');
58
        $this->addField('online', 'Online', true, '@KunstmaanNode/Admin/online.html.twig');
59
        $this->addField('updated', 'Updated at', true);
60
    }
61
62
    /**
63
     * Build filters for admin list
64
     */
65
    public function buildFilters()
66
    {
67
        $this->addFilter('title', new StringFilterType('title'), 'Title');
68
        $this->addFilter('online', new BooleanFilterType('online', 't'), 'Online');
69
        $this->addFilter('updated', new DateTimeFilterType('updated', 'v'), 'Updated at');
70
    }
71
72
    /**
73
     * Get the edit url for the given $item
74
     *
75
     * @param array $item
76
     *
77
     * @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...
78
     */
79
    public function getEditUrlFor($item)
80
    {
81
        return [
82
            'path' => 'KunstmaanNodeBundle_nodes_edit',
83
            'params' => ['id' => $item['node_id']],
84
        ];
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getDeleteUrlFor($item)
91
    {
92
        return [
93
            'path' => 'KunstmaanNodeBundle_nodes_delete',
94
            'params' => ['id' => $item['node_id']],
95
        ];
96
    }
97
98
    /**
99
     * Get the fully qualified class name
100
     *
101
     * @return string
102
     */
103
    public function getPageClass()
104
    {
105
        return $this->em->getClassMetadata($this->getRepositoryName())->getName();
106
    }
107
108
    public function adaptQueryBuilder(QueryBuilder $queryBuilder, array $params = [])
109
    {
110
        $qbQuery = clone $queryBuilder;
111
112
        $qbQuery
113
            ->select('b.id, b.node_id')
114
            ->from('kuma_node_translations', 'b')
115
            ->innerJoin('b', 'kuma_nodes', 'n', 'b.node_id = n.id')
116
            ->where('n.deleted = 0')
117
            ->andWhere('n.ref_entity_name = :class')
118
            ->setParameter('class', $this->getPageClass())
119
            ->addOrderBy('b.updated', 'DESC');
120
121
        // Clone query for next step with same start query.
122
        $qbHelper = clone $qbQuery;
123
        // Get the node translations having current locale.
124
        $this->getCurrentLocaleResults($qbQuery);
125
        // Get the node translations for the other locales, excluding current locale
126
        $this->getOtherLocalesResults($qbHelper);
127
128
        // Make the final query.
129
        $queryBuilder
130
            ->select('b.*')
131
            ->from('kuma_node_translations', 'b')
132
            ->innerJoin('b', 'kuma_nodes', 'n', 'b.node_id = n.id')
133
            ->andWhere('b.id IN (:ids)')
134
            ->setParameter('ids', $this->nodeTranslationIds, Connection::PARAM_STR_ARRAY)
135
            ->orderBy('b.updated', 'DESC');
136
    }
137
138
    private function getCurrentLocaleResults(QueryBuilder $qb)
139
    {
140
        $results = $qb
141
            ->andWhere('b.lang = :lang')
142
            ->setParameter('lang', $this->locale)
143
            ->execute()
144
            ->fetchAll();
145
146
        foreach ($results as $result) {
147
            $this->nodeIds[] = $result['node_id'];
148
            $this->nodeTranslationIds[] = $result['id'];
149
        }
150
    }
151
152
    private function getOtherLocalesResults(QueryBuilder $qb)
153
    {
154
        $qb
155
            ->andWhere('b.lang != :lang')
156
            ->setParameter('lang', $this->locale);
157
158
        if (!empty($this->nodeIds)) {
159
            $qb
160
                ->andWhere('b.node_id NOT IN (:ids)')
161
                ->setParameter('ids', $this->nodeIds, Connection::PARAM_STR_ARRAY);
162
        }
163
164
        $results = $qb
165
            ->groupBy('b.node_id')
166
            ->execute()
167
            ->fetchAll();
168
169
        foreach ($results as $result) {
170
            $this->nodeTranslationIds[] = $result['id'];
171
        }
172
    }
173
174
    /**
175
     * Return default repository name.
176
     *
177
     * @return string
178
     */
179
    public function getRepositoryName()
180
    {
181
        return sprintf('%s:%s\%s', $this->getBundleName(), 'Pages', $this->getEntityName());
182
    }
183
184
    /**
185
     * @return EntityInterface
186
     */
187
    abstract public function getOverviewPageClass();
188
189
    /**
190
     * Returns the overviewpage.
191
     */
192
    public function getOverviewPage()
193
    {
194
        /** @var EntityRepository $repository */
195
        $repository = $this->em->getRepository($this->getOverviewPageClass());
196
197
        $overviewPage = $repository->createQueryBuilder('o')
198
            ->orderBy('o.id', 'DESC')
199
            ->setMaxResults(1)
200
            ->getQuery()
201
            ->getOneOrNullResult();
202
203
        return $overviewPage;
204
    }
205
206
    /**
207
     * @return string
208
     */
209
    abstract public function getReadableName();
210
}
211