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