Skip to content

Commit

Permalink
__dateTimeSelected and day functions done, with 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 30f3494 commit a1b1a07
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
37 changes: 18 additions & 19 deletions cake/libs/view/helpers/form.php
Expand Up @@ -1499,15 +1499,14 @@ public function select($fieldName, $options = array(), $attributes = array()) {
* that string is displayed as the empty element.
*
* @param string $fieldName Prefix name for the SELECT element
* @param string $selected Option which is selected.
* @param array $attributes HTML attributes for the select element
* @return string A generated day select box.
* @access public
* @link http://book.cakephp.org/view/1419/day
*/
public function day($fieldName, $selected = null, $attributes = array()) {
public function day($fieldName = null, $attributes = array()) {
$attributes += array('empty' => true);
$attributes['value'] = $this->__dateTimeSelected('day', $fieldName, $selected, $attributes);
$attributes = $this->__dateTimeSelected('day', $fieldName, $attributes);

if (strlen($attributes['value']) > 2) {
$attributes['value'] = date('d', strtotime($attributes['value']));
Expand Down Expand Up @@ -1591,8 +1590,8 @@ public function year($fieldName, $minYear = null, $maxYear = null, $selected = n
* @link http://book.cakephp.org/view/1417/month
*/
public function month($fieldName, $selected = null, $attributes = array()) {
$attributes += array('empty' => true);
$attributes['value'] = $this->__dateTimeSelected('month', $fieldName, $selected, $attributes);
$attributes += array('empty' => true, 'value' => $selected);
$attributes = $this->__dateTimeSelected('month', $fieldName, $attributes);

if (strlen($attributes['value']) > 2) {
$attributes['value'] = date('m', strtotime($attributes['value']));
Expand Down Expand Up @@ -1628,8 +1627,8 @@ public function month($fieldName, $selected = null, $attributes = array()) {
* @link http://book.cakephp.org/view/1420/hour
*/
public function hour($fieldName, $format24Hours = false, $selected = null, $attributes = array()) {
$attributes += array('empty' => true);
$attributes['value'] = $this->__dateTimeSelected('hour', $fieldName, $selected, $attributes);
$attributes += array('empty' => true, 'value' => $selected);
$attributes = $this->__dateTimeSelected('hour', $fieldName, $attributes);

if (strlen($attributes['value']) > 2) {
if ($format24Hours) {
Expand Down Expand Up @@ -1663,8 +1662,8 @@ public function hour($fieldName, $format24Hours = false, $selected = null, $attr
* @link http://book.cakephp.org/view/1421/minute
*/
public function minute($fieldName, $selected = null, $attributes = array()) {
$attributes += array('empty' => true);
$attributes['value'] = $this->__dateTimeSelected('min', $fieldName, $selected, $attributes);
$attributes += array('empty' => true, 'value' => $selected);
$attributes = $this->__dateTimeSelected('min', $fieldName, $attributes);

if (strlen($attributes['value']) > 2) {
$attributes['value'] = date('i', strtotime($attributes['value']));
Expand All @@ -1688,26 +1687,25 @@ public function minute($fieldName, $selected = null, $attributes = array()) {
*
* @param string $select Name of element field. ex. 'day'
* @param string $fieldName Name of fieldName being generated ex. Model.created
* @param mixed $selected The current selected value.
* @param array $attributes Array of attributes, must contain 'empty' key.
* @return string Currently selected value.
* @return array Attributes array with currently selected value.
* @access private
*/
function __dateTimeSelected($select, $fieldName, $selected, $attributes) {
if ((empty($selected) || $selected === true) && $value = $this->value($fieldName)) {
function __dateTimeSelected($select, $fieldName, $attributes) {
if ((empty($attributes['value']) || $attributes['value'] === true) && $value = $this->value($fieldName)) {
if (is_array($value) && isset($value[$select])) {
$selected = $value[$select];
$attributes['value'] = $value[$select];
} else {
if (empty($value)) {
if (!$attributes['empty']) {
$selected = 'now';
$attributes['value'] = 'now';
}
} else {
$selected = $value;
$attributes['value'] = $value;
}
}
}
return $selected;
return $attributes;
}

/**
Expand Down Expand Up @@ -1891,8 +1889,9 @@ public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $s
$selectMonthAttr['monthNames'] = $monthNames;
$selects[] = $this->month($fieldName, $month, $selectMonthAttr);
break;
case 'D':
$selects[] = $this->day($fieldName, $day, $selectDayAttr);
case 'D':
$selectDayAttr['value'] = $day;
$selects[] = $this->day($fieldName, $selectDayAttr);
break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions cake/tests/cases/libs/view/helpers/form.test.php
Expand Up @@ -4558,7 +4558,7 @@ function testMonth() {
function testDay() {
extract($this->dateRegex);

$result = $this->Form->day('Model.field', false);
$result = $this->Form->day('Model.field', array('value' => false));
$expected = array(
array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
array('option' => array('value' => '')),
Expand Down Expand Up @@ -4596,7 +4596,7 @@ function testDay() {
$this->assertTags($result, $expected);

$this->Form->request->data['Model']['field'] = '';
$result = $this->Form->day('Model.field', '10');
$result = $this->Form->day('Model.field', array('value' => '10'));
$expected = array(
array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
array('option' => array('value' => '')),
Expand All @@ -4617,7 +4617,7 @@ function testDay() {
$this->assertTags($result, $expected);

$this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
$result = $this->Form->day('Model.field', true);
$result = $this->Form->day('Model.field', array('value' => true));
$expected = array(
array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
array('option' => array('value' => '')),
Expand Down

0 comments on commit a1b1a07

Please sign in to comment.