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$widget = (new CWidget())
22	->setTitle(_('Discovery rules'))
23	->setControls((new CForm('get'))
24		->cleanItems()
25		->addVar('hostid', $this->data['hostid'])
26		->addItem((new CList())->addItem(new CSubmit('form', _('Create discovery rule'))))
27	)
28	->addItem(get_header_host_table('discoveries', $this->data['hostid']));
29
30// create form
31$discoveryForm = (new CForm())
32	->setName('discovery')
33	->addVar('hostid', $this->data['hostid']);
34
35$url = (new CUrl('host_discovery.php'))
36	->setArgument('hostid', $data['hostid'])
37	->getUrl();
38
39// create table
40$discoveryTable = (new CTableInfo())
41	->setHeader([
42		(new CColHeader(
43			(new CCheckBox('all_items'))->onClick("checkAll('".$discoveryForm->getName()."', 'all_items', 'g_hostdruleid');")
44		))->addClass(ZBX_STYLE_CELL_WIDTH),
45		make_sorting_header(_('Name'), 'name', $data['sort'], $data['sortorder'], $url),
46		_('Items'),
47		_('Triggers'),
48		_('Graphs'),
49		($data['host']['flags'] == ZBX_FLAG_DISCOVERY_NORMAL) ? _('Hosts') : null,
50		make_sorting_header(_('Key'), 'key_', $data['sort'], $data['sortorder'], $url),
51		make_sorting_header(_('Interval'), 'delay', $data['sort'], $data['sortorder'], $url),
52		make_sorting_header(_('Type'), 'type', $data['sort'], $data['sortorder'], $url),
53		make_sorting_header(_('Status'), 'status', $data['sort'], $data['sortorder'], $url),
54		$data['showInfoColumn'] ? _('Info') : null
55	]);
56
57foreach ($data['discoveries'] as $discovery) {
58	// description
59	$description = [];
60
61	if ($discovery['templateid']) {
62		if (array_key_exists($discovery['dbTemplate']['hostid'], $data['writable_templates'])) {
63			$description[] = (new CLink($discovery['dbTemplate']['name'],
64				'?hostid='.$discovery['dbTemplate']['hostid']
65			))
66				->addClass(ZBX_STYLE_LINK_ALT)
67				->addClass(ZBX_STYLE_GREY);
68		}
69		else {
70			$description[] = (new CSpan($discovery['dbTemplate']['name']))
71				->addClass(ZBX_STYLE_GREY);
72		}
73
74		$description[] = NAME_DELIMITER;
75	}
76
77	$description[] = new CLink($discovery['name_expanded'], '?form=update&itemid='.$discovery['itemid']);
78
79	// status
80	$status = (new CLink(
81		itemIndicator($discovery['status'], $discovery['state']),
82		'?hostid='.$_REQUEST['hostid'].
83			'&g_hostdruleid='.$discovery['itemid'].
84			'&action='.($discovery['status'] == ITEM_STATUS_DISABLED
85				? 'discoveryrule.massenable'
86				: 'discoveryrule.massdisable'
87			))
88		)
89			->addClass(ZBX_STYLE_LINK_ACTION)
90			->addClass(itemIndicatorStyle($discovery['status'], $discovery['state']))
91			->addSID();
92
93	// info
94	if ($data['showInfoColumn']) {
95		if ($discovery['status'] == ITEM_STATUS_ACTIVE && !zbx_empty($discovery['error'])) {
96			$info = makeErrorIcon($discovery['error']);
97		}
98		else {
99			$info = '';
100		}
101	}
102	else {
103		$info = null;
104	}
105
106	// host prototype link
107	$hostPrototypeLink = null;
108	if ($data['host']['flags'] == ZBX_FLAG_DISCOVERY_NORMAL) {
109		$hostPrototypeLink = [
110			new CLink(_('Host prototypes'), 'host_prototypes.php?parent_discoveryid='.$discovery['itemid']),
111			CViewHelper::showNum($discovery['hostPrototypes'])
112		];
113	}
114
115	$discoveryTable->addRow([
116		new CCheckBox('g_hostdruleid['.$discovery['itemid'].']', $discovery['itemid']),
117		$description,
118		[
119			new CLink(
120				_('Item prototypes'),
121				'disc_prototypes.php?parent_discoveryid='.$discovery['itemid']
122			),
123			CViewHelper::showNum($discovery['items'])
124		],
125		[
126			new CLink(
127				_('Trigger prototypes'),
128				'trigger_prototypes.php?parent_discoveryid='.$discovery['itemid']
129			),
130			CViewHelper::showNum($discovery['triggers'])
131		],
132		[
133			new CLink(
134				_('Graph prototypes'),
135				'graphs.php?parent_discoveryid='.$discovery['itemid']
136			),
137			CViewHelper::showNum($discovery['graphs'])
138		],
139		$hostPrototypeLink,
140		$discovery['key_'],
141		($discovery['delay'] === '') ? '' : convertUnitsS($discovery['delay']),
142		item_type2str($discovery['type']),
143		$status,
144		$info
145	]);
146}
147
148zbx_add_post_js('cookie.prefix = "'.$this->data['hostid'].'";');
149
150// append table to form
151$discoveryForm->addItem([
152	$discoveryTable,
153	$this->data['paging'],
154	new CActionButtonList('action', 'g_hostdruleid',
155		[
156			'discoveryrule.massenable' => ['name' => _('Enable'),
157				'confirm' =>_('Enable selected discovery rules?')
158			],
159			'discoveryrule.massdisable' => ['name' => _('Disable'),
160				'confirm' =>_('Disable selected discovery rules?')
161			],
162			'discoveryrule.massdelete' => ['name' => _('Delete'),
163				'confirm' =>_('Delete selected discovery rules?')
164			]
165		],
166		$this->data['hostid']
167	)
168]);
169
170// append form to widget
171$widget->addItem($discoveryForm);
172
173return $widget;
174