Version: current

     1 <?php
     2 /**
     3  * The Notifications class allows you to create email notifications for various events.
     4  * It lets your scripts generate a number of notifications, and delay sending of the emails until
     5  * the end of execution, so that multiple notifications can collated together
     6  * @package cms
     7  */
     8 class Notifications extends Object {
     9     protected static $events = array();
    10
    11     /**
    12      * Raise an event that requires notification.
    13      * @param eventType A string used to identify different event types.  You can refer back to the events
    14      * raised by this eventType.
    15      * @param item An object related to the notification, such as a database record.
    16      * @param notifyMemberID A person to notify via email about the event.  Events won't be notified by
    17      * email until you call {@link notifyByEmail()}
    18      */
    19     static function event($eventType, $item, $notifyMemberID) {
    20         Notifications::$events[$eventType][$notifyMemberID][] = $item;
    21     }
    22
    23     /**
    24      * Notify the appropriate parties about all instances of this event, by email.
    25      * @param eventType A string, this should match the eventType passed to {@link event()}
    26      * @param emailTemplateClass The class-name of the email template to use.
    27      */
    28
    29     static function notifyByEmail($eventType, $emailTemplateClass) {
    30         $count = 0;
    31         if(class_exists($emailTemplateClass)) {
    32             foreach(Notifications::$events[$eventType] as $memberID => $items) {
    33                 if($memberID) {
    34                     $email = new $emailTemplateClass();
    35                     $email->populateTemplate(new ArrayData(array(
    36                         "Recipient" => DataObject::get_by_id("Member", $memberID),
    37                         "BrokenPages" => new DataObjectSet($items),
    38                     )));
    39                     $email->debug();
    40                     $email->send();
    41                     $count++;
    42                 }
    43             }
    44         }
    45         return $count;
    46     }
    47
    48     /**
    49      * Get all the items that were passed with this event type.
    50      * @param eventType A string, this should match the eventType passed to {@link event()}
    51      */
    52     static function getItems($eventType) {
    53         $allItems = array();
    54         if(isset(Notifications::$events[$eventType])) {
    55             foreach(Notifications::$events[$eventType] as $memberID => $items) {
    56                 $allItems = array_merge($allItems, (array)$items);
    57             }
    58         }
    59         return $allItems;
    60     }
    61
    62 }
    63
    64 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -