Version: current

      1 <?php
      2
      3 /**
      4  * Single newsletter instance.  Each Newsletter belongs to a NewsletterType.
      5  *
      6  * @package newsletter
      7  */
      8 class Newsletter extends DataObject {
      9
     10     static $db = array(
     11         "Status" => "Enum('Draft, Send', 'Draft')",
     12         "Content" => "HTMLText",
     13         "Subject" => "Varchar(255)",
     14         "SentDate" => "Datetime"
     15     );
     16
     17     static $has_one = array(
     18         "Parent" => "NewsletterType",
     19     );
     20
     21     static $has_many = array(
     22         "Recipients" => "Newsletter_Recipient",
     23         "SentRecipients" => "Newsletter_SentRecipient",
     24     );
     25
     26     /**
     27      * Returns a FieldSet with which to create the CMS editing form.
     28      * You can use the extend() method of FieldSet to create customised forms for your other
     29      * data objects.
     30      *
     31      * @param Controller
     32      * @return FieldSet
     33      */
     34     function getCMSFields($controller = null) {
     35         $group = DataObject::get_by_id("Group", $this->Parent()->GroupID);
     36         $sent_status_report = $this->renderWith("Newsletter_SentStatusReport");
     37         $previewLink = Director::absoluteBaseURL() . 'admin/newsletter/preview/' . $this->ID;
     38
     39         $ret = new FieldSet(
     40             new TabSet("Root",
     41                 $mailTab = new Tab('Newsletter', _t('Newsletter.NEWSLETTER', 'Newsletter'),
     42                     new TextField("Subject", _t('Newsletter.SUBJECT', 'Subject'), $this->Subject),
     43                     new HtmlEditorField("Content", _t('Newsletter.CONTENT', 'Content')),
     44                     new LiteralField('PreviewNewsletter', "<a href=\"$previewLink\" target=\"_blank\">" . _t('NewsletterAdmin.PREVIEWNEWSLETTER', 'Preview this newsletter') . "</a>")
     45                 ),
     46                 $sentToTab = new Tab('SentStatusReport', _t('Newsletter.SENTREPORT', 'Sent Status Report'),
     47                     new LiteralField("Sent Status Report", $sent_status_report)
     48                 )
     49             )
     50         );
     51
     52         if($this->Status != 'Draft') {
     53             $mailTab->push( new ReadonlyField("SendDate", _t('Newsletter.SENTAT', 'Sent at'), $this->SendDate) );
     54         }
     55
     56         $this->extend('updateCMSFields', $ret);
     57
     58         return $ret;
     59     }
     60
     61     /**
     62      * Returns a DataObject listing the recipients for the given status for this newsletter
     63      *
     64      * @param string $result 3 possible values: "Sent", (mail() returned TRUE), "Failed" (mail() returned FALSE), or "Bounced" ({@see $email_bouncehandler}).
     65      * @return DataObjectSet
     66      */
     67     function SentRecipients($result) {
     68         $SQL_result = Convert::raw2sql($result);
     69         return DataObject::get("Newsletter_SentRecipient",array("ParentID='".$this->ID."'", "Result='".$SQL_result."'"));
     70     }
     71
     72     /**
     73      * Returns a DataObjectSet containing the subscribers who have never been sent this Newsletter
     74      *
     75      * @return DataObjectSet
     76      */
     77     function UnsentSubscribers() {
     78         // Get a list of everyone who has been sent this newsletter
     79         $sent_recipients = DataObject::get("Newsletter_SentRecipient","ParentID='".$this->ID."'");
     80         // If this Newsletter has not been sent to anyone yet, $sent_recipients will be null
     81         if ($sent_recipients != null) {
     82             $sent_recipients_array = $sent_recipients->toNestedArray('MemberID');
     83         } else {
     84             $sent_recipients_array = array();
     85         }
     86
     87         // Get a list of all the subscribers to this newsletter
     88         if(defined('DB::USE_ANSI_SQL')) {
     89             $subscribers = DataObject::get( 'Member', "\"GroupID\"='".$this->Parent()->GroupID."'", null, "INNER JOIN \"Group_Members\" ON \"MemberID\"=\"Member\".\"ID\"" );
     90         } else {
     91             $subscribers = DataObject::get( 'Member', "`GroupID`='".$this->Parent()->GroupID."'", null, "INNER JOIN `Group_Members` ON `MemberID`=`Member`.`ID`" );
     92         }
     93         // If this Newsletter has no subscribers, $subscribers will be null
     94         if ($subscribers != null) {
     95             $subscribers_array = $subscribers->toNestedArray();
     96         } else {
     97             $subscribers_array = array();
     98         }
     99
    100         // Get list of subscribers who have not been sent this newsletter:
    101         $unsent_subscribers_array = array_diff_key($subscribers_array, $sent_recipients_array);
    102
    103         // Create new data object set containing the subscribers who have not been sent this newsletter:
    104         $unsent_subscribers = new DataObjectSet();
    105         foreach($unsent_subscribers_array as $key => $data) {
    106             $unsent_subscribers->push(new ArrayData($data));
    107         }
    108
    109         return $unsent_subscribers;
    110     }
    111
    112     function getTitle() {
    113         return $this->getField('Subject');
    114     }
    115
    116     function getNewsletterType() {
    117         return DataObject::get_by_id('NewsletterType', $this->ParentID);
    118     }
    119
    120     static function newDraft($parentID, $subject, $content) {
    121         if( is_numeric($parentID)) {
    122            $newsletter = new Newsletter();
    123             $newsletter->Status = 'Draft';
    124             $newsletter->Title = $newsletter->Subject = $subject;
    125             $newsletter->ParentID = $parentID;
    126             $newsletter->Content = $content;
    127             $newsletter->write();
    128         } else {
    129             user_error( $parentID, E_USER_ERROR );
    130         }
    131         return $newsletter;
    132     }
    133 }
    134
    135 /**
    136  * Database record for recipients that have had the newsletter sent to them.
    137  *
    138  * @package newsletter
    139  */
    140 class Newsletter_SentRecipient extends DataObject {
    141     /**
    142      * The DB schema for Newsletter_SentRecipient.
    143      *
    144      * ParentID is the the Newsletter
    145      * Email and MemberID keep track of the recpients information
    146      * Result has 4 possible values: "Sent", (mail() returned TRUE), "Failed" (mail() returned FALSE),
    147      *  "Bounced" ({@see $email_bouncehandler}), or "BlackListed" (sending to is disabled).
    148      */
    149     static $db = array(
    150         "ParentID" => "Int",
    151         "Email" => "Varchar(255)",
    152         "Result" => "Enum('Sent, Failed, Bounced, BlackListed', 'Sent')",
    153     );
    154     static $has_one = array(
    155         "Member" => "Member",
    156     );
    157 }
    158
    159 /**
    160  * Single recipient of the newsletter
    161  *
    162  * @package newsletter
    163  */
    164 class Newsletter_Recipient extends DataObject {
    165
    166     static $db = array(
    167         "ParentID" => "Int",
    168     );
    169     static $has_one = array(
    170         "Member" => "Member",
    171     );
    172 }
    173
    174 /**
    175  * Email object for sending newsletters.
    176  *
    177  * @package newsletter
    178  */
    179 class Newsletter_Email extends Email {
    180
    181     protected $nlType;
    182
    183     function __construct($nlType) {
    184         $this->nlType = $nlType;
    185         parent::__construct();
    186     }
    187
    188     function setTemplate( $template ) {
    189         $this->ss_template = $template;
    190     }
    191
    192     function UnsubscribeLink(){
    193         $emailAddr = $this->To();
    194         $nlTypeID = $this->nlType->ID;
    195         return Director::absoluteBaseURL() . "unsubscribe/index/$emailAddr/$nlTypeID";
    196     }
    197 }
    198 ?>

[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. -