2007-12-14 01:54:53 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class NewsletterSignupForm extends Form {
|
|
|
|
|
|
|
|
/**
|
2007-12-20 05:32:01 +01:00
|
|
|
* Get the group code of the newsletter associated with the
|
|
|
|
* BlogHolder instance that this form was created from.
|
2007-12-14 01:54:53 +01:00
|
|
|
*/
|
2007-12-15 23:47:20 +01:00
|
|
|
function get_group_code() {
|
|
|
|
if($controller = $this->controller) {
|
2007-12-20 04:26:20 +01:00
|
|
|
if($controller instanceof BlogHolder) {
|
2007-12-15 23:47:20 +01:00
|
|
|
if($controller->Newsletter()) {
|
|
|
|
return $controller->Newsletter()->Group()->Code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-12-14 01:54:53 +01:00
|
|
|
}
|
|
|
|
|
2007-12-20 05:32:01 +01:00
|
|
|
/**
|
|
|
|
* Create the NewsletterSignupForm.
|
|
|
|
* Take the fields and required fields from the extension role.
|
|
|
|
*/
|
2007-12-14 01:54:53 +01:00
|
|
|
function __construct($controller, $name) {
|
2007-12-20 05:32:01 +01:00
|
|
|
$member = singleton('Member');
|
|
|
|
|
|
|
|
$fields = $member->subscribeFields();
|
|
|
|
|
|
|
|
$validator = $member->subscribeRequiredFields();
|
2007-12-14 01:54:53 +01:00
|
|
|
|
|
|
|
$actions = new FieldSet(
|
|
|
|
new FormAction('subscribe', 'Subscribe')
|
|
|
|
);
|
2007-12-20 05:32:01 +01:00
|
|
|
|
2007-12-14 01:54:53 +01:00
|
|
|
parent::__construct($controller, $name, $fields, $actions, $validator);
|
|
|
|
}
|
2007-12-20 05:32:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* NewsletterSignupForm action.
|
|
|
|
* Requires that the email address be submitted in the form.
|
|
|
|
*
|
|
|
|
* Checks if there is a member in the system by submitted email, and checks if
|
|
|
|
* that member has already signed up. If member has, then form gives message and
|
|
|
|
* redirects back. If not, then it carries on with the process.
|
|
|
|
*/
|
2007-12-14 01:54:53 +01:00
|
|
|
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'")) {
|
2007-12-15 23:47:20 +01:00
|
|
|
if($groupCode = $this->get_group_code()) {
|
2007-12-14 01:54:53 +01:00
|
|
|
if($group = DataObject::get_one('Group', "Code = '$groupCode'")) {
|
|
|
|
if($member->inGroup($group->ID)) {
|
|
|
|
$form->sessionMessage('You are already subscribed.', 'warning');
|
|
|
|
Director::redirectBack();
|
2007-12-18 03:42:52 +01:00
|
|
|
return false;
|
2007-12-14 01:54:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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);
|
2007-12-15 23:47:20 +01:00
|
|
|
|
|
|
|
// If there is a group code found, add it to a field on the member for
|
|
|
|
// later use (when a member confirms to be added).
|
|
|
|
if($groupCode = $this->get_group_code()) {
|
|
|
|
$member->GroupCode = $groupCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the member to the database.
|
2007-12-14 01:54:53 +01:00
|
|
|
$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.
|
2007-12-15 23:47:20 +01:00
|
|
|
if($this->get_group_code()) {
|
2007-12-14 01:54:53 +01:00
|
|
|
$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';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|