diff options
author | Andrejs Verza <andrejs.verza@zabbix.com> | 2022-03-06 19:04:37 +0300 |
---|---|---|
committer | Andrejs Verza <andrejs.verza@zabbix.com> | 2022-03-06 19:04:37 +0300 |
commit | 7856b913f261ee0b7a579f3a4866b7f726916dfa (patch) | |
tree | 68378b2469ab04ca2e9c81f5081bedaff8eee324 /ui | |
parent | 5f49c090dd07a5e6bc32fc2156d68c1aab327d08 (diff) |
..F....... [ZBX-19583] unified date format across datepickers
Diffstat (limited to 'ui')
25 files changed, 231 insertions, 261 deletions
diff --git a/ui/app/controllers/CControllerPopupSlaEdit.php b/ui/app/controllers/CControllerPopupSlaEdit.php index 818e530479c..f87ff6c4ea4 100644 --- a/ui/app/controllers/CControllerPopupSlaEdit.php +++ b/ui/app/controllers/CControllerPopupSlaEdit.php @@ -107,7 +107,7 @@ class CControllerPopupSlaEdit extends CController { ? CSlaHelper::SCHEDULE_MODE_CUSTOM : CSlaHelper::SCHEDULE_MODE_24X7, 'schedule_periods' => $schedule_periods, - 'effective_date' => zbx_date2str(DATE_FORMAT, $this->sla['effective_date'], 'UTC'), + 'effective_date' => zbx_date2str(ZBX_DATE, $this->sla['effective_date'], 'UTC'), 'service_tags' => $this->sla['service_tags'], 'description' => $this->sla['description'], 'status' => $this->sla['status'], @@ -125,7 +125,7 @@ class CControllerPopupSlaEdit extends CController { 'timezone' => ZBX_DEFAULT_TIMEZONE, 'schedule_mode' => CSlaHelper::SCHEDULE_MODE_24X7, 'schedule_periods' => [0 => ''] + array_fill(1, 5, '8:00-17:00') + [6 => ''], - 'effective_date' => zbx_date2str(DATE_FORMAT, null, CTimezoneHelper::getSystemTimezone()), + 'effective_date' => zbx_date2str(ZBX_DATE, null, CTimezoneHelper::getSystemTimezone()), 'service_tags' => [ ['tag' => '', 'operator' => ZBX_SLA_SERVICE_TAG_OPERATOR_EQUAL, 'value' => ''] ], diff --git a/ui/app/controllers/CControllerPopupSlaExcludedDowntimeEdit.php b/ui/app/controllers/CControllerPopupSlaExcludedDowntimeEdit.php index ffdd31050ee..89714f059d3 100644 --- a/ui/app/controllers/CControllerPopupSlaExcludedDowntimeEdit.php +++ b/ui/app/controllers/CControllerPopupSlaExcludedDowntimeEdit.php @@ -74,7 +74,7 @@ class CControllerPopupSlaExcludedDowntimeEdit extends CController { $form = [ 'name' => $this->getInput('name'), - 'start_time' => $datetime_from->format(DATE_TIME_FORMAT), + 'start_time' => $datetime_from->format(ZBX_DATE_TIME), 'duration_days' => $interval->days, 'duration_hours' => $interval->h, 'duration_minutes' => $interval->i @@ -83,7 +83,7 @@ class CControllerPopupSlaExcludedDowntimeEdit extends CController { else { $form = [ 'name' => '', - 'start_time' => date(DATE_TIME_FORMAT, strtotime('tomorrow')), + 'start_time' => date(ZBX_DATE_TIME, strtotime('tomorrow')), 'duration_days' => 0, 'duration_hours' => 1, 'duration_minutes' => 0 diff --git a/ui/app/controllers/CControllerSlaCreate.php b/ui/app/controllers/CControllerSlaCreate.php index 76e35d6243a..df9dfaeb666 100644 --- a/ui/app/controllers/CControllerSlaCreate.php +++ b/ui/app/controllers/CControllerSlaCreate.php @@ -26,11 +26,6 @@ class CControllerSlaCreate extends CControllerSlaCreateUpdate { */ private $schedule = []; - /** - * @var int - */ - private $effective_date; - protected function init(): void { $this->setPostContentType(self::POST_CONTENT_TYPE_JSON); } @@ -44,7 +39,7 @@ class CControllerSlaCreate extends CControllerSlaCreateUpdate { 'schedule_mode' => 'required|in '.implode(',', [CSlaHelper::SCHEDULE_MODE_24X7, CSlaHelper::SCHEDULE_MODE_CUSTOM]), 'schedule_enabled' => 'array', 'schedule_periods' => 'array', - 'effective_date' => 'required|string|not_empty', + 'effective_date' => 'required|abs_date', 'service_tags' => 'required|array', 'description' => 'required|string', 'status' => 'in '.ZBX_SLA_STATUS_ENABLED, @@ -65,16 +60,6 @@ class CControllerSlaCreate extends CControllerSlaCreateUpdate { $ret = false; } } - - try { - $this->effective_date = self::validateEffectiveDate($this->getInput('effective_date'), - 'effective_date' - ); - } - catch (InvalidArgumentException $e) { - info($e->getMessage()); - $ret = false; - } } if (!$ret) { @@ -94,11 +79,17 @@ class CControllerSlaCreate extends CControllerSlaCreateUpdate { } /** - * @throws APIException + * @throws Exception */ protected function doAction(): void { + $parser = new CAbsoluteTimeParser(); + $parser->parse($this->getInput('effective_date')); + $effective_date = $parser + ->getDateTime(true, new DateTimeZone('UTC')) + ->getTimestamp(); + $sla = [ - 'effective_date' => $this->effective_date, + 'effective_date' => $effective_date, 'status' => $this->hasInput('status') ? ZBX_SLA_STATUS_ENABLED : ZBX_SLA_STATUS_DISABLED, 'schedule' => $this->schedule, 'service_tags' => [], diff --git a/ui/app/controllers/CControllerSlaCreateUpdate.php b/ui/app/controllers/CControllerSlaCreateUpdate.php index 0c04a1d8e8a..df69fb15652 100644 --- a/ui/app/controllers/CControllerSlaCreateUpdate.php +++ b/ui/app/controllers/CControllerSlaCreateUpdate.php @@ -22,33 +22,6 @@ abstract class CControllerSlaCreateUpdate extends CController { /** - * @param string $effective_date - * @param string $field_name - * - * @throws Exception - * - * @return int - */ - protected static function validateEffectiveDate(string $effective_date, string $field_name): int { - $datetime = DateTime::createFromFormat('!'.DATE_FORMAT, $effective_date, new DateTimeZone('UTC')); - $last_errors = DateTime::getLastErrors(); - - if ($datetime === false || $last_errors['warning_count'] > 0 || $last_errors['error_count'] > 0) { - throw new InvalidArgumentException( - _s('Incorrect value for field "%1$s": %2$s.', $field_name, _('a date is expected')) - ); - } - - if ($datetime->getTimestamp() < 0 || $datetime->getTimestamp() > ZBX_MAX_DATE) { - throw new InvalidArgumentException(_s('Incorrect value for field "%1$s": %2$s.', $field_name, - _s('a date not later than %1$s is expected', zbx_date2str(DATE_FORMAT, ZBX_MAX_DATE, 'UTC')) - )); - } - - return $datetime->getTimestamp(); - } - - /** * @param array $schedule_enabled * @param array $schedule_periods * diff --git a/ui/app/controllers/CControllerSlaExcludedDowntimeValidate.php b/ui/app/controllers/CControllerSlaExcludedDowntimeValidate.php index 1f6bb909b11..4300f59955f 100644 --- a/ui/app/controllers/CControllerSlaExcludedDowntimeValidate.php +++ b/ui/app/controllers/CControllerSlaExcludedDowntimeValidate.php @@ -21,16 +21,6 @@ class CControllerSlaExcludedDowntimeValidate extends CController { - /** - * @var int - */ - private $period_from; - - /** - * @var int - */ - private $period_to; - protected function init(): void { $this->setPostContentType(self::POST_CONTENT_TYPE_JSON); } @@ -42,7 +32,7 @@ class CControllerSlaExcludedDowntimeValidate extends CController { $fields = [ 'row_index' => 'required|int32', 'name' => 'required|db sla.name|not_empty', - 'start_time' => 'required|string|not_empty', + 'start_time' => 'required|abs_time', 'duration_days' => 'required|ge 0', 'duration_hours' => 'required|in '.implode(',', range(0, 23)), 'duration_minutes' => 'required|in '.implode(',', range(0, 59)) @@ -50,56 +40,6 @@ class CControllerSlaExcludedDowntimeValidate extends CController { $ret = $this->validateInput($fields); - if ($ret) { - $datetime_from = DateTime::createFromFormat('!'.DATE_TIME_FORMAT, $this->getInput('start_time')); - $last_errors = DateTime::getLastErrors(); - - if ($datetime_from === false || $last_errors['warning_count'] > 0 || $last_errors['error_count'] > 0) { - error(_s('Incorrect value for field "%1$s": %2$s.', 'start_time', _('a time is expected'))); - $ret = false; - } - - $this->period_from = $datetime_from->getTimestamp(); - - if ($this->period_from < 0 || $this->period_from > ZBX_MAX_DATE) { - error(_s('Incorrect value for field "%1$s": %2$s.', 'start_time', - _s('a time not later than %1$s is expected', zbx_date2str(DATE_TIME_FORMAT, ZBX_MAX_DATE)) - )); - - $ret = false; - } - - if ($ret) { - $duration_days = $this->getInput('duration_days'); - $duration_hours = $this->getInput('duration_hours'); - $duration_minutes = $this->getInput('duration_minutes'); - - try { - $duration = new DateInterval("P{$duration_days}DT{$duration_hours}H{$duration_minutes}M"); - - $datetime_to = clone $datetime_from; - $datetime_to->add($duration); - - $this->period_to = $datetime_to->getTimestamp(); - - if ($this->period_to <= $this->period_from || $this->period_to > ZBX_MAX_DATE) { - error(_s('Incorrect value for field "%1$s": %2$s.', 'duration', - _s('a time not later than %1$s is expected', zbx_date2str(DATE_TIME_FORMAT, ZBX_MAX_DATE)) - )); - - $ret = false; - } - } - catch (Exception $e) { - error(_s('Incorrect value for field "%1$s": %2$s.', 'duration', - _s('a time not later than %1$s is expected', zbx_date2str(DATE_TIME_FORMAT, ZBX_MAX_DATE)) - )); - - $ret = false; - } - } - } - if (!$ret) { $this->setResponse( (new CControllerResponseData([ @@ -119,14 +59,30 @@ class CControllerSlaExcludedDowntimeValidate extends CController { * @throws Exception */ protected function doAction(): void { + $parser = new CAbsoluteTimeParser(); + $parser->parse($this->getInput('start_time')); + $datetime_from = $parser->getDateTime(true); + + $duration_days = $this->getInput('duration_days'); + $duration_hours = $this->getInput('duration_hours'); + $duration_minutes = $this->getInput('duration_minutes'); + + $duration = new DateInterval("P{$duration_days}DT{$duration_hours}H{$duration_minutes}M"); + + $datetime_to = clone $datetime_from; + $datetime_to->add($duration); + + $period_from = $datetime_from->getTimestamp(); + $period_to = $datetime_to->getTimestamp(); + $data = [ 'body' => [ 'row_index' => $this->getInput('row_index'), 'name' => $this->getInput('name'), - 'period_from' => $this->period_from, - 'period_to' => $this->period_to, - 'start_time' => zbx_date2str(DATE_TIME_FORMAT, $this->period_from), - 'duration' => convertUnitsS($this->period_to - $this->period_from, true) + 'period_from' => $period_from, + 'period_to' => $period_to, + 'start_time' => zbx_date2str(DATE_TIME_FORMAT, $period_from), + 'duration' => convertUnitsS($period_to - $period_from, true) ] ]; diff --git a/ui/app/controllers/CControllerSlaReportList.php b/ui/app/controllers/CControllerSlaReportList.php index 62be110e14b..9ec25d1006a 100644 --- a/ui/app/controllers/CControllerSlaReportList.php +++ b/ui/app/controllers/CControllerSlaReportList.php @@ -44,6 +44,28 @@ class CControllerSlaReportList extends CController { $ret = $this->validateInput($fields); + if ($ret) { + if ($this->getInput('filter_date_from', '') !== '') { + $fields['filter_date_from'] = 'abs_date'; + } + + if ($this->getInput('filter_date_to', '') !== '') { + $fields['filter_date_to'] = 'abs_date'; + } + + if ($fields) { + $validator = new CNewValidator($this->getInputAll(), $fields); + + foreach ($validator->getAllErrors() as $error) { + info($error); + } + + if ($validator->isErrorFatal() || $validator->isError()) { + $ret = false; + } + } + } + if (!$ret) { $this->setResponse(new CControllerResponseFatal()); } @@ -56,7 +78,7 @@ class CControllerSlaReportList extends CController { } /** - * @throws APIException + * @throws Exception */ protected function doAction(): void { if ($this->hasInput('filter_set')) { @@ -133,45 +155,21 @@ class CControllerSlaReportList extends CController { $period_from = null; if ($filter['date_from'] !== '') { - $date_from = DateTime::createFromFormat('!'.DATE_FORMAT, $filter['date_from'], new DateTimeZone('UTC')); - $last_errors = DateTime::getLastErrors(); - - if ($date_from === false || $last_errors['warning_count'] > 0 || $last_errors['error_count'] > 0) { - error(_s('Incorrect value for field "%1$s": %2$s.', _('From'), _('a date is expected'))); - } - else { - $period_from = $date_from->getTimestamp(); - - if ($period_from < 0 || $period_from > ZBX_MAX_DATE) { - $period_from = null; - - error(_s('Incorrect value for field "%1$s": %2$s.', _('From'), - _s('a date not later than %1$s is expected', zbx_date2str(DATE_FORMAT, ZBX_MAX_DATE)) - )); - } - } + $parser = new CAbsoluteTimeParser(); + $parser->parse($filter['date_from']); + $period_from = $parser + ->getDateTime(true, new DateTimeZone('UTC')) + ->getTimestamp(); } $period_to = null; if ($filter['date_to'] !== '') { - $date_to = DateTime::createFromFormat('!'.DATE_FORMAT, $filter['date_to'], new DateTimeZone('UTC')); - $last_errors = DateTime::getLastErrors(); - - if ($date_to === false || $last_errors['warning_count'] > 0 || $last_errors['error_count'] > 0) { - error(_s('Incorrect value for field "%1$s": %2$s.', _('To'), _('a date is expected'))); - } - else { - $period_to = $date_to->getTimestamp(); - - if ($period_to < 0 || $period_to > ZBX_MAX_DATE) { - $period_to = null; - - error(_s('Incorrect value for field "%1$s": %2$s.', _('To'), - _s('a date not later than %1$s is expected', zbx_date2str(DATE_FORMAT, ZBX_MAX_DATE)) - )); - } - } + $parser = new CAbsoluteTimeParser(); + $parser->parse($filter['date_to']); + $period_to = $parser + ->getDateTime(false, new DateTimeZone('UTC')) + ->getTimestamp(); } if ($period_from !== null && $period_to !== null && $period_to <= $period_from) { diff --git a/ui/app/controllers/CControllerSlaUpdate.php b/ui/app/controllers/CControllerSlaUpdate.php index 0da096563ff..5720fb14ab9 100644 --- a/ui/app/controllers/CControllerSlaUpdate.php +++ b/ui/app/controllers/CControllerSlaUpdate.php @@ -26,11 +26,6 @@ class CControllerSlaUpdate extends CControllerSlaCreateUpdate { */ private $schedule = []; - /** - * @var int - */ - private $effective_date; - protected function init(): void { $this->setPostContentType(self::POST_CONTENT_TYPE_JSON); } @@ -45,7 +40,7 @@ class CControllerSlaUpdate extends CControllerSlaCreateUpdate { 'schedule_mode' => 'required|in '.implode(',', [CSlaHelper::SCHEDULE_MODE_24X7, CSlaHelper::SCHEDULE_MODE_CUSTOM]), 'schedule_enabled' => 'array', 'schedule_periods' => 'array', - 'effective_date' => 'required|string|not_empty', + 'effective_date' => 'required|abs_date', 'service_tags' => 'required|array', 'description' => 'required|string', 'status' => 'in '.ZBX_SLA_STATUS_ENABLED, @@ -66,16 +61,6 @@ class CControllerSlaUpdate extends CControllerSlaCreateUpdate { $ret = false; } } - - try { - $this->effective_date = self::validateEffectiveDate($this->getInput('effective_date'), - 'effective_date' - ); - } - catch (InvalidArgumentException $e) { - info($e->getMessage()); - $ret = false; - } } if (!$ret) { @@ -105,11 +90,17 @@ class CControllerSlaUpdate extends CControllerSlaCreateUpdate { } /** - * @throws APIException + * @throws Exception */ protected function doAction(): void { + $parser = new CAbsoluteTimeParser(); + $parser->parse($this->getInput('effective_date')); + $effective_date = $parser + ->getDateTime(true, new DateTimeZone('UTC')) + ->getTimestamp(); + $sla = [ - 'effective_date' => $this->effective_date, + 'effective_date' => $effective_date, 'status' => $this->hasInput('status') ? ZBX_SLA_STATUS_ENABLED : ZBX_SLA_STATUS_DISABLED, 'schedule' => $this->schedule, 'service_tags' => [], diff --git a/ui/app/controllers/CControllerTokenCreate.php b/ui/app/controllers/CControllerTokenCreate.php index 58efb9cc7f0..b0925ae9e94 100644 --- a/ui/app/controllers/CControllerTokenCreate.php +++ b/ui/app/controllers/CControllerTokenCreate.php @@ -27,7 +27,7 @@ class CControllerTokenCreate extends CController { 'description' => 'db token.description', 'userid' => 'db users.userid|required', 'expires_state' => 'in 0,1|required', - 'expires_at' => 'range_time', + 'expires_at' => 'abs_time', 'status' => 'db token.status|required|in '.ZBX_AUTH_TOKEN_ENABLED.','.ZBX_AUTH_TOKEN_DISABLED, 'action_src' => 'fatal|required|in token.edit,user.token.edit', 'action_dst' => 'fatal|required|in token.view,user.token.view' @@ -61,12 +61,23 @@ class CControllerTokenCreate extends CController { return $this->checkAccess(CRoleHelper::ACTIONS_MANAGE_API_TOKENS); } + /** + * @throws Exception + */ protected function doAction() { $this->getInputs($token, ['name', 'description', 'userid', 'expires_at', 'status']); - $token['expires_at'] = $this->getInput('expires_state') - ? (new DateTime($token['expires_at']))->getTimestamp() - : 0; + if ($this->getInput('expires_state')) { + $parser = new CAbsoluteTimeParser(); + $parser->parse($token['expires_at']); + + $token['expires_at'] = $parser + ->getDateTime(true) + ->getTimestamp(); + } + else { + $token['expires_at'] = 0; + } $result = API::Token()->create($token); diff --git a/ui/app/controllers/CControllerTokenEdit.php b/ui/app/controllers/CControllerTokenEdit.php index 405a44d822a..608a96920b5 100644 --- a/ui/app/controllers/CControllerTokenEdit.php +++ b/ui/app/controllers/CControllerTokenEdit.php @@ -69,7 +69,7 @@ class CControllerTokenEdit extends CController { $data = $tokens[0]; if ($data['expires_at'] != 0) { - $data['expires_at'] = date(DATE_TIME_FORMAT_SECONDS, (int) $data['expires_at']); + $data['expires_at'] = date(ZBX_FULL_DATE_TIME, (int) $data['expires_at']); $data['expires_state'] = '1'; } else { diff --git a/ui/app/controllers/CControllerTokenUpdate.php b/ui/app/controllers/CControllerTokenUpdate.php index 052c0a59ef2..c155cbc4390 100644 --- a/ui/app/controllers/CControllerTokenUpdate.php +++ b/ui/app/controllers/CControllerTokenUpdate.php @@ -27,7 +27,7 @@ class CControllerTokenUpdate extends CController { 'name' => 'db token.name|required|not_empty', 'description' => 'db token.description', 'expires_state' => 'in 0,1|required', - 'expires_at' => 'range_time', + 'expires_at' => 'abs_time', 'status' => 'db token.status|required|in '.ZBX_AUTH_TOKEN_ENABLED.','.ZBX_AUTH_TOKEN_DISABLED, 'action_src' => 'fatal|required|in token.edit,user.token.edit', 'action_dst' => 'fatal|required|in token.list,user.token.list,token.view,user.token.view', @@ -67,9 +67,17 @@ class CControllerTokenUpdate extends CController { protected function doAction() { $this->getInputs($token, ['tokenid', 'name', 'description', 'expires_at', 'status']); - $token['expires_at'] = $this->getInput('expires_state') - ? (new DateTime($token['expires_at']))->getTimestamp() - : 0; + if ($this->getInput('expires_state')) { + $parser = new CAbsoluteTimeParser(); + $parser->parse($token['expires_at']); + + $token['expires_at'] = $parser + ->getDateTime(true) + ->getTimestamp(); + } + else { + $token['expires_at'] = 0; + } $result = API::Token()->update($token); diff --git a/ui/app/controllers/CControllerUserTokenEdit.php b/ui/app/controllers/CControllerUserTokenEdit.php index 881689869bb..a84d7dd5f31 100644 --- a/ui/app/controllers/CControllerUserTokenEdit.php +++ b/ui/app/controllers/CControllerUserTokenEdit.php @@ -66,7 +66,7 @@ class CControllerUserTokenEdit extends CController { $data = $tokens[0]; if ($data['expires_at'] != 0) { - $data['expires_at'] = date(DATE_TIME_FORMAT_SECONDS, (int) $data['expires_at']); + $data['expires_at'] = date(ZBX_FULL_DATE_TIME, (int) $data['expires_at']); $data['expires_state'] = '1'; } else { diff --git a/ui/app/views/administration.token.edit.php b/ui/app/views/administration.token.edit.php index 71dd551a961..b3593d0e0b8 100644 --- a/ui/app/views/administration.token.edit.php +++ b/ui/app/views/administration.token.edit.php @@ -80,8 +80,8 @@ $token_from_list = (new CFormList()) ) ->addRow((new CLabel(_('Expires at')))->setAsteriskMark(), (new CDateSelector('expires_at', $data['expires_at'])) - ->setDateFormat(DATE_TIME_FORMAT_SECONDS) - ->setPlaceholder(DATE_TIME_FORMAT_SECONDS_PLACEHOLDER) + ->setDateFormat(ZBX_FULL_DATE_TIME) + ->setPlaceholder(_('YYYY-MM-DD hh:mm:ss')) ->setAriaRequired(), 'expires-at-row' ) diff --git a/ui/app/views/administration.user.token.edit.php b/ui/app/views/administration.user.token.edit.php index 5fc706bd1bc..9193b1185ab 100644 --- a/ui/app/views/administration.user.token.edit.php +++ b/ui/app/views/administration.user.token.edit.php @@ -62,8 +62,8 @@ $token_from_list = (new CFormList()) ) ->addRow((new CLabel(_('Expires at')))->setAsteriskMark(), (new CDateSelector('expires_at', $data['expires_at'])) - ->setDateFormat(DATE_TIME_FORMAT_SECONDS) - ->setPlaceholder(DATE_TIME_FORMAT_SECONDS_PLACEHOLDER) + ->setDateFormat(ZBX_FULL_DATE_TIME) + ->setPlaceholder(_('YYYY-MM-DD hh:mm:ss')) ->setAriaRequired(), 'expires-at-row' ) diff --git a/ui/app/views/popup.sla.edit.php b/ui/app/views/popup.sla.edit.php index 12ea5e8847f..dbce2e17119 100644 --- a/ui/app/views/popup.sla.edit.php +++ b/ui/app/views/popup.sla.edit.php @@ -113,8 +113,8 @@ $sla_tab = (new CFormGrid()) (new CLabel(_('Effective date'), 'effective_date'))->setAsteriskMark(), new CFormField( (new CDateSelector('effective_date', $data['form']['effective_date'])) - ->setDateFormat(DATE_FORMAT) - ->setPlaceholder(DATE_FORMAT_PLACEHOLDER) + ->setDateFormat(ZBX_DATE) + ->setPlaceholder(_('YYYY-MM-DD')) ->setAriaRequired() ) ]) diff --git a/ui/app/views/popup.sla.excludeddowntime.edit.php b/ui/app/views/popup.sla.excludeddowntime.edit.php index abaacf4e9a0..2cf70a41055 100644 --- a/ui/app/views/popup.sla.excludeddowntime.edit.php +++ b/ui/app/views/popup.sla.excludeddowntime.edit.php @@ -49,8 +49,8 @@ $form_grid = (new CFormGrid()) (new CLabel(_('Start time'), 'start_time'))->setAsteriskMark(), new CFormField( (new CDateSelector('start_time', $data['form']['start_time'])) - ->setDateFormat(DATE_TIME_FORMAT) - ->setPlaceholder(DATE_TIME_FORMAT_PLACEHOLDER) + ->setDateFormat(ZBX_DATE_TIME) + ->setPlaceholder(_('YYYY-MM-DD hh:mm')) ->setAriaRequired() ) ]) diff --git a/ui/app/views/slareport.list.php b/ui/app/views/slareport.list.php index 6d4a90843d0..42dd9385a82 100644 --- a/ui/app/views/slareport.list.php +++ b/ui/app/views/slareport.list.php @@ -76,14 +76,14 @@ $filter = (new CFilter()) new CLabel(_('From'), 'filter_date_from'), new CFormField( (new CDateSelector('filter_date_from', $data['filter']['date_from'])) - ->setDateFormat(DATE_FORMAT) - ->setPlaceholder(DATE_FORMAT_PLACEHOLDER) + ->setDateFormat(ZBX_DATE) + ->setPlaceholder(_('YYYY-MM-DD')) ), new CLabel(_('To'), 'filter_date_to'), new CFormField( (new CDateSelector('filter_date_to', $data['filter']['date_to'])) - ->setDateFormat(DATE_FORMAT) - ->setPlaceholder(DATE_FORMAT_PLACEHOLDER) + ->setDateFormat(ZBX_DATE) + ->setPlaceholder(_('YYYY-MM-DD')) ) ]) ]); diff --git a/ui/include/classes/parsers/CAbsoluteTimeParser.php b/ui/include/classes/parsers/CAbsoluteTimeParser.php index ea7d847267b..f2f0f1775f4 100644 --- a/ui/include/classes/parsers/CAbsoluteTimeParser.php +++ b/ui/include/classes/parsers/CAbsoluteTimeParser.php @@ -106,19 +106,20 @@ class CAbsoluteTimeParser extends CParser { } /** - * Returns date in "YYYY-MM-DD hh:mm:ss" format. + * Get DateTime object with its value set to either start or end of the period derived from the date/time specified. * - * @param bool $is_start If set to true date will be modified to lowest value, example "2018" will be returned - * as "2018-01-01 00:00:00", otherwise "2018-12-31 23:59:59". + * @param $is_start + * @param DateTimeZone|null $timezone * + * @throws Exception * @return DateTime|null */ - public function getDateTime($is_start) { + public function getDateTime($is_start, DateTimeZone $timezone = null) { if ($this->date === '') { return null; } - $date = new DateTime($this->date); + $date = new DateTime($this->date, $timezone); if ($is_start) { return $date; diff --git a/ui/include/classes/parsers/CValidationRule.php b/ui/include/classes/parsers/CValidationRule.php index 0c44e2f6d40..5cac7b0f0c4 100644 --- a/ui/include/classes/parsers/CValidationRule.php +++ b/ui/include/classes/parsers/CValidationRule.php @@ -42,7 +42,6 @@ class CValidationRule { $pos = 0; $state = self::STATE_BEGIN; $rules = []; - $is_empty = true; while (isset($buffer[$pos])) { switch ($state) { @@ -53,29 +52,30 @@ class CValidationRule { break; default: - $is_empty = false; $rule = []; - if (!$this->parseString($buffer, $pos, $rule) // string - && !$this->parseRangeTime($buffer, $pos, $rule) // range time - && !$this->parseTimePeriods($buffer, $pos, $rule) // time periods - && !$this->parseTimeUnit($buffer, $pos, $rule) // time unit - && !$this->parseRgb($buffer, $pos, $rule) // rgb - && !$this->parseRequired($buffer, $pos, $rule) // required - && !$this->parseNotEmpty($buffer, $pos, $rule) // not_empty - && !$this->parseLE($buffer, $pos, $rule) // le - && !$this->parseJson($buffer, $pos, $rule) // json - && !$this->parseInt32($buffer, $pos, $rule) // int32 - && !$this->parseIn($buffer, $pos, $rule) // in - && !$this->parseId($buffer, $pos, $rule) // id - && !$this->parseGE($buffer, $pos, $rule) // ge - && !$this->parseFatal($buffer, $pos, $rule) // fatal - && !$this->parseDB($buffer, $pos, $rule) // db - && !$this->parseArrayId($buffer, $pos, $rule) // array_id - && !$this->parseArrayDB($buffer, $pos, $rule) // array_db - && !$this->parseArray($buffer, $pos, $rule) // array - && !$this->parseFlags($buffer, $pos, $rule) // flags - && !$this->parseCuid($buffer, $pos, $rule)) { // cuid + if (!$this->parseString($buffer, $pos, $rule) + && !$this->parseRangeTime($buffer, $pos, $rule) + && !$this->parseAbsDate($buffer, $pos, $rule) + && !$this->parseAbsTime($buffer, $pos, $rule) + && !$this->parseTimePeriods($buffer, $pos, $rule) + && !$this->parseTimeUnit($buffer, $pos, $rule) + && !$this->parseRgb($buffer, $pos, $rule) + && !$this->parseRequired($buffer, $pos, $rule) + && !$this->parseNotEmpty($buffer, $pos, $rule) + && !$this->parseLE($buffer, $pos, $rule) + && !$this->parseJson($buffer, $pos, $rule) + && !$this->parseInt32($buffer, $pos, $rule) + && !$this->parseIn($buffer, $pos, $rule) + && !$this->parseId($buffer, $pos, $rule) + && !$this->parseGE($buffer, $pos, $rule) + && !$this->parseFatal($buffer, $pos, $rule) + && !$this->parseDB($buffer, $pos, $rule) + && !$this->parseArrayId($buffer, $pos, $rule) + && !$this->parseArrayDB($buffer, $pos, $rule) + && !$this->parseArray($buffer, $pos, $rule) + && !$this->parseFlags($buffer, $pos, $rule) + && !$this->parseCuid($buffer, $pos, $rule)) { // incorrect validation rule break 3; } @@ -176,6 +176,38 @@ class CValidationRule { } /** + * abs_date + * + * 'abs_date' => true + */ + private function parseAbsDate($buffer, &$pos, &$rules) { + if (strncmp(substr($buffer, $pos), 'abs_date', 8) != 0) { + return false; + } + + $pos += 8; + $rules['abs_date'] = true; + + return true; + } + + /** + * abs_time + * + * 'abs_time' => true + */ + private function parseAbsTime($buffer, &$pos, &$rules) { + if (strncmp(substr($buffer, $pos), 'abs_time', 8) != 0) { + return false; + } + + $pos += 8; + $rules['abs_time'] = true; + + return true; + } + + /** * range_time * * 'time_periods' => true diff --git a/ui/include/classes/validators/CNewValidator.php b/ui/include/classes/validators/CNewValidator.php index 524832829b0..ab7a23ab9c1 100644 --- a/ui/include/classes/validators/CNewValidator.php +++ b/ui/include/classes/validators/CNewValidator.php @@ -242,6 +242,36 @@ class CNewValidator { } break; + case 'abs_date': + $absolute_time_parser = new CAbsoluteTimeParser(); + + $has_errors = !is_string($value) + || $absolute_time_parser->parse($value) != CParser::PARSE_SUCCESS + || $absolute_time_parser->getDateTime(true)->format('H:i:s') !== '00:00:00'; + + if ($has_errors) { + $this->addError($fatal, + _s('Incorrect value for field "%1$s": %2$s.', $field, _('a date is expected')) + ); + + return false; + } + break; + + case 'abs_time': + $absolute_time_parser = new CAbsoluteTimeParser(); + + $has_errors = !is_string($value) || $absolute_time_parser->parse($value) != CParser::PARSE_SUCCESS; + + if ($has_errors) { + $this->addError($fatal, + _s('Incorrect value for field "%1$s": %2$s.', $field, _('a time is expected')) + ); + + return false; + } + break; + case 'time_periods': if ($this->time_periods_parser === null) { $this->time_periods_parser = new CTimePeriodsParser(['usermacros' => true]); diff --git a/ui/include/classes/widgets/fields/CWidgetFieldDatePicker.php b/ui/include/classes/widgets/fields/CWidgetFieldDatePicker.php index 444242293b9..e0c7a6a348d 100644 --- a/ui/include/classes/widgets/fields/CWidgetFieldDatePicker.php +++ b/ui/include/classes/widgets/fields/CWidgetFieldDatePicker.php @@ -22,11 +22,6 @@ class CWidgetFieldDatePicker extends CWidgetField { /** - * @var array - */ - private $date_time_formats; - - /** * @var bool */ private $is_date_only; @@ -34,13 +29,11 @@ class CWidgetFieldDatePicker extends CWidgetField { /** * @param string $name * @param string $label - * @param array $date_time_formats * @param bool $is_date_only */ - public function __construct(string $name, string $label, array $date_time_formats, bool $is_date_only) { + public function __construct(string $name, string $label, bool $is_date_only) { parent::__construct($name, $label); - $this->date_time_formats = $date_time_formats; $this->is_date_only = $is_date_only; $this->setSaveType(ZBX_WIDGET_FIELD_TYPE_STR); @@ -75,20 +68,6 @@ class CWidgetFieldDatePicker extends CWidgetField { } /** - * @return array - */ - public function getDateTimeFormats(): array { - return $this->date_time_formats; - } - - /** - * @return bool - */ - public function isDateOnly(): bool { - return $this->is_date_only; - } - - /** * @param bool $strict * * @return array @@ -107,11 +86,16 @@ class CWidgetFieldDatePicker extends CWidgetField { return []; } - foreach ($this->getDateTimeFormats() as $datetime_format) { - $datetime = DateTime::createFromFormat('!'.$datetime_format, $value); - $last_errors = DateTime::getLastErrors(); + $absolute_time_parser = new CAbsoluteTimeParser(); + + if ($absolute_time_parser->parse($value) == CParser::PARSE_SUCCESS) { + $has_errors = false; + + if ($this->is_date_only) { + $has_errors = $absolute_time_parser->getDateTime(true)->format('H:i:s') !== '00:00:00'; + } - if ($datetime !== false && $last_errors['warning_count'] == 0 && $last_errors['error_count'] == 0) { + if (!$has_errors) { $this->setValue($value); return []; @@ -123,7 +107,7 @@ class CWidgetFieldDatePicker extends CWidgetField { if ($relative_time_parser->parse($value) == CParser::PARSE_SUCCESS) { $has_errors = false; - if ($this->isDateOnly()) { + if ($this->is_date_only) { foreach ($relative_time_parser->getTokens() as $token) { if ($token['suffix'] === 'h' || $token['suffix'] === 'm' || $token['suffix'] === 's') { $has_errors = true; @@ -143,7 +127,7 @@ class CWidgetFieldDatePicker extends CWidgetField { return [ _s('Invalid parameter "%1$s": %2$s.', $label, - $this->isDateOnly() ? _('a date is expected') : _('a time is expected') + $this->is_date_only ? _('a date is expected') : _('a time is expected') ) ]; } diff --git a/ui/include/classes/widgets/forms/CWidgetFormSlaReport.php b/ui/include/classes/widgets/forms/CWidgetFormSlaReport.php index 584c72c9d97..85c57ebdb31 100644 --- a/ui/include/classes/widgets/forms/CWidgetFormSlaReport.php +++ b/ui/include/classes/widgets/forms/CWidgetFormSlaReport.php @@ -57,7 +57,7 @@ class CWidgetFormSlaReport extends CWidgetForm $this->fields[$field_show_periods->getName()] = $field_show_periods; // Date from. - $field_date_from = new CWidgetFieldDatePicker('date_from', _('From'), [DATE_FORMAT], true); + $field_date_from = new CWidgetFieldDatePicker('date_from', _('From'), true); if (array_key_exists('date_from', $this->data)) { $field_date_from->setValue($this->data['date_from']); @@ -66,7 +66,7 @@ class CWidgetFormSlaReport extends CWidgetForm $this->fields[$field_date_from->getName()] = $field_date_from; // Date to. - $field_date_to = new CWidgetFieldDatePicker('date_to', _('To'), [DATE_FORMAT], true); + $field_date_to = new CWidgetFieldDatePicker('date_to', _('To'), true); if (array_key_exists('date_to', $this->data)) { $field_date_to->setValue($this->data['date_to']); diff --git a/ui/include/classes/widgets/forms/CWidgetFormSvgGraph.php b/ui/include/classes/widgets/forms/CWidgetFormSvgGraph.php index 86da5236b39..5324b7e6243 100644 --- a/ui/include/classes/widgets/forms/CWidgetFormSvgGraph.php +++ b/ui/include/classes/widgets/forms/CWidgetFormSvgGraph.php @@ -76,8 +76,7 @@ class CWidgetFormSvgGraph extends CWidgetForm { $this->fields[$field_graph_time->getName()] = $field_graph_time; // Date from. - $field_time_from = (new CWidgetFieldDatePicker('time_from', _('From'), - [DATE_FORMAT, DATE_TIME_FORMAT, DATE_TIME_FORMAT_SECONDS], false)) + $field_time_from = (new CWidgetFieldDatePicker('time_from', _('From'), false)) ->setDefault('now-1h') ->setFlags(CWidgetField::FLAG_NOT_EMPTY); @@ -91,8 +90,7 @@ class CWidgetFormSvgGraph extends CWidgetForm { $this->fields[$field_time_from->getName()] = $field_time_from; // Time to. - $field_time_to = (new CWidgetFieldDatePicker('time_to', _('To'), - [DATE_FORMAT, DATE_TIME_FORMAT, DATE_TIME_FORMAT_SECONDS], false)) + $field_time_to = (new CWidgetFieldDatePicker('time_to', _('To'), false)) ->setDefault('now') ->setFlags(CWidgetField::FLAG_NOT_EMPTY); diff --git a/ui/include/classes/widgets/views/widget.slareport.form.view.php b/ui/include/classes/widgets/views/widget.slareport.form.view.php index 9d08487e7f3..470cf5ef349 100644 --- a/ui/include/classes/widgets/views/widget.slareport.form.view.php +++ b/ui/include/classes/widgets/views/widget.slareport.form.view.php @@ -61,16 +61,16 @@ $form_list->addRow( $form_list->addRow( CWidgetHelper::getLabel($fields['date_from']), CWidgetHelper::getDatePicker($fields['date_from']) - ->setDateFormat(DATE_FORMAT) - ->setPlaceholder(DATE_FORMAT_PLACEHOLDER) + ->setDateFormat(ZBX_DATE) + ->setPlaceholder(_('YYYY-MM-DD')) ); // Date to. $form_list->addRow( CWidgetHelper::getLabel($fields['date_to']), CWidgetHelper::getDatePicker($fields['date_to']) - ->setDateFormat(DATE_FORMAT) - ->setPlaceholder(DATE_FORMAT_PLACEHOLDER) + ->setDateFormat(ZBX_DATE) + ->setPlaceholder(_('YYYY-MM-DD')) ); $form->addItem($form_list); diff --git a/ui/include/classes/widgets/views/widget.svggraph.form.view.php b/ui/include/classes/widgets/views/widget.svggraph.form.view.php index 0e926ef7082..0ea1f74ae2a 100644 --- a/ui/include/classes/widgets/views/widget.svggraph.form.view.php +++ b/ui/include/classes/widgets/views/widget.svggraph.form.view.php @@ -221,14 +221,14 @@ $tab_time_period = (new CFormList()) ->addRow( CWidgetHelper::getLabel($fields['time_from']), CWidgetHelper::getDatePicker($fields['time_from']) - ->setDateFormat(DATE_TIME_FORMAT_SECONDS) - ->setPlaceholder(DATE_TIME_FORMAT_SECONDS_PLACEHOLDER) + ->setDateFormat(ZBX_FULL_DATE_TIME) + ->setPlaceholder(_('YYYY-MM-DD hh:mm:ss')) ) ->addRow( CWidgetHelper::getLabel($fields['time_to']), CWidgetHelper::getDatePicker($fields['time_to']) - ->setDateFormat(DATE_TIME_FORMAT_SECONDS) - ->setPlaceholder(DATE_TIME_FORMAT_SECONDS_PLACEHOLDER) + ->setDateFormat(ZBX_FULL_DATE_TIME) + ->setPlaceholder(_('YYYY-MM-DD hh:mm:ss')) ); // Create 'Axes' tab. diff --git a/ui/include/translateDefines.inc.php b/ui/include/translateDefines.inc.php index 48699b95b5f..e196838d91d 100644 --- a/ui/include/translateDefines.inc.php +++ b/ui/include/translateDefines.inc.php @@ -29,12 +29,9 @@ define('UNRESOLVED_MACRO_STRING', '*'._('UNKNOWN').'*'); * Date formats must be compatible with the CDate class (class.cdate.js). */ define('DATE_TIME_FORMAT_SECONDS', _('Y-m-d H:i:s')); -define('DATE_TIME_FORMAT_SECONDS_PLACEHOLDER', _('YYYY-MM-DD hh:mm:ss')); define('DATE_TIME_FORMAT', _('Y-m-d H:i')); -define('DATE_TIME_FORMAT_PLACEHOLDER', _('YYYY-MM-DD hh:mm')); define('DATE_TIME_FORMAT_SHORT', _('m-d H:i')); define('DATE_FORMAT', _('Y-m-d')); -define('DATE_FORMAT_PLACEHOLDER', _('YYYY-MM-DD')); define('DATE_FORMAT_SHORT', _('m-d')); define('TIME_FORMAT_SECONDS', _('H:i:s')); define('TIME_FORMAT', _('H:i')); |