values) && $this->values['units'] !== '') { $this->is_binary_units = isBinaryUnits($this->values['units']); } elseif (array_key_exists('itemid', $this->values)) { $items = API::Item()->get([ 'output' => ['units'], 'itemids' => $this->values['itemid'], 'webitems' => true ]); $this->is_binary_units = $items && isBinaryUnits($items[0]['units']); } } public function validate(bool $strict = false): array { $errors = parent::validate($strict); if ($errors) { return $errors; } $number_parser = new CNumberParser([ 'with_size_suffix' => true, 'with_time_suffix' => true, 'is_binary_size' => $this->is_binary_units ]); $number_parser->parse($this->getFieldValue('min')); $min = $number_parser->calcValue(); $number_parser->parse($this->getFieldValue('max')); $max = $number_parser->calcValue(); if ($min >= $max) { $errors[] = _s('Invalid parameter "%1$s": %2$s.', _('Max'), _s('value must be greater than "%1$s"', $min)); } $min_threshold = null; $max_threshold = null; foreach ($this->getFieldValue('thresholds') as $threshold) { $number_parser->parse($threshold['threshold']); $threshold_value = $number_parser->calcValue(); $min_threshold = $min_threshold !== null ? min($min_threshold, $threshold_value) : $threshold_value; $max_threshold = $max_threshold !== null ? max($max_threshold, $threshold_value) : $threshold_value; } if ($min_threshold !== null && $min_threshold < $min) { $errors[] = _s('Invalid parameter "%1$s": %2$s.', _('Thresholds'), _s('value must be no less than "%1$s"', $min) ); } if ($max_threshold !== null && $max_threshold > $max) { $errors[] = _s('Invalid parameter "%1$s": %2$s.', _('Thresholds'), _s('value must be no greater than "%1$s"', $max) ); } return $errors; } public function addFields(): self { return $this ->addField( (new CWidgetFieldMultiSelectItem('itemid', _('Item'))) ->setFlags(CWidgetField::FLAG_NOT_EMPTY | CWidgetField::FLAG_LABEL_ASTERISK) ->setMultiple(false) ) ->addField( (new CWidgetFieldRadioButtonList('angle', _('Angle'), array_combine(self::ANGLES, array_map(static fn(int $angle): string => $angle.'°', self::ANGLES)) )) ->setDefault(self::DEFAULT_ANGLE) ) ->addField( (new CWidgetFieldNumericBox('min', _('Min'))) ->setDefault(self::DEFAULT_MIN) ->setFlags(CWidgetField::FLAG_NOT_EMPTY | CWidgetField::FLAG_LABEL_ASTERISK) ) ->addField( (new CWidgetFieldNumericBox('max', _('Max'))) ->setDefault(self::DEFAULT_MAX) ->setFlags(CWidgetField::FLAG_NOT_EMPTY | CWidgetField::FLAG_LABEL_ASTERISK) ) ->addField( (new CWidgetFieldTextArea('description', _('Description'))) ->setDefault('{ITEM.NAME}') ->setFlags(CWidgetField::FLAG_NOT_EMPTY | CWidgetField::FLAG_LABEL_ASTERISK) ) ->addField( (new CWidgetFieldIntegerBox('desc_size', _('Size'), self::SIZE_PERCENT_MIN, self::SIZE_PERCENT_MAX)) ->setFullName(_('Description size')) ->setDefault(self::DEFAULT_DESCRIPTION_SIZE_PERCENT) ) ->addField( (new CWidgetFieldCheckBox('desc_bold', _('Bold')))->setDefault(self::DEFAULT_DESCRIPTION_BOLD) ) ->addField( (new CWidgetFieldRadioButtonList('desc_v_pos', _('Vertical position'), [ Widget::DESC_V_POSITION_TOP => _('Top'), Widget::DESC_V_POSITION_BOTTOM => _('Bottom') ]))->setDefault(Widget::DESC_V_POSITION_BOTTOM) ) ->addField( new CWidgetFieldColor('desc_color', _('Color')) ) ->addField( (new CWidgetFieldIntegerBox('decimal_places', _('Decimal places'), self::DECIMAL_PLACES_MIN, self::DECIMAL_PLACES_MAX )) ->setDefault(self::DEFAULT_DECIMAL_PLACES) ->setFlags(CWidgetField::FLAG_NOT_EMPTY) ) ->addField( (new CWidgetFieldCheckBox('value_bold', _('Bold')))->setDefault(self::DEFAULT_VALUE_BOLD) ) ->addField( (new CWidgetFieldCheckBox('value_arc', _('Arc')))->setDefault(self::DEFAULT_VALUE_ARC_SHOW) ) ->addField( (new CWidgetFieldIntegerBox('value_size', _('Size'), self::SIZE_PERCENT_MIN, self::SIZE_PERCENT_MAX)) ->setFullName(_('Value size')) ->setDefault(self::DEFAULT_VALUE_SIZE_PERCENT) ) ->addField( new CWidgetFieldColor('value_color', _('Color')) ) ->addField( (new CWidgetFieldIntegerBox('value_arc_size', _('Arc size'), self::SIZE_PERCENT_MIN, self::SIZE_PERCENT_MAX ))->setDefault(self::DEFAULT_VALUE_ARC_SIZE_PERCENT) ) ->addField( new CWidgetFieldColor('value_arc_color', _('Value arc')) ) ->addField( new CWidgetFieldColor('empty_color', _('Arc background')) ) ->addField( new CWidgetFieldColor('bg_color', _('Background')) ) ->addField( (new CWidgetFieldCheckBox('units_show', _('Units')))->setDefault(self::DEFAULT_UNITS_SHOW) ) ->addField( new CWidgetFieldTextBox('units', _('Units')) ) ->addField( (new CWidgetFieldIntegerBox('units_size', _('Size'),self::SIZE_PERCENT_MIN, self::SIZE_PERCENT_MAX)) ->setFullName(_('Units size')) ->setDefault(self::DEFAULT_UNITS_SIZE_PERCENT) ) ->addField( (new CWidgetFieldSelect('units_pos', _('Position'), [ Widget::UNITS_POSITION_BEFORE => _('Before value'), Widget::UNITS_POSITION_ABOVE => _('Above value'), Widget::UNITS_POSITION_AFTER => _('After value'), Widget::UNITS_POSITION_BELOW => _('Below value') ]))->setDefault(Widget::UNITS_POSITION_AFTER) ) ->addField( (new CWidgetFieldCheckBox('units_bold', _('Bold')))->setDefault(self::DEFAULT_UNITS_BOLD) ) ->addField( new CWidgetFieldColor('units_color', _('Color')) ) ->addField( (new CWidgetFieldCheckBox('needle_show', _('Needle')))->setDefault(self::DEFAULT_NEEDLE_SHOW) ) ->addField( new CWidgetFieldColor('needle_color', _('Color')) ) ->addField( (new CWidgetFieldCheckBox('scale_show', _('Scale')))->setDefault(self::DEFAULT_SCALE_SHOW) ) ->addField( (new CWidgetFieldIntegerBox('scale_decimal_places', _('Decimal places'), self::DECIMAL_PLACES_MIN, self::DECIMAL_PLACES_MAX )) ->setDefault(self::DEFAULT_SCALE_DECIMAL_PLACES) ->setFlags(CWidgetField::FLAG_NOT_EMPTY) ) ->addField( (new CWidgetFieldIntegerBox('scale_size', _('Size'), self::SIZE_PERCENT_MIN, self::SIZE_PERCENT_MAX)) ->setFullName(_('Scale size')) ->setDefault(self::DEFAULT_SCALE_SIZE_PERCENT) ) ->addField( (new CWidgetFieldCheckBox('scale_show_units', _('Show units'))) ->setDefault(self::DEFAULT_SCALE_SHOW_UNITS) ) ->addField( new CWidgetFieldThresholds('thresholds', _('Thresholds'), $this->is_binary_units) ) ->addField( (new CWidgetFieldCheckBox('th_show_labels', _('Show labels')))->setDefault(self::DEFAULT_TH_SHOW_LABELS) ) ->addField( (new CWidgetFieldCheckBox('th_show_arc', _('Show arc')))->setDefault(self::DEFAULT_TH_SHOW_ARC) ) ->addField( (new CWidgetFieldIntegerBox('th_arc_size', _('Arc size'), self::SIZE_PERCENT_MIN, self::SIZE_PERCENT_MAX ))->setDefault(self::DEFAULT_TH_ARC_SIZE_PERCENT) ) ->addField($this->isTemplateDashboard() ? null : new CWidgetFieldCheckBox('dynamic', _('Enable host selection')) ); } }