Beispiel #1
0
 /**
  * Returns the count of items in the list that have the specified status
  * (i.e. sent, opened, clicked, unsubscribed)
  * @return integer
  */
 public function statusCount($type)
 {
     $whitelist = array('sent', 'opened', 'clicked', 'unsubscribed');
     if (!in_array($type, $whitelist)) {
         return 0;
     }
     $lstTbl = X2ListItem::model()->tableName();
     $count = Yii::app()->db->createCommand('SELECT COUNT(*) FROM ' . $lstTbl . ' WHERE listId = :listid AND ' . $type . ' > 0')->queryScalar(array('listid' => $this->id));
     return $count;
 }
Beispiel #2
0
 /**
  * Track when an email is viewed, a link is clicked, or the recipient unsubscribes
  *
  * Campaign emails include an img tag to a blank image to track when the message was opened,
  * an unsubscribe link, and converted links to track when a recipient clicks a link.
  * All those links are handled by this action.
  *
  * @param integer $uid The unique id of the recipient
  * @param string $type 'open', 'click', or 'unsub'
  * @param string $url For click types, this is the urlencoded URL to redirect to
  * @param string $email For unsub types, this is the urlencoded email address
  *  of the person unsubscribing
  */
 public function actionClick($uid, $type, $url = null, $email = null)
 {
     $now = time();
     $item = CActiveRecord::model('X2ListItem')->with('contact', 'list')->findByAttributes(array('uniqueId' => $uid));
     // It should never happen that we have a list item without a campaign,
     // but it WILL happen on any old db where x2_list_items does not cascade on delete
     // we can't track anything if the listitem was deleted, but at least prevent breaking links
     if ($item === null || $item->list->campaign === null) {
         if ($type == 'click') {
             // campaign redirect link click
             $this->redirect(urldecode($url));
         } elseif ($type == 'open') {
             //return a one pixel transparent gif
             header('Content-Type: image/gif');
             echo base64_decode('R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==');
         } elseif ($type == 'unsub' && !empty($email)) {
             Contacts::model()->updateAll(array('doNotEmail' => true), 'email=:email', array(':email' => $email));
             X2ListItem::model()->updateAll(array('unsubscribed' => time()), 'emailAddress=:email AND unsubscribed=0', array('email' => $email));
             $message = Yii::t('marketing', 'You have been unsubscribed');
             echo '<html><head><title>' . $message . '</title></head><body>' . $message . '</body></html>';
         }
         return;
     }
     $contact = $item->contact;
     $list = $item->list;
     $event = new Events();
     $notif = new Notification();
     $action = new Actions();
     $action->completeDate = $now;
     $action->complete = 'Yes';
     $action->updatedBy = 'API';
     $skipActionEvent = true;
     if ($contact !== null) {
         $skipActionEvent = false;
         if ($email === null) {
             $email = $contact->email;
         }
         $action->associationType = 'contacts';
         $action->associationId = $contact->id;
         $action->associationName = $contact->name;
         $action->visibility = $contact->visibility;
         $action->assignedTo = $contact->assignedTo;
         $event->associationId = $action->associationId;
         $event->associationType = 'Contacts';
         if ($action->assignedTo !== '' && $action->assignedTo !== 'Anyone') {
             $notif->user = $contact->assignedTo;
             $notif->modelType = 'Contacts';
             $notif->modelId = $contact->id;
             $notif->createDate = $now;
             $notif->value = $item->list->campaign->getLink();
         }
     } elseif ($list !== null) {
         $action = new Actions();
         $action->type = 'note';
         $action->createDate = $now;
         $action->lastUpdated = $now;
         $action->completeDate = $now;
         $action->complete = 'Yes';
         $action->updatedBy = 'admin';
         $action->associationType = 'X2List';
         $action->associationId = $list->id;
         $action->associationName = $list->name;
         $action->visibility = $list->visibility;
         $action->assignedTo = $list->assignedTo;
     }
     if ($type == 'unsub') {
         $item->unsubscribe();
         // find any weblists associated with the email address and create unsubscribe actions
         // for each of them
         $sql = 'SELECT t.* 
             FROM x2_lists as t 
             JOIN x2_list_items as li ON t.id=li.listId 
             WHERE li.emailAddress=:email AND t.type="weblist";';
         $weblists = Yii::app()->db->createCommand($sql)->queryAll(true, array('email' => $email));
         foreach ($weblists as $weblist) {
             $weblistAction = new Actions();
             $weblistAction->disableBehavior('changelog');
             //$weblistAction->id = 0; // this causes primary key contraint violation errors
             $weblistAction->isNewRecord = true;
             $weblistAction->type = 'email_unsubscribed';
             $weblistAction->associationType = 'X2List';
             $weblistAction->associationId = $weblist['id'];
             $weblistAction->associationName = $weblist['name'];
             $weblistAction->visibility = $weblist['visibility'];
             $weblistAction->assignedTo = $weblist['assignedTo'];
             $weblistAction->actionDescription = Yii::t('marketing', 'Campaign') . ': ' . $item->list->campaign->name . "\n\n" . $email . " " . Yii::t('marketing', 'has unsubscribed') . ".";
             $weblistAction->save();
         }
         $action->type = 'email_unsubscribed';
         $notif->type = 'email_unsubscribed';
         if ($contact === null) {
             $action->actionDescription = Yii::t('marketing', 'Campaign') . ': ' . $item->list->campaign->name . "\n\n" . $item->emailAddress . ' ' . Yii::t('marketing', 'has unsubscribed') . ".";
         } else {
             $action->actionDescription = Yii::t('marketing', 'Campaign') . ': ' . $item->list->campaign->name . "\n\n" . Yii::t('marketing', 'Contact has unsubscribed') . ".\n" . Yii::t('marketing', '\'Do Not Email\' has been set') . ".";
         }
         $message = Yii::t('marketing', 'You have been unsubscribed');
         echo '<html><head><title>' . $message . '</title></head><body>' . $message . '</body></html>';
     } elseif ($type == 'open') {
         //return a one pixel transparent gif
         header('Content-Type: image/gif');
         echo base64_decode('R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==');
         // Check if it has been marked as opened already, or if the contact
         // no longer exists. If so, exit; nothing more need be done.
         if ($item->opened != 0) {
             Yii::app()->end();
         }
         // This needs to happen before the skip option to accomodate the case of newsletters
         $item->markOpened();
         if ($skipActionEvent) {
             Yii::app()->end();
         }
         $action->disableBehavior('changelog');
         $action->type = 'campaignEmailOpened';
         $event->type = 'email_opened';
         $notif->type = 'email_opened';
         $event->save();
         if ($contact === null) {
             $action->actionDescription = Yii::t('marketing', 'Campaign') . ': ' . $item->list->campaign->name . "\n\n" . $item->emailAddress . ' ' . Yii::t('marketing', 'has opened the email') . ".";
         } else {
             $action->actionDescription = Yii::t('marketing', 'Campaign') . ': ' . $item->list->campaign->name . "\n\n" . Yii::t('marketing', 'Contact has opened the email') . ".";
         }
     } elseif ($type == 'click') {
         // redirect link click
         $item->markClicked($url);
         $action->type = 'email_clicked';
         $notif->type = 'email_clicked';
         if ($contact === null) {
             $action->actionDescription = Yii::t('marketing', 'Campaign') . ': ' . $item->list->campaign->name . "\n\n" . Yii::t('marketing', 'Contact has clicked a link') . ":\n" . urldecode($url);
         } else {
             $action->actionDescription = Yii::t('marketing', 'Campaign') . ': ' . $item->list->campaign->name . "\n\n" . $item->emailAddress . ' ' . Yii::t('marketing', 'has clicked a link') . ":\n" . urldecode($url);
         }
         $this->redirect(urldecode($url));
     }
     $action->save();
     // if any of these hasn't been fully configured
     $notif->save();
     // it will simply not validate and not be saved
 }
 /**
  * Getter for {@link listItem}
  */
 public function getListItem()
 {
     if (!isset($this->_listItem)) {
         $this->_listItem = X2ListItem::model()->findByAttributes(array('id' => $this->itemId));
     }
     return $this->_listItem;
 }
 public function actionDeleteList()
 {
     $id = isset($_GET['id']) ? $_GET['id'] : 'all';
     if (is_numeric($id)) {
         $list = CActiveRecord::model('X2List')->findByPk($id);
     }
     if (isset($list)) {
         // check permissions
         if ($this->checkPermissions($list, 'edit')) {
             X2ListItem::model()->deleteAllByAttributes(array('listId' => $list->id));
             // delete all the things!
             $list->delete();
         } else {
             throw new CHttpException(403, Yii::t('app', 'You do not have permission to modify this list.'));
         }
     }
     $this->redirect(array('/contacts/lists'));
 }