Issues (3627)

CategoryBundle/Entity/CategoryRepository.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\CategoryBundle\Entity;
13
14
use Doctrine\ORM\Tools\Pagination\Paginator;
15
use Mautic\CoreBundle\Entity\CommonRepository;
16
17
/**
18
 * Class CategoryRepository.
19
 */
20
class CategoryRepository extends CommonRepository
21
{
22
    /**
23
     * Get a list of entities.
24
     *
25
     * @return Paginator
26
     */
27
    public function getEntities(array $args = [])
28
    {
29
        $q = $this
30
            ->createQueryBuilder('c')
31
            ->select('c');
32
33
        $args['qb'] = $q;
34
35
        return parent::getEntities($args);
36
    }
37
38
    /**
39
     * @param        $bundle
40
     * @param string $search
41
     * @param int    $limit
42
     * @param int    $start
43
     *
44
     * @return array
45
     */
46
    public function getCategoryList($bundle, $search = '', $limit = 10, $start = 0, $includeGlobal = true)
47
    {
48
        $q = $this->createQueryBuilder('c');
49
        $q->select('partial c.{id, title, alias, color, bundle}');
50
51
        $q->where('c.isPublished = :true')
52
            ->setParameter('true', true, 'boolean');
53
54
        $expr = $q->expr()->orX(
55
            $q->expr()->eq('c.bundle', ':bundle')
56
        );
57
58
        if ($includeGlobal && 'global' !== $bundle) {
59
            $expr->add(
60
                $q->expr()->eq('c.bundle', $q->expr()->literal('global'))
61
            );
62
        }
63
64
        $q->andWhere($expr)
65
          ->setParameter('bundle', $bundle);
66
67
        if (!empty($search)) {
68
            $q->andWhere($q->expr()->like('c.title', ':search'))
69
                ->setParameter('search', "{$search}%");
70
        }
71
72
        $q->orderBy('c.title');
73
74
        if (!empty($limit)) {
75
            $q->setFirstResult($start)
76
                ->setMaxResults($limit);
77
        }
78
79
        return $q->getQuery()->getArrayResult();
80
    }
81
82
    /**
83
     * @param \Doctrine\ORM\QueryBuilder|\Doctrine\DBAL\Query\QueryBuilder $q
84
     * @param                                                              $filter
85
     *
86
     * @return array
87
     */
88
    protected function addCatchAllWhereClause($q, $filter)
89
    {
90
        return $this->addStandardCatchAllWhereClause($q, $filter, [
91
            'c.title',
92
            'c.description',
93
        ]);
94
    }
95
96
    /**
97
     * @param \Doctrine\ORM\QueryBuilder|\Doctrine\DBAL\Query\QueryBuilder $q
98
     * @param                                                              $filter
99
     *
100
     * @return array
101
     */
102
    protected function addSearchCommandWhereClause($q, $filter)
103
    {
104
        $command                 = $field                 = $filter->command;
105
        $unique                  = $this->generateRandomParameterName();
106
        list($expr, $parameters) = parent::addSearchCommandWhereClause($q, $filter);
107
108
        switch ($command) {
109
            case $this->translator->trans('mautic.core.searchcommand.ispublished'):
110
            case $this->translator->trans('mautic.core.searchcommand.ispublished', [], null, 'en_US'):
111
                $expr                = $q->expr()->eq('c.isPublished', ":$unique");
112
                $parameters[$unique] = true;
113
                break;
114
            case $this->translator->trans('mautic.core.searchcommand.isunpublished'):
115
            case $this->translator->trans('mautic.core.searchcommand.isunpublished', [], null, 'en_US'):
116
                $expr                = $q->expr()->eq('c.isPublished', ":$unique");
117
                $parameters[$unique] = false;
118
                break;
119
        }
120
121
        if ($expr && $filter->not) {
122
            $expr = $q->expr()->not($expr);
0 ignored issues
show
The method not() does not exist on Doctrine\DBAL\Query\Expression\ExpressionBuilder. Did you maybe mean notIn()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
            $expr = $q->expr()->/** @scrutinizer ignore-call */ not($expr);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
123
        }
124
125
        return [
126
            $expr,
127
            $parameters,
128
        ];
129
    }
130
131
    /**
132
     * @return array
133
     */
134
    public function getSearchCommands()
135
    {
136
        $commands = [
137
            'mautic.core.searchcommand.ispublished',
138
            'mautic.core.searchcommand.isunpublished',
139
        ];
140
141
        return array_merge($commands, parent::getSearchCommands());
142
    }
143
144
    /**
145
     * @return string
146
     */
147
    protected function getDefaultOrder()
148
    {
149
        return [
150
            ['c.title', 'ASC'],
151
        ];
152
    }
153
154
    /**
155
     * @param string $bundle
156
     * @param string $alias
157
     * @param object $entity
158
     *
159
     * @return mixed
160
     */
161
    public function checkUniqueCategoryAlias($bundle, $alias, $entity = null)
162
    {
163
        $q = $this->createQueryBuilder('e')
164
            ->select('count(e.id) as aliascount')
165
            ->where('e.alias = :alias')
166
            ->andWhere('e.bundle = :bundle')
167
            ->setParameter('alias', $alias)
168
            ->setParameter('bundle', $bundle);
169
170
        if (!empty($entity) && $entity->getId()) {
171
            $q->andWhere('e.id != :id');
172
            $q->setParameter('id', $entity->getId());
173
        }
174
175
        $results = $q->getQuery()->getSingleResult();
176
177
        return $results['aliascount'];
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    public function getTableAlias()
184
    {
185
        return 'c';
186
    }
187
}
188