<?php declare(strict_types = 0);
class CControllerMaintenanceCreate extends CController {
protected function init(): void {
$this->setPostContentType(self::POST_CONTENT_TYPE_JSON);
}
protected function checkInput(): bool {
$fields = [
'name' => 'required|string|not_empty',
'maintenance_type' => 'required|in '.implode(',', [MAINTENANCE_TYPE_NORMAL, MAINTENANCE_TYPE_NODATA]),
'active_since' => 'required|abs_time',
'active_till' => 'required|abs_time',
'timeperiods' => 'required|array',
'groupids' => 'array_id',
'hostids' => 'array_id',
'tags_evaltype' => 'in '.implode(',', [MAINTENANCE_TAG_EVAL_TYPE_AND_OR, MAINTENANCE_TAG_EVAL_TYPE_OR]),
'tags' => 'array',
'description' => 'required|string'
];
$ret = $this->validateInput($fields);
if ($ret) {
if ($this->getInput('maintenance_type') == MAINTENANCE_TYPE_NORMAL) {
$fields = [
'tags_evaltype' => 'required'
];
$validator = new CNewValidator(array_intersect_key($this->getInputAll(), $fields), $fields);
foreach ($validator->getAllErrors() as $error) {
error($error);
}
if ($validator->isErrorFatal() || $validator->isError()) {
$ret = false;
}
}
if (!$this->hasInput('groupids') && !$this->hasInput('hostids')) {
error(_('At least one host group or host must be selected.'));
$ret = false;
}
}
if (!$ret) {
$this->setResponse(
new CControllerResponseData(['main_block' => json_encode([
'error' => [
'title' => _('Cannot create maintenance period'),
'messages' => array_column(get_and_clear_messages(), 'message')
]
])])
);
}
return $ret;
}
protected function checkPermissions(): bool {
return $this->checkAccess(CRoleHelper::UI_CONFIGURATION_MAINTENANCE)
&& $this->checkAccess(CRoleHelper::ACTIONS_EDIT_MAINTENANCE);
}
protected function doAction(): void {
$absolute_time_parser = new CAbsoluteTimeParser();
$absolute_time_parser->parse($this->getInput('active_since'));
$active_since_ts = $absolute_time_parser->getDateTime(true)->getTimestamp();