Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

Configurator/AbstractPageAdminListConfigurator.php (1 issue)

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