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 22class CUiWidget extends CDiv { 23 24 /** 25 * Widget id. 26 * 27 * @var string 28 */ 29 public $id; 30 31 /** 32 * Expand/collapse widget. 33 * 34 * Supported values: 35 * - true - expanded; 36 * - false - collapsed. 37 * 38 * @var bool 39 */ 40 public $open; 41 42 /** 43 * Header div. 44 * 45 * @var CDiv 46 */ 47 protected $header; 48 49 /** 50 * Body div. 51 * 52 * @var array 53 */ 54 protected $body; 55 56 /** 57 * Footer div. 58 * 59 * @var CDiv 60 */ 61 protected $footer; 62 63 /** 64 * Construct widget. 65 * 66 * @param string $id 67 * @param string|array|CTag $body 68 */ 69 public function __construct($id, $body = null) { 70 $this->id = $id; 71 $this->body = $body ? [$body] : []; 72 73 parent::__construct(); 74 75 $this->addClass(ZBX_STYLE_DASHBOARD_WIDGET); 76 $this->setId($this->id.'_widget'); 77 } 78 79 /** 80 * Set widget header. 81 * 82 * @param string $caption 83 * @param array $controls 84 * 85 * @return $this 86 */ 87 public function setHeader($caption, array $controls = []) { 88 $this->header = (new CDiv()) 89 ->addClass(ZBX_STYLE_DASHBOARD_WIDGET_HEAD) 90 ->addItem( 91 (new CTag('h4', true, $caption))->setId($this->id.'_header') 92 ); 93 94 if ($controls) { 95 $this->header->addItem(new CList($controls)); 96 } 97 98 return $this; 99 } 100 101 /** 102 * Set widget footer. 103 * 104 * @param string|array|CTag $footer 105 * @param bool $right 106 */ 107 public function setFooter($list) { 108 $this->footer = $list; 109 $this->footer->addClass(ZBX_STYLE_DASHBOARD_WIDGET_FOOT); 110 return $this; 111 } 112 113 /** 114 * Build widget header, body and footer. 115 */ 116 protected function build() { 117 $body = (new CDiv($this->body)) 118 ->setId($this->id); 119 120 $this->cleanItems(); 121 122 $this->addItem($this->header); 123 $this->addItem($body); 124 $this->addItem($this->footer); 125 return $this; 126 } 127 128 /** 129 * Get widget html. 130 */ 131 public function toString($destroy = true) { 132 $this->build(); 133 134 return parent::toString($destroy); 135 } 136} 137