Skip to content

FIX / PDF export for assignable asset groups#63

Open
Mary-Clb wants to merge 4 commits intomainfrom
fix/pdf-assignable-groups-export
Open

FIX / PDF export for assignable asset groups#63
Mary-Clb wants to merge 4 commits intomainfrom
fix/pdf-assignable-groups-export

Conversation

@Mary-Clb
Copy link
Copy Markdown

  • I have performed a self-review of my code.
  • I have added tests (when available) that prove my fix is effective or that my feature works.
  • I have updated the CHANGELOG with a short functional description of the fix or new feature.
  • This change requires a documentation update.

Description

  • It fixes !43643
  • This PR fixes PDF exports for assignable GLPI assets when group fields are loaded as arrays of IDs. The PDF plugin was still assuming that groups_id and groups_id_tech were always scalar values, while GLPI now loads these fields from glpi_groups_items as arrays for assignable items. As a result, group-related fields could be empty or incorrectly rendered in generated PDFs.
    The classes updated are the ones impacted beacause they correspond to assets implementing the AssignableItem feature.

Screenshots :

Before fix :

Capture d’écran du 2026-04-29 11-09-40 Capture d’écran du 2026-04-29 11-10-18

After fix :

Capture d’écran du 2026-04-29 11-10-45

@Mary-Clb Mary-Clb self-assigned this Apr 29, 2026
@Mary-Clb Mary-Clb added the bug Something isn't working label Apr 29, 2026
@Mary-Clb Mary-Clb requested review from Rom1-B and stonebuzz April 29, 2026 12:04
@Rom1-B
Copy link
Copy Markdown

Rom1-B commented Apr 29, 2026

CHANGELOG please (always update it)

Comment thread inc/common.class.php Outdated
Comment on lines +40 to +73
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.

Copy link
Copy Markdown

@stonebuzz stonebuzz left a comment

Choose a reason for hiding this comment

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

Better 🙂

However, we now have a significant amount of duplicated code. We should refactor and consolidate these methods into a single unified implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants