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\ChannelBundle\Entity; |
13
|
|
|
|
14
|
|
|
use Mautic\CoreBundle\Entity\CommonRepository; |
15
|
|
|
use Mautic\LeadBundle\Entity\TimelineTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* MessageQueueRepository. |
19
|
|
|
*/ |
20
|
|
|
class MessageQueueRepository extends CommonRepository |
21
|
|
|
{ |
22
|
|
|
use TimelineTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param $channel |
26
|
|
|
* @param $channelId |
27
|
|
|
* @param $leadId |
28
|
|
|
*/ |
29
|
|
|
public function findMessage($channel, $channelId, $leadId) |
30
|
|
|
{ |
31
|
|
|
$results = $this->createQueryBuilder('mq') |
32
|
|
|
->where('IDENTITY(mq.lead) = :leadId') |
33
|
|
|
->andWhere('mq.channel = :channel') |
34
|
|
|
->andWhere('mq.channelId = :channelId') |
35
|
|
|
->setParameter('leadId', $leadId) |
36
|
|
|
->setParameter('channel', $channel) |
37
|
|
|
->setParameter('channelId', $channelId) |
38
|
|
|
->getQuery() |
39
|
|
|
->getResult(); |
40
|
|
|
|
41
|
|
|
return ($results) ? $results[0] : null; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $limit |
46
|
|
|
* @param $processStarted |
47
|
|
|
* @param null $channel |
48
|
|
|
* @param null $channelId |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function getQueuedMessages($limit, $processStarted, $channel = null, $channelId = null) |
53
|
|
|
{ |
54
|
|
|
$q = $this->createQueryBuilder('mq'); |
55
|
|
|
|
56
|
|
|
$q->where($q->expr()->eq('mq.success', ':success')) |
57
|
|
|
->andWhere($q->expr()->lt('mq.attempts', 'mq.maxAttempts')) |
58
|
|
|
->andWhere('mq.lastAttempt is null or mq.lastAttempt < :processStarted') |
59
|
|
|
->andWhere('mq.scheduledDate <= :processStarted') |
60
|
|
|
->setParameter('success', false, 'boolean') |
61
|
|
|
->setParameter('processStarted', $processStarted) |
62
|
|
|
->indexBy('mq', 'mq.id'); |
63
|
|
|
|
64
|
|
|
$q->orderBy('mq.priority, mq.scheduledDate', 'ASC'); |
65
|
|
|
|
66
|
|
|
if ($limit) { |
67
|
|
|
$q->setMaxResults((int) $limit); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($channel) { |
|
|
|
|
71
|
|
|
$q->andWhere($q->expr()->eq('mq.channel', ':channel')) |
72
|
|
|
->setParameter('channel', $channel); |
73
|
|
|
|
74
|
|
|
if ($channelId) { |
75
|
|
|
$q->andWhere($q->expr()->eq('mq.channelId', (int) $channelId)); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $q->getQuery()->getResult(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $channel |
84
|
|
|
* |
85
|
|
|
* @return bool|string |
86
|
|
|
*/ |
87
|
|
|
public function getQueuedChannelCount($channel, array $ids = null) |
88
|
|
|
{ |
89
|
|
|
$q = $this->getEntityManager()->getConnection()->createQueryBuilder(); |
90
|
|
|
|
91
|
|
|
$expr = $q->expr()->andX( |
92
|
|
|
$q->expr()->eq($this->getTableAlias().'.channel', ':channel'), |
93
|
|
|
$q->expr()->neq($this->getTableAlias().'.status', ':status') |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
if (!empty($ids)) { |
97
|
|
|
$expr->add( |
98
|
|
|
$q->expr()->in($this->getTableAlias().'.channel_id', $ids) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return (int) $q->select('count(*)') |
103
|
|
|
->from(MAUTIC_TABLE_PREFIX.'message_queue', $this->getTableAlias()) |
104
|
|
|
->where($expr) |
105
|
|
|
->setParameters( |
106
|
|
|
[ |
107
|
|
|
'channel' => $channel, |
108
|
|
|
'status' => MessageQueue::STATUS_SENT, |
109
|
|
|
] |
110
|
|
|
) |
111
|
|
|
->execute() |
112
|
|
|
->fetchColumn(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get a lead's point log. |
117
|
|
|
* |
118
|
|
|
* @param int|null $leadId |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function getLeadTimelineEvents($leadId = null, array $options = []) |
123
|
|
|
{ |
124
|
|
|
$query = $this->getEntityManager()->getConnection()->createQueryBuilder() |
125
|
|
|
->from(MAUTIC_TABLE_PREFIX.'message_queue', 'mq') |
126
|
|
|
->select('mq.id, mq.lead_id, mq.channel as channelName, mq.channel_id as channelId, |
127
|
|
|
mq.priority as priority, mq.attempts, mq.success, mq.status, mq.date_published as dateAdded, |
128
|
|
|
mq.scheduled_date as scheduledDate, mq.last_attempt as lastAttempt, mq.date_sent as dateSent'); |
129
|
|
|
|
130
|
|
|
if ($leadId) { |
|
|
|
|
131
|
|
|
$query->where('mq.lead_id = '.(int) $leadId); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (isset($options['search']) && $options['search']) { |
135
|
|
|
$query->andWhere($query->expr()->orX( |
136
|
|
|
$query->expr()->like('mq.channel', $query->expr()->literal('%'.$options['search'].'%')) |
137
|
|
|
)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this->getTimelineResults($query, $options, 'mq.channel', 'mq.date_published', [], ['dateAdded']); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|