Source
<?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/>.
**/
use Zabbix\Widgets\CWidgetField;
use Zabbix\Widgets\Fields\CWidgetFieldReference;
class CWidgetFormView {
private array $data;
private string $name;
private array $vars = [];
private array $javascript = [];
private array $templates = [];
private CFormGrid $form_grid;
private array $registered_fields = [];
public function __construct(array $data, string $name = 'widget_dialogue_form') {
$this->data = $data;
$this->name = $name;
if (array_key_exists(CWidgetFieldReference::FIELD_NAME, $data['fields'])) {
$this->addFieldVar($data['fields'][CWidgetFieldReference::FIELD_NAME]);
}
$this->makeFormGrid();
}
/**
* Add fieldset with multiple CWidgetFieldView-s as content.
*/
public function addFieldset(?CWidgetFormFieldsetCollapsibleView $fieldset): self {
if ($fieldset === null) {
return $this;
}
foreach ($fieldset->getFields() as $field) {
if ($field instanceof CWidgetFieldView) {
$this->registerField($field);
}
}
return $this->addItem($fieldset);
}
/**
* Add configuration row with single label and multiple CWidgetFieldView-s as content.
*/
public function addFieldsGroup(?CWidgetFieldsGroupView $fields_group): self {
if ($fields_group === null) {
return $this;
}
foreach ($fields_group->getFields() as $field) {
if ($field instanceof CWidgetFieldView) {
$this->registerField($field);
}
}
return $this->addItem([$fields_group->getLabel(), $fields_group]);
}
/**
* Add configuration row based on single CWidgetFieldView.
*/
public function addField(?CWidgetFieldView $field): self {
if ($field === null || in_array($field->getName(), $this->registered_fields, true)) {
return $this;
}
$this->registerField($field);
foreach ($field->getViewCollection() as ['label' => $label, 'view' => $view, 'class' => $class]) {
$this->form_grid->addItem([$label, (new CFormField($view))->addClass($class)]);
}