1
<?php
2

3
namespace SilverStripe\CommentNotifications\Extensions;
4

5
use SilverStripe\Comments\Model\Comment;
6
use SilverStripe\ORM\DataExtension;
7
use SilverStripe\Control\Email\Email;
8
use SilverStripe\Security\Member;
9
use Traversable;
10

11
class CommentNotifiable extends DataExtension
12
{
13

14
    /**
15
     * Default subject line if the owner doesn't override it
16
     *
17
     * @config
18
     * @var string
19
     */
20
    private static $default_notification_subject = 'A new comment has been posted';
21

22
    /**
23
     * Default sender
24
     *
25
     * @config
26
     * @var string
27
     */
28
    private static $default_notification_sender = 'noreply@{host}';
29

30
    /**
31
     * Default template to use for comment notifications
32
     *
33
     * @config
34
     * @var string
35
     */
36
    private static $default_notification_template = 'SilverStripe\\CommentNotifications\\CommentEmail';
37

38
    /**
39
     * Return the list of members or emails to send comment notifications to
40
     *
41
     * @param Comment $comment
42
     * @return array|Traversable
43
     */
44 2
    public function notificationRecipients($comment)
45
    {
46 2
        $list = [];
47

48 2
        if ($adminEmail = Email::config()->admin_email) {
49 2
            $list[] = $adminEmail;
50
        }
51

52 2
        $this->owner->invokeWithExtensions('updateNotificationRecipients', $list, $comment);
53

54 2
        return $list;
55
    }
56

57
    /**
58
     * Gets the email subject line for comment notifications
59
     *
60
     * @param Comment $comment Comment
61
     * @param Member|string $recipient
62
     * @return string
63
     */
64 2
    public function notificationSubject($comment, $recipient)
65
    {
66 2
        $subject = $this->owner->config()->default_notification_subject;
67

68 2
        $this->owner->invokeWithExtensions('updateNotificationSubject', $subject, $comment, $recipient);
69

70 2
        return $subject;
71
    }
72

73
    /**
74
     * Get the sender email address to use for email notifications
75
     *
76
     * @param Comment $comment
77
     * @param Member|string $recipient
78
     * @return string
79
     */
80 2
    public function notificationSender($comment, $recipient)
81
    {
82 2
        $sender = $this->owner->config()->default_notification_sender;
83

84
        // Do hostname substitution
85 2
        $host = isset($_SERVER['HTTP_HOST'])
86 2
            ? preg_replace('/^www\./i', '', $_SERVER['HTTP_HOST'])
87 2
            : 'localhost';
88 2
        $sender = preg_replace('/{host}/', $host, $sender);
89

90 2
        $this->owner->invokeWithExtensions('updateNotificationSender', $sender, $comment, $recipient);
91

92 2
        return $sender;
93
    }
94

95
    /**
96
     * Determine the template to use for this email
97
     *
98
     * @param Comment $comment
99
     * @param Member|string $recipient
100
     * @return string Template name (excluding .ss extension)
101
     */
102 2
    public function notificationTemplate($comment, $recipient)
103
    {
104 2
        $template = $this->owner->config()->default_notification_template;
105

106 2
        $this->owner->invokeWithExtensions('updateNotificationTemplate', $template, $comment, $recipient);
107

108 2
        return $template;
109
    }
110

111
    /**
112
     * Update the notification email
113
     *
114
     * @param Email $email
115
     * @param Comment $comment
116
     * @param Member|string $recipient
117
     */
118
    public function updateCommentNotification($email, $comment, $recipient)
119
    {
120
        //
121
    }
122
}

Read our documentation on viewing source code .

Loading