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\AssetBundle\Entity; |
13
|
|
|
|
14
|
|
|
use Mautic\CoreBundle\Entity\CommonRepository; |
15
|
|
|
use Mautic\CoreBundle\Helper\Chart\PieChart; |
16
|
|
|
use Mautic\CoreBundle\Helper\DateTimeHelper; |
17
|
|
|
use Mautic\LeadBundle\Entity\TimelineTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class DownloadRepository. |
21
|
|
|
*/ |
22
|
|
|
class DownloadRepository extends CommonRepository |
23
|
|
|
{ |
24
|
|
|
use TimelineTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Determine if the download is a unique download. |
28
|
|
|
* |
29
|
|
|
* @param $assetId |
30
|
|
|
* @param $trackingId |
31
|
|
|
* |
32
|
|
|
* @return bool |
33
|
|
|
*/ |
34
|
|
|
public function isUniqueDownload($assetId, $trackingId) |
35
|
|
|
{ |
36
|
|
|
$q = $this->getEntityManager()->getConnection()->createQueryBuilder(); |
37
|
|
|
$q2 = $this->getEntityManager()->getConnection()->createQueryBuilder(); |
38
|
|
|
|
39
|
|
|
$q2->select('null') |
40
|
|
|
->from(MAUTIC_TABLE_PREFIX.'asset_downloads', 'd'); |
41
|
|
|
|
42
|
|
|
$q2->where( |
43
|
|
|
$q2->expr()->andX( |
44
|
|
|
$q2->expr()->eq('d.tracking_id', ':id'), |
45
|
|
|
$q2->expr()->eq('d.asset_id', (int) $assetId) |
46
|
|
|
) |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$q->select('u.is_unique') |
50
|
|
|
->from(sprintf('(SELECT (NOT EXISTS (%s)) is_unique)', $q2->getSQL()), 'u' |
51
|
|
|
) |
52
|
|
|
->setParameter('id', $trackingId); |
53
|
|
|
|
54
|
|
|
return (bool) $q->execute()->fetchColumn(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get a lead's page downloads. |
59
|
|
|
* |
60
|
|
|
* @param int|null $leadId |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
public function getLeadDownloads($leadId = null, array $options = []) |
65
|
|
|
{ |
66
|
|
|
$query = $this->getEntityManager()->getConnection()->createQueryBuilder() |
67
|
|
|
->select('a.id as asset_id, d.date_download as dateDownload, a.title, d.id as download_id, d.lead_id') |
68
|
|
|
->from(MAUTIC_TABLE_PREFIX.'asset_downloads', 'd') |
69
|
|
|
->leftJoin('d', MAUTIC_TABLE_PREFIX.'assets', 'a', 'd.asset_id = a.id'); |
70
|
|
|
|
71
|
|
|
if ($leadId) { |
|
|
|
|
72
|
|
|
$query->where('d.lead_id = '.(int) $leadId); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (isset($options['search']) && $options['search']) { |
76
|
|
|
$query->andWhere($query->expr()->like('a.title', $query->expr()->literal('%'.$options['search'].'%'))); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->getTimelineResults($query, $options, 'a.title', 'd.date_download', [], ['date_download']); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get list of assets ordered by it's download count. |
84
|
|
|
* |
85
|
|
|
* @param QueryBuilder $query |
|
|
|
|
86
|
|
|
* @param int $limit |
87
|
|
|
* @param int $offset |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
* |
91
|
|
|
* @throws \Doctrine\ORM\NoResultException |
92
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
93
|
|
|
*/ |
94
|
|
|
public function getMostDownloaded($query, $limit = 10, $offset = 0) |
95
|
|
|
{ |
96
|
|
|
$query->select('a.title, a.id, count(ad.id) as downloads') |
97
|
|
|
->groupBy('a.id, a.title') |
98
|
|
|
->orderBy('downloads', 'DESC') |
99
|
|
|
->setMaxResults($limit) |
100
|
|
|
->setFirstResult($offset); |
101
|
|
|
|
102
|
|
|
return $query->execute()->fetchAll(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get list of asset referrals ordered by it's count. |
107
|
|
|
* |
108
|
|
|
* @param QueryBuilder $query |
109
|
|
|
* @param int $limit |
110
|
|
|
* @param int $offset |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
* |
114
|
|
|
* @throws \Doctrine\ORM\NoResultException |
115
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
116
|
|
|
*/ |
117
|
|
|
public function getTopReferrers($query, $limit = 10, $offset = 0) |
118
|
|
|
{ |
119
|
|
|
$query->select('ad.referer, count(ad.referer) as downloads') |
120
|
|
|
->groupBy('ad.referer') |
121
|
|
|
->orderBy('downloads', 'DESC') |
122
|
|
|
->setMaxResults($limit) |
123
|
|
|
->setFirstResult($offset); |
124
|
|
|
|
125
|
|
|
return $query->execute()->fetchAll(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get pie graph data for http statuses. |
130
|
|
|
* |
131
|
|
|
* @param QueryBuilder $query |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
* |
135
|
|
|
* @throws \Doctrine\ORM\NoResultException |
136
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
137
|
|
|
*/ |
138
|
|
|
public function getHttpStatuses($query) |
139
|
|
|
{ |
140
|
|
|
$query->select('ad.code as status, count(ad.code) as count') |
141
|
|
|
->groupBy('ad.code') |
142
|
|
|
->orderBy('count', 'DESC'); |
143
|
|
|
|
144
|
|
|
$results = $query->execute()->fetchAll(); |
145
|
|
|
$chart = new PieChart(); |
146
|
|
|
|
147
|
|
|
foreach ($results as $result) { |
148
|
|
|
$chart->setDataset($result['status'], $result['count']); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $chart->render(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param $pageId |
156
|
|
|
* @param \DateTime $fromDate |
157
|
|
|
* |
158
|
|
|
* @return mixed |
159
|
|
|
*/ |
160
|
|
|
public function getDownloadCountsByPage($pageId, \DateTime $fromDate = null) |
161
|
|
|
{ |
162
|
|
|
$q = $this->_em->getConnection()->createQueryBuilder(); |
163
|
|
|
$q->select('count(distinct(a.tracking_id)) as count, a.source_id as id, p.title as name, p.hits as total') |
164
|
|
|
->from(MAUTIC_TABLE_PREFIX.'asset_downloads', 'a') |
165
|
|
|
->join('a', MAUTIC_TABLE_PREFIX.'pages', 'p', 'a.source_id = p.id'); |
166
|
|
|
|
167
|
|
|
if (is_array($pageId)) { |
168
|
|
|
$q->where($q->expr()->in('p.id', $pageId)) |
169
|
|
|
->groupBy('p.id, a.source_id, p.title, p.hits'); |
170
|
|
|
} else { |
171
|
|
|
$q->where($q->expr()->eq('p.id', ':page')) |
172
|
|
|
->setParameter('page', (int) $pageId); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$q->andWhere('a.source = "page"') |
176
|
|
|
->andWhere('a.code = 200'); |
177
|
|
|
|
178
|
|
|
if (null != $fromDate) { |
179
|
|
|
$dh = new DateTimeHelper($fromDate); |
180
|
|
|
$q->andWhere($q->expr()->gte('a.date_download', ':date')) |
181
|
|
|
->setParameter('date', $dh->toUtcString()); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$results = $q->execute()->fetchAll(); |
185
|
|
|
|
186
|
|
|
$downloads = []; |
187
|
|
|
foreach ($results as $r) { |
188
|
|
|
$downloads[$r['id']] = $r; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $downloads; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get download count by email by linking emails that have been associated with a page hit that has the |
196
|
|
|
* same tracking ID as an asset download tracking ID and thus assumed happened in the same session. |
197
|
|
|
* |
198
|
|
|
* @param $emailId |
199
|
|
|
* @param \DateTime $fromDate |
200
|
|
|
* |
201
|
|
|
* @return mixed |
202
|
|
|
*/ |
203
|
|
|
public function getDownloadCountsByEmail($emailId, \DateTime $fromDate = null) |
204
|
|
|
{ |
205
|
|
|
//link email to page hit tracking id to download tracking id |
206
|
|
|
$q = $this->_em->getConnection()->createQueryBuilder(); |
207
|
|
|
$q->select('count(distinct(a.tracking_id)) as count, e.id, e.subject as name, e.variant_sent_count as total') |
208
|
|
|
->from(MAUTIC_TABLE_PREFIX.'asset_downloads', 'a') |
209
|
|
|
->join('a', MAUTIC_TABLE_PREFIX.'emails', 'e', 'a.email_id = e.id'); |
210
|
|
|
|
211
|
|
|
if (is_array($emailId)) { |
212
|
|
|
$q->where($q->expr()->in('e.id', $emailId)) |
213
|
|
|
->groupBy('e.id, e.subject, e.variant_sent_count'); |
214
|
|
|
} else { |
215
|
|
|
$q->where($q->expr()->eq('e.id', ':email')) |
216
|
|
|
->setParameter('email', (int) $emailId); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$q->andWhere('a.code = 200'); |
220
|
|
|
|
221
|
|
|
if (null != $fromDate) { |
222
|
|
|
$dh = new DateTimeHelper($fromDate); |
223
|
|
|
$q->andWhere($q->expr()->gte('a.date_download', ':date')) |
224
|
|
|
->setParameter('date', $dh->toUtcString()); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$results = $q->execute()->fetchAll(); |
228
|
|
|
|
229
|
|
|
$downloads = []; |
230
|
|
|
foreach ($results as $r) { |
231
|
|
|
$downloads[$r['id']] = $r; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return $downloads; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param $leadId |
239
|
|
|
* @param $newTrackingId |
240
|
|
|
* @param $oldTrackingId |
241
|
|
|
*/ |
242
|
|
|
public function updateLeadByTrackingId($leadId, $newTrackingId, $oldTrackingId) |
243
|
|
|
{ |
244
|
|
|
$q = $this->_em->getConnection()->createQueryBuilder(); |
245
|
|
|
$q->update(MAUTIC_TABLE_PREFIX.'asset_downloads') |
246
|
|
|
->set('lead_id', (int) $leadId) |
247
|
|
|
->set('tracking_id', ':newTrackingId') |
248
|
|
|
->where( |
249
|
|
|
$q->expr()->eq('tracking_id', ':oldTrackingId') |
250
|
|
|
) |
251
|
|
|
->setParameters([ |
252
|
|
|
'newTrackingId' => $newTrackingId, |
253
|
|
|
'oldTrackingId' => $oldTrackingId, |
254
|
|
|
]) |
255
|
|
|
->execute(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Updates lead ID (e.g. after a lead merge). |
260
|
|
|
* |
261
|
|
|
* @param $fromLeadId |
262
|
|
|
* @param $toLeadId |
263
|
|
|
*/ |
264
|
|
|
public function updateLead($fromLeadId, $toLeadId) |
265
|
|
|
{ |
266
|
|
|
$q = $this->_em->getConnection()->createQueryBuilder(); |
267
|
|
|
$q->update(MAUTIC_TABLE_PREFIX.'asset_downloads') |
268
|
|
|
->set('lead_id', (int) $toLeadId) |
269
|
|
|
->where('lead_id = '.(int) $fromLeadId) |
270
|
|
|
->execute(); |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
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: