Swapped out the Mail class for the Mailer class (it's better and smarter)
git-svn-id: http://svn.cleancode.org/svn/pickles@70 4d10bc64-7434-11dc-a737-d2d0f8310089
This commit is contained in:
parent
8e2d74674a
commit
b25ba0992a
4 changed files with 133 additions and 117 deletions
|
@ -50,6 +50,7 @@ class Controller extends Object {
|
|||
$logger = new Logger();
|
||||
$error = new Error($config, $logger);
|
||||
$db = new DB($config, $error);
|
||||
$mailer = new Mailer($config, $error);
|
||||
|
||||
// Generate a generic "site down" message
|
||||
if ($config->getDisabled()) {
|
||||
|
@ -95,7 +96,7 @@ class Controller extends Object {
|
|||
require_once $model_file;
|
||||
|
||||
if (class_exists($class)) {
|
||||
$model = new $class($config, $db);
|
||||
$model = new $class($config, $db, $mailer);
|
||||
}
|
||||
}
|
||||
// Tries to load the shared model
|
||||
|
@ -108,12 +109,12 @@ class Controller extends Object {
|
|||
}
|
||||
|
||||
if (class_exists($class)) {
|
||||
$model = new $class($config, $db);
|
||||
$model = new $class($config, $db, $mailer);
|
||||
}
|
||||
}
|
||||
// Loads the stock model
|
||||
else {
|
||||
$model = new Model($config, $db);
|
||||
$model = new Model($config, $db, $mailer);
|
||||
}
|
||||
|
||||
// Checks if we loaded a model file and no class was present
|
||||
|
@ -137,6 +138,12 @@ class Controller extends Object {
|
|||
// Potentially executes the model's logic
|
||||
if (method_exists($model, '__default')) {
|
||||
$model->__default();
|
||||
|
||||
if (isset($mailer->message)) {
|
||||
$status = $mailer->send();
|
||||
$model->type = $status['type'];
|
||||
$model->message = $status['message'];
|
||||
}
|
||||
}
|
||||
|
||||
// Creates a new viewer object
|
||||
|
|
113
classes/Mail.php
113
classes/Mail.php
|
@ -1,113 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Mail Class File for PICKLES
|
||||
*
|
||||
* PICKLES is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* PICKLES is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with PICKLES. If not, see
|
||||
* <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @author Joshua John Sherman <josh@phpwithpickles.org>
|
||||
* @copyright Copyright 2007, 2008 Joshua John Sherman
|
||||
* @link http://phpwithpickles.org
|
||||
* @license http://www.gnu.org/copyleft/lesser.html
|
||||
* @package PICKLES
|
||||
*/
|
||||
|
||||
/**
|
||||
* Small collection of mail utilities
|
||||
*
|
||||
* A bit too small actually, as there is still only one function in here. Since
|
||||
* I have built PICKLES based around my own needs, this has been the only
|
||||
* function I have felt the need to add.
|
||||
*
|
||||
* @todo Just so it doesn't seem so bare, I need to come up and implement a
|
||||
* few more mail functions.
|
||||
* @todo Still thinking about making this an instantiated object instead of
|
||||
* a static class. Perhaps I should add a mail (rename it to mailer
|
||||
* maybe) object to each model so it's ready to be loaded, and if it
|
||||
* is loaded, then go ahead and automatically send it?!? Loves it.
|
||||
*/
|
||||
class Mail {
|
||||
|
||||
/**
|
||||
* Sends an email message
|
||||
*
|
||||
* Creates and sends an email message. Relies heavily on a certain set of
|
||||
* circumstances specifically, the sender information, subject and message
|
||||
* are all assumed to be in the $_REQUEST variable and named a certain way.
|
||||
* This isn't that bad assuming you're using the PICKLES canned contact form
|
||||
* or adhere to the naming conventions. Recipient, subject line and subject
|
||||
* line prefix can all be loaded in from the configuration file.
|
||||
*
|
||||
* @param array $recipients An array of recipients (optional)
|
||||
* @param string $prefix Prefix to use on the subject line (optional)
|
||||
* @return array An associative array with a status type and message
|
||||
*/
|
||||
static function send($recipients = null, $prefix = null) {
|
||||
$config = Config::getInstance();
|
||||
$defaults = $config->contact;
|
||||
|
||||
if (!isset($recipients)) {
|
||||
$recipients = $defaults->recipients->recipient;
|
||||
}
|
||||
|
||||
if (is_object($recipients)) {
|
||||
$to = null;
|
||||
foreach ($recipients as $recipient) {
|
||||
$to .= (isset($to) ? ',' : '') . (string)$recipient;
|
||||
}
|
||||
}
|
||||
else {
|
||||
$to = $recipients;
|
||||
}
|
||||
|
||||
if (!isset($prefix)) {
|
||||
$prefix = isset($defaults->prefix) && $defaults->prefix != '' ? $defaults->prefix : null;
|
||||
}
|
||||
|
||||
$subject = str_replace("\n", '', (isset($prefix) ? "[{$prefix}] " : ''));
|
||||
|
||||
if (isset($defaults->subject)) {
|
||||
$subject .= $defaults->subject;
|
||||
}
|
||||
else {
|
||||
$subject .= $_REQUEST['subject'];
|
||||
}
|
||||
|
||||
if (isset($_REQUEST['name'])) {
|
||||
$from = "{$_REQUEST['name']} <{$_REQUEST['email']}>";
|
||||
}
|
||||
else {
|
||||
$from = $_REQUEST['email'];
|
||||
}
|
||||
|
||||
if (mail($to, $subject, stripslashes($_REQUEST['message']), "From: {$from}\r\n")) {
|
||||
$type = 'success';
|
||||
$message = isset($defaults['response']) ? $defaults['response'] : 'Message sent successfully';
|
||||
}
|
||||
else {
|
||||
$type = 'error';
|
||||
$message = 'An unexpected error has occurred';
|
||||
}
|
||||
|
||||
$return = array(
|
||||
'type' => $type,
|
||||
'message' => $message
|
||||
);
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
115
classes/Mailer.php
Normal file
115
classes/Mailer.php
Normal file
|
@ -0,0 +1,115 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Mailer Class File for PICKLES
|
||||
*
|
||||
* PICKLES is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version.
|
||||
*
|
||||
* PICKLES is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with PICKLES. If not, see
|
||||
* <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @author Joshua John Sherman <josh@phpwithpickles.org>
|
||||
* @copyright Copyright 2007, 2008 Joshua John Sherman
|
||||
* @link http://phpwithpickles.org
|
||||
* @license http://www.gnu.org/copyleft/lesser.html
|
||||
* @package PICKLES
|
||||
*/
|
||||
|
||||
/**
|
||||
* Mailer Class
|
||||
*
|
||||
* Handles mailing messages from within PICKLES. Mailer data is
|
||||
* loaded into the object (each model has one) and after everything
|
||||
* is done loading, it will automatically send out the email.
|
||||
*
|
||||
* @todo Logic needs to be cleaned up a bit (it's just sloppy since
|
||||
* the conversion from Mail();
|
||||
*/
|
||||
class Mailer extends Object {
|
||||
|
||||
public function __construct(Config $config, Error $error) {
|
||||
parent::__construct($config);
|
||||
$this->config = $config;
|
||||
$this->error = $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an email message
|
||||
*
|
||||
* @param array $recipients An array of recipients (optional)
|
||||
* @param string $prefix Prefix to use on the subject line (optional)
|
||||
* @return array An associative array with a status type and message
|
||||
*/
|
||||
public function send() {
|
||||
|
||||
// Gets the values (is any) set in the config
|
||||
$defaults = $this->config->contact;
|
||||
|
||||
// Pulls the recipients from the config
|
||||
if (!isset($this->recipients)) {
|
||||
$this->recipients = $defaults->recipients->recipient;
|
||||
}
|
||||
|
||||
// Loads up the "to" value
|
||||
if (is_object($this->recipients)) {
|
||||
$to = null;
|
||||
foreach ($this->recipients as $recipient) {
|
||||
$to .= (isset($to) ? ',' : '') . (string)$recipient;
|
||||
}
|
||||
}
|
||||
else {
|
||||
$to = $this->recipients;
|
||||
}
|
||||
|
||||
// Loads the subject line prefix
|
||||
$prefix = isset($this->prefix) ? $this->prefix : (isset($defaults->prefix) && $defaults->prefix != '' ? $defaults->prefix : null);
|
||||
|
||||
// Assembles the subject line with prefix
|
||||
$subject = str_replace("\n", '', (isset($prefix) ? '[' . $prefix . '] ' : ''));
|
||||
|
||||
// Tacks on the subject
|
||||
if (isset($this->subject)) {
|
||||
$subject .= $this->subject;
|
||||
}
|
||||
else if (isset($defaults->subjec)) {
|
||||
$subject .= $defaults->subject;
|
||||
}
|
||||
|
||||
// Puts together the sender's contact info in name <email> format
|
||||
if (isset($this->name)) {
|
||||
$from = $this->name . ' <' . $this->email . '>';
|
||||
}
|
||||
else {
|
||||
$from = $this->email;
|
||||
}
|
||||
|
||||
// Sends the mail
|
||||
if (mail($to, $subject, stripslashes($this->message), "From: {$from}\r\nX-Mailer: PHP with PICKLES\r\n")) {
|
||||
$type = 'success';
|
||||
$message = isset($defaults['response']) ? $defaults['response'] : 'Message sent successfully';
|
||||
}
|
||||
else {
|
||||
$type = 'error';
|
||||
$message = 'An unexpected error has occurred';
|
||||
}
|
||||
|
||||
// Builds the status array to be returned
|
||||
$return = array(
|
||||
'type' => $type,
|
||||
'message' => $message
|
||||
);
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -54,6 +54,11 @@ class Model extends Object {
|
|||
*/
|
||||
protected $name = null;
|
||||
|
||||
/**
|
||||
* Mailer object
|
||||
*/
|
||||
protected $mailer = null;
|
||||
|
||||
protected $authentication = null;
|
||||
protected $viewer = null;
|
||||
protected $session = null;
|
||||
|
@ -64,11 +69,13 @@ class Model extends Object {
|
|||
* Handles calling the parent constructor and sets up the model's
|
||||
* internal config and database object
|
||||
*/
|
||||
public function __construct($config, $db) {
|
||||
public function __construct(Config $config, DB $db, Mailer $mailer = null) {
|
||||
parent::__construct($config);
|
||||
|
||||
$this->config = $config;
|
||||
$this->db = $db;
|
||||
|
||||
$this->mailer = isset($mailer) ? $mailer : new Mailer($config);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue