Issues (3627)

Entity/DynamicContentRepository.php (2 issues)

1
<?php
2
3
/*
4
 * @copyright   2016 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\DynamicContentBundle\Entity;
13
14
use Doctrine\ORM\Tools\Pagination\Paginator;
15
use Mautic\CoreBundle\Entity\CommonRepository;
16
use Mautic\CoreBundle\Helper\Serializer;
17
18
/**
19
 * DynamicContentRepository.
20
 */
21
class DynamicContentRepository extends CommonRepository
22
{
23
    /**
24
     * Get a list of entities.
25
     *
26
     * @return Paginator
27
     */
28
    public function getEntities(array $args = [])
29
    {
30
        $q = $this->_em
31
            ->createQueryBuilder()
32
            ->select('e')
33
            ->from('MauticDynamicContentBundle:DynamicContent', 'e', 'e.id');
34
35
        if (empty($args['iterator_mode'])) {
36
            $q->leftJoin('e.category', 'c');
37
        }
38
39
        $args['qb'] = $q;
40
41
        return parent::getEntities($args);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::getEntities($args) also could return the type Doctrine\ORM\Internal\Hy...on\IterableResult|array which is incompatible with the documented return type Doctrine\ORM\Tools\Pagination\Paginator.
Loading history...
42
    }
43
44
    /**
45
     * @param \Doctrine\ORM\QueryBuilder|\Doctrine\DBAL\Query\QueryBuilder $q
46
     * @param                                                              $filter
47
     *
48
     * @return array
49
     */
50
    protected function addSearchCommandWhereClause($q, $filter)
51
    {
52
        list($expr, $parameters) = $this->addStandardSearchCommandWhereClause($q, $filter);
53
        if ($expr) {
54
            return [$expr, $parameters];
55
        }
56
57
        list($expr, $parameters) = parent::addSearchCommandWhereClause($q, $filter);
58
        if ($expr) {
59
            return [$expr, $parameters];
60
        }
61
62
        $command         = $filter->command;
63
        $unique          = $this->generateRandomParameterName();
64
        $returnParameter = false; //returning a parameter that is not used will lead to a Doctrine error
65
66
        switch ($command) {
67
            case $this->translator->trans('mautic.core.searchcommand.lang'):
68
                $langUnique      = $this->generateRandomParameterName();
69
                $langValue       = $filter->string.'_%';
70
                $forceParameters = [
71
                    $langUnique => $langValue,
72
                    $unique     => $filter->string,
73
                ];
74
                $expr = $q->expr()->orX(
75
                    $q->expr()->eq('e.language', ":$unique"),
76
                    $q->expr()->like('e.language', ":$langUnique")
77
                );
78
                break;
79
        }
80
81
        if ($expr && $filter->not) {
82
            $expr = $q->expr()->not($expr);
83
        }
84
85
        if (!empty($forceParameters)) {
86
            $parameters = $forceParameters;
87
        } elseif ($returnParameter) {
88
            $string     = ($filter->strict) ? $filter->string : "%{$filter->string}%";
89
            $parameters = ["$unique" => $string];
90
        }
91
92
        return [$expr, $parameters];
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    public function getSearchCommands()
99
    {
100
        $commands = [
101
            'mautic.core.searchcommand.ispublished',
102
            'mautic.core.searchcommand.isunpublished',
103
            'mautic.core.searchcommand.isuncategorized',
104
            'mautic.core.searchcommand.ismine',
105
            'mautic.core.searchcommand.category',
106
            'mautic.core.searchcommand.lang',
107
        ];
108
109
        return array_merge($commands, parent::getSearchCommands());
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    protected function getDefaultOrder()
116
    {
117
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array(array('e.name', 'ASC')) returns the type array<integer,array<integer,string>> which is incompatible with the documented return type string.
Loading history...
118
            ['e.name', 'ASC'],
119
        ];
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    public function getTableAlias()
126
    {
127
        return 'e';
128
    }
129
130
    /**
131
     * Up the sent counts.
132
     *
133
     * @param     $id
134
     * @param int $increaseBy
135
     */
136
    public function upSentCount($id, $increaseBy = 1)
137
    {
138
        $q = $this->_em->getConnection()->createQueryBuilder();
139
140
        $q->update(MAUTIC_TABLE_PREFIX.'dynamic_content')
141
            ->set('sent_count', 'sent_count + '.(int) $increaseBy)
142
            ->where('id = '.(int) $id);
143
144
        $q->execute();
145
    }
146
147
    /**
148
     * @param string $search
149
     * @param int    $limit
150
     * @param int    $start
151
     * @param bool   $viewOther
152
     * @param bool   $topLevel
153
     * @param array  $ignoreIds
154
     * @param string $where
155
     *
156
     * @return array
157
     */
158
    public function getDynamicContentList($search = '', $limit = 10, $start = 0, $viewOther = false, $topLevel = false, $ignoreIds = [], $where = null)
159
    {
160
        $q = $this->createQueryBuilder('e');
161
        $q->select('partial e.{id, name, language}');
162
163
        if (!empty($search)) {
164
            if (is_array($search)) {
165
                $search = array_map('intval', $search);
166
                $q->andWhere($q->expr()->in('e.id', ':search'))
167
                  ->setParameter('search', $search);
168
            } else {
169
                $q->andWhere($q->expr()->like('e.name', ':search'))
170
                  ->setParameter('search', "%{$search}%");
171
            }
172
        }
173
174
        if (!$viewOther) {
175
            $q->andWhere($q->expr()->eq('e.createdBy', ':id'))
176
                ->setParameter('id', $this->currentUser->getId());
177
        }
178
179
        if ('translation' == $topLevel) {
180
            //only get top level pages
181
            $q->andWhere($q->expr()->isNull('e.translationParent'));
182
        } elseif ('variant' == $topLevel) {
183
            $q->andWhere($q->expr()->isNull('e.variantParent'));
184
        }
185
186
        if (!empty($ignoreIds)) {
187
            $q->andWhere($q->expr()->notIn('e.id', ':dwc_ids'))
188
                ->setParameter('dwc_ids', $ignoreIds);
189
        }
190
191
        if ($where) {
192
            $q->andWhere($where);
193
        }
194
195
        $q->orderBy('e.name');
196
197
        if (!empty($limit)) {
198
            $q->setFirstResult($start)
199
                ->setMaxResults($limit);
200
        }
201
202
        return $q->getQuery()->getArrayResult();
203
    }
204
205
    /**
206
     * @param $slot
207
     *
208
     * @return bool|object|null
209
     */
210
    public function getDynamicContentForSlotFromCampaign($slot)
211
    {
212
        $qb = $this->_em->getConnection()->createQueryBuilder();
213
214
        $qb->select('ce.properties')
215
            ->from(MAUTIC_TABLE_PREFIX.'campaign_events', 'ce')
216
            ->leftJoin('ce', MAUTIC_TABLE_PREFIX.'campaigns', 'c', 'c.id = ce.campaign_id')
217
            ->andWhere($qb->expr()->eq('ce.type', $qb->expr()->literal('dwc.decision')))
218
            ->andWhere($qb->expr()->like('ce.properties', ':slot'))
219
            ->setParameter('slot', '%'.$slot.'%')
220
            ->orderBy('c.is_published');
221
222
        $result = $qb->execute()->fetchAll();
223
224
        foreach ($result as $item) {
225
            $properties = Serializer::decode($item['properties']);
226
227
            if (isset($properties['dynamicContent'])) {
228
                $dwc = $this->getEntity($properties['dynamicContent']);
229
230
                if ($dwc instanceof DynamicContent) {
231
                    return $dwc;
232
                }
233
            }
234
        }
235
236
        return false;
237
    }
238
}
239