Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fix search on number fields: `is` / `is not` ignoring values from 1000, `<` / `>` comparing values as text, and `is not` excluding items without value
- Fix `Some mandatory fields are empty` error on a filled dropdown field when its block table contains a leftover column with the same name
- Fix GenericObject migration error message to name the required 3.0.x version
- Fix slow forms, lists and saves on large databases: block values were read without using their index, and label translations, field definitions and block lists were queried again for every label, list cell and request

## [1.24.7] - 2026-10-28

Expand Down
49 changes: 31 additions & 18 deletions inc/abstractcontainerinstance.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ public static function getSpecificValueToSelect($field, $name = '', $values = ''

$field_id = $options['searchopt']['pfields_fields_id'] ?? null;

$field_specs = new PluginFieldsField();
if ($field_id !== null && $field_specs->getFromDB($field_id)) {
$field_specs = $field_id !== null ? self::getFieldSpecs((int) $field_id) : null;
if ($field_specs !== null) {
$dropdown_matches = [];
if (
preg_match('/^dropdown-(?<class>.+)$/i', (string) $field_specs->fields['type'], $dropdown_matches) === 1
&& $field_specs->fields['multiple']
preg_match('/^dropdown-(?<class>.+)$/i', (string) $field_specs['type'], $dropdown_matches) === 1
&& $field_specs['multiple']
) {
$itemtype = $dropdown_matches['class'];
if (!is_a($itemtype, CommonDBTM::class, true)) {
Expand All @@ -127,10 +127,10 @@ public static function getSpecificValueToSelect($field, $name = '', $values = ''
'display' => false,
]);
} elseif (
$field_specs->fields['type'] === 'dropdown'
&& $field_specs->fields['multiple']
$field_specs['type'] === 'dropdown'
&& $field_specs['multiple']
) {
$itemtype = PluginFieldsDropdown::getClassname($field_specs->fields['name']);
$itemtype = PluginFieldsDropdown::getClassname($field_specs['name']);

return Dropdown::show($itemtype, [
'name' => $name,
Expand All @@ -151,20 +151,20 @@ public static function getSpecificValueToDisplay($field, $values, array $options

$field_id = $options['searchopt']['pfields_fields_id'] ?? null;

$field_specs = new PluginFieldsField();
if ($field_id !== null && $field_specs->getFromDB($field_id)) {
$field_specs = $field_id !== null ? self::getFieldSpecs((int) $field_id) : null;
if ($field_specs !== null) {
$dropdown_matches = [];
if (
preg_match('/^dropdown-(?<class>.+)$/i', (string) $field_specs->fields['type'], $dropdown_matches) === 1
&& $field_specs->fields['multiple']
preg_match('/^dropdown-(?<class>.+)$/i', (string) $field_specs['type'], $dropdown_matches) === 1
&& $field_specs['multiple']
) {
$itemtype = $dropdown_matches['class'];
if (!is_a($itemtype, CommonDBTM::class, true)) {
return ''; // Itemtype not exists (maybe a deactivated plugin)
}

if (empty($values[$field]) && !empty($field_specs->fields['default_value'])) {
$values[$field] = $field_specs->fields['default_value'];
if (empty($values[$field]) && !empty($field_specs['default_value'])) {
$values[$field] = $field_specs['default_value'];
}

if (empty($values[$field])) {
Expand All @@ -186,13 +186,13 @@ public static function getSpecificValueToDisplay($field, $values, array $options

return implode($options['separator'] ?? '<br />', $names);
} elseif (
$field_specs->fields['type'] === 'dropdown'
&& $field_specs->fields['multiple']
$field_specs['type'] === 'dropdown'
&& $field_specs['multiple']
) {
$itemtype = PluginFieldsDropdown::getClassname($field_specs->fields['name']);
$itemtype = PluginFieldsDropdown::getClassname($field_specs['name']);
if (empty($values[$field])) {
if (!empty($field_specs->fields["default_value"])) {
$values[$field] = $field_specs->fields['default_value'];
if (!empty($field_specs["default_value"])) {
$values[$field] = $field_specs['default_value'];
} else {
return ''; // Value not defined
}
Expand All @@ -213,6 +213,19 @@ public static function getSpecificValueToDisplay($field, $values, array $options
return parent::getSpecificValueToDisplay($field, $values, $options);
}

private static function getFieldSpecs(int $field_id): ?array
{
/** @var array<int, array<string, mixed>|null> $specs */
static $specs = [];

if (!array_key_exists($field_id, $specs)) {
$field = new PluginFieldsField();
$specs[$field_id] = $field->getFromDB($field_id) ? $field->fields : null;
}

return $specs[$field_id];
}

public static function addField($fieldname, $type, array $options = [])
{
$migration = new PluginFieldsMigration('0');
Expand Down
77 changes: 43 additions & 34 deletions inc/container.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class PluginFieldsContainer extends CommonDBTM
*/
private static ?WeakMap $pending_values = null;

/** @var array<int, array<string, mixed>>|null */
private static ?array $containers = null;

public static function canCreate(): bool
{
return self::canUpdate();
Expand Down Expand Up @@ -855,6 +858,8 @@ public function prepareInputForAdd($input)
// phpcs:ignore PSR1.Methods.CamelCapsMethodName
public function post_addItem()
{
self::$containers = null;

if (!isset($this->input['clone']) || !$this->input['clone']) {
//create profiles associated to this container
PluginFieldsProfile::createForContainer($this);
Expand Down Expand Up @@ -973,6 +978,20 @@ public static function generateTemplate($fields)
return true;
}

// phpcs:ignore PSR1.Methods.CamelCapsMethodName
public function post_updateItem($history = true)
{
parent::post_updateItem($history);
self::$containers = null;
}

// phpcs:ignore PSR1.Methods.CamelCapsMethodName
public function post_purgeItem()
{
parent::post_purgeItem();
self::$containers = null;
}

// phpcs:ignore PSR1.Methods.CamelCapsMethodName
public function pre_deleteItem()
{
Expand Down Expand Up @@ -1055,7 +1074,7 @@ public static function preItemPurge($item)
}

$fields = new $classname();
$fields->deleteByCriteria(['items_id' => $item->fields['id']], true);
$fields->deleteByCriteria(['itemtype' => $itemtype, 'items_id' => $item->fields['id']], true);
}
}

Expand Down Expand Up @@ -1570,20 +1589,15 @@ public static function getEntries($type = 'tab', $full = false): array
/** @var DBmysql $DB */
global $DB;

$condition = [
'is_active' => 1,
];
if ($type !== 'all') {
$condition[] = ['type' => $type];
}

if (!$DB->tableExists(self::getTable())) {
return [];
}

$itemtypes = [];
$container = new self();
$found = $container->find($condition, 'label');
$found = array_filter(
self::getContainers(),
fn(array $container): bool => $container['is_active'] == 1 && ($type === 'all' || $container['type'] === $type),
);

if (
$found === []
Expand Down Expand Up @@ -1648,28 +1662,15 @@ public static function getEntries($type = 'tab', $full = false): array

public static function getUsedItemtypes($type = 'all', $must_be_active = false)
{
/** @var DBmysql $DB */
global $DB;
$itemtypes = [];
$where = [];

if ($type !== 'all') {
$where['type'] = $type;
}

if ($must_be_active) {
$where['is_active'] = 1;
}

$iterator = $DB->request([
'SELECT' => 'itemtypes',
'DISTINCT' => true,
'FROM' => self::getTable(),
'WHERE' => $where,
]);
$containers = array_filter(
self::getContainers(),
fn(array $container): bool => ($type === 'all' || $container['type'] === $type)
&& (!$must_be_active || $container['is_active'] == 1),
);

foreach ($iterator as $data) {
$jsonitemtype = PluginFieldsToolbox::decodeJSONItemtypes($data['itemtypes']);
foreach (array_unique(array_column($containers, 'itemtypes')) as $json_itemtypes) {
$jsonitemtype = PluginFieldsToolbox::decodeJSONItemtypes($json_itemtypes);
if (is_array($jsonitemtype)) {
$itemtypes = array_merge($itemtypes, $jsonitemtype);
}
Expand All @@ -1678,6 +1679,14 @@ public static function getUsedItemtypes($type = 'all', $must_be_active = false)
return $itemtypes;
}

/**
* @return array<int, array<string, mixed>>
*/
private static function getContainers(): array
{
return self::$containers ??= (new self())->find([], 'label');
}

public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
{
if ($withtemplate) {
Expand Down Expand Up @@ -1790,7 +1799,7 @@ public function updateFieldsValues($data, $itemtype, $massiveaction = false)

$obj = new $classname();

$exist = $obj->getFromDBByCrit(['items_id' => $items_id]);
$exist = $obj->getFromDBByCrit(['itemtype' => $itemtype, 'items_id' => $items_id]);

// Convert "multiple" values into a JSON string
$multiple_fields_iterator = $DB->request([
Expand Down Expand Up @@ -1835,7 +1844,7 @@ public function updateFieldsValues($data, $itemtype, $massiveaction = false)
$obj = $dbu->getItemForItemtype($classname);

if ($obj !== false) {
if ($obj->getFromDBByCrit(['items_id' => $items_id]) === false) {
if ($obj->getFromDBByCrit(['itemtype' => $itemtype, 'items_id' => $items_id]) === false) {
// add fields data
$obj->add($data);
} else {
Expand Down Expand Up @@ -2296,7 +2305,7 @@ private static function getStoredValues(self $container, string $itemtype, int $
}

$values_obj = (new DbUtils())->getItemForItemtype(self::getClassname($itemtype, $container->fields['name']));
if ($values_obj === false || !$values_obj->getFromDBByCrit(['items_id' => $items_id])) {
if ($values_obj === false || !$values_obj->getFromDBByCrit(['itemtype' => $itemtype, 'items_id' => $items_id])) {
return [];
}

Expand Down Expand Up @@ -2741,7 +2750,7 @@ private static function checkContainerMandatory(CommonDBTM $item, PluginFieldsCo
$classname = self::getClassname($item::getType(), $loc_c->fields['name']);
$dbu = new DbUtils();
$obj = $dbu->getItemForItemtype($classname);
if ($obj !== false && $obj->getFromDBByCrit(['items_id' => $item->getID()])) {
if ($obj !== false && $obj->getFromDBByCrit(['itemtype' => $item::getType(), 'items_id' => $item->getID()])) {
$data += $obj->fields;
} else {
$data += self::getPendingValues($item, $c_id);
Expand Down
1 change: 1 addition & 0 deletions inc/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,7 @@ public static function prepareHtmlFields(
if ($obj instanceof CommonDBTM) {
$found_values = $obj->find(
[
'itemtype' => $item->getType(),
'plugin_fields_containers_id' => $first_field['plugin_fields_containers_id'],
'items_id' => $item->getID(),
],
Expand Down
51 changes: 43 additions & 8 deletions inc/labeltranslation.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class PluginFieldsLabelTranslation extends CommonDBChild

public static $items_id = 'items_id';

/** @var array<string, array<string, array<int, string>>> */
private static array $labels_by_language = [];

/**
* Install or update plugin base data.
*
Expand Down Expand Up @@ -125,6 +128,24 @@ public function prepareInputForAdd($input)
return $input;
}

public function post_addItem()
{
parent::post_addItem();
self::$labels_by_language = [];
}

public function post_updateItem($history = true)
{
parent::post_updateItem($history);
self::$labels_by_language = [];
}

public function post_purgeItem()
{
parent::post_purgeItem();
self::$labels_by_language = [];
}


public static function createForItem(CommonDBTM $item)
{
Expand Down Expand Up @@ -374,16 +395,30 @@ public static function getAlreadyTranslatedForItem($itemtype, $items_id)
*/
public static function getLabelFor(array $item)
{
$obj = new self();
$found = $obj->find(['itemtype' => $item['itemtype'],
'items_id' => $item['id'],
'language' => $_SESSION['glpilanguage'],
]);
$language = $_SESSION['glpilanguage'];
self::$labels_by_language[$language] ??= self::loadLabels($language);

if (count($found) > 0) {
return array_values($found)[0]['label'];
return self::$labels_by_language[$language][$item['itemtype']][(int) $item['id']] ?? $item['label'] ?? '';
}

/**
* @return array<string, array<int, string>>
*/
private static function loadLabels(string $language): array
{
/** @var DBmysql $DB */
global $DB;

$labels = [];
$iterator = $DB->request([
'SELECT' => ['itemtype', 'items_id', 'label'],
'FROM' => self::getTable(),
'WHERE' => ['language' => $language],
]);
foreach ($iterator as $row) {
$labels[$row['itemtype']][(int) $row['items_id']] = (string) $row['label'];
}

return $item['label'] ?? '';
return $labels;
}
}
13 changes: 7 additions & 6 deletions inc/questiontype.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,13 +521,14 @@ public static function getFieldsFromBlock(?int $block_id): array
public static function hasAvailableFields(): bool
{
$blocks = (new self())->getAvailableBlocks();
foreach (array_keys($blocks) as $block_id) {
$fields = (new self())->getFieldsFromBlock($block_id);
if ($fields !== []) {
return true;
}
if ($blocks === []) {
return false;
}

return false;
return countElementsInTable(PluginFieldsField::getTable(), [
'is_active' => 1,
'plugin_fields_containers_id' => array_keys($blocks),
'NOT' => ['type' => 'header'],
]) > 0;
}
}
5 changes: 3 additions & 2 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ function plugin_init_fields()
$pluginfields_autoloader = new PluginFieldsAutoloader([PLUGINFIELDS_CLASS_PATH]);
$pluginfields_autoloader->register();

if (!Plugin::isPluginActive('fields')) {
// Activated plugins are listed at boot; isPluginActive() queries the DB until init is over.
if (!in_array('fields', Plugin::getPlugins(), true)) {
return;
}

Expand Down Expand Up @@ -202,7 +203,7 @@ function plugin_init_fields()
}

// Add Fields to Datainjection
if (Plugin::isPluginActive('datainjection')) {
if (in_array('datainjection', Plugin::getPlugins(), true)) {
$PLUGIN_HOOKS['plugin_datainjection_populate']['fields'] = 'plugin_datainjection_populate_fields';
}

Expand Down