1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2019 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
class CWidgetField {
const FLAG_ACKNOWLEDGES = 0x01;
const FLAG_NOT_EMPTY = 0x02;
const FLAG_LABEL_ASTERISK = 0x04;
const FLAG_DISABLED = 0x08;
protected $name;
protected $label;
protected $value;
protected $default;
protected $save_type;
protected $action;
private $validation_rules = [];
private $strict_validation_rules = null;
private $ex_validation_rules = [];
private $flags;
/**
* Create widget field (general)
*
* @param string $name Field name in form.
* @param string $label Label for the field in form.
*/
public function __construct($name, $label = null) {
$this->name = $name;
$this->label = $label;
$this->value = null;
$this->setSaveType(ZBX_WIDGET_FIELD_TYPE_STR);
$this->flags = 0x00;
}
public function setValue($value) {
$this->value = $value;
return $this;
}
public function setDefault($value) {
$this->default = $value;
return $this;
}
/**
* Set JS code that will be called on field change.
*
* @param string $action JS function to call on field change.
*
* @return $this
*/
public function setAction($action) {
$this->action = $action;
return $this;
}
protected function setSaveType($save_type) {
switch ($save_type) {
case ZBX_WIDGET_FIELD_TYPE_INT32:
$this->validation_rules = ['type' => API_INT32];
break;
case ZBX_WIDGET_FIELD_TYPE_STR:
$this->validation_rules = ['type' => API_STRING_UTF8, 'length' => 255];
break;
case ZBX_WIDGET_FIELD_TYPE_ITEM:
case ZBX_WIDGET_FIELD_TYPE_GROUP:
case ZBX_WIDGET_FIELD_TYPE_HOST:
$this->validation_rules = ['type' => API_IDS];
break;
case ZBX_WIDGET_FIELD_TYPE_MAP:
case ZBX_WIDGET_FIELD_TYPE_GRAPH:
$this->validation_rules = ['type' => API_ID];
break;
default:
exit(_('Internal error.'));
}
$this->save_type = $save_type;
return $this;
}
protected function setValidationRules(array $validation_rules) {
$this->validation_rules = $validation_rules;
}
protected function getValidationRules() {
return $this->validation_rules;
}
/**
* Set validation rules for "strict" mode.
*
* @param array|null $strict_validation_rules
*/
protected function setStrictValidationRules(array $strict_validation_rules = null) {
$this->strict_validation_rules = $strict_validation_rules;
}
protected function setExValidationRules(array $ex_validation_rules) {
$this->ex_validation_rules = $ex_validation_rules;
}
/**
* Get field value. If no value is set, will return default value.
*
* @return mixed
*/
public function getValue() {
return ($this->value === null) ? $this->default : $this->value;
}
public function getLabel() {
return $this->label;
}
public function getName() {
return $this->name;
}
public function getAction() {
return $this->action;
}
public function getSaveType() {
return $this->save_type;
}
/**
* Set additional flags for validation rule array.
*
* @param array $validation_rule
* @param int $flag
*
*/
protected static function setValidationRuleFlag(array &$validation_rule, $flag) {
if (array_key_exists('flags', $validation_rule)) {
$validation_rule['flags'] |= $flag;
}
else {
$validation_rule['flags'] = $flag;
}
}
/**
* Set additional flags, which can be used in configuration form.
*
* @param int $flags
*
* @return $this
*/
public function setFlags($flags) {
$this->flags = $flags;
return $this;
}
/**
* Get additional flags, which can be used in configuration form.
*
* @return int
*/
public function getFlags() {
return $this->flags;
}
/**
* Validate field.
*
* @param bool $strict Enables more strict validation of the field.
*
* @return bool
*/
public function validate($strict = false) {
$errors = [];
$validation_rules = ($strict && $this->strict_validation_rules !== null)
? $this->strict_validation_rules
: $this->validation_rules;
$validation_rules += $this->ex_validation_rules;
$value = $this->getValue();
$label = ($this->label === null) ? $this->name : $this->label;
if (!CApiInputValidator::validate($validation_rules, $value, $label, $error)) {
$this->setValue($this->default);
$errors[] = $error;
}
return $errors;
}
/**
* Prepares array entry for widget field, ready to be passed to CDashboard API functions.
* Reference is needed here to avoid array merging in CWidgetForm::fieldsToApi method. With large number of widget
* fields it causes significant performance decrease.
*
* @param array $widget_fields reference to Array of widget fields.
*/
public function toApi(array &$widget_fields = []) {
$value = $this->getValue();
if ($value !== null && $value !== $this->default) {
$widget_field = [
'type' => $this->save_type,
'name' => $this->name
];
if (is_array($value)) {
foreach ($value as $val) {
$widget_field['value'] = $val;
$widget_fields[] = $widget_field;
}
}
else {
$widget_field['value'] = $value;
$widget_fields[] = $widget_field;
}
}
}
}
|