Merge pull request #1424 from dereuromark/2.4-form-helper-datetime-rounding

provide ability for rounding - closes #1986
This commit is contained in:
Mark 2013-08-07 10:15:35 -07:00
commit 6c30851783
2 changed files with 42 additions and 2 deletions

View file

@ -5877,6 +5877,33 @@ class FormHelperTest extends CakeTestCase {
$this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
}
/**
* Test dateTime with rounding
*
* @return void
*/
public function testDateTimeRounding() {
$this->Form->request->data['Contact'] = array(
'date' => array(
'day' => '13',
'month' => '12',
'year' => '2010',
'hour' => '04',
'min' => '19',
'meridian' => 'AM'
)
);
$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
$this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
$this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
$result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
$this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
}
/**
* Test that empty values don't trigger errors.
*

View file

@ -2343,6 +2343,7 @@ class FormHelper extends AppHelper {
* - `separator` The contents of the string between select elements. Defaults to '-'
* - `empty` - If true, the empty select option is shown. If a string,
* that string is displayed as the empty element.
* - `round` - Set to `up` or `down` if you want to force rounding in either direction. Defaults to null.
* - `value` | `default` The default value to be used by the input. A value in `$this->data`
* matching the field name will override this value. If no default is provided `time()` will be used.
*
@ -2377,7 +2378,7 @@ class FormHelper extends AppHelper {
$defaults = array(
'minYear' => null, 'maxYear' => null, 'separator' => '-',
'interval' => 1, 'monthNames' => true
'interval' => 1, 'monthNames' => true, 'round' => null
);
$attributes = array_merge($defaults, (array)$attributes);
if (isset($attributes['minuteInterval'])) {
@ -2389,6 +2390,7 @@ class FormHelper extends AppHelper {
$separator = $attributes['separator'];
$interval = $attributes['interval'];
$monthNames = $attributes['monthNames'];
$round = $attributes['round'];
$attributes = array_diff_key($attributes, $defaults);
if ($timeFormat == 12 && $hour == 12) {
@ -2403,7 +2405,18 @@ class FormHelper extends AppHelper {
if ($hour !== null) {
$current->setTime($hour, $min);
}
$change = (round($min * (1 / $interval)) * $interval) - $min;
$changeValue = $min * (1 / $interval);
switch ($round) {
case 'up':
$changeValue = ceil($changeValue);
break;
case 'down':
$changeValue = floor($changeValue);
break;
default:
$changeValue = round($changeValue);
}
$change = ($changeValue * $interval) - $min;
$current->modify($change > 0 ? "+$change minutes" : "$change minutes");
$format = ($timeFormat == 12) ? 'Y m d h i a' : 'Y m d H i a';
$newTime = explode(' ', $current->format($format));