Skip to content

Commit 64765b9

Browse files
committed
Merge branch 'release/1.9.2'
2 parents 9f83ed4 + e5c4ca9 commit 64765b9

39 files changed

+1242
-591
lines changed

composer.lock

+257-97
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hook.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ function plugin_fields_install() {
6161
echo "<td align='center'>";
6262

6363
//load all classes
64+
$dir = GLPI_ROOT . "/plugins/fields/inc/";
65+
include_once ("{$dir}toolbox.class.php");
6466
foreach ($classesToInstall as $class) {
6567
if ($plug = isPluginItemType($class)) {
66-
$dir = GLPI_ROOT . "/plugins/fields/inc/";
6768
$item = strtolower($plug['class']);
6869
if (file_exists("$dir$item.class.php")) {
6970
include_once ("$dir$item.class.php");
@@ -74,7 +75,6 @@ function plugin_fields_install() {
7475
//install
7576
foreach ($classesToInstall as $class) {
7677
if ($plug = isPluginItemType($class)) {
77-
$dir = GLPI_ROOT . "/plugins/fields/inc/";
7878
$item =strtolower($plug['class']);
7979
if (file_exists("$dir$item.class.php")) {
8080
if (!call_user_func([$class,'install'], $migration, $version)) {

inc/container.class.php

+43-7
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,47 @@ static function install(Migration $migration, $version) {
6868
$migration->migrationOneTable($table);
6969
}
7070

71+
// Fix containers names that were generated prior to Fields 1.9.2.
72+
$glpi_version = preg_replace('/^((\d+\.?)+).*$/', '$1', GLPI_VERSION);
73+
$bad_named_containers = $DB->request(
74+
[
75+
'FROM' => self::getTable(),
76+
'WHERE' => [
77+
'name' => [
78+
'REGEXP',
79+
// Regex will be escaped by PDO in GLPI 10+, but has to be escaped for GLPI < 10
80+
version_compare($glpi_version, '10.0', '>=') ? '\d+' : $DB->escape('\d+')
81+
],
82+
],
83+
]
84+
);
85+
86+
if ($bad_named_containers->count() > 0) {
87+
$migration->displayMessage(__("Fix container names", "fields"));
88+
89+
foreach ($bad_named_containers as $container) {
90+
$old_name = $container['name'];
91+
92+
// Update container name
93+
$toolbox = new PluginFieldsToolbox();
94+
$container['name'] = $toolbox->getSystemNameFromLabel($container['label']);
95+
$container_obj = new PluginFieldsContainer();
96+
$container_obj->update(
97+
$container,
98+
false
99+
);
100+
101+
// Rename container tables
102+
foreach (json_decode($container['itemtypes']) as $itemtype) {
103+
$old_table = getTableForItemType(self::getClassname($itemtype, $old_name));
104+
$new_table = getTableForItemType(self::getClassname($itemtype, $container['name']));
105+
if ($DB->tableExists($old_table)) {
106+
$migration->renameTable($old_table, $new_table);
107+
}
108+
}
109+
}
110+
}
111+
71112
//Computer OS tab is no longer part of computer object. Moving to main
72113
$ostab = self::findContainer(Computer::getType(), 'domtab', Computer::getType() . '$1');
73114
if ($ostab) {
@@ -355,13 +396,8 @@ function prepareInputForAdd($input) {
355396
}
356397
}
357398

358-
// construct field name by processing label
359-
// (remove non alphanumeric char and any trailing spaces)
360-
$input['name'] = strtolower(preg_replace("/[^\da-z]/i", "", preg_replace('/s*$/', '', $input['label'])));
361-
// if empty, uses a random number
362-
if (strlen($input['name']) == 0) {
363-
$input['name'] = rand();
364-
}
399+
$toolbox = new PluginFieldsToolbox();
400+
$input['name'] = $toolbox->getSystemNameFromLabel($input['label']);
365401

366402
//check for already existing container with same name
367403
$found = $this->find(['name' => $input['name']]);

inc/dropdown.class.php

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class PluginFieldsDropdown {
1313
* @return void
1414
*/
1515
static function install(Migration $migration, $version) {
16+
$toolbox = new PluginFieldsToolbox();
17+
$toolbox->fixFieldsNames($migration, ['type' => 'dropdown']);
18+
1619
$migration->displayMessage(__("Updating generated dropdown files", "fields"));
1720
// -> 0.90-1.3: generated class moved
1821
// OLD path: GLPI_ROOT."/plugins/fields/inc/$class_filename"

inc/field.class.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ static function install(Migration $migration, $version) {
5757
}
5858
$migration->executeMigration();
5959

60+
$toolbox = new PluginFieldsToolbox();
61+
$toolbox->fixFieldsNames($migration, ['NOT' => ['type' => 'dropdown']]);
62+
6063
return true;
6164
}
6265

@@ -182,9 +185,11 @@ function post_purgeItem() {
182185
* @return string the parsed name
183186
*/
184187
function prepareName($input) {
188+
$toolbox = new PluginFieldsToolbox();
189+
185190
//contruct field name by processing label (remove non alphanumeric char)
186191
if (empty($input['name'])) {
187-
$input['name'] = strtolower(preg_replace("/[^\da-z]/i", "", $input['label']))."field";
192+
$input['name'] = $toolbox->getSystemNameFromLabel($input['label']) . 'field';
188193
}
189194

190195
//for dropdown, if already exist, link to it
@@ -203,7 +208,7 @@ function prepareName($input) {
203208
$field_name = $input['name'];
204209
$i = 2;
205210
while (count($field->find(['name' => $field_name])) > 0) {
206-
$field_name = $input['name'].$i;
211+
$field_name = $toolbox->getIncrementedSystemName($input['name'], $i);
207212
$i++;
208213
}
209214

inc/migration.class.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?php
22

3-
class PluginFieldsMigration {
3+
class PluginFieldsMigration extends Migration {
4+
5+
function __construct($ver = "") {
6+
parent::__construct($ver);
7+
}
48

59
static function install(Migration $migration, $version) {
610
global $DB;
@@ -21,12 +25,14 @@ static function uninstall() {
2125
}
2226

2327
function updateFromCustomfields($glpi_version = "0.80") {
24-
global $DB;
25-
2628
//TODO : REWRITE customfield update
2729
return true;
2830
}
2931

32+
function displayMessage($msg) {
33+
Session::addMessageAfterRedirect($msg);
34+
}
35+
3036
function migrateCustomfieldTypes($old_type) {
3137
$types = [
3238
'sectionhead' => 'header',

inc/toolbox.class.php

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
class PluginFieldsToolbox {
4+
5+
/**
6+
* Get a clean system name from a label.
7+
*
8+
* @param string $label
9+
*
10+
* @return string
11+
*/
12+
public function getSystemNameFromLabel($label) {
13+
14+
$name = strtolower($label);
15+
16+
// 1. remove trailing "s" (plural forms)
17+
$name = getSingular($name);
18+
19+
// 2. keep only alphanum
20+
$name = preg_replace('/[^\da-z]/i', '', $name);
21+
22+
// 3. if empty, uses a random number
23+
if (strlen($name) == 0) {
24+
$name = rand();
25+
}
26+
27+
// 4. replace numbers by letters
28+
$name = $this->replaceIntByLetters($name);
29+
30+
return $name;
31+
}
32+
33+
/**
34+
* Return system name incremented by given increment.
35+
*
36+
* @param string $name
37+
* @param integer $increment
38+
*
39+
* @return string
40+
*/
41+
public function getIncrementedSystemName($name, $increment) {
42+
return $name . $this->replaceIntByLetters((string)$increment);
43+
}
44+
45+
/**
46+
* Replace integers by corresponding letters inside given string.
47+
*
48+
* @param string $str
49+
*
50+
* @return mixed
51+
*/
52+
private function replaceIntByLetters($str) {
53+
return str_replace(
54+
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
55+
['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'],
56+
$str
57+
);
58+
}
59+
60+
/**
61+
* Fix dropdown names that were generated prior to Fields 1.9.2.
62+
*
63+
* @param Migration $migration
64+
* @param mixed $condition
65+
*
66+
* @return void
67+
*/
68+
public function fixFieldsNames(Migration $migration, $condition) {
69+
global $DB;
70+
71+
$glpi_version = preg_replace('/^((\d+\.?)+).*$/', '$1', GLPI_VERSION);
72+
$bad_named_fields = $DB->request(
73+
[
74+
'FROM' => PluginFieldsField::getTable(),
75+
'WHERE' => [
76+
'name' => [
77+
'REGEXP',
78+
// Regex will be escaped by PDO in GLPI 10+, but has to be escaped for GLPI < 10
79+
version_compare($glpi_version, '10.0', '>=') ? '\d+' : $DB->escape('\d+')
80+
],
81+
$condition,
82+
],
83+
]
84+
);
85+
86+
if ($bad_named_fields->count() === 0) {
87+
return;
88+
}
89+
90+
$migration->displayMessage(__("Fix fields names", "fields"));
91+
92+
foreach ($bad_named_fields as $field) {
93+
$old_name = $field['name'];
94+
95+
// Update field name
96+
$field_obj = new PluginFieldsField();
97+
$field['name'] = null;
98+
$field['name'] = $field_obj->prepareName($field);
99+
$field_obj->update(
100+
$field,
101+
false
102+
);
103+
104+
$sql_fields_to_rename = [
105+
$old_name => $field['name'],
106+
];
107+
108+
if ('dropdown' === $field['type']) {
109+
// Rename dropdown table
110+
$old_table = getTableForItemType(PluginFieldsDropdown::getClassname($old_name));
111+
$new_table = getTableForItemType(PluginFieldsDropdown::getClassname($field['name']));
112+
if ($DB->tableExists($old_table)) {
113+
$migration->renameTable($old_table, $new_table);
114+
}
115+
116+
// Rename foreign keys in containers tables
117+
$old_fk = getForeignKeyFieldForTable($old_table);
118+
$new_fk = getForeignKeyFieldForTable($new_table);
119+
$sql_fields_to_rename[$old_fk] = $new_fk;
120+
}
121+
122+
// Rename columns in plugin tables
123+
foreach ($sql_fields_to_rename as $old_field_name => $new_field_name) {
124+
$tables_to_update = $DB->request(
125+
[
126+
'SELECT DISTINCT' => 'TABLE_NAME',
127+
'FROM' => 'INFORMATION_SCHEMA.COLUMNS',
128+
'WHERE' => [
129+
'TABLE_NAME' => ['LIKE', 'glpi_plugin_fields_%'],
130+
'COLUMN_NAME' => $old_field_name
131+
],
132+
]
133+
);
134+
135+
foreach ($tables_to_update as $table_to_update) {
136+
$sql_type = PluginFieldsMigration::getSQLType($field['type']);
137+
$migration->changeField(
138+
$table_to_update['TABLE_NAME'],
139+
$old_field_name,
140+
$new_field_name,
141+
$sql_type
142+
);
143+
$migration->migrationOneTable($table_to_update['TABLE_NAME']);
144+
}
145+
}
146+
}
147+
}
148+
}

locales/cs_CZ.mo

-27 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)