/** * 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()); } }
/** * @depends testCreateAndGetCampaignListById */ public function testRequiredAttributes() { $campaign = new Campaign(); $this->assertFalse($campaign->save()); $errors = $campaign->getErrors(); $this->assertNotEmpty($errors); $this->assertCount(7, $errors); $this->assertArrayHasKey('name', $errors); $this->assertEquals('Name cannot be blank.', $errors['name'][0]); $this->assertArrayHasKey('supportsRichText', $errors); $this->assertEquals('Supports HTML cannot be blank.', $errors['supportsRichText'][0]); $this->assertArrayHasKey('subject', $errors); $this->assertEquals('Subject cannot be blank.', $errors['subject'][0]); $this->assertArrayHasKey('fromName', $errors); $this->assertEquals('From Name cannot be blank.', $errors['fromName'][0]); $this->assertArrayHasKey('fromAddress', $errors); $this->assertEquals('From Address cannot be blank.', $errors['fromAddress'][0]); $this->assertArrayHasKey('textContent', $errors); $this->assertEquals('Please provide at least one of the contents field.', $errors['textContent'][0]); $this->assertArrayHasKey('marketingList', $errors); $this->assertEquals('Marketing List cannot be blank.', $errors['marketingList'][0]); $campaign->name = 'Test Campaign Name2'; $campaign->supportsRichText = 0; $campaign->status = Campaign::STATUS_ACTIVE; $campaign->fromName = 'From Name2'; $campaign->fromAddress = '*****@*****.**'; $campaign->subject = 'Test Subject2'; $campaign->htmlContent = 'Test Html Content2'; $campaign->textContent = 'Test Text Content2'; $campaign->fromName = 'From Name2'; $campaign->fromAddress = '*****@*****.**'; $campaign->marketingList = self::$marketingList; $this->assertTrue($campaign->save()); $id = $campaign->id; unset($campaign); $campaign = Campaign::getById($id); $this->assertEquals('Test Campaign Name2', $campaign->name); $this->assertEquals(0, $campaign->supportsRichText); $this->assertEquals(Campaign::STATUS_ACTIVE, $campaign->status); $this->assertEquals('From Name2', $campaign->fromName); $this->assertEquals('*****@*****.**', $campaign->fromAddress); $this->assertEquals('Test Subject2', $campaign->subject); $this->assertEquals('Test Html Content2', $campaign->htmlContent); $this->assertEquals('Test Text Content2', $campaign->textContent); $this->assertTrue(time() + 15 > DateTimeUtil::convertDbFormatDateTimeToTimestamp($campaign->sendOnDateTime)); }
/** * @depends testCreateAndGetCampaignListById */ public function testDummyHtmlContentThrowsValidationErrorWhenTextContentIsEmpty() { $campaign = new Campaign(); $campaign->name = 'Another Test Campaign Name'; $campaign->supportsRichText = 1; $campaign->status = Campaign::STATUS_ACTIVE; $campaign->fromName = 'Another From Name'; $campaign->fromAddress = '*****@*****.**'; $campaign->subject = 'Another Test Subject'; $campaign->textContent = ''; $campaign->htmlContent = "<html>\n<head>\n</head>\n<body>\n</body>\n</html>"; $campaign->marketingList = self::$marketingList; $this->assertFalse($campaign->save()); $errorMessages = $campaign->getErrors(); $this->assertEquals(1, count($errorMessages)); $this->assertTrue(array_key_exists('textContent', $errorMessages)); $this->assertEquals(1, count($errorMessages['textContent'])); $this->assertEquals('Please provide at least one of the contents field.', $errorMessages['textContent'][0]); $campaign->htmlContent = 'Text Content'; $this->assertTrue($campaign->save()); $id = $campaign->id; unset($campaign); $campaign = Campaign::getById($id); $this->assertEquals('Another Test Campaign Name', $campaign->name); $this->assertEquals(1, $campaign->supportsRichText); $this->assertEquals(Campaign::STATUS_ACTIVE, $campaign->status); $this->assertEquals('Another From Name', $campaign->fromName); $this->assertEquals('*****@*****.**', $campaign->fromAddress); $this->assertEquals('Another Test Subject', $campaign->subject); $this->assertEquals(null, $campaign->textContent); $this->assertEquals('Text Content', $campaign->htmlContent); $this->assertEquals(self::$marketingList->id, $campaign->marketingList->id); }
function deleteAction() { $id = AF::get($_POST, 'id', 0); $modelsID = explode(',', $id); $errors = FALSE; foreach ($modelsID as $id) { $model = new Campaign(); $model->model_uset_id = $this->user->user_id; if ($model->findByPk($id)) { // if beforeDelete() returns an error, indicate this to the user if (!$model->delete($id)) { $errors = TRUE; } } else { $errors = TRUE; } if ($model->getErrors()) { $errors = TRUE; } unset($model); } if (isset($_POST['ajax'])) { AF::setJsonHeaders('json'); if ($errors) { Message::echoJsonError(__('campaign_no_deleted')); } else { Message::echoJsonSuccess(__('campaign_deleted')); } } $this->redirect(); }