From a34d7fb821e862badd372af7c34d935659837ad5 Mon Sep 17 00:00:00 2001 From: helpfulrobot Date: Fri, 1 Jan 2016 04:43:02 +1300 Subject: [PATCH] Converted to PSR-2 --- code/controllers/CapturedEmailController.php | 26 +- code/controllers/MailCaptureAdmin.php | 50 ++-- code/dataobjects/CapturedEmail.php | 49 ++-- code/dataobjects/MassMailSend.php | 96 ++++---- code/email/CaptureMailer.php | 243 ++++++++++--------- code/tasks/PruneEmailsTask.php | 24 +- 6 files changed, 254 insertions(+), 234 deletions(-) diff --git a/code/controllers/CapturedEmailController.php b/code/controllers/CapturedEmailController.php index 6b311ac..3a68c6d 100644 --- a/code/controllers/CapturedEmailController.php +++ b/code/controllers/CapturedEmailController.php @@ -6,19 +6,21 @@ * @author marcus@silverstripe.com.au * @license BSD License http://silverstripe.org/bsd-license/ */ -class CapturedEmailController extends Controller { +class CapturedEmailController extends Controller +{ - private static $allowed_actions = array('view' => 'CMS_ACCESS_MailCaptureAdmin'); + private static $allowed_actions = array('view' => 'CMS_ACCESS_MailCaptureAdmin'); - public function view() { - $id = (int) $this->getRequest()->param('ID'); + public function view() + { + $id = (int) $this->getRequest()->param('ID'); - if ($id) { - $email = DataList::create('CapturedEmail')->byID($id); - if ($email) { - return array('Email' => $email); - return $this->customise()->renderWith('CapturedEmailController_view'); - } - } - } + if ($id) { + $email = DataList::create('CapturedEmail')->byID($id); + if ($email) { + return array('Email' => $email); + return $this->customise()->renderWith('CapturedEmailController_view'); + } + } + } } diff --git a/code/controllers/MailCaptureAdmin.php b/code/controllers/MailCaptureAdmin.php index ef41c69..6bc686c 100644 --- a/code/controllers/MailCaptureAdmin.php +++ b/code/controllers/MailCaptureAdmin.php @@ -4,32 +4,34 @@ * @author marcus@silverstripe.com.au * @license BSD License http://silverstripe.org/bsd-license/ */ -class MailCaptureAdmin extends ModelAdmin { +class MailCaptureAdmin extends ModelAdmin +{ - private static $url_segment = 'mailcapture'; - private static $menu_title = 'Email Logs'; - private static $managed_models = array( - 'CapturedEmail', - 'MassMailSend', - ); + private static $url_segment = 'mailcapture'; + private static $menu_title = 'Email Logs'; + private static $managed_models = array( + 'CapturedEmail', + 'MassMailSend', + ); - public function init() { - parent::init(); - $this->showImportForm = false; - } + public function init() + { + parent::init(); + $this->showImportForm = false; + } - public function getEditForm($id = null, $fields = null) { - $form = parent::getEditForm($id, $fields); - - if ($this->modelClass == 'CapturedEmail') { - $grid = $form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass)); - if ($grid) { - $grid->getConfig()->removeComponentsByType('GridFieldEditButton'); - $grid->getConfig()->removeComponentsByType('GridFieldDeleteAction'); - $grid->getConfig()->addComponent(new ViewEmailButton()); - } - } - return $form; - } + public function getEditForm($id = null, $fields = null) + { + $form = parent::getEditForm($id, $fields); + if ($this->modelClass == 'CapturedEmail') { + $grid = $form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass)); + if ($grid) { + $grid->getConfig()->removeComponentsByType('GridFieldEditButton'); + $grid->getConfig()->removeComponentsByType('GridFieldDeleteAction'); + $grid->getConfig()->addComponent(new ViewEmailButton()); + } + } + return $form; + } } diff --git a/code/dataobjects/CapturedEmail.php b/code/dataobjects/CapturedEmail.php index a3244be..2a18aad 100644 --- a/code/dataobjects/CapturedEmail.php +++ b/code/dataobjects/CapturedEmail.php @@ -4,31 +4,32 @@ * @author marcus@silverstripe.com.au * @license BSD License http://silverstripe.org/bsd-license/ */ -class CapturedEmail extends DataObject { - public static $db = array( - 'To' => 'Varchar(128)', - 'From' => 'Varchar(128)', - 'Subject' => 'Varchar(128)', - 'Headers' => 'Text', - 'Content' => 'Text', - 'PlainText' => 'Text', - ); +class CapturedEmail extends DataObject +{ + public static $db = array( + 'To' => 'Varchar(128)', + 'From' => 'Varchar(128)', + 'Subject' => 'Varchar(128)', + 'Headers' => 'Text', + 'Content' => 'Text', + 'PlainText' => 'Text', + ); - public static $has_one = array( - 'Send' => 'MassMailSend', - ); + public static $has_one = array( + 'Send' => 'MassMailSend', + ); - public static $summary_fields = array( - 'Created', - 'Subject', - 'To', - 'From' - ); - public static $searchable_fields = array( - 'Subject', - 'To', - 'From' - ); + public static $summary_fields = array( + 'Created', + 'Subject', + 'To', + 'From' + ); + public static $searchable_fields = array( + 'Subject', + 'To', + 'From' + ); - public static $default_sort = 'ID DESC'; + public static $default_sort = 'ID DESC'; } diff --git a/code/dataobjects/MassMailSend.php b/code/dataobjects/MassMailSend.php index d268195..fe55ca2 100644 --- a/code/dataobjects/MassMailSend.php +++ b/code/dataobjects/MassMailSend.php @@ -6,55 +6,65 @@ * @author marcus@silverstripe.com.au * @license BSD License http://silverstripe.org/bsd-license/ */ -class MassMailSend extends DataObject { - public static $db = array( - 'Title' => 'Varchar(128)', - ); +class MassMailSend extends DataObject +{ + public static $db = array( + 'Title' => 'Varchar(128)', + ); - public static $has_many = array( - 'Emails' => 'CapturedEmail', - ); + public static $has_many = array( + 'Emails' => 'CapturedEmail', + ); - public static $default_sort = 'ID DESC'; + public static $default_sort = 'ID DESC'; - public function getCMSFields() { - $fields = parent::getCMSFields(); + public function getCMSFields() + { + $fields = parent::getCMSFields(); - $fields->removeByName('Emails'); - $config = GridFieldConfig_Base::create(); - $config->addComponent(new ViewEmailButton()); + $fields->removeByName('Emails'); + $config = GridFieldConfig_Base::create(); + $config->addComponent(new ViewEmailButton()); - $gf = GridField::create('Emails', 'Sent emails', $this->Emails(), $config); + $gf = GridField::create('Emails', 'Sent emails', $this->Emails(), $config); - $fields->addFieldToTab('Root.Main', $gf); + $fields->addFieldToTab('Root.Main', $gf); - return $fields; - } + return $fields; + } } -class ViewEmailButton implements GridField_ColumnProvider { - public function augmentColumns($field, &$cols) { - if(!in_array('Actions', $cols)) $cols[] = 'Actions'; - } - - public function getColumnsHandled($field) { - return array('Actions'); - } - - public function getColumnContent($field, $record, $col) { - if($record->canView()) { - $data = new ArrayData(array( - 'Link' => Controller::join_links('CapturedEmailController', 'view', $record->ID) - )); - return $data->renderWith('ViewEmailButton'); - } - } - - public function getColumnAttributes($field, $record, $col) { - return array('class' => 'col-buttons'); - } - - public function getColumnMetadata($gridField, $col) { - return array('title' => null); - } -} \ No newline at end of file +class ViewEmailButton implements GridField_ColumnProvider +{ + public function augmentColumns($field, &$cols) + { + if (!in_array('Actions', $cols)) { + $cols[] = 'Actions'; + } + } + + public function getColumnsHandled($field) + { + return array('Actions'); + } + + public function getColumnContent($field, $record, $col) + { + if ($record->canView()) { + $data = new ArrayData(array( + 'Link' => Controller::join_links('CapturedEmailController', 'view', $record->ID) + )); + return $data->renderWith('ViewEmailButton'); + } + } + + public function getColumnAttributes($field, $record, $col) + { + return array('class' => 'col-buttons'); + } + + public function getColumnMetadata($gridField, $col) + { + return array('title' => null); + } +} diff --git a/code/email/CaptureMailer.php b/code/email/CaptureMailer.php index 3898fdf..0727691 100644 --- a/code/email/CaptureMailer.php +++ b/code/email/CaptureMailer.php @@ -6,124 +6,127 @@ * @author marcus@silverstripe.com.au * @license BSD License http://silverstripe.org/bsd-license/ */ -class CaptureMailer extends Mailer { - - /** - * Do we capture emails in the system? - * - * @var type - */ - public static $capture_emails = true; - - /** - * Do we use the 'parent' send functionality to actually send emails out of the system? - * - * @var type - */ - public static $outbound_send = false; - - protected $send; - - public function setMassMailSend($item) { - $this->send = $item; - } - - /** - * @param string $to Email recipient - * @param string $from Email from - * @param string $subject Subject text - * @param string $plainContent Plain text content - * @param array $attachedFiles List of attached files - * @param array $customHeaders List of custom headers - * @return mixed Return false if failure, or list of arguments if success - */ - public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customHeaders = false) { - if (self::$capture_emails) { - $mail = new CapturedEmail(); - $mail->To = $to; - $mail->From = $from; - $mail->Subject = $subject; - - if (is_array($customHeaders)) { - foreach ($customHeaders as $header => $val) { - $mail->Headers .= "$header: $val \n"; - } - } - - $mail->PlainText = $plainContent; - - if ($this->send) { - $mail->SendID = $this->send->ID; - } - - $mail->write(); - } - - if (self::$outbound_send) { - return parent::sendPlain($to, $from, $subject, $plainContent, $attachedFiles, $customHeaders); - } - - return true; - } - - /** - * Send a multi-part HTML email - * TestMailer will merely record that the email was asked to be sent, without sending anything. - * - * @param string $to Email recipient - * @param string $from Email from - * @param string $subject Subject text - * @param string $htmlContent HTML Content - * @param array $attachedFiles List of attachments - * @param array $customHeaders User specified headers - * @param string $plainContent Plain text content. If omitted, will be generated from $htmlContent - * @return mixed Return false if failure, or list of arguments if success - */ - public function sendHTML( - $to, - $from, - $subject, - $htmlContent, - $attachedFiles = false, - $customHeaders = false, - $plainContent = false, - $inlineImages = false - ) { - if (self::$capture_emails) { - $mail = new CapturedEmail(); - $mail->To = $to; - $mail->From = $from; - $mail->Subject = $subject; - - if (is_array($customHeaders)) { - foreach ($customHeaders as $header => $val) { - $mail->Headers .= "$header: $val \n"; - } - } - - $mail->Content = $htmlContent; - $mail->PlainText = $plainContent; - - if ($this->send) { - $mail->SendID = $this->send->ID; - } - - $mail->write(); - } - - if (self::$outbound_send) { - return parent::sendHTML( - $to, - $from, - $subject, - $htmlContent, - $attachedFiles, - $customHeaders, - $plainContent, - $inlineImages - ); - } - - return true; - } +class CaptureMailer extends Mailer +{ + + /** + * Do we capture emails in the system? + * + * @var type + */ + public static $capture_emails = true; + + /** + * Do we use the 'parent' send functionality to actually send emails out of the system? + * + * @var type + */ + public static $outbound_send = false; + + protected $send; + + public function setMassMailSend($item) + { + $this->send = $item; + } + + /** + * @param string $to Email recipient + * @param string $from Email from + * @param string $subject Subject text + * @param string $plainContent Plain text content + * @param array $attachedFiles List of attached files + * @param array $customHeaders List of custom headers + * @return mixed Return false if failure, or list of arguments if success + */ + public function sendPlain($to, $from, $subject, $plainContent, $attachedFiles = false, $customHeaders = false) + { + if (self::$capture_emails) { + $mail = new CapturedEmail(); + $mail->To = $to; + $mail->From = $from; + $mail->Subject = $subject; + + if (is_array($customHeaders)) { + foreach ($customHeaders as $header => $val) { + $mail->Headers .= "$header: $val \n"; + } + } + + $mail->PlainText = $plainContent; + + if ($this->send) { + $mail->SendID = $this->send->ID; + } + + $mail->write(); + } + + if (self::$outbound_send) { + return parent::sendPlain($to, $from, $subject, $plainContent, $attachedFiles, $customHeaders); + } + + return true; + } + + /** + * Send a multi-part HTML email + * TestMailer will merely record that the email was asked to be sent, without sending anything. + * + * @param string $to Email recipient + * @param string $from Email from + * @param string $subject Subject text + * @param string $htmlContent HTML Content + * @param array $attachedFiles List of attachments + * @param array $customHeaders User specified headers + * @param string $plainContent Plain text content. If omitted, will be generated from $htmlContent + * @return mixed Return false if failure, or list of arguments if success + */ + public function sendHTML( + $to, + $from, + $subject, + $htmlContent, + $attachedFiles = false, + $customHeaders = false, + $plainContent = false, + $inlineImages = false + ) { + if (self::$capture_emails) { + $mail = new CapturedEmail(); + $mail->To = $to; + $mail->From = $from; + $mail->Subject = $subject; + + if (is_array($customHeaders)) { + foreach ($customHeaders as $header => $val) { + $mail->Headers .= "$header: $val \n"; + } + } + + $mail->Content = $htmlContent; + $mail->PlainText = $plainContent; + + if ($this->send) { + $mail->SendID = $this->send->ID; + } + + $mail->write(); + } + + if (self::$outbound_send) { + return parent::sendHTML( + $to, + $from, + $subject, + $htmlContent, + $attachedFiles, + $customHeaders, + $plainContent, + $inlineImages + ); + } + + return true; + } } diff --git a/code/tasks/PruneEmailsTask.php b/code/tasks/PruneEmailsTask.php index db0b55b..d3fdf23 100644 --- a/code/tasks/PruneEmailsTask.php +++ b/code/tasks/PruneEmailsTask.php @@ -4,16 +4,18 @@ * @author * @license BSD License http://www.silverstripe.org/bsd-license */ -class PruneEmailsTask extends BuildTask { - public function run($request) { - if (Permission::check('ADMIN')) { - $since = date('Y-m-d H:i:s', strtotime('-1 month')); - $list = CapturedEmail::get()->filter('Created:LessThan', $since); - echo "Deleting " . $list->count() . " captured emails (if confirm flag set)
\n"; +class PruneEmailsTask extends BuildTask +{ + public function run($request) + { + if (Permission::check('ADMIN')) { + $since = date('Y-m-d H:i:s', strtotime('-1 month')); + $list = CapturedEmail::get()->filter('Created:LessThan', $since); + echo "Deleting " . $list->count() . " captured emails (if confirm flag set)
\n"; - if ($request->getVar('confirm')) { - $list->removeAll(); - } - } - } + if ($request->getVar('confirm')) { + $list->removeAll(); + } + } + } }