-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcategory_group.class.php
More file actions
131 lines (117 loc) · 5.88 KB
/
category_group.class.php
File metadata and controls
131 lines (117 loc) · 5.88 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
<?php
/**
* -------------------------------------------------------------------------
* ItilCategoryGroups plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of ItilCategoryGroups.
*
* ItilCategoryGroups 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.
*
* ItilCategoryGroups 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 ItilCategoryGroups. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2012-2022 by ItilCategoryGroups plugin team.
* @license GPLv2 https://www.gnu.org/licenses/gpl-2.0.html
* @link https://github.com/pluginsGLPI/itilcategorygroups
* -------------------------------------------------------------------------
*/
class PluginItilcategorygroupsCategory_Group extends CommonDBChild {
static public $itemtype = "PluginItilcategorygroupsCategory";
static public $items_id = "plugin_itilcategorygroups_categories_id";
static function install(Migration $migration) {
global $DB;
$default_charset = DBConnection::getDefaultCharset();
$default_collation = DBConnection::getDefaultCollation();
$default_key_sign = DBConnection::getDefaultPrimaryKeySignOption();
$table = getTableForItemType(__CLASS__);
if (!$DB->tableExists($table)) {
$query = "CREATE TABLE IF NOT EXISTS `$table` (
`id` INT {$default_key_sign} NOT NULL AUTO_INCREMENT,
`plugin_itilcategorygroups_categories_id` INT {$default_key_sign} NOT NULL DEFAULT '0',
`level` TINYINT NOT NULL DEFAULT '0',
`itilcategories_id` INT {$default_key_sign} NOT NULL DEFAULT '0',
`groups_id` INT {$default_key_sign} NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `group_lvl_unicity` (plugin_itilcategorygroups_categories_id, level, groups_id),
KEY `plugin_itilcategorygroups_categories_id` (`plugin_itilcategorygroups_categories_id`),
KEY `level` (`level`),
KEY `itilcategories_id` (`itilcategories_id`),
KEY `groups_id` (`groups_id`)
) ENGINE=InnoDB DEFAULT CHARSET={$default_charset} COLLATE={$default_collation} ROW_FORMAT=DYNAMIC;";
$DB->query($query);
}
$parent_table = "glpi_plugin_itilcategorygroups_categories";
//we must migrate groups datas in sub table
if ($DB->fieldExists($parent_table, 'groups_id_levelone')) {
$all_lvl = $cat_groups = [];
//foreach old levels
foreach ([1=>'one', 2=>'two', 3=>'three', 4=>'four'] as $lvl_num => $lvl_str) {
$it = $DB->request([
'SELECT' => ['id', 'itilcategories_id', "groups_id_level{$lvl_str}"],
'FROM' => $parent_table,
]);
foreach ($it as $data) {
//specific case (all group of this lvl), store it for further treatment
if ($data["groups_id_level$lvl_str"] == -1) {
$all_lvl[$data['itilcategories_id']][$lvl_num] = $lvl_str;
}
if ($data["groups_id_level$lvl_str"] > 0) {
$cat_groups[] = [
'plugin_itilcategorygroups_categories_id' => $data['id'],
'level' => $lvl_num,
'itilcategories_id' => $data['itilcategories_id'],
'groups_id' => $data["groups_id_level$lvl_str"]];
}
}
//insert "all groups for this lvl'
foreach ($all_lvl as $itilcategories_id => $lvl) {
foreach ($lvl as $lvl_num => $lvl_str) {
$DB->update($parent_table, [
"view_all_lvl$lvl_num" => 1,
], [
'itilcategories_id' => $itilcategories_id,
]);
}
}
//insert groups in sub table
foreach ($cat_groups as $cat_groups_data) {
$DB->query("REPLACE INTO glpi_plugin_itilcategorygroups_categories_groups
(plugin_itilcategorygroups_categories_id,
level,
itilcategories_id,
groups_id)
VALUES (
".$cat_groups_data['plugin_itilcategorygroups_categories_id'].",
".$cat_groups_data['level'].",
".$cat_groups_data['itilcategories_id'].",
".$cat_groups_data['groups_id']."
)");
}
}
//drop migrated fields
$migration->dropField($parent_table, "groups_id_levelone");
$migration->dropField($parent_table, "groups_id_leveltwo");
$migration->dropField($parent_table, "groups_id_levelthree");
$migration->dropField($parent_table, "groups_id_levelfour");
$migration->migrationOneTable($parent_table);
}
return true;
}
static function uninstall() {
global $DB;
$table = getTableForItemType(__CLASS__);
$DB->query("DROP TABLE IF EXISTS`$table`");
return true;
}
}