-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathexportabletrait.class.php
More file actions
165 lines (152 loc) · 5.49 KB
/
exportabletrait.class.php
File metadata and controls
165 lines (152 loc) · 5.49 KB
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
<?php
/**
* ---------------------------------------------------------------------
* Formcreator is a plugin which allows creation of custom forms of
* easy access.
* ---------------------------------------------------------------------
* LICENSE
*
* This file is part of Formcreator.
*
* Formcreator 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.
*
* Formcreator 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 Formcreator. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
* @copyright Copyright © 2011 - 2021 Teclib'
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
* @link https://github.com/pluginsGLPI/formcreator/
* @link https://pluginsglpi.github.io/formcreator/
* @link http://plugins.glpi-project.org/#/plugin/formcreator
* ---------------------------------------------------------------------
*/
if (!defined('GLPI_ROOT')) {
die("Sorry. You can't access this file directly");
}
trait PluginFormcreatorExportableTrait
{
protected $useAutomaticOrdering = true;
/**
* Insert the export of sub items in the export
*
* @param array $subItems key/value pair list of sub items
* @param array $export the export of the object
* @param bool $remove_uuid
* @return array
*/
public function exportChildrenObjects(array $subItems, array $export, bool $remove_uuid = false) : array {
global $DB;
foreach ($subItems as $key => $itemtypes) {
if (!is_array($itemtypes)) {
$itemtypes = [$itemtypes];
}
$export[$key] = [];
foreach ($itemtypes as $itemtype) {
$criteria = $itemtype::getSQLCriteriaToSearchForItem($this->getType(), $this->getID());
// Add ORDER BY clause if the itemtype has an 'order' field
// This ensures sections and other ordered items are exported in the correct order
$subItem = new $itemtype();
if ($subItem->isField('order')) {
$criteria['ORDER'] = ['order ASC'];
}
$allSubItems = $DB->request($criteria);
$list = [];
foreach ($allSubItems as $row) {
$subItem->getFromDB($row['id']);
$list[] = $subItem->export($remove_uuid);
}
if (!is_array($subItems[$key])) {
$export[$key] = $list;
} else {
$export[$key][$itemtype] = $list;
}
}
}
return $export;
}
/**
* Import children objects
*
* @param PluginFormcreatorExportableInterface $item
* @param PluginFormcreatorLinker $linker
* @param array $subItems
* @param array $input
* @return void
*/
public function importChildrenObjects(PluginFormcreatorExportableInterface $parent, PluginFormcreatorLinker $linker, array $subItems, array $input) : void {
/** @var CommonDBTM $parent */
$itemId = $parent->getID();
foreach ($subItems as $key => $itemtypes) {
if (!is_array($itemtypes)) {
if (!isset($input[$key])) {
$input[$key] = [];
}
$input[$key] = [$itemtypes => $input[$key]];
$itemtypes = [$itemtypes];
}
foreach ($itemtypes as $itemtype) {
$importedItems = [];
if (!isset($input[$key][$itemtype])) {
continue;
}
foreach ($input[$key][$itemtype] as $subInput) {
$importedItem = $itemtype::import(
$linker,
$subInput,
$itemId
);
// If $importedItem === false the item import is postponed
if ($importedItem !== false) {
$importedItems[] = $importedItem;
}
}
// Delete all other restrictions
/** @var PluginFormcreatorExportableInterface $subItem */
$subItem = new $itemtype();
$subItem->deleteObsoleteItems($parent, $importedItems);
}
}
}
/**
* Count sub items
*
* @param array $input
* @param array $subItems
* @return int
*/
public static function countChildren(array $input, array $subItems = []) : int {
if (count($subItems) < 1) {
return 1;
}
$count = 0;
foreach ($subItems as $key => $itemtypes) {
if (isset($input[$key])) {
// force array of itemtypes
if (!is_array($itemtypes)) {
if (!isset($input[$key])) {
$input[$key] = [];
}
$input[$key] = [$itemtypes => $input[$key]];
$itemtypes = [$itemtypes];
}
foreach ($itemtypes as $itemtype) {
if (!isset($input[$key][$itemtype])) {
continue;
}
foreach ($input[$key][$itemtype] as $subInput) {
$count += $itemtype::countItemsToImport($subInput);
}
}
}
}
return $count;
}
}