1<?php
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
22/**
23 * Class containing methods for operations with discovery checks.
24 */
25class CDCheck extends CApiService {
26
27	public const ACCESS_RULES = [
28		'get' => ['min_user_type' => USER_TYPE_ZABBIX_USER]
29	];
30
31	protected $tableName = 'dchecks';
32	protected $tableAlias = 'dc';
33	protected $sortColumns = ['dcheckid', 'druleid'];
34
35	public function get($options) {
36		$result = [];
37
38		$sqlParts = [
39			'select'	=> ['dchecks' => 'dc.dcheckid'],
40			'from'		=> ['dchecks' => 'dchecks dc'],
41			'where'		=> [],
42			'group'		=> [],
43			'order'		=> [],
44			'limit'		=> null
45		];
46
47		$defOptions = [
48			'dcheckids'					=> null,
49			'druleids'					=> null,
50			'dserviceids'				=> null,
51			'editable'					=> false,
52			'nopermissions'				=> null,
53			// filter
54			'filter'					=> null,
55			'search'					=> null,
56			'searchByAny'				=> null,
57			'startSearch'				=> false,
58			'excludeSearch'				=> false,
59			'searchWildcardsEnabled'	=> null,
60			// output
61			'output'					=> API_OUTPUT_EXTEND,
62			'selectDRules'				=> null,
63			'countOutput'				=> false,
64			'groupCount'				=> false,
65			'preservekeys'				=> false,
66			'sortfield'					=> '',
67			'sortorder'					=> '',
68			'limit'						=> null,
69			'limitSelects'				=> null
70		];
71		$options = zbx_array_merge($defOptions, $options);
72
73		if (self::$userData['type'] < USER_TYPE_ZABBIX_ADMIN) {
74			return [];
75		}
76
77// dcheckids
78		if (!is_null($options['dcheckids'])) {
79			zbx_value2array($options['dcheckids']);
80			$sqlParts['where']['dcheckid'] = dbConditionInt('dc.dcheckid', $options['dcheckids']);
81		}
82
83// druleids
84		if (!is_null($options['druleids'])) {
85			zbx_value2array($options['druleids']);
86
87			$sqlParts['where'][] = dbConditionInt('dc.druleid', $options['druleids']);
88
89			if ($options['groupCount']) {
90				$sqlParts['group']['druleid'] = 'dc.druleid';
91			}
92		}
93
94// dserviceids
95		if (!is_null($options['dserviceids'])) {
96			zbx_value2array($options['dserviceids']);
97
98			$sqlParts['from']['dhosts'] = 'dhosts dh';
99			$sqlParts['from']['dservices'] = 'dservices ds';
100
101			$sqlParts['where']['ds'] = dbConditionInt('ds.dserviceid', $options['dserviceids']);
102			$sqlParts['where']['dcdh'] = 'dc.druleid=dh.druleid';
103			$sqlParts['where']['dhds'] = 'dh.dhostid=ds.dhostid';
104
105			if ($options['groupCount']) {
106				$sqlParts['group']['dserviceid'] = 'ds.dserviceid';
107			}
108		}
109
110// filter
111		if (is_array($options['filter'])) {
112			$this->dbFilter('dchecks dc', $options, $sqlParts);
113		}
114
115// search
116		if (is_array($options['search'])) {
117			zbx_db_search('dchecks dc', $options, $sqlParts);
118		}
119
120// limit
121		if (zbx_ctype_digit($options['limit']) && $options['limit']) {
122			$sqlParts['limit'] = $options['limit'];
123		}
124//-------
125
126		$sqlParts = $this->applyQueryOutputOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
127		$sqlParts = $this->applyQuerySortOptions($this->tableName(), $this->tableAlias(), $options, $sqlParts);
128		$res = DBselect(self::createSelectQueryFromParts($sqlParts), $sqlParts['limit']);
129		while ($dcheck = DBfetch($res)) {
130			if ($options['countOutput']) {
131				if ($options['groupCount']) {
132					$result[] = $dcheck;
133				}
134				else {
135					$result = $dcheck['rowscount'];
136				}
137			}
138			else {
139				$result[$dcheck['dcheckid']] = $dcheck;
140			}
141		}
142
143		if ($options['countOutput']) {
144			return $result;
145		}
146
147		if ($result) {
148			$result = $this->addRelatedObjects($options, $result);
149			$result = $this->unsetExtraFields($result, ['druleid'], $options['output']);
150		}
151
152// removing keys (hash -> array)
153		if (!$options['preservekeys']) {
154			$result = zbx_cleanHashes($result);
155		}
156
157		return $result;
158	}
159
160	protected function applyQueryOutputOptions($tableName, $tableAlias, array $options, array $sqlParts) {
161		$sqlParts = parent::applyQueryOutputOptions($tableName, $tableAlias, $options, $sqlParts);
162
163		if (!$options['countOutput']) {
164			if ($options['selectDRules'] !== null) {
165				$sqlParts = $this->addQuerySelect('dc.druleid', $sqlParts);
166			}
167		}
168
169		return $sqlParts;
170	}
171
172	protected function addRelatedObjects(array $options, array $result) {
173		$result = parent::addRelatedObjects($options, $result);
174
175		// select_drules
176		if ($options['selectDRules'] !== null && $options['selectDRules'] !== API_OUTPUT_COUNT) {
177			$relationMap = $this->createRelationMap($result, 'dcheckid', 'druleid');
178			$drules = API::DRule()->get([
179				'output' => $options['selectDRules'],
180				'druleids' => $relationMap->getRelatedIds(),
181				'preservekeys' => true
182			]);
183			if (!is_null($options['limitSelects'])) {
184				order_result($drules, 'name');
185			}
186			$result = $relationMap->mapMany($result, $drules, 'drules', $options['limitSelects']);
187		}
188
189		return $result;
190	}
191}
192