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 * A class for rendering a widget that can be collapsed or expanded.
24 */
25class CCollapsibleUiWidget extends CUiWidget {
26
27	/**
28	 * Expand/collapse widget.
29	 *
30	 * Supported values:
31	 * - true - expanded;
32	 * - false - collapsed.
33	 *
34	 * @var bool
35	 */
36	private $expanded = true;
37
38	/**
39	 * Sets the header and adds a default expand-collapse icon.
40	 *
41	 * @param string $caption   Header caption.
42	 * @param array  $controls  (optional)
43	 * @param string $idx       (optional)
44	 *
45	 * @return $this
46	 */
47	public function setHeader($caption, array $controls = [], $idx = '') {
48		$icon = (new CRedirectButton(null, null))
49			->setId($this->id.'_icon')
50			->onClick('changeWidgetState(this, "'.$this->id.'", "'.$idx.'");');
51
52		if ($this->expanded) {
53			$icon
54				->addClass(ZBX_STYLE_BTN_WIDGET_COLLAPSE)
55				->setTitle(_('Collapse'));
56		}
57		else {
58			$icon
59				->addClass(ZBX_STYLE_BTN_WIDGET_EXPAND)
60				->setTitle(_('Expand'));
61		}
62
63		$controls[] = $icon;
64
65		parent::setHeader($caption, $controls);
66
67		return $this;
68	}
69
70	/**
71	 * Display the widget in expanded or collapsed state.
72	 */
73	protected function build() {
74		$body = (new CDiv($this->body))
75			->addClass('body')
76			->setId($this->id);
77
78		if (!$this->expanded) {
79			$body->setAttribute('style', 'display: none;');
80
81			if ($this->footer) {
82				$this->footer->setAttribute('style', 'display: none;');
83			}
84		}
85
86		$this->cleanItems();
87		$this->addItem($this->header);
88		$this->addItem($body);
89		$this->addItem($this->footer);
90
91		return $this;
92	}
93
94	/**
95	 * Sets expanded or collapsed state of the widget.
96	 *
97	 * @param bool
98	 */
99	public function setExpanded($expanded) {
100		$this->expanded = $expanded;
101		return $this;
102	}
103}
104