Move to sane directory structure. Don't make 'cms' the top level of the silverstripe...
[silverstripe-elijah.git] / silverstripe-gsoc / cms / code / Newsletter / Newsletter.php
blobbcb47faa43b3785875edcfb9c455980e51e09bce
1 <?php
3 class Newsletter extends DataObject {
5 /**
6 * Returns a FieldSet with which to create the CMS editing form.
7 * You can use the extend() method of FieldSet to create customised forms for your other
8 * data objects.
9 */
10 function getCMSFields($controller = null) {
11 require_once("forms/Form.php");
13 $group = DataObject::get_by_id("Group", $this->Parent()->GroupID);
14 $sent_status_report = $this->renderWith("Newsletter_SentStatusReport");
15 $ret = new FieldSet(
16 new TabSet("Root",
17 $mailTab = new Tab("Newsletter",
18 new TextField("Subject", "Subject", $this->Subject),
19 new HtmlEditorField("Content", "Content")
21 $sentToTab = new Tab("Sent Status Report",
22 new LiteralField("Sent Status Report", $sent_status_report)
27 if( $this->Status != 'Draft' ) {
28 $mailTab->push( new ReadonlyField("SendDate", "Sent at", $this->SendDate) );
32 return $ret;
35 /**
36 * Returns a DataObject listing the recipients for the given status for this newsletter
38 * @param string $result 3 possible values: "Sent", (mail() returned TRUE), "Failed" (mail() returned FALSE), or "Bounced" ({@see $email_bouncehandler}).
40 function SentRecipients($result) {
41 $SQL_result = Convert::raw2sql($result);
42 return DataObject::get("Newsletter_SentRecipient",array("ParentID='".$this->ID."'", "Result='".$SQL_result."'"));
45 /**
46 * Returns a DataObjectSet containing the subscribers who have never been sent this Newsletter
49 function UnsentSubscribers() {
50 // Get a list of everyone who has been sent this newsletter
51 $sent_recipients = DataObject::get("Newsletter_SentRecipient","ParentID='".$this->ID."'");
52 // If this Newsletter has not been sent to anyone yet, $sent_recipients will be null
53 if ($sent_recipients != null) {
54 $sent_recipients_array = $sent_recipients->toNestedArray('MemberID');
55 } else {
56 $sent_recipients_array = array();
59 // Get a list of all the subscribers to this newsletter
60 $subscribers = DataObject::get( 'Member', "`GroupID`='".$this->Parent()->GroupID."'", null, "INNER JOIN `Group_Members` ON `MemberID`=`Member`.`ID`" );
61 // If this Newsletter has no subscribers, $subscribers will be null
62 if ($subscribers != null) {
63 $subscribers_array = $subscribers->toNestedArray();
64 } else {
65 $subscribers_array = array();
68 // Get list of subscribers who have not been sent this newsletter:
69 $unsent_subscribers_array = array_diff_key($subscribers_array, $sent_recipients_array);
71 // Create new data object set containing the subscribers who have not been sent this newsletter:
72 $unsent_subscribers = new DataObjectSet();
73 foreach($unsent_subscribers_array as $key => $data) {
74 $unsent_subscribers->push(new ArrayData($data));
77 return $unsent_subscribers;
80 function getTitle() {
81 return $this->getField('Subject');
84 function getNewsletterType() {
85 return DataObject::get_by_id('NewsletterType', $this->ParentID);
88 static $db = array(
89 "Status" => "Enum('Draft, Send', 'Draft')",
90 "Content" => "HTMLText",
91 "Subject" => "Varchar(255)",
92 "SentDate" => "Datetime",
96 static $has_one = array(
97 "Parent" => "NewsletterType",
100 static $has_many = array(
101 "Recipients" => "Newsletter_Recipient",
102 "SentRecipients" => "Newsletter_SentRecipient",
105 static function newDraft( $parentID, $subject, $content ) {
106 if( is_numeric( $parentID ) ) {
107 $newsletter = new Newsletter();
108 $newsletter->Status = 'Draft';
109 $newsletter->Title = $newsletter->Subject = $subject;
110 $newsletter->ParentID = $parentID;
111 $newsletter->Content = $content;
112 $newsletter->write();
113 } else {
114 user_error( $parentID, E_USER_ERROR );
117 return $newsletter;
121 class Newsletter_SentRecipient extends DataObject {
123 * The DB schema for Newsletter_SentRecipient.
125 * ParentID is the the Newsletter
126 * Email and MemberID keep track of the recpients information
127 * Result has 4 possible values: "Sent", (mail() returned TRUE), "Failed" (mail() returned FALSE),
128 * "Bounced" ({@see $email_bouncehandler}), or "BlackListed" (sending to is disabled).
130 static $db = array(
131 "ParentID" => "Int",
132 "Email" => "Varchar(255)",
133 "Result" => "Enum('Sent, Failed, Bounced, BlackListed', 'Sent')",
135 static $has_one = array(
136 "Member" => "Member",
139 class Newsletter_Recipient extends DataObject {
140 static $db = array(
141 "ParentID" => "Int",
143 static $has_one = array(
144 "Member" => "Member",
148 class Newsletter_Email extends Email_Template {
149 protected $nlType;
151 function __construct($nlType) {
152 $this->nlType = $nlType;
153 parent::__construct();
156 function setTemplate( $template ) {
157 $this->ss_template = $template;
160 function UnsubscribeLink(){
161 $emailAddr = $this->To();
162 $nlTypeID = $this->nlType->ID;
163 return Director::absoluteBaseURL()."unsubscribe/$emailAddr/$nlTypeID";