Skip to content
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Fix image (field : description) in to pdf
- Fix massive action PDF export redirecting to item list instead of generating the PDF
- Fix PDF export for assignable asset groups by properly handling multiple groups

## [4.1.2] - 2026-01-08

Expand Down
36 changes: 32 additions & 4 deletions inc/common.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,43 @@
* --------------------------------------------------------------------------
*/

use Glpi\Features\AssignableItemInterface;

abstract class PluginPdfCommon extends CommonGLPI
{
protected $obj = null;
protected $pdf = null;

public static $rightname = 'plugin_pdf';

protected static function getGroupName(CommonDBTM $item, int $group_type = Group_Item::GROUP_TYPE_NORMAL): string
{
$field = $group_type === Group_Item::GROUP_TYPE_TECH ? 'groups_id_tech' : 'groups_id';

if ($item instanceof AssignableItemInterface) {
// Many-to-many relation, stored in the glpi_groups_items pivot table
global $DB;
$it = $DB->request([
'SELECT' => 'groups_id',
'FROM' => Group_Item::getTable(),
'WHERE' => [
'itemtype' => $item::class,
'items_id' => $item->getID(),
'type' => $group_type,
],
]);
$group_ids = array_column(iterator_to_array($it), 'groups_id');
} else {
// One-to-one relation, direct column on the item (e.g. glpi_itilcategories)
$group_ids = (array) ($item->fields[$field] ?? 0);
}
Comment on lines +46 to +62

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The AssignableItemInterface branch re-queries glpi_groups_items directly, but this is redundant: AssignableItem::post_getFromDB() (src/Glpi/Features/AssignableItem.php) already calls loadGroupFields() on every getFromDB(), which populates $item->fields['groups_id'] / ['groups_id_tech'] as arrays of group ids straight from glpi_groups_items. Every caller of getGroupName() here reaches it through PluginPdfCommon::addHeader()$this->obj->getFromDB($ID), so that array is always already loaded by the time this runs.

The else branch's (array) ($item->fields[$field] ?? 0) already covers both shapes uniformly (an array cast is a no-op on an array, and wraps a scalar id in a single-element array), which is exactly the simplification you suggested and had applied in commit 1e1b4f5 before it was reverted by 8231424 ("handle many-to-many groups"). Dropping the instanceof branch removes an extra DB query per group field per item and directly resolves stonebuzz's "significant amount of duplicated code" comment, since it also brings this function in line with what AssignableItem already guarantees instead of adding a parallel lookup.


return implode(', ', array_filter(array_map(
static fn($group_id) => Toolbox::stripTags(Dropdown::getDropdownName('glpi_groups', $group_id)),
$group_ids,
)));
Comment on lines +64 to +67

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There’s something I’m not fully grasping in this PR.

In GLPI, an object can be assigned to a group using a one-to-one relationship (i.e., the object contains a groups_id or groups_id_tech column, for example in glpi_itilcategories).

Objects can also be assigned to multiple groups through an intermediate relation table, glpi_groups_items (cf: glpi_computers).

These objects rely on that relation table when they use the AssignableItem trait.

Here (unless I’m mistaken), we are only handling the first case.

}

/**
* Constructor, should intialize $this->obj property
**/
Expand All @@ -59,6 +89,7 @@ public function addStandardTab($itemtype, array &$ong, array $options)
$withtemplate = $options['withtemplate'];
}

// @phpstan-ignore-next-line function.impossibleType (itemtype is not always a class-string at runtime, despite CommonGLPI's phpdoc)
if (!is_numeric($itemtype) && ($obj = $dbu->getItemForItemtype($itemtype)) && (method_exists($itemtype, 'displayTabContentForPDF'))) {
$titles = $obj->getTabNameForItem($this->obj, $withtemplate);
if (!is_array($titles)) {
Expand Down Expand Up @@ -509,10 +540,7 @@ public static function mainLine(PluginPdfSimplePDF $pdf, $item, $field)
'<b><i>' . sprintf(
__s('%1$s: %2$s'),
__s('Group in charge of the hardware') . '</i></b>',
Dropdown::getDropdownName(
'glpi_groups',
$item->fields['groups_id_tech'],
),
self::getGroupName($item, Group_Item::GROUP_TYPE_TECH),
),
'<b><i>' . sprintf(
__s('%1$s: %2$s'),
Expand Down
19 changes: 14 additions & 5 deletions inc/computer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,21 @@ public static function pdfMain(PluginPdfSimplePDF $pdf, Computer $computer)
'<b><i>' . sprintf(
__('%1$s: %2$s'),
__('Group') . '</i></b>',
Dropdown::getDropdownName(
'glpi_groups',
$computer->fields['groups_id'],
),
self::getGroupName($computer),
),
'<b><i>' . sprintf(
__('%1$s: %2$s'),
__('Group in charge of the hardware') . '</i></b>',
self::getGroupName($computer, Group_Item::GROUP_TYPE_TECH),
),
);

$pdf->displayLine(
'<b><i>' . sprintf(
__('%1$s: %2$s'),
__('UUID') . '</i></b>',
$computer->fields['uuid'],
),
'<b><i>' . sprintf(__('%1$s: %2$s'), __('UUID') . '</i></b>', $computer->fields['uuid']),
);

$pdf->displayLine(
Expand Down
6 changes: 1 addition & 5 deletions inc/monitor.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ public static function pdfMain(PluginPdfSimplePDF $pdf, Monitor $item)
PluginPdfCommon::mainLine($pdf, $item, 'user-management');

$pdf->displayLine(
'<b><i>' . sprintf(
__s('%1$s: %2$s'),
__s('Group') . '</i></b>',
Dropdown::getDropdownName('glpi_groups', $item->fields['groups_id']),
),
'<b><i>' . sprintf(__s('%1$s: %2$s'), __s('Group') . '</i></b>', self::getGroupName($item)),
'<b><i>' . sprintf(
__s('%1$s: %2$s'),
__s('Size') . '</i></b>',
Expand Down
6 changes: 1 addition & 5 deletions inc/networkequipment.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ public static function pdfMain(PluginPdfSimplePDF $pdf, NetworkEquipment $item)
);

$pdf->displayLine(
'<b><i>' . sprintf(
__s('%1$s: %2$s'),
__s('Group') . '</i></b>',
Dropdown::getDropdownName('glpi_groups', $item->fields['groups_id']),
),
'<b><i>' . sprintf(__s('%1$s: %2$s'), __s('Group') . '</i></b>', self::getGroupName($item)),
'<b><i>' . __s('The MAC address and the IP of the equipment are included in an aggregated network port'),
'<b><i>' . sprintf(
__s('%1$s: %2$s'),
Expand Down
6 changes: 1 addition & 5 deletions inc/peripheral.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ public static function pdfMain(PluginPdfSimplePDF $pdf, Peripheral $item)
PluginPdfCommon::mainLine($pdf, $item, 'user-management');

$pdf->displayLine(
'<b><i>' . sprintf(
__s('%1$s: %2$s'),
__s('Group') . '</i></b>',
Dropdown::getDropdownName('glpi_groups', $item->fields['groups_id']),
),
'<b><i>' . sprintf(__s('%1$s: %2$s'), __s('Group') . '</i></b>', self::getGroupName($item)),
'<b><i>' . sprintf(__s('%1$s: %2$s'), __s('Brand') . '</i></b>', $item->fields['brand']),
);

Expand Down
12 changes: 2 additions & 10 deletions inc/phone.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,8 @@ public static function pdfMain(PluginPdfSimplePDF $pdf, Phone $item)


$pdf->displayLine(
'<b><i>' . sprintf(
__s('%1$s: %2$s'),
__s('Group') . '</i></b>',
Dropdown::getDropdownName('glpi_groups', $item->fields['groups_id']),
),
'<b><i>' . sprintf(
__s('%1$s: %2$s'),
__s('UUID') . '</i></b>',
$item->fields['uuid'],
),
'<b><i>' . sprintf(__s('%1$s: %2$s'), __s('Group') . '</i></b>', self::getGroupName($item)),
'<b><i>' . sprintf(__s('%1$s: %2$s'), __s('UUID') . '</i></b>', $item->fields['uuid']),
);

$pdf->displayLine(
Expand Down
12 changes: 2 additions & 10 deletions inc/printer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,8 @@ public static function pdfMain(PluginPdfSimplePDF $pdf, Printer $printer)
);

$pdf->displayLine(
'<b><i>' . sprintf(
__s('%1$s: %2$s'),
__s('Group') . '</i></b>',
Dropdown::getDropdownName('glpi_groups', $printer->fields['groups_id']),
),
'<b><i>' . sprintf(
__s('%1$s: %2$s'),
__s('UUID') . '</i></b>',
$printer->fields['uuid'],
),
'<b><i>' . sprintf(__s('%1$s: %2$s'), __s('Group') . '</i></b>', self::getGroupName($printer)),
'<b><i>' . sprintf(__s('%1$s: %2$s'), __s('UUID') . '</i></b>', $printer->fields['uuid']),
);


Expand Down
7 changes: 2 additions & 5 deletions inc/software.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ public static function pdfMain(PluginPdfSimplePDF $pdf, Software $software)
'<b><i>' . sprintf(
__s('%1$s: %2$s'),
__s('Group in charge of the hardware') . '</i></b>',
Dropdown::getDropdownName(
'glpi_groups',
$software->fields['groups_id_tech'],
),
self::getGroupName($software, Group_Item::GROUP_TYPE_TECH),
),
'<b><i>' . sprintf(
__s('%1$s: %2$s'),
Expand All @@ -109,7 +106,7 @@ public static function pdfMain(PluginPdfSimplePDF $pdf, Software $software)
'<b><i>' . sprintf(
__s('%1$s: %2$s'),
__s('Group') . '</i></b>',
Dropdown::getDropdownName('glpi_groups', $software->fields['groups_id']),
self::getGroupName($software),
),
);

Expand Down