From 529791842c179e5f24ad88cdc17d2f2b1c48755f Mon Sep 17 00:00:00 2001 From: Robert Sworder Date: Thu, 5 Aug 2010 22:56:29 +0100 Subject: [PATCH] year function + tests --- cake/libs/view/helpers/form.php | 35 +++++++++---------- .../cases/libs/view/helpers/form.test.php | 20 +++++------ 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index e3aa93eed91..dcf8f20f3f5 100644 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -1505,7 +1505,7 @@ public function select($fieldName, $options = array(), $attributes = array()) { * @link http://book.cakephp.org/view/1419/day */ public function day($fieldName = null, $attributes = array()) { - $attributes += array('empty' => true); + $attributes += array('empty' => true, 'value' => null); $attributes = $this->__dateTimeSelected('day', $fieldName, $attributes); if (strlen($attributes['value']) > 2) { @@ -1529,43 +1529,41 @@ public function day($fieldName = null, $attributes = array()) { * @param string $fieldName Prefix name for the SELECT element * @param integer $minYear First year in sequence * @param integer $maxYear Last year in sequence - * @param string $selected Option which is selected. * @param array $attributes Attribute array for the select elements. * @return string Completed year select input * @access public * @link http://book.cakephp.org/view/1416/year */ - public function year($fieldName, $minYear = null, $maxYear = null, $selected = null, $attributes = array()) { - $attributes += array('empty' => true); - if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) { + public function year($fieldName, $minYear = null, $maxYear = null, $attributes = array()) { + $attributes += array('empty' => true, 'value' => null); + if ((empty($attributes['value']) || $attributes['value'] === true) && $value = $this->value($fieldName)) { if (is_array($value)) { extract($value); - $selected = $year; + $attributes['value'] = $year; } else { if (empty($value)) { if (!$attributes['empty'] && !$maxYear) { - $selected = 'now'; + $attributes['value'] = 'now'; - } elseif (!$attributes['empty'] && $maxYear && !$selected) { - $selected = $maxYear; + } elseif (!$attributes['empty'] && $maxYear && !$attributes['value']) { + $attributes['value'] = $maxYear; } } else { - $selected = $value; + $attributes['value'] = $value; } } } - if (strlen($selected) > 4 || $selected === 'now') { - $selected = date('Y', strtotime($selected)); - } elseif ($selected === false) { - $selected = null; + if (strlen($attributes['value']) > 4 || $attributes['value'] === 'now') { + $attributes['value'] = date('Y', strtotime($attributes['value'])); + } elseif ($attributes['value'] === false) { + $attributes['value'] = null; } $yearOptions = array('min' => $minYear, 'max' => $maxYear, 'order' => 'desc'); if (isset($attributes['orderYear'])) { $yearOptions['order'] = $attributes['orderYear']; unset($attributes['orderYear']); } - $attributes['value'] = $selected; return $this->select( $fieldName . '.year', $this->__generateOptions('year', $yearOptions), $attributes @@ -1776,7 +1774,7 @@ public function meridian($fieldName, $selected = null, $attributes = array()) { * @link http://book.cakephp.org/view/1418/dateTime */ public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $selected = null, $attributes = array()) { - $attributes += array('empty' => true); + $attributes += array('empty' => true); $year = $month = $day = $hour = $min = $meridian = null; if (empty($selected)) { @@ -1880,9 +1878,10 @@ public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $s $selects = array(); foreach (preg_split('//', $dateFormat, -1, PREG_SPLIT_NO_EMPTY) as $char) { switch ($char) { - case 'Y': + case 'Y': + $selectYearAttr['value'] = $year; $selects[] = $this->year( - $fieldName, $minYear, $maxYear, $year, $selectYearAttr + $fieldName, $minYear, $maxYear, $selectYearAttr ); break; case 'M': diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index c15a1bffbf0..b0c565ac57f 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -4848,7 +4848,7 @@ function testYear() { ); $this->assertTags($result, $expected); - $result = $this->Form->year('Model.field', 2006, 2007, null, array('orderYear' => 'asc')); + $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc')); $expected = array( array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')), array('option' => array('value' => '')), @@ -4864,7 +4864,7 @@ function testYear() { $this->assertTags($result, $expected); $this->request->data['Contact']['published'] = ''; - $result = $this->Form->year('Contact.published', 2006, 2007, null, array('class' => 'year')); + $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year')); $expected = array( array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')), array('option' => array('value' => '')), @@ -4880,7 +4880,7 @@ function testYear() { $this->assertTags($result, $expected); $this->Form->request->data['Contact']['published'] = '2006-10-10'; - $result = $this->Form->year('Contact.published', 2006, 2007, null, array('empty' => false)); + $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false)); $expected = array( array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')), array('option' => array('value' => '2007')), @@ -4894,7 +4894,7 @@ function testYear() { $this->assertTags($result, $expected); $this->Form->request->data['Contact']['published'] = ''; - $result = $this->Form->year('Contact.published', 2006, 2007, false); + $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false)); $expected = array( array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')), array('option' => array('value' => '')), @@ -4910,7 +4910,7 @@ function testYear() { $this->assertTags($result, $expected); $this->Form->request->data['Contact']['published'] = '2006-10-10'; - $result = $this->Form->year('Contact.published', 2006, 2007, false, array('empty' => false)); + $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false)); $expected = array( array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')), array('option' => array('value' => '2007')), @@ -4924,7 +4924,7 @@ function testYear() { $this->assertTags($result, $expected); $this->Form->request->data['Contact']['published'] = ''; - $result = $this->Form->year('Contact.published', 2006, 2007, 2007); + $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007)); $expected = array( array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')), array('option' => array('value' => '')), @@ -4940,7 +4940,7 @@ function testYear() { $this->assertTags($result, $expected); $this->Form->request->data['Contact']['published'] = '2006-10-10'; - $result = $this->Form->year('Contact.published', 2006, 2007, 2007, array('empty' => false)); + $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007)); $expected = array( array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')), array('option' => array('value' => '2007', 'selected' => 'selected')), @@ -4954,7 +4954,7 @@ function testYear() { $this->assertTags($result, $expected); $this->Form->request->data['Contact']['published'] = ''; - $result = $this->Form->year('Contact.published', 2006, 2008, 2007, array('empty' => false)); + $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007)); $expected = array( array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')), array('option' => array('value' => '2008')), @@ -4971,7 +4971,7 @@ function testYear() { $this->assertTags($result, $expected); $this->Form->request->data['Contact']['published'] = '2006-10-10'; - $result = $this->Form->year('Contact.published', 2006, 2008, null, array('empty' => false)); + $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false)); $expected = array( array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')), array('option' => array('value' => '2008')), @@ -4989,7 +4989,7 @@ function testYear() { $this->Form->request->data = array(); $this->Form->create('Contact'); - $result = $this->Form->year('published', 2006, 2008, null, array('empty' => false)); + $result = $this->Form->year('published', 2006, 2008, array('empty' => false)); $expected = array( array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')), array('option' => array('value' => '2008')),