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('dashbrd-widget'); 76 $this->setId($this->id.'_widget'); 77 } 78 79 /** 80 * Set widget header. 81 * 82 * @param string $caption 83 * @param arrayi $controls 84 */ 85 public function setHeader($caption, array $controls = [], $cursor_move = false) { 86 $this->header = (new CDiv()) 87 ->addClass('dashbrd-widget-head') 88 ->addItem( 89 (new CTag('h4', true, $caption))->setId($this->id.'_header') 90 ); 91 92 if ($cursor_move) { 93 $this->header->addClass(ZBX_STYLE_CURSOR_MOVE); 94 } 95 96 if ($controls) { 97 $this->header->addItem(new CList($controls)); 98 } 99 100 return $this; 101 } 102 103 /** 104 * Set widget header with left and right parts. 105 * 106 * @param string|array|CTag $leftColumn 107 * @param string|array|CTag $rightColumn 108 */ 109 public function setDoubleHeader($leftColumn, $rightColumn) { 110 $leftColumn = (new CCol($leftColumn))->addStyle('text-align: left; border: 0;'); 111 $rightColumn = (new CCol($rightColumn))->addStyle('text-align: right; border: 0;'); 112 113 $table = new CTable(); 114 $table->addStyle('width: 100%;'); 115 $table->addRow([$leftColumn, $rightColumn]); 116 117 $this->header = (new CDiv($table)) 118 ->addClass(ZBX_STYLE_NOWRAP) 119 ->addClass('ui-widget-header header'); 120 return $this; 121 } 122 123 /** 124 * Set widget footer. 125 * 126 * @param string|array|CTag $footer 127 * @param bool $right 128 */ 129 public function setFooter($list) { 130 $this->footer = $list; 131 $this->footer->addClass(ZBX_STYLE_DASHBRD_WIDGET_FOOT); 132 return $this; 133 } 134 135 /** 136 * Build widget header, body and footer. 137 */ 138 protected function build() { 139 $body = (new CDiv($this->body)) 140 ->setId($this->id); 141 142 $this->cleanItems(); 143 144 $this->addItem($this->header); 145 $this->addItem($body); 146 $this->addItem($this->footer); 147 return $this; 148 } 149 150 /** 151 * Get widget html. 152 */ 153 public function toString($destroy = true) { 154 $this->build(); 155 156 return parent::toString($destroy); 157 } 158} 159