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
|
<?php
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
class CTag extends CObject {
/**
* The list of tag attributes.
*
* @var array
*/
protected $attributes = [];
/**
* The name of the tag.
*
* @var string
*/
protected $tagname = '';
/**
* The type of tag.
*
* @var bool
*/
private $paired = false;
public function __construct(string $tagname, bool $paired = false, $body = null) {
parent::__construct();
$this->tagname = $tagname;
$this->paired = $paired;
if (!is_null($body)) {
$this->addItem($body);
}
}
protected function startToString() {
$attributes = '';
foreach ($this->attributes as $key => $value) {
if ($value !== null) {
$attributes .= ' '.$key.'="'.htmlspecialchars($value, ENT_QUOTES, 'UTF-8').'"';
}
}
return '<'.$this->tagname.$attributes.'>';
}
protected function bodyToString() {
return parent::toString(false);
}
protected function endToString() {
$res = ($this->paired) ? '</'.$this->tagname.'>' : '';
return $res;
}
public function toString($destroy = true) {
$res = $this->startToString();
$res .= $this->bodyToString();
$res .= $this->endToString();
if ($destroy) {
$this->destroy();
}
return $res;
}
public function addItem($value) {
if (is_string($value)) {
$value = htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8');
}
parent::addItem($value);
return $this;
}
public function setName($value) {
$this->setAttribute('name', $value);
return $this;
}
public function getName() {
return $this->getAttribute('name');
}
public function addClass($class) {
if ($class !== null) {
if (!array_key_exists('class', $this->attributes) || $this->attributes['class'] === '') {
$this->attributes['class'] = $class;
}
else {
$this->attributes['class'] .= ' '.$class;
}
}
return $this;
}
public function getAttribute($name) {
return isset($this->attributes[$name]) ? $this->attributes[$name] : null;
}
public function setAttribute($name, $value) {
if (is_object($value)) {
$value = unpack_object($value);
}
elseif (is_array($value)) {
$value = json_encode($value);
}
$this->attributes[$name] = $value;
return $this;
}
public function removeAttribute($name) {
unset($this->attributes[$name]);
return $this;
}
private function addAction($name, $value) {
$this->attributes[$name] = $value;
return $this;
}
/**
* Adds a hint box to the element.
*
* @param string|array|CTag $text Hint content.
* @param string $span_class Wrap the content in a span element and assign this class
* to the span.
* @param bool $freeze_on_click If set to true, it will be possible to "freeze" the hint box
* via a mouse click.
* @param string $styles Custom css styles.
* Syntax:
* property1: value1; property2: value2; property(n): value(n)
* @param int $delay Delay in milliseconds before showing hintbox.
* @return CTag
*/
public function setHint($text, $span_class = '', $freeze_on_click = true, $styles = '', $delay = null) {
$this->setAttribute('data-hintbox-contents', (new CTag('', false, $text))->bodyToString());
$this->setAttribute('data-hintbox', '1');
if ($span_class !== '') {
$this->setAttribute('data-hintbox-class', $span_class);
}
if ($styles !== '') {
$this->setAttribute('data-hintbox-style', $styles);
}
if ($freeze_on_click) {
$this->setAttribute('data-hintbox-static', '1');
}
if ($delay !== null) {
$this->setAttribute('data-hintbox-delay', $delay);
}
return $this;
}
/**
* Add a hint box with preloader to the element.
*
* @param array $data
* @param string $span_class
* @param bool $freeze_on_click
* @param string $styles
*
* @return $this
*/
public function setAjaxHint(array $data, string $span_class = '', bool $freeze_on_click = true,
string $styles = ''): CTag {
$this
->setAttribute('data-hintbox-preload', $data)
->setHint('', $span_class, $freeze_on_click, $styles);
return $this;
}
/**
* Set data for menu popup.
*
* @param array $data
*/
public function setMenuPopup(array $data) {
$this->setAttribute('data-menu-popup', $data);
$this->setAttribute('aria-expanded', 'false');
if (!$this->getAttribute('disabled')) {
$this->setAttribute('aria-haspopup', 'true');
}
return $this;
}
public function onChange($script) {
$this->addAction('onchange', $script);
return $this;
}
public function onClick($script) {
$this->addAction('onclick', $script);
return $this;
}
public function onMouseover($script) {
$this->addAction('onmouseover', $script);
return $this;
}
public function onMouseout($script) {
$this->addAction('onmouseout', $script);
return $this;
}
public function onKeyup($script) {
$this->addAction('onkeyup', $script);
return $this;
}
public function addStyle($value) {
if (!isset($this->attributes['style'])) {
$this->attributes['style'] = '';
}
if (isset($value)) {
$this->attributes['style'] .= $value;
}
else {
unset($this->attributes['style']);
}
return $this;
}
public function error($value) {
error('class('.get_class($this).') - '.$value);
return 1;
}
public function setTitle($value) {
$this->setAttribute('title', $value);
return $this;
}
public function setId($id) {
$this->setAttribute('id', $id);
return $this;
}
public function getId() {
return $this->getAttribute('id');
}
/**
* Remove ID attribute from tag.
*
* @return CTag
*/
public function removeId() {
$this->removeAttribute('id');
return $this;
}
/**
* Set or reset element 'aria-required' attribute.
*
* @param bool $is_required Define aria-required attribute for element.
*
* @return $this
*/
public function setAriaRequired($is_required = true) {
if ($is_required) {
$this->setAttribute('aria-required', 'true');
}
else {
$this->removeAttribute('aria-required');
}
return $this;
}
}
|