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