|
1
|
|
|
<?php |
|
2
|
|
|
namespace SilverStripe\Forum\Models; |
|
3
|
|
|
|
|
4
|
|
|
use SilverStripe\Control\Controller; |
|
5
|
|
|
use SilverStripe\Control\Director; |
|
6
|
|
|
use SilverStripe\Control\Email\Email; |
|
7
|
|
|
use SilverStripe\Core\Config\Config; |
|
8
|
|
|
use SilverStripe\Core\Convert; |
|
9
|
|
|
use SilverStripe\ORM\DataObject; |
|
10
|
|
|
use SilverStripe\ORM\DB; |
|
11
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
|
12
|
|
|
use SilverStripe\Security\Member; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Forum Thread Subscription: Allows members to subscribe to this thread |
|
16
|
|
|
* and receive email notifications when these topics are replied to. |
|
17
|
|
|
* |
|
18
|
|
|
* @package forum |
|
19
|
|
|
* @property DBDatetime LastSent |
|
20
|
|
|
* @method ForumThread Thread |
|
21
|
|
|
* @method Member Member |
|
22
|
|
|
*/ |
|
23
|
|
|
class ForumThreadSubscription extends DataObject |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var array */ |
|
26
|
|
|
private static $db = array( |
|
27
|
|
|
"LastSent" => "Datetime" |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
/** @var array */ |
|
31
|
|
|
private static $has_one = array( |
|
32
|
|
|
"Thread" => ForumThread::class, |
|
33
|
|
|
"Member" => Member::class |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Checks to see if a Member is already subscribed to this thread |
|
38
|
|
|
* |
|
39
|
|
|
* @param int $threadID The ID of the thread to check |
|
40
|
|
|
* @param int $memberID The ID of the currently logged in member (Defaults to Member::currentUserID()) |
|
41
|
|
|
* |
|
42
|
|
|
* @return bool true if they are subscribed, false if they're not |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function alreadySubscribed($threadID, $memberID = null) |
|
45
|
|
|
{ |
|
46
|
|
|
if (!$memberID) { |
|
|
|
|
|
|
47
|
|
|
$memberID = Member::currentUserID(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$threadID = Convert::raw2sql($threadID); |
|
51
|
|
|
$memberID = Convert::raw2sql($memberID); |
|
52
|
|
|
|
|
53
|
|
|
if ($threadID == '' || $memberID == '') { |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return (bool)self::get()->filter( |
|
58
|
|
|
[ |
|
59
|
|
|
'ThreadID' => $threadID, |
|
60
|
|
|
'MemberID' => $memberID |
|
61
|
|
|
] |
|
62
|
|
|
)->count(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Notifies everybody that has subscribed to this topic that a new post has been added. |
|
67
|
|
|
* To get emailed, people subscribed to this topic must have visited the forum |
|
68
|
|
|
* since the last time they received an email |
|
69
|
|
|
* |
|
70
|
|
|
* @param Post $post The post that has just been added |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function notify(Post $post) |
|
73
|
|
|
{ |
|
74
|
|
|
$list = self::get()->filter( |
|
75
|
|
|
[ |
|
76
|
|
|
'ThreadID' => $post->ThreadID, |
|
77
|
|
|
'MemberID' => $post->AuthorID |
|
78
|
|
|
] |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
if (!$list) { |
|
82
|
|
|
return; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
foreach ($list as $obj) { |
|
86
|
|
|
$id = Convert::raw2sql((int)$obj->MemberID); |
|
87
|
|
|
|
|
88
|
|
|
// Get the members details |
|
89
|
|
|
$member = Member::get()->byID($id); |
|
90
|
|
|
$adminEmail = Config::inst()->get(Email::class, 'admin_email'); |
|
91
|
|
|
|
|
92
|
|
|
if ($member) { |
|
93
|
|
|
Email::create() |
|
94
|
|
|
->setFrom($adminEmail) |
|
95
|
|
|
->setTo($member->Email) |
|
96
|
|
|
->setSubject(_t('Post.NEWREPLY', 'New reply for {title}', array('title' => $post->Title))) |
|
|
|
|
|
|
97
|
|
|
->setHTMLTemplate('ForumMember_TopicNotification') |
|
98
|
|
|
->setData($member) |
|
99
|
|
|
->setData($post) |
|
100
|
|
|
->setData( |
|
101
|
|
|
[ |
|
102
|
|
|
'UnsubscribeLink' => Controller::join_links(Director::absoluteBaseURL(), $post->Thread()->Forum()->Link(), 'unsubscribe', $post->ID) |
|
103
|
|
|
] |
|
104
|
|
|
) |
|
105
|
|
|
->send(); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: