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 Mautic\CoreBundle\Entity\CommonRepository; |
||
15 | use Mautic\CoreBundle\Helper\DateTimeHelper; |
||
16 | use Mautic\LeadBundle\Entity\TimelineTrait; |
||
17 | |||
18 | /** |
||
19 | * Class StatRepository. |
||
20 | */ |
||
21 | class StatRepository extends CommonRepository |
||
22 | { |
||
23 | use TimelineTrait; |
||
24 | |||
25 | /** |
||
26 | * @param $trackingHash |
||
27 | * |
||
28 | * @return mixed |
||
29 | * |
||
30 | * @throws \Doctrine\ORM\NoResultException |
||
31 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
32 | */ |
||
33 | public function getSmsStatus($trackingHash) |
||
34 | { |
||
35 | $q = $this->createQueryBuilder('s'); |
||
36 | $q->select('s') |
||
37 | ->leftJoin('s.lead', 'l') |
||
38 | ->leftJoin('s.sms', 'e') |
||
39 | ->where( |
||
40 | $q->expr()->eq('s.trackingHash', ':hash') |
||
41 | ) |
||
42 | ->setParameter('hash', $trackingHash); |
||
43 | |||
44 | $result = $q->getQuery()->getResult(); |
||
45 | |||
46 | return (!empty($result)) ? $result[0] : null; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @param $smsId |
||
51 | * @param null $listId |
||
52 | * |
||
53 | * @return array |
||
54 | */ |
||
55 | public function getSentStats($smsId, $listId = null) |
||
56 | { |
||
57 | $q = $this->_em->getConnection()->createQueryBuilder(); |
||
58 | $q->select('s.lead_id') |
||
59 | ->from(MAUTIC_TABLE_PREFIX.'sms_messages_stats', 's') |
||
60 | ->where('s.sms_id = :sms') |
||
61 | ->setParameter('sms', $smsId); |
||
62 | |||
63 | if ($listId) { |
||
64 | $q->andWhere('s.list_id = :list') |
||
65 | ->setParameter('list', $listId); |
||
66 | } |
||
67 | |||
68 | $result = $q->execute()->fetchAll(); |
||
69 | |||
70 | //index by lead |
||
71 | $stats = []; |
||
72 | foreach ($result as $r) { |
||
73 | $stats[$r['lead_id']] = $r['lead_id']; |
||
74 | } |
||
75 | |||
76 | unset($result); |
||
77 | |||
78 | return $stats; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param int|array $smsIds |
||
83 | * @param int $listId |
||
84 | * |
||
85 | * @return int |
||
86 | */ |
||
87 | public function getSentCount($smsIds = null, $listId = null) |
||
88 | { |
||
89 | $q = $this->_em->getConnection()->createQueryBuilder(); |
||
90 | |||
91 | $q->select('count(s.id) as sent_count') |
||
92 | ->from(MAUTIC_TABLE_PREFIX.'sms_message_stats', 's'); |
||
93 | |||
94 | if ($smsIds) { |
||
95 | if (!is_array($smsIds)) { |
||
96 | $smsIds = [(int) $smsIds]; |
||
97 | } |
||
98 | $q->where( |
||
99 | $q->expr()->in('s.sms_id', $smsIds) |
||
100 | ); |
||
101 | } |
||
102 | |||
103 | if ($listId) { |
||
0 ignored issues
–
show
|
|||
104 | $q->andWhere('s.list_id = '.(int) $listId); |
||
105 | } |
||
106 | |||
107 | $q->andWhere('s.is_failed = :false') |
||
108 | ->setParameter('false', false, 'boolean'); |
||
109 | |||
110 | $results = $q->execute()->fetchAll(); |
||
111 | |||
112 | return (isset($results[0])) ? $results[0]['sent_count'] : 0; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Get a lead's email stat. |
||
117 | * |
||
118 | * @param int $leadId |
||
119 | * |
||
120 | * @return array |
||
121 | * |
||
122 | * @throws \Doctrine\ORM\NoResultException |
||
123 | * @throws \Doctrine\ORM\NonUniqueResultException |
||
124 | */ |
||
125 | public function getLeadStats($leadId, array $options = []) |
||
126 | { |
||
127 | $query = $this->getEntityManager()->getConnection()->createQueryBuilder(); |
||
128 | $query->from(MAUTIC_TABLE_PREFIX.'sms_message_stats', 's') |
||
129 | ->leftJoin('s', MAUTIC_TABLE_PREFIX.'sms_messages', 'e', 's.sms_id = e.id'); |
||
130 | |||
131 | if ($leadId) { |
||
132 | $query->andWhere( |
||
133 | $query->expr()->eq('s.lead_id', (int) $leadId) |
||
134 | ); |
||
135 | } |
||
136 | |||
137 | if (!empty($options['basic_select'])) { |
||
138 | $query->select( |
||
139 | 's.sms_id, s.id, s.date_sent as dateSent, e.name, e.name as sms_name, s.is_failed as isFailed' |
||
140 | ); |
||
141 | } else { |
||
142 | $query->select( |
||
143 | 's.sms_id, s.id, s.date_sent as dateSent, e.name, e.name as sms_name, s.is_failed as isFailed, s.list_id, l.name as list_name, s.tracking_hash as idHash, s.lead_id, s.details' |
||
144 | ) |
||
145 | ->leftJoin('s', MAUTIC_TABLE_PREFIX.'lead_lists', 'l', 's.list_id = l.id'); |
||
146 | } |
||
147 | |||
148 | if (isset($options['state'])) { |
||
149 | $state = $options['state']; |
||
150 | if ('failed' == $state) { |
||
151 | $query->andWhere( |
||
152 | $query->expr()->eq('s.is_failed', 1) |
||
153 | ); |
||
154 | } |
||
155 | } |
||
156 | $state = 'sent'; |
||
157 | |||
158 | if (isset($options['search']) && $options['search']) { |
||
159 | $query->andWhere( |
||
160 | $query->expr()->orX( |
||
161 | $query->expr()->like('e.name', $query->expr()->literal('%'.$options['search'].'%')) |
||
162 | ) |
||
163 | ); |
||
164 | } |
||
165 | |||
166 | if (isset($options['fromDate']) && $options['fromDate']) { |
||
167 | $dt = new DateTimeHelper($options['fromDate']); |
||
168 | $query->andWhere( |
||
169 | $query->expr()->gte('s.date_sent', $query->expr()->literal($dt->toUtcString())) |
||
170 | ); |
||
171 | } |
||
172 | |||
173 | return $this->getTimelineResults( |
||
174 | $query, |
||
175 | $options, |
||
176 | 'e.name', |
||
177 | 's.date_'.$state |
||
178 | ); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Updates lead ID (e.g. after a lead merge). |
||
183 | * |
||
184 | * @param $fromLeadId |
||
185 | * @param $toLeadId |
||
186 | */ |
||
187 | public function updateLead($fromLeadId, $toLeadId) |
||
188 | { |
||
189 | $q = $this->_em->getConnection()->createQueryBuilder(); |
||
190 | $q->update(MAUTIC_TABLE_PREFIX.'sms_message_stats') |
||
191 | ->set('sms_id', (int) $toLeadId) |
||
192 | ->where('sms_id = '.(int) $fromLeadId) |
||
193 | ->execute(); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Delete a stat. |
||
198 | * |
||
199 | * @param $id |
||
200 | */ |
||
201 | public function deleteStat($id) |
||
202 | { |
||
203 | $this->_em->getConnection()->delete(MAUTIC_TABLE_PREFIX.'sms_message_stats', ['id' => (int) $id]); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @return string |
||
208 | */ |
||
209 | public function getTableAlias() |
||
210 | { |
||
211 | return 's'; |
||
212 | } |
||
213 | } |
||
214 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: