Skip to content

Commit

Permalink
fixing up the subscription process with subscribe and confirm double …
Browse files Browse the repository at this point in the history
…opt in emails
  • Loading branch information
dogmatic69 committed Jan 20, 2013
1 parent c1393d5 commit 8e205d3
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 44 deletions.
42 changes: 0 additions & 42 deletions Core/Newsletter/Controller/Component/EmailerComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@
* the named args array into the Controller::params member array and does
* not place the named args in the key 'named'.
*
* @code
* $this->Emailer->sendDirectMail(
* array('someone@example.com'),
* array(
* 'subject' => 'this is a test email',
* 'body' => 'The body of the email',
* 'template' => 'User - Registration'
* )
* );
*
* @author dogmatic
*/

Expand Down Expand Up @@ -91,36 +81,4 @@ public function settings() {
$this->trackViews = Configure::read('Newsletter.track_views');
}

public function sendDirectMail($userDetails, $email = array()) {
$this->trackViews = false;
if (empty($userDetails)) {
return false;
}

$email = array_merge(array(
'body' => '',
'template' => ''
), $email);

try {
$template = ClassRegistry::init('Newsletter.Template')->getTemplate($email['template']);
} catch(Exception $e) {
$this->Controller->notice(
$e->getMessage(),
array('redirect' => true)
);
}

$html = $template['Template']['header'] . $email['body'] . $template['Template']['footer'];

$Email = new InfinitasEmail();
$Email->sendMail(array(
'to' => $userDetails,
'from' => $email['from'],
'subject' => $email['subject'],
'html' => $html,
'text' => strip_tags(str_replace(array('<br/>', '<br>', '</p><p>'), "\n\r", $html))
));
}

}
72 changes: 70 additions & 2 deletions Core/Newsletter/Controller/NewsletterSubscribersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
*/

class NewsletterSubscribersController extends NewsletterAppController {

/**
* BeforeFilter callback
*/
public function beforeFilter() {
parent::beforeFilter();

Expand All @@ -29,7 +33,6 @@ public function beforeFilter() {

$this->notice['subscribed'] = array(
'redirect' => true,
'level' => 'success',
'message' => __d('newsletter', 'Your subscription has been saved, please check your email')
);

Expand All @@ -38,8 +41,24 @@ public function beforeFilter() {
'level' => 'warning',
'message' => __d('newsletter', 'There was a problem saving your subscription')
);

$this->notice['subscription_activated'] = array(
'redirect' => '/',
'message' => __d('newsletter', 'Your subscription has been confirmed')
);

$this->notice['subscription_not_activated'] = array(
'redirect' => '/',
'level' => 'warning',
'message' => __d('newsletter', 'There was a problem activating your subscription')
);
}

/**
* Subscribe to newsletters
*
* @return void
*/
public function subscribe() {
if (!$this->request->is('post')) {
$this->notice(__d('newsletter', 'No direct access allowed'), array(
Expand All @@ -60,13 +79,62 @@ public function subscribe() {
}

if ($this->{$this->modelClass}->subscribe($this->request->data)) {
// send out confirmation email
$this->request->data[$this->modelClass]['confirm_url'] = InfinitasRouter::url(array(
'action' => 'confirm',
$this->{$this->modelClass}->createTicket($this->request->data[$this->modelClass]['email'])
));
$this->Event->trigger('systemEmail', array(
'email' => array(
'newsletter' => 'newsletter-confirm-subscription',
'email' => $this->request->data[$this->modelClass]['email'],
'name' => $this->request->data[$this->modelClass]['prefered_name'],
),
'var' => array(
'Newsletter' => $this->request->data[$this->modelClass],
'User' => array(
'email' => $this->request->data[$this->modelClass]['email'],
'name' => $this->request->data[$this->modelClass]['prefered_name'],
)
)
));
return $this->notice('subscribed');
}

return $this->notice('not_subscribed');
}

/**
* Confirm a subscription
*
* @param string $hash the hash key for the subscription
*
* @return void
*/
public function confirm($hash = null) {
$subscription = $this->{$this->modelClass}->saveActivation($hash);
if ($subscription) {
$subscription[$this->modelClass]['name'] = $subscription[$this->modelClass]['prefered_name'];
$this->Event->trigger('adminEmail', array(
'email' => array('newsletter' => 'newsletter-new-subscriber-admin'),
'var' => array('Subscriber' => $subscription[$this->modelClass])
));

$this->Event->trigger('systemEmail', array(
'email' => array(
'email' => $subscription[$this->modelClass]['email'],
'name' => $subscription[$this->modelClass]['prefered_name'],
'newsletter' => 'newsletter-new-subscriber'
),
'var' => array(
'User' => $subscription[$this->modelClass]
)
));
return $this->notice('subscription_activated');
}

$this->notice('subscription_not_activated');
}

/**
* List all subscribers
*
Expand Down
34 changes: 34 additions & 0 deletions Core/Newsletter/Model/NewsletterSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ class NewsletterSubscriber extends NewsletterAppModel {
'paginated' => true
);

/**
* Behaviors to attach
*
* @var array
*/
public $actsAs = array(
'Libs.Ticketable'
);

/**
* BelongsTo relations
*
Expand Down Expand Up @@ -155,6 +164,31 @@ public function subscribe(array $data) {
return $this->save($data);
}

/**
* Method for activating a subscription
*
* This gets the ticket and checks everything is valid before setting the user to active.
*
* @param array $hash the data to save
*
* @return array
*/
public function saveActivation($hash) {
$subscription = $this->find('first', array(
'conditions' => array(
$this->alias . '.email' => $this->getTicket($hash)
)
));
if (empty($subscription)) {
return false;
}

$subscription[$this->alias]['confirmed'] = date('Y-m-d H:i:s');
$subscription[$this->alias]['active'] = 1;

return $this->save($subscription[$this->alias]);
}

public function updateUserDetails(array $user) {
return true;
}
Expand Down

0 comments on commit 8e205d3

Please sign in to comment.