From 6c4a2e24aa2451a6f1234e38ffea5b728946b264 Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Tue, 28 Aug 2007 01:55:45 +0000 Subject: [PATCH] Refactored newsletter system to put recipient selection and form-fieldset generation into the NewsletterType class, to allow for subclassing NewsletterType to create special system newsletters. [see oriwave2 for an example] Removed a number of notice-level errors. Allowed for BOUNCE_EMAIL to not exist and bounce-handling to be disabled. git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@40963 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- code/Newsletter/NewsletterEmailProcess.php | 4 +- code/Newsletter/NewsletterType.php | 71 ++++++++++ code/NewsletterAdmin.php | 157 ++------------------- 3 files changed, 80 insertions(+), 152 deletions(-) diff --git a/code/Newsletter/NewsletterEmailProcess.php b/code/Newsletter/NewsletterEmailProcess.php index 40031361..7991e38c 100755 --- a/code/Newsletter/NewsletterEmailProcess.php +++ b/code/Newsletter/NewsletterEmailProcess.php @@ -17,9 +17,7 @@ class NewsletterEmailProcess extends BatchProcess { $this->nlType = $nlType; $this->messageID = $messageID; - $groupID = $nlType->GroupID; - - parent::__construct( DataObject::get( 'Member', "`GroupID`='$groupID'", null, "INNER JOIN `Group_Members` ON `MemberID`=`Member`.`ID`" ) ); + parent::__construct( $nlType->Recipients() ); } diff --git a/code/Newsletter/NewsletterType.php b/code/Newsletter/NewsletterType.php index 70ef1b35..be323b79 100755 --- a/code/Newsletter/NewsletterType.php +++ b/code/Newsletter/NewsletterType.php @@ -51,5 +51,76 @@ class NewsletterType extends DataObject { } parent::onBeforeWrite(); } + + /** + * Get the fieldset to display in the administration section + */ + function getCMSFields() { + $group = null; + if($this->GroupID) { + $group = DataObject::get_one("Group", "ID = $this->GroupID"); + } + + $fields = new FieldSet( + new TextField("Title", "Newsletter Type"), + new TextField("FromEmail", "Send newsletters from"), + new TabSet("Root", + new Tab("Drafts", + $draftList = new NewsletterList("Draft", $this, "Draft") + ), + new TabSet("Sent", + new Tab("Sent", + $sendList = new NewsletterList("Send", $this, "Send") + ), + new Tab("Unsubscribed", + $unsubscribedList = new UnsubscribedList("Unsubscribed", $this) + ), + new Tab("Bounced", + $bouncedList = new BouncedList("Bounced", $this ) + ) + ) + ) + ); + + if($this->GroupID) { + $fields->addFieldToTab('Root', + new TabSet("Recipients", + new Tab( "Recipients", + $recipients = new MemberTableField( + $this, + "Recipients", + $group + ) + ), + new Tab( "Import", + $importField = new RecipientImportField("ImportFile","Import from file", $group ) + ) + ) + ); + + $recipients->setController($this); + $importField->setController($this); + $importField->setTypeID( $this->ID ); + } + + $fields->addFieldToTab('Root', + new Tab("Template", + $templates = new TemplateList("Template","Template", $this->Template, NewsletterAdmin::template_path()) + ) + ); + + $draftList->setController($this); + $sendList->setController($this); + + $templates->setController($this); + $unsubscribedList->setController($this); + $bouncedList->setController($this); + + $fields->push($idField = new HiddenField("ID")); + $fields->push( new HiddenField( "executeForm", "", "TypeEditForm" ) ); + $idField->setValue($this->ID); + + return $fields; + } } ?> diff --git a/code/NewsletterAdmin.php b/code/NewsletterAdmin.php index d2e41e75..b0364d50 100755 --- a/code/NewsletterAdmin.php +++ b/code/NewsletterAdmin.php @@ -128,7 +128,7 @@ class NewsletterAdmin extends LeftAndMain { * Get the EditForm */ public function EditForm() { - if((isset($_REQUEST['ID']) && $_REQUEST['Type'] == 'Newsletter') || isset($_REQUEST['action_savenewsletter'])) { + if((isset($_REQUEST['ID']) && isset($_REQUEST['Type']) && $_REQUEST['Type'] == 'Newsletter') || isset($_REQUEST['action_savenewsletter'])) { return $this->NewsletterEditForm(); } else { return $this->TypeEditForm(); @@ -164,60 +164,9 @@ class NewsletterAdmin extends LeftAndMain { } } - if(isset($mailType) && is_object($mailType) && $mailType->GroupID) { - $group = DataObject::get_one("Group", "ID = $mailType->GroupID"); - } - //The function could be called from CMS with $mailType isset but with empty string. - if(isset($mailType)&&$mailType) { - $fields = new FieldSet( - new TextField("Title", "Newsletter Type"), - new TextField("FromEmail", "Send newsletters from"), - new TabSet("Root", - new Tab("Drafts", - $draftList = new NewsletterList("Draft", $mailType, "Draft") - ), - new TabSet("Sent", - new Tab("Sent", - $sendList = new NewsletterList("Send", $mailType, "Send") - ), - new Tab("Unsubscribed", - $unsubscribedList = new UnsubscribedList("Unsubscribed", $mailType) - ), - new Tab("Bounced", - $bouncedList = new BouncedList("Bounced", $mailType ) - ) - ), - new TabSet("Recipients", - new Tab( "Recipients", - $recipients = new MemberTableField( - $this, - "Recipients", - $group - ) - ), - new Tab( "Import", - $importField = new RecipientImportField("ImportFile","Import from file", $group ) - ) - ), - new Tab("Template", - $templates = new TemplateList("Template","Template", $mailType->Template, self::template_path()) - ) - ) - ); - - $draftList->setController($this); - $sendList->setController($this); - $recipients->setController($this); - $templates->setController($this); - $importField->setController($this); - $unsubscribedList->setController($this); - $bouncedList->setController($this); - - $importField->setTypeID( $id ); - - $fields->push($idField = new HiddenField("ID")); - $fields->push( new HiddenField( "executeForm", "", "TypeEditForm" ) ); - $idField->setValue($id); + if(isset($mailType) && $mailType) { + $fields = $mailType->getCMSFields(); + // $actions = new FieldSet(new FormAction('adddraft', 'Add Draft')); $actions = new FieldSet(new FormAction('save','Save')); @@ -234,57 +183,6 @@ class NewsletterAdmin extends LeftAndMain { return $form; } - /* - public function showmailinglist($params) { - return $this->showWithEditForm( $params, $this->getMailinglistEditForm( $params['ID'] ) ); - } - - public function getMailinglistEditForm($id) { - if(!is_numeric($id)) { - $id = $_SESSION['currentPage']; - } - if( is_a( $id, 'NewsletterType' ) ) { - $mailType = $id; - $id = $mailType->ID; - } else { - if($id && is_numeric($id)) { - $mailType = DataObject::get_by_id( 'NewsletterType', $id ); - } - } - - if($mailType->GroupID) { - $group = DataObject::get_one("Group", "ID = $mailType->GroupID"); - } - - if($mailType) { - $fields = new FieldSet( - new TabSet("Recipients", - new Tab( "Recipients", - $recipients = new MemberTableField( - $this, - "Recipients", - $group - ) - ), - new Tab( "Import", - $importField = new RecipientImportField("ImportFile","Import from file", $group ) - ) - ) - ); - - $recipients->setController($this); - $importField->setController($this); - $importField->setTypeID( $id ); - - $fields->push($idField = new HiddenField("ID")); - $fields->push( new HiddenField( "executeForm", "", "TypeEditForm" ) ); - $idField->setValue($id); - - $form = new Form($this, "EditForm", $fields, new FieldSet()); - } - } - */ - /** * Reloads the list of recipients via ajax */ @@ -369,7 +267,7 @@ class NewsletterAdmin extends LeftAndMain { else $e->From = $from = Email::getAdminEmail(); - $e->To = $_REQUEST['TestEmail']; + if(isset($_REQUEST['TestEmail'])) $e->To = $_REQUEST['TestEmail']; $e->setTemplate( $nlType->Template ); $messageID = base64_encode( $newsletter->ID . '_' . date( 'd-m-Y H:i:s' ) ); @@ -401,54 +299,16 @@ class NewsletterAdmin extends LeftAndMain { } static function sendToList( $subject, $body, $from, $newsletter, $nlType, $messageID = null ) { - $emailProcess = new NewsletterEmailProcess( $subject, $body, $from, $newsletter, $nlType, $messageID ); return $emailProcess->start(); - - /*$groupID = $nlType->GroupID; - - $members = DataObject::get( 'Member', "`GroupID`='$groupID'", null, "INNER JOIN `Group_Members` ON `MemberID`=`Member`.`ID`" ); - - // user_error( $members, E_USER_ERROR ); - - if( !$members ) - return "statusMessage('Unable to retrieve members from mailing list', 'bad' )"; - - foreach( $members as $member ) { - // check to see if the user has unsubscribed from the mailing list - $unsubscribeRecord = DataObject::get_one('Member_UnsubscribeRecord', "`MemberID`= {$member->ID} AND `NewsletterTypeID`={$nlType->ID}"); - - if( !$unsubscribeRecord ) { - $e = new Newsletter_Email($nlType); - $e->Body = $body; - $e->Subject =$subject; - $e->From = $from; - $e->setTemplate( $nlType->Template ); - - $e->populateTemplate( array( 'Member' => $member, 'FirstName' => $member->FirstName ) ); - $this->sendToAddress( $e, $member->Email, $messageID ); - } - } - - if( $newsletter->Sent ) - $resent = true; - - $newsletter->Sent = 'now'; - $newsletter->Status = 'Send'; - $newsletter->write(); - - if( $resent ) - return "resent_ok( '{$nlType->ID}', '{$newsletter->ID}' )"; - else - return "draft_sent_ok( '{$nlType->ID}', '{$newsletter->ID}' )";*/ } public function save($urlParams, $form) { - if( $_REQUEST['Type'] && $_REQUEST['Type'] == 'Newsletter' ) + if( isset($_REQUEST['Type']) && $_REQUEST['Type'] == 'Newsletter' ) return $this->savenewsletter( $urlParams, $form ); $id = $_REQUEST['ID']; - $record = DataObject::get_one('NewsletterType', "`$className`.ID = $id"); + $record = DataObject::get_one('NewsletterType', "`NewsletterType`.ID = $id"); // Is the template attached to the type, or the newsletter itself? @@ -464,9 +324,8 @@ class NewsletterAdmin extends LeftAndMain { } public function savenewsletter($urlParams, $form) { - $id = $_REQUEST['ID']; - $record = DataObject::get_one('Newsletter', "`$className`.ID = $id"); + $record = DataObject::get_one('Newsletter', "`Newsletter`.ID = $id"); // Is the template attached to the type, or the newsletter itself? $type = $record->getNewsletterType();