Issues (3627)

app/bundles/SmsBundle/Entity/SmsRepository.php (1 issue)

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\SmsBundle\Entity;
13
14
use Doctrine\ORM\Query;
15
use Doctrine\ORM\Tools\Pagination\Paginator;
16
use Mautic\CoreBundle\Entity\CommonRepository;
17
18
/**
19
 * Class SmsRepository.
20
 */
21
class SmsRepository 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($this->getTableAlias())
33
            ->from('MauticSmsBundle:Sms', $this->getTableAlias(), $this->getTableAlias().'.id');
34
35
        if (empty($args['iterator_mode'])) {
36
            $q->leftJoin($this->getTableAlias().'.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 null $id
46
     *
47
     * @return \Doctrine\ORM\Internal\Hydration\IterableResult
48
     */
49
    public function getPublishedBroadcasts($id = null)
50
    {
51
        $qb   = $this->createQueryBuilder($this->getTableAlias());
52
        $expr = $this->getPublishedByDateExpression($qb, null, true, true, false);
53
54
        $expr->add(
55
            $qb->expr()->eq($this->getTableAlias().'.smsType', $qb->expr()->literal('list'))
56
        );
57
58
        if (!empty($id)) {
59
            $expr->add(
60
                $qb->expr()->eq($this->getTableAlias().'.id', (int) $id)
61
            );
62
        }
63
        $qb->where($expr);
64
65
        return $qb->getQuery()->iterate();
66
    }
67
68
    /**
69
     * @return \Doctrine\DBAL\Query\QueryBuilder
70
     */
71
    public function getSegmentsContactsQuery(int $smsId)
72
    {
73
        // Main query
74
        $q = $this->getEntityManager()->getConnection()->createQueryBuilder();
75
        $q->from('sms_message_list_xref', 'sml')
76
            ->join('sml', MAUTIC_TABLE_PREFIX.'lead_lists', 'll', 'll.id = sml.leadlist_id and ll.is_published = 1')
77
            ->join('ll', MAUTIC_TABLE_PREFIX.'lead_lists_leads', 'lll', 'lll.leadlist_id = sml.leadlist_id and lll.manually_removed = 0')
78
            ->join('lll', MAUTIC_TABLE_PREFIX.'leads', 'l', 'lll.lead_id = l.id')
79
            ->where(
80
                $q->expr()->andX(
81
                    $q->expr()->eq('sml.sms_id', ':smsId')
82
                )
83
            )
84
            ->setParameter('smsId', $smsId)
85
            // Order by ID so we can query by greater than X contact ID when batching
86
            ->orderBy('lll.lead_id');
87
88
        return $q;
89
    }
90
91
    /**
92
     * Get amounts of sent and read emails.
93
     *
94
     * @return array
95
     */
96
    public function getSentCount()
97
    {
98
        $q = $this->_em->createQueryBuilder();
99
        $q->select('SUM(e.sentCount) as sent_count')
100
            ->from('MauticSmsBundle:Sms', 'e');
101
        $results = $q->getQuery()->getSingleResult(Query::HYDRATE_ARRAY);
102
103
        if (!isset($results['sent_count'])) {
104
            $results['sent_count'] = 0;
105
        }
106
107
        return $results;
108
    }
109
110
    /**
111
     * @param \Doctrine\ORM\QueryBuilder|\Doctrine\DBAL\Query\QueryBuilder $q
112
     * @param                                                              $filter
113
     *
114
     * @return array
115
     */
116
    protected function addSearchCommandWhereClause($q, $filter)
117
    {
118
        list($expr, $parameters) = $this->addStandardSearchCommandWhereClause($q, $filter);
119
        if ($expr) {
120
            return [$expr, $parameters];
121
        }
122
123
        $command         = $filter->command;
124
        $unique          = $this->generateRandomParameterName();
125
        $returnParameter = false; //returning a parameter that is not used will lead to a Doctrine error
126
127
        switch ($command) {
128
            case $this->translator->trans('mautic.core.searchcommand.lang'):
129
                $langUnique      = $this->generateRandomParameterName();
130
                $langValue       = $filter->string.'_%';
131
                $forceParameters = [
132
                    $langUnique => $langValue,
133
                    $unique     => $filter->string,
134
                ];
135
                $expr = $q->expr()->orX(
136
                    $q->expr()->eq('e.language', ":$unique"),
137
                    $q->expr()->like('e.language', ":$langUnique")
138
                );
139
                $returnParameter = true;
140
                break;
141
        }
142
143
        if ($expr && $filter->not) {
144
            $expr = $q->expr()->not($expr);
145
        }
146
147
        if (!empty($forceParameters)) {
148
            $parameters = $forceParameters;
149
        } elseif ($returnParameter) {
150
            $string     = ($filter->strict) ? $filter->string : "%{$filter->string}%";
151
            $parameters = ["$unique" => $string];
152
        }
153
154
        return [$expr, $parameters];
155
    }
156
157
    /**
158
     * @return array
159
     */
160
    public function getSearchCommands()
161
    {
162
        $commands = [
163
            'mautic.core.searchcommand.ispublished',
164
            'mautic.core.searchcommand.isunpublished',
165
            'mautic.core.searchcommand.isuncategorized',
166
            'mautic.core.searchcommand.ismine',
167
            'mautic.core.searchcommand.category',
168
            'mautic.core.searchcommand.lang',
169
        ];
170
171
        return array_merge($commands, parent::getSearchCommands());
172
    }
173
174
    /**
175
     * @return string
176
     */
177
    protected function getDefaultOrder()
178
    {
179
        return [
180
            ['e.name', 'ASC'],
181
        ];
182
    }
183
184
    /**
185
     * @return string
186
     */
187
    public function getTableAlias()
188
    {
189
        return 'e';
190
    }
191
192
    /**
193
     * Up the click/sent counts.
194
     *
195
     * @param        $id
196
     * @param string $type
197
     * @param int    $increaseBy
198
     */
199
    public function upCount($id, $type = 'sent', $increaseBy = 1)
200
    {
201
        try {
202
            $q = $this->_em->getConnection()->createQueryBuilder();
203
204
            $q->update(MAUTIC_TABLE_PREFIX.'sms_messages')
205
                ->set($type.'_count', $type.'_count + '.(int) $increaseBy)
206
                ->where('id = '.(int) $id);
207
208
            $q->execute();
209
        } catch (\Exception $exception) {
210
            // not important
211
        }
212
    }
213
214
    /**
215
     * @param string $search
216
     * @param int    $limit
217
     * @param int    $start
218
     * @param bool   $viewOther
219
     * @param string $smsType
220
     *
221
     * @return array
222
     */
223
    public function getSmsList($search = '', $limit = 10, $start = 0, $viewOther = false, $smsType = null)
224
    {
225
        $q = $this->createQueryBuilder('e');
226
        $q->select('partial e.{id, name, language}');
227
228
        if (!empty($search)) {
229
            if (is_array($search)) {
230
                $search = array_map('intval', $search);
231
                $q->andWhere($q->expr()->in('e.id', ':search'))
232
                  ->setParameter('search', $search);
233
            } else {
234
                $q->andWhere($q->expr()->like('e.name', ':search'))
235
                  ->setParameter('search', "%{$search}%");
236
            }
237
        }
238
239
        if (!$viewOther) {
240
            $q->andWhere($q->expr()->eq('e.createdBy', ':id'))
241
                ->setParameter('id', $this->currentUser->getId());
242
        }
243
244
        if (!empty($smsType)) {
245
            $q->andWhere(
246
                $q->expr()->eq('e.smsType', $q->expr()->literal($smsType))
247
            );
248
        }
249
250
        $q->orderBy('e.name');
251
252
        if (!empty($limit)) {
253
            $q->setFirstResult($start)
254
                ->setMaxResults($limit);
255
        }
256
257
        return $q->getQuery()->getArrayResult();
258
    }
259
}
260