-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Device.php
204 lines (179 loc) · 7.74 KB
/
Device.php
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
<?php
/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2022 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/
namespace Glpi\Inventory\Asset;
use Glpi\Toolbox\Sanitizer;
use Item_Devices;
abstract class Device extends InventoryAsset
{
/**
* Get existing entries from database
*
* @return array
*/
protected function getExisting($itemdevicetable, $fk): array
{
global $DB;
$db_existing = [];
$iterator = $DB->request([
'FROM' => $itemdevicetable,
'WHERE' => [
"$itemdevicetable.items_id" => $this->item->fields['id'],
"$itemdevicetable.itemtype" => $this->item->getType()
]
]);
foreach ($iterator as $row) {
$db_existing[$row[$fk]][] = $row;
}
return $db_existing;
}
public function handle()
{
global $DB;
$devicetypes = Item_Devices::getItemAffinities($this->item->getType());
$itemdevicetype = $this->getItemtype();
if (in_array($itemdevicetype, $devicetypes)) {
$value = $this->data;
$itemdevice = new $itemdevicetype();
$itemdevicetable = getTableForItemType($itemdevicetype);
$devicetype = $itemdevicetype::getDeviceType();
$device = new $devicetype();
$devicetable = getTableForItemType($devicetype);
$fk = getForeignKeyFieldForTable($devicetable);
$existing = $this->getExisting($itemdevicetable, $fk);
foreach ($value as $val) {
if (!isset($val->designation) || $val->designation == '') {
//cannot be empty
$val->designation = $itemdevice->getTypeName(1);
}
//force conversion if needed for date format as 2015-04-16T00:00:00Z
// TODO : need to straighten up date format globally (especially for JSON inventory) which does not use the converter
if (property_exists($val, 'date')) {
$val->date = date('Y-m-d', strtotime($val->date));
}
//create device or get existing device ID
$raw_input = $this->handleInput($val, $device);
$device_input = Sanitizer::dbEscapeRecursive($raw_input); // `handleInput` may copy unescaped values
$device_id = $device->import($device_input + ['with_history' => false]);
$i_criteria = $itemdevice->getImportCriteria();
$fk_input = [
$fk => $device_id,
'itemtype' => $this->item->getType(),
'items_id' => $this->item->fields['id'],
'is_dynamic' => 1
];
$i_input = $fk_input;
//populate compare criteria
foreach (array_keys($i_criteria) as $column) {
if (isset($raw_input[$column])) {
$i_input[$column] = $raw_input[$column];
}
}
//check if deviceitem should be updated or added.
foreach ($existing[$device_id] ?? [] as $key => $existing_item) {
$equals = true;
foreach ($i_criteria as $field => $compare) {
if ($equals === false) {
//no need to continue if one of conditions is false already
break;
}
$compare = explode(':', $compare);
if (!isset($i_input[$field]) && !isset($existing_item[$field])) {
//field not present, skip
continue;
}
switch ($compare[0]) {
case 'equal':
if (!isset($i_input[$field]) || $i_input[$field] != $existing_item[$field]) {
$equals = false;
}
break;
case 'delta':
if (
$i_input[$field] - (int)$compare[1] > $existing_item[$field]
&& $i_input[$field] + (int)$compare[1] < $existing_item[$field]
) {
$equals = false;
}
break;
}
}
if ($equals === true) {
$itemdevice->getFromDB($existing_item['id']);
$itemdevice_data = [
'id' => $existing_item['id'],
$fk => $device_id,
'itemtype' => $this->item->getType(),
'items_id' => $this->item->fields['id'],
'is_dynamic' => 1
] + $this->handleInput($val, $itemdevice);
$itemdevice->update(Sanitizer::sanitize($itemdevice_data), false);
unset($existing[$device_id][$key]);
break;
}
}
if (($equals ?? false) !== true) {
$itemdevice->getEmpty();
$itemdevice_data = [
$fk => $device_id,
'itemtype' => $this->item->getType(),
'items_id' => $this->item->fields['id'],
'is_dynamic' => 1
] + $this->handleInput($val, $itemdevice);
$itemdevice->add(Sanitizer::sanitize($itemdevice_data), [], false);
$this->itemdeviceAdded($itemdevice, $val);
}
if (count($existing[$device_id] ?? []) == 0) {
unset($existing[$device_id]);
}
}
//remove remaining devices instances
foreach ($existing as $data) {
foreach ($data as $itemdevice_data) {
if ($itemdevice_data['is_dynamic'] == 1) {
$DB->delete(
$itemdevice->getTable(),
[
'id' => $itemdevice_data['id']
]
);
}
}
}
}
}
protected function itemdeviceAdded(Item_Devices $itemdevice, $val)
{
//to be overrided
}
}