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$widget = (new CWidget())
23	->setTitle(_('Item prototypes'))
24	->setControls(
25		(new CTag('nav', true,
26			(new CList())->addItem(new CRedirectButton(_('Create item prototype'),
27				(new CUrl('disc_prototypes.php'))
28					->setArgument('form', 'create')
29					->setArgument('parent_discoveryid', $data['parent_discoveryid'])
30					->getUrl()
31			))
32		))->setAttribute('aria-label', _('Content controls'))
33	)
34	->addItem(get_header_host_table('items', $this->data['hostid'], $this->data['parent_discoveryid']));
35
36// create form
37$itemForm = (new CForm())
38	->setName('items')
39	->addVar('parent_discoveryid', $this->data['parent_discoveryid']);
40
41$url = (new CUrl('disc_prototypes.php'))
42	->setArgument('parent_discoveryid', $data['parent_discoveryid'])
43	->getUrl();
44
45// create table
46$itemTable = (new CTableInfo())
47	->setHeader([
48		(new CColHeader(
49			(new CCheckBox('all_items'))->onClick("checkAll('".$itemForm->getName()."', 'all_items', 'group_itemid');")
50		))->addClass(ZBX_STYLE_CELL_WIDTH),
51		_('Wizard'),
52		make_sorting_header(_('Name'),'name', $data['sort'], $data['sortorder'], $url),
53		make_sorting_header(_('Key'), 'key_', $data['sort'], $data['sortorder'], $url),
54		make_sorting_header(_('Interval'), 'delay', $data['sort'], $data['sortorder'], $url),
55		make_sorting_header(_('History'), 'history', $data['sort'], $data['sortorder'], $url),
56		make_sorting_header(_('Trends'), 'trends', $data['sort'], $data['sortorder'], $url),
57		make_sorting_header(_('Type'), 'type', $data['sort'], $data['sortorder'], $url),
58		_('Applications'),
59		make_sorting_header(_('Create enabled'), 'status', $data['sort'], $data['sortorder'], $url)
60	]);
61
62$update_interval_parser = new CUpdateIntervalParser(['usermacros' => true, 'lldmacros' => true]);
63
64foreach ($data['items'] as $item) {
65	$description = [];
66	$description[] = makeItemTemplatePrefix($item['itemid'], $data['parent_templates'], ZBX_FLAG_DISCOVERY_PROTOTYPE);
67
68	if ($item['type'] == ITEM_TYPE_DEPENDENT) {
69		if ($item['master_item']['type'] == ITEM_TYPE_HTTPTEST) {
70			$description[] = CHtml::encode($item['master_item']['name_expanded']);
71		}
72		else {
73			$link = ($item['master_item']['source'] === 'itemprototypes')
74				? (new CUrl('disc_prototypes.php'))->setArgument('parent_discoveryid', $data['parent_discoveryid'])
75				: (new CUrl('items.php'))->setArgument('hostid', $item['hostid']);
76
77			$description[] = (new CLink(CHtml::encode($item['master_item']['name_expanded']),
78				$link
79					->setArgument('form', 'update')
80					->setArgument('itemid', $item['master_item']['itemid'])
81					->getUrl()
82			))
83				->addClass(ZBX_STYLE_LINK_ALT)
84				->addClass(ZBX_STYLE_TEAL);
85		}
86
87		$description[] = NAME_DELIMITER;
88	}
89
90	$description[] = new CLink(
91		$item['name_expanded'],
92		(new CUrl('disc_prototypes.php'))
93			->setArgument('form', 'update')
94			->setArgument('parent_discoveryid', $data['parent_discoveryid'])
95			->setArgument('itemid', $item['itemid'])
96			->getUrl()
97	);
98
99	$status = (new CLink(
100		($item['status'] == ITEM_STATUS_DISABLED) ? _('No') : _('Yes'),
101		(new CUrl('disc_prototypes.php'))
102			->setArgument('group_itemid[]', $item['itemid'])
103			->setArgument('parent_discoveryid', $data['parent_discoveryid'])
104			->setArgument('action', ($item['status'] == ITEM_STATUS_DISABLED)
105				? 'itemprototype.massenable'
106				: 'itemprototype.massdisable'
107			)
108			->getUrl()
109	))
110		->addClass(ZBX_STYLE_LINK_ACTION)
111		->addClass(itemIndicatorStyle($item['status']))
112		->addSID();
113
114	if (!empty($item['applications'])) {
115		order_result($item['applications'], 'name');
116
117		$applications = zbx_objectValues($item['applications'], 'name');
118		$applications = implode(', ', $applications);
119		if (empty($applications)) {
120			$applications = '';
121		}
122	}
123	else {
124		$applications = '';
125	}
126
127	if (in_array($item['value_type'], [ITEM_VALUE_TYPE_STR, ITEM_VALUE_TYPE_LOG, ITEM_VALUE_TYPE_TEXT])) {
128		$item['trends'] = '';
129	}
130
131	// Hide zeros for trapper, SNMP trap and dependent items.
132	if ($item['type'] == ITEM_TYPE_TRAPPER || $item['type'] == ITEM_TYPE_SNMPTRAP
133			|| $item['type'] == ITEM_TYPE_DEPENDENT) {
134		$item['delay'] = '';
135	}
136	elseif ($update_interval_parser->parse($item['delay']) == CParser::PARSE_SUCCESS) {
137		$item['delay'] = $update_interval_parser->getDelay();
138	}
139
140	$item_menu = CMenuPopupHelper::getItemPrototype($item['itemid'], $data['parent_discoveryid']);
141
142	$wizard = (new CSpan(
143		(new CButton(null))->addClass(ZBX_STYLE_ICON_WZRD_ACTION)->setMenuPopup($item_menu)
144	))->addClass(ZBX_STYLE_REL_CONTAINER);
145
146	$itemTable->addRow([
147		new CCheckBox('group_itemid['.$item['itemid'].']', $item['itemid']),
148		$wizard,
149		$description,
150		$item['key_'],
151		$item['delay'],
152		$item['history'],
153		$item['trends'],
154		item_type2str($item['type']),
155		$applications,
156		$status
157	]);
158}
159
160// append table to form
161$itemForm->addItem([
162	$itemTable,
163	$this->data['paging'],
164	new CActionButtonList('action', 'group_itemid',
165		[
166			'itemprototype.massenable' => ['name' => _('Create enabled'),
167				'confirm' => _('Create items from selected prototypes as enabled?')
168			],
169			'itemprototype.massdisable' => ['name' => _('Create disabled'),
170				'confirm' => _('Create items from selected prototypes as disabled?')
171			],
172			'itemprototype.massdelete' => ['name' => _('Delete'),
173				'confirm' => _('Delete selected item prototypes?')
174			]
175		],
176		$this->data['parent_discoveryid']
177	)
178]);
179
180// append form to widget
181$widget->addItem($itemForm);
182
183return $widget;
184