Exemplo n.º 1
0
 /**
  * Track a click-through and return the URL to redirect.  If the numbers
  * don't match up, return the base url.
  *
  * @param int $queue_id
  *   The Queue Event ID of the clicker.
  * @param int $url_id
  *   The ID of the trackable URL.
  *
  * @return string
  *   The redirection url, or base url on failure.
  */
 public static function track($queue_id, $url_id)
 {
     $search = new CRM_Mailing_BAO_TrackableURL();
     /* To find the url, we also join on the queue and job tables.  This
      * prevents foreign key violations. */
     $job = CRM_Mailing_BAO_MailingJob::getTableName();
     $eq = CRM_Mailing_Event_BAO_Queue::getTableName();
     $turl = CRM_Mailing_BAO_TrackableURL::getTableName();
     if (!$queue_id) {
         $search->query("SELECT {$turl}.url as url from {$turl}\n                    WHERE {$turl}.id = " . CRM_Utils_Type::escape($url_id, 'Integer'));
         if (!$search->fetch()) {
             return CRM_Utils_System::baseURL();
         }
         return $search->url;
     }
     $search->query("SELECT {$turl}.url as url from {$turl}\n                    INNER JOIN {$job} ON {$turl}.mailing_id = {$job}.mailing_id\n                    INNER JOIN {$eq} ON {$job}.id = {$eq}.job_id\n                    WHERE {$eq}.id = " . CRM_Utils_Type::escape($queue_id, 'Integer') . " AND {$turl}.id = " . CRM_Utils_Type::escape($url_id, 'Integer'));
     if (!$search->fetch()) {
         /* Whoops, error, don't track it.  Return the base url. */
         return CRM_Utils_System::baseURL();
     }
     $open = new CRM_Mailing_Event_BAO_TrackableURLOpen();
     $open->event_queue_id = $queue_id;
     $open->trackable_url_id = $url_id;
     $open->time_stamp = date('YmdHis');
     $open->save();
     return $search->url;
 }
Exemplo n.º 2
0
 /**
  * Track a click-through and return the URL to redirect.
  *
  * If the numbers don't match up, return the base url.
  *
  * @param int $queue_id
  *   The Queue Event ID of the clicker.
  * @param int $url_id
  *   The ID of the trackable URL.
  *
  * @return string
  *   The redirection url, or base url on failure.
  */
 public static function track($queue_id, $url_id)
 {
     // To find the url, we also join on the queue and job tables.  This
     // prevents foreign key violations.
     $job = CRM_Utils_Type::escape(CRM_Mailing_BAO_MailingJob::getTableName(), 'MysqlColumnNameOrAlias');
     $eq = CRM_Utils_Type::escape(CRM_Mailing_Event_BAO_Queue::getTableName(), 'MysqlColumnNameOrAlias');
     $turl = CRM_Utils_Type::escape(CRM_Mailing_BAO_TrackableURL::getTableName(), 'MysqlColumnNameOrAlias');
     if (!$queue_id) {
         $search = CRM_Core_DAO::executeQuery("SELECT url\n           FROM {$turl}\n          WHERE {$turl}.id = %1", array(1 => array($url_id, 'Integer')));
         if (!$search->fetch()) {
             return CRM_Utils_System::baseURL();
         }
         return $search->url;
     }
     $search = CRM_Core_DAO::executeQuery("SELECT {$turl}.url as url\n         FROM {$turl}\n        INNER JOIN {$job} ON {$turl}.mailing_id = {$job}.mailing_id\n        INNER JOIN {$eq} ON {$job}.id = {$eq}.job_id\n        WHERE {$eq}.id = %1 AND {$turl}.id = %2", array(1 => array($queue_id, 'Integer'), 2 => array($url_id, 'Integer')));
     if (!$search->fetch()) {
         // Can't find either the URL or the queue. If we can find the URL then
         // return the URL without tracking.  Otherwise return the base URL.
         $search = CRM_Core_DAO::executeQuery("SELECT {$turl}.url as url\n           FROM {$turl}\n          WHERE {$turl}.id = %1", array(1 => array($url_id, 'Integer')));
         if (!$search->fetch()) {
             return CRM_Utils_System::baseURL();
         }
         return $search->url;
     }
     $open = new CRM_Mailing_Event_BAO_TrackableURLOpen();
     $open->event_queue_id = $queue_id;
     $open->trackable_url_id = $url_id;
     $open->time_stamp = date('YmdHis');
     $open->save();
     return $search->url;
 }
Exemplo n.º 3
0
 public static function processMandrillCalls($reponse)
 {
     $events = array('open', 'click', 'hard_bounce', 'soft_bounce', 'spam', 'reject');
     $bounceType = array();
     //MTE-17
     $config = CRM_Core_Config::singleton();
     if (property_exists($config, 'civiVersion')) {
         $civiVersion = $config->civiVersion;
     } else {
         $civiVersion = CRM_Core_BAO_Domain::version();
     }
     if (version_compare('4.4alpha1', $civiVersion) > 0) {
         $jobCLassName = 'CRM_Mailing_DAO_Job';
     } else {
         $jobCLassName = 'CRM_Mailing_DAO_MailingJob';
     }
     foreach ($reponse as $value) {
         //changes done to check if email exists in response array
         if (in_array($value['event'], $events) && CRM_Utils_Array::value('email', $value['msg'])) {
             $metaData = CRM_Utils_Array::value('metadata', $value['msg']) ? CRM_Utils_Array::value('CiviCRM_Mandrill_id', $value['msg']['metadata']) : NULL;
             $header = self::extractHeader($metaData);
             $mail = self::getMailing($header, $jobCLassName);
             $contacts = array();
             if ($mail->find(TRUE)) {
                 if ($value['event'] == 'click' && $mail->url_tracking === FALSE || $value['event'] == 'open' && $mail->open_tracking === FALSE) {
                     continue;
                 }
                 $emails = self::retrieveEmailContactId($value['msg']['email']);
                 if (!CRM_Utils_Array::value('contact_id', $emails['email'])) {
                     continue;
                 }
                 $value['mailing_id'] = $mail->id;
                 // IF no activity id in header then create new activity
                 if (empty($header[0])) {
                     self::createActivity($value, NULL, $header);
                 }
                 if (empty($header[2])) {
                     $params = array('job_id' => CRM_Core_DAO::getFieldValue($jobCLassName, $mail->id, 'id', 'mailing_id'), 'contact_id' => $emails['email']['contact_id'], 'email_id' => $emails['email']['id']);
                     $eventQueue = CRM_Mailing_Event_BAO_Queue::create($params);
                     $eventQueueID = $eventQueue->id;
                     $hash = $eventQueue->hash;
                     $jobId = $params['job_id'];
                 } else {
                     $eventQueueID = $header[3];
                     $hash = explode('@', $header[4]);
                     $hash = $hash[0];
                     $jobId = $header[2];
                 }
                 if ($eventQueueID) {
                     $mandrillActivtyParams = array('mailing_queue_id' => $eventQueueID, 'activity_id' => $header[0]);
                     CRM_Mte_BAO_MandrillActivity::create($mandrillActivtyParams);
                 }
                 $msgBody = '';
                 if (!empty($header[0])) {
                     $msgBody = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $header[0], 'details');
                 }
                 $value['mail_body'] = $msgBody;
                 $bType = ucfirst(preg_replace('/_\\w+/', '', $value['event']));
                 $assignedContacts = array();
                 switch ($value['event']) {
                     case 'open':
                         $oe = new CRM_Mailing_Event_BAO_Opened();
                         $oe->event_queue_id = $eventQueueID;
                         $oe->time_stamp = date('YmdHis', $value['ts']);
                         $oe->save();
                         break;
                     case 'click':
                         if (CRM_Utils_Array::value(1, $header) == 'b') {
                             break;
                         }
                         $tracker = new CRM_Mailing_BAO_TrackableURL();
                         $tracker->url = $value['url'];
                         $tracker->mailing_id = $mail->id;
                         if (!$tracker->find(TRUE)) {
                             $tracker->save();
                         }
                         $open = new CRM_Mailing_Event_BAO_TrackableURLOpen();
                         $open->event_queue_id = $eventQueueID;
                         $open->trackable_url_id = $tracker->id;
                         $open->time_stamp = date('YmdHis', $value['ts']);
                         $open->save();
                         break;
                     case 'hard_bounce':
                     case 'soft_bounce':
                     case 'spam':
                     case 'reject':
                         if (empty($bounceType)) {
                             CRM_Core_PseudoConstant::populate($bounceType, 'CRM_Mailing_DAO_BounceType', TRUE, 'id', NULL, NULL, NULL, 'name');
                         }
                         //Delete queue in delivered since this email is not successfull
                         $delivered = new CRM_Mailing_Event_BAO_Delivered();
                         $delivered->event_queue_id = $eventQueueID;
                         if ($delivered->find(TRUE)) {
                             $delivered->delete();
                         }
                         $bounceParams = array('time_stamp' => date('YmdHis', $value['ts']), 'event_queue_id' => $eventQueueID, 'bounce_type_id' => $bounceType["Mandrill {$bType}"], 'job_id' => $jobId, 'hash' => $hash);
                         $bounceParams['bounce_reason'] = CRM_Utils_Array::value('bounce_description', $value['msg']);
                         if (empty($bounceParams['bounce_reason'])) {
                             $bounceParams['bounce_reason'] = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_BounceType', $bounceType["Mandrill {$bType}"], 'description');
                         }
                         CRM_Mailing_Event_BAO_Bounce::create($bounceParams);
                         if (substr($value['event'], -7) == '_bounce') {
                             $mailingBackend = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MAILING_PREFERENCES_NAME, 'mandrill_smtp_settings');
                             if (CRM_Utils_Array::value('group_id', $mailingBackend)) {
                                 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
                                 $mailBody = ts('The following email failed to be delivered due to a') . " {$bType} Bounce :</br>\nTo: {$value['msg']['email']} </br>\nFrom: {$value['msg']['sender']} </br>\nSubject: {$value['msg']['subject']}</br>\nMessage Body: {$msgBody}";
                                 $mailParams = array('groupName' => 'Mandrill bounce notification', 'from' => '"' . $domainEmailName . '" <' . $domainEmailAddress . '>', 'subject' => ts('Mandrill Bounce Notification'), 'text' => $mailBody, 'html' => $mailBody);
                                 $query = "SELECT ce.email, cc.sort_name, cgc.contact_id FROM civicrm_contact cc\nINNER JOIN civicrm_group_contact cgc ON cgc.contact_id = cc.id\nINNER JOIN civicrm_email ce ON ce.contact_id = cc.id\nWHERE cc.is_deleted = 0 AND cc.is_deceased = 0 AND cgc.group_id = {$mailingBackend['group_id']} AND ce.is_primary = 1 AND ce.email <> %1";
                                 $queryParam = array(1 => array($value['msg']['email'], 'String'));
                                 $dao = CRM_Core_DAO::executeQuery($query, $queryParam);
                                 while ($dao->fetch()) {
                                     $mailParams['toName'] = $dao->sort_name;
                                     $mailParams['toEmail'] = $dao->email;
                                     CRM_Utils_Mail::send($mailParams);
                                     $value['assignee_contact_id'][] = $dao->contact_id;
                                 }
                             }
                         }
                         $bType = 'Bounce';
                         break;
                 }
                 // create activity for click and open event
                 if ($value['event'] == 'open' || $value['event'] == 'click' || $bType == 'Bounce') {
                     self::createActivity($value, $bType, $header);
                 }
             }
         }
     }
 }