1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
<?php declare(strict_types = 0);
/*
** Zabbix
** Copyright (C) 2001-2023 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
/**
* Class to manage tab filters.
*/
class CTabFilter extends CDiv {
const ZBX_STYLE_CLASS = 'tabfilter-container';
const CSS_TAB_SELECTED = 'selected';
const CSS_TAB_EXPANDED = 'expanded';
const CSS_TAB_SORTABLE_CONTAINER = 'ui-sortable-container';
const CSS_ID_PREFIX = 'tabfilter_';
const CSS_TABFILTER_ICON_FILTER = 'icon-filter';
const CSS_TABFILTER_ITEM = 'tabfilter-item-label';
/**
* Array of arrays for tabs data. Single element contains: tab label object, tab content object or null and tab data
* array.
*
* @var array $tabs
*/
public $tabs = [];
/**
* Tab options array.
* idx - namespace used to get/update filter related data in profiles table.
* selected - zero based index of selected filter.
* expanded - is selected filter expanded or not.
* support_custom_time - can filters define custom time range or not.
* data - array of filters data arrays.
* page - current page number used by selected tab for pagination.
* timeselector - array of timeselector data, can be set with addTimeselector or passed as array.
*/
public $options = [
'idx' => '',
'selected' => 0,
'expanded' => false,
'support_custom_time' => 1,
'data' => [],
'page' => null
];
/**
* Tab form available buttons node. Will be initialized during __construct but can be overwritten if needed.
*/
public $buttons = null;
/**
* Subfilter node.
*/
public $subfilter = null;
/**
* Array of CPartial elements used as tab content rendering templates.
*
* @var array $template
*/
protected $templates = [];
/**
* Array of CTag tab label elements.
*/
protected $labels = [];
/**
* Array of CTag or null of tab content elements.
*/
protected $contents = [];
public function __construct($items = null) {
parent::__construct($items);
$this
->setId(uniqid(static::CSS_ID_PREFIX))
->addClass(ZBX_STYLE_FILTER_CONTAINER)
->addClass(static::ZBX_STYLE_CLASS);
$this->buttons = (new CDiv())
->addItem([
(new CSubmitButton(_('Update'), 'filter_update', 1))->addClass(ZBX_STYLE_BTN_ALT),
(new CSubmitButton(_('Save as'), 'filter_new', 1))->addClass(ZBX_STYLE_BTN_ALT),
new CSubmitButton(_('Apply'), 'filter_apply', 1),
(new CSubmitButton(_('Reset'), 'filter_reset', 1))->addClass(ZBX_STYLE_BTN_ALT)
])
->addClass(ZBX_STYLE_FILTER_FORMS)
->addClass('form-buttons');
}
/**
* Set tabfilter options used by javascript.
*
* @param array $options Array of options.
*/
public function setOptions(array $options) {
$this->options = $options;
if (array_key_exists('timeselector', $options)) {
$this->addTimeselector($this->options['timeselector']);
}
return $this;
}
/**
* Add template for browser side rendered tabs.
*
* @param CPartial $template Template object.
*/
public function addTemplate(CPartial $template) {
$this->templates[$template->getName()] = $template;
return $this;
}
/**
* Add subfilter.
*
* @param CPartial $subfilter Rendered subfilter.
*/
public function addSubfilter(CPartial $subfilter) {
$this->subfilter = $subfilter;
return $this;
}
/**
* Add single tab filter.
*
* @param CTag|string $label String or CTag as tab label element.
* @param CTag|null $content Tab content node or null if tab is dynamic.
* @param array $data
* @param bool $data['filter_sortable'] Make tab sortable.
* @param bool $data['filter_configurable'] Make tab configurable.
* @param mixed $data[<any>] Tab filter fields.
*/
public function addTab($label, ?CTag $content, array $data = []) {
$tab_index = count($this->labels);
$targetid = static::CSS_ID_PREFIX.$tab_index;
if ($content !== null && method_exists($content, 'getId') && $content->getId()) {
$targetid = $content->getId();
}
if (!is_a($label, CTag::class)) {
if ($tab_index == 0) {
$label = (new CLink(''))
->setAttribute('aria-label', _('Home'))
->addClass(self::CSS_TABFILTER_ICON_FILTER);
$data += [
'filter_sortable' => false,
'filter_configurable' => false
];
}
else {
$label = new CLink($label);
}
}
if (is_a($label, CLink::class)) {
// Disable navigation by TAB, javascript code will modify this attribute.
$label->setAttribute('tabindex', -1);
}
if ($content) {
$content->setId($targetid);
}
$this->labels[] = (new CListItem($label->addClass('tabfilter-item-link')))
->setAttribute('data-target', $targetid)
->addClass(self::CSS_TABFILTER_ITEM);
$this->contents[] = $content;
$this->options['data'][] = $data + [
'filter_sortable' => true,
'filter_configurable' => true
];
return $this;
}
/**
* Add time range selector tab.
*
* @param array $timerange
* @param string $timerange['from'] Time range start time.
* @param string $timerange['to'] Time range end time.
* @param string $timerange['idx']
* @param int $timerange['idx2']
*/
public function addTimeselector(array $timerange) {
$this->options['timeselector'] = $timerange + [
'format' => ZBX_FULL_DATE_TIME,
'from' => 'now-'.CSettingsHelper::get(CSettingsHelper::PERIOD_DEFAULT),
'to' => 'now',
'disabled' => true
];
return $this;
}
/**
* Add tab for render on browser side using passed $data.
* Selected and expanded tab will be pre-rendered server side to prevent screen flickering during page initial load.
*
* @param string|CTag $label Tab label.
* @param array $data Tab data associative array.
*
* @return CTabFilter
*/
public function addTemplatedTab($label, array $data) {
$uniqid = count($this->labels);
return $this->addTab($label, null, $data + ['uniqid' => $uniqid]);
}
/**
* Return HTML of tab filter body element.
*
* @return string
*/
public function bodyToString(): string {
$timeselector = array_key_exists('timeselector', $this->options) ? $this->options['timeselector'] : [];
$selected = $this->options['selected'];
$this->labels[$selected]->addClass(static::CSS_TAB_SELECTED);
if ($this->options['expanded'] && $this->contents[$selected] === null) {
$tab_data = $this->options['data'][$selected];
$tab_data['render_html'] = true;
$this->labels[$selected]->addClass(static::CSS_TAB_EXPANDED);
$this->contents[$selected] = (new CDiv([new CPartial($tab_data['tab_view'], $tab_data)]))
->setId($this->labels[$selected]->getAttribute('data-target'));
}
if ($timeselector) {
$data = $timeselector + [
'label' => relativeDateToText($timeselector['from'], $timeselector['to']),
'filter_timeselector' => true,
'filter_sortable' => false,
'filter_configurable' => false
];
$this->contents[] = (new CDiv(new CPartial('timeselector.filter', $data)))
->setId(static::CSS_ID_PREFIX.'timeselector');
$this->options['data'][] = $data;
}
foreach ($this->contents as $index => $content) {
if (is_a($content, CTag::class)) {
$content->addClass($index == $selected ? null : ZBX_STYLE_DISPLAY_NONE);
}
}
$templates = '';
foreach ($this->templates as $template) {
$templates .= $template->getOutput();
}
$tabfilter_container_classes = 'tabfilter-content-container';
if (!$this->options['expanded']) {
$tabfilter_container_classes .= ' tabfilter-collapsed';
if (!$this->subfilter) {
$tabfilter_container_classes .= ' display-none';
}
}
return implode('', [
$this->getNavigation(),
(new CDiv([
(new CDiv($this->contents))->addClass('tabfilter-tabs-container'),
$this->buttons,
$this->subfilter
]))
->addClass($tabfilter_container_classes),
$templates
]);
}
public function toString($destroy = true) {
if (array_key_exists('timeselector', $this->options)) {
$this
->addClass(ZBX_STYLE_FILTER_SPACE)
->setAttribute('data-disable-initial-check', 1)
->setAttribute('data-accessible', 1)
->setAttribute('data-profile-idx', $this->options['idx'])
->setAttribute('data-profile-idx2', 0);
}
return parent::toString($destroy);
}
/**
* Get time selector navigation buttons as array.
*
* @return array
*/
protected function getTimeselectorNavigation(): array {
$data = $this->options['timeselector'];
$selected = $this->options['data'][$this->options['selected']] + ['filter_custom_time' => 0];
$enabled = (!$selected['filter_custom_time'] && !$data['disabled']);
// Disable navigation by TAB, if timeselector is disabled.
$link = (new CLink(relativeDateToText($data['from'], $data['to'])))
->addClass('tabfilter-item-link')
->setAttribute('tabindex', $enabled ? 0 : -1)
->addClass(ZBX_STYLE_BTN_TIME)
->addClass($data['disabled'] ? ZBX_STYLE_DISABLED : null);
return [
(new CListItem($link))
->setAttribute('data-target', static::CSS_ID_PREFIX.'timeselector')
->addClass(self::CSS_TABFILTER_ITEM)
->addClass($enabled ? null : ZBX_STYLE_DISABLED),
(new CSimpleButton())
->setEnabled($enabled)
->addClass(ZBX_STYLE_BTN_TIME_LEFT),
(new CSimpleButton(_('Zoom out')))
->setEnabled($enabled)
->addClass(ZBX_STYLE_BTN_TIME_OUT),
(new CSimpleButton())
->setEnabled($enabled)
->addClass(ZBX_STYLE_BTN_TIME_RIGHT)
];
}
/**
* Generate tabs navigation layout.
*
* @return CTag
*/
protected function getNavigation(): CTag {
$sortable = [];
$static = [];
foreach ($this->labels as $index => $label) {
if ($this->options['data'][$index]['filter_sortable']) {
$sortable[$index] = $label;
}
else {
$static[$index] = $label;
}
}
// First dynamic tab is 'Home' tab and cannot be sorted.
array_unshift($sortable, array_shift($static));
$nav_list = new CList([
(new CSimpleButton())
->setAttribute('data-action', 'toggleTabsList')
->addClass('btn-widget-expand'),
(new CSimpleButton())
->setAttribute('data-action', 'selectNextTab')
->addClass('btn-iterator-page-next')
]);
if (array_key_exists('timeselector', $this->options)) {
array_map([$nav_list, 'addItem'], $this->getTimeselectorNavigation());
}
return new CTag('nav', true , new CList([
(new CSimpleButton())
->setAttribute('data-action', 'selectPrevTab')
->addClass('btn-iterator-page-previous'),
$sortable ? (new CList($sortable))->addClass(static::CSS_TAB_SORTABLE_CONTAINER) : null,
$static ? $static : null,
$nav_list
]));
}
}
|