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\Model; |
||
13 | |||
14 | use Doctrine\ORM\PersistentCollection; |
||
15 | use Mautic\AssetBundle\AssetEvents; |
||
16 | use Mautic\AssetBundle\Entity\Asset; |
||
17 | use Mautic\AssetBundle\Entity\Download; |
||
18 | use Mautic\AssetBundle\Event\AssetEvent; |
||
19 | use Mautic\AssetBundle\Event\AssetLoadEvent; |
||
20 | use Mautic\CategoryBundle\Model\CategoryModel; |
||
21 | use Mautic\CoreBundle\Helper\Chart\ChartQuery; |
||
22 | use Mautic\CoreBundle\Helper\Chart\LineChart; |
||
23 | use Mautic\CoreBundle\Helper\Chart\PieChart; |
||
24 | use Mautic\CoreBundle\Helper\CoreParametersHelper; |
||
25 | use Mautic\CoreBundle\Helper\IpLookupHelper; |
||
26 | use Mautic\CoreBundle\Model\FormModel; |
||
27 | use Mautic\EmailBundle\Entity\Email; |
||
28 | use Mautic\LeadBundle\Entity\Lead; |
||
29 | use Mautic\LeadBundle\Model\LeadModel; |
||
30 | use Mautic\LeadBundle\Tracker\Factory\DeviceDetectorFactory\DeviceDetectorFactoryInterface; |
||
31 | use Mautic\LeadBundle\Tracker\Service\DeviceCreatorService\DeviceCreatorServiceInterface; |
||
32 | use Mautic\LeadBundle\Tracker\Service\DeviceTrackingService\DeviceTrackingServiceInterface; |
||
33 | use Symfony\Component\EventDispatcher\Event; |
||
34 | use Symfony\Component\HttpFoundation\RequestStack; |
||
35 | use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; |
||
36 | |||
37 | /** |
||
38 | * Class AssetModel. |
||
39 | */ |
||
40 | class AssetModel extends FormModel |
||
41 | { |
||
42 | /** |
||
43 | * @var CategoryModel |
||
44 | */ |
||
45 | protected $categoryModel; |
||
46 | |||
47 | /** |
||
48 | * @var LeadModel |
||
49 | */ |
||
50 | protected $leadModel; |
||
51 | |||
52 | /** |
||
53 | * @var null|\Symfony\Component\HttpFoundation\Request |
||
54 | */ |
||
55 | protected $request; |
||
56 | |||
57 | /** |
||
58 | * @var IpLookupHelper |
||
59 | */ |
||
60 | protected $ipLookupHelper; |
||
61 | |||
62 | /** |
||
63 | * @var int |
||
64 | */ |
||
65 | protected $maxAssetSize; |
||
66 | |||
67 | /** |
||
68 | * @var DeviceCreatorServiceInterface |
||
69 | */ |
||
70 | private $deviceCreatorService; |
||
71 | |||
72 | /** |
||
73 | * @var DeviceDetectorFactoryInterface |
||
74 | */ |
||
75 | private $deviceDetectorFactory; |
||
76 | |||
77 | /** |
||
78 | * @var DeviceTrackingServiceInterface |
||
79 | */ |
||
80 | private $deviceTrackingService; |
||
81 | |||
82 | /** |
||
83 | * AssetModel constructor. |
||
84 | * |
||
85 | * @param LeadModel $leadModel |
||
86 | * @param CategoryModel $categoryModel |
||
87 | * @param RequestStack $requestStack |
||
88 | * @param IpLookupHelper $ipLookupHelper |
||
89 | * @param CoreParametersHelper $coreParametersHelper |
||
90 | * @param DeviceCreatorServiceInterface $deviceCreatorService |
||
91 | * @param DeviceDetectorFactoryInterface $deviceDetectorFactory |
||
92 | * @param DeviceTrackingServiceInterface $deviceTrackingService |
||
93 | */ |
||
94 | public function __construct( |
||
95 | LeadModel $leadModel, |
||
96 | CategoryModel $categoryModel, |
||
97 | RequestStack $requestStack, |
||
98 | IpLookupHelper $ipLookupHelper, |
||
99 | CoreParametersHelper $coreParametersHelper, |
||
100 | DeviceCreatorServiceInterface $deviceCreatorService, |
||
101 | DeviceDetectorFactoryInterface $deviceDetectorFactory, |
||
102 | DeviceTrackingServiceInterface $deviceTrackingService |
||
103 | ) { |
||
104 | $this->leadModel = $leadModel; |
||
105 | $this->categoryModel = $categoryModel; |
||
106 | $this->request = $requestStack->getCurrentRequest(); |
||
107 | $this->ipLookupHelper = $ipLookupHelper; |
||
108 | $this->deviceCreatorService = $deviceCreatorService; |
||
109 | $this->deviceDetectorFactory = $deviceDetectorFactory; |
||
110 | $this->deviceTrackingService = $deviceTrackingService; |
||
111 | $this->maxAssetSize = $coreParametersHelper->getParameter('mautic.max_size'); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function saveEntity($entity, $unlock = true) |
||
118 | { |
||
119 | if (empty($this->inConversion)) { |
||
120 | $alias = $entity->getAlias(); |
||
121 | if (empty($alias)) { |
||
122 | $alias = $entity->getTitle(); |
||
123 | } |
||
124 | $alias = $this->cleanAlias($alias, '', false, '-'); |
||
125 | |||
126 | //make sure alias is not already taken |
||
127 | $repo = $this->getRepository(); |
||
128 | $testAlias = $alias; |
||
129 | $count = $repo->checkUniqueAlias($testAlias, $entity); |
||
130 | $aliasTag = $count; |
||
131 | |||
132 | while ($count) { |
||
133 | $testAlias = $alias.$aliasTag; |
||
134 | $count = $repo->checkUniqueAlias($testAlias, $entity); |
||
135 | ++$aliasTag; |
||
136 | } |
||
137 | if ($testAlias != $alias) { |
||
138 | $alias = $testAlias; |
||
139 | } |
||
140 | $entity->setAlias($alias); |
||
141 | } |
||
142 | |||
143 | if (!$entity->isNew()) { |
||
144 | //increase the revision |
||
145 | $revision = $entity->getRevision(); |
||
146 | ++$revision; |
||
147 | $entity->setRevision($revision); |
||
148 | } |
||
149 | |||
150 | parent::saveEntity($entity, $unlock); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param $asset |
||
155 | * @param null $request |
||
156 | * @param string $code |
||
157 | * @param array $systemEntry |
||
158 | * |
||
159 | * @throws \Doctrine\ORM\ORMException |
||
160 | * @throws \Exception |
||
161 | */ |
||
162 | public function trackDownload($asset, $request = null, $code = '200', $systemEntry = []) |
||
163 | { |
||
164 | // Don't skew results with in-house downloads |
||
165 | if (empty($systemEntry) && !$this->security->isAnonymous()) { |
||
166 | return; |
||
167 | } |
||
168 | |||
169 | if ($request == null) { |
||
170 | $request = $this->request; |
||
171 | } |
||
172 | |||
173 | $download = new Download(); |
||
174 | $download->setDateDownload(new \Datetime()); |
||
175 | |||
176 | // Download triggered by lead |
||
177 | if (empty($systemEntry)) { |
||
178 | //check for any clickthrough info |
||
179 | $clickthrough = $request->get('ct', false); |
||
180 | if (!empty($clickthrough)) { |
||
181 | $clickthrough = $this->decodeArrayFromUrl($clickthrough); |
||
182 | |||
183 | if (!empty($clickthrough['lead'])) { |
||
184 | $lead = $this->leadModel->getEntity($clickthrough['lead']); |
||
185 | if ($lead !== null) { |
||
186 | $wasTrackedAlready = $this->deviceTrackingService->isTracked(); |
||
187 | $deviceDetector = $this->deviceDetectorFactory->create($request->server->get('HTTP_USER_AGENT')); |
||
188 | $deviceDetector->parse(); |
||
189 | $currentDevice = $this->deviceCreatorService->getCurrentFromDetector($deviceDetector, $lead); |
||
190 | $trackedDevice = $this->deviceTrackingService->trackCurrentDevice($currentDevice, false); |
||
191 | $trackingId = $trackedDevice->getTrackingId(); |
||
192 | $trackingNewlyGenerated = !$wasTrackedAlready; |
||
193 | $leadClickthrough = true; |
||
194 | |||
195 | $this->leadModel->setCurrentLead($lead); |
||
196 | } |
||
197 | } |
||
198 | if (!empty($clickthrough['channel'])) { |
||
199 | if (count($clickthrough['channel']) === 1) { |
||
200 | $channelId = reset($clickthrough['channel']); |
||
201 | $channel = key($clickthrough['channel']); |
||
202 | } else { |
||
203 | $channel = $clickthrough['channel'][0]; |
||
204 | $channelId = (int) $clickthrough['channel'][1]; |
||
205 | } |
||
206 | $download->setSource($channel); |
||
207 | $download->setSourceId($channelId); |
||
208 | } elseif (!empty($clickthrough['source'])) { |
||
209 | $download->setSource($clickthrough['source'][0]); |
||
210 | $download->setSourceId($clickthrough['source'][1]); |
||
211 | } |
||
212 | |||
213 | if (!empty($clickthrough['email'])) { |
||
214 | $emailRepo = $this->em->getRepository('MauticEmailBundle:Email'); |
||
215 | if ($emailEntity = $emailRepo->getEntity($clickthrough['email'])) { |
||
216 | $download->setEmail($emailEntity); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
217 | } |
||
218 | } |
||
219 | } |
||
220 | |||
221 | if (empty($leadClickthrough)) { |
||
222 | $wasTrackedAlready = $this->deviceTrackingService->isTracked(); |
||
223 | $lead = $this->leadModel->getCurrentLead(); |
||
224 | $trackedDevice = $this->deviceTrackingService->getTrackedDevice(); |
||
225 | $trackingId = null; |
||
226 | $trackingNewlyGenerated = false; |
||
227 | if ($trackedDevice !== null) { |
||
228 | $trackingId = $trackedDevice->getTrackingId(); |
||
229 | $trackingNewlyGenerated = !$wasTrackedAlready; |
||
230 | } |
||
231 | } |
||
232 | |||
233 | $download->setLead($lead); |
||
234 | } else { |
||
235 | $trackingId = ''; |
||
236 | |||
237 | if (isset($systemEntry['lead'])) { |
||
238 | $lead = $systemEntry['lead']; |
||
239 | if (!$lead instanceof Lead) { |
||
240 | $leadId = is_array($lead) ? $lead['id'] : $lead; |
||
241 | $lead = $this->em->getReference('MauticLeadBundle:Lead', $leadId); |
||
242 | } |
||
243 | |||
244 | $download->setLead($lead); |
||
245 | } |
||
246 | |||
247 | if (!empty($systemEntry['source'])) { |
||
248 | $download->setSource($systemEntry['source'][0]); |
||
249 | $download->setSourceId($systemEntry['source'][1]); |
||
250 | } |
||
251 | |||
252 | if (isset($systemEntry['email'])) { |
||
253 | $email = $systemEntry['email']; |
||
254 | if (!$email instanceof Email) { |
||
255 | $emailId = is_array($email) ? $email['id'] : $email; |
||
256 | $email = $this->em->getReference('MauticEmailBundle:Email', $emailId); |
||
257 | } |
||
258 | |||
259 | $download->setEmail($email); |
||
260 | } |
||
261 | |||
262 | if (isset($systemEntry['tracking_id'])) { |
||
263 | $trackingId = $systemEntry['tracking_id']; |
||
264 | $trackingNewlyGenerated = false; |
||
265 | } elseif ($this->security->isAnonymous() && !defined('IN_MAUTIC_CONSOLE')) { |
||
266 | // If the session is anonymous and not triggered via CLI, assume the lead did something to trigger the |
||
267 | // system forced download such as an email |
||
268 | $deviceWasTracked = $this->deviceTrackingService->isTracked(); |
||
269 | $deviceDetector = $this->deviceDetectorFactory->create($request->server->get('HTTP_USER_AGENT')); |
||
270 | $deviceDetector->parse(); |
||
271 | $currentDevice = $this->deviceCreatorService->getCurrentFromDetector($deviceDetector, $lead); |
||
272 | $trackedDevice = $this->deviceTrackingService->trackCurrentDevice($currentDevice, false); |
||
273 | $trackingId = $trackedDevice->getTrackingId(); |
||
274 | $trackingNewlyGenerated = !$deviceWasTracked; |
||
275 | } |
||
276 | } |
||
277 | |||
278 | $isUnique = true; |
||
279 | if (!empty($trackingNewlyGenerated)) { |
||
280 | // Cookie was just generated so this is definitely a unique download |
||
281 | $isUnique = $trackingNewlyGenerated; |
||
282 | } elseif (!empty($trackingId)) { |
||
283 | // Determine if this is a unique download |
||
284 | $isUnique = $this->getDownloadRepository()->isUniqueDownload($asset->getId(), $trackingId); |
||
285 | } |
||
286 | |||
287 | $download->setTrackingId($trackingId); |
||
288 | |||
289 | if (!empty($asset) && empty($systemEntry)) { |
||
290 | $download->setAsset($asset); |
||
291 | |||
292 | $this->getRepository()->upDownloadCount($asset->getId(), 1, $isUnique); |
||
293 | } |
||
294 | |||
295 | //check for existing IP |
||
296 | $ipAddress = $this->ipLookupHelper->getIpAddress(); |
||
297 | |||
298 | $download->setCode($code); |
||
299 | $download->setIpAddress($ipAddress); |
||
300 | |||
301 | if ($request !== null) { |
||
302 | $download->setReferer($request->server->get('HTTP_REFERER')); |
||
303 | } |
||
304 | |||
305 | // Dispatch event |
||
306 | if ($this->dispatcher->hasListeners(AssetEvents::ASSET_ON_LOAD)) { |
||
307 | $event = new AssetLoadEvent($download, $isUnique); |
||
308 | $this->dispatcher->dispatch(AssetEvents::ASSET_ON_LOAD, $event); |
||
309 | } |
||
310 | |||
311 | // Wrap in a try/catch to prevent deadlock errors on busy servers |
||
312 | try { |
||
313 | $this->em->persist($download); |
||
314 | $this->em->flush(); |
||
315 | } catch (\Exception $e) { |
||
316 | if (MAUTIC_ENV === 'dev') { |
||
317 | throw $e; |
||
318 | } else { |
||
319 | error_log($e); |
||
320 | } |
||
321 | } |
||
322 | |||
323 | $this->em->detach($download); |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Increase the download count. |
||
328 | * |
||
329 | * @param $asset |
||
330 | * @param int $increaseBy |
||
331 | * @param bool|false $unique |
||
332 | */ |
||
333 | public function upDownloadCount($asset, $increaseBy = 1, $unique = false) |
||
334 | { |
||
335 | $id = ($asset instanceof Asset) ? $asset->getId() : (int) $asset; |
||
336 | |||
337 | $this->getRepository()->upDownloadCount($id, $increaseBy, $unique); |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * @return \Mautic\AssetBundle\Entity\AssetRepository |
||
342 | */ |
||
343 | public function getRepository() |
||
344 | { |
||
345 | return $this->em->getRepository('MauticAssetBundle:Asset'); |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * @return \Mautic\AssetBundle\Entity\DownloadRepository |
||
350 | */ |
||
351 | public function getDownloadRepository() |
||
352 | { |
||
353 | return $this->em->getRepository('MauticAssetBundle:Download'); |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * @return string |
||
358 | */ |
||
359 | public function getPermissionBase() |
||
360 | { |
||
361 | return 'asset:assets'; |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * @return string |
||
366 | */ |
||
367 | public function getNameGetter() |
||
368 | { |
||
369 | return 'getTitle'; |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * {@inheritdoc} |
||
374 | * |
||
375 | * @throws NotFoundHttpException |
||
376 | */ |
||
377 | public function createForm($entity, $formFactory, $action = null, $options = []) |
||
378 | { |
||
379 | if (!$entity instanceof Asset) { |
||
380 | throw new MethodNotAllowedHttpException(['Asset']); |
||
381 | } |
||
382 | |||
383 | if (!empty($action)) { |
||
384 | $options['action'] = $action; |
||
385 | } |
||
386 | |||
387 | return $formFactory->create('asset', $entity, $options); |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * Get a specific entity or generate a new one if id is empty. |
||
392 | * |
||
393 | * @param $id |
||
394 | * |
||
395 | * @return null|Asset |
||
396 | */ |
||
397 | public function getEntity($id = null) |
||
398 | { |
||
399 | if ($id === null) { |
||
400 | $entity = new Asset(); |
||
401 | } else { |
||
402 | $entity = parent::getEntity($id); |
||
403 | } |
||
404 | |||
405 | return $entity; |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * {@inheritdoc} |
||
410 | * |
||
411 | * @param $action |
||
412 | * @param $event |
||
413 | * @param $entity |
||
414 | * @param $isNew |
||
415 | * |
||
416 | * @throws \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException |
||
417 | */ |
||
418 | protected function dispatchEvent($action, &$entity, $isNew = false, Event $event = null) |
||
419 | { |
||
420 | if (!$entity instanceof Asset) { |
||
421 | throw new MethodNotAllowedHttpException(['Asset']); |
||
422 | } |
||
423 | |||
424 | switch ($action) { |
||
425 | case 'pre_save': |
||
426 | $name = AssetEvents::ASSET_PRE_SAVE; |
||
427 | break; |
||
428 | case 'post_save': |
||
429 | $name = AssetEvents::ASSET_POST_SAVE; |
||
430 | break; |
||
431 | case 'pre_delete': |
||
432 | $name = AssetEvents::ASSET_PRE_DELETE; |
||
433 | break; |
||
434 | case 'post_delete': |
||
435 | $name = AssetEvents::ASSET_POST_DELETE; |
||
436 | break; |
||
437 | default: |
||
438 | return null; |
||
439 | } |
||
440 | |||
441 | if ($this->dispatcher->hasListeners($name)) { |
||
442 | if (empty($event)) { |
||
443 | $event = new AssetEvent($entity, $isNew); |
||
444 | $event->setEntityManager($this->em); |
||
445 | } |
||
446 | |||
447 | $this->dispatcher->dispatch($name, $event); |
||
448 | |||
449 | return $event; |
||
450 | } else { |
||
451 | return null; |
||
452 | } |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * Get list of entities for autopopulate fields. |
||
457 | * |
||
458 | * @param $type |
||
459 | * @param $filter |
||
460 | * @param $limit |
||
461 | * |
||
462 | * @return array |
||
463 | */ |
||
464 | public function getLookupResults($type, $filter = '', $limit = 10) |
||
465 | { |
||
466 | $results = []; |
||
467 | switch ($type) { |
||
468 | case 'asset': |
||
469 | $viewOther = $this->security->isGranted('asset:assets:viewother'); |
||
470 | $repo = $this->getRepository(); |
||
471 | $repo->setCurrentUser($this->userHelper->getUser()); |
||
472 | $results = $repo->getAssetList($filter, $limit, 0, $viewOther); |
||
473 | break; |
||
474 | case 'category': |
||
475 | $results = $this->categoryModel->getRepository()->getCategoryList($filter, $limit, 0); |
||
476 | break; |
||
477 | } |
||
478 | |||
479 | return $results; |
||
480 | } |
||
481 | |||
482 | /** |
||
483 | * Generate url for an asset. |
||
484 | * |
||
485 | * @param Asset $entity |
||
486 | * @param bool $absolute |
||
487 | * @param array $clickthrough |
||
488 | * |
||
489 | * @return string |
||
490 | */ |
||
491 | public function generateUrl($entity, $absolute = true, $clickthrough = []) |
||
492 | { |
||
493 | $assetSlug = $entity->getId().':'.$entity->getAlias(); |
||
494 | |||
495 | $slugs = [ |
||
496 | 'slug' => $assetSlug, |
||
497 | ]; |
||
498 | |||
499 | return $this->buildUrl('mautic_asset_download', $slugs, $absolute, $clickthrough); |
||
500 | } |
||
501 | |||
502 | /** |
||
503 | * Determine the max upload size based on PHP restrictions and config. |
||
504 | * |
||
505 | * @param string $unit If '', determine the best unit based on the number |
||
506 | * @param bool|false $humanReadable Return as a human readable filesize |
||
507 | * |
||
508 | * @return float |
||
509 | */ |
||
510 | public function getMaxUploadSize($unit = 'M', $humanReadable = false) |
||
511 | { |
||
512 | $maxAssetSize = $this->maxAssetSize; |
||
513 | $maxAssetSize = ($maxAssetSize == -1 || $maxAssetSize === 0) ? PHP_INT_MAX : Asset::convertSizeToBytes($maxAssetSize.'M'); |
||
514 | $maxPostSize = Asset::getIniValue('post_max_size'); |
||
515 | $maxUploadSize = Asset::getIniValue('upload_max_filesize'); |
||
516 | $memoryLimit = Asset::getIniValue('memory_limit'); |
||
517 | $maxAllowed = min(array_filter([$maxAssetSize, $maxPostSize, $maxUploadSize, $memoryLimit])); |
||
518 | |||
519 | if ($humanReadable) { |
||
520 | $number = Asset::convertBytesToHumanReadable($maxAllowed); |
||
521 | } else { |
||
522 | list($number, $unit) = Asset::convertBytesToUnit($maxAllowed, $unit); |
||
523 | } |
||
524 | |||
525 | return $number; |
||
526 | } |
||
527 | |||
528 | /** |
||
529 | * @param $assets |
||
530 | * |
||
531 | * @return int|string |
||
532 | */ |
||
533 | public function getTotalFilesize($assets) |
||
534 | { |
||
535 | $firstAsset = is_array($assets) ? reset($assets) : false; |
||
536 | if ($assets instanceof PersistentCollection || is_object($firstAsset)) { |
||
537 | $assetIds = []; |
||
538 | foreach ($assets as $asset) { |
||
539 | $assetIds[] = $asset->getId(); |
||
540 | } |
||
541 | $assets = $assetIds; |
||
542 | } |
||
543 | |||
544 | if (!is_array($assets)) { |
||
545 | $assets = [$assets]; |
||
546 | } |
||
547 | |||
548 | if (empty($assets)) { |
||
549 | return 0; |
||
550 | } |
||
551 | |||
552 | $repo = $this->getRepository(); |
||
553 | $size = $repo->getAssetSize($assets); |
||
554 | |||
555 | if ($size) { |
||
556 | $size = Asset::convertBytesToHumanReadable($size); |
||
557 | } |
||
558 | |||
559 | return $size; |
||
560 | } |
||
561 | |||
562 | /** |
||
563 | * Get line chart data of downloads. |
||
564 | * |
||
565 | * @param char $unit {@link php.net/manual/en/function.date.php#refsect1-function.date-parameters} |
||
566 | * @param \DateTime $dateFrom |
||
567 | * @param \DateTime $dateTo |
||
568 | * @param string $dateFormat |
||
569 | * @param array $filter |
||
570 | * @param bool $canViewOthers |
||
571 | * |
||
572 | * @return array |
||
573 | */ |
||
574 | public function getDownloadsLineChartData($unit, \DateTime $dateFrom, \DateTime $dateTo, $dateFormat = null, $filter = [], $canViewOthers = true) |
||
575 | { |
||
576 | $chart = new LineChart($unit, $dateFrom, $dateTo, $dateFormat); |
||
577 | $query = new ChartQuery($this->em->getConnection(), $dateFrom, $dateTo); |
||
578 | $q = $query->prepareTimeDataQuery('asset_downloads', 'date_download', $filter); |
||
579 | |||
580 | if (!$canViewOthers) { |
||
581 | $q->join('t', MAUTIC_TABLE_PREFIX.'assets', 'a', 'a.id = t.asset_id') |
||
582 | ->andWhere('a.created_by = :userId') |
||
583 | ->setParameter('userId', $this->userHelper->getUser()->getId()); |
||
584 | } |
||
585 | |||
586 | $data = $query->loadAndBuildTimeData($q); |
||
587 | |||
588 | $chart->setDataset($this->translator->trans('mautic.asset.downloadcount'), $data); |
||
589 | |||
590 | return $chart->render(); |
||
591 | } |
||
592 | |||
593 | /** |
||
594 | * Get pie chart data of unique vs repetitive downloads. |
||
595 | * Repetitive in this case mean if a lead downloaded any of the assets more than once. |
||
596 | * |
||
597 | * @param string $dateFrom |
||
598 | * @param string $dateTo |
||
599 | * @param array $filters |
||
600 | * @param bool $canViewOthers |
||
601 | * |
||
602 | * @return array |
||
603 | */ |
||
604 | public function getUniqueVsRepetitivePieChartData($dateFrom, $dateTo, $filters = [], $canViewOthers = true) |
||
605 | { |
||
606 | $chart = new PieChart(); |
||
607 | $query = new ChartQuery($this->em->getConnection(), $dateFrom, $dateTo); |
||
608 | $allQ = $query->getCountQuery('asset_downloads', 'id', 'date_download', $filters); |
||
609 | $uniqueQ = $query->getCountQuery('asset_downloads', 'lead_id', 'date_download', $filters, ['getUnique' => true]); |
||
610 | |||
611 | if (!$canViewOthers) { |
||
612 | $allQ->join('t', MAUTIC_TABLE_PREFIX.'assets', 'a', 'a.id = t.asset_id') |
||
613 | ->andWhere('a.created_by = :userId') |
||
614 | ->setParameter('userId', $this->userHelper->getUser()->getId()); |
||
615 | $uniqueQ->join('t', MAUTIC_TABLE_PREFIX.'assets', 'a', 'a.id = t.asset_id') |
||
616 | ->andWhere('a.created_by = :userId') |
||
617 | ->setParameter('userId', $this->userHelper->getUser()->getId()); |
||
618 | } |
||
619 | |||
620 | $all = $query->fetchCount($allQ); |
||
621 | $unique = $query->fetchCount($uniqueQ); |
||
622 | |||
623 | $repetitive = $all - $unique; |
||
624 | $chart->setDataset($this->translator->trans('mautic.asset.unique'), $unique); |
||
625 | $chart->setDataset($this->translator->trans('mautic.asset.repetitive'), $repetitive); |
||
626 | |||
627 | return $chart->render(); |
||
628 | } |
||
629 | |||
630 | /** |
||
631 | * Get a list of popular (by downloads) assets. |
||
632 | * |
||
633 | * @param int $limit |
||
634 | * @param string $dateFrom |
||
635 | * @param string $dateTo |
||
636 | * @param array $filters |
||
637 | * @param bool $canViewOthers |
||
638 | * |
||
639 | * @return array |
||
640 | */ |
||
641 | public function getPopularAssets($limit = 10, $dateFrom = null, $dateTo = null, $filters = [], $canViewOthers = true) |
||
642 | { |
||
643 | $q = $this->em->getConnection()->createQueryBuilder(); |
||
644 | $q->select('COUNT(DISTINCT t.id) AS download_count, a.id, a.title') |
||
645 | ->from(MAUTIC_TABLE_PREFIX.'asset_downloads', 't') |
||
646 | ->join('t', MAUTIC_TABLE_PREFIX.'assets', 'a', 'a.id = t.asset_id') |
||
647 | ->orderBy('download_count', 'DESC') |
||
648 | ->groupBy('a.id') |
||
649 | ->setMaxResults($limit); |
||
650 | |||
651 | if (!$canViewOthers) { |
||
652 | $q->andWhere('a.created_by = :userId') |
||
653 | ->setParameter('userId', $this->userHelper->getUser()->getId()); |
||
654 | } |
||
655 | |||
656 | $chartQuery = new ChartQuery($this->em->getConnection(), $dateFrom, $dateTo); |
||
657 | $chartQuery->applyFilters($q, $filters); |
||
658 | $chartQuery->applyDateFilters($q, 'date_download'); |
||
659 | |||
660 | $results = $q->execute()->fetchAll(); |
||
661 | |||
662 | return $results; |
||
663 | } |
||
664 | |||
665 | /** |
||
666 | * Get a list of assets in a date range. |
||
667 | * |
||
668 | * @param int $limit |
||
669 | * @param \DateTime $dateFrom |
||
670 | * @param \DateTime $dateTo |
||
671 | * @param array $filters |
||
672 | * @param array $options |
||
673 | * |
||
674 | * @return array |
||
675 | */ |
||
676 | public function getAssetList($limit = 10, \DateTime $dateFrom = null, \DateTime $dateTo = null, $filters = [], $options = []) |
||
677 | { |
||
678 | $q = $this->em->getConnection()->createQueryBuilder(); |
||
679 | $q->select('t.id, t.title as name, t.date_added, t.date_modified') |
||
680 | ->from(MAUTIC_TABLE_PREFIX.'assets', 't') |
||
681 | ->setMaxResults($limit); |
||
682 | |||
683 | if (!empty($options['canViewOthers'])) { |
||
684 | $q->andWhere('t.created_by = :userId') |
||
685 | ->setParameter('userId', $this->userHelper->getUser()->getId()); |
||
686 | } |
||
687 | |||
688 | $chartQuery = new ChartQuery($this->em->getConnection(), $dateFrom, $dateTo); |
||
689 | $chartQuery->applyFilters($q, $filters); |
||
690 | $chartQuery->applyDateFilters($q, 'date_added'); |
||
691 | |||
692 | $results = $q->execute()->fetchAll(); |
||
693 | |||
694 | return $results; |
||
695 | } |
||
696 | } |
||
697 |