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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
<?php
/*
** 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 CFilter extends CDiv {
// Filter form object name and id attribute.
private const FORM_NAME = 'zbx_filter';
// Filter form object.
private $form;
// Visibility of 'Apply', 'Reset' form buttons. Visibility is set to all tabs.
private $show_buttons = true;
/**
* Filter reset URL.
*/
private $reset_url;
// Array of filter tab headers. Every header is mapped to it content via href(header) and id(content) attribute.
protected $headers = [];
// Array of filter tab content.
protected $tabs = [];
// jQuery.tabs initialization options.
protected $tabs_options = [
'collapsible' => true,
'active' => false
];
// Profile data associated with filter object.
protected $idx = null;
protected $idx2 = 0;
/**
* List of predefined time ranges.
*/
protected $time_ranges = [
[
['now-2d', 'now'],
['now-7d', 'now'],
['now-30d', 'now'],
['now-3M', 'now'],
['now-6M', 'now'],
['now-1y', 'now'],
['now-2y', 'now']
],
[
['now-1d/d', 'now-1d/d'],
['now-2d/d', 'now-2d/d'],
['now-1w/d', 'now-1w/d'],
['now-1w/w', 'now-1w/w'],
['now-1M/M', 'now-1M/M'],
['now-1y/y', 'now-1y/y']
],
[
['now/d', 'now/d'],
['now/d', 'now'],
['now/w', 'now/w'],
['now/w', 'now'],
['now/M', 'now/M'],
['now/M', 'now'],
['now/y', 'now/y'],
['now/y', 'now']
],
[
['now-5m', 'now'],
['now-15m', 'now'],
['now-30m', 'now'],
['now-1h', 'now'],
['now-3h', 'now'],
['now-6h', 'now'],
['now-12h', 'now'],
['now-24h', 'now']
]
];
public function __construct() {
parent::__construct();
$this
->setAttribute('data-accessible', 1)
->addClass(ZBX_STYLE_FILTER_SPACE)
->setId(uniqid('filter_'));
$this->form = (new CForm('get'))
->cleanItems()
->setAttribute('name', self::FORM_NAME);
$this->reset_url = new CUrl();
}
public function setResetUrl(CUrl $url): self {
$this->reset_url = (clone $url)
->setArgument('filter_rst', 1)
->getUrl();
return $this;
}
/**
* Add variable to filter form.
*
* @param string $name Variable name.
* @param string $value Variable value.
*
* @return CFilter
*/
public function addVar($name, $value, $id = null) {
$this->form->addVar($name, $value, $id);
return $this;
}
/**
* Hide filter tab buttons. Should be called before addFilterTab.
*/
public function hideFilterButtons() {
$this->show_buttons = false;
return $this;
}
/**
* Set profile 'idx' and 'idx2' data.
*
* @param string $idx Profile 'idx' string.
* @param int $idx2 Profile 'idx2' identifier, default 0.
*
* @return CFilter
*/
public function setProfile($idx, $idx2 = 0) {
$this->idx = $idx;
$this->idx2 = $idx2;
$this->setAttribute('data-profile-idx', $idx);
$this->setAttribute('data-profile-idx2', $idx2);
return $this;
}
/**
* Adds an item inside the form object.
*
* @param mixed $item An item to add inside the form object.
*
* @return CFilter
*/
public function addFormItem($item) {
$this->form->addItem($item);
return $this;
}
/**
* Set active tab.
*
* @param int $tab 1 based index of active tab. If set to 0 all tabs will be collapsed.
*
* @return CFilter
*/
public function setActiveTab($tab) {
$this->tabs_options['active'] = $tab > 0 ? $tab - 1 : false;
return $this;
}
/**
* Add tab with filter form.
*
* @param string $header Tab header title string.
* @param array $columns Array of filter columns markup.
* @param array $footer Additional markup objects for filter tab, default null.
*
* @return CFilter
*/
public function addFilterTab($header, $columns, $footer = null) {
$row = (new CDiv())->addClass(ZBX_STYLE_ROW);
$body = [];
$anchor = 'tab_'.count($this->tabs);
foreach ($columns as $column) {
$row->addItem((new CDiv($column))->addClass(ZBX_STYLE_CELL));
}
$body[] = (new CDiv())
->addClass(ZBX_STYLE_TABLE)
->addClass(ZBX_STYLE_FILTER_FORMS)
->addItem($row);
if ($this->show_buttons) {
$body[] = (new CDiv())
->addClass(ZBX_STYLE_FILTER_FORMS)
->addItem(
(new CSubmitButton(_('Apply'), 'filter_set', 1))
->onClick('javascript: chkbxRange.clearSelectedOnFilterChange();')
)
->addItem(
(new CRedirectButton(_('Reset'), $this->reset_url))
->addClass(ZBX_STYLE_BTN_ALT)
->onClick('javascript: chkbxRange.clearSelectedOnFilterChange();')
);
}
if ($footer !== null) {
$body[] = $footer;
}
return $this->addTab(
(new CLink($header, '#'.$anchor))->addClass(ZBX_STYLE_FILTER_TRIGGER),
(new CDiv($body))
->addClass(ZBX_STYLE_FILTER_CONTAINER)
->setId($anchor)
);
}
/**
* Add time selector specific tab. Should be called before any tab is added. Adds two tabs:
* - time selector range change buttons: back, zoom out, forward.
* - time selector range change form with predefined ranges.
*
* @param string $from Start date. (can be in relative time format, example: now-1w)
* @param string $to End date. (can be in relative time format, example: now-1w)
* @param bool $visible Either to make time selector visible or hidden.
* @param string $format Date and time format used in CDateSelector.
*
* @return CFilter
*/
public function addTimeSelector($from, $to, $visible = true, $format = ZBX_FULL_DATE_TIME) {
$header = relativeDateToText($from, $to);
if ($visible) {
$this->addTab(new CDiv([
(new CSimpleButton())->addClass(ZBX_STYLE_BTN_TIME_LEFT),
(new CSimpleButton(_('Zoom out')))->addClass(ZBX_STYLE_BTN_TIME_OUT),
(new CSimpleButton())->addClass(ZBX_STYLE_BTN_TIME_RIGHT)
]), null);
$predefined_ranges = [];
foreach ($this->time_ranges as $column_ranges) {
$column = (new CList())->addClass(ZBX_STYLE_TIME_QUICK);
foreach ($column_ranges as $range) {
$label = relativeDateToText($range[0], $range[1]);
$is_selected = ($header === $label);
$column->addItem((new CLink($label))
->setAttribute('data-from', $range[0])
->setAttribute('data-to', $range[1])
->setAttribute('data-label', $label)
->addClass($is_selected ? ZBX_STYLE_SELECTED : null)
);
}
$predefined_ranges[] = (new CDiv($column))->addClass(ZBX_STYLE_CELL);
}
$anchor = 'tab_'.count($this->tabs);
$this->addTab(
(new CLink($header, '#'.$anchor))->addClass(ZBX_STYLE_BTN_TIME),
(new CDiv([
(new CDiv([
new CList([
new CLabel(_('From'), 'from'),
(new CDateSelector('from', $from))->setDateFormat($format)
]),
(new CList([(new CListItem(''))->addClass(ZBX_STYLE_RED)]))
->setAttribute('data-error-for', 'from')
->addClass(ZBX_STYLE_TIME_INPUT_ERROR)
->addStyle('display: none'),
new CList([
new CLabel(_('To'), 'to'),
(new CDateSelector('to', $to))->setDateFormat($format)
]),
(new CList([(new CListItem(''))->addClass(ZBX_STYLE_RED)]))
->setAttribute('data-error-for', 'to')
->addClass(ZBX_STYLE_TIME_INPUT_ERROR)
->addStyle('display: none'),
new CList([
new CButton('apply', _('Apply'))
])
]))->addClass(ZBX_STYLE_TIME_INPUT),
(new CDiv($predefined_ranges))->addClass(ZBX_STYLE_TIME_QUICK_RANGE)
]))
->addClass(ZBX_STYLE_FILTER_CONTAINER)
->addClass(ZBX_STYLE_TIME_SELECTION_CONTAINER)
->setId($anchor)
);
}
else {
$this
->setAttribute('data-accessible', 0)
->addTab(null, (new CDiv([
new CVar('from', $from),
new CVar('to', $to)
])));
}
return $this;
}
/**
* Add tab.
*
* @param string|CTag $header Tab header title string or CTag container.
* @param array|CTag $body Array of body elements.
*
* @return CFilter
*/
public function addTab($header, $body) {
$this->headers[] = $header;
$this->tabs[] = $body;
return $this;
}
/**
* Return javascript code for jquery-ui initialization.
*
* @return string
*/
private function getJS() {
$id = '#'.$this->getId();
$js = 'jQuery("'.$id.'").tabs('.json_encode($this->tabs_options).').show();';
// Set the focus to a field with autofocus after the filter becomes visible.
$js .= 'jQuery("[autofocus=autofocus]", jQuery("'.$id.'")).filter(":visible").focus();';
if ($this->idx !== null && $this->idx !== '') {
$js .= 'jQuery("'.$id.'").on("tabsactivate", function(e, ui) {'.
'var active = ui.newPanel.length ? jQuery(this).tabs("option", "active") + 1 : 0;'.
'updateUserProfile("'.$this->idx.'.active", active, []);'.
'if (active) {'.
'jQuery("[autofocus=autofocus]", ui.newPanel).focus();'.
'}'.
'});';
}
return $js;
}
/**
* Render current CFilter object as HTML string.
*
* @return string
*/
public function toString($destroy = true) {
$headers = (new CList())->addClass(ZBX_STYLE_FILTER_BTN_CONTAINER);
$headers_cnt = 0;
if ($this->tabs_options['active'] !== false
&& !array_key_exists($this->tabs_options['active'], $this->headers)) {
$this->tabs_options['active'] = 0;
}
foreach ($this->headers as $index => $header) {
if ($header) {
$headers->addItem($header);
$headers_cnt++;
}
if ($this->tabs[$index] !== null && $index !== $this->tabs_options['active']) {
$this->tabs[$index]->addStyle('display: none');
}
}
$this
->addStyle('display:none')
->form->addItem($this->tabs);
if ($headers_cnt) {
$this
->addItem($headers)
->setAttribute('aria-label', _('Filter'));
}
$this->addItem($this->form);
return parent::toString($destroy).($headers_cnt ? get_js($this->getJS()) : '');
}
}
|