From 2977448891aaaec7be1c23dd566fe19b75e6b15e Mon Sep 17 00:00:00 2001 From: Jeremy Harris Date: Tue, 1 Mar 2011 17:14:15 -0800 Subject: [PATCH 1/2] Added more options to NumberHelper::currency() to allow for better flexibility in defining formats. Fixes #1267 --- cake/libs/view/helpers/number.php | 38 +++++++++++-------- .../cases/libs/view/helpers/number.test.php | 38 ++++++++++++++++++- 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/cake/libs/view/helpers/number.php b/cake/libs/view/helpers/number.php index c88cd8ff2..8c9a2e665 100644 --- a/cake/libs/view/helpers/number.php +++ b/cake/libs/view/helpers/number.php @@ -38,16 +38,16 @@ class NumberHelper extends AppHelper { */ protected $_currencies = array( 'USD' => array( - 'before' => '$', 'after' => 'c', 'zero' => 0, 'places' => 2, 'thousands' => ',', - 'decimals' => '.', 'negative' => '()', 'escape' => true + 'wholeSymbol' => '$', 'wholePosition' => 'before', 'fractionSymbol' => 'c', 'fractionPosition' => 'after', + 'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()', 'escape' => true ), 'GBP' => array( - 'before'=>'£', 'after' => 'p', 'zero' => 0, 'places' => 2, 'thousands' => ',', - 'decimals' => '.', 'negative' => '()','escape' => false + 'wholeSymbol'=>'£', 'wholePosition' => 'before', 'fractionSymbol' => 'p', 'fractionPosition' => 'after', + 'zero' => 0, 'places' => 2, 'thousands' => ',', 'decimals' => '.', 'negative' => '()','escape' => false ), 'EUR' => array( - 'before'=>'€', 'after' => false, 'zero' => 0, 'places' => 2, 'thousands' => '.', - 'decimals' => ',', 'negative' => '()', 'escape' => false + 'wholeSymbol'=>'€', 'wholePosition' => 'before', 'fractionSymbol' => false, 'fractionPosition' => 'after', + 'zero' => 0, 'places' => 2, 'thousands' => '.', 'decimals' => ',', 'negative' => '()', 'escape' => false ) ); @@ -58,8 +58,8 @@ class NumberHelper extends AppHelper { * @access protected */ protected $_currencyDefaults = array( - 'before'=>'', 'after' => '', 'zero' => '0', 'places' => 2, 'thousands' => ',', - 'decimals' => '.','negative' => '()', 'escape' => true + 'wholeSymbol'=>'', 'wholePosition' => 'before', 'fractionSymbol' => '', 'fractionPosition' => 'after', + 'zero' => '0', 'places' => 2, 'thousands' => ',', 'decimals' => '.','negative' => '()', 'escape' => true ); /** @@ -190,26 +190,32 @@ class NumberHelper extends AppHelper { $options = array_merge($default, $options); - $result = null; + if (isset($options['before']) && $options['before'] !== '') { + $options['wholeSymbol'] = $options['before']; + } + if (isset($options['after']) && !$options['after'] !== '') { + $options['fractionSymbol'] = $options['after']; + } + $result = $options['before'] = $options['after'] = null; + + $symbolKey = 'whole'; if ($number == 0 ) { if ($options['zero'] !== 0 ) { return $options['zero']; } - $options['after'] = null; } elseif ($number < 1 && $number > -1 ) { - if ($options['after'] !== false) { + if ($options['fractionSymbol'] !== false) { $multiply = intval('1' . str_pad('', $options['places'], '0')); $number = $number * $multiply; - $options['before'] = null; $options['places'] = null; + $symbolKey = 'fraction'; } - } elseif (empty($options['before'])) { - $options['before'] = null; - } else { - $options['after'] = null; } + $position = $options[$symbolKey.'Position'] != 'after' ? 'before' : 'after'; + $options[$position] = $options[$symbolKey.'Symbol']; + $abs = abs($number); $result = $this->format($abs, $options); diff --git a/cake/tests/cases/libs/view/helpers/number.test.php b/cake/tests/cases/libs/view/helpers/number.test.php index 99857df99..8bbd1eea6 100644 --- a/cake/tests/cases/libs/view/helpers/number.test.php +++ b/cake/tests/cases/libs/view/helpers/number.test.php @@ -114,7 +114,7 @@ class NumberHelperTest extends CakeTestCase { $expected = '£100,100,100.00'; $this->assertEqual($expected, $result); - $result = $this->Number->currency($value, '', array('thousands' =>' ', 'after' => '€', 'decimals' => ',', 'zero' => 'Gratuit')); + $result = $this->Number->currency($value, '', array('thousands' =>' ', 'wholeSymbol' => '€', 'wholePosition' => 'after', 'decimals' => ',', 'zero' => 'Gratuit')); $expected = '100 100 100,00€'; $this->assertEqual($expected, $result); @@ -129,6 +129,42 @@ class NumberHelperTest extends CakeTestCase { $result = $this->Number->currency(0.5, NULL, array('after'=>'øre')); $expected = '50øre'; $this->assertEqual($expected,$result); + + $result = $this->Number->currency(1, null, array('wholeSymbol' => '$ ')); + $expected = '$ 1.00'; + $this->assertEqual($result, $expected); + + $result = $this->Number->currency(1, null, array('wholeSymbol' => ' $', 'wholePosition' => 'after')); + $expected = '1.00 $'; + $this->assertEqual($result, $expected); + + $result = $this->Number->currency(.2, null, array('wholeSymbol' => ' $', 'wholePosition' => 'after', 'fractionSymbol' => 'cents')); + $expected = '20cents'; + $this->assertEqual($result, $expected); + + $result = $this->Number->currency(.2, null, array('wholeSymbol' => ' $', 'wholePosition' => 'after', 'fractionSymbol' => 'cents', 'fractionPosition' => 'before')); + $expected = 'cents20'; + $this->assertEqual($result, $expected); + + $result = $this->Number->currency(311, 'USD', array('wholePosition' => 'after')); + $expected = '311.00$'; + $this->assertEqual($result, $expected); + + $result = $this->Number->currency(.2, 'EUR'); + $expected = '€0,20'; + $this->assertEqual($result, $expected); + + $result = $this->Number->currency(12, null, array('wholeSymbol' => ' dollars', 'wholePosition' => 'after', 'fractionSymbol' => ' cents', 'fractionPosition' => 'after')); + $expected = '12.00 dollars'; + $this->assertEqual($result, $expected); + + $result = $this->Number->currency(.12, null, array('wholeSymbol' => ' dollars', 'wholePosition' => 'after', 'fractionSymbol' => ' cents', 'fractionPosition' => 'after')); + $expected = '12 cents'; + $this->assertEqual($result, $expected); + + $result = $this->Number->currency(.5, null, array('fractionSymbol' => false, 'fractionPosition' => 'before', 'wholeSymbol' => '$')); + $expected = '$0.50'; + $this->assertEqual($result, $expected); } /** From 06f9fc0fc9cb28a41fcfb8989596f4de0f349e05 Mon Sep 17 00:00:00 2001 From: Jeremy Harris Date: Tue, 1 Mar 2011 17:31:02 -0800 Subject: [PATCH 2/2] Made form post to the current location by default. Fixes #1418 --- cake/libs/view/helpers/form.php | 8 +++-- .../cases/libs/view/helpers/form.test.php | 35 +++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index 1eb2b315f..75b5ce62d 100644 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -238,8 +238,12 @@ class FormHelper extends AppHelper { $options); $this->_inputDefaults = $options['inputDefaults']; unset($options['inputDefaults']); - - if (empty($options['url']) || is_array($options['url'])) { + if ($options['action'] === null && $options['url'] === null) { + $options['action'] = $this->request->here; + if (!isset($options['id'])) { + $options['id'] = $this->domId($this->request['action'] . 'Form'); + } + } elseif (empty($options['url']) || is_array($options['url'])) { if (empty($options['url']['controller'])) { if (!empty($model) && $model != $this->defaultModel) { $options['url']['controller'] = Inflector::underscore(Inflector::pluralize($model)); diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index bb389396a..5694d8959 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -670,6 +670,7 @@ class FormHelperTest extends CakeTestCase { $this->Form = new FormHelper($this->View); $this->Form->Html = new HtmlHelper($this->View); $this->Form->request = new CakeRequest(null, false); + $this->Form->request->here = '/contacts/add'; $this->Form->request['action'] = 'add'; $this->Form->request->webroot = ''; $this->Form->request->base = ''; @@ -5524,6 +5525,7 @@ class FormHelperTest extends CakeTestCase { $this->assertTags($result, $expected); $this->Form->request->data['Contact']['id'] = 1; + $this->Form->request->here = '/contacts/edit/1'; $this->Form->request['action'] = 'edit'; $result = $this->Form->create('Contact'); $expected = array( @@ -5538,6 +5540,7 @@ class FormHelperTest extends CakeTestCase { $this->assertTags($result, $expected); $this->Form->request->data['Contact']['id'] = 1; + $this->Form->request->here = '/contacts/edit/1'; $this->Form->request['action'] = 'edit'; $result = $this->Form->create('Contact', array('type' => 'file')); $expected = array( @@ -5552,7 +5555,7 @@ class FormHelperTest extends CakeTestCase { $this->assertTags($result, $expected); $this->Form->request->data['ContactNonStandardPk']['pk'] = 1; - $result = $this->Form->create('ContactNonStandardPk'); + $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit'))); $expected = array( 'form' => array( 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post', @@ -5638,6 +5641,33 @@ class FormHelperTest extends CakeTestCase { '/div' ); $this->assertTags($result, $expected); + + $this->Form->request->here = '/contacts/add/Contact:1'; + $result = $this->Form->create(); + $expected = array( + 'form' => array( + 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add/Contact:1', + 'accept-charset' => 'utf-8' + ), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), + '/div' + ); + $this->assertTags($result, $expected); + + $this->Form->request['action'] = 'delete'; + $this->Form->request->here = '/contacts/delete/10/User:42'; + $result = $this->Form->create(); + $expected = array( + 'form' => array( + 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/contacts/delete/10/User:42', + 'accept-charset' => 'utf-8' + ), + 'div' => array('style' => 'display:none;'), + 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), + '/div' + ); + $this->assertTags($result, $expected); } /** @@ -5809,7 +5839,8 @@ class FormHelperTest extends CakeTestCase { */ function testGetFormWithFalseModel() { $encoding = strtolower(Configure::read('App.encoding')); - $result = $this->Form->create(false, array('type' => 'get')); + $this->Form->request['controller'] = 'contact_test'; + $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test'))); $expected = array('form' => array( 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',