Skip to content

Commit 71d2399

Browse files
authored
Fix(Core): Fix json_decode for 'itemtypes' field (#1057)
* Fix(Core): Fix json_decode for 'itemtypes' field * adapt CHANGELOG.md
1 parent 861298e commit 71d2399

File tree

5 files changed

+22
-21
lines changed

5 files changed

+22
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010

1111
### Fixed
1212

13+
- Fix `json_decode` using class with namespace
1314
- Fix drag and drop
1415
- Increased the maximum length of the language column to support longer locale codes
1516
- Fix left side menu url

hook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ function plugin_datainjection_populate_fields()
338338
$container = new PluginFieldsContainer();
339339
$found = $container->find(['is_active' => 1]);
340340
foreach ($found as $values) {
341-
$types = json_decode($values['itemtypes']);
341+
$types = PluginFieldsToolbox::decodeJSONItemtypes($values['itemtypes']);
342342

343343
foreach ($types as $type) {
344344
$classname = PluginFieldsContainer::getClassname($type, $values['name'], 'Injection');

inc/container.class.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public static function installBaseData(Migration $migration, $version)
172172
foreach ($result as $type) {
173173
$migration_genericobject_itemtype[$type['itemtype']] = [
174174
'genericobject_itemtype' => $type['itemtype'],
175-
'itemtype' => 'Glpi\CustomAsset\\' . $type['name'] . 'Asset',
175+
'itemtype' => 'Glpi\\\\CustomAsset\\\\' . $type['name'] . 'Asset',
176176
'genericobject_name' => $type['name'],
177177
'name' => $type['name'] . 'Asset',
178178
];
@@ -191,7 +191,7 @@ public static function installBaseData(Migration $migration, $version)
191191
$container_class = new self();
192192
foreach ($result as $container) {
193193
self::generateTemplate($container);
194-
foreach (json_decode($container['itemtypes']) as $itemtype) {
194+
foreach (PluginFieldsToolbox::decodeJSONItemtypes($container['itemtypes']) as $itemtype) {
195195
$classname = self::getClassname($itemtype, $container["name"]);
196196
$old_table = $classname::getTable();
197197
// Rename genericobject container table
@@ -239,7 +239,7 @@ public static function installUserData(Migration $migration, $version)
239239
foreach ($containers as $container) {
240240
$itemtypes = [];
241241
if (!empty($container['itemtypes'])) {
242-
$decoded = json_decode($container['itemtypes'], true);
242+
$decoded = PluginFieldsToolbox::decodeJSONItemtypes($container['itemtypes'], true);
243243
if (is_array($decoded)) {
244244
$itemtypes = $decoded;
245245
}
@@ -290,7 +290,7 @@ public static function installUserData(Migration $migration, $version)
290290

291291
// Update container name
292292
$new_name = $toolbox->getSystemNameFromLabel($container['label']);
293-
foreach (json_decode($container['itemtypes']) as $itemtype) {
293+
foreach (PluginFieldsToolbox::decodeJSONItemtypes($container['itemtypes']) as $itemtype) {
294294
while (strlen(getTableForItemType(self::getClassname($itemtype, $new_name))) > 64) {
295295
// limit tables names to 64 chars (MySQL limit)
296296
$new_name = substr($new_name, 0, -1);
@@ -304,7 +304,7 @@ public static function installUserData(Migration $migration, $version)
304304
);
305305

306306
// Rename container tables and itemtype if needed
307-
foreach (json_decode($container['itemtypes']) as $itemtype) {
307+
foreach (PluginFieldsToolbox::decodeJSONItemtypes($container['itemtypes']) as $itemtype) {
308308
$migration->renameItemtype(
309309
self::getClassname($itemtype, $old_name),
310310
self::getClassname($itemtype, $new_name),
@@ -640,7 +640,7 @@ public function prepareInputForAdd($input)
640640
$found = $this->find(['type' => 'dom']);
641641
if (count($found) > 0) {
642642
foreach (array_column($found, 'itemtypes') as $founditemtypes) {
643-
foreach (json_decode($founditemtypes) as $founditemtype) {
643+
foreach (PluginFieldsToolbox::decodeJSONItemtypes($founditemtypes) as $founditemtype) {
644644
if (in_array($founditemtype, $input['itemtypes'])) {
645645
Session::AddMessageAfterRedirect(__("You cannot add several blocks with type 'Insertion in the form' on same object", 'fields'), false, ERROR);
646646

@@ -656,7 +656,7 @@ public function prepareInputForAdd($input)
656656
$found = $this->find(['type' => 'domtab', 'subtype' => $input['subtype']]);
657657
if (count($found) > 0) {
658658
foreach (array_column($found, 'itemtypes') as $founditemtypes) {
659-
foreach (json_decode($founditemtypes) as $founditemtype) {
659+
foreach (PluginFieldsToolbox::decodeJSONItemtypes($founditemtypes) as $founditemtype) {
660660
if (in_array($founditemtype, $input['itemtypes'])) {
661661
Session::AddMessageAfterRedirect(__("You cannot add several blocks with type 'Insertion in the form of a specific tab' on same object tab", 'fields'), false, ERROR);
662662

@@ -688,7 +688,7 @@ public function prepareInputForAdd($input)
688688
$found = $this->find(['name' => $input['name']]);
689689
if (count($found) > 0) {
690690
foreach (array_column($found, 'itemtypes') as $founditemtypes) {
691-
foreach (json_decode($founditemtypes) as $founditemtype) {
691+
foreach (PluginFieldsToolbox::decodeJSONItemtypes($founditemtypes) as $founditemtype) {
692692
if (in_array($founditemtype, $input['itemtypes'])) {
693693
Session::AddMessageAfterRedirect(__('You cannot add several blocs with identical name on same object', 'fields'), false, ERROR);
694694

@@ -781,7 +781,7 @@ public function pre_deleteItem()
781781

782782
$_SESSION['delete_container'] = true;
783783

784-
foreach (json_decode($this->fields['itemtypes']) as $itemtype) {
784+
foreach (PluginFieldsToolbox::decodeJSONItemtypes($this->fields['itemtypes']) as $itemtype) {
785785
$classname = self::getClassname($itemtype, $this->fields['name']);
786786
$sysname = self::getSystemName($itemtype, $this->fields['name']);
787787
$class_filename = $sysname . '.class.php';
@@ -843,7 +843,7 @@ public static function preItemPurge($item)
843843
$containers = new self();
844844
$founded_containers = $containers->find();
845845
foreach ($founded_containers as $container) {
846-
$itemtypes = json_decode($container['itemtypes']);
846+
$itemtypes = PluginFieldsToolbox::decodeJSONItemtypes($container['itemtypes']);
847847
if (in_array($itemtype, $itemtypes)) {
848848
$classname = self::getClassname($itemtype, $container['name']);
849849
$fields = new $classname();
@@ -927,7 +927,7 @@ public function showForm($ID, $options = [])
927927
echo '<td>' . __('Associated item type') . ' : </td>';
928928
echo '<td>';
929929
if ($ID > 0) {
930-
$types = json_decode($this->fields['itemtypes']);
930+
$types = PluginFieldsToolbox::decodeJSONItemtypes($this->fields['itemtypes']);
931931
$obj = '';
932932
$count = count($types);
933933
$i = 1;
@@ -968,7 +968,7 @@ public function showForm($ID, $options = [])
968968
echo '<td>';
969969
echo "&nbsp;<span id='subtype_$rand'></span>";
970970
if ($ID > 0 && !empty($this->fields['subtype'])) {
971-
$itemtypes = json_decode($this->fields['itemtypes'], true);
971+
$itemtypes = PluginFieldsToolbox::decodeJSONItemtypes($this->fields['itemtypes'], true);
972972
$itemtype = array_shift($itemtypes);
973973
$dbu = new DbUtils();
974974
$item = $dbu->getItemForItemtype($itemtype);
@@ -1233,7 +1233,7 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
12331233
foreach ($itemtypes[$item->getType()] as $tab_name => $tab_label) {
12341234
// needs to check if entity of item is in hierachy of $tab_name
12351235
foreach ($container->find(['is_active' => 1, 'name' => $tab_name]) as $data) {
1236-
$dataitemtypes = json_decode($data['itemtypes']);
1236+
$dataitemtypes = PluginFieldsToolbox::decodeJSONItemtypes($data['itemtypes']);
12371237
if (in_array(get_class($item), $dataitemtypes) != false) {
12381238
$entities = [$data['entities_id']];
12391239
if ($data['is_recursive']) {
@@ -1265,7 +1265,7 @@ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $
12651265
//retrieve container for current tab
12661266
$container = new self();
12671267
if ($container->getFromDB($tabnum)) {
1268-
$dataitemtypes = json_decode($container->fields['itemtypes']);
1268+
$dataitemtypes = PluginFieldsToolbox::decodeJSONItemtypes($container->fields['itemtypes']);
12691269
if (in_array(get_class($item), $dataitemtypes) != false) {
12701270
return PluginFieldsField::showForTabContainer($container->fields['id'], $item);
12711271
}
@@ -2242,7 +2242,7 @@ public function prepareInputForClone($input)
22422242
if (array_key_exists('itemtypes', $input) && !empty($input['itemtypes'])) {
22432243
// $input has been transformed with `Toolbox::addslashes_deep()`, and `self::prepareInputForAdd()`
22442244
// is expecting an array, so it have to be unslashed then json decoded.
2245-
$input['itemtypes'] = json_decode($input['itemtypes']);
2245+
$input['itemtypes'] = PluginFieldsToolbox::decodeJSONItemtypes($input['itemtypes']);
22462246
} else {
22472247
unset($input['itemtypes']);
22482248
}

inc/field.class.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ public function prepareInputForAdd($input)
302302
if ($input['type'] !== 'header') {
303303
$container_obj = new PluginFieldsContainer();
304304
$container_obj->getFromDB($input['plugin_fields_containers_id']);
305-
foreach (json_decode($container_obj->fields['itemtypes']) as $itemtype) {
305+
foreach (PluginFieldsToolbox::decodeJSONItemtypes($container_obj->fields['itemtypes']) as $itemtype) {
306306
$classname = PluginFieldsContainer::getClassname($itemtype, $container_obj->fields['name']);
307307
$classname::addField(
308308
$input['name'],
@@ -354,7 +354,7 @@ public function pre_deleteItem()
354354
$container_obj = new PluginFieldsContainer();
355355
$container_obj->getFromDB($this->fields['plugin_fields_containers_id']);
356356

357-
foreach (json_decode($container_obj->fields['itemtypes']) as $itemtype) {
357+
foreach (PluginFieldsToolbox::decodeJSONItemtypes($container_obj->fields['itemtypes']) as $itemtype) {
358358
$so = PluginFieldsContainer::getAddSearchOptions($itemtype, $this->fields['plugin_fields_containers_id']);
359359
foreach ($so as $so_id => $so_value) {
360360
if ($this->fields['type'] == 'glpi_item') {
@@ -380,7 +380,7 @@ public function pre_deleteItem()
380380
&& !isset($_SESSION['uninstall_fields'])
381381
&& !isset($_SESSION['delete_container'])
382382
) {
383-
foreach (json_decode($container_obj->fields['itemtypes']) as $itemtype) {
383+
foreach (PluginFieldsToolbox::decodeJSONItemtypes($container_obj->fields['itemtypes']) as $itemtype) {
384384
$classname = PluginFieldsContainer::getClassname($itemtype, $container_obj->fields['name']);
385385
$classname::removeField($this->fields['name'], $this->fields['type']);
386386
}
@@ -400,7 +400,7 @@ public function pre_deleteItem()
400400

401401
$use_by_another = false;
402402
foreach ($all_container as $container_fields) {
403-
foreach (json_decode($container_fields['itemtypes']) as $itemtype) {
403+
foreach (PluginFieldsToolbox::decodeJSONItemtypes($container_fields['itemtypes']) as $itemtype) {
404404
$dropdown_classname = PluginFieldsDropdown::getClassname($this->fields['name']);
405405
$classname = PluginFieldsContainer::getClassname($itemtype, $container_fields['name']);
406406
$dropdown_fk = getForeignKeyFieldForItemType($dropdown_classname);

setup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ function plugin_fields_exportBlockAsYaml($container_id = null)
305305
$containers = $container_obj->find($where);
306306
foreach ($containers as $container) {
307307
$itemtypes = (strlen($container['itemtypes']) > 0)
308-
? json_decode($container['itemtypes'], true)
308+
? PluginFieldsToolbox::decodeJSONItemtypes($container['itemtypes'], true)
309309
: [];
310310

311311
foreach ($itemtypes as $itemtype) {

0 commit comments

Comments
 (0)