mirror of
https://github.com/silverstripe/silverstripe-blog
synced 2024-10-22 11:05:58 +02:00
API CHANGE Removed Newsletter references in blog module. This shouldn't be in here because it's easy enough to create one on a per-project basis
This commit is contained in:
parent
9c004b468c
commit
6256c6d871
@ -1,13 +1,5 @@
|
||||
<?php
|
||||
|
||||
// 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'
|
||||
));
|
||||
|
||||
if(method_exists('LeftAndMain', 'require_javascript')) {
|
||||
LeftAndMain::require_javascript('blog/javascript/bbcodehelp.js');
|
||||
}
|
||||
|
@ -140,15 +140,6 @@ class BlogEntry extends Page {
|
||||
return $this->getParent()->Link('post')."/".$this->ID."/";
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the NewsletterSignupForm from the parent (BlogHolder).
|
||||
*/
|
||||
function NewsletterSignupForm() {
|
||||
if(isset($this->Parent->ID) && $this->Parent->NewsletterSignupForm()) {
|
||||
return $this->Parent->NewsletterSignupForm();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Call this to enable WYSIWYG editing on your blog entries.
|
||||
* By default the blog uses BBCode
|
||||
|
@ -17,8 +17,7 @@ class BlogHolder extends Page {
|
||||
);
|
||||
|
||||
static $has_one = array(
|
||||
"SideBar" => "WidgetArea",
|
||||
'Newsletter' => 'NewsletterType'
|
||||
"SideBar" => "WidgetArea"
|
||||
);
|
||||
|
||||
static $allowed_children = array(
|
||||
@ -50,22 +49,9 @@ class BlogHolder extends Page {
|
||||
"12 MONTH" => "Last year's entries",
|
||||
)));
|
||||
|
||||
// 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
|
||||
*/
|
||||
@ -116,16 +102,6 @@ class BlogHolder extends Page {
|
||||
return isset($_GET['tag']) ? $_GET['tag'] : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new instance of NewsletterSignupForm.
|
||||
* If there is no related Newsletter, then don't show it.
|
||||
*/
|
||||
function NewsletterSignupForm() {
|
||||
if($this->Newsletter() && $this->Newsletter()->ID) {
|
||||
return new NewsletterSignupForm($this, 'NewsletterSignupForm');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A simple form for creating blog entries
|
||||
*/
|
||||
|
@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
class BlogRole extends DataObjectDecorator {
|
||||
|
||||
/**
|
||||
* Extend the member table with some extra fields.
|
||||
*/
|
||||
function extraDBFields() {
|
||||
return array(
|
||||
'db' => array(
|
||||
'Hash' => 'Varchar(32)',
|
||||
'GroupCode' => 'Varchar(255)'
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* These fields are used for the newsletter subscription form.
|
||||
*/
|
||||
function subscribeFields() {
|
||||
return new FieldSet(
|
||||
new TextField('FirstName', 'Your first name'),
|
||||
new TextField('Surname', 'Your surname'),
|
||||
new EmailField('Email', 'Your email')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* These are the required fields for the newsletter subscription form.
|
||||
*/
|
||||
function subscribeRequiredFields() {
|
||||
return new RequiredFields(array(
|
||||
'FirstName',
|
||||
'Email'
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@ -1,79 +0,0 @@
|
||||
<?php
|
||||
|
||||
class ConfirmNewsletterSignup extends Controller {
|
||||
|
||||
/**
|
||||
* Add theme CSS requirements to make this look controller look a bit prettier.
|
||||
* If these aren't here, the page looks like a mess to users.
|
||||
*/
|
||||
function init() {
|
||||
parent::init();
|
||||
Requirements::themedCSS('layout');
|
||||
Requirements::themedCSS('typography');
|
||||
Requirements::themedCSS('form');
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = $member->GroupCode) {
|
||||
// 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();
|
||||
$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';
|
||||
|
||||
}
|
||||
|
||||
?>
|
@ -1,107 +0,0 @@
|
||||
<?php
|
||||
|
||||
class NewsletterSignupForm extends Form {
|
||||
|
||||
/**
|
||||
* Get the group code of the newsletter associated with the
|
||||
* BlogHolder instance that this form was created from.
|
||||
*/
|
||||
function get_group_code() {
|
||||
if($controller = $this->controller) {
|
||||
if($controller instanceof BlogHolder) {
|
||||
if($controller->Newsletter()) {
|
||||
return $controller->Newsletter()->Group()->Code;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the NewsletterSignupForm.
|
||||
* Take the fields and required fields from the extension role.
|
||||
*/
|
||||
function __construct($controller, $name) {
|
||||
$member = singleton('Member');
|
||||
|
||||
$fields = $member->subscribeFields();
|
||||
|
||||
$validator = $member->subscribeRequiredFields();
|
||||
|
||||
$actions = new FieldSet(
|
||||
new FormAction('subscribe', 'Subscribe')
|
||||
);
|
||||
|
||||
parent::__construct($controller, $name, $fields, $actions, $validator);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
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 = $this->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();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
} 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);
|
||||
|
||||
// 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($this->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';
|
||||
|
||||
}
|
||||
|
||||
?>
|
@ -1,8 +0,0 @@
|
||||
<div id="Content">
|
||||
<div class="typography">
|
||||
<h2>Confirm newsletter signup</h2>
|
||||
$Content
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
@ -1,14 +0,0 @@
|
||||
<!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>
|
@ -1,19 +0,0 @@
|
||||
<!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
Block a user