/** * Adds the specified ID(s) to this list (if they're not already in there) * @param mixed $ids a single integer or an array of integer IDs */ public function addIds($ids) { if ($this->type !== 'static') { return false; } $ids = (array) $ids; $parameters = AuxLib::bindArray($ids, 'addIds'); $existingIds = Yii::app()->db->createCommand()->select('contactId')->from('x2_list_items')->where('listId=' . $this->id . ' AND contactId IN(' . implode(',', array_keys($parameters)) . ')', $parameters)->queryColumn(); foreach ($ids as $id) { if (in_array($id, $existingIds)) { continue; } $listItem = new X2ListItem(); $listItem->listId = $this->id; $listItem->contactId = $id; $listItem->save(); } $this->count = CActiveRecord::model('X2ListItem')->countByAttributes(array('listId' => $this->id)); return $this->update(array('count')); }
/** * 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 execute(array $gvSelection) { if (Yii::app()->controller->modelClass !== 'Contacts' || !isset($_POST['listName']) || $_POST['listName'] === '') { throw new CHttpException(400, Yii::t('app', 'Bad Request')); } if (!Yii::app()->params->isAdmin && !Yii::app()->user->checkAccess('ContactsCreateListFromSelection')) { return -1; } $listName = $_POST['listName']; foreach ($gvSelection as &$contactId) { if (!ctype_digit((string) $contactId)) { throw new CHttpException(400, Yii::t('app', 'Invalid selection.')); } } $list = new X2List(); $list->name = $_POST['listName']; $list->modelName = 'Contacts'; $list->type = 'static'; $list->assignedTo = Yii::app()->user->getName(); $list->visibility = 1; $list->createDate = time(); $list->lastUpdated = time(); $itemModel = X2Model::model('Contacts'); $success = true; if ($list->save()) { // if the list is valid save it so we can get the ID $count = 0; foreach ($gvSelection as &$itemId) { if ($itemModel->exists('id="' . $itemId . '"')) { // check if contact exists $item = new X2ListItem(); $item->contactId = $itemId; $item->listId = $list->id; if ($item->save()) { // add all the things! $count++; } } } $list->count = $count; $this->listId = $list->id; if ($list->save()) { self::$successFlashes[] = Yii::t('app', '{count} record' . ($count === 1 ? '' : 's') . ' added to new list "{list}"', array('{count}' => $count, '{list}' => $list->name)); } else { self::$errorFlashes[] = Yii::t('app', 'List created but records could not be added to it'); } } else { $success = false; self::$errorFlashes[] = Yii::t('app', 'List could not be created'); } return $success ? $count : -1; }
/** * 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 }
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')); }