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 * @var CView $this
24 */
25
26if (!empty($this->data['parent_discoveryid'])) {
27	$widget = (new CWidget())
28		->setTitle(_('Graph prototypes'))
29		->setControls(
30			(new CTag('nav', true,
31				(new CList())->addItem(new CRedirectButton(_('Create graph prototype'),
32					(new CUrl('graphs.php'))
33						->setArgument('form', 'create')
34						->setArgument('parent_discoveryid', $data['parent_discoveryid'])
35						->getUrl()
36				))
37			))->setAttribute('aria-label', _('Content controls'))
38		)
39		->addItem(get_header_host_table('graphs', $this->data['hostid'], $this->data['parent_discoveryid']));
40}
41else {
42	$widget = (new CWidget())
43		->setTitle(_('Graphs'))
44		->setControls(
45			(new CTag('nav', true, ($data['hostid'] == 0)
46				? (new CButton('form', _('Create graph (select host first)')))->setEnabled(false)
47				: new CRedirectButton(_('Create graph'), (new CUrl('graphs.php'))
48					->setArgument('hostid', $data['hostid'])
49					->setArgument('form', 'create')
50					->getUrl()
51				)
52			))
53				->setAttribute('aria-label', _('Content controls'))
54		);
55
56	if (!empty($this->data['hostid'])) {
57		$widget->addItem(get_header_host_table('graphs', $this->data['hostid']));
58	}
59
60	// Add filter tab.
61	$widget->addItem(
62		(new CFilter(new CUrl('graphs.php')))
63			->setProfile($data['profileIdx'])
64			->setActiveTab($data['active_tab'])
65			->addFilterTab(_('Filter'), [
66				(new CFormList())
67					->addRow(
68						(new CLabel(_('Host groups'), 'filter_groups__ms')),
69						(new CMultiSelect([
70							'name' => 'filter_groups[]',
71							'object_name' => 'hostGroup',
72							'data' => $data['filter']['groups'],
73							'popup' => [
74								'parameters' => [
75									'srctbl' => 'host_groups',
76									'srcfld1' => 'groupid',
77									'dstfrm' => 'zbx_filter',
78									'dstfld1' => 'filter_groups_',
79									'with_hosts_and_templates' => 1,
80									'editable' => 1,
81									'enrich_parent_groups' => true
82								]
83							]
84						]))->setWidth(ZBX_TEXTAREA_MEDIUM_WIDTH)
85					)
86					->addRow(
87						(new CLabel(_('Hosts'), 'filter_hosts__ms')),
88						(new CMultiSelect([
89							'name' => 'filter_hostids[]',
90							'object_name' => 'host_templates',
91							'data' => $data['filter']['hosts'],
92							'popup' => [
93								'filter_preselect_fields' => [
94									'hostgroups' => 'filter_groups_'
95								],
96								'parameters' => [
97									'srctbl' => 'host_templates',
98									'srcfld1' => 'hostid',
99									'dstfrm' => 'zbx_filter',
100									'dstfld1' => 'filter_hostids_',
101									'editable' => 1
102								]
103							]
104						]))->setWidth(ZBX_TEXTAREA_MEDIUM_WIDTH)
105					)
106			])
107	);
108}
109
110// create form
111$graphForm = (new CForm())
112	->setName('graphForm')
113	->addVar('hostid', $this->data['hostid']);
114if (!empty($this->data['parent_discoveryid'])) {
115	$graphForm->addVar('parent_discoveryid', $this->data['parent_discoveryid']);
116}
117
118// create table
119$url = (new CUrl('graphs.php'))->getUrl();
120$discover = null;
121$info_column = null;
122
123if ($data['parent_discoveryid']) {
124	$discover = make_sorting_header(_('Discover'), 'discover', $data['sort'], $data['sortorder'], $url);
125}
126else {
127	$info_column = _('Info');
128}
129
130$graphTable = (new CTableInfo())
131	->setHeader([
132		(new CColHeader(
133			(new CCheckBox('all_graphs'))->onClick("checkAll('".$graphForm->getName()."', 'all_graphs', 'group_graphid');")
134		))->addClass(ZBX_STYLE_CELL_WIDTH),
135		!empty($this->data['hostid']) ? null : _('Hosts'),
136		make_sorting_header(_('Name'), 'name', $this->data['sort'], $this->data['sortorder'], $url),
137		_('Width'),
138		_('Height'),
139		make_sorting_header(_('Graph type'), 'graphtype', $this->data['sort'], $this->data['sortorder'], $url),
140		$discover,
141		$info_column
142	]);
143
144foreach ($data['graphs'] as $graph) {
145	$graphid = $graph['graphid'];
146
147	$hostList = null;
148	if (empty($this->data['hostid'])) {
149		$hostList = [];
150		foreach ($graph['hosts'] as $host) {
151			$hostList[$host['name']] = $host['name'];
152		}
153
154		foreach ($graph['templates'] as $template) {
155			$hostList[$template['name']] = $template['name'];
156		}
157		$hostList = implode(', ', $hostList);
158	}
159
160	$name = [];
161	$name[] = makeGraphTemplatePrefix($graphid, $data['parent_templates'], ($data['parent_discoveryid'] === null)
162		? ZBX_FLAG_DISCOVERY_NORMAL
163		: ZBX_FLAG_DISCOVERY_PROTOTYPE
164	);
165
166	if ($graph['discoveryRule'] && $data['parent_discoveryid'] === null) {
167		$name[] = (new CLink(CHtml::encode($graph['discoveryRule']['name']),
168			(new CUrl('host_discovery.php'))
169				->setArgument('form', 'update')
170				->setArgument('itemid', $graph['discoveryRule']['itemid'])
171		))
172			->addClass(ZBX_STYLE_LINK_ALT)
173			->addClass(ZBX_STYLE_ORANGE);
174		$name[] = NAME_DELIMITER;
175	}
176
177	$url = (new CUrl('graphs.php'))
178		->setArgument('form', 'update')
179		->setArgument('parent_discoveryid', $data['parent_discoveryid'])
180		->setArgument('graphid', $graphid);
181
182	if ($data['parent_discoveryid'] === null) {
183		$url->setArgument('filter_hostids', [$data['hostid']]);
184	}
185
186	$name[] = new CLink(CHtml::encode($graph['name']), $url);
187	$info_icons = [];
188	$discover = null;
189
190	if ($data['parent_discoveryid']) {
191		$nodiscover = ($graph['discover'] == ZBX_PROTOTYPE_NO_DISCOVER);
192		$discover = (new CLink($nodiscover ? _('No') : _('Yes'),
193				(new CUrl('graphs.php'))
194					->setArgument('action', 'graph.updatediscover')
195					->setArgument('parent_discoveryid', $data['parent_discoveryid'])
196					->setArgument('graphid', $graphid)
197					->setArgument('discover', $nodiscover ? ZBX_PROTOTYPE_DISCOVER : ZBX_PROTOTYPE_NO_DISCOVER)
198					->getUrl()
199			))
200				->addSID()
201				->addClass(ZBX_STYLE_LINK_ACTION)
202				->addClass($nodiscover ? ZBX_STYLE_RED : ZBX_STYLE_GREEN);
203	}
204	else if (array_key_exists('ts_delete', $graph['graphDiscovery']) && $graph['graphDiscovery']['ts_delete'] > 0) {
205		$info_icons[] = getGraphLifetimeIndicator(time(), $graph['graphDiscovery']['ts_delete']);
206	}
207
208	$graphTable->addRow([
209		new CCheckBox('group_graphid['.$graphid.']', $graphid),
210		$hostList,
211		$name,
212		$graph['width'],
213		$graph['height'],
214		$graph['graphtype'],
215		$discover,
216		($info_column === null) ? null : makeInformationList($info_icons)
217	]);
218}
219
220// buttons
221$buttonsArray = [];
222if (!$this->data['parent_discoveryid']) {
223	$buttonsArray['graph.masscopyto'] = ['name' => _('Copy')];
224}
225$buttonsArray['graph.massdelete'] = ['name' => _('Delete'), 'confirm' => $this->data['parent_discoveryid']
226	? _('Delete selected graph prototypes?')
227	: _('Delete selected graphs?')
228];
229
230// append table to form
231$graphForm->addItem([
232	$graphTable,
233	$this->data['paging'],
234	new CActionButtonList('action', 'group_graphid', $buttonsArray,
235		$this->data['parent_discoveryid']
236			? $this->data['parent_discoveryid']
237			: $this->data['hostid']
238	)
239]);
240
241// append form to widget
242$widget->addItem($graphForm);
243
244$widget->show();
245