ソースを参照

Make numberlib and formhelper work again

euromark 12 年 前
コミット
6f44197281

+ 11 - 2
Lib/Utility/NumberLib.php

@@ -213,14 +213,23 @@ class NumberLib extends CakeNumber {
 	/**
 	/**
 	 * Formats a number into a percentage string.
 	 * Formats a number into a percentage string.
 	 *
 	 *
+	 * Options:
+	 *
+	 * - `multiply`: Multiply the input value by 100 for decimal percentages.
+	 * - `decimals`: Decimal character.
+	 *
 	 * @param float $number A floating point number
 	 * @param float $number A floating point number
 	 * @param integer $precision The precision of the returned number
 	 * @param integer $precision The precision of the returned number
 	 * @param string $decimals
 	 * @param string $decimals
 	 * @return string Percentage string
 	 * @return string Percentage string
 	 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
 	 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/number.html#NumberHelper::toPercentage
 	 */
 	 */
-	public static function toPercentage($number, $precision = 2, $decimals = '.') {
-		return self::precision($number, $precision, $decimals) . '%';
+	public static function toPercentage($number, $precision = 2, $options = array()) {
+		$options += array('multiply' => false, 'decimals' => '.');
+		if ($options['multiply']) {
+			$number *= 100;
+		}
+		return self::precision($number, $precision, $options['decimals']) . '%';
 	}
 	}
 
 
 	/**
 	/**

+ 9 - 3
Test/Case/Lib/Utility/NumberLibTest.php

@@ -56,22 +56,28 @@ class NumberLibTest extends MyCakeTestCase {
 	}
 	}
 
 
 	/**
 	/**
+	 * @return void
 	 */
 	 */
 	public function testToPercentage() {
 	public function testToPercentage() {
-		$is = NumberLib::toPercentage(22.11, 2, '.');
+		$is = NumberLib::toPercentage(22.11, 2, array('decimals' => '.'));
 		$expected = '22.11%';
 		$expected = '22.11%';
 		$this->assertSame($expected, $is);
 		$this->assertSame($expected, $is);
 
 
-		$is = NumberLib::toPercentage(22.11, 2, ',');
+		$is = NumberLib::toPercentage(22.11, 2, array('decimals' => ','));
 		$expected = '22,11%';
 		$expected = '22,11%';
 		$this->assertSame($expected, $is);
 		$this->assertSame($expected, $is);
 
 
-		$is = NumberLib::toPercentage(22.11, 0, ',');
+		$is = NumberLib::toPercentage(22.11, 0, array('decimals' => '.'));
 		$expected = '22%';
 		$expected = '22%';
 		$this->assertSame($expected, $is);
 		$this->assertSame($expected, $is);
+
+		$is = NumberLib::toPercentage(0.2311, 0, array('multiply' => true, 'decimals' => '.'));
+		$expected = '23%';
+		$this->assertSame($expected, $is);
 	}
 	}
 
 
 	/**
 	/**
+	 * @return void
 	 */
 	 */
 	public function testRoundTo() {
 	public function testRoundTo() {
 		//increment = 10
 		//increment = 10

+ 89 - 21
View/Helper/FormExtHelper.php

@@ -6,7 +6,7 @@ App::uses('FormHelper', 'View/Helper');
  *
  *
  * Some fixes:
  * Some fixes:
  * - 24 instead of 12 for dateTime()
  * - 24 instead of 12 for dateTime()
- * - postLink() has class postLink
+ * - postLink() has class postLink, deleteLink() class deleteLink
  * - normalize for textareas
  * - normalize for textareas
  * - novalidate can be applied globally via Configure
  * - novalidate can be applied globally via Configure
  *
  *
@@ -114,6 +114,44 @@ class FormExtHelper extends FormHelper {
 	}
 	}
 
 
 	/**
 	/**
+	 * Adds the given class to the element options.
+	 *
+	 * Do not add a "form-error" class, though.
+	 *
+	 * @overwrite
+	 * @param array $options Array options/attributes to add a class to
+	 * @param string $class The classname being added.
+	 * @param string $key the key to use for class.
+	 * @return array Array of options with $key set.
+	 */
+	public function addClass($options = array(), $class = null, $key = 'class') {
+		if ($key === 'class' && $class === 'form-error') {
+			return $options;
+		}
+		return parent::addClass($options, $class, $key);
+	}
+
+	/**
+	 * Overwrite FormHelper::_selectOptions()
+	 * Remove form-control if added here as it would only be added to the div.
+	 *
+	 * @param array $elements
+	 * @param array $parents
+	 * @param boolean $showParents
+	 * @param array $attributes
+	 * @return array
+	 */
+	protected function _selectOptions($elements = array(), $parents = array(), $showParents = null, $attributes = array()) {
+		if ($attributes['style'] === 'checkbox') {
+			if (!empty($attributes['class']) && $attributes['class'] === array('form-control')) {
+				unset($attributes['class']);
+			}
+		}
+		$selectOptions = parent::_selectOptions($elements, $parents, $showParents, $attributes);
+		return $selectOptions;
+	}
+
+	/**
 	 * Creates a textarea widget.
 	 * Creates a textarea widget.
 	 *
 	 *
 	 * ### Options:
 	 * ### Options:
@@ -161,8 +199,6 @@ class FormExtHelper extends FormHelper {
 	 * @link http://book.cakephp.org/view/1390/Automagic-Form-Elements
 	 * @link http://book.cakephp.org/view/1390/Automagic-Form-Elements
 	 */
 	 */
 	public function inputExt($fieldName, $options = array()) {
 	public function inputExt($fieldName, $options = array()) {
-		//$this->setEntity($fieldName);
-
 		$options = array_merge(
 		$options = array_merge(
 			array('before' => null, 'between' => null, 'after' => null, 'format' => null),
 			array('before' => null, 'between' => null, 'after' => null, 'format' => null),
 			$this->_inputDefaults,
 			$this->_inputDefaults,
@@ -299,7 +335,7 @@ class FormExtHelper extends FormHelper {
 		}
 		}
 		if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time') {
 		if ($options['type'] === 'datetime' || $options['type'] === 'date' || $options['type'] === 'time') {
 			$dateFormat = $this->_extractOption('dateFormat', $options, 'MDY');
 			$dateFormat = $this->_extractOption('dateFormat', $options, 'MDY');
-			$timeFormat = $this->_extractOption('timeFormat', $options, 12);
+			$timeFormat = $this->_extractOption('timeFormat', $options, 24);
 			unset($options['dateFormat'], $options['timeFormat']);
 			unset($options['dateFormat'], $options['timeFormat']);
 		}
 		}
 		if ($options['type'] === 'email') {
 		if ($options['type'] === 'email') {
@@ -380,6 +416,19 @@ class FormExtHelper extends FormHelper {
 	}
 	}
 
 
 	/**
 	/**
+	 * FormExtHelper::hour()
+	 * Overwrite parent
+	 *
+	 * @param mixed $fieldName
+	 * @param bool $format24Hours
+	 * @param mixed $attributes
+	 * @return void
+	 */
+	public function hour($fieldName, $format24Hours = true, $attributes = array()) {
+		return parent::hour($fieldName, $format24Hours, $attributes);
+	}
+
+	/**
 	 * Override with some custom functionality
 	 * Override with some custom functionality
 	 *
 	 *
 	 * - `datalist` - html5 list/datalist (fallback = invisible).
 	 * - `datalist` - html5 list/datalist (fallback = invisible).
@@ -421,14 +470,28 @@ class FormExtHelper extends FormHelper {
 	}
 	}
 
 
 	/**
 	/**
+	 * FormExtHelper::radio()
+	 * Overwrite to avoid "form-control" to be added.
+	 *
+	 * @param mixed $fieldName
+	 * @param mixed $options
+	 * @param mixed $attributes
+	 * @return void
+	 */
+	public function radio($fieldName, $options = array(), $attributes = array()) {
+		$attributes = $this->_initInputField($fieldName, $attributes);
+		if (!empty($attributes['class']) && $attributes['class'] == array('form-control')) {
+			$attributes['class'] = false;
+		}
+		return parent::radio($fieldName, $options, $attributes);
+	}
+
+	/**
 	 * Overwrite the default method with custom enhancements
 	 * Overwrite the default method with custom enhancements
 	 *
 	 *
 	 * @return array options
 	 * @return array options
 	 */
 	 */
 	protected function _initInputField($field, $options = array()) {
 	protected function _initInputField($field, $options = array()) {
-		//$autoRequire = Configure::read('Validation.autoRequire');
-		//Configure::write('Validation.autoRequire', false);
-
 		$normalize = true;
 		$normalize = true;
 		if (isset($options['normalize'])) {
 		if (isset($options['normalize'])) {
 			$normalize = $options['normalize'];
 			$normalize = $options['normalize'];
@@ -440,7 +503,7 @@ class FormExtHelper extends FormHelper {
 		if (!empty($options['value']) && is_string($options['value']) && $normalize) {
 		if (!empty($options['value']) && is_string($options['value']) && $normalize) {
 			$options['value'] = str_replace(array("\t", "\r\n", "\n"), ' ', $options['value']);
 			$options['value'] = str_replace(array("\t", "\r\n", "\n"), ' ', $options['value']);
 		}
 		}
-		//Configure::write('Validation.autoRequire', $autoRequire);
+
 		return $options;
 		return $options;
 	}
 	}
 
 
@@ -628,6 +691,13 @@ class FormExtHelper extends FormHelper {
 			$fieldName = $field;
 			$fieldName = $field;
 		}
 		}
 
 
+		if (isset($options['class'])) {
+			$class = $options['class'];
+			unset($options['class']);
+		}
+
+		$blacklist = array('timeFormat' => null, 'dateFormat' => null, 'minYear' => null, 'maxYear' => null, 'separator' => null);
+
 		$defaultOptions = array(
 		$defaultOptions = array(
 			'empty' => false,
 			'empty' => false,
 			'minYear' => date('Y') - 10,
 			'minYear' => date('Y') - 10,
@@ -642,13 +712,14 @@ class FormExtHelper extends FormHelper {
 			'class' => 'form-control day'
 			'class' => 'form-control day'
 		);
 		);
 		$customOptions = array_merge($defaultOptions, $customOptions, $options);
 		$customOptions = array_merge($defaultOptions, $customOptions, $options);
+		$customOptions = array_diff_key($customOptions, $blacklist);
 		$res['d'] = $this->day($field, $customOptions);
 		$res['d'] = $this->day($field, $customOptions);
-
 		$customOptions = array(
 		$customOptions = array(
 			'id' => $modelName.$fieldName.'-mm',
 			'id' => $modelName.$fieldName.'-mm',
-			'class' => 'form-control month'
+			'class' => 'form-control month',
 		);
 		);
 		$customOptions = array_merge($defaultOptions, $customOptions, $options);
 		$customOptions = array_merge($defaultOptions, $customOptions, $options);
+		$customOptions = array_diff_key($customOptions, $blacklist);
 		$res['m'] = $this->month($field, $customOptions);
 		$res['m'] = $this->month($field, $customOptions);
 
 
 		$customOptions = array(
 		$customOptions = array(
@@ -658,13 +729,9 @@ class FormExtHelper extends FormHelper {
 		$customOptions = array_merge($defaultOptions, $customOptions, $options);
 		$customOptions = array_merge($defaultOptions, $customOptions, $options);
 		$minYear = $customOptions['minYear'];
 		$minYear = $customOptions['minYear'];
 		$maxYear = $customOptions['maxYear'];
 		$maxYear = $customOptions['maxYear'];
+		$customOptions = array_diff_key($customOptions, $blacklist);
 		$res['y'] = $this->year($field, $minYear, $maxYear, $customOptions);
 		$res['y'] = $this->year($field, $minYear, $maxYear, $customOptions);
 
 
-		if (isset($options['class'])) {
-			$class = $options['class'];
-			unset($options['class']);
-		}
-
 		$select = implode($options['separator'], $res);
 		$select = implode($options['separator'], $res);
 
 
 		if ($this->isFieldError($field)) {
 		if ($this->isFieldError($field)) {
@@ -739,13 +806,10 @@ class FormExtHelper extends FormHelper {
 	 * @param mixed $options
 	 * @param mixed $options
 	 * @return string Generated set of select boxes for the date and time formats chosen.
 	 * @return string Generated set of select boxes for the date and time formats chosen.
 	 */
 	 */
-	public function dateTime($field, $options = array(), $tf = 24, $a = array()) {
+	public function dateTime($field, $options = array(), $timeFormat = 24, $attributes = array()) {
 		# temp fix
 		# temp fix
 		if (!is_array($options)) {
 		if (!is_array($options)) {
-			if ($options === null) {
-				//$options = 'DMY';
-			}
-			return parent::dateTime($field, $options, $tf, $a);
+			return parent::dateTime($field, $options, $timeFormat, $attributes);
 		}
 		}
 		return $this->dateTimeExt($field, $options);
 		return $this->dateTimeExt($field, $options);
 	}
 	}
@@ -788,7 +852,7 @@ class FormExtHelper extends FormHelper {
 		$fieldname = Inflector::camelize($field);
 		$fieldname = Inflector::camelize($field);
 
 
 		$customOptions = array_merge($defaultOptions, $options);
 		$customOptions = array_merge($defaultOptions, $options);
-		$format24Hours = $customOptions['timeFormat'] !== '24' ? false : true;
+		$format24Hours = (int)$customOptions['timeFormat'] !== 24 ? false : true;
 
 
 		if (strpos($field, '.') !== false) {
 		if (strpos($field, '.') !== false) {
 			list($model, $field) = explode('.', $field, 2);
 			list($model, $field) = explode('.', $field, 2);
@@ -797,10 +861,14 @@ class FormExtHelper extends FormHelper {
 			$model = $this->model();
 			$model = $this->model();
 		}
 		}
 
 
+		$blacklist = array('timeFormat' => null, 'dateFormat' => null, 'separator' => null);
+
 		$hourOptions = array_merge($customOptions, array('class'=>'form-control hour'));
 		$hourOptions = array_merge($customOptions, array('class'=>'form-control hour'));
+		$hourOptions = array_diff_key($hourOptions, $blacklist);
 		$res['h'] = $this->hour($field, $format24Hours, $hourOptions);
 		$res['h'] = $this->hour($field, $format24Hours, $hourOptions);
 
 
 		$minuteOptions = array_merge($customOptions, array('class'=>'form-control minute'));
 		$minuteOptions = array_merge($customOptions, array('class'=>'form-control minute'));
+		$minuteOptions = array_diff_key($minuteOptions, $blacklist);
 		$res['m'] = $this->minute($field, $minuteOptions);
 		$res['m'] = $this->minute($field, $minuteOptions);
 
 
 		$select = implode($options['separator'], $res);
 		$select = implode($options['separator'], $res);