/** * 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')); }
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; }
/** * Create a campaign for all contacts with a certain tag. * * This action will create and save the campaign and redirect the user to * edit screen to fill in the email message, etc. It is intended to provide * a fast workflow from tags to campaigns. * * @param string $tag */ public function actionCreateFromTag($tag) { //enusre tag sanity if (empty($tag) || strlen(trim($tag)) == 0) { Yii::app()->user->setFlash('error', Yii::t('marketing', 'Invalid tag value')); $this->redirect(Yii::app()->request->getUrlReferrer()); } //ensure sacred hash if (substr($tag, 0, 1) != '#') { $tag = '#' . $tag; } //only works for contacts $modelType = 'Contacts'; $now = time(); //get all contact ids from tags $ids = Yii::app()->db->createCommand()->select('itemId')->from('x2_tags')->where('type=:type AND tag=:tag')->group('itemId')->order('itemId ASC')->bindValues(array(':type' => $modelType, ':tag' => $tag))->queryColumn(); //create static list $list = new X2List(); $list->name = Yii::t('marketing', 'Contacts for tag') . ' ' . $tag; $list->modelName = $modelType; $list->type = 'campaign'; $list->count = count($ids); $list->visibility = 1; $list->assignedTo = Yii::app()->user->getName(); $list->createDate = $now; $list->lastUpdated = $now; //create campaign $campaign = new Campaign(); $campaign->name = Yii::t('marketing', 'Mailing for tag') . ' ' . $tag; $campaign->type = 'Email'; $campaign->visibility = 1; $campaign->assignedTo = Yii::app()->user->getName(); $campaign->createdBy = Yii::app()->user->getName(); $campaign->updatedBy = Yii::app()->user->getName(); $campaign->createDate = $now; $campaign->lastUpdated = $now; $transaction = Yii::app()->db->beginTransaction(); try { if (!$list->save()) { throw new Exception(array_shift(array_shift($list->getErrors()))); } $campaign->listId = $list->nameId; if (!$campaign->save()) { throw new Exception(array_shift(array_shift($campaign->getErrors()))); } foreach ($ids as $id) { $listItem = new X2ListItem(); $listItem->listId = $list->id; $listItem->contactId = $id; if (!$listItem->save()) { throw new Exception(array_shift(array_shift($listItem->getErrors()))); } } $transaction->commit(); $this->redirect($this->createUrl('update', array('id' => $campaign->id))); } catch (Exception $e) { $transaction->rollBack(); Yii::app()->user->setFlash('error', Yii::t('marketing', 'Could not create mailing') . ': ' . $e->getMessage()); $this->redirect(Yii::app()->request->getUrlReferrer()); } }
public function actionAddToList() { if (isset($_POST['gvSelection'], $_POST['listId']) && !empty($_POST['gvSelection']) && is_array($_POST['gvSelection'])) { foreach ($_POST['gvSelection'] as &$contactId) { if (!ctype_digit($contactId)) { throw new CHttpException(400, Yii::t('app', 'Invalid selection.')); } } $list = X2List::model()->findByPk($_POST['listId']); // check permissions if (isset($list) && $list->type == 'static' && $this->checkPermissions($list, 'edit')) { $count = 0; foreach ($_POST['gvSelection'] as &$contactId) { $listItem = new X2ListItem(); $listItem->listId = $list->id; $listItem->contactId = $contactId; if ($listItem->save()) { $count++; } } $list->count = X2ListItem::model()->countByAttributes(array('listId' => $list->id)); $list->save(); echo 'success'; } else { throw new CHttpException(403, Yii::t('app', 'You do not have permission to modify this list.')); } } }