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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2022 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 containing methods for operations with value maps.
*/
class CValueMap extends CApiService {
public const ACCESS_RULES = [
'get' => ['min_user_type' => USER_TYPE_ZABBIX_USER],
'create' => ['min_user_type' => USER_TYPE_ZABBIX_ADMIN],
'update' => ['min_user_type' => USER_TYPE_ZABBIX_ADMIN],
'delete' => ['min_user_type' => USER_TYPE_ZABBIX_ADMIN]
];
protected $tableName = 'valuemap';
protected $tableAlias = 'vm';
protected $sortColumns = ['valuemapid', 'name'];
/**
* Get value maps.
*
* @param array $options
*
* @return array
*/
public function get($options = []) {
$api_input_rules = ['type' => API_OBJECT, 'fields' => [
// filter
'valuemapids' => ['type' => API_IDS, 'flags' => API_ALLOW_NULL | API_NORMALIZE, 'default' => null],
'hostids' => ['type' => API_IDS, 'flags' => API_ALLOW_NULL | API_NORMALIZE, 'default' => null],
'filter' => ['type' => API_OBJECT, 'flags' => API_ALLOW_NULL, 'default' => null, 'fields' => [
'valuemapid' => ['type' => API_IDS, 'flags' => API_ALLOW_NULL | API_NORMALIZE],
'hostid' => ['type' => API_IDS, 'flags' => API_ALLOW_NULL | API_NORMALIZE],
'name' => ['type' => API_STRINGS_UTF8, 'flags' => API_ALLOW_NULL | API_NORMALIZE]
]],
'search' => ['type' => API_OBJECT, 'flags' => API_ALLOW_NULL, 'default' => null, 'fields' => [
'name' => ['type' => API_STRINGS_UTF8, 'flags' => API_ALLOW_NULL | API_NORMALIZE]
]],
'searchByAny' => ['type' => API_BOOLEAN, 'default' => false],
'startSearch' => ['type' => API_FLAG, 'default' => false],
'excludeSearch' => ['type' => API_FLAG, 'default' => false],
'searchWildcardsEnabled' => ['type' => API_BOOLEAN, 'default' => false],
// output
'output' => ['type' => API_OUTPUT, 'in' => implode(',', ['valuemapid', 'uuid', 'name', 'hostid']), 'default' => API_OUTPUT_EXTEND],
'selectMappings' => ['type' => API_OUTPUT, 'flags' => API_ALLOW_NULL | API_ALLOW_COUNT, 'in' => implode(',', ['type', 'value', 'newvalue']), 'default' => null],
'countOutput' => ['type' => API_FLAG, 'default' => false],
// sort and limit
'sortfield' => ['type' => API_STRINGS_UTF8, 'flags' => API_NORMALIZE, 'in' => implode(',', $this->sortColumns), 'uniq' => true, 'default' => []],
'sortorder' => ['type' => API_SORTORDER, 'default' => []],
'limit' => ['type' => API_INT32, 'flags' => API_ALLOW_NULL, 'in' => '1:'.ZBX_MAX_INT32, 'default' => null],
// flags
'editable' => ['type' => API_BOOLEAN, 'default' => false],
'preservekeys' => ['type' => API_BOOLEAN, 'default' => false]
]];
if (!CApiInputValidator::validate($api_input_rules, $options, '/', $error)) {
self::exception(ZBX_API_ERROR_PARAMETERS, $error);
}
$db_valuemaps = [];
$sql_parts = $this->createSelectQueryParts($this->tableName(), $this->tableAlias(), $options);
// Permission check.
if (self::$userData['type'] != USER_TYPE_SUPER_ADMIN) {
$permission = $options['editable'] ? PERM_READ_WRITE : PERM_READ;
$userGroups = getUserGroupsByUserId(self::$userData['userid']);
$sql_parts['where'][] = 'EXISTS ('.
'SELECT NULL'.
' FROM hosts_groups hgg'.
' JOIN rights r'.
' ON r.id=hgg.groupid'.
' AND '.dbConditionInt('r.groupid', $userGroups).
' WHERE vm.hostid=hgg.hostid'.
' GROUP BY hgg.hostid'.
' HAVING MIN(r.permission)>'.PERM_DENY.
' AND MAX(r.permission)>='.zbx_dbstr($permission).
')';
}
// hostids
if ($options['hostids'] !== null) {
$sql_parts['where']['hostid'] = dbConditionInt('vm.hostid', $options['hostids']);
}
$result = DBselect(self::createSelectQueryFromParts($sql_parts), $options['limit']);
while ($row = DBfetch($result)) {
if ($options['countOutput']) {
return $row['rowscount'];
}
$db_valuemaps[$row['valuemapid']] = $row;
}
if ($db_valuemaps) {
$db_valuemaps = $this->addRelatedObjects($options, $db_valuemaps);
$db_valuemaps = $this->unsetExtraFields($db_valuemaps, ['valuemapid'], $options['output']);
if (!$options['preservekeys']) {
$db_valuemaps = zbx_cleanHashes($db_valuemaps);
}
}
return $db_valuemaps;
}
/**
* @param array $valuemaps
*
* @return array
*
* @throws APIException
*/
public function create(array $valuemaps) {
$this->validateCreate($valuemaps);
$valuemapids = DB::insertBatch('valuemap', $valuemaps);
$mappings = [];
foreach ($valuemaps as $index => &$valuemap) {
$valuemap['valuemapid'] = $valuemapids[$index];
$sortorder = 0;
foreach ($valuemap['mappings'] as $mapping) {
$mappings[] = [
'type' => array_key_exists('type', $mapping) ? $mapping['type'] : VALUEMAP_MAPPING_TYPE_EQUAL,
'valuemapid' => $valuemap['valuemapid'],
'value' => array_key_exists('value', $mapping) ? $mapping['value'] : '',
'newvalue' => $mapping['newvalue'],
'sortorder' => $sortorder++
];
}
}
unset($valuemap);
DB::insertBatch('valuemap_mapping', $mappings);
$this->addAuditBulk(CAudit::ACTION_ADD, CAudit::RESOURCE_VALUE_MAP, $valuemaps);
return ['valuemapids' => $valuemapids];
}
/**
* @param array $valuemap
*
* @return array
*/
public function update(array $valuemaps) {
$this->validateUpdate($valuemaps, $db_valuemaps);
$upd_valuemaps = [];
$valuemaps_mappings = [];
foreach ($valuemaps as $valuemap) {
$valuemapid = $valuemap['valuemapid'];
$db_valuemap = $db_valuemaps[$valuemapid];
if (array_key_exists('name', $valuemap) && $valuemap['name'] !== $db_valuemap['name']) {
$upd_valuemaps[] = [
'values' => ['name' => $valuemap['name']],
'where' => ['valuemapid' => $valuemap['valuemapid']]
];
}
if (array_key_exists('mappings', $valuemap)) {
$valuemaps_mappings[$valuemapid] = [];
$sortorder = 0;
foreach ($valuemap['mappings'] as $mapping) {
$mapping += ['type' => VALUEMAP_MAPPING_TYPE_EQUAL, 'value' => ''];
$valuemaps_mappings[$valuemapid][] = [
'type' => $mapping['type'],
'value' => $mapping['value'],
'newvalue' => $mapping['newvalue'],
'sortorder' => $sortorder++
];
}
}
}
if ($upd_valuemaps) {
DB::update('valuemap', $upd_valuemaps);
}
if ($valuemaps_mappings) {
$db_mappings = DB::select('valuemap_mapping', [
'output' => ['valuemap_mappingid', 'valuemapid', 'type', 'value', 'newvalue', 'sortorder'],
'filter' => ['valuemapid' => array_keys($valuemaps_mappings)]
]);
CArrayHelper::sort($db_mappings, [['field' => 'sortorder', 'order' => ZBX_SORT_UP]]);
$ins_mapings = [];
$upd_mapings = [];
$del_mapingids = [];
$valuemapid_db_mappings = array_fill_keys(array_keys($valuemaps_mappings), []);
foreach ($db_mappings as $db_mapping) {
$valuemapid_db_mappings[$db_mapping['valuemapid']][] = $db_mapping;
}
foreach ($valuemaps_mappings as $valuemapid => $mappings) {
$db_mappings = &$valuemapid_db_mappings[$valuemapid];
foreach ($mappings as $mapping) {
$exists = false;
foreach ($db_mappings as $i => $db_mapping) {
if ($db_mapping['type'] == $mapping['type'] && $db_mapping['value'] == $mapping['value']) {
$exists = true;
break;
}
}
if (!$exists) {
$ins_mapings[] = ['valuemapid' => $valuemapid] + $mapping;
continue;
}
$update_fields = array_diff_assoc($mapping, $db_mapping);
if ($update_fields) {
$upd_mapings[] = [
'values' => $update_fields,
'where' => ['valuemap_mappingid' => $db_mapping['valuemap_mappingid']]
];
}
unset($db_mappings[$i]);
}
}
unset($db_mappings);
foreach ($valuemapid_db_mappings as $db_mappings) {
if ($db_mappings) {
$del_mapingids = array_merge($del_mapingids, array_column($db_mappings, 'valuemap_mappingid'));
}
}
if ($del_mapingids) {
DB::delete('valuemap_mapping', ['valuemap_mappingid' => $del_mapingids]);
}
if ($upd_mapings) {
DB::update('valuemap_mapping', $upd_mapings);
}
if ($ins_mapings) {
DB::insertBatch('valuemap_mapping', $ins_mapings);
}
}
$this->addAuditBulk(CAudit::ACTION_UPDATE, CAudit::RESOURCE_VALUE_MAP, $valuemaps, $db_valuemaps);
return ['valuemapids' => array_column($valuemaps, 'valuemapid')];
}
/**
* @param array $valuemapids
*
* @return array
*/
public function delete(array $valuemapids) {
$api_input_rules = ['type' => API_IDS, 'flags' => API_NOT_EMPTY, 'uniq' => true];
if (!CApiInputValidator::validate($api_input_rules, $valuemapids, '/', $error)) {
self::exception(ZBX_API_ERROR_PARAMETERS, $error);
}
$db_valuemaps = $this->get([
'output' => ['valuemapid', 'name'],
'valuemapids' => $valuemapids,
'editable' => true,
'preservekeys' => true
]);
foreach ($valuemapids as $valuemapid) {
if (!array_key_exists($valuemapid, $db_valuemaps)) {
self::exception(ZBX_API_ERROR_PERMISSIONS,
_('No permissions to referred object or it does not exist!')
);
}
}
DB::update('items', [[
'values' => ['valuemapid' => 0],
'where' => ['valuemapid' => $valuemapids]
]]);
$this->deleteByIds($valuemapids);
$this->addAuditBulk(CAudit::ACTION_DELETE, CAudit::RESOURCE_VALUE_MAP, $db_valuemaps);
return ['valuemapids' => $valuemapids];
}
/**
* Check for duplicated value maps.
*
* @param array $names_by_hostid
*
* @throws APIException if value map already exists.
*/
private function checkDuplicates(array $names_by_hostid) {
$sql_where = [];
foreach ($names_by_hostid as $hostid => $names) {
$sql_where[] = '(vm.hostid='.$hostid.' AND '.dbConditionString('vm.name', $names).')';
}
$db_valuemaps = DBfetchArray(
DBselect('SELECT vm.name FROM valuemap vm WHERE '.implode(' OR ', $sql_where), 1)
);
if ($db_valuemaps) {
self::exception(ZBX_API_ERROR_PARAMETERS,
_s('Value map "%1$s" already exists.', $db_valuemaps[0]['name'])
);
}
}
/**
* @param array $valuemaps
*
* @throws APIException if the input is invalid.
*/
private function validateCreate(array &$valuemaps) {
$api_input_rules = ['type' => API_OBJECTS, 'flags' => API_NOT_EMPTY | API_NORMALIZE, 'uniq' => [['uuid'], ['hostid', 'name']], 'fields' => [
'hostid' => ['type' => API_ID, 'flags' => API_REQUIRED],
'uuid' => ['type' => API_UUID],
'name' => ['type' => API_STRING_UTF8, 'flags' => API_REQUIRED | API_NOT_EMPTY, 'length' => DB::getFieldLength('valuemap', 'name')],
'mappings' => ['type' => API_OBJECTS, 'flags' => API_REQUIRED | API_NOT_EMPTY, 'fields' => [
'type' => ['type' => API_INT32, 'default' => VALUEMAP_MAPPING_TYPE_EQUAL, 'in' => implode(',', [VALUEMAP_MAPPING_TYPE_EQUAL, VALUEMAP_MAPPING_TYPE_GREATER_EQUAL, VALUEMAP_MAPPING_TYPE_LESS_EQUAL, VALUEMAP_MAPPING_TYPE_IN_RANGE, VALUEMAP_MAPPING_TYPE_REGEXP, VALUEMAP_MAPPING_TYPE_DEFAULT])],
'value' => ['type' => API_MULTIPLE, 'rules' => [
[
'if' => ['field' => 'type', 'in' => implode(',', [VALUEMAP_MAPPING_TYPE_EQUAL])],
'type' => API_STRING_UTF8,
'length' => DB::getFieldLength('valuemap_mapping', 'value')
],
[
'if' => ['field' => 'type', 'in' => implode(',', [VALUEMAP_MAPPING_TYPE_GREATER_EQUAL, VALUEMAP_MAPPING_TYPE_LESS_EQUAL])],
'type' => API_FLOAT,
'length' => DB::getFieldLength('valuemap_mapping', 'value')
],
[
'if' => ['field' => 'type', 'in' => implode(',', [VALUEMAP_MAPPING_TYPE_IN_RANGE])],
'type' => API_NUMERIC_RANGES,
'flags' => API_NOT_EMPTY,
'length' => DB::getFieldLength('valuemap_mapping', 'value')
],
[
'if' => ['field' => 'type', 'in' => implode(',', [VALUEMAP_MAPPING_TYPE_REGEXP])],
'type' => API_REGEX,
'flags' => API_NOT_EMPTY,
'length' => DB::getFieldLength('valuemap_mapping', 'value')
],
[
'if' => ['field' => 'type', 'in' => implode(',', [VALUEMAP_MAPPING_TYPE_DEFAULT])],
'type' => API_STRING_UTF8
]
]],
'newvalue' => ['type' => API_STRING_UTF8, 'flags' => API_REQUIRED | API_NOT_EMPTY, 'length' => DB::getFieldLength('valuemap_mapping', 'newvalue')]
]]
]];
if (!CApiInputValidator::validate($api_input_rules, $valuemaps, '/', $error)) {
self::exception(ZBX_API_ERROR_PARAMETERS, $error);
}
$this->validateValuemapMappings($valuemaps);
$hostids = [];
foreach ($valuemaps as $valuemap) {
$hostids[$valuemap['hostid']] = true;
}
$db_hosts = API::Host()->get([
'output' => ['status'],
'hostids' => array_keys($hostids),
'templated_hosts' => true,
'editable' => true,
'preservekeys' => true
]);
$names_by_hostid = [];
foreach ($valuemaps as $valuemap) {
// check permissions by hostid
if (!array_key_exists($valuemap['hostid'], $db_hosts)) {
self::exception(ZBX_API_ERROR_PERMISSIONS,
_('No permissions to referred object or it does not exist!')
);
}
$names_by_hostid[$valuemap['hostid']][] = $valuemap['name'];
}
$this->checkAndAddUuid($valuemaps, $db_hosts);
$this->checkDuplicates($names_by_hostid);
}
/**
* Check that only value maps on templates have UUID. Add UUID to all value maps on templates, if it doesn't exist.
*
* @param array $valuemaps_to_create
* @param array $db_hosts
*
* @throws APIException
*/
protected function checkAndAddUuid(array &$valuemaps_to_create, array $db_hosts): void {
foreach ($valuemaps_to_create as $index => &$valuemap) {
if ($db_hosts[$valuemap['hostid']]['status'] != HOST_STATUS_TEMPLATE
&& array_key_exists('uuid', $valuemap)) {
self::exception(ZBX_API_ERROR_PARAMETERS,
_s('Invalid parameter "%1$s": %2$s.', '/'.($index + 1), _s('unexpected parameter "%1$s"', 'uuid'))
);
}
if ($db_hosts[$valuemap['hostid']]['status'] == HOST_STATUS_TEMPLATE
&& !array_key_exists('uuid', $valuemap)) {
$valuemap['uuid'] = generateUuidV4();
}
}
unset($valuemap);
$db_uuid = DB::select('valuemap', [
'output' => ['uuid'],
'filter' => ['uuid' => array_column($valuemaps_to_create, 'uuid')],
'limit' => 1
]);
if ($db_uuid) {
self::exception(ZBX_API_ERROR_PARAMETERS,
_s('Entry with UUID "%1$s" already exists.', $db_uuid[0]['uuid'])
);
}
}
/**
* @param array $valuemaps
* @param array $db_valuemaps
*
* @throws APIException if the input is invalid.
*/
private function validateUpdate(array &$valuemaps, array &$db_valuemaps = null) {
$api_input_rules = ['type' => API_OBJECTS, 'flags' => API_NOT_EMPTY | API_NORMALIZE, 'uniq' => [['valuemapid']], 'fields' => [
'valuemapid' => ['type' => API_ID, 'flags' => API_REQUIRED],
'name' => ['type' => API_STRING_UTF8, 'flags' => API_NOT_EMPTY, 'length' => DB::getFieldLength('valuemap', 'name')],
'mappings' => ['type' => API_OBJECTS, 'flags' => API_NOT_EMPTY, 'fields' => [
'type' => ['type' => API_INT32, 'default' => VALUEMAP_MAPPING_TYPE_EQUAL, 'in' => implode(',', [VALUEMAP_MAPPING_TYPE_EQUAL, VALUEMAP_MAPPING_TYPE_GREATER_EQUAL, VALUEMAP_MAPPING_TYPE_LESS_EQUAL, VALUEMAP_MAPPING_TYPE_IN_RANGE, VALUEMAP_MAPPING_TYPE_REGEXP, VALUEMAP_MAPPING_TYPE_DEFAULT])],
'value' => ['type' => API_MULTIPLE, 'rules' => [
[
'if' => ['field' => 'type', 'in' => implode(',', [VALUEMAP_MAPPING_TYPE_EQUAL])],
'type' => API_STRING_UTF8,
'length' => DB::getFieldLength('valuemap_mapping', 'value')
],
[
'if' => ['field' => 'type', 'in' => implode(',', [VALUEMAP_MAPPING_TYPE_GREATER_EQUAL, VALUEMAP_MAPPING_TYPE_LESS_EQUAL])],
'type' => API_FLOAT,
'length' => DB::getFieldLength('valuemap_mapping', 'value')
],
[
'if' => ['field' => 'type', 'in' => implode(',', [VALUEMAP_MAPPING_TYPE_IN_RANGE])],
'type' => API_NUMERIC_RANGES,
'flags' => API_NOT_EMPTY,
'length' => DB::getFieldLength('valuemap_mapping', 'value')
],
[
'if' => ['field' => 'type', 'in' => implode(',', [VALUEMAP_MAPPING_TYPE_REGEXP])],
'type' => API_REGEX,
'flags' => API_NOT_EMPTY,
'length' => DB::getFieldLength('valuemap_mapping', 'value')
],
[
'if' => ['field' => 'type', 'in' => implode(',', [VALUEMAP_MAPPING_TYPE_DEFAULT])],
'type' => API_STRING_UTF8
]
]],
'newvalue' => ['type' => API_STRING_UTF8, 'flags' => API_REQUIRED | API_NOT_EMPTY, 'length' => DB::getFieldLength('valuemap_mapping', 'newvalue')]
]]
]];
if (!CApiInputValidator::validate($api_input_rules, $valuemaps, '/', $error)) {
self::exception(ZBX_API_ERROR_PARAMETERS, $error);
}
$this->validateValuemapMappings($valuemaps);
$db_valuemaps = $this->get([
'output' => ['valuemapid', 'hostid', 'name'],
'valuemapids' => array_column($valuemaps, 'valuemapid'),
'editable' => true,
'preservekeys' => true
]);
foreach ($valuemaps as $valuemap) {
if (!array_key_exists($valuemap['valuemapid'], $db_valuemaps)) {
self::exception(ZBX_API_ERROR_PERMISSIONS,
_('No permissions to referred object or it does not exist!')
);
}
}
$valuemaps = $this->extendObjectsByKey($valuemaps, $db_valuemaps, 'valuemapid', ['hostid']);
$api_input_rules = ['type' => API_OBJECTS, 'uniq' => [['hostid', 'name']], 'fields' => [
'hostid' => ['type' => API_ID],
'name' => ['type' => API_STRING_UTF8]
]];
if (!CApiInputValidator::validateUniqueness($api_input_rules, $valuemaps, '/', $error)) {
self::exception(ZBX_API_ERROR_PARAMETERS, $error);
}
$names_by_hostid = [];
foreach ($valuemaps as $valuemap) {
$db_valuemap = $db_valuemaps[$valuemap['valuemapid']];
if (array_key_exists('name', $valuemap) && $valuemap['name'] !== $db_valuemap['name']) {
$names_by_hostid[$valuemap['hostid']][] = $valuemap['name'];
}
}
if ($names_by_hostid) {
$this->checkDuplicates($names_by_hostid);
}
}
/**
* Validate uniqueness of mapping value in value maps, type VALUEMAP_MAPPING_TYPE_DEFAULT can be defined only once
* per value map mappings.
*
* @param array $valuemaps Array of valuemaps
*
* @throws Exception when non uniquw
*/
protected function validateValuemapMappings(array $valuemaps) {
$i = 0;
$error = '';
foreach ($valuemaps as $valuemap) {
$i++;
if (!array_key_exists('mappings', $valuemap)) {
continue;
}
$type_uniq = array_fill_keys([VALUEMAP_MAPPING_TYPE_EQUAL, VALUEMAP_MAPPING_TYPE_GREATER_EQUAL,
VALUEMAP_MAPPING_TYPE_LESS_EQUAL, VALUEMAP_MAPPING_TYPE_IN_RANGE, VALUEMAP_MAPPING_TYPE_REGEXP
], []
);
$has_default = false;
foreach (array_values($valuemap['mappings']) as $j => $mapping) {
$type = array_key_exists('type', $mapping) ? $mapping['type'] : VALUEMAP_MAPPING_TYPE_EQUAL;
$value = array_key_exists('value', $mapping) ? (string) $mapping['value'] : '';
if ($has_default && $type == VALUEMAP_MAPPING_TYPE_DEFAULT) {
$error = _s('value %1$s already exists', '(type)=('.VALUEMAP_MAPPING_TYPE_DEFAULT.')');
}
elseif (!array_key_exists('value', $mapping) && $type != VALUEMAP_MAPPING_TYPE_DEFAULT) {
$error = _s('the parameter "%1$s" is missing', 'value');
}
elseif ($value !== '' && $type == VALUEMAP_MAPPING_TYPE_DEFAULT) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Invalid parameter "%1$s": %2$s.',
sprintf('/%1$s/mappings/%2$s/value', $i, $j + 1),
_('should be empty')
));
}
elseif ($type != VALUEMAP_MAPPING_TYPE_DEFAULT && array_key_exists($value, $type_uniq[$type])) {
$error = _s('value %1$s already exists', '(value)=('.$value.')');
}
if ($error !== '') {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Invalid parameter "%1$s": %2$s.',
sprintf('/%1$s/mappings/%2$s', $i, $j + 1),
$error
));
}
$has_default = ($has_default || $type == VALUEMAP_MAPPING_TYPE_DEFAULT);
$type_uniq[$type][$value] = true;
}
}
}
protected function addRelatedObjects(array $options, array $db_valuemaps) {
$db_valuemaps = parent::addRelatedObjects($options, $db_valuemaps);
// Select mappings for value map.
if ($options['selectMappings'] !== null) {
$def_mappings = ($options['selectMappings'] == API_OUTPUT_COUNT) ? '0' : [];
foreach ($db_valuemaps as $valuemapid => $db_valuemap) {
$db_valuemaps[$valuemapid]['mappings'] = $def_mappings;
}
if ($options['selectMappings'] == API_OUTPUT_COUNT) {
$db_mappings = DBselect(
'SELECT m.valuemapid,COUNT(*) AS cnt'.
' FROM valuemap_mapping m'.
' WHERE '.dbConditionInt('m.valuemapid', array_keys($db_valuemaps)).
' GROUP BY m.valuemapid'
);
while ($db_mapping = DBfetch($db_mappings)) {
$db_valuemaps[$db_mapping['valuemapid']]['mappings'] = $db_mapping['cnt'];
}
}
else {
$db_mappings = API::getApiService()->select('valuemap_mapping', [
'output' => $this->outputExtend($options['selectMappings'], ['valuemapid',
'valuemap_mappingid', 'sortorder'
]),
'filter' => ['valuemapid' => array_keys($db_valuemaps)]
]);
CArrayHelper::sort($db_mappings, [['field' => 'sortorder', 'order' => ZBX_SORT_UP]]);
foreach ($db_mappings as $db_mapping) {
$valuemapid = $db_mapping['valuemapid'];
unset($db_mapping['valuemap_mappingid'], $db_mapping['valuemapid'], $db_mapping['sortorder']);
$db_valuemaps[$valuemapid]['mappings'][] = $db_mapping;
}
}
}
return $db_valuemaps;
}
}
|