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
27function drawToc(array $toc): CDiv {
28	$change_types_list = (new CTag('ul', true))
29		->addClass(ZBX_STYLE_TOC_LIST);
30
31	foreach ($toc as $change_type => $entity_types) {
32		$change_types_list->addItem(drawChangeType($change_type, $entity_types));
33	}
34
35	return (new CDiv())
36		->addClass(ZBX_STYLE_TOC)
37		->addItem($change_types_list);
38}
39
40function drawChangeType(string $name, array $entity_types): CTag {
41	$entity_types_list = (new CTag('ul', true))
42		->addClass(ZBX_STYLE_TOC_SUBLIST);
43
44	foreach ($entity_types as $entity_type => $entities) {
45		$entity_types_list->addItem(drawEntityType($entity_type, $entities));
46	}
47
48	return (new CTag('li', true))
49		->addItem((new CDiv())
50			->addClass(ZBX_STYLE_TOC_ROW)
51			->addItem((new CTag('button', true))
52				->addClass(ZBX_STYLE_TOC_ITEM)
53				->addClass(ZBX_STYLE_TOC_ARROW)
54				->addItem((new CSpan())
55					->addClass(ZBX_STYLE_ARROW_DOWN)
56				)
57				->addItem($name)
58			)
59		)
60		->addItem($entity_types_list);
61}
62
63function drawEntityType(string $name, array $entities): CTag {
64	$entities_list = (new CTag('ul', true))
65		->addClass(ZBX_STYLE_TOC_SUBLIST);
66
67	foreach ($entities as $entity) {
68		$entities_list->addItem(drawEntity($entity));
69	}
70
71	return (new CTag('li', true))
72		->addItem((new CDiv())
73			->addClass(ZBX_STYLE_TOC_ROW)
74			->addItem((new CTag('button', true))
75				->addClass(ZBX_STYLE_TOC_ITEM)
76				->addClass(ZBX_STYLE_TOC_ARROW)
77				->addItem((new CSpan())
78					->addClass(ZBX_STYLE_ARROW_DOWN)
79				)
80				->addItem($name)
81			)
82		)
83		->addItem($entities_list);
84}
85
86function drawEntity(array $entity): CTag {
87	return (new CTag('li', true))
88		->addItem((new CDiv())
89			->addClass(ZBX_STYLE_TOC_ROW)
90			->addItem((new CLink($entity['name'], '#importcompare_toc_'.$entity['id']))
91				->addClass(ZBX_STYLE_TOC_ITEM)
92			)
93		);
94}
95
96function drawDiff(array $diff): CDiv {
97	return (new CDiv())
98		->addClass(ZBX_STYLE_DIFF)
99		->addItem(new CPre(rowsToDivs($diff)));
100}
101
102function rowsToDivs(array $rows): array {
103	$divs = [];
104
105	$first_characters = [
106		CControllerPopupImportCompare::CHANGE_NONE => ' ',
107		CControllerPopupImportCompare::CHANGE_ADDED => '+',
108		CControllerPopupImportCompare::CHANGE_REMOVED => '-'
109	];
110
111	$classes = [
112		CControllerPopupImportCompare::CHANGE_ADDED => ZBX_STYLE_DIFF_ADDED,
113		CControllerPopupImportCompare::CHANGE_REMOVED => ZBX_STYLE_DIFF_REMOVED
114	];
115
116	foreach ($rows as $row) {
117		$lines = explode("\n", $row['value']);
118
119		foreach ($lines as $index => $line) {
120			if ($line === '') {
121				continue;
122			}
123
124			$text = $first_characters[$row['change_type']] . str_repeat(' ', $row['depth'] * 2 -1) . $line . "\n";
125			$div = (new CDiv($text));
126
127			if (array_key_exists('id', $row) && $index === 0) {
128				$div->setAttribute('id', 'importcompare_toc_' . $row['id']);
129			}
130
131			if (array_key_exists($row['change_type'], $classes)) {
132				$div->addClass($classes[$row['change_type']]);
133			}
134
135			$divs[] = $div;
136		}
137	}
138
139	return $divs;
140}
141
142if ($data['errors'] !== null) {
143	$output = [
144		'errors' => $data['errors']
145	];
146}
147else {
148	$buttons = [];
149
150	if ($data['diff']) {
151		$buttons[] = [
152			'title' => _('Import'),
153			'class' => '',
154			'keepOpen' => true,
155			'isSubmit' => true,
156			'focused' => true,
157			'action' => 'submitImportComparePopup(overlay);'
158		];
159	}
160
161	$buttons[] = [
162		'title' => $data['diff'] ? _('Cancel') : _('Close'),
163		'cancel' => true,
164		'class' => ZBX_STYLE_BTN_ALT,
165		'action' => ''
166	];
167
168	$output = [
169		'header' => $data['title'],
170		'script_inline' => trim($this->readJsFile('popup.import.compare.js.php')),
171		'body' => !$data['diff']
172			? (new CTableInfo())
173					->setNoDataMessage(_('No changes.'))
174					->toString()
175			: (new CForm())
176				->addClass('import-compare')
177				->addVar('import_overlayid', $data['import_overlayid'])
178				->addItem(drawToc($data['diff_toc']))
179				->addItem(drawDiff($data['diff']))
180				->toString(),
181		'buttons' => $buttons,
182		'no_changes' => !$data['diff']
183	];
184}
185
186if ($data['user']['debug_mode'] == GROUP_DEBUG_MODE_ENABLED) {
187	CProfiler::getInstance()->stop();
188	$output['debug'] = CProfiler::getInstance()->make()->toString();
189}
190
191echo json_encode($output);
192