Skip to content

Commit

Permalink
Start making FormHelper::dateTime() tests pass.
Browse files Browse the repository at this point in the history
Split a big test up into some smaller ones that are a bit easier to work
with. Re skip some test as they are failing.
  • Loading branch information
markstory committed Feb 23, 2014
1 parent 83a2b7d commit 8390040
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 170 deletions.
111 changes: 75 additions & 36 deletions src/View/Helper/FormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class FormHelper extends Helper {
'button' => '<button{{attrs}}>{{text}}</button>',
'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
'checkboxContainer' => '<div class="checkbox">{{input}}{{label}}</div>',
'dateWidget' => '{{month}}{{day}}{{year}}{{hour}}{{minute}}{{second}}{{meridian}}',
'error' => '<div class="error-message">{{content}}</div>',
'errorList' => '<ul>{{content}}</ul>',
'errorItem' => '<li>{{text}}</li>',
Expand Down Expand Up @@ -2125,43 +2126,21 @@ public function meridian($fieldName, $attributes = array()) {
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::dateTime
*/
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($attributes['value'])) {
$attributes = $this->value($attributes, $fieldName);
}

if ($attributes['value'] === null && $attributes['empty'] != true) {
$attributes['value'] = time();
if (!empty($attributes['maxYear']) && $attributes['maxYear'] < date('Y')) {
$attributes['value'] = strtotime(date($attributes['maxYear'] . '-m-d'));
}
}

if (!empty($attributes['value'])) {
list($year, $month, $day, $hour, $min, $meridian) = $this->_getDateTimeValue(
$attributes['value'],
$timeFormat
);
}
$attributes += [
'empty' => true,
'value' => null,
'interval' => 1,
'round' => null,
'monthNames' => true,
'minYear' => null,
'maxYear' => null,
'timeFormat' => $timeFormat,
'second' => false,
];
$attributes = $this->_initInputField($fieldName, $attributes);
$attributes = $this->_dateTimeAttributes($attributes);

$defaults = array(
'minYear' => null, 'maxYear' => null, 'separator' => '-',
'interval' => 1, 'monthNames' => true, 'round' => null
);
$attributes = array_merge($defaults, (array)$attributes);
if (isset($attributes['minuteInterval'])) {
$attributes['interval'] = $attributes['minuteInterval'];
unset($attributes['minuteInterval']);
}
$minYear = $attributes['minYear'];
$maxYear = $attributes['maxYear'];
$separator = $attributes['separator'];
$interval = $attributes['interval'];
$monthNames = $attributes['monthNames'];
$round = $attributes['round'];
$attributes = array_diff_key($attributes, $defaults);
return $this->widget('datetime', $attributes);

if (!empty($interval) && $interval > 1 && !empty($min)) {
$current = new \DateTime();
Expand Down Expand Up @@ -2270,6 +2249,66 @@ public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $a
return $opt;
}

/**
* Helper method for converting from FormHelper attributes data to widget format.
*
* @param array $attributes Attributes to convert.
* @return array Converted attributes.
*/
protected function _dateTimeAttributes($attributes) {
$types = ['year', 'month', 'day', 'hour', 'minute', 'second', 'meridian'];
foreach ($types as $type) {
if (!isset($attributes[$type])) {
$attributes[$type] = [];
}

// Pass empty boolean to each type.
if (
!empty($attributes['empty']) &&
is_bool($attributes['empty']) &&
is_array($attributes[$type])
) {
$attributes[$type]['empty'] = $attributes['empty'];
}

// Move empty options into each type array.
if (isset($attributes['empty'][$type])) {
$attributes[$type]['empty'] = $attributes['empty'][$type];
}
}
unset($attributes['empty']);

$hasYear = is_array($attributes['year']);
if ($hasYear && isset($attributes['minYear'])) {
$attributes['year']['start'] = $attributes['minYear'];
}
if ($hasYear && isset($attributes['maxYear'])) {
$attributes['year']['end'] = $attributes['maxYear'];
}
unset($attributes['minYear'], $attributes['maxYear']);

if (is_array($attributes['month'])) {
$attributes['month']['names'] = $attributes['monthNames'];
}
unset($attributes['monthNames']);

if (is_array($attributes['hour']) && isset($attributes['timeFormat'])) {
$attributes['hour']['format'] = $attributes['timeFormat'];
}
unset($attributes['timeFormat']);

if (is_array($attributes['minute'])) {
$attributes['minute']['interval'] = $attributes['interval'];
$attributes['minute']['round'] = $attributes['round'];
}
unset($attributes['interval'], $attributes['round']);

if (!isset($attributes['val'])) {
$attributes['val'] = new \DateTime();
}
return $attributes;
}

/**
* Parse the value for a datetime selected value
*
Expand Down
Loading

0 comments on commit 8390040

Please sign in to comment.