/** * Creates, saves, and returns a duplicate static list containing the same items. * * @return X2List|null The active record model for the static clone is returned * if the operation was successful; otherwise Null is returned. */ public function staticDuplicate() { $dup = new X2List(); $dup->attributes = $this->attributes; $dup->id = null; $dup->nameId = null; $dup->type = 'static'; $dup->createDate = $dup->lastUpdated = time(); $dup->isNewRecord = true; if (!$dup->save()) { return; } $count = 0; $listItemRecords = array(); $params = array(); if ($this->type == 'dynamic') { $itemIds = $this->queryCommand(true)->select('id')->queryColumn(); foreach ($itemIds as $id) { $listItemRecords[] = '(NULL,:contactId' . $count . ',:listId' . $count . ',0)'; $params[':contactId' . $count] = $id; $params[':listId' . $count] = $dup->id; $count++; } } else { //static type lists //generate sql to replicate list items foreach ($this->listItems as $listItem) { if (!empty($listItem->emailAddress)) { $itemSql = '(:email' . $count; $params[':email' . $count] = $listItem->emailAddress; } else { $itemSql = '(NULL'; } if (!empty($listItem->contactId)) { $itemSql .= ',:contactId' . $count; $params[':contactId' . $count] = $listItem->contactId; } else { $itemSql .= ',NULL'; } $itemSql .= ',:listId' . $count . ',:unsubd' . $count . ')'; $params[':listId' . $count] = $dup->id; $params[':unsubd' . $count] = $listItem->unsubscribed; $listItemRecords[] = $itemSql; $count++; } } if (count($listItemRecords) == 0) { return; } $sql = 'INSERT into x2_list_items (emailAddress, contactId, listId, unsubscribed) VALUES ' . implode(',', $listItemRecords) . ';'; $dup->count = $count; $transaction = Yii::app()->db->beginTransaction(); try { Yii::app()->db->createCommand($sql)->execute($params); $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); $dup->delete(); Yii::log($e->getMessage(), 'error', 'application'); $dup = null; } return $dup; }
/** * 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 actionCreateList($ajax = false) { $list = new X2List(); $list->modelName = 'Contacts'; $list->type = 'dynamic'; $list->assignedTo = Yii::app()->user->getName(); $list->visibility = 1; $contactModel = new Contacts(); $comparisonList = X2List::getComparisonList(); if (isset($_POST['X2List'])) { $list->attributes = $_POST['X2List']; $list->modelName = 'Contacts'; $list->createDate = time(); $list->lastUpdated = time(); if (isset($_POST['X2List'], $_POST['X2List']['attribute'], $_POST['X2List']['comparison'], $_POST['X2List']['value'])) { $attributes =& $_POST['X2List']['attribute']; $comparisons =& $_POST['X2List']['comparison']; $values =& $_POST['X2List']['value']; if (count($attributes) > 0 && count($attributes) == count($comparisons) && count($comparisons) == count($values)) { $list->attributes = $_POST['X2List']; $list->modelName = 'Contacts'; $list->lastUpdated = time(); if ($list->save()) { if ($ajax) { echo CJSON::encode($list->attributes); return; } $this->redirect(array('/contacts/contacts/list', 'id' => $list->id)); } } } } if (empty($criteriaModels)) { $default = new X2ListCriterion(); $default->value = ''; $default->attribute = ''; $default->comparison = 'contains'; $criteriaModels[] = $default; } if ($ajax) { $html = $this->renderPartial('createList', array('model' => $list, 'criteriaModels' => $criteriaModels, 'users' => User::getNames(), 'comparisonList' => $comparisonList, 'listTypes' => array('dynamic' => Yii::t('contacts', 'Dynamic'), 'static' => Yii::t('contacts', 'Static')), 'itemModel' => $contactModel), false); echo $this->processOutput($html); return; } $this->render('createList', array('model' => $list, 'criteriaModels' => $criteriaModels, 'users' => User::getNames(), 'comparisonList' => $comparisonList, 'listTypes' => array('dynamic' => Yii::t('contacts', 'Dynamic'), 'static' => Yii::t('contacts', 'Static')), 'itemModel' => $contactModel)); }
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; }
public function actionCreateList() { $list = new X2List(); $list->modelName = 'Contacts'; $list->type = 'dynamic'; $list->assignedTo = Yii::app()->user->getName(); $list->visibility = 1; $contactModel = new Contacts(); $comparisonList = array('=' => '=', '>' => '>', '<' => '<', '<>' => '<>', 'contains' => Yii::t('contacts', 'contains'), 'empty' => Yii::t('empty', 'empty'), 'notEmpty' => Yii::t('contacts', 'not empty'), 'list' => Yii::t('contacts', 'in list')); if (isset($_POST['X2List'])) { $list->attributes = $_POST['X2List']; $list->modelName = 'Contacts'; $list->createDate = time(); $list->lastUpdated = time(); if ($list->type == 'dynamic') { $criteriaModels = X2ListCriterion::model()->findAllByAttributes(array('listId' => $list->id), new CDbCriteria(array('order' => 'id ASC'))); } if (isset($_POST['X2List'], $_POST['X2List']['attribute'], $_POST['X2List']['comparison'], $_POST['X2List']['value'])) { $attributes =& $_POST['X2List']['attribute']; $comparisons =& $_POST['X2List']['comparison']; $values =& $_POST['X2List']['value']; if (count($attributes) > 0 && count($attributes) == count($comparisons) && count($comparisons) == count($values)) { $list->attributes = $_POST['X2List']; $list->modelName = 'Contacts'; $list->lastUpdated = time(); if ($list->save()) { X2ListCriterion::model()->deleteAllByAttributes(array('listId' => $list->id)); // delete old criteria for ($i = 0; $i < count($attributes); $i++) { // create new criteria if ((array_key_exists($attributes[$i], $contactModel->attributeLabels()) || $attributes[$i] == 'tags') && array_key_exists($comparisons[$i], $comparisonList)) { //&& $values[$i] != '' $criterion = new X2ListCriterion(); $criterion->listId = $list->id; $criterion->type = 'attribute'; $criterion->attribute = $attributes[$i]; $criterion->comparison = $comparisons[$i]; $criterion->value = $values[$i]; $criterion->save(); } } $this->redirect(array('/contacts/list/' . $list->id)); } } } } if (empty($criteriaModels)) { $default = new X2ListCriterion(); $default->value = ''; $default->attribute = ''; $default->comparison = 'contains'; $criteriaModels[] = $default; } $this->render('createList', array('model' => $list, 'criteriaModels' => $criteriaModels, 'users' => User::getNames(), 'comparisonList' => $comparisonList, 'listTypes' => array('dynamic' => Yii::t('contacts', 'Dynamic'), 'static' => Yii::t('contacts', 'Static')), 'itemModel' => $contactModel)); }
/** * Creates, saves, and returns a duplicate static list containing the same items. */ public function staticDuplicate() { $dup = new X2List(); $dup->attributes = $this->attributes; $dup->id = null; $dup->type = 'static'; $dup->createDate = $dup->lastUpdated = time(); $dup->isNewRecord = true; if (!$dup->save()) { return; } $count = 0; $itemIds = $this->dbCommand()->select('id')->queryColumn(); //generate some sql, because I can't find a yii way to insert many records in one query $values = ''; foreach ($itemIds as $id) { if ($count !== 0) { $values .= ','; } $values .= '(' . $id . ',' . $dup->id . ')'; $count++; } $sql = 'INSERT into x2_list_items (contactId, listId) VALUES ' . $values . ';'; Yii::app()->db->createCommand($sql)->execute(); $dup->count = $count; $dup->save(); return $dup; }