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$output = [];
23
24// Create form.
25$services_form = (new CForm())
26	->cleanItems()
27	->setName('services_form');
28
29if (array_key_exists('service', $data)) {
30	$services_form->addItem((new CVar('serviceid', $data['service']['serviceid']))->removeId());
31}
32
33// Create table.
34$services_table = (new CTableInfo())
35	->setHeader([
36		array_key_exists('db_cservices', $data)
37			? (new CColHeader(
38					(new CCheckBox('all_services'))
39						->onClick("javascript: checkAll('".$services_form->getName()."', 'all_services', 'services');")
40				))->addClass(ZBX_STYLE_CELL_WIDTH)
41			: null,
42		_('Service'),
43		_('Status calculation'),
44		_('Trigger')
45	]);
46
47$js_action_onclick = ' jQuery(this).removeAttr("onclick");';
48$js_action_onclick .= ' overlayDialogueDestroy(jQuery(this).closest("[data-dialogueid]").attr("data-dialogueid"));';
49$js_action_onclick .= ' return false;';
50
51// Add table rows.
52if (array_key_exists('db_pservices', $data)) {
53	// Add root item.
54	if ($data['parentid'] == 0) {
55		$description = new CSpan(_('root'));
56	}
57	else {
58		$description = (new CLink(_('root'), '#'))
59			->onClick('javascript:
60				jQuery(\'#parent_name\', window.document).val('.zbx_jsvalue(_('root')).');
61				jQuery(\'#parentname\', window.document).val('.zbx_jsvalue(_('root')).');
62				jQuery(\'#parentid\', window.document).val('.zbx_jsvalue(0).');'.
63				$js_action_onclick
64			);
65	}
66
67	$services_table->addRow([$description, _('Note'), '-']);
68
69	foreach ($data['db_pservices'] as $db_service) {
70		if (bccomp($data['parentid'], $db_service['serviceid']) == 0) {
71			$description = new CSpan($db_service['name']);
72		}
73		else {
74			$description = (new CLink($db_service['name'], '#'))
75				->addClass('link')
76				->onClick('javascript:
77					jQuery(\'#parent_name\', window.document).val('.zbx_jsvalue($db_service['name']).');
78					jQuery(\'#parentname\', window.document).val('.zbx_jsvalue($db_service['name']).');
79					jQuery(\'#parentid\', window.document).val('.zbx_jsvalue($db_service['serviceid']).');'.
80					$js_action_onclick
81				);
82		}
83
84		$services_table->addRow([$description, serviceAlgorithm($db_service['algorithm']), $db_service['trigger']]);
85	}
86
87	$output['buttons'] = null;
88}
89elseif (array_key_exists('db_cservices', $data)) {
90	foreach ($data['db_cservices'] as $service) {
91		$description = (new CLink($service['name'], '#'))
92			->addClass('service-name')
93			->setId('service-name-'.$service['serviceid'])
94			->setAttribute('data-name', $service['name'])
95			->setAttribute('data-serviceid', $service['serviceid'])
96			->setAttribute('data-trigger', $service['trigger']);
97
98		$cb = (new CCheckBox('services['.$service['serviceid'].']', $service['serviceid']))
99			->addClass('service-select');
100
101		$services_table->addRow([$cb, $description, serviceAlgorithm($service['algorithm']), $service['trigger']]);
102	}
103
104	$output['script_inline'] =
105		'jQuery(document).ready(function() {'.
106			'jQuery(".service-name").click(function() {'.
107				'var e = jQuery(this);'.
108				'window.add_child_service(e.data("name"), e.data("serviceid"), e.data("trigger"));'.
109				$js_action_onclick.
110			'});'.
111
112			'cookie.init();'.
113			'chkbxRange.init();'.
114		'});'.
115
116		'var addSelectedServices = function() {'.
117			'var e;'.
118			'jQuery(".service-select:checked").each(function(key, cb) {'.
119				'e = jQuery("#service-name-" + jQuery(cb).val());'.
120				'window.add_child_service(e.data("name"), e.data("serviceid"), e.data("trigger"));'.
121			'});'.
122
123			'return true;'.
124		'};';
125
126	$output['buttons'] = [
127		[
128			'title' => _('Select'),
129			'class' => '',
130			'action' => 'return addSelectedServices();'
131		]
132	];
133}
134
135$services_form->addItem($services_table);
136
137$output += [
138	'header' => $data['title'],
139	'body' => (new CDiv($services_form))->toString()
140];
141
142if ($data['user']['debug_mode'] == GROUP_DEBUG_MODE_ENABLED) {
143	CProfiler::getInstance()->stop();
144	$output['debug'] = CProfiler::getInstance()->make()->toString();
145}
146
147echo (new CJson())->encode($output);
148