Newsletter changes for blogholders

This commit is contained in:
Sean Harvey 2007-12-15 22:47:20 +00:00
parent acb5d1abae
commit 2c6edcf0df
4 changed files with 43 additions and 26 deletions

View File

@ -16,7 +16,8 @@ class BlogHolder extends Page {
);
static $has_one = array(
"SideBar" => "WidgetArea"
"SideBar" => "WidgetArea",
'Newsletter' => 'NewsletterType'
);
static $allowed_children = array(
@ -27,10 +28,23 @@ class BlogHolder extends Page {
$fields = parent::getCMSFields();
$fields->removeFieldFromTab("Root.Content.Main","Content");
$fields->addFieldToTab("Root.Content.Widgets", new WidgetAreaEditor("SideBar"));
// Add a dropdown to display all newsletter types.
if($groups = $this->getNewsletters()) {
$groupsMap = $groups->toDropdownMap('ID', 'Title');
$fields->addFieldToTab('Root.Content.Main', new DropdownField('NewsletterID', 'Subscription newsletter type', $groupsMap, '', '', '(Select one)'));
}
return $fields;
}
/**
* Get all newsletter type instances.
*/
function getNewsletters() {
return DataObject::get('NewsletterType');
}
/**
* The DataObject of blog entries
*/

View File

@ -5,7 +5,8 @@ class BlogRole extends DataObjectDecorator {
function extraDBFields() {
return array(
'db' => array(
'Hash' => 'Varchar(32)'
'Hash' => 'Varchar(32)',
'GroupCode' => 'Varchar(255)'
),
);
}

View File

@ -19,7 +19,7 @@ class ConfirmNewsletterSignup extends Controller {
$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()) {
if($groupCode = $member->GroupCode) {
// Check if the member is in this group.
if($group = DataObject::get_one('Group', "Code = '$groupCode'")) {
if($member->inGroup($group->ID)) {
@ -29,7 +29,7 @@ class ConfirmNewsletterSignup extends Controller {
$member->Groups()->add($group);
// Send an email welcoming the member.
$email = new ConfirmNewsletterSignup_Email($member);
$email = new ConfirmNewsletterSignup_Email();
$email->to = $member->Email;
$email->from = Email::getAdminEmail();
$email->subject = 'Welcome to the mailing list';

View File

@ -3,25 +3,20 @@
class NewsletterSignupForm extends Form {
/**
* Code of a group.
* Gets a NewsletterType which is associated with the BlogHolder, the
* controller of this sign up form. It then uses the relationship getter
* to find the Group of a NewsletterType.
*/
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;
function get_group_code() {
if($controller = $this->controller) {
if($controller->ClassName == 'BlogHolder') {
if($controller->Newsletter()) {
return $controller->Newsletter()->Group()->Code;
}
}
}
}
/**
* Get the group used for sign ups.
*/
public static function get_group_code() {
return self::$groupCode;
}
function __construct($controller, $name) {
$fields = new FieldSet(
@ -31,10 +26,9 @@ class NewsletterSignupForm extends Form {
);
$validator = new RequiredFields(array(
'FirstName',
'Email'
)
);
'FirstName',
'Email'
));
$actions = new FieldSet(
new FormAction('subscribe', 'Subscribe')
@ -48,7 +42,7 @@ class NewsletterSignupForm extends Form {
// 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($groupCode = $this->get_group_code()) {
if($group = DataObject::get_one('Group', "Code = '$groupCode'")) {
if($member->inGroup($group->ID)) {
$form->sessionMessage('You are already subscribed.', 'warning');
@ -66,13 +60,21 @@ class NewsletterSignupForm extends Form {
// Hash the email of the subscriber and microtime, write the member.
$member->Hash = md5(microtime() . $member->Email);
// 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.
$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()) {
if($this->get_group_code()) {
$populateArray['ConfirmLink'] = Director::absoluteBaseURL() . 'confirm-subscription/member/' . $member->Hash;
}