1<?php 2/* 3** Zabbix 4** Copyright (C) 2001-2021 Zabbix SIA 5** 6** This program is free software; you can redistribute it and/or modify 7** it under the terms of the GNU General Public License as published by 8** the Free Software Foundation; either version 2 of the License, or 9** (at your option) any later version. 10** 11** This program is distributed in the hope that it will be useful, 12** but WITHOUT ANY WARRANTY; without even the implied warranty of 13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14** GNU General Public License for more details. 15** 16** You should have received a copy of the GNU General Public License 17** along with this program; if not, write to the Free Software 18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19**/ 20 21 22/** 23 * Class for override widget field used in Graph widget configuration overrides tab. 24 */ 25class CWidgetFieldGraphOverride extends CWidgetField { 26 27 /** 28 * Create widget field for Graph Override selection. 29 * 30 * @param string $name Field name in form. 31 * @param string $label Label for the field in form. 32 */ 33 public function __construct($name, $label) { 34 parent::__construct($name, $label); 35 36 $this->setSaveType(ZBX_WIDGET_FIELD_TYPE_STR); 37 $this->setValidationRules(['type' => API_OBJECTS, 'fields' => [ 38 'hosts' => ['type' => API_STRINGS_UTF8, 'flags' => API_REQUIRED], 39 'items' => ['type' => API_STRINGS_UTF8, 'flags' => API_REQUIRED], 40 'color' => ['type' => API_COLOR], 41 'type' => ['type' => API_INT32, 'in' => implode(',', [SVG_GRAPH_TYPE_LINE, SVG_GRAPH_TYPE_POINTS, SVG_GRAPH_TYPE_STAIRCASE, SVG_GRAPH_TYPE_BAR])], 42 'width' => ['type' => API_INT32, 'in' => implode(',', range(0, 10))], 43 'pointsize' => ['type' => API_INT32, 'in' => implode(',', range(1, 10))], 44 'transparency' => ['type' => API_INT32, 'in' => implode(',', range(0, 10))], 45 'fill' => ['type' => API_INT32, 'in' => implode(',', range(0, 10))], 46 'missingdatafunc' => ['type' => API_INT32, 'in' => implode(',', [SVG_GRAPH_MISSING_DATA_NONE, SVG_GRAPH_MISSING_DATA_CONNECTED, SVG_GRAPH_MISSING_DATA_TREAT_AS_ZERO])], 47 'axisy' => ['type' => API_INT32, 'in' => implode(',', [GRAPH_YAXIS_SIDE_LEFT, GRAPH_YAXIS_SIDE_RIGHT])], 48 'timeshift' => ['type' => API_TIME_UNIT, 'in' => implode(':', [ZBX_MIN_TIMESHIFT, ZBX_MAX_TIMESHIFT])] 49 ]]); 50 $this->setDefault([]); 51 } 52 53 /** 54 * Set field values for the overrides. 55 * 56 * @return $this 57 */ 58 public function setValue($value) { 59 $overrides = []; 60 61 foreach ((array) $value as $override) { 62 $overrides[] = $override + self::getDefaults(); 63 } 64 65 return parent::setValue($overrides); 66 } 67 68 /** 69 * Set additional flags, which can be used in configuration form. 70 * 71 * @param int $flags 72 * 73 * @return $this 74 */ 75 public function setFlags($flags) { 76 parent::setFlags($flags); 77 78 if ($flags & self::FLAG_NOT_EMPTY) { 79 $strict_validation_rules = $this->getValidationRules(); 80 self::setValidationRuleFlag($strict_validation_rules['fields']['hosts'], API_NOT_EMPTY); 81 self::setValidationRuleFlag($strict_validation_rules['fields']['items'], API_NOT_EMPTY); 82 self::setValidationRuleFlag($strict_validation_rules['fields']['color'], API_NOT_EMPTY); 83 self::setValidationRuleFlag($strict_validation_rules['fields']['timeshift'], API_NOT_EMPTY); 84 $this->setStrictValidationRules($strict_validation_rules); 85 } 86 else { 87 $this->setStrictValidationRules(null); 88 } 89 90 return $this; 91 } 92 93 /** 94 * Default values filled in newly created data set or used as unspecified values. 95 * 96 * @return array 97 */ 98 public static function getDefaults() { 99 return [ 100 'hosts' => [], 101 'items' => [] 102 ]; 103 } 104 105 /** 106 * Returns array of supported override options. 107 * 108 * @return array 109 */ 110 public static function getOverrideOptions() { 111 return ['color', 'width', 'type', 'transparency', 'fill', 'pointsize', 'missingdatafunc', 'axisy', 'timeshift']; 112 } 113 114 /** 115 * Function makes field specific validation for values set using self::setValue(). 116 * 117 * @param bool $strict Either to make a strict validation. 118 * 119 * @return array $errors List of errors found during validation. 120 */ 121 public function validate($strict = false) { 122 $errors = parent::validate($strict); 123 $value = $this->getValue(); 124 $label = ($this->label === null) ? $this->name : $this->label; 125 126 // Validate options. 127 if (!$errors && $strict) { 128 foreach ($value as $index => $overrides) { 129 if (!array_intersect(self::getOverrideOptions(), array_keys($overrides))) { 130 $errors[] = _s('Invalid parameter "%1$s": %2$s.', $label.'/'.($index + 1), 131 _('at least one override option must be specified') 132 ); 133 break; 134 } 135 } 136 } 137 138 return $errors; 139 } 140 141 /** 142 * Prepares array entry for widget field, ready to be passed to CDashboard API functions. 143 * Reference is needed here to avoid array merging in CWidgetForm::fieldsToApi method. With large number of widget 144 * fields it causes significant performance decrease. 145 * 146 * @param array $widget_fields reference to Array of widget fields. 147 */ 148 public function toApi(array &$widget_fields = []) { 149 $value = $this->getValue(); 150 151 foreach ($value as $index => $val) { 152 // Hosts and items fields are stored as arrays to bypass length limit. 153 foreach ($val['hosts'] as $num => $pattern_item) { 154 $widget_fields[] = [ 155 'type' => ZBX_WIDGET_FIELD_TYPE_STR, 156 'name' => $this->name.'.hosts.'.$index.'.'.$num, 157 'value' => $pattern_item 158 ]; 159 } 160 foreach ($val['items'] as $num => $pattern_item) { 161 $widget_fields[] = [ 162 'type' => ZBX_WIDGET_FIELD_TYPE_STR, 163 'name' => $this->name.'.items.'.$index.'.'.$num, 164 'value' => $pattern_item 165 ]; 166 } 167 168 foreach (self::getOverrideOptions() as $opt) { 169 if (array_key_exists($opt, $val)) { 170 $widget_fields[] = [ 171 'type' => ($opt === 'color' || $opt === 'timeshift') 172 ? ZBX_WIDGET_FIELD_TYPE_STR 173 : ZBX_WIDGET_FIELD_TYPE_INT32, 174 'name' => $this->name.'.'.$opt.'.'.$index, 175 'value' => $val[$opt] 176 ]; 177 } 178 } 179 } 180 } 181} 182