From f49df3458a0e5ab1796a0e4cbddc3e37ba2aae07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Anne?= Date: Fri, 7 Sep 2018 12:01:27 +0200 Subject: [PATCH] Prefer "complete[name]" field for mapping with tier item; see #121 --- inc/commoninjectionlib.class.php | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/inc/commoninjectionlib.class.php b/inc/commoninjectionlib.class.php index f21ebfef..a47199ce 100644 --- a/inc/commoninjectionlib.class.php +++ b/inc/commoninjectionlib.class.php @@ -2008,7 +2008,36 @@ static function addToSearchOptions(array $type_searchOptions, array $options, } } - return $type_searchOptions; + /* + * Preserve only one option per linkfield, as mapping process is based on arrays indexed by linkfield. + * This is a hack to handle behaviour explained in issue https://github.com/pluginsGLPI/datainjection/issues/121. + * + * Preserved option is "complename" if existing, or "name" if existing, + * or first founded option for each linkfield. + */ + $linkfield_preserved_option = array_fill_keys(array_column($type_searchOptions, 'linkfield'), null); + foreach ($type_searchOptions as $option) { + if (!array_key_exists('linkfield', $option)) { + continue; + } + + $linkfield = $option['linkfield']; + + if (null === $linkfield_preserved_option[$linkfield] + || ('name' === $option['field'] && 'completename' !== $linkfield_preserved_option[$linkfield]['field']) + || 'completename' === $option['field']) { + $linkfield_preserved_option[$linkfield] = $option; + } + } + + $type_searchOptions = array_filter( + $type_searchOptions, + function ($option) use ($linkfield_preserved_option) { + return in_array($option, $linkfield_preserved_option); + } + ); + + return $type_searchOptions; }