From c9e047ce370145148b41ca10d218a67d6188f7b1 Mon Sep 17 00:00:00 2001 From: ADmad Date: Thu, 28 Jan 2010 01:48:53 +0530 Subject: [PATCH] Adding 'orderYear' option for FormHelper::year to allow control over ordering of select options --- cake/libs/view/helpers/form.php | 22 +++++++++++++------ .../cases/libs/view/helpers/form.test.php | 15 +++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index 045a95fec17..1ba1ccdae93 100755 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -306,7 +306,7 @@ function create($model = null, $options = array()) { * * {{{ * array usage: - * + * * array('label' => 'save'); value="save" * array('label' => 'save', 'name' => 'Whatever'); value="save" name="Whatever" * array('name' => 'Whatever'); value="Submit" name="Whatever" @@ -389,7 +389,7 @@ function secure($fields = array()) { } /** - * Determine which fields of a form should be used for hash. + * Determine which fields of a form should be used for hash. * Populates $this->fields * * @param mixed $field Reference to field to be secured @@ -644,7 +644,7 @@ function inputs($fields = null, $blacklist = null) { * * - `type` - Force the type of widget you want. e.g. `type => 'select'` * - `label` - Either a string label, or an array of options for the label. See FormHelper::label() - * - `div` - Either `false` to disable the div, or an array of options for the div. + * - `div` - Either `false` to disable the div, or an array of options for the div. * See HtmlHelper::div() for more options. * - `options` - for widgets that take options e.g. radio, select * - `error` - control the error message that is produced @@ -1412,6 +1412,8 @@ function day($fieldName, $selected = null, $attributes = array()) { * * - `empty` - If true, the empty select option is shown. If a string, * that string is displayed as the empty element. + * - `orderYear` - Ordering of year values in select options. + * Possible values 'asc', 'desc'. Default 'desc' * * @param string $fieldName Prefix name for the SELECT element * @param integer $minYear First year in sequence @@ -1446,9 +1448,13 @@ function year($fieldName, $minYear = null, $maxYear = null, $selected = null, $a } elseif ($selected === false) { $selected = null; } - $yearOptions = array('min' => $minYear, 'max' => $maxYear); + $yearOptions = array('min' => $minYear, 'max' => $maxYear, 'order' => 'desc'); + if (isset($attributes['orderYear'])) { + $yearOptions['order'] = $attributes['orderYear']; + unset($attributes['orderYear']); + } return $this->select( - $fieldName . ".year", $this->__generateOptions('year', $yearOptions), + $fieldName . '.year', $this->__generateOptions('year', $yearOptions), $selected, $attributes ); } @@ -1862,7 +1868,7 @@ function __selectOptions($elements = array(), $selected = null, $parents = array if ($name !== null) { if ( - (!$selectedIsArray && !$selectedIsEmpty && (string)$selected == (string)$name) || + (!$selectedIsArray && !$selectedIsEmpty && (string)$selected == (string)$name) || ($selectedIsArray && in_array($name, $selected)) ) { if ($attributes['style'] === 'checkbox') { @@ -2005,7 +2011,9 @@ function __generateOptions($name, $options = array()) { for ($i = $min; $i <= $max; $i++) { $data[$i] = $i; } - $data = array_reverse($data, true); + if ($options['order'] != 'asc') { + $data = array_reverse($data, true); + } break; } $this->__options[$name] = $data; diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index 01da0727f44..aa128f7e058 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -4465,6 +4465,21 @@ function testYear() { ); $this->assertTags($result, $expected); + $result = $this->Form->year('Model.field', 2006, 2007, null, array('orderYear' => 'asc')); + $expected = array( + array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')), + array('option' => array('value' => '')), + '/option', + array('option' => array('value' => '2006')), + '2006', + '/option', + array('option' => array('value' => '2007')), + '2007', + '/option', + '/select', + ); + $this->assertTags($result, $expected); + $this->data['Contact']['published'] = ''; $result = $this->Form->year('Contact.published', 2006, 2007, null, array('class' => 'year')); $expected = array(