Browse Source

cleanup and fixes

euromark 12 years ago
parent
commit
8f285a4bef

+ 56 - 121
Lib/EmailLib.php

@@ -10,12 +10,13 @@ if (!defined('BR')) {
 /**
  * Convenience class for internal mailer.
  * Adds some nice features and fixes some bugs:
- * - enbale embedded images in html mails
+ * - enable embedded images in html mails
  * - allow setting domain for CLI environment (now in core)
  * - enable easier attachment adding
  * - extensive logging and error tracing
  * - create mails with blob attachments (embedded or attached)
  * - allow wrapLength to be adjusted
+ * - Configure::read('Config.x-mailer') can modify the x-mailer
  *
  * @author Mark Scherer
  * @license MIT
@@ -63,13 +64,12 @@ class EmailLib extends CakeEmail {
 		} elseif ($message === null && array_key_exists('message', $config = $instance->config())) {
 			$message = $config['message'];
 		}
-		if (true || $send === true) {
-			return $instance->send($message);
-		}
-		return $instance;
+		return $instance->send($message);
 	}
 
 	/**
+	 * Change the layout
+	 *
 	 * @param string $layout Layout to use (or false to use none)
 	 * @return resource EmailLib
 	 * 2011-11-02 ms
@@ -82,6 +82,8 @@ class EmailLib extends CakeEmail {
 	}
 
 	/**
+	 * Add an attachment from file
+	 *
 	 * @param string $file: absolute path
 	 * @param string $filename
 	 * @param array $fileInfo
@@ -99,6 +101,8 @@ class EmailLib extends CakeEmail {
 	}
 
 	/**
+	 * Add an attachment as blob
+	 *
 	 * @param binary $content: blob data
 	 * @param string $filename to attach it
 	 * @param string $mimeType (leave it empty to get mimetype from $filename)
@@ -114,28 +118,8 @@ class EmailLib extends CakeEmail {
 	}
 
 	/**
-	 * @param binary $content: blob data
-	 * @param string $filename to attach it
-	 * @param string $mimeType (leave it empty to get mimetype from $filename)
-	 * @param string $contentId (optional)
-	 * @param array $options
-	 * - contentDisposition
-	 * @return mixed resource $EmailLib or string $contentId
-	 * 2011-11-02 ms
-	 */
-	public function addEmbeddedBlobAttachment($content, $name, $mimeType = null, $contentId = null, $options = array()) {
-		$options['content'] = $content;
-		$options['mimetype'] = $mimeType;
-		$options['contentId'] = $contentId ? $contentId : str_replace('-', '', String::uuid()) . '@' . $this->_domain;
-		$file = array($name => $options);
-		$res = $this->addAttachments($file);
-		if ($contentId === null) {
-			return $options['contentId'];
-		}
-		return $res;
-	}
-
-	/**
+	 * Add an inline attachment from file
+	 *
 	 * @param string $file: absolute path
 	 * @param string $filename (optional)
 	 * @param string $contentId (optional)
@@ -168,6 +152,30 @@ class EmailLib extends CakeEmail {
 	}
 
 	/**
+	 * Add an inline attachment as blob
+	 *
+	 * @param binary $content: blob data
+	 * @param string $filename to attach it
+	 * @param string $mimeType (leave it empty to get mimetype from $filename)
+	 * @param string $contentId (optional)
+	 * @param array $options
+	 * - contentDisposition
+	 * @return mixed resource $EmailLib or string $contentId
+	 * 2011-11-02 ms
+	 */
+	public function addEmbeddedBlobAttachment($content, $name, $mimeType = null, $contentId = null, $options = array()) {
+		$options['content'] = $content;
+		$options['mimetype'] = $mimeType;
+		$options['contentId'] = $contentId ? $contentId : str_replace('-', '', String::uuid()) . '@' . $this->_domain;
+		$file = array($name => $options);
+		$res = $this->addAttachments($file);
+		if ($contentId === null) {
+			return $options['contentId'];
+		}
+		return $res;
+	}
+
+	/**
 	 * Returns if this particular file has already been attached as embedded file with this exact name
 	 * to prevent the same image to overwrite each other and also to only send this image once.
 	 * Allows multiple usage of the same embedded image (using the same cid)
@@ -176,10 +184,10 @@ class EmailLib extends CakeEmail {
 	 */
 	protected function _isEmbeddedAttachment($file, $name) {
 		foreach ($this->_attachments as $filename => $fileInfo) {
-			if ($filename != $name) {
+			if ($filename !== $name) {
 				continue;
 			}
-			if ($fileInfo['file'] == $file) {
+			if ($fileInfo['file'] === $file) {
 				return $fileInfo['contentId'];
 			}
 		}
@@ -239,9 +247,10 @@ class EmailLib extends CakeEmail {
 
 	/**
 	 * Attach inline/embedded files to the message.
-	 * @override
+	 *
 	 * CUSTOM FIX: blob data support
 	 *
+	 * @override
 	 * @param string $boundary Boundary to use. If null, will default to $this->_boundary
 	 * @return array An array of lines to add to the message
 	 */
@@ -280,9 +289,10 @@ class EmailLib extends CakeEmail {
 
 	/**
 	 * Attach non-embedded files by adding file contents inside boundaries.
-	 * @override
+	 *
 	 * CUSTOM FIX: blob data support
 	 *
+	 * @override
 	 * @param string $boundary Boundary to use. If null, will default to $this->_boundary
 	 * @return array An array of lines to add to the message
 	 */
@@ -325,7 +335,7 @@ class EmailLib extends CakeEmail {
 
 	/**
 	 * Add attachments to the email message
-	 * @override
+	 *
 	 * CUSTOM FIX: blob data support
 	 *
 	 * Attachments can be defined in a few forms depending on how much control you need:
@@ -355,6 +365,7 @@ class EmailLib extends CakeEmail {
 	 * The `contentId` key allows you to specify an inline attachment. In your email text, you
 	 * can use `<img src="cid:abc123" />` to display the image inline.
 	 *
+	 * @override
 	 * @param mixed $attachments String with the filename or array with filenames
 	 * @return mixed Either the array of attachments when getting or $this when setting.
 	 * @throws SocketException
@@ -390,102 +401,15 @@ class EmailLib extends CakeEmail {
 		return $this;
 	}
 
-
-	/**
-	 * Get list of headers
-	 * @override
-	 * CUSTOM FIX: message id correctly set in CLI and can be passed in via domain()
-	 *
-	 * ### Includes:
-	 *
-	 * - `from`
-	 * - `replyTo`
-	 * - `readReceipt`
-	 * - `returnPath`
-	 * - `to`
-	 * - `cc`
-	 * - `bcc`
-	 * - `subject`
-	 *
-	 * @param array $include
-	 * @return array
-	 */
-	public function getHeaders($include = array()) {
-		if ($include == array_values($include)) {
-			$include = array_fill_keys($include, true);
-		}
-		$defaults = array_fill_keys(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject'), false);
-		$include += $defaults;
-
-		$headers = array();
-		$relation = array(
-			'from' => 'From',
-			'replyTo' => 'Reply-To',
-			'readReceipt' => 'Disposition-Notification-To',
-			'returnPath' => 'Return-Path'
-		);
-		foreach ($relation as $var => $header) {
-			if ($include[$var]) {
-				$var = '_' . $var;
-				$headers[$header] = current($this->_formatAddress($this->{$var}));
-			}
-		}
-		if ($include['sender']) {
-			if (key($this->_sender) === key($this->_from)) {
-				$headers['Sender'] = '';
-			} else {
-				$headers['Sender'] = current($this->_formatAddress($this->_sender));
-			}
-		}
-
-		foreach (array('to', 'cc', 'bcc') as $var) {
-			if ($include[$var]) {
-				$classVar = '_' . $var;
-				$headers[ucfirst($var)] = implode(', ', $this->_formatAddress($this->{$classVar}));
-			}
-		}
-
-		$headers += $this->_headers;
-		if (!isset($headers['X-Mailer'])) {
-			$headers['X-Mailer'] = self::EMAIL_CLIENT;
-		}
-		if (!isset($headers['Date'])) {
-			$headers['Date'] = date(DATE_RFC2822);
-		}
-		if ($this->_messageId !== false) {
-			if ($this->_messageId === true) {
-				$headers['Message-ID'] = '<' . str_replace('-', '', String::UUID()) . '@' . $this->_domain . '>';
-			} else {
-				$headers['Message-ID'] = $this->_messageId;
-			}
-		}
-
-		if ($include['subject']) {
-			$headers['Subject'] = $this->_subject;
-		}
-
-		$headers['MIME-Version'] = '1.0';
-		if (!empty($this->_attachments) || $this->_emailFormat === 'both') {
-			$headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"';
-		} elseif ($this->_emailFormat === 'text') {
-			$headers['Content-Type'] = 'text/plain; charset=' . $this->charset;
-		} elseif ($this->_emailFormat === 'html') {
-			$headers['Content-Type'] = 'text/html; charset=' . $this->charset;
-		}
-		$headers['Content-Transfer-Encoding'] = $this->_getContentTransferEncoding();
-
-		return $headers;
-	}
-
 	/**
 	 * Apply the config to an instance
 	 *
+	 * @overwrite
 	 * @param CakeEmail $obj CakeEmail
 	 * @param array $config
 	 * @return void
 	 * @throws ConfigureException When configuration file cannot be found, or is missing
 	 * the named config.
-	 * @overwrite
 	 */
 	protected function _applyConfig($config) {
 		if (is_string($config)) {
@@ -541,6 +465,7 @@ class EmailLib extends CakeEmail {
 	 *
 	 * LEAVE empty if you use $this->set() in combination with templates
 	 *
+	 * @overwrite
 	 * @param string/array: message
 	 * @return bool $success
 	 */
@@ -574,8 +499,13 @@ class EmailLib extends CakeEmail {
 		return true;
 	}
 
+	/**
+	 * Allow modifications of the message
+	 *
+	 * @param string $text
+	 * @return string Text
+	 */
 	protected function _prepMessage($text) {
-
 		return $text;
 	}
 
@@ -642,6 +572,11 @@ class EmailLib extends CakeEmail {
 		CakeLog::write($type, $res);
 	}
 
+	/**
+	 * EmailLib::resetAndSet()
+	 *
+	 * @return void
+	 */
 	public function resetAndSet() {
 		//$this->reset();
 

+ 21 - 5
Lib/Utility/NumberLib.php

@@ -10,11 +10,27 @@ class NumberLib extends CakeNumber {
 
 	protected static $_symbolRight = '€';
 
-	protected static $_symbolLeft = null;
+	protected static $_symbolLeft = '';
 
-	protected static $_decimalPoint = ',';
+	protected static $_decimals = ',';
 
-	protected static $_thousandsPoint = '.';
+	protected static $_thousands = '.';
+
+	/**
+	 * Correct the defaul values according to localization
+	 *
+	 * @return void
+	 */
+	public static function config($options = array()) {
+		$config = $options + (array)Configure::read('Localization');
+		foreach ($config as $key => $value) {
+			$key = '_' . $key;
+			if (!isset(self::${$key})) {
+				continue;
+			}
+			self::${$key} = $value;
+		}
+	}
 
 	/**
 	 * Display price (or was price if available)
@@ -77,7 +93,7 @@ class NumberLib extends CakeNumber {
 		} elseif (!is_array($formatOptions)) {
 			$formatOptions = array('places' => $formatOptions);
 		}
-		$options = array('before' => '', 'after' => '', 'places' => 2, 'thousands' => self::$_thousandsPoint, 'decimals' => self::$_decimalPoint, 'escape' => false);
+		$options = array('before' => '', 'after' => '', 'places' => 2, 'thousands' => self::$_thousands, 'decimals' => self::$_decimals, 'escape' => false);
 		$options = array_merge($options, $formatOptions);
 
 		if (!empty($options['currency'])) {
@@ -124,7 +140,7 @@ class NumberLib extends CakeNumber {
 		$options = array(
 			'wholeSymbol' => self::$_symbolRight, 'wholePosition' => 'after',
 			'negative' => '-', 'positive'=> '+', 'escape' => true,
-			'decimals' => self::$_decimalPoint, 'thousands' => self::$_thousandsPoint,
+			'decimals' => self::$_decimals, 'thousands' => self::$_thousands,
 		);
 		$options = array_merge($options, $formatOptions);
 

+ 5 - 1
Test/Case/Lib/Utility/NumberLibTest.php

@@ -10,7 +10,11 @@ class NumberLibTest extends MyCakeTestCase {
 	public function setUp() {
 		parent::setUp();
 
-		//$this->NumberLib = new NumberLib();
+		Configure::write('Localization', array(
+			'decimals' => ',',
+			'thousands' => '.'
+		));
+		NumberLib::config();
 	}
 
 	public function testMoney() {

+ 16 - 26
Test/Case/View/Helper/NumericHelperTest.php

@@ -3,35 +3,32 @@
 App::uses('NumericHelper', 'Tools.View/Helper');
 App::uses('MyCakeTestCase', 'Tools.TestSuite');
 App::uses('View', 'View');
+
 /**
  * Numeric Test Case
- *
- * @package cake.tests
- * @subpackage cake.tests.cases.libs.view.helpers
  */
 class NumericHelperTest extends MyCakeTestCase {
-/**
- * setUp method
- *
- * @access public
- * @return void
- */
+
 	public function setUp() {
 		parent::setUp();
 
+		Configure::write('Localization', array(
+			'decimals' => ',',
+			'thousands' => '.'
+		));
+		NumberLib::config();
 		$this->Numeric = new NumericHelper(new View(null));
 	}
 
-
-/**
- * test format
- *
- * TODO: move to NumberLib test?
- *
- * @access public
- * @return void
- * 2009-03-11 ms
- */
+	/**
+	 * test format
+	 *
+	 * TODO: move to NumberLib test?
+	 *
+	 * @access public
+	 * @return void
+	 * 2009-03-11 ms
+	 */
 	public function testFormat() {
 		$is = $this->Numeric->format('22');
 		$expected = '22,00';
@@ -79,13 +76,6 @@ class NumericHelperTest extends MyCakeTestCase {
 
 	}
 
-
-/**
- * tearDown method
- *
- * @access public
- * @return void
- */
 	public function tearDown() {
 		parent::tearDown();