Example #1
0
 public function loadModel($id)
 {
     $Model = CampaignGroup::model()->findByPk($id);
     if ($Model === null) {
         throw new CHttpException(404, 'The requested page does not exist.');
     }
     return $Model;
 }
Example #2
0
 /**
  * Create or update a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionCreateUpdate()
 {
     $step = 1;
     if ($_GET['step']) {
         $step = $_GET['step'];
     }
     $Campaign = new Campaign();
     $Campaign->scenario = "insiderCampaign";
     $CampaignFile = new CampaignFile();
     $NewOutcome = new CampaignOutcome();
     $NewGroup = new CampaignGroup();
     $letters = array('A', 'B', 'C', 'D');
     if ($_GET['id']) {
         $Campaign = $this->loadModel($_GET['id']);
         $this->redirectRunCampaign($Campaign);
         // the following is to cope with groups on various dev environments with no groups
         if (!sizeof($Campaign->groups)) {
             foreach (array('A', 'B') as $letter) {
                 $CampaignGroup = new CampaignGroup();
                 $CampaignGroup->campaign_id = $Campaign->id;
                 $CampaignGroup->name = $letter;
                 $CampaignGroup->fraction = 50;
                 $CampaignGroup->save();
             }
             $this->refresh();
         }
     }
     if (!$Campaign->isNewRecord && !is_null($Campaign->query) && (int) $Campaign->query->invite === 1) {
         // not allowed here as it's an invite campaign.
         throw new CHttpException('401', 'Forbidden');
     }
     if (!$Campaign->isNewRecord && isset($_GET['add-initial']) && !sizeof($Campaign->outcomes)) {
         // add an initial default outcome
         $NewOutcome = new CampaignOutcome();
         $NewOutcome->campaign_id = $Campaign->id;
         $NewOutcome->name = 'An Outcome';
         $NewOutcome->description = 'An outcome description can help explain the desired result of this outcome. Delete this outcome and create your own below.';
         $NewOutcome->save();
         $this->redirect(array('campaign/createUpdate', 'id' => $Campaign->id));
     }
     if (sizeof($_POST['CampaignOutcome'])) {
         // adding outcome to campaign
         $NewOutcome = new CampaignOutcome();
         $NewOutcome->campaign_id = $Campaign->id;
         $NewOutcome->name = $_POST['CampaignOutcome']['name'];
         $NewOutcome->description = $_POST['CampaignOutcome']['description'];
         $NewOutcome->url = $_POST['CampaignOutcome']['url'];
         if (!strlen($NewOutcome->url)) {
             // save null, not
             $NewOutcome->url = null;
         }
         if ($NewOutcome->save()) {
             Yii::app()->user->setFlash('outcome-success', "Successfully created new outcome '" . $NewOutcome->name . "'");
             $this->redirect(array('campaign/createUpdate', 'id' => $Campaign->id, 'step' => 2, '#' => 'outcomes'));
         } else {
             Yii::app()->user->setFlash('outcome-danger', 'Failed to create new outcome. See errors below.');
         }
     }
     if (isset($_GET['remove-outcome'])) {
         $Criteria = new CDbCriteria();
         $Criteria->compare('id', (int) $_GET['remove-outcome']);
         $Criteria->compare('campaign_id', (int) $_GET['campaign_id']);
         $Outcomes = CampaignOutcome::model()->findAll($Criteria);
         if (sizeof($Outcomes) === 1) {
             // deleting one
             $Outcomes[0]->delete();
         }
         Yii::app()->user->setFlash('outcome-success', "Successfully removed the outcome.");
         $this->redirect(array('campaign/createUpdate', 'id' => $Campaign->id, 'step' => 2, '#' => 'outcomes'));
     }
     if (isset($_GET['remove-group'])) {
         $Criteria = new CDbCriteria();
         $Criteria->compare('id', (int) $_GET['remove-group']);
         $Criteria->compare('campaign_id', $Campaign->id);
         CampaignGroup::model()->deleteAll($Criteria);
         Yii::app()->user->setFlash('group-success', "Successfully removed the group.");
         $this->redirect(array('campaign/createUpdate', 'id' => $Campaign->id, 'step' => 3, '#' => 'groups'));
     }
     if (sizeof($_POST['CampaignGroup'])) {
         $step = 3;
         if ($_POST['CampaignGroup']['id']) {
             $CampaignGroup = CampaignGroup::model()->findByPk($_POST['CampaignGroup']['id']);
             $CampaignGroup->setAttributes($_POST['CampaignGroup']);
             if ($CampaignGroup->save()) {
                 Yii::app()->user->setFlash('group-success', 'Group "' . $CampaignGroup->name . '" updated successfully');
                 $this->redirect(array('campaign/createUpdate', 'id' => $Campaign->id, 'step' => 3, '#' => 'groups'));
             } else {
                 $errors = '';
                 foreach ($CampaignGroup->errors as $error) {
                     $errors .= $error[0] . ' ';
                 }
                 Yii::app()->user->setFlash('group-danger', $errors);
             }
         } else {
             //its new
             $NewGroup->setAttributes($_POST['CampaignGroup']);
             $NewGroup->campaign_id = $Campaign->id;
             if ($NewGroup->save()) {
                 Yii::app()->user->setFlash('group-success', 'Group "' . $NewGroup->name . '" updated successfully');
                 $this->redirect(array('campaign/createUpdate', 'id' => $Campaign->id, 'step' => 3, '#' => 'groups'));
             } else {
                 $errors = '';
                 foreach ($NewGroup->errors as $error) {
                     $errors .= $error[0] . ' ';
                 }
                 Yii::app()->user->setFlash('group-danger', $errors);
             }
         }
     }
     if ($_POST['Campaign']) {
         $Campaign->setAttributes($_POST['Campaign']);
         if ($Campaign->save()) {
             if (!(int) $_GET['id']) {
                 // new. Add default 50% groups
                 foreach (array('A', 'B') as $letter) {
                     $CampaignGroup = new CampaignGroup();
                     $CampaignGroup->campaign_id = $Campaign->id;
                     $CampaignGroup->name = $letter;
                     $CampaignGroup->fraction = 50;
                     $CampaignGroup->save();
                 }
             }
             Yii::app()->user->setFlash('campaign-success', 'Campaign ' . ((int) $_GET['id'] ? 'updated' : 'created') . ' successfully');
             //if it's a new record then go to step 2, otherwise go to step 1
             if ($Campaign->isNewRecord) {
                 $this->redirect(array('campaign/createUpdate', 'id' => $Campaign->id, 'step' => 2, '#' => 'outcomes'));
             } else {
                 $this->redirect(array('campaign/createUpdate', 'id' => $Campaign->id, 'step' => 1));
             }
         }
     }
     // check group totals
     $totalPercentage = 0;
     foreach ($Campaign->groups as $CampaignGroup) {
         $totalPercentage += $CampaignGroup->fraction;
     }
     if ((int) $totalPercentage !== 100) {
         Yii::app()->user->setFlash('group-danger', 'The percentage splits of campaign groups must total 100. Currently group percentage splits total ' . $totalPercentage . '.');
     }
     // check for subjectless groups
     $SubjectlessCampaignGroups = $Campaign->groups(array('condition' => 'isnull(`groups`.`subject`) || LENGTH(`groups`.`subject`) < 1'));
     if (!$Campaign->groups) {
         $MissingTemplates = true;
     } else {
         $MissingTemplates = false;
     }
     // check for templateless groups
     foreach ($Campaign->groups as $Group) {
         if (!$Group->email_template) {
             $MissingTemplates = true;
             break;
         }
     }
     $Queries = Query::model()->findAll(array('condition' => 'invite = 0', 'index' => 'id', 'order' => 'name ASC'));
     if (isset($Queries[$Campaign->query_id])) {
         $Query = null;
         if (!$Campaign->isNewRecord) {
             if ($Campaign->size > 0) {
                 // we have to faff here.
                 // we need to add a limit to an existing query as we can't pass it through the run params.
                 // so we decode, add it and then encode again so when it's run it has the right limit
                 $JSON = json_decode($Queries[$Campaign->query_id]->JSON);
                 $JSON->limit = $Campaign->size;
                 $Queries[$Campaign->query_id]->JSON = json_encode($JSON);
             }
             $Query = $Queries[$Campaign->query_id]->run();
         }
     }
     $this->pageTitle = ((int) $_GET['id'] ? 'Update' : 'Create') . ' Campaign | ' . Yii::app()->name;
     $this->breadcrumbs = array('Campaigns' => array('index'), ((int) $_GET['id'] ? 'Update' : 'Create') . ' Campaign');
     $Campaign->refresh();
     $this->render('createUpdate', array('Campaign' => $Campaign, 'CampaignFile' => $CampaignFile, 'Queries' => $Queries, 'Query' => $Query, 'NewOutcome' => $NewOutcome, 'NewGroup' => $NewGroup, 'step' => $step, 'totalPercentage' => $totalPercentage, 'SubjectlessCampaignGroups' => $SubjectlessCampaignGroups, 'MissingTemplates' => $MissingTemplates));
 }
Example #3
0
 public function actionView($template_id, $group_id, $campaign_id)
 {
     $this->layout = '/layouts/vanilla';
     $fromEmailDomain = Yii::app()->params['insiderEmailDomain'];
     $testEmailInvalid = false;
     $EmailTemplate = EmailTemplate::model()->findByPK($template_id);
     if (is_null($EmailTemplate)) {
         throw new CHttpException(404, 'Unable to find template.');
     }
     $CampaignGroup = CampaignGroup::model()->with(array('campaign'))->findByPK($group_id);
     if (is_null($CampaignGroup)) {
         throw new CHttpException(404, 'Unable to find campaign group.');
     }
     if (isset($_POST['delete'])) {
         $path = dirname(Yii::app()->request->scriptFile) . '/templates/' . $EmailTemplate->folder;
         $this->deleteDir($path);
         $EmailTemplate->delete();
         $this->redirect(array('emailTemplate/create', 'campaign_id' => $campaign_id, 'group_id' => $group_id));
     }
     //send a test email
     if (isset($_POST['EmailTemplate']['email_test_recipient']) && $_POST['EmailTemplate']['email_test_recipient']) {
         $insiderEmailDomain = 'abc123.mailgun.org';
         $mailgunApiVerify = new MailgunCampaign($fromEmailDomain, Yii::app()->params['mailgun']['public-key']);
         try {
             $emails = explode(',', $_POST['EmailTemplate']['email_test_recipient']);
             foreach ($emails as $key => $email) {
                 $emails[$key] = trim($email);
                 $validEmailResponse = $mailgunApiVerify->validateEmailAddress($emails[$key]);
                 if ($validEmailResponse['is_valid'] != true) {
                     unset($emails[$key]);
                 }
             }
             if (sizeof($emails)) {
                 // send it
                 foreach ($emails as $key => $email) {
                     $mailgunApi = new MailgunCampaign($fromEmailDomain, Yii::app()->params['mailgun']['key']);
                     $message = $mailgunApi->newMessage();
                     $message->setFrom('email@' . $fromEmailDomain, 'Application Name');
                     $message->addTo($email);
                     $html = $EmailTemplate->exampleEmail;
                     $html = preg_replace("@%recipient.warehouse_id%@", '000', $html);
                     foreach ($CampaignGroup->campaign->outcomes as $Outcome) {
                         if (strlen($Outcome->url)) {
                             $html = preg_replace("@%recipient.outcome_" . $Outcome->id . "%@", $Outcome->url, $html);
                         }
                     }
                     $message->setHtml($html);
                     $message->setSubject($CampaignGroup->subject);
                     $message->send();
                 }
                 Yii::app()->user->setFlash('success', "Emails sent to " . implode(', ', $emails) . '.');
             } else {
                 Yii::app()->user->setFlash('danger', 'No valid email addresses found to send to');
             }
             $this->refresh();
         } catch (Exception $e) {
             var_dump($e);
             exit;
             $testEmailInvalid = true;
         }
     }
     //check template for first_name
     if (stripos($EmailTemplate->html, '%recipient.first_name%') === false) {
         $EmailTemplate->addNotice('Are you aware that the template.html file does not have a recipient first name tag?: "%recipient.first_name%');
     }
     //check template for last_name
     if (stripos($EmailTemplate->html, '%recipient.last_name%') === false) {
         $EmailTemplate->addNotice('Are you aware that the template.html file does not have a recipient last name tag?: "%recipient.last_name%');
     }
     //check we have an email subject
     if (!strlen($CampaignGroup->subject)) {
         $EmailTemplate->addNotice('You have not set an email subject for this group.');
     }
     //check for outcomes in the template
     preg_match_all("/%recipient.outcome_(\\d+)%/", $EmailTemplate->html, $outcomes);
     if (!sizeof($outcomes[0])) {
         $EmailTemplate->addNotice('Are you aware there are no outcome tags in the template?');
     } else {
         foreach ($outcomes[0] as $tag) {
             $EmailTemplate->addNotice('We found the following tag in your template: ' . $tag);
         }
     }
     $this->breadcrumbs = array('Campaigns' => array('campaign/index'), $CampaignGroup->campaign->name => array('campaign/createUpdate', 'id' => $CampaignGroup->campaign_id), 'Group ' . $CampaignGroup->name => array('campaignGroup/update', 'id' => $CampaignGroup->id, 'campaign_id' => $CampaignGroup->campaign_id), 'View Email Template');
     $this->render('view', array('EmailTemplate' => $EmailTemplate, 'subject' => $CampaignGroup->subject, 'has_ran' => $CampaignGroup->campaign->hasBeenRun, 'testEmailInvalid' => $testEmailInvalid));
 }