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