Source of file CommentNotifier.php
Size: 3,063 Bytes - Last Modified: 2021-12-23T10:28:56+00:00
/var/www/docs.ssmods.com/process/src/src/Extensions/CommentNotifier.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 | <?php namespace SilverStripe\CommentNotifications\Extensions; use SilverStripe\Comments\Model\Comment; use SilverStripe\Security\Member; use SilverStripe\Control\Email\Email; use SilverStripe\Core\Extension; /** * Extension applied to CommentingController to invoke notifications * * Relies on the parent object to {@see Comment} having the {@see CommentNotifiable} extension applied.. */ class CommentNotifier extends Extension { /** * Notify Members of the post there is a new comment. * * @param Comment $comment */ public function onAfterPostComment(Comment $comment) { $parent = $comment->Parent(); if (!$parent || !$parent->hasMethod('notificationRecipients')) { return; } // Ask parent to submit all recipients $recipients = $parent->notificationRecipients($comment); foreach ($recipients as $recipient) { $this->notifyCommentRecipient($comment, $parent, $recipient); } } /** * Send comment notification to a given recipient * * @param Comment $comment * @param DataObject $parent Object with the {@see CommentNotifiable} extension applied * @param Member|string $recipient Either a member object or an email address to which notifications should be sent */ public function notifyCommentRecipient($comment, $parent, $recipient) { $subject = $parent->notificationSubject($comment, $recipient); $sender = $parent->notificationSender($comment, $recipient); $template = $parent->notificationTemplate($comment, $recipient); // Validate email // Important in case of the owner being a default-admin or a username with no contact email $to = ($recipient instanceof Member) ? $recipient->Email : $recipient; if (!Email::is_valid_address($to)) { return; } // Prepare the email $email = Email::create(); $email->setSubject($subject); $email->setFrom($sender); $email->setTo($to); $email->setHTMLTemplate($template); if ($recipient instanceof Member) { $email->setData([ 'Parent' => $parent, 'Comment' => $comment, 'Recipient' => $recipient, 'ApproveLink' => $comment->ApproveLink($recipient), 'HamLink' => $comment->HamLink($recipient), 'SpamLink' => $comment->SpamLink($recipient), 'DeleteLink' => $comment->DeleteLink($recipient), ]); } else { $email->setData([ 'Parent' => $parent, 'Comment' => $comment, 'ApproveLink' => false, 'SpamLink' => false, 'DeleteLink' => false, 'HamLink' => false, 'Recipient' => $recipient ]); } $this->owner->invokeWithExtensions('updateCommentNotification', $email, $comment, $recipient); return $email->send(); } } |