private function edit(MailshotType $model)
 {
     if (isset($_POST['MailshotType']) && $_POST['MailshotType']) {
         $model->attributes = $_POST['MailshotType'];
         if ($model->save()) {
             $this->redirect(['mailshotType/update', 'name' => $model->name]);
         }
     }
     $this->render('edit', ['model' => $model]);
 }
 /**
  * This got messy
  *
  * @param $instructionId
  * @throws CHttpException
  */
 public function actionSendMailshot($instructionId)
 {
     $model = new Client('search');
     if (isset($_POST['send']) && $_POST['send']) {
         $instruction = Deal::model()->findByPk($instructionId);
         $type = MailshotType::model()->findByPk($_POST['MailshotType']);
         $model->attributes = $_GET['Client'];
         $dataProvider = $model->search();
         $dataProvider->setPagination(['pageSize' => 100]);
         if (!$type) {
             throw new CHttpException(404, 'Mailshot Type [name: ' . $_POST['MailshotType'] . '] was not found');
         }
         if (!$instruction) {
             throw new CHttpException(404, 'Instruction [id: ' . $instructionId . '] was not found');
         }
         $mailshot = new MandrillMailshot();
         $mailshot->instructionId = $instruction->dea_id;
         $mailshot->type = $type->name;
         $mailshot->save();
         if ($type->templatePath && file_exists($type->templatePath)) {
             ob_start();
             include $type->templatePath;
             $htmlTemplate = ob_get_clean();
         } else {
             $htmlTemplate = $this->execTemplate($type->htmlTemplate);
         }
         $textTemplate = $this->execTemplate($type->textTemplate);
         $mandrillMessagePreset = new MandrillMessage();
         $mandrillMessagePreset->setFrom(Yii::app()->params['mailshot']['sender_email'], Yii::app()->params['mailshot']['sender_name']);
         $mandrillMessagePreset->setHtmlBody($htmlTemplate);
         $mandrillMessagePreset->setTextBody($textTemplate);
         $mandrillMessagePreset->setSubject($type->subject);
         $mandrillMessagePreset->setPreserveRecepients(false);
         $mandrillMessagePreset->setGlobalMergeVar('MAILSHOT_ID', $mailshot->id);
         $mandrillMessagePreset->setGlobalMergeVar('INSTRUCTION_ID', $instruction->dea_id);
         $mandrillMessagePreset->setGlobalMergeVar('INSTRUCTION_PRICE', Locale::formatCurrency($instruction->dea_marketprice));
         $mandrillMessagePreset->setGlobalMergeVar('INSTRUCTION_TITLE', $instruction->title);
         $mandrillMessagePreset->setGlobalMergeVar('INSTRUCTION_STRAPLINE', $instruction->dea_strapline);
         $mandrillMessagePreset->setGlobalMergeVar('OFFICE_TITLE', $instruction->branch->office->title);
         $mandrillMessagePreset->setGlobalMergeVar('OFFICE_NUMBER', $instruction->branch->bra_tel);
         if (isset($_POST['test']) && $_POST['test']) {
             $mandrillMessagePreset->enableTest();
         }
         $iterator = new DataProviderIterator($dataProvider);
         /** @var $mandrillMessage MandrillMessage */
         $mandrillMessage = null;
         $x = 0;
         $staffEmails = array_fill_keys(Yii::app()->params['mailshot']['alwaysSendTo'], '?');
         if (Yii::app()->params['mandrill']['test_run']) {
             $testEmails = Yii::app()->params['mandrill']['test_emails'];
         }
         /** @var $client Client */
         foreach ($iterator as $client) {
             $email = $client->cli_email;
             if (isset($testEmails)) {
                 if (!$testEmails) {
                     break;
                 }
                 $email = array_shift($testEmails);
             }
             if (!$email) {
                 continue;
             }
             //we are sending emails to our staff in a different message
             if (array_key_exists($email, $staffEmails)) {
                 continue;
             }
             if ($x % Yii::app()->params['mandrill']['mails_in_message'] === 0) {
                 if ($mandrillMessage) {
                     $mandrillMessage->send();
                     $mailshot->addMessage($mandrillMessage);
                 }
                 $mandrillMessage = clone $mandrillMessagePreset;
             }
             $mandrillMessage->addTo($email, $client->getFullName());
             $mandrillMessage->setRecepientMergeVar($email, 'CLIENT_FULLNAME', $client->getFullName());
             $mandrillMessage->setRecepientMergeVar($email, 'CLIENT_SALUTATION', $client->cli_salutation);
             $mandrillMessage->setRecepientMergeVar($email, 'CLIENT_ID', $client->cli_id);
             $mandrillMessage->setRecepientMergeVar($email, 'CLIENT_EMAIL', $email);
             $mandrillMessage->setRecepientMetaData($email, 'clientId', $client->cli_id);
             $x++;
         }
         if (!$mandrillMessage->id) {
             $mandrillMessage->send();
             $mailshot->addMessage($mandrillMessage);
         }
         /**
          * doing a trick. will send an email to everyone in staff list to avoid complains
          */
         $staffMessage = clone $mandrillMessagePreset;
         $clients = Client::model()->findAll('cli_email IN (' . implode(', ', $staffEmails) . ')', array_keys($staffEmails));
         foreach ($clients as $client) {
             $email = $client->cli_email;
             $staffMessage->addTo($email, $client->getFullName());
             $staffMessage->setRecepientMergeVar($email, 'CLIENT_FULLNAME', $client->getFullName());
             $staffMessage->setRecepientMergeVar($email, 'CLIENT_SALUTATION', $client->cli_salutation);
             $staffMessage->setRecepientMergeVar($email, 'CLIENT_ID', $client->cli_id);
             $staffMessage->setRecepientMergeVar($email, 'CLIENT_EMAIL', $email);
             $staffMessage->setRecepientMetaData($email, 'clientId', $client->cli_id);
         }
         $staffMessage->send();
         $mailshot->addMessage($staffMessage);
         Yii::app()->user->setFlash('mailshot-sent', 'Mailshot successfully sent');
         $this->redirect(['instruction/summary', 'id' => $instructionId]);
     }
     $this->render('sendMailshot');
 }
Example #3
0
/**
 * @var $this ClientController
 */
$this->beginWidget('AdminForm', ['method' => 'post']);
?>
<div class="row-fluid">
	<div class="span12">
		<fieldset>
			<div class="block-header">Select mailshot type</div>
			<div class="content">
				<div class="control-group">
					<label class="control-label">Mailshot Type</label>

					<div class="controls">
						<?php 
echo CHtml::dropDownList('MailshotType', null, CHtml::listData(MailshotType::model()->findAll(), 'name', 'subject'));
?>
					</div>
				</div>

				<?php 
if (Yii::app()->user->is(UserRole::SUPER_ADMIN)) {
    ?>
					<div class="control-group">
						<label class="control-label">Run with test API key</label>

						<div class="controls">
							<?php 
    echo CHtml::checkBox('test', false, ['value' => 'test']);
    ?>
						</div>