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 ($data['uncheck']) {
27	uncheckTableRows('script');
28}
29
30$widget = (new CWidget())
31	->setTitle(_('Scripts'))
32	->setControls((new CTag('nav', true,
33		(new CList())
34			->addItem(new CRedirectButton(_('Create script'), 'zabbix.php?action=script.edit'))
35		))
36			->setAttribute('aria-label', _('Content controls'))
37	)
38	->addItem((new CFilter((new CUrl('zabbix.php'))->setArgument('action', 'script.list')))
39		->setProfile($data['profileIdx'])
40		->setActiveTab($data['active_tab'])
41		->addFilterTab(_('Filter'), [
42			(new CFormList())->addRow(_('Name'),
43				(new CTextBox('filter_name', $data['filter']['name']))
44					->setWidth(ZBX_TEXTAREA_FILTER_SMALL_WIDTH)
45					->setAttribute('autofocus', 'autofocus')
46			)
47		])
48		->addVar('action', 'script.list')
49	);
50
51$scriptsForm = (new CForm())
52	->setName('scriptsForm')
53	->setId('scripts');
54
55$url = (new CUrl('zabbix.php'))
56	->setArgument('action', 'script.list')
57	->getUrl();
58
59$scriptsTable = (new CTableInfo())
60	->setHeader([
61		(new CColHeader(
62			(new CCheckBox('all_scripts'))
63				->onClick("checkAll('".$scriptsForm->getName()."', 'all_scripts', 'scriptids');")
64		))->addClass(ZBX_STYLE_CELL_WIDTH),
65		make_sorting_header(_('Name'), 'name', $data['sort'], $data['sortorder'], $url),
66		_('Type'),
67		_('Execute on'),
68		make_sorting_header(_('Commands'), 'command', $data['sort'], $data['sortorder'], $url),
69		_('User group'),
70		_('Host group'),
71		_('Host access')
72	]);
73
74foreach ($data['scripts'] as $script) {
75	switch ($script['type']) {
76		case ZBX_SCRIPT_TYPE_CUSTOM_SCRIPT:
77			$scriptType = _('Script');
78			break;
79		case ZBX_SCRIPT_TYPE_IPMI:
80			$scriptType = _('IPMI');
81			break;
82	}
83
84	if ($script['type'] == ZBX_SCRIPT_TYPE_CUSTOM_SCRIPT) {
85		switch ($script['execute_on']) {
86			case ZBX_SCRIPT_EXECUTE_ON_AGENT:
87				$scriptExecuteOn = _('Agent');
88				break;
89			case ZBX_SCRIPT_EXECUTE_ON_SERVER:
90				$scriptExecuteOn = _('Server');
91				break;
92			case ZBX_SCRIPT_EXECUTE_ON_PROXY:
93				$scriptExecuteOn = _('Server (proxy)');
94				break;
95		}
96	}
97	else {
98		$scriptExecuteOn = '';
99	}
100
101	$scriptsTable->addRow([
102		new CCheckBox('scriptids['.$script['scriptid'].']', $script['scriptid']),
103		(new CCol(
104			new CLink($script['name'], 'zabbix.php?action=script.edit&scriptid='.$script['scriptid'])
105		))->addClass(ZBX_STYLE_NOWRAP),
106		$scriptType,
107		$scriptExecuteOn,
108		(new CCol(
109			zbx_nl2br(htmlspecialchars($script['command'], ENT_COMPAT, 'UTF-8'))
110		))->addClass(ZBX_STYLE_MONOSPACE_FONT),
111		($script['userGroupName'] === null) ? _('All') : $script['userGroupName'],
112		($script['hostGroupName'] === null) ? _('All') : $script['hostGroupName'],
113		($script['host_access'] == PERM_READ_WRITE) ? _('Write') : _('Read')
114	]);
115}
116
117// append table to form
118$scriptsForm->addItem([
119	$scriptsTable,
120	$data['paging'],
121	new CActionButtonList('action', 'scriptids', [
122		'script.delete' => ['name' => _('Delete'), 'confirm' => _('Delete selected scripts?')]
123	], 'script')
124]);
125
126// append form to widget
127$widget
128	->addItem($scriptsForm)
129	->show();
130