mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 11:06:15 +00:00
Merge pull request #1424 from dereuromark/2.4-form-helper-datetime-rounding
provide ability for rounding - closes #1986
This commit is contained in:
commit
6c30851783
2 changed files with 42 additions and 2 deletions
|
@ -5877,6 +5877,33 @@ class FormHelperTest extends CakeTestCase {
|
||||||
$this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
|
$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.
|
* Test that empty values don't trigger errors.
|
||||||
*
|
*
|
||||||
|
|
|
@ -2343,6 +2343,7 @@ class FormHelper extends AppHelper {
|
||||||
* - `separator` The contents of the string between select elements. Defaults to '-'
|
* - `separator` The contents of the string between select elements. Defaults to '-'
|
||||||
* - `empty` - If true, the empty select option is shown. If a string,
|
* - `empty` - If true, the empty select option is shown. If a string,
|
||||||
* that string is displayed as the empty element.
|
* 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`
|
* - `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.
|
* 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(
|
$defaults = array(
|
||||||
'minYear' => null, 'maxYear' => null, 'separator' => '-',
|
'minYear' => null, 'maxYear' => null, 'separator' => '-',
|
||||||
'interval' => 1, 'monthNames' => true
|
'interval' => 1, 'monthNames' => true, 'round' => null
|
||||||
);
|
);
|
||||||
$attributes = array_merge($defaults, (array)$attributes);
|
$attributes = array_merge($defaults, (array)$attributes);
|
||||||
if (isset($attributes['minuteInterval'])) {
|
if (isset($attributes['minuteInterval'])) {
|
||||||
|
@ -2389,6 +2390,7 @@ class FormHelper extends AppHelper {
|
||||||
$separator = $attributes['separator'];
|
$separator = $attributes['separator'];
|
||||||
$interval = $attributes['interval'];
|
$interval = $attributes['interval'];
|
||||||
$monthNames = $attributes['monthNames'];
|
$monthNames = $attributes['monthNames'];
|
||||||
|
$round = $attributes['round'];
|
||||||
$attributes = array_diff_key($attributes, $defaults);
|
$attributes = array_diff_key($attributes, $defaults);
|
||||||
|
|
||||||
if ($timeFormat == 12 && $hour == 12) {
|
if ($timeFormat == 12 && $hour == 12) {
|
||||||
|
@ -2403,7 +2405,18 @@ class FormHelper extends AppHelper {
|
||||||
if ($hour !== null) {
|
if ($hour !== null) {
|
||||||
$current->setTime($hour, $min);
|
$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");
|
$current->modify($change > 0 ? "+$change minutes" : "$change minutes");
|
||||||
$format = ($timeFormat == 12) ? 'Y m d h i a' : 'Y m d H i a';
|
$format = ($timeFormat == 12) ? 'Y m d h i a' : 'Y m d H i a';
|
||||||
$newTime = explode(' ', $current->format($format));
|
$newTime = explode(' ', $current->format($format));
|
||||||
|
|
Loading…
Add table
Reference in a new issue