Skip to content

Commit

Permalink
year function + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thatcode authored and markstory committed Oct 30, 2010
1 parent a1b1a07 commit 5297918
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
35 changes: 17 additions & 18 deletions cake/libs/view/helpers/form.php
Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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':
Expand Down
20 changes: 10 additions & 10 deletions cake/tests/cases/libs/view/helpers/form.test.php
Expand Up @@ -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' => '')),
Expand All @@ -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' => '')),
Expand All @@ -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')),
Expand All @@ -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' => '')),
Expand All @@ -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')),
Expand All @@ -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' => '')),
Expand All @@ -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')),
Expand All @@ -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')),
Expand All @@ -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')),
Expand All @@ -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')),
Expand Down

0 comments on commit 5297918

Please sign in to comment.