Skip to content

Commit

Permalink
fixing up the code for sending direct mails
Browse files Browse the repository at this point in the history
  • Loading branch information
dogmatic69 committed Jan 18, 2013
1 parent b293882 commit cf08f80
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 34 deletions.
4 changes: 1 addition & 3 deletions Core/Newsletter/Controller/Component/EmailerComponent.php
Expand Up @@ -104,9 +104,7 @@ public function sendDirectMail($userDetails, $email = array()) {

try {
$template = ClassRegistry::init('Newsletter.Template')->getTemplate($email['template']);
}

catch(Exception $e) {
} catch(Exception $e) {
$this->Controller->notice(
$e->getMessage(),
array('redirect' => true)
Expand Down
21 changes: 9 additions & 12 deletions Core/Newsletter/Controller/NewsletterCampaignsController.php
Expand Up @@ -144,19 +144,16 @@ public function __massActionDelete($ids) {
* @return array
*/
private function __canDelete($ids) {
$newsletters = $this->{$this->modelClass}->Newsletter->find(
'list',
array(
'fields' => array(
'Newsletter.campaign_id',
'Newsletter.campaign_id'
),
'conditions' => array(
'Newsletter.sent' => 1,
'Newsletter.campaign_id' => $ids
)
$newsletters = $this->{$this->modelClass}->Newsletter->find('list', array(
'fields' => array(
'Newsletter.newsletter_campaign_id',
'Newsletter.newsletter_campaign_id'
),
'conditions' => array(
'Newsletter.sent' => 1,
'Newsletter.newsletter_campaign_id' => $ids
)
);
));

if (empty($newsletters)) {
return $ids;
Expand Down
12 changes: 6 additions & 6 deletions Core/Newsletter/Controller/NewsletterTemplatesController.php
Expand Up @@ -189,11 +189,11 @@ public function __massActionDelete($ids) {
private function __canDelete($ids) {
$newsletters = $this->{$this->modelClass}->Newsletter->find('list', array(
'fields' => array(
'Newsletter.template_id',
'Newsletter.template_id'
'Newsletter.newsletter_template_id',
'Newsletter.newsletter_template_id'
),
'conditions' => array(
'Newsletter.template_id' => $ids
'Newsletter.newsletter_template_id' => $ids
)
));

Expand All @@ -214,11 +214,11 @@ private function __canDelete($ids) {

$campaigns = $this->{$this->modelClass}->NewsletterCampaign->find('list', array(
'fields' => array(
'NewsletterCampaign.template_id',
'NewsletterCampaign.template_id'
'NewsletterCampaign.newsletter_template_id',
'NewsletterCampaign.newsletter_template_id'
),
'conditions' => array(
'NewsletterCampaign.template_id' => $ids
'NewsletterCampaign.newsletter_template_id' => $ids
)
));

Expand Down
4 changes: 2 additions & 2 deletions Core/Newsletter/Controller/NewslettersController.php
Expand Up @@ -251,8 +251,8 @@ public function admin_index() {
*/
public function admin_add() {
if ($this->request->isPost()) {
$this->request->data[$this->modelClass]['template_id'] = $this->{$this->modelClass}->NewsletterCampaign->field('template_id', array(
'Campaign.id' => $this->request->data[$this->modelClass]['campaign_id']
$this->request->data[$this->modelClass]['newsletter_template_id'] = $this->{$this->modelClass}->NewsletterCampaign->field('newsletter_template_id', array(
'NewsletterCampaign.id' => $this->request->data[$this->modelClass]['campaign_id']
));
}

Expand Down
60 changes: 60 additions & 0 deletions Core/Newsletter/Lib/NewsletterEvents.php
Expand Up @@ -101,4 +101,64 @@ public function onUserProfile(Event $Event, array $user) {
public function onUserRegistration(Event $Event, array $user) {
return ClassRegistry::init('Newsletter.NewsletterSubscriber')->updateUserDetails($user);
}

/**
* Event for sending out system mails (directly, no queue)
*
* This should be limited to things like register and forgot password that requires the email being
* sent right away. For general newsletters you should use queues.
*
* $email should contain to arrays:
*
* - email: The details of the email being sent
* - var: Any view variables that should be made available to the template
*
* Email requires an email address `email` and the newsletter to render `newsletter`. Optional is the
* `name` of the user.
*
* Var differs per mail, but sometimes there is nothing required here
*
* @param Event $Event
* @param array $email
*
* @return boolean
*
* @throws InvalidArgumentException
*/
public function onSystemEmail(Event $Event, array $email) {
if (empty($email['email'])) {
throw new InvalidArgumentException(__d('newsletter', 'Missing email config'));
}
$email['email'] = array_merge(array(
'email' => null,
'name' => null,
'newsletter' => null
), $email['email']);
if (empty($email['var'])) {
$email['var'] = array();
}

$Newsletter = ClassRegistry::init('Newsletter.Newsletter');
$mail = $Newsletter->find('email', $email['email']['newsletter']);

$Email = new InfinitasEmail();
$sent = $Email->sendMail(array(
'to' => $email['email']['email'],
'from' => $mail['Newsletter']['from'],
'subject' => $mail['Newsletter']['subject'],
'html' => $mail['NewsletterTemplate']['header'] . $mail['Newsletter']['html'] . $mail['NewsletterTemplate']['footer'],
'text' => strip_tags(str_replace(array('<br/>', '<br>', '</p><p>'), "\n\r", implode("\n", array(
$mail['NewsletterTemplate']['header'],
$mail['Newsletter']['text'],
$mail['NewsletterTemplate']['footer']
)))),
'viewVars' => $email['var']
));

if (!$sent) {
return false;
}

return true;
}
}
90 changes: 79 additions & 11 deletions Core/Newsletter/Model/Newsletter.php
Expand Up @@ -48,7 +48,8 @@ class Newsletter extends NewsletterAppModel {
'toSend' => true,
'paginated' => true,
'preview' => true,
'deleteable' => true
'deleteable' => true,
'email' => true
);

/**
Expand Down Expand Up @@ -81,7 +82,7 @@ public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);

$this->order = array(
$this->alias . '.' . $this->displayField => 'asc'
$this->alias . '.slug' => 'asc'
);

$this->validate = array(
Expand All @@ -103,7 +104,7 @@ public function __construct($id = false, $table = null, $ds = null) {
'message' => __d('newsletter', 'Please enter the from address')
),
'email' => array(
'rule' => array( 'email', true ),
'rule' => array('email', true),
'message' => __d('newsletter', 'Please enter a valid email addres')
)
),
Expand Down Expand Up @@ -138,32 +139,49 @@ public function __construct($id = false, $table = null, $ds = null) {
);
}

/**
* General find
*
* @param string $state
* @param array $query
* @param array $results
*
* @return array
*/
protected function _findPaginated($state, array $query, array $results = array()) {
if ($state == 'before') {
$query['fields'] = array_merge((array)$query['fields'], array(
$this->alias . '.' . $this->primaryKey,
$this->alias . '.slug',
$this->alias . '.newsletter_campaign_id',
$this->alias . '.from',
$this->alias . '.reply_to',
$this->alias . '.subject',
$this->alias . '.active',
$this->alias . '.sent',
$this->alias . '.created',
));

$query['contain'] = array_merge((array)$query['fields'], array(
'NewsletterCampaign' => array(
'fields' => array(
'NewsletterCampaign.name'
)
)
$this->NewsletterCampaign->alias . '.' . $this->NewsletterCampaign->displayField
));

$query['joins'] = (array)$query['joins'];
$query['joins'][] = $this->autoJoinModel($this->NewsletterCampaign);

return $query;
}

return $results;
}

/**
* Find only active newsletters
*
* @param string $state
* @param array $query
* @param array $results
*
* @return array
*/
protected function _findActive($state, array $query, array $results = array()) {
if ($state == 'before') {
$query['fields'] = array_merge((array)$query['fields'], array(
Expand Down Expand Up @@ -196,11 +214,23 @@ protected function _findActive($state, array $query, array $results = array()) {
return $results;
}

/**
* Find preview data
*
* @param string $state
* @param array $query
* @param array $results
*
* @return array
*
* @throws InvalidArgumentException
*/
protected function _findPreview($state, array $query, array $results = array()) {
if ($state == 'before') {
if (empty($query[0])) {
throw new InvalidArgumentException(__d('newsletter', 'Invalid newsletter selected'));
}

$query['fields'] = array_merge((array)$query['fields'], array(
$this->alias . '.' . $this->primaryKey,
$this->alias . '.html',
Expand All @@ -218,6 +248,7 @@ protected function _findPreview($state, array $query, array $results = array())
$query['limit'] = 1;
return $query;
}

if (empty($results[0])) {
return array();
}
Expand All @@ -228,6 +259,8 @@ protected function _findPreview($state, array $query, array $results = array())
/**
* Get newsletters that can be deleted safely
*
* This method takes list of `ids` and returns a list of ids that can be safely removed.
*
* @param string $state
* @param array $query
* @param array $results
Expand All @@ -236,7 +269,7 @@ protected function _findPreview($state, array $query, array $results = array())
*
* @throws InvalidArgumentException
*/
protected function _findDeletable($state, array $query, array $results = array()) {
protected function _findDeleteable($state, array $query, array $results = array()) {
if ($state == 'before') {
if (empty($query['ids'])) {
throw new InvalidArgumentException(__d('newsletter', 'No newsletters selected'));
Expand All @@ -262,6 +295,41 @@ protected function _findDeletable($state, array $query, array $results = array()
return $results;
}

protected function _findEmail($state, array $query, array $results = array()) {
if ($state == 'before') {
if (empty($query[0])) {
throw new CakeException(__d('newsletter', 'No email selected'));
}

$query['fields'] = array_merge((array)$query['fields'], array(
$this->alias . '.' . $this->primaryKey,
$this->alias . '.from',
$this->alias . '.reply_to',
$this->alias . '.subject',
$this->alias . '.html',
$this->alias . '.text',

$this->NewsletterTemplate->alias . '.header',
$this->NewsletterTemplate->alias . '.footer',
));

$query['conditions'] = array_merge((array)$query['conditions'], array(
$this->alias . '.slug' => $query[0]
));

$query['joins'] = (array)$query['joins'];
$query['joins'][] = $this->autoJoinModel($this->NewsletterTemplate);

$query['limit'] = 1;
return $query;
}
if (empty($results[0])) {
throw new CakeException(__d('newsletter', 'No email found'));
}

return $results[0];
}

/**
* toggle the send status
*
Expand Down
1 change: 1 addition & 0 deletions Core/Newsletter/View/Newsletters/admin_form.ctp
Expand Up @@ -32,6 +32,7 @@ echo $this->Form->create('Newsletter');
'class' => 'span9'
)),
$this->Html->tag('div', implode('', array(
$this->Form->input('slug'),
$this->Form->input('campaign_id', array(
'empty' => Configure::read('Website.empty_select')
)),
Expand Down
9 changes: 9 additions & 0 deletions Core/Newsletter/View/Newsletters/admin_index.ctp
Expand Up @@ -35,6 +35,7 @@
$this->Form->checkbox('all') => array(
'class' => 'first'
),
$this->Paginator->sort('slug', __d('newsletter', 'Alias')),
$this->Paginator->sort('subject'),
$this->Paginator->sort('NewsletterCampaign.name', __d('newsletter', 'Campaign')) => array(
'class' => 'larger'
Expand All @@ -54,6 +55,14 @@
?>
<tr>
<td><?php echo $this->Infinitas->massActionCheckBox($newsletter); ?>&nbsp;</td>
<td>
<?php
echo $this->Html->link($newsletter['Newsletter']['slug'], array(
'action' => 'edit',
$newsletter['Newsletter']['id']
));
?>&nbsp;
</td>
<td><?php echo $newsletter['Newsletter']['subject']; ?>&nbsp;</td>
<td>
<?php
Expand Down

0 comments on commit cf08f80

Please sign in to comment.