You are viewing documentation for the development version, it may be incomplete.
Join our translation project and help translate Zabbix documentation into your native language.

Configuratie

Deze pagina beschrijft klassen die kunnen worden gebruikt om een widgetconfiguratieweergave met aangepaste configuratievelden te maken. De widgetconfiguratieweergave is het deel van de widget waarmee de gebruiker widgetparameters kan configureren voor presentatie.

Widget

Primaire widgetklasse, breidt de basisklasse uit van alle dashboardwidgets - CWidget. Nodig om het standaard widgetgedrag te overschrijven.

De Widget klasse moet zich bevinden in de hoofdmap van de widget (bijvoorbeeld ui/modules/my_custom_widget).

Voorbeeld Widget.php

<?php
       
       namespace Modules\MyCustomWidget;
       
       use Zabbix\Core\CWidget;
       
       class Widget extends CWidget {
       
           public const MY_CONSTANT = 0;
       
           public function getTranslationStrings(): array {
               return [
                   'class.widget.js' => [
                       'No data' => _('Geen gegevens')
                   ]
               ];
           }
       }

WidgetForm

De klasse WidgetForm breidt de standaardklasse CWidgetForm uit en bevat een reeks CWidgetField velden die nodig zijn voor het definiëren van de structuur voor het opslaan van widgetconfiguraties in de database en het afhandelen van invoervalidatie.

De WidgetForm klasse moet zich bevinden in de includes map. Als de klasse een andere naam heeft, moet de naam worden gespecificeerd in de widget/form_class parameter in het manifest.json bestand.

Voorbeeld WidgetForm.php

<?php
       
       namespace Modules\MyCustomWidget\Includes;
       
       use Modules\MyCustomWidget\Widget;
       
       use Zabbix\Widgets\{
           CWidgetField,
           CWidgetForm
       };
       
       use Zabbix\Widgets\Fields\{
           CWidgetFieldMultiSelectItem,
           CWidgetFieldTextBox,
           CWidgetFieldColor
       };
       
       class WidgetForm extends CWidgetForm {
       
           public function addFields(): self {
               return $this
                   ->addField(
                       (new CWidgetFieldMultiSelectItem('itemid', _('Item')))
                           ->setFlags(CWidgetField::FLAG_NOT_EMPTY | CWidgetField::FLAG_LABEL_ASTERISK)
                           ->setMultiple(false)
                           ->setFilterParameter('numeric', true)
                   )
                   ->addField(
                       new CWidgetFieldTextBox('description', _('Description'))
                   )
                   ->addField(
                       (new CWidgetFieldColor('chart_color', _('Color')))->setDefault('FF0000')
                   );
           }
       }

CWidgetFormView

De klasse CWidgetFormView is vereist voor het specificeren van de presentatielogica van de velden die zijn gedefinieerd in de WidgetForm klasse, waarbij hun uiterlijk en gedrag worden bepaald wanneer ze worden weergegeven in de configuratie weergave.

De klasse CWidgetFormView ondersteunt de volgende methoden:

  • addField() - ontvangt een instantie van de CWidgetFieldView klasse als parameter; elke CWidgetField klasse heeft een respectieve CWidgetFieldView klasse voor gebruik in de widget configuratie weergave.
  • includeJsFile() - stelt u in staat om een JavaScript-bestand toe te voegen aan de widget configuratie weergave.
  • addJavaScript() - stelt u in staat om inline JavaScript toe te voegen dat wordt uitgevoerd zodra de widget configuratie weergave wordt geladen.

De klasse CWidgetFormView moet zich bevinden in de views map.

Voorbeeld widget.edit.php

<?php
           
       /**
        * Mijn aangepaste widget formulierweergave.
        *
        * @var CView $this
        * @var array $data
        */
       
       use Zabbix\Widgets\Fields\CWidgetFieldGraphDataSet;
       
       (new CWidgetFormView($data))
           ->addField(
               new CWidgetFieldMultiSelectItemView($data['fields']['itemid'], $data['captions']['items']['itemid'])
           )
           ->addField(
               new CWidgetFieldTextBoxView($data['fields']['description']),
               'js-advanced-configuration'
           )
           ->addField(
               new CWidgetFieldColorView($data['fields']['chart_color']),
               'js-advanced-configuration'
           )
           ->includeJsFile('widget.edit.js.php')
           ->addJavaScript('my_widget_form.init('.json_encode([
               'color_palette' => CWidgetFieldGraphDataSet::DEFAULT_COLOR_PALETTE
           ], JSON_THROW_ON_ERROR).');')
           ->show();

JavaScript

A JavaScript class can be used to add dynamic behavior and interactivity to the widget configuration view. For example, you can initialize a color picker, defined in the CWidgetFormView class.

The JavaScript class should be loaded with the form, therefore it should be referenced in the CWidgetFormView class by using the methods includeJsFile() and addJavaScript().

In the example below, a singleton class instance is immediately created and stored under the window.my_custom_widget_form name. Thus, opening the form for the second time will re-create the instance.

The JavaScript class should be located in the views directory.

views/widget.edit.js.php example

<?php
       
       use Modules\MyCustomWidget\Widget;
       
       ?>
       
       window.my_custom_widget_form = new class {
       
           init({color_palette}) {
               colorPalette.setThemeColors(color_palette);
       
               for (const colorpicker of jQuery('.<?= ZBX_STYLE_COLOR_PICKER ?> input')) {
                   jQuery(colorpicker).colorpicker();
               }
       
               const overlay = overlays_stack.getById('widget_properties');
       
               for (const event of ['overlay.reload', 'overlay.close']) {
                   overlay.$dialogue[0].addEventListener(event, () => { jQuery.colorpicker('hide'); });
               }
           }
       };

CWidgetField

The CWidgetField class is a base class from which all form field classes (CWidgetFieldCheckBox, CWidgetFieldTextArea, CWidgetFieldRadioButtonList, etc.) are inherited. Classes extending CWidgetField are responsible for receiving, saving, and validating widget configuration values.

The following CWidgetField classes are available.

CWidgetField class Database field type Description
CWidgetFieldCheckBox int32 Single checkbox.
CWidgetFieldCheckBoxList array of int32 Multiple checkboxes under a single configuration field.
CWidgetFieldColor string Color selection field.
CWidgetFieldDatePicker string Date selection field.
CWidgetFieldHostPatternSelect string Multiselect field that allows to select one or multiple hosts. Supports defining host name patterns (all matching hosts will be selected).
CWidgetFieldIntegerBox int32 Field to enter an integer. Can be used to configure minimum and maximum values.
CWidgetFieldLatLng string Text box that allows to enter comma-separated latitude, longitude, and map zoom level.
CWidgetFieldMultiSelectAction ID Multiselect field for selecting actions (from the list of actions defined in the Alerts → Actions).
CWidgetFieldMultiSelectGraph ID Multiselect field for selecting custom graphs.
CWidgetFieldMultiSelectGraphPrototype ID Multiselect field for selecting custom graph prototypes.
CWidgetFieldMultiSelectGroup ID Multiselect field for selecting host groups.
CWidgetFieldMultiSelectHost ID Multiselect field for selecting hosts.
CWidgetFieldMultiSelectItem ID Multiselect field for selecting items.
CWidgetFieldMultiSelectItemPrototype ID Multiselect field for selecting item prototypes.
CWidgetFieldMultiSelectMediaType ID Multiselect field for selecting media types.
CWidgetFieldMultiSelectService ID Multiselect field for selecting services.
CWidgetFieldMultiSelectSla ID Multiselect field for selecting SLAs.
CWidgetFieldMultiSelectUser ID Multiselect field for selecting users.
CWidgetFieldNumericBox string Field to enter a float number.
CWidgetFieldRadioButtonList int32 Radio box group that consists of one or more radio boxes.
CWidgetFieldRangeControl int32 Slider to select an integer type value.
CWidgetFieldSelect int32 Dropdown select box.
CWidgetFieldSeverities array of int32 CWidgetFieldCheckBoxList preset with trigger severities.
CWidgetFieldTags array of (string, int32, string) Allows to configure one or more tag filter rows.
CWidgetFieldTextArea string Text area for entering multi-line text.
CWidgetFieldTextBox string Text box for entering single-line text.
CWidgetFieldTimeZone string Dropdown with timezones.
CWidgetFieldUrl string Text box that allows to enter URLs.

The following CWidgetField classes have been created for particular widgets. These classes have very specific use cases, but they can also be reused if needed.

CWidgetField class Database field type Description
CWidgetFieldColumnsList array of (multiple mixed) For top host widget. Create a table with custom columns of allowed types.
CWidgetFieldGraphDataSet array of (multiple mixed) For graph widget. Setup dataset configuration and all related options.
CWidgetFieldGraphOverride array of (multiple mixed) For graph widget. Setup overrides for specific hosts/items. Any dataset configuration can be overridden.
CWidgetFieldNavTree string For map navigation tree widget. Replaces widget view in edit mode with the map selection tree.
CWidgetFieldReference string For map navigation tree widget. Creates a unique identifier for this widget on dashboard. It is used to reference this widget from other widgets.
CWidgetFieldSelectResource ID For map widget. Allows to select Zabbix network map.
CWidgetFieldThresholds array of (string, string) For top hosts widget. Allows to configure color and number pairs.
CWidgetFieldWidgetSelect string For map widget. Allows to select a map navigation tree from the current dashboard. Must be used in combination with CWidgetFieldReference in the Map Navigation Tree widget.