<?php declare(strict_types = 0);
namespace Zabbix\Widgets\Fields;
use Zabbix\Widgets\CWidgetField;
use CWidgetsData;
class CWidgetFieldSparkline extends CWidgetField {
public const DEFAULT_VIEW = \CWidgetFieldSparklineView::class;
public const DEFAULT_VALUE = [];
public const DATA_SOURCE_AUTO = 0;
public const DATA_SOURCE_HISTORY = 1;
public const DATA_SOURCE_TRENDS = 2;
protected array $options;
protected array $fields = [];
public function __construct(string $name, string $label = null, array $options = []) {
parent::__construct($name, $label);
$options = array_replace_recursive([
'width' => ['min' => 0, 'max' => 10, 'step' => 1],
'fill' => ['min' => 0, 'max' => 10, 'step' => 1],
'color' => ['use_default' => false],
'time_period' => [
'default_period' => ['from' => 'now-1h', 'to' => 'now']
]
], $options);
$this->options = $options;
$this->fields = [
'width' => new CWidgetFieldRangeControl(
$this->getFormFieldName('width'), _('Width'),
$options['width']['min'], $options['width']['max'], $options['width']['step']
),
'fill' => new CWidgetFieldRangeControl(
$this->getFormFieldName('fill'), _('Fill'),
$options['fill']['min'], $options['fill']['max'], $options['fill']['step']
),
'color' => (new CWidgetFieldColor($this->getFormFieldName('color'), _('Color')))
->allowInherited($options['color']['use_default']),
'time_period' => (new CWidgetFieldTimePeriod(
$this->getFormFieldName('time_period'), _('Time period'))
)
->setDefaultPeriod($options['time_period']['default_period'])
->setDefault([
CWidgetField::FOREIGN_REFERENCE_KEY => CWidgetField::createTypedReference(
CWidgetField::REFERENCE_DASHBOARD, CWidgetsData::DATA_TYPE_TIME_PERIOD
)
])
->setFlags(CWidgetField::FLAG_NOT_EMPTY | CWidgetField::FLAG_LABEL_ASTERISK)
->acceptWidget()
->acceptDashboard(),
'history' => (new CWidgetFieldRadioButtonList(
$this->getFormFieldName('history'), _('History data'), [
self::DATA_SOURCE_AUTO => _('Auto'),
self::DATA_SOURCE_HISTORY => _('History'),
self::DATA_SOURCE_TRENDS => _('Trends')
]))->setDefault(self::DATA_SOURCE_AUTO)
];
if ($label !== null) {
foreach ($this->fields as $field) {
$field->prefixLabel($label);
}
}
$this->setDefault(static::DEFAULT_VALUE);
}
public function getFormFieldName($name): string {
return sprintf('%1$s[%2$s]', $this->name, $name);
}