6
6
*
7
7
* @author Matt Peel <[email protected] >
8
8
*/
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
+ );
22
23
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 ();
33
35
34
- $ parentClass = $ this ->owner ->BaseClass ;
35
- $ parentID = $ this ->owner ->ParentID ;
36
+ $ parentClass = $ this ->owner ->BaseClass ;
37
+ $ parentID = $ this ->owner ->ParentID ;
36
38
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 );
43
45
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
+ ));
52
54
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 ();
56
58
57
- foreach ($ comments as $ c ) {
58
- $ author = $ c ->Author ();
59
+ foreach ($ comments as $ c ) {
60
+ $ author = $ c ->Author ();
59
61
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
+ }
66
68
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 );
75
77
76
- $ email ->send ();
77
- }
78
- }
79
- }
80
- }
81
- }
82
- }
83
- }
78
+ $ email ->send ();
79
+ }
80
+ }
81
+ }
82
+ }
83
+ }
84
+ }
85
+ }
84
86
85
- private function shouldSendUserNotificationEmails () {
86
- $ changedFields = $ this ->owner ->getChangedFields ();
87
+ private function shouldSendUserNotificationEmails ()
88
+ {
89
+ $ changedFields = $ this ->owner ->getChangedFields ();
87
90
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
+ }
0 commit comments