Skip to content

Commit

Permalink
dateTime functions + 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 a876c77 commit 9eb533b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
45 changes: 21 additions & 24 deletions cake/libs/view/helpers/form.php
Expand Up @@ -874,14 +874,17 @@ public function input($fieldName, $options = array()) {
unset($options['options']);
$input = $this->select($fieldName, $list, $options);
break;
case 'time':
$input = $this->dateTime($fieldName, null, $timeFormat, $selected, $options);
case 'time':
$options['value'] = $selected;
$input = $this->dateTime($fieldName, null, $timeFormat, $options);
break;
case 'date':
$input = $this->dateTime($fieldName, $dateFormat, null, $selected, $options);
$options['value'] = $selected;
$input = $this->dateTime($fieldName, $dateFormat, null, $options);
break;
case 'datetime':
$input = $this->dateTime($fieldName, $dateFormat, $timeFormat, $selected, $options);
$options['value'] = $selected;
$input = $this->dateTime($fieldName, $dateFormat, $timeFormat, $options);
break;
case 'textarea':
$input = $this->textarea($fieldName, $options + array('cols' => '30', 'rows' => '6'));
Expand Down Expand Up @@ -1762,46 +1765,40 @@ public function meridian($fieldName, $attributes = array()) {
* @param string $fieldName Prefix name for the SELECT element
* @param string $dateFormat DMY, MDY, YMD.
* @param string $timeFormat 12, 24.
* @param string $selected Option which is selected.
* @param string $attributes array of Attributes
* @return string Generated set of select boxes for the date and time formats chosen.
* @access public
* @link http://book.cakephp.org/view/1418/dateTime
*/
public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $selected = null, $attributes = array()) {
$attributes += array('empty' => true);
public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $attributes = array()) {
$attributes += array('empty' => true, 'value' => null);
$year = $month = $day = $hour = $min = $meridian = null;

if (empty($selected)) {
$selected = $this->value($attributes, $fieldName);
if (isset($selected['value'])) {
$selected = $selected['value'];
} else {
$selected = null;
}
if (empty($attributes['value'])) {
$attributes['value'] = $this->value($fieldName);
}

if ($selected === null && $attributes['empty'] != true) {
$selected = time();
if ($attributes['value'] === null && $attributes['empty'] != true) {
$attributes['value'] = time();
}

if (!empty($selected)) {
if (is_array($selected)) {
extract($selected);
if (!empty($attributes['value'])) {
if (is_array($attributes['value'])) {
extract($attributes['value']);
} else {
if (is_numeric($selected)) {
$selected = strftime('%Y-%m-%d %H:%M:%S', $selected);
if (is_numeric($attributes['value'])) {
$attributes['value'] = strftime('%Y-%m-%d %H:%M:%S', $attributes['value']);
}
$meridian = 'am';
$pos = strpos($selected, '-');
$pos = strpos($attributes['value'], '-');
if ($pos !== false) {
$date = explode('-', $selected);
$date = explode('-', $attributes['value']);
$days = explode(' ', $date[2]);
$day = $days[0];
$month = $date[1];
$year = $date[0];
} else {
$days[1] = $selected;
$days[1] = $attributes['value'];
}

if (!empty($timeFormat)) {
Expand Down
18 changes: 9 additions & 9 deletions cake/tests/cases/libs/view/helpers/form.test.php
Expand Up @@ -3858,7 +3858,7 @@ function testCheckboxHiddenFieldOmission() {
function testDateTime() {
extract($this->dateRegex);

$result = $this->Form->dateTime('Contact.date', 'DMY', '12', null, array('empty' => false));
$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
$now = strtotime('now');
$expected = array(
array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
Expand Down Expand Up @@ -3944,7 +3944,7 @@ function testDateTime() {
$this->assertTags($result, $expected);
$this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);

$result = $this->Form->dateTime('Contact.date', 'DMY', '12', false);
$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
$expected = array(
array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
$daysRegex,
Expand Down Expand Up @@ -3984,7 +3984,7 @@ function testDateTime() {
$this->assertTags($result, $expected);
$this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);

$result = $this->Form->dateTime('Contact.date', 'DMY', '12', '');
$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
$expected = array(
array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
$daysRegex,
Expand Down Expand Up @@ -4024,7 +4024,7 @@ function testDateTime() {
$this->assertTags($result, $expected);
$this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);

$result = $this->Form->dateTime('Contact.date', 'DMY', '12', '', array('interval' => 5));
$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
$expected = array(
array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
$daysRegex,
Expand Down Expand Up @@ -4073,7 +4073,7 @@ function testDateTime() {
$this->assertTags($result, $expected);
$this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);

$result = $this->Form->dateTime('Contact.date', 'DMY', '12', '', array('minuteInterval' => 5));
$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
$expected = array(
array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
$daysRegex,
Expand Down Expand Up @@ -4164,8 +4164,8 @@ function testDateTime() {
$this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);

$this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
$now = strtotime($this->Form->request->data['Model']['field']);
$result = $this->Form->dateTime('Model.field', 'DMY', '12', null, array('empty' => false));
$now = strtotime($this->Form->data['Model']['field']);
$result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
$expected = array(
array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
$daysRegex,
Expand Down Expand Up @@ -4211,7 +4211,7 @@ function testDateTime() {
$this->assertTags($result, $expected);

$selected = strtotime('2008-10-26 10:33:00');
$result = $this->Form->dateTime('Model.field', 'DMY', '12', $selected);
$result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
$this->assertPattern('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
$this->assertPattern('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>10<\/option>/', $result);
$this->assertPattern('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
Expand Down Expand Up @@ -4366,7 +4366,7 @@ function testDateTime() {
'meridian' => ''
)
);
$result = $this->Form->dateTime('Contact.date', 'DMY', '12', null, array('empty' => false));
$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
}

/**
Expand Down

0 comments on commit 9eb533b

Please sign in to comment.