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
|
<?php declare(strict_types = 0);
/*
** 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 CWidgetFieldsGroupView extends CDiv {
protected array $fields = [];
protected array $label_class_list = [];
protected string $label;
protected ?CTag $field_hint = null;
public function __construct(string $label, array $fields = []) {
parent::__construct();
$this->label = $label;
foreach ($fields as $field) {
$this->addItem($field);
}
$this->addClass(CFormGrid::ZBX_STYLE_FIELDS_GROUP);
}
public function getFields(): array {
return $this->fields;
}
public function addField(?CWidgetFieldView $field): self {
return $this->addItem($field);
}
public function addItem($value): self {
if ($value === null) {
return $this;
}
if (is_string($value)) {
$value = $this->encode($value, $this->getEncStrategy());
}
$this->fields[] = $value;
return $this;
}
public function getLabel(): CLabel {
$focusable_element_id = null;
if ($this->fields && $this->fields[0] instanceof CWidgetFieldView) {
$label = $this->fields[0]->getLabel();
if ($label === null) {
$focusable_element_id = $this->fields[0]->getFocusableElementId();
}
}
return (new CLabel([$this->label, $this->field_hint], $focusable_element_id))
->addClass(CFormGrid::ZBX_STYLE_FIELDS_GROUP_LABEL)
->addClass($this->label_class_list ? implode(' ', $this->label_class_list) : null);
}
public function setFieldHint(CTag $hint): self {
$this->field_hint = $hint;
return $this;
}
public function addLabelClass(?string $label_class): self {
if ($label_class !== null) {
$this->label_class_list[] = $label_class;
}
return $this;
}
public function addRowClass(?string $row_class): self {
$this->addLabelClass($row_class);
$this->addClass($row_class);
return $this;
}
protected function bodyToString() {
$collection = [];
foreach ($this->fields as $field) {
if ($field instanceof CWidgetFieldView) {
foreach ($field->getViewCollection() as ['label' => $label, 'view' => $view, 'class' => $class]) {
$collection[] = [$label, (new CFormField($view))->addClass($class)];
}
}
else {
$collection[] = $field;
}
}
return unpack_object($collection);
}
}
|