Skip to content

Commit a368376

Browse files
author
helpfulrobot
committed
Converted to PSR-2
1 parent 1604d93 commit a368376

File tree

3 files changed

+246
-232
lines changed

3 files changed

+246
-232
lines changed

code/extensions/CommentUserNotificationsExtension.php

Lines changed: 91 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,102 +6,105 @@
66
*
77
* @author Matt Peel <[email protected]>
88
*/
9-
class CommentUserNotificationsExtension extends DataExtension {
10-
/**
11-
* Add a boolean to track which {@link Comment} objects (and therefore the {@link Member} that posted them) wantk
12-
*
13-
*
14-
*
15-
* to be notified when new comments are posted.
16-
*
17-
* @var array Additional database fields to add to the {@link Comment} class.
18-
*/
19-
private static $db = array(
20-
"NotifyOfUpdates" => "Boolean"
21-
);
9+
class CommentUserNotificationsExtension extends DataExtension
10+
{
11+
/**
12+
* Add a boolean to track which {@link Comment} objects (and therefore the {@link Member} that posted them) wantk
13+
*
14+
*
15+
*
16+
* to be notified when new comments are posted.
17+
*
18+
* @var array Additional database fields to add to the {@link Comment} class.
19+
*/
20+
private static $db = array(
21+
"NotifyOfUpdates" => "Boolean"
22+
);
2223

23-
/**
24-
* We hook into onAfterWrite() because we want to check this every time the comment is written - primarily because
25-
* of the test that we perform to ensure that the comment isn't currently moderated. Most sites will moderate
26-
* comments initially, and there's no point sending an email to a user if the comment is still awaiting moderation
27-
* (and therefore the user can't see it yet).
28-
*
29-
* @todo This will lead to multiple emails being sent if a comment is edited after being posted
30-
*/
31-
public function onAfterWrite() {
32-
parent::onAfterWrite();
24+
/**
25+
* We hook into onAfterWrite() because we want to check this every time the comment is written - primarily because
26+
* of the test that we perform to ensure that the comment isn't currently moderated. Most sites will moderate
27+
* comments initially, and there's no point sending an email to a user if the comment is still awaiting moderation
28+
* (and therefore the user can't see it yet).
29+
*
30+
* @todo This will lead to multiple emails being sent if a comment is edited after being posted
31+
*/
32+
public function onAfterWrite()
33+
{
34+
parent::onAfterWrite();
3335

34-
$parentClass = $this->owner->BaseClass;
35-
$parentID = $this->owner->ParentID;
36+
$parentClass = $this->owner->BaseClass;
37+
$parentID = $this->owner->ParentID;
3638

37-
// We only want to notify people if certain conditions are met:
38-
// - The comment has passed moderation (aka. if required, it has been approved by an admin)
39-
// - We are either seeing the Comment for the first time, or it has just passed moderation by an admin
40-
if($this->shouldSendUserNotificationEmails()) {
41-
if(ClassInfo::exists($parentClass)) {
42-
$commentParent = $parentClass::get()->byID($parentID);
39+
// We only want to notify people if certain conditions are met:
40+
// - The comment has passed moderation (aka. if required, it has been approved by an admin)
41+
// - We are either seeing the Comment for the first time, or it has just passed moderation by an admin
42+
if ($this->shouldSendUserNotificationEmails()) {
43+
if (ClassInfo::exists($parentClass)) {
44+
$commentParent = $parentClass::get()->byID($parentID);
4345

44-
// Get all comments attached to this page, which we have to do manually as the has_one relationship is
45-
// 'faked' by the Comment class (because it can be attached to multiple parent classes).
46-
if($commentParent) {
47-
$comments = Comment::get()->filter(array(
48-
'BaseClass' => $parentClass,
49-
'ParentID' => $parentID,
50-
'NotifyOfUpdates' => true
51-
));
46+
// Get all comments attached to this page, which we have to do manually as the has_one relationship is
47+
// 'faked' by the Comment class (because it can be attached to multiple parent classes).
48+
if ($commentParent) {
49+
$comments = Comment::get()->filter(array(
50+
'BaseClass' => $parentClass,
51+
'ParentID' => $parentID,
52+
'NotifyOfUpdates' => true
53+
));
5254

53-
// If we have comments, iterate over them to build a unique list of all email addresses to notify
54-
if($comments) {
55-
$emailList = array();
55+
// If we have comments, iterate over them to build a unique list of all email addresses to notify
56+
if ($comments) {
57+
$emailList = array();
5658

57-
foreach($comments as $c) {
58-
$author = $c->Author();
59+
foreach ($comments as $c) {
60+
$author = $c->Author();
5961

60-
if($author) {
61-
if(!in_array($author->Email, $emailList)) {
62-
$emailList[] = $author->Email;
63-
}
64-
}
65-
}
62+
if ($author) {
63+
if (!in_array($author->Email, $emailList)) {
64+
$emailList[] = $author->Email;
65+
}
66+
}
67+
}
6668

67-
// Send an email to everyone in the list
68-
if(sizeof($emailList) > 0) {
69-
foreach($emailList as $emailAddress) {
70-
$email = new Email();
71-
$email->setSubject('New Comment on "' . $commentParent->dbObject('Title')->XML() . '"');
72-
$email->setFrom(Email::getAdminEmail());
73-
$email->setTo($emailAddress);
74-
$email->populateTemplate($this->owner);
69+
// Send an email to everyone in the list
70+
if (sizeof($emailList) > 0) {
71+
foreach ($emailList as $emailAddress) {
72+
$email = new Email();
73+
$email->setSubject('New Comment on "' . $commentParent->dbObject('Title')->XML() . '"');
74+
$email->setFrom(Email::getAdminEmail());
75+
$email->setTo($emailAddress);
76+
$email->populateTemplate($this->owner);
7577

76-
$email->send();
77-
}
78-
}
79-
}
80-
}
81-
}
82-
}
83-
}
78+
$email->send();
79+
}
80+
}
81+
}
82+
}
83+
}
84+
}
85+
}
8486

85-
private function shouldSendUserNotificationEmails() {
86-
$changedFields = $this->owner->getChangedFields();
87+
private function shouldSendUserNotificationEmails()
88+
{
89+
$changedFields = $this->owner->getChangedFields();
8790

88-
return
89-
$changedFields &&
90-
(
91-
// New record, automatically moderated as moderation is not enabled for this site
92-
(
93-
isset($changedFields['ID']) &&
94-
isset($changedFields['Moderated']) &&
95-
$changedFields['ID']['before'] == 0 &&
96-
$changedFields['Moderated']['after'] === true
97-
)
98-
||
99-
// Existing record, moderation has just been set - meaning it has been approved by an admin
100-
(
101-
isset($changedFields['Moderated']) &&
102-
$changedFields['Moderated']['before'] == false &&
103-
$changedFields['Moderated']['after'] === true
104-
)
105-
);
106-
}
107-
}
91+
return
92+
$changedFields &&
93+
(
94+
// New record, automatically moderated as moderation is not enabled for this site
95+
(
96+
isset($changedFields['ID']) &&
97+
isset($changedFields['Moderated']) &&
98+
$changedFields['ID']['before'] == 0 &&
99+
$changedFields['Moderated']['after'] === true
100+
)
101+
||
102+
// Existing record, moderation has just been set - meaning it has been approved by an admin
103+
(
104+
isset($changedFields['Moderated']) &&
105+
$changedFields['Moderated']['before'] == false &&
106+
$changedFields['Moderated']['after'] === true
107+
)
108+
);
109+
}
110+
}

code/extensions/CommentingControllerUserNotificationsExtension.php

Lines changed: 78 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6,89 +6,92 @@
66
*
77
* @see Comment
88
*/
9-
class CommentingControllerUserNotificationsExtension extends Extension {
10-
public static $allowed_actions = array(
11-
'unsubscribenotification'
12-
);
9+
class CommentingControllerUserNotificationsExtension extends Extension
10+
{
11+
public static $allowed_actions = array(
12+
'unsubscribenotification'
13+
);
1314

14-
/**
15-
* Alter the comment form to add a checkbox to give users the ability to receive notifications when further
16-
* comments are posted. The {@link CommentingController::doPostComment()} method will take care of saving this
17-
* field for us, as it's part of the {@link Comment} DataObject
18-
*
19-
* @see CommentUserNotificationsExtension
20-
* @param Form $form The Form object used to render the comments form
21-
*/
22-
public function alterCommentForm(Form $form) {
23-
$form->Fields()->insertAfter(
24-
CheckboxField::create(
25-
'NotifyOfUpdates',
26-
_t('CommentInterface.NOTIFYOFUPDATES', 'Please notify me about new comments posted here.')
27-
),
28-
'Comment'
29-
);
30-
}
15+
/**
16+
* Alter the comment form to add a checkbox to give users the ability to receive notifications when further
17+
* comments are posted. The {@link CommentingController::doPostComment()} method will take care of saving this
18+
* field for us, as it's part of the {@link Comment} DataObject
19+
*
20+
* @see CommentUserNotificationsExtension
21+
* @param Form $form The Form object used to render the comments form
22+
*/
23+
public function alterCommentForm(Form $form)
24+
{
25+
$form->Fields()->insertAfter(
26+
CheckboxField::create(
27+
'NotifyOfUpdates',
28+
_t('CommentInterface.NOTIFYOFUPDATES', 'Please notify me about new comments posted here.')
29+
),
30+
'Comment'
31+
);
32+
}
3133

32-
/**
33-
* Uses $this->owner->request (a {@link SS_HTTPRequest} object) to determine which comment we want to unsubscribe
34-
* the member from. If the current user isn't logged in, or is logged in as a different user, then we send them to
35-
* the login screen.
36-
*/
37-
public function unsubscribenotification() {
38-
$request = $this->owner->getRequest();
34+
/**
35+
* Uses $this->owner->request (a {@link SS_HTTPRequest} object) to determine which comment we want to unsubscribe
36+
* the member from. If the current user isn't logged in, or is logged in as a different user, then we send them to
37+
* the login screen.
38+
*/
39+
public function unsubscribenotification()
40+
{
41+
$request = $this->owner->getRequest();
3942

40-
$commentID = $request->param('ID');
41-
$member = Member::currentUser();
43+
$commentID = $request->param('ID');
44+
$member = Member::currentUser();
4245

43-
if(!$commentID) {
44-
$this->owner->httpError(403);
45-
return;
46-
}
46+
if (!$commentID) {
47+
$this->owner->httpError(403);
48+
return;
49+
}
4750

48-
$comment = Comment::get()->byID($commentID);
51+
$comment = Comment::get()->byID($commentID);
4952

50-
if(!$comment) {
51-
$this->owner->httpError(403);
52-
return;
53-
}
53+
if (!$comment) {
54+
$this->owner->httpError(403);
55+
return;
56+
}
5457

55-
if(!$member || $member->ID != $comment->AuthorID) {
56-
return Security::permissionFailure(
57-
$this->owner,
58-
array(
59-
'default' => _t(
60-
'CommentingControllerUserNotificationsExtension.DEFAULTFAIL',
61-
'You must login to unsubscribe.'
62-
),
63-
'alreadyLoggedIn' => _t(
64-
'CommentingControllerUserNotificationsExtension.ALREADYLOGGEDINFAIL',
65-
'You must login as the correct user (the user who submitted the comment) to continue.'
66-
),
67-
'logInAgain' => _t(
68-
'CommentingControllerUserNotificationsExtension.LOGINAGAINFAIL',
69-
'You have been logged out. If you would like to login again, enter your credentials below.'
70-
)
71-
)
72-
);
73-
}
58+
if (!$member || $member->ID != $comment->AuthorID) {
59+
return Security::permissionFailure(
60+
$this->owner,
61+
array(
62+
'default' => _t(
63+
'CommentingControllerUserNotificationsExtension.DEFAULTFAIL',
64+
'You must login to unsubscribe.'
65+
),
66+
'alreadyLoggedIn' => _t(
67+
'CommentingControllerUserNotificationsExtension.ALREADYLOGGEDINFAIL',
68+
'You must login as the correct user (the user who submitted the comment) to continue.'
69+
),
70+
'logInAgain' => _t(
71+
'CommentingControllerUserNotificationsExtension.LOGINAGAINFAIL',
72+
'You have been logged out. If you would like to login again, enter your credentials below.'
73+
)
74+
)
75+
);
76+
}
7477

75-
// Currently logged in Member's ID matches the author of the comment, so we can unsubscribe them
76-
// We want to find all comments posted to this object by this author, and unsubscribe all of them.
77-
$allComments = Comment::get()->filter(array(
78-
'BaseClass' => $comment->BaseClass,
79-
'ParentID' => $comment->ParentID,
80-
'NotifyOfUpdates' => true
81-
));
78+
// Currently logged in Member's ID matches the author of the comment, so we can unsubscribe them
79+
// We want to find all comments posted to this object by this author, and unsubscribe all of them.
80+
$allComments = Comment::get()->filter(array(
81+
'BaseClass' => $comment->BaseClass,
82+
'ParentID' => $comment->ParentID,
83+
'NotifyOfUpdates' => true
84+
));
8285

83-
foreach($allComments as $c) {
84-
$c->NotifyOfUpdates = false;
85-
$c->write();
86-
}
86+
foreach ($allComments as $c) {
87+
$c->NotifyOfUpdates = false;
88+
$c->write();
89+
}
8790

88-
// This sets a session var that can be queried on the page that we redirect the user back to, so that we can
89-
// display a nice message to let the user know their unsubscription was successful.
90-
Session::set('CommentUserNotificationsUnsubscribed', '1');
91+
// This sets a session var that can be queried on the page that we redirect the user back to, so that we can
92+
// display a nice message to let the user know their unsubscription was successful.
93+
Session::set('CommentUserNotificationsUnsubscribed', '1');
9194

92-
$this->owner->redirectBack();
93-
}
94-
}
95+
$this->owner->redirectBack();
96+
}
97+
}

0 commit comments

Comments
 (0)