", $infos_id, $fields_html['mandatory']);
}
if ($canedit) {
diff --git a/inc/injectiontype.class.php b/inc/injectiontype.class.php
index dc0397d0..900c0705 100644
--- a/inc/injectiontype.class.php
+++ b/inc/injectiontype.class.php
@@ -32,6 +32,8 @@
use function Safe\json_decode;
use function Safe\json_encode;
+use function Safe\ob_get_clean;
+use function Safe\ob_start;
use function Safe\preg_replace;
class PluginDatainjectionInjectionType
@@ -120,12 +122,89 @@ public static function dropdown($value = '', $only_primary = false)
*
* @param mixed $mapping_or_info
* @param array $options array
+ * @param bool $load_fields load the fields dropdown through AJAX on page load
**/
- public static function dropdownLinkedTypes($mapping_or_info, $options = [])
+ public static function dropdownLinkedTypes($mapping_or_info, $options = [], bool $load_fields = true)
+ {
+ [$p, $values] = self::getLinkedTypesParams($mapping_or_info, $options);
+
+ $mappings_id = $mapping_or_info->fields['id'];
+
+ $rand = Dropdown::showFromArray(
+ "data[" . $mappings_id . "][itemtype]",
+ $values,
+ ['value' => $p['itemtype']],
+ );
+
+ $p['itemtype'] = '__VALUE__';
+ $di_base_url = plugin_datainjection_geturl();
+ $url_field = $di_base_url . "ajax/dropdownChooseField.php";
+ $toobserve = "dropdown_data[" . $mapping_or_info->getID() . ('][itemtype]' . $rand);
+ $toupdate = "span_field_" . $mappings_id;
+ if ($load_fields) {
+ Ajax::updateItem($toupdate, $url_field, $p, $toobserve);
+ }
+
+ Ajax::updateItemOnSelectEvent($toobserve, $toupdate, $url_field, $p);
+ return $rand;
+ }
+
+
+ /**
+ * Get the HTML of the fields dropdown and of the mandatory checkbox of a mapping or an info,
+ * as the AJAX calls of dropdownLinkedTypes() and dropdownFields() load them
+ *
+ * @param mixed $mapping_or_info
+ * @param array $options array
+ *
+ * @return array{field: string, mandatory: string}
+ **/
+ public static function getFieldsHtml($mapping_or_info, $options = []): array
+ {
+ [$p, $values] = self::getLinkedTypesParams($mapping_or_info, $options);
+
+ //Value the browser selects in the itemtype dropdown
+ $p['itemtype'] = self::getSelectedValue($values, $p['itemtype']);
+
+ ob_start();
+ $p['value'] = self::dropdownFields($p, false);
+ $field_html = ob_get_clean();
+
+ ob_start();
+ self::showMandatoryCheckbox($p + ['need_decode' => true]);
+ $mandatory_html = ob_get_clean();
+
+ return [
+ 'field' => $field_html,
+ 'mandatory' => $mandatory_html,
+ ];
+ }
+
+
+ /**
+ * Value selected by the browser in a dropdown: the given value, or the first one if it is not in the list
+ *
+ * @param int|string $value
+ * @return int|string
+ **/
+ private static function getSelectedValue(array $values, $value)
+ {
+ return isset($values[$value]) ? $value : array_key_first($values);
+ }
+
+
+ /**
+ * Get the parameters and the values of the linked types dropdown
+ *
+ * @param mixed $mapping_or_info
+ * @param array $options array
+ *
+ * @return array{0: array, 1: array}
+ **/
+ private static function getLinkedTypesParams($mapping_or_info, $options = []): array
{
/** @var array[mixed] $INJECTABLE_TYPES */
- /** @var array $CFG_GLPI */
- global $INJECTABLE_TYPES, $CFG_GLPI;
+ global $INJECTABLE_TYPES;
getTypesToInject(); // populate $INJECTABLE_TYPES
@@ -142,7 +221,6 @@ public static function dropdownLinkedTypes($mapping_or_info, $options = [])
$p[$key] = $value;
}
- $mappings_id = $mapping_or_info->fields['id'];
$values = [];
if (
@@ -160,6 +238,12 @@ public static function dropdownLinkedTypes($mapping_or_info, $options = [])
throw new HttpException(500, 'Class ' . $p['primary_type'] . ' is not a valid class');
}
+ //The linked types are the same for all the rows of a model: computed once per request
+ static $values_by_primary_type = [];
+ if (isset($values_by_primary_type[$p['primary_type']])) {
+ return [$p, $values_by_primary_type[$p['primary_type']]];
+ }
+
$type = new $p['primary_type']();
$values[$p['primary_type']] = $type->getTypeName();
@@ -177,33 +261,24 @@ public static function dropdownLinkedTypes($mapping_or_info, $options = [])
}
asort($values);
+ $values_by_primary_type[$p['primary_type']] = $values;
- $rand = Dropdown::showFromArray(
- "data[" . $mappings_id . "][itemtype]",
- $values,
- ['value' => $p['itemtype']],
- );
-
- $p['itemtype'] = '__VALUE__';
- $di_base_url = plugin_datainjection_geturl();
- $url_field = $di_base_url . "ajax/dropdownChooseField.php";
- $toobserve = "dropdown_data[" . $mapping_or_info->getID() . ('][itemtype]' . $rand);
- $toupdate = "span_field_" . $mappings_id;
- Ajax::updateItem($toupdate, $url_field, $p, $toobserve);
- Ajax::updateItemOnSelectEvent($toobserve, $toupdate, $url_field, $p);
- return $rand;
+ return [$p, $values];
}
/**
- * @param array $options array
+ * @param array $options array
+ * @param bool $load_mandatory load the mandatory checkbox through AJAX on page load
+ *
+ * @return int|string the value selected in the fields dropdown
**/
- public static function dropdownFields($options = [])
+ public static function dropdownFields($options = [], bool $load_mandatory = true)
{
/** @var array $CFG_GLPI */
global $CFG_GLPI;
- $used = [];
+ $used = [];
$p['itemtype'] = self::NO_VALUE;
$p['primary_type'] = '';
$p['mapping_or_info'] = [];
@@ -236,9 +311,16 @@ public static function dropdownFields($options = [])
$mapping_value = $mapping_or_info['value'];
}
- $injectionClass = PluginDatainjectionCommonInjectionLib::getInjectionClassInstance($p['itemtype']);
+ //Search options are the same for all the rows of a model: computed once per request
+ static $search_options_cache = [];
+ $cache_key = $p['itemtype'] . '|' . $p['primary_type'];
+ if (!isset($search_options_cache[$cache_key])) {
+ $injectionClass = PluginDatainjectionCommonInjectionLib::getInjectionClassInstance($p['itemtype']);
+ $search_options_cache[$cache_key] = $injectionClass->getOptions($p['primary_type']);
+ }
- foreach ($injectionClass->getOptions($p['primary_type']) as $option) {
+ $search_options = $search_options_cache[$cache_key];
+ foreach ($search_options as $option) {
//If it's a real option (not a group label) and if field is not blacklisted
//and if a linkfield is defined (meaning that the field can be updated)
if (
@@ -258,7 +340,7 @@ public static function dropdownFields($options = [])
}
}
- $used = self::getUsedMappingsOrInfos($p);
+ $used = self::getUsedMappingsOrInfos($p, $search_options);
}
asort($fields);
@@ -273,18 +355,23 @@ public static function dropdownFields($options = [])
$p['value'] = '__VALUE__';
$url = plugin_datainjection_geturl() . "ajax/dropdownMandatory.php";
- Ajax::updateItem(
- "span_mandatory_" . $mapping_or_info['id'],
- $url,
- $p,
- "dropdown_data[" . $mapping_or_info['id'] . ('][value]' . $rand),
- );
+ if ($load_mandatory) {
+ Ajax::updateItem(
+ "span_mandatory_" . $mapping_or_info['id'],
+ $url,
+ $p,
+ "dropdown_data[" . $mapping_or_info['id'] . ('][value]' . $rand),
+ );
+ }
+
Ajax::updateItemOnSelectEvent(
"dropdown_data[" . $mapping_or_info['id'] . ('][value]' . $rand),
"span_mandatory_" . $mapping_or_info['id'],
$url,
$p,
);
+
+ return self::getSelectedValue($fields, $mapping_value);
}
@@ -364,9 +451,10 @@ public static function showMandatoryCheckbox($options = [])
/**
- * @param array $options array
+ * @param array $options array
+ * @param array|null $search_options search options of the injection class, computed if not given
**/
- public static function getUsedMappingsOrInfos($options = [])
+ public static function getUsedMappingsOrInfos($options = [], ?array $search_options = null)
{
/** @var DBmysql $DB */
global $DB;
@@ -390,32 +478,45 @@ public static function getUsedMappingsOrInfos($options = [])
$mapping_or_info = $options['mapping_or_info'];
}
- $used = [];
+ //The fields used by the model are the same for all its rows: computed once per request
+ static $used_by_model = [];
+
$table = (($p['called_by'] == 'PluginDatainjectionMapping') ? "glpi_plugin_datainjection_mappings"
: "glpi_plugin_datainjection_infos");
+ $cache_key = implode('|', [$table, $mapping_or_info['models_id'], $p['itemtype'], $p['primary_type'], $search_options === null]);
- $datas = getAllDataFromTable($table, ['models_id' => $mapping_or_info['models_id']]);
+ if (!isset($used_by_model[$cache_key])) {
+ $datas = getAllDataFromTable($table, ['models_id' => $mapping_or_info['models_id']]);
- $injectionClass = PluginDatainjectionCommonInjectionLib::getInjectionClassInstance($p['itemtype']);
- $options = $injectionClass->getOptions();
+ if ($search_options === null) {
+ $injectionClass = PluginDatainjectionCommonInjectionLib::getInjectionClassInstance($p['itemtype']);
+ $search_options = $injectionClass->getOptions();
+ }
- foreach ($datas as $data) {
- if ($data['value'] != self::NO_VALUE) {
- foreach ($options as $option) {
- if (
- isset($option['table'])
- && ($option['table'] == getItemTypeForTable($data['itemtype']))
- && ($option['linkfield'] == $data['value'])
- && ($option['displaytype'] != 'multiline_text')
- && ($mapping_or_info['value'] != $data['value'])
- ) {
- $used[$option['linkfield']] = $option['linkfield'];
- break;
+ $options = $search_options;
+
+ $used_by_model[$cache_key] = [];
+ foreach ($datas as $data) {
+ if ($data['value'] != self::NO_VALUE) {
+ foreach ($options as $option) {
+ if (
+ isset($option['table'])
+ && ($option['table'] == getTableForItemType($data['itemtype']))
+ && ($option['linkfield'] == $data['value'])
+ && ($option['displaytype'] != 'multiline_text')
+ ) {
+ $used_by_model[$cache_key][$option['linkfield']] = $option['linkfield'];
+ break;
+ }
}
}
}
}
- return $used;
+ //The field of the current row stays available
+ return array_filter(
+ $used_by_model[$cache_key],
+ fn($linkfield): bool => $mapping_or_info['value'] != $linkfield,
+ );
}
}
diff --git a/inc/mapping.class.php b/inc/mapping.class.php
index 20dfa36e..aa62f025 100644
--- a/inc/mapping.class.php
+++ b/inc/mapping.class.php
@@ -114,13 +114,16 @@ public static function showFormMappings(PluginDatainjectionModel $model)
foreach ($model->getMappings() as $mapping) {
ob_start();
$options = ['primary_type' => $model->fields['itemtype']];
- PluginDatainjectionInjectionType::dropdownLinkedTypes($mapping, $options);
+ PluginDatainjectionInjectionType::dropdownLinkedTypes($mapping, $options, false);
$dropdown_html = ob_get_clean();
+ $fields_html = PluginDatainjectionInjectionType::getFieldsHtml($mapping, $options);
$mappings[] = [
- 'id' => $mapping->getID(),
- 'name' => $mapping->fields['name'],
- 'dropdown_html' => $dropdown_html,
+ 'id' => $mapping->getID(),
+ 'name' => $mapping->fields['name'],
+ 'dropdown_html' => $dropdown_html,
+ 'field_html' => $fields_html['field'],
+ 'mandatory_html' => $fields_html['mandatory'],
];
}
diff --git a/inc/model.class.php b/inc/model.class.php
index d719329e..910afdcf 100644
--- a/inc/model.class.php
+++ b/inc/model.class.php
@@ -424,6 +424,8 @@ public static function getModels($user_id, $order = "name", $entity = -1, $all =
'itemtype',
'step',
'comment',
+ 'behavior_add',
+ 'behavior_update',
],
'FROM' => 'glpi_plugin_datainjection_models',
'WHERE' => $where,
@@ -434,11 +436,45 @@ public static function getModels($user_id, $order = "name", $entity = -1, $all =
],
];
+ $candidates = [];
foreach ($DB->request($query) as $data) {
+ if (class_exists($data['itemtype'])) {
+ $candidates[$data['id']] = $data;
+ }
+ }
+
+ if ($candidates === []) {
+ return $models;
+ }
+
+ //Itemtypes of all candidate models, read once instead of once per model
+ $itemtypes = [];
+ foreach ($candidates as $models_id => $data) {
+ $itemtypes[$models_id] = [$data['itemtype']];
+ }
+
+ foreach (['glpi_plugin_datainjection_mappings', 'glpi_plugin_datainjection_infos'] as $table) {
+ $iterator = $DB->request([
+ 'SELECT' => ['models_id', 'itemtype'],
+ 'DISTINCT' => true,
+ 'FROM' => $table,
+ 'WHERE' => ['models_id' => array_keys($candidates)],
+ ]);
+
+ foreach ($iterator as $row) {
+ $itemtypes[$row['models_id']][] = $row['itemtype'];
+ }
+ }
+
+ foreach ($candidates as $models_id => $data) {
if (
- self::checkRightOnModel((int) $data['id'])
- && class_exists($data['itemtype'])
+ self::checkRightOnItemtypes(
+ $itemtypes[$models_id],
+ (bool) $data['behavior_add'],
+ (bool) $data['behavior_update'],
+ )
) {
+ unset($data['behavior_add'], $data['behavior_update']);
$models[] = $data;
}
}
@@ -1277,9 +1313,23 @@ public static function checkRightOnModel(int $models_id): bool
}
}
- $check_add = (bool) ($model->fields['behavior_add'] ?? 0);
- $check_update = (bool) ($model->fields['behavior_update'] ?? 0);
+ return self::checkRightOnItemtypes(
+ $itemtypes,
+ (bool) ($model->fields['behavior_add'] ?? 0),
+ (bool) ($model->fields['behavior_update'] ?? 0),
+ );
+ }
+
+ /**
+ * Check that the current user can write the itemtypes injected by a model
+ *
+ * @param array $itemtypes itemtypes of the model, its mappings and its infos
+ * @param bool $check_add the model adds items
+ * @param bool $check_update the model updates items
+ */
+ private static function checkRightOnItemtypes(array $itemtypes, bool $check_add, bool $check_update): bool
+ {
//A model doing nothing still requires the creation right to be listed
if (!$check_add && !$check_update) {
$check_add = true;
diff --git a/setup.php b/setup.php
index 6f323a25..f3ef3b87 100644
--- a/setup.php
+++ b/setup.php
@@ -52,14 +52,13 @@ function plugin_init_datainjection()
$PLUGIN_HOOKS['csrf_compliant']['datainjection'] = true;
$PLUGIN_HOOKS['migratetypes']['datainjection'] = 'plugin_datainjection_migratetypes_datainjection';
- $plugin = new Plugin();
- if ($plugin->isActivated("datainjection")) {
- Plugin::registerClass(
- 'PluginDatainjectionProfile',
- ['addtabon' => ['Profile'],
- ],
- );
+ Plugin::registerClass(
+ 'PluginDatainjectionProfile',
+ ['addtabon' => ['Profile'],
+ ],
+ );
+<<<<<<< HEAD
//If directory doesn't exists, create it
if (!plugin_datainjection_checkDirectories()) {
@ mkdir(PLUGIN_DATAINJECTION_UPLOAD_DIR);
@@ -85,7 +84,31 @@ function plugin_init_datainjection()
$PLUGIN_HOOKS['add_javascript']['datainjection'] = 'js/datainjection.js';
$INJECTABLE_TYPES = [];
+=======
+ //If directory doesn't exists, create it
+ if (!plugin_datainjection_checkDirectories()) {
+ @ mkdir(PLUGIN_DATAINJECTION_UPLOAD_DIR);
+>>>>>>> 98486d9 (Fix(Perf): index models_id, check model rights in one pass and render mapping rows server-side (#716))
+ }
+
+ $PLUGIN_HOOKS["config_page"]['datainjection'] = "front/clientinjection.form.php";
+
+
+
+ if (Session::haveRight('plugin_datainjection_use', READ)) {
+ $PLUGIN_HOOKS["menu_toadd"]['datainjection'] = ['tools' => 'PluginDatainjectionMenu'];
+ }
+
+ $PLUGIN_HOOKS['pre_item_purge']['datainjection']
+ = ['Profile' => ['PluginDatainjectionProfile', 'purgeProfiles']];
+
+ // Css and Javascript files, only needed on the plugin pages
+ if (str_contains($_SERVER['REQUEST_URI'] ?? '', Plugin::getPhpDir('datainjection', false))) {
+ $PLUGIN_HOOKS['add_css']['datainjection'] = 'css/datainjection.css';
+ $PLUGIN_HOOKS['add_javascript']['datainjection'] = 'js/datainjection.js';
}
+
+ $INJECTABLE_TYPES = [];
}
diff --git a/templates/mappings_form.html.twig b/templates/mappings_form.html.twig
index 13a4cf93..ffe3fac7 100644
--- a/templates/mappings_form.html.twig
+++ b/templates/mappings_form.html.twig
@@ -36,8 +36,29 @@
{% endif %}
+<<<<<<< HEAD
+=======
+
+
+
{{ __('Header of the file', 'datainjection') }}
+
{{ __('Tables', 'datainjection') }}
+
{{ _n('Field', 'Fields', 2) }}
+
{{ __('Link field', 'datainjection') }}
+
+
+ {% for mapping in mappings %}
+
+
{{ mapping.name }}
+
{{ mapping.dropdown_html|raw }}
+
{{ mapping.field_html|raw }}
+
{{ mapping.mandatory_html|raw }}
+
+ {% endfor %}
+
+ {% if canedit %}
+>>>>>>> 98486d9 (Fix(Perf): index models_id, check model rights in one pass and render mapping rows server-side (#716))