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\CampaignBundle\Entity; |
||
13 | |||
14 | use Mautic\CoreBundle\Entity\CommonRepository; |
||
15 | |||
16 | class EventRepository extends CommonRepository |
||
17 | { |
||
18 | /** |
||
19 | * Get a list of entities. |
||
20 | * |
||
21 | * @return \Doctrine\ORM\Tools\Pagination\Paginator |
||
22 | */ |
||
23 | public function getEntities(array $args = []) |
||
24 | { |
||
25 | $select = 'e'; |
||
26 | $q = $this |
||
27 | ->createQueryBuilder('e') |
||
28 | ->join('e.campaign', 'c'); |
||
29 | |||
30 | if (!empty($args['campaign_id'])) { |
||
31 | $q->andWhere( |
||
32 | $q->expr()->eq('IDENTITY(e.campaign)', (int) $args['campaign_id']) |
||
33 | ); |
||
34 | } |
||
35 | |||
36 | if (empty($args['ignore_children'])) { |
||
37 | $select .= ', ec, ep'; |
||
38 | $q->leftJoin('e.children', 'ec') |
||
39 | ->leftJoin('e.parent', 'ep'); |
||
40 | } |
||
41 | |||
42 | $q->select($select); |
||
43 | |||
44 | $args['qb'] = $q; |
||
45 | |||
46 | return parent::getEntities($args); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @param $contactId |
||
51 | * |
||
52 | * @return array |
||
53 | */ |
||
54 | public function getContactPendingEvents($contactId, $type) |
||
55 | { |
||
56 | // Limit to events that hasn't been executed or scheduled yet |
||
57 | $eventQb = $this->getEntityManager()->createQueryBuilder(); |
||
58 | $eventQb->select('IDENTITY(log_event.event)') |
||
59 | ->from(LeadEventLog::class, 'log_event') |
||
60 | ->where( |
||
61 | $eventQb->expr()->andX( |
||
62 | $eventQb->expr()->eq('log_event.event', 'e'), |
||
63 | $eventQb->expr()->eq('log_event.lead', 'l.lead'), |
||
64 | $eventQb->expr()->eq('log_event.rotation', 'l.rotation') |
||
65 | ) |
||
66 | ); |
||
67 | |||
68 | // Limit to events that has no parent or whose parent has already been executed |
||
69 | $parentQb = $this->getEntityManager()->createQueryBuilder(); |
||
70 | $parentQb->select('parent_log_event.id') |
||
71 | ->from(LeadEventLog::class, 'parent_log_event') |
||
72 | ->where( |
||
73 | $parentQb->expr()->eq('parent_log_event.event', 'e.parent'), |
||
74 | $parentQb->expr()->eq('parent_log_event.lead', 'l.lead'), |
||
75 | $parentQb->expr()->eq('parent_log_event.rotation', 'l.rotation'), |
||
76 | $parentQb->expr()->eq('parent_log_event.isScheduled', 0) |
||
77 | ); |
||
78 | |||
79 | $q = $this->createQueryBuilder('e', 'e.id'); |
||
80 | $q->select('e,c') |
||
81 | ->innerJoin('e.campaign', 'c') |
||
82 | ->innerJoin('c.leads', 'l') |
||
83 | ->where( |
||
84 | $q->expr()->andX( |
||
85 | $q->expr()->eq('c.isPublished', 1), |
||
86 | $q->expr()->eq('e.type', ':type'), |
||
87 | $q->expr()->eq('IDENTITY(l.lead)', ':contactId'), |
||
88 | $q->expr()->eq('l.manuallyRemoved', 0), |
||
89 | $q->expr()->notIn('e.id', $eventQb->getDQL()), |
||
90 | $q->expr()->orX( |
||
91 | $q->expr()->isNull('e.parent'), |
||
92 | $q->expr()->exists($parentQb->getDQL()) |
||
93 | ) |
||
94 | ) |
||
95 | ) |
||
96 | ->setParameter('type', $type) |
||
97 | ->setParameter('contactId', (int) $contactId); |
||
98 | |||
99 | return $q->getQuery()->getResult(); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Get array of events by parent. |
||
104 | * |
||
105 | * @param $parentId |
||
106 | * @param null $decisionPath |
||
107 | * @param null $eventType |
||
108 | * |
||
109 | * @return array |
||
110 | */ |
||
111 | public function getEventsByParent($parentId, $decisionPath = null, $eventType = null) |
||
112 | { |
||
113 | $q = $this->getEntityManager()->createQueryBuilder(); |
||
114 | |||
115 | $q->select('e') |
||
116 | ->from('MauticCampaignBundle:Event', 'e', 'e.id') |
||
117 | ->where( |
||
118 | $q->expr()->eq('IDENTITY(e.parent)', (int) $parentId) |
||
119 | ); |
||
120 | |||
121 | if (null !== $decisionPath) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
122 | $q->andWhere( |
||
123 | $q->expr()->eq('e.decisionPath', ':decisionPath') |
||
124 | ) |
||
125 | ->setParameter('decisionPath', $decisionPath); |
||
126 | } |
||
127 | |||
128 | if (null !== $eventType) { |
||
0 ignored issues
–
show
|
|||
129 | $q->andWhere( |
||
130 | $q->expr()->eq('e.eventType', ':eventType') |
||
131 | ) |
||
132 | ->setParameter('eventType', $eventType); |
||
133 | } |
||
134 | |||
135 | return $q->getQuery()->getArrayResult(); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param $campaignId |
||
140 | * |
||
141 | * @return array |
||
142 | */ |
||
143 | public function getCampaignEvents($campaignId) |
||
144 | { |
||
145 | $q = $this->getEntityManager()->createQueryBuilder(); |
||
146 | $q->select('e, IDENTITY(e.parent)') |
||
147 | ->from('MauticCampaignBundle:Event', 'e', 'e.id') |
||
148 | ->where( |
||
149 | $q->expr()->eq('IDENTITY(e.campaign)', (int) $campaignId) |
||
150 | ) |
||
151 | ->orderBy('e.order', 'ASC'); |
||
152 | |||
153 | $results = $q->getQuery()->getArrayResult(); |
||
154 | |||
155 | // Fix the parent ID |
||
156 | $events = []; |
||
157 | foreach ($results as $id => $r) { |
||
158 | $r[0]['parent_id'] = $r[1]; |
||
159 | $events[$id] = $r[0]; |
||
160 | } |
||
161 | unset($results); |
||
162 | |||
163 | return $events; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Get array of events with stats. |
||
168 | * |
||
169 | * @param array $args |
||
170 | * |
||
171 | * @return array |
||
172 | */ |
||
173 | public function getEvents($args = []) |
||
174 | { |
||
175 | $q = $this->createQueryBuilder('e') |
||
176 | ->select('e, ec, ep') |
||
177 | ->join('e.campaign', 'c') |
||
178 | ->leftJoin('e.children', 'ec') |
||
179 | ->leftJoin('e.parent', 'ep') |
||
180 | ->orderBy('e.order'); |
||
181 | |||
182 | if (!empty($args['campaigns'])) { |
||
183 | $q->andWhere($q->expr()->in('e.campaign', ':campaigns')) |
||
184 | ->setParameter('campaigns', $args['campaigns']); |
||
185 | } |
||
186 | |||
187 | if (isset($args['positivePathOnly'])) { |
||
188 | $q->andWhere( |
||
189 | $q->expr()->orX( |
||
190 | $q->expr()->neq( |
||
191 | 'e.decisionPath', |
||
192 | $q->expr()->literal('no') |
||
193 | ), |
||
194 | $q->expr()->isNull('e.decisionPath') |
||
195 | ) |
||
196 | ); |
||
197 | } |
||
198 | |||
199 | return $q->getQuery()->getArrayResult(); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Null event parents in preparation for deleI'lting a campaign. |
||
204 | * |
||
205 | * @param $campaignId |
||
206 | */ |
||
207 | public function nullEventParents($campaignId) |
||
208 | { |
||
209 | $this->getEntityManager()->getConnection()->update( |
||
210 | MAUTIC_TABLE_PREFIX.'campaign_events', |
||
211 | ['parent_id' => null], |
||
212 | ['campaign_id' => (int) $campaignId] |
||
213 | ); |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Null event parents in preparation for deleting events from a campaign. |
||
218 | * |
||
219 | * @param $events |
||
220 | */ |
||
221 | public function nullEventRelationships($events) |
||
222 | { |
||
223 | $qb = $this->getEntityManager()->getConnection()->createQueryBuilder(); |
||
224 | $qb->update(MAUTIC_TABLE_PREFIX.'campaign_events') |
||
225 | ->set('parent_id', ':null') |
||
226 | ->setParameter('null', null) |
||
227 | ->where( |
||
228 | $qb->expr()->in('parent_id', $events) |
||
229 | ) |
||
230 | ->execute(); |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * @return string |
||
235 | */ |
||
236 | public function getTableAlias() |
||
237 | { |
||
238 | return 'e'; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * {@inheritdoc} |
||
243 | * |
||
244 | * For the API |
||
245 | */ |
||
246 | public function getSearchCommands() |
||
247 | { |
||
248 | return $this->getStandardSearchCommands(); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @param $channel |
||
253 | * @param null $campaignId |
||
254 | * @param string $eventType |
||
255 | */ |
||
256 | public function getEventsByChannel($channel, $campaignId = null, $eventType = 'action') |
||
257 | { |
||
258 | $q = $this->getEntityManager()->createQueryBuilder(); |
||
259 | |||
260 | $q->select('e') |
||
261 | ->from('MauticCampaignBundle:Event', 'e', 'e.id'); |
||
262 | |||
263 | $expr = $q->expr()->andX(); |
||
264 | if ($campaignId) { |
||
0 ignored issues
–
show
|
|||
265 | $expr->add( |
||
266 | $q->expr()->eq('IDENTITY(e.campaign)', (int) $campaignId) |
||
267 | ); |
||
268 | |||
269 | $q->orderBy('e.order'); |
||
270 | } |
||
271 | |||
272 | $expr->add( |
||
273 | $q->expr()->eq('e.channel', ':channel') |
||
274 | ); |
||
275 | $q->setParameter('channel', $channel); |
||
276 | |||
277 | if ($eventType) { |
||
278 | $expr->add( |
||
279 | $q->expr()->eq('e.eventType', ':eventType') |
||
280 | ); |
||
281 | $q->setParameter('eventType', $eventType); |
||
282 | } |
||
283 | |||
284 | $q->where($expr); |
||
285 | |||
286 | return $q->getQuery()->getResult(); |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Get an array of events that have been triggered by this lead. |
||
291 | * |
||
292 | * @param $leadId |
||
293 | * |
||
294 | * @return array |
||
295 | */ |
||
296 | public function getLeadTriggeredEvents($leadId) |
||
297 | { |
||
298 | $q = $this->getEntityManager()->createQueryBuilder() |
||
299 | ->select('e, c, l') |
||
300 | ->from('MauticCampaignBundle:Event', 'e') |
||
301 | ->join('e.campaign', 'c') |
||
302 | ->join('e.log', 'l'); |
||
303 | |||
304 | //make sure the published up and down dates are good |
||
305 | $q->where($q->expr()->eq('IDENTITY(l.lead)', (int) $leadId)); |
||
306 | |||
307 | $results = $q->getQuery()->getArrayResult(); |
||
308 | |||
309 | $return = []; |
||
310 | foreach ($results as $r) { |
||
311 | $return[$r['id']] = $r; |
||
312 | } |
||
313 | |||
314 | return $return; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * {@inheritdoc} |
||
319 | * |
||
320 | * For the API |
||
321 | */ |
||
322 | protected function addCatchAllWhereClause($q, $filter) |
||
323 | { |
||
324 | return $this->addStandardCatchAllWhereClause( |
||
325 | $q, |
||
326 | $filter, |
||
327 | [ |
||
328 | $this->getTableAlias().'.name', |
||
329 | ] |
||
330 | ); |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * {@inheritdoc} |
||
335 | * |
||
336 | * For the API |
||
337 | */ |
||
338 | protected function addSearchCommandWhereClause($q, $filter) |
||
339 | { |
||
340 | return $this->addStandardSearchCommandWhereClause($q, $filter); |
||
341 | } |
||
342 | } |
||
343 |