Esempio n. 1
0
 /**
  * @param        $asset
  * @param        $request
  * @param string $code
  * @param array  $systemEntry
  *
  * @throws \Doctrine\ORM\ORMException
  */
 public function trackDownload($asset, $request = null, $code = '200', $systemEntry = array())
 {
     //don't skew results with in-house downloads
     if (empty($systemEntry) && !$this->factory->getSecurity()->isAnonymous()) {
         return;
     }
     if ($request == null) {
         $request = $this->factory->getRequest();
     }
     $download = new Download();
     $download->setDateDownload(new \Datetime());
     /** @var \Mautic\LeadBundle\Model\LeadModel $leadModel */
     $leadModel = $this->factory->getModel('lead');
     // Download triggered by lead
     if (empty($systemEntry)) {
         //check for any clickthrough info
         $clickthrough = $request->get('ct', false);
         if (!empty($clickthrough)) {
             $clickthrough = $this->decodeArrayFromUrl($clickthrough);
             if (!empty($clickthrough['lead'])) {
                 $lead = $leadModel->getEntity($clickthrough['lead']);
                 if ($lead !== null) {
                     $leadModel->setLeadCookie($clickthrough['lead']);
                     list($trackingId, $generated) = $leadModel->getTrackingCookie();
                     $leadClickthrough = true;
                     $leadModel->setCurrentLead($lead);
                 }
             }
             if (!empty($clickthrough['source'])) {
                 $download->setSource($clickthrough['source'][0]);
                 $download->setSourceId($clickthrough['source'][1]);
             }
             if (!empty($clickthrough['email'])) {
                 $download->setEmail($this->em->getReference('MauticEmailBundle:Email', $clickthrough['email']));
             }
         }
         if (empty($leadClickthrough)) {
             list($lead, $trackingId, $generated) = $leadModel->getCurrentLead(true);
         }
         $download->setLead($lead);
     } else {
         $trackingId = '';
         if (isset($systemEntry['lead'])) {
             $lead = $systemEntry['lead'];
             if (!$lead instanceof Lead) {
                 $leadId = is_array($lead) ? $lead['id'] : $lead;
                 $lead = $this->em->getReference('MauticLeadBundle:Lead', $leadId);
             }
             $download->setLead($lead);
         }
         if (!empty($systemEntry['source'])) {
             $download->setSource($systemEntry['source'][0]);
             $download->setSourceId($systemEntry['source'][1]);
         }
         if (isset($systemEntry['email'])) {
             $email = $systemEntry['email'];
             if (!$email instanceof Email) {
                 $emailId = is_array($email) ? $email['id'] : $email;
                 $email = $this->em->getReference('MauticEmailBundle:Email', $emailId);
             }
             $download->setEmail($email);
         }
         if (isset($systemEntry['tracking_id'])) {
             $trackingId = $systemEntry['tracking_id'];
         } elseif ($this->factory->getSecurity()->isAnonymous() && !defined('IN_MAUTIC_CONSOLE')) {
             // If the session is anonymous and not triggered via CLI, assume the lead did something to trigger the
             // system forced download such as an email
             list($trackingId, $generated) = $leadModel->getTrackingCookie();
         }
     }
     $download->setTrackingId($trackingId);
     if (!empty($asset) && empty($systemEntry)) {
         $download->setAsset($asset);
         $downloadCount = $asset->getDownloadCount();
         $downloadCount++;
         $asset->setDownloadCount($downloadCount);
         //check for a download count from tracking id
         $countById = $this->getDownloadRepository()->getDownloadCountForTrackingId($asset->getId(), $trackingId);
         if (empty($countById)) {
             $uniqueDownloadCount = $asset->getUniqueDownloadCount();
             $uniqueDownloadCount++;
             $asset->setUniqueDownloadCount($uniqueDownloadCount);
         }
         $this->em->persist($asset);
     }
     //check for existing IP
     $ipAddress = $this->factory->getIpAddress();
     $download->setCode($code);
     $download->setIpAddress($ipAddress);
     $download->setReferer($request->server->get('HTTP_REFERER'));
     $this->em->persist($download);
     // Wrap in a try/catch to prevent deadlock errors on busy servers
     try {
         $this->em->flush();
     } catch (\Exception $e) {
         error_log($e);
     }
 }
 /**
  * {@inheritDoc}
  */
 public function setEmail(\Mautic\EmailBundle\Entity\Email $email)
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'setEmail', array($email));
     return parent::setEmail($email);
 }
Esempio n. 3
0
 /**
  * @param $asset
  * @param null   $request
  * @param string $code
  * @param array  $systemEntry
  *
  * @throws \Doctrine\ORM\ORMException
  * @throws \Exception
  */
 public function trackDownload($asset, $request = null, $code = '200', $systemEntry = [])
 {
     // Don't skew results with in-house downloads
     if (empty($systemEntry) && !$this->security->isAnonymous()) {
         return;
     }
     if ($request == null) {
         $request = $this->request;
     }
     $download = new Download();
     $download->setDateDownload(new \Datetime());
     // Download triggered by lead
     if (empty($systemEntry)) {
         //check for any clickthrough info
         $clickthrough = $request->get('ct', false);
         if (!empty($clickthrough)) {
             $clickthrough = $this->decodeArrayFromUrl($clickthrough);
             if (!empty($clickthrough['lead'])) {
                 $lead = $this->leadModel->getEntity($clickthrough['lead']);
                 if ($lead !== null) {
                     $this->leadModel->setLeadCookie($clickthrough['lead']);
                     list($trackingId, $trackingNewlyGenerated) = $this->leadModel->getTrackingCookie();
                     $leadClickthrough = true;
                     $this->leadModel->setCurrentLead($lead);
                 }
             }
             if (!empty($clickthrough['channel'])) {
                 if (count($clickthrough['channel']) === 1) {
                     $channelId = reset($clickthrough['channel']);
                     $channel = key($clickthrough['channel']);
                 } else {
                     $channel = $clickthrough['channel'][0];
                     $channelId = (int) $clickthrough['channel'][1];
                 }
                 $download->setSource($channel);
                 $download->setSourceId($channelId);
             } elseif (!empty($clickthrough['source'])) {
                 $download->setSource($clickthrough['source'][0]);
                 $download->setSourceId($clickthrough['source'][1]);
             }
             if (!empty($clickthrough['email'])) {
                 $emailRepo = $this->em->getRepository('MauticEmailBundle:Email');
                 if ($emailEntity = $emailRepo->getEntity($clickthrough['email'])) {
                     $download->setEmail($emailEntity);
                 }
             }
         }
         if (empty($leadClickthrough)) {
             list($lead, $trackingId, $trackingNewlyGenerated) = $this->leadModel->getCurrentLead(true);
         }
         $download->setLead($lead);
     } else {
         $trackingId = '';
         if (isset($systemEntry['lead'])) {
             $lead = $systemEntry['lead'];
             if (!$lead instanceof Lead) {
                 $leadId = is_array($lead) ? $lead['id'] : $lead;
                 $lead = $this->em->getReference('MauticLeadBundle:Lead', $leadId);
             }
             $download->setLead($lead);
         }
         if (!empty($systemEntry['source'])) {
             $download->setSource($systemEntry['source'][0]);
             $download->setSourceId($systemEntry['source'][1]);
         }
         if (isset($systemEntry['email'])) {
             $email = $systemEntry['email'];
             if (!$email instanceof Email) {
                 $emailId = is_array($email) ? $email['id'] : $email;
                 $email = $this->em->getReference('MauticEmailBundle:Email', $emailId);
             }
             $download->setEmail($email);
         }
         if (isset($systemEntry['tracking_id'])) {
             $trackingId = $systemEntry['tracking_id'];
             $trackingNewlyGenerated = false;
         } elseif ($this->security->isAnonymous() && !defined('IN_MAUTIC_CONSOLE')) {
             // If the session is anonymous and not triggered via CLI, assume the lead did something to trigger the
             // system forced download such as an email
             list($trackingId, $trackingNewlyGenerated) = $this->leadModel->getTrackingCookie();
         }
     }
     $isUnique = true;
     if (!empty($trackingNewlyGenerated)) {
         // Cookie was just generated so this is definitely a unique download
         $isUnique = $trackingNewlyGenerated;
     } elseif (!empty($trackingId)) {
         // Determine if this is a unique download
         $isUnique = $this->getDownloadRepository()->isUniqueDownload($asset->getId(), $trackingId);
     }
     $download->setTrackingId($trackingId);
     if (!empty($asset) && empty($systemEntry)) {
         $download->setAsset($asset);
         $this->getRepository()->upDownloadCount($asset->getId(), 1, $isUnique);
     }
     //check for existing IP
     $ipAddress = $this->ipLookupHelper->getIpAddress();
     $download->setCode($code);
     $download->setIpAddress($ipAddress);
     if ($request !== null) {
         $download->setReferer($request->server->get('HTTP_REFERER'));
     }
     // Dispatch event
     if ($this->dispatcher->hasListeners(AssetEvents::ASSET_ON_LOAD)) {
         $event = new AssetLoadEvent($download, $isUnique);
         $this->dispatcher->dispatch(AssetEvents::ASSET_ON_LOAD, $event);
     }
     // Wrap in a try/catch to prevent deadlock errors on busy servers
     try {
         $this->em->persist($download);
         $this->em->flush($download);
     } catch (\Exception $e) {
         if (MAUTIC_ENV === 'dev') {
             throw $e;
         } else {
             error_log($e);
         }
     }
     $this->em->detach($download);
 }