Added newsletter sign up and confirm functionality (as required) and templates
This commit is contained in:
parent
d10e859d1b
commit
acb5d1abae
15
_config.php
15
_config.php
|
@ -1,8 +1,11 @@
|
|||
<?php
|
||||
/*
|
||||
* Created on 9/07/2007
|
||||
*
|
||||
* To change the template for this generated file go to
|
||||
* Window - Preferences - PHPeclipse - PHP - Code Templates
|
||||
*/
|
||||
|
||||
// Add the extension to the Member class.
|
||||
Object::add_extension('Member', 'BlogRole');
|
||||
|
||||
// Director rule for ability to visit mysite.com/confirm/signup/123
|
||||
Director::addRules(50, array(
|
||||
'confirm-subscription/$Action/$ID' => 'ConfirmNewsletterSignup'
|
||||
));
|
||||
|
||||
?>
|
||||
|
|
|
@ -217,6 +217,10 @@ class BlogHolder_Controller extends Page_Controller {
|
|||
return $output;
|
||||
}
|
||||
|
||||
function NewsletterSignupForm() {
|
||||
return new NewsletterSignupForm($this, 'NewsletterSignupForm');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the rss fee for this blog holder's entries
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
class BlogRole extends DataObjectDecorator {
|
||||
|
||||
function extraDBFields() {
|
||||
return array(
|
||||
'db' => array(
|
||||
'Hash' => 'Varchar(32)'
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
class ConfirmNewsletterSignup extends Controller {
|
||||
|
||||
/**
|
||||
* Action for signing up a member to a given group in NewsletterSignupForm.
|
||||
* Used as mysite.com/confirm-subscription/member/123 (where 123 is a md5 hash to find the member)
|
||||
*/
|
||||
function member() {
|
||||
// Create an empty string for messages to be passed back to the template.
|
||||
$content = '';
|
||||
|
||||
// Check if the ID params exist, and ensure safe for SQL.
|
||||
if(!$hash = Convert::raw2sql(Director::urlParam('ID'))) {
|
||||
$content = "<p><strong>Error:</strong> No member identification was given.</p>";
|
||||
} else {
|
||||
// Check if a member exists with the hash given from ID param.
|
||||
if(!$member = DataObject::get_one('Member', "Hash = '$hash'")) {
|
||||
$content = "<p><strong>Error:</strong> Member does not exist by given parameters.</p>";
|
||||
} else {
|
||||
// Check if a group was passed in and exists.
|
||||
if($groupCode = NewsletterSignupForm::get_group_code()) {
|
||||
// Check if the member is in this group.
|
||||
if($group = DataObject::get_one('Group', "Code = '$groupCode'")) {
|
||||
if($member->inGroup($group->ID)) {
|
||||
$content = "<p><strong>$member->Email</strong> is already signed up.</p>";
|
||||
} else {
|
||||
// Member is not in the group, so add the member to the group.
|
||||
$member->Groups()->add($group);
|
||||
|
||||
// Send an email welcoming the member.
|
||||
$email = new ConfirmNewsletterSignup_Email($member);
|
||||
$email->to = $member->Email;
|
||||
$email->from = Email::getAdminEmail();
|
||||
$email->subject = 'Welcome to the mailing list';
|
||||
$email->populateTemplate(array(
|
||||
'Member' => $member
|
||||
));
|
||||
$email->send();
|
||||
|
||||
$content = "<p><strong>$member->Email</strong> has been signed up successfully. A welcome email has been sent.</p>";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Render these variables into the template. Pass in the message from previous logic.
|
||||
$this->customise(array(
|
||||
'Content' => $content
|
||||
));
|
||||
|
||||
// Render with a chosen template.
|
||||
return $this->renderWith(array(
|
||||
'ConfirmNewsletterSignup_member',
|
||||
'Page'
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ConfirmNewsletterSignup_Email extends Email_Template {
|
||||
|
||||
protected $ss_template = 'ConfirmNewsletterSignup_Email';
|
||||
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,99 @@
|
|||
<?php
|
||||
|
||||
class NewsletterSignupForm extends Form {
|
||||
|
||||
/**
|
||||
* Code of a group.
|
||||
*/
|
||||
protected static $groupCode;
|
||||
|
||||
/**
|
||||
* Set a group for sign up. Must be a code value of an instance of Group.
|
||||
* Members who sign up will be added to this group.
|
||||
*/
|
||||
public static function set_group_code($code) {
|
||||
self::$groupCode = $code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the group used for sign ups.
|
||||
*/
|
||||
public static function get_group_code() {
|
||||
return self::$groupCode;
|
||||
}
|
||||
|
||||
function __construct($controller, $name) {
|
||||
|
||||
$fields = new FieldSet(
|
||||
new TextField('FirstName', 'Your first name'),
|
||||
new TextField('Surname', 'Your surname'),
|
||||
new EmailField('Email', 'Your email')
|
||||
);
|
||||
|
||||
$validator = new RequiredFields(array(
|
||||
'FirstName',
|
||||
'Email'
|
||||
)
|
||||
);
|
||||
|
||||
$actions = new FieldSet(
|
||||
new FormAction('subscribe', 'Subscribe')
|
||||
);
|
||||
|
||||
parent::__construct($controller, $name, $fields, $actions, $validator);
|
||||
}
|
||||
|
||||
function subscribe($data, $form) {
|
||||
$SQL_email = $data['Email'];
|
||||
|
||||
// Check if there is a current member of given email in data. Check if in group.
|
||||
if($member = DataObject::get_one('Member', "`Member`.`Email` = '$SQL_email'")) {
|
||||
if($groupCode = self::get_group_code()) {
|
||||
if($group = DataObject::get_one('Group', "Code = '$groupCode'")) {
|
||||
if($member->inGroup($group->ID)) {
|
||||
$form->sessionMessage('You are already subscribed.', 'warning');
|
||||
Director::redirectBack();
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Create a new member, as this is a new subscriber.
|
||||
$member = new Member();
|
||||
}
|
||||
|
||||
// Save the data into the member.
|
||||
$form->saveInto($member);
|
||||
|
||||
// Hash the email of the subscriber and microtime, write the member.
|
||||
$member->Hash = md5(microtime() . $member->Email);
|
||||
$member->write();
|
||||
|
||||
// Create an array with data to populate in the email.
|
||||
$populateArray = array();
|
||||
$populateArray['Member'] = $member;
|
||||
// If there is a group, populate a confirm link.
|
||||
if(self::get_group_code()) {
|
||||
$populateArray['ConfirmLink'] = Director::absoluteBaseURL() . 'confirm-subscription/member/' . $member->Hash;
|
||||
}
|
||||
|
||||
// Send off a confirmation email to the subscriber.
|
||||
$email = new NewsletterSignupForm_Email();
|
||||
$email->to = $member->Email;
|
||||
$email->from = Email::getAdminEmail();
|
||||
$email->subject = 'Thank you for subscribing';
|
||||
$email->populateTemplate($populateArray);
|
||||
$email->send();
|
||||
|
||||
// Display message, and redirect back.
|
||||
$form->sessionMessage('You have been sent an email to confirm your subscription.', 'good');
|
||||
Director::redirectBack();
|
||||
}
|
||||
}
|
||||
|
||||
class NewsletterSignupForm_Email extends Email_Template {
|
||||
|
||||
protected $ss_template = 'NewsletterSignupForm_Email';
|
||||
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,14 @@
|
|||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
|
||||
"http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>$Subject</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>$Subject</h1>
|
||||
|
||||
<p>Hello $Member.FirstName,</p>
|
||||
|
||||
<p>Welcome to the mailing list.</p>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,19 @@
|
|||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
|
||||
"http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>$Subject</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>$Subject</h1>
|
||||
|
||||
<p>Hello $Member.FirstName,</p>
|
||||
|
||||
<p>This email confirms you signed up to the site.</p>
|
||||
|
||||
<% if ConfirmLink %>
|
||||
<p>Please visit this link to confirm you would like to sign up:</p>
|
||||
<p><a href="$ConfirmLink" title="Confirm newsletter subscription">$ConfirmLink</a></p>
|
||||
<% end_if %>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue