Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions inc/container.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1587,13 +1587,6 @@ public static function getAddSearchOptions($itemtype, $containers_id = false)

$i = 76665;

$search_string = json_encode($itemtype);
// Backslashes must be doubled in LIKE clause, according to MySQL documentation:
// > To search for \, specify it as \\\\; this is because the backslashes are stripped
// > once by the parser and again when the pattern match is made,
// > leaving a single backslash to be matched against.
$search_string = str_replace('\\', '\\\\', $search_string);

Comment on lines -1590 to -1596
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be reverted. It is mandatory to correctly handle itemtype using namespaces (i.e. Glpi\Socket).

$query = "SELECT DISTINCT fields.id, fields.name, fields.label, fields.type, fields.is_readonly, fields.allowed_values,
containers.name as container_name, containers.label as container_label,
containers.itemtypes, containers.id as container_id, fields.id as field_id
Expand All @@ -1605,7 +1598,7 @@ public static function getAddSearchOptions($itemtype, $containers_id = false)
INNER JOIN glpi_plugin_fields_fields fields
ON containers.id = fields.plugin_fields_containers_id
AND containers.is_active = 1
WHERE containers.itemtypes LIKE '%" . $DB->escape($search_string) . "%'
WHERE containers.itemtypes LIKE '%" . $itemtype . "%'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same remark.

AND fields.type != 'header'
ORDER BY fields.id ASC";
$res = $DB->query($query);
Expand All @@ -1617,7 +1610,21 @@ public static function getAddSearchOptions($itemtype, $containers_id = false)
continue;
}
}
$tablename = getTableForItemType(self::getClassname($itemtype, $data['container_name']));

// reset $CurrentItemType to initial itemtype
$CurrentItemType = $itemtype;
//get all types in itemtypes
$it = json_decode($data['itemtypes']);
foreach ($it as $label) {
// set $CurrentItemType with the real current type
// if $label is in ['itemtypeModel', 'itemtypeType']
$re = "/$itemtype(Model|Type)/";
if (preg_match($re, $label)) {
$CurrentItemType = $label;
break;
}
}
$tablename = "glpi_plugin_fields_" . strtolower($CurrentItemType . getPlural(preg_replace('/s$/', '', $data['container_name'])));

//get translations
$container = [
Expand Down Expand Up @@ -1690,6 +1697,11 @@ public static function getAddSearchOptions($itemtype, $containers_id = false)
$opt[$i]['joinparams']['jointype'] = "";
$opt[$i]['joinparams']['beforejoin']['table'] = $tablename;
$opt[$i]['joinparams']['beforejoin']['joinparams']['jointype'] = "itemtype_item";
$opt[$i]['joinparams']['beforejoin']['joinparams']['specific_itemtype'] = $CurrentItemType; //set itemtype customfield in join
// needs to add a ref to the $itemtype
$opt[$i]['joinparams']['beforejoin']['joinparams']['beforejoin'] = [
'table' => getTableForItemType($CurrentItemType)
];
} elseif ($data['type'] === "glpi_item") {
$itemtype_field = sprintf('itemtype_%s', $data['name']);
$items_id_field = sprintf('items_id_%s', $data['name']);
Expand Down
16 changes: 2 additions & 14 deletions inc/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,24 +512,12 @@ function () {
JAVASCRIPT
);

// Exclude dropdown that corresponds to itemtypes targetted by container.
// This will prevent issues with search options.
// FIXME: Fix search options handling and remove this limitation.
$itemtypes_to_exclude = !empty($container->fields['itemtypes'])
? array_map(
function ($itemtype) {
return 'dropdown-' . $itemtype;
},
json_decode($container->fields['itemtypes'])
)
: [];
Dropdown::showFromArray(
'type',
self::getTypes(false),
self::getTypes(),
[
'value' => $this->fields['type'],
'on_change' => 'plugin_fields_change_field_type_' . $rand . '(this.value)',
'used' => array_combine($itemtypes_to_exclude, $itemtypes_to_exclude),
'on_change' => 'plugin_fields_change_field_type_' . $rand . '(this.value)'
]
);
}
Expand Down
2 changes: 1 addition & 1 deletion setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* -------------------------------------------------------------------------
*/

define('PLUGIN_FIELDS_VERSION', '1.17.2');
define('PLUGIN_FIELDS_VERSION', '1.17.2.1');

// Minimal GLPI version, inclusive
define("PLUGIN_FIELDS_MIN_GLPI", "10.0.0");
Expand Down