Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -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
48 changes: 37 additions & 11 deletions inc/common.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,40 @@ abstract class PluginPdfCommon extends CommonGLPI

public static $rightname = 'plugin_pdf';

protected static function get_label_value(string $label, mixed $value): string
{
return '<b><i>' . sprintf(__s('%1$s: %2$s'), $label . '</i></b>', $value);
}

protected static function get_dropdown_values(string $table, mixed $values): string
{
if (!is_array($values)) {
return Toolbox::stripTags(Dropdown::getDropdownName($table, $values));
}

$names = array_filter(array_map(
static fn($value) => Toolbox::stripTags(Dropdown::getDropdownName($table, $value)),
$values,
));

return implode(', ', $names);
}

protected static function get_group_value(CommonGLPI $item, string $field = 'groups_id'): string
{
return self::get_dropdown_values('glpi_groups', $item->fields[$field] ?? []);
}

protected static function get_group_column(CommonGLPI $item, string $field = 'groups_id', ?string $label = null): string
{
return self::get_label_value($label ?? __s('Group'), self::get_group_value($item, $field));
}

protected static function display_group_line(PluginPdfSimplePDF $pdf, CommonGLPI $item, string $right_column): void
{
$pdf->displayLine(self::get_group_column($item), $right_column);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I would recommend not adding a dedicated function to manage columns, rows, or labels for groups.

As it stands, only a very small portion of the plugin would actually use it, while the vast majority still relies on $pdf->displayLine().

Regarding the handling of multi-group assignments:

Group_Item, being a CommonDBRelation, provides the getItemsAssociatedTo method. This allows us to retrieve all groups associated with a given asset.

(Note that some assets may not have multiple groups.)

Therefore, the logic should be conditional:

if (!Toolbox::hasTrait(Asset::class, AssignableItem::class)) {

    Dropdown::getDropdownName(
        'glpi_groups',
        $item->fields['groups_id_tech']
    );

} else {

    $group_item = new Group_Item();
    $groups = $group_item->getItemsAssociatedTo(Asset::class, $items_id);

   //here filter groups depending of type (groups_id, groups_id_tech etc ...à

}

After that, the line can be constructed using displayLine.

/**
* Constructor, should intialize $this->obj property
**/
Expand Down Expand Up @@ -506,17 +540,9 @@ public static function mainLine(PluginPdfSimplePDF $pdf, $item, $field)
);
case 'group-model':
return $pdf->displayLine(
'<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'],
),
),
'<b><i>' . sprintf(
__s('%1$s: %2$s'),
__s('Model') . '</i></b>',
self::get_group_column($item, 'groups_id_tech', __s('Group in charge of the hardware')),
self::get_label_value(
__s('Model'),
Toolbox::stripTags(Dropdown::getDropdownName(
'glpi_' . $type . 'models',
$item->fields[$type . 'models_id'],
Expand Down
13 changes: 4 additions & 9 deletions inc/computer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,12 @@ public static function pdfMain(PluginPdfSimplePDF $pdf, Computer $computer)
);

$pdf->displayLine(
'<b><i>' . sprintf(
__('%1$s: %2$s'),
__('Group') . '</i></b>',
Dropdown::getDropdownName(
'glpi_groups',
$computer->fields['groups_id'],
),
),
'<b><i>' . sprintf(__('%1$s: %2$s'), __('UUID') . '</i></b>', $computer->fields['uuid']),
self::get_group_column($computer),
self::get_group_column($computer, 'groups_id_tech', __('Group in charge of the hardware')),
);

$pdf->displayLine(self::get_label_value(__('UUID'), $computer->fields['uuid']));

$pdf->displayLine(
'<b><i>' . sprintf(
__('%1$s: %2$s'),
Expand Down
15 changes: 4 additions & 11 deletions inc/monitor.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,10 @@ public static function pdfMain(PluginPdfSimplePDF $pdf, Monitor $item)
PluginPdfCommon::mainLine($pdf, $item, 'contact-otherserial');
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('Size') . '</i></b>',
sprintf(__s('%1$s %2$s'), $item->fields['size'], '"'),
),
self::display_group_line(
$pdf,
$item,
self::get_label_value(__s('Size'), sprintf(__s('%1$s %2$s'), $item->fields['size'], '"')),
);

$opts = ['have_micro' => __s('Microphone'),
Expand Down
11 changes: 3 additions & 8 deletions inc/networkequipment.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,10 @@ 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']),
),
self::get_group_column($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'),
sprintf(__s('%1$s (%2$s)'), __s('Memory'), __s('Mio')) . '</i></b>',
self::get_label_value(
sprintf(__s('%1$s (%2$s)'), __s('Memory'), __s('Mio')),
$item->fields['ram'],
),
);
Expand Down
11 changes: 4 additions & 7 deletions inc/peripheral.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,10 @@ public static function pdfMain(PluginPdfSimplePDF $pdf, Peripheral $item)
PluginPdfCommon::mainLine($pdf, $item, 'contact-otherserial');
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('Brand') . '</i></b>', $item->fields['brand']),
self::display_group_line(
$pdf,
$item,
self::get_label_value(__s('Brand'), $item->fields['brand']),
);

$pdf->setColumnsSize(100);
Expand Down
15 changes: 4 additions & 11 deletions inc/phone.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,10 @@ public static function pdfMain(PluginPdfSimplePDF $pdf, Phone $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('UUID') . '</i></b>',
$item->fields['uuid'],
),
self::display_group_line(
$pdf,
$item,
self::get_label_value(__s('UUID'), $item->fields['uuid']),
);

$pdf->displayLine(
Expand Down
15 changes: 4 additions & 11 deletions inc/printer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,10 @@ 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'],
),
self::display_group_line(
$pdf,
$printer,
self::get_label_value(__s('UUID'), $printer->fields['uuid']),
);


Expand Down
22 changes: 4 additions & 18 deletions inc/software.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,14 @@ public static function pdfMain(PluginPdfSimplePDF $pdf, Software $software)
);

$pdf->displayLine(
'<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'],
),
),
'<b><i>' . sprintf(
__s('%1$s: %2$s'),
__s('User') . '</i></b>',
self::get_group_column($software, 'groups_id_tech', __s('Group in charge of the hardware')),
self::get_label_value(
__s('User'),
$dbu->getUserName($software->fields['users_id']),
),
);

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

$pdf->displayLine(
'<b><i>' . sprintf(
Expand Down