Completed
Push — master ( e6c0c9...d841f8 )
by Jeroen
35:52 queued 19:21
created

AdminList/TranslationAdminListConfigurator.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\TranslatorBundle\AdminList;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Query\QueryBuilder;
7
use Kunstmaan\AdminListBundle\AdminList\Configurator\AbstractDoctrineDBALAdminListConfigurator;
8
use Kunstmaan\AdminListBundle\AdminList\FilterType\DBAL\EnumerationFilterType;
9
use Kunstmaan\AdminListBundle\AdminList\FilterType\DBAL\StringFilterType;
10
use Kunstmaan\TranslatorBundle\Entity\Translation;
11
12
/**
13
 * TranslationAdminListConfigurator
14
 */
15
class TranslationAdminListConfigurator extends AbstractDoctrineDBALAdminListConfigurator
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $locales;
21
22
    /**
23
     * @var string
24
     */
25
    protected $locale;
26
27
    /**
28
     * @param Connection $connection
29
     * @param array      $locales
30
     */
31
    public function __construct(Connection $connection, array $locales)
32
    {
33
        parent::__construct($connection);
34
        $this->locales = $locales;
35
        $this->setCountField('CONCAT(b.translation_id)');
36
    }
37
38
    /**
39
     * Configure filters
40
     */
41
    public function buildFilters()
42
    {
43
        $this->addFilter('status', new StringFilterType('status'), 'kuma_translator.adminlist.filter.status');
44
        $this->addFilter('domain', new StringFilterType('domain'), 'kuma_translator.adminlist.filter.domain');
45
        $this->addFilter('keyword', new StringFilterType('keyword'), 'kuma_translator.adminlist.filter.keyword');
46
        $this->addFilter('text', new StringFilterType('text'), 'kuma_translator.adminlist.filter.text');
47
        $this->addFilter('locale', new EnumerationFilterType('locale'), 'kuma_translator.adminlist.filter.locale', array_combine(
48
            $this->locales, $this->locales
49
        ));
50
    }
51
52
    /**
53
     * Configure the visible columns
54
     */
55
    public function buildFields()
56
    {
57
        $this->addField('domain', 'kuma_translator.adminlist.header.domain', true);
58
        $this->addField('keyword', 'kuma_translator.adminlist.header.keyword', true);
59
        $this->addField('status', 'kuma_translator.adminlist.header.status', 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...
60
    }
61
62
    /**
63
     * @return bool
64
     */
65
    public function canAdd()
66
    {
67
        return true;
68
    }
69
70
    /**
71
     * @param object|array $item
72
     *
73
     * @return bool
74
     */
75
    public function canEdit($item)
76
    {
77
        return false;
78
    }
79
80
    /**
81
     * @param object|array $item
82
     *
83
     * @return bool
84
     */
85
    public function canEditInline($item)
86
    {
87
        return true;
88
    }
89
90
    /**
91
     * Override path convention (because settings is a virtual admin subtree)
92
     *
93
     * @param string $suffix
94
     *
95
     * @return string
96
     */
97 View Code Duplication
    public function getPathByConvention($suffix = null)
98
    {
99
        if (empty($suffix)) {
100
            return sprintf('%s_settings_%ss', $this->getBundleName(), strtolower($this->getEntityName()));
101
        }
102
103
        return sprintf('%s_settings_%ss_%s', $this->getBundleName(), strtolower($this->getEntityName()), $suffix);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function getAdminType($item)
110
    {
111
        return null;
112
    }
113
114
    public function getBundleName()
115
    {
116
        return 'KunstmaanTranslatorBundle';
117
    }
118
119
    public function getEntityName()
120
    {
121
        return 'Translation';
122
    }
123
124
    public function getControllerPath()
125
    {
126
        return 'KunstmaanTranslatorBundle:Index';
127
    }
128
129
    /**
130
     * @return QueryBuilder|null
131
     */
132
    public function getQueryBuilder()
133
    {
134
        if (is_null($this->queryBuilder)) {
135
            $this->queryBuilder = new QueryBuilder($this->connection);
136
            $this->queryBuilder
137
                ->select('DISTINCT b.translation_id AS id, b.keyword, b.domain, b.status')
138
                ->from('kuma_translation', 'b')
139
                ->andWhere('b.status != :statusstring')
140
                ->setParameter('statusstring', Translation::STATUS_DISABLED);
141
142
            // Apply filters
143
            $filters = $this->getFilterBuilder()->getCurrentFilters();
144
            $locales = array();
145
146
            $textValue = $textComparator = null;
147
            foreach ($filters as $filter) {
148
                if ($filter->getType() instanceof EnumerationFilterType && $filter->getColumnName() == 'locale') {
149
                    // Override default enumeration filter handling ... catch selected locales here
150
                    $data = $filter->getData();
151
                    $comparator = $filter->getType()->getComparator();
152
                    if ($comparator == 'in') {
153
                        $locales = $data['value'];
154
                    } elseif ($comparator == 'notin') {
155
                        $locales = array_diff($this->locales, $data['value']);
156
                    }
157
                } elseif ($filter->getType() instanceof StringFilterType && $filter->getColumnName() == 'text') {
158
                    // Override default text filter handling ...
159
                    $data = $filter->getData();
160
                    $textValue = $data['value'];
161
                    $textComparator = $data['comparator'];
162
                } else {
163
                    /* @var AbstractDBALFilterType $type */
164
                    $type = $filter->getType();
165
                    $type->setQueryBuilder($this->queryBuilder);
166
                    $filter->apply();
167
                }
168
            }
169
170
            if (!empty($locales)) {
171
                $this->locales = $locales;
172
            }
173
            $this->locales = array_unique($this->locales);
174
175
            // Field building hack...
176
            foreach ($this->locales as $locale) {
177
                $this->addField($locale, strtoupper($locale), false, 'KunstmaanTranslatorBundle:Translator:inline_edit.html.twig');
178
            }
179
180
            // Field filter hack...
181
            $this->addFilter('locale', new EnumerationFilterType('locale'), 'kuma_translator.adminlist.filter.locale', array_combine(
182
                $this->locales, $this->locales
183
            ));
184
185
            // Add join for every locale
186
            foreach ($this->locales as $locale) {
187
                $this->queryBuilder->addSelect('t_' . $locale . '.`text` AS ' . $locale);
188
                $this->queryBuilder->addSelect('t_' . $locale . '.id AS ' . $locale . '_id');
189
                $this->queryBuilder->leftJoin('b', 'kuma_translation', 't_' . $locale,
190
                  'b.keyword = t_' . $locale . '.keyword and b.domain = t_' . $locale . '.domain and t_' . $locale . '.locale=:locale_' . $locale);
191
                $this->queryBuilder->setParameter('locale_' . $locale, $locale);
192
            }
193
194
            // Apply text filter
195
            if (!is_null($textValue) && !is_null($textComparator)) {
196
                $orX = $this->queryBuilder->expr()->orX();
197
198
                foreach ($this->locales as $key => $locale) {
199
                    $uniqueId = 'txt_' . $key;
200
                    switch ($textComparator) {
201
                        case 'equals':
202
                            $expr = $this->queryBuilder->expr()->eq('t_' . $locale . '.`text`', ':var_' . $uniqueId);
203
                            $this->queryBuilder->setParameter('var_' . $uniqueId, $textValue);
204
205
                            break;
206
                        case 'notequals':
207
                            $expr = $this->queryBuilder->expr()->neq('t_' . $locale . '.`text`', ':var_' . $uniqueId);
208
                            $this->queryBuilder->setParameter('var_' . $uniqueId, $textValue);
209
210
                            break;
211 View Code Duplication
                        case 'contains':
212
                            $expr = $this->queryBuilder->expr()->like('t_' . $locale . '.`text`', ':var_' . $uniqueId);
213
                            $this->queryBuilder->setParameter('var_' . $uniqueId, '%' . $textValue . '%');
214
215
                            break;
216 View Code Duplication
                        case 'doesnotcontain':
217
                            $expr = 't_' . $locale . '.`text`' . ' NOT LIKE :var_' . $uniqueId;
218
                            $this->queryBuilder->setParameter('var_' . $uniqueId, '%' . $textValue . '%');
219
220
                            break;
221
                        case 'startswith':
222
                            $expr = $this->queryBuilder->expr()->like('t_' . $locale . '.`text`', ':var_' . $uniqueId);
223
                            $this->queryBuilder->setParameter('var_' . $uniqueId, $textValue . '%');
224
225
                            break;
226
                        case 'endswith':
227
                            $expr = $this->queryBuilder->expr()->like('t_' . $locale . '.`text`', ':var_' . $uniqueId);
228
                            $this->queryBuilder->setParameter('var_' . $uniqueId, '%' . $textValue);
229
230
                            break;
231
                    }
232
                    $orX->add($expr);
233
                }
234
235
                $this->queryBuilder->andWhere($orX);
236
            }
237
238
            // Apply sorting
239 View Code Duplication
            if (!empty($this->orderBy)) {
240
                $orderBy = $this->orderBy;
241
                $this->queryBuilder->orderBy($orderBy, ($this->orderDirection == 'DESC' ? 'DESC' : 'ASC'));
242
            }
243
        }
244
245
        return $this->queryBuilder;
246
    }
247
}
248