1<?php declare(strict_types=1);
2/*
3** Zabbix
4** Copyright (C) 2001-2021 Zabbix SIA
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19**/
20
21
22class CControllerCorrelationUpdate extends CController {
23
24	protected function checkInput() {
25		$fields = [
26			'correlationid' => 'db correlation.correlationid|required',
27			'name'          => 'db correlation.name|required|not_empty',
28			'description'   => 'db correlation.description',
29			'evaltype'      => 'db correlation.evaltype|required|in '.implode(',', [CONDITION_EVAL_TYPE_AND_OR, CONDITION_EVAL_TYPE_AND, CONDITION_EVAL_TYPE_OR, CONDITION_EVAL_TYPE_EXPRESSION]),
30			'status'        => 'db correlation.status|required|in '.implode(',', [ZBX_CORRELATION_ENABLED, ZBX_CORRELATION_DISABLED]),
31			'formula'       => 'db correlation.formula',
32			'op_close_new'  => 'in 1',
33			'op_close_old'  => 'in 1',
34			'conditions'    => 'array',
35			'form_refresh'  => 'int32'
36		];
37
38		$ret = $this->validateInput($fields);
39		$error = $this->getValidationError();
40
41		if (!$ret) {
42			switch ($error) {
43				case self::VALIDATION_ERROR:
44					$response = new CControllerResponseRedirect((new CUrl('zabbix.php'))
45						->setArgument('action', 'correlation.edit')
46					);
47					$response->setFormData($this->getInputAll());
48					CMessageHelper::setErrorTitle(_('Cannot update correlation'));
49					$this->setResponse($response);
50					break;
51
52				case self::VALIDATION_FATAL_ERROR:
53					$this->setResponse(new CControllerResponseFatal());
54					break;
55			}
56		}
57
58		return $ret;
59	}
60
61	protected function checkPermissions() {
62		if (!$this->checkAccess(CRoleHelper::UI_CONFIGURATION_EVENT_CORRELATION)) {
63			return false;
64		}
65
66		return (bool) API::Correlation()->get([
67			'output' => [],
68			'correlationid' => $this->getInput('correlationid'),
69			'editable' => true
70		]);
71	}
72
73	protected function doAction() {
74		$data = [
75			'conditions' => [],
76			'status' => ZBX_CORRELATION_DISABLED
77		];
78
79		$this->getInputs($data, ['correlationid', 'name', 'description', 'evaltype', 'status', 'formula',
80			'op_close_new', 'op_close_old', 'conditions', 'new_condition'
81		]);
82
83		$correlation = [
84			'correlationid' => $data['correlationid'],
85			'name' => $data['name'],
86			'description' => $data['description'],
87			'status' => $data['status'],
88			'filter' => [
89				'evaltype' => $data['evaltype'],
90				'formula' => $data['formula'],
91				'conditions' => $data['conditions']
92			],
93			'operations' => []
94		];
95
96		if ($correlation['filter']['evaltype'] == CONDITION_EVAL_TYPE_EXPRESSION
97				&& count($correlation['filter']['conditions']) < 2) {
98			$correlation['filter']['formula'] = '';
99			$correlation['filter']['evaltype'] = CONDITION_EVAL_TYPE_AND_OR;
100		}
101
102		if (array_key_exists('op_close_old', $data)) {
103			$correlation['operations'][] = ['type' => ZBX_CORR_OPERATION_CLOSE_OLD];
104		}
105
106		if (array_key_exists('op_close_new', $data)) {
107			$correlation['operations'][] = ['type' => ZBX_CORR_OPERATION_CLOSE_NEW];
108		}
109
110		$result = API::Correlation()->update($correlation);
111
112		if ($result) {
113			$response = new CControllerResponseRedirect((new CUrl('zabbix.php'))
114				->setArgument('action', 'correlation.list')
115				->setArgument('page', CPagerHelper::loadPage('correlation.list', null))
116			);
117			$response->setFormData(['uncheck' => '1']);
118			CMessageHelper::setSuccessTitle(_('Correlation updated'));
119		}
120		else {
121			$response = new CControllerResponseRedirect((new CUrl('zabbix.php'))
122				->setArgument('action', 'correlation.edit')
123			);
124			$response->setFormData($this->getInputAll());
125			CMessageHelper::setErrorTitle(_('Cannot update correlation'));
126		}
127
128		$this->setResponse($response);
129	}
130}
131