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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fix errors when generating a pdf with software license/installation tabs
- Incomplete columns when exporting Users or Groups

### Changed

- Improve PDF generation performance (fewer SQL queries, faster rendering, session released during the export)

## [4.1.6] - 2026-10-06

### Fixed
Expand Down
3 changes: 3 additions & 0 deletions front/export.massive.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
}

$itempdf = new $pdf_class($item);

Session::writeClose();

$itempdf->generatePDF($tab_id, $tab, ($pag ?? 0));
} else {
throw new RuntimeException('Missing PDF plugin hook for type: ' . $type);
Expand Down
3 changes: 3 additions & 0 deletions front/export.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
}

$itempdf = new $pdf_class($item);

Session::writeClose();

$itempdf->generatePDF([$_POST['itemID']], $tab, ($_POST['page'] ?? 0));
} else {
throw new RuntimeException('Missing PDF plugin hook for type: ' . $type);
Expand Down
5 changes: 5 additions & 0 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ function plugin_pdf_postinit()
foreach ($PLUGIN_HOOKS['plugin_pdf'] as $type => $typepdf) {
CommonGLPI::registerStandardTab($type, $typepdf);
}

$active_plugins = Plugin::getPlugins();
if (in_array('datainjection', $active_plugins, true) || in_array('geststock', $active_plugins, true)) {
$PLUGIN_HOOKS['menu_entry']['pdf'] = 'front/preference.form.php';
}
}


Expand Down
8 changes: 3 additions & 5 deletions inc/changetask.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public static function pdfForChange(PluginPdfSimplePDF $pdf, Change $job)
/** @var DBmysql $DB */
global $DB;

$dbu = new DbUtils();

$ID = $job->getField('id');

$result = $DB->request([
Expand Down Expand Up @@ -101,17 +99,17 @@ public static function pdfForChange(PluginPdfSimplePDF $pdf, Change $job)
$planification .= '<br>' . sprintf(
__s('%1$s: %2$s'),
__s('By'),
$dbu->getUserName($data['users_id_tech']),
PluginPdfCommon::getUserNameCached($data['users_id_tech']),
);
}

$lib = $data['taskcategories_id'] ? Dropdown::getDropdownName('glpi_taskcategories', $data['taskcategories_id']) : '';
$lib = $data['taskcategories_id'] ? PluginPdfCommon::getDropdownNameCached('glpi_taskcategories', $data['taskcategories_id']) : '';

$pdf->displayLine(
'</b>' . Toolbox::stripTags($lib),
Html::convDateTime($data['date']),
Html::timestampToString($data['actiontime'], false),
Toolbox::stripTags($dbu->getUserName($data['users_id'])),
Toolbox::stripTags(PluginPdfCommon::getUserNameCached($data['users_id'])),
Toolbox::stripTags($planification),
1,
);
Expand Down
6 changes: 2 additions & 4 deletions inc/changevalidation.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public static function pdfForChange(PluginPdfSimplePDF $pdf, Change $change)
/** @var DBmysql $DB */
global $DB;

$dbu = new DbUtils();

$pdf->setColumnsSize(100);
$pdf->displayTitle('<b>' . __s('Approvals for the change', 'pdf') . '</b>');

Expand Down Expand Up @@ -91,10 +89,10 @@ public static function pdfForChange(PluginPdfSimplePDF $pdf, Change $change)
$pdf->displayLine(
TicketValidation::getStatus($row['status']),
Html::convDateTime($row['submission_date']),
$dbu->getUserName($row['users_id']),
PluginPdfCommon::getUserNameCached($row['users_id']),
trim(html_entity_decode($row['comment_submission'] ?? '', ENT_QUOTES, 'UTF-8')),
Html::convDateTime($row['validation_date']),
$dbu->getUserName($row['users_id_validate']),
PluginPdfCommon::getUserNameCached($row['users_id_validate']),
trim(html_entity_decode($row['comment_validation'] ?? '', ENT_QUOTES, 'UTF-8')),
);
}
Expand Down
40 changes: 40 additions & 0 deletions inc/common.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,46 @@ public static function getGroupNames(array $groups_ids): string
return Toolbox::stripTags(implode(', ', $names));
}

/**
* Get the name of a user, memoized for the current request
*
* @param int $users_id
*
* @return string
*/
public static function getUserNameCached($users_id): string
{
static $names = [];

$users_id = (int) $users_id;
if (!array_key_exists($users_id, $names)) {
$dbu = new DbUtils();
$names[$users_id] = $dbu->getUserName($users_id);
}

return $names[$users_id];
}

/**
* Get the name of a dropdown value, memoized for the current request
*
* @param string $table
* @param int $id
*
* @return string
*/
public static function getDropdownNameCached(string $table, $id): string
{
static $names = [];

$key = $table . '#' . (int) $id;
if (!array_key_exists($key, $names)) {
$names[$key] = (string) Dropdown::getDropdownName($table, $id);
}

return $names[$key];
}

/**
* Get the groups ids of several items of the same type, indexed by item id
*
Expand Down
2 changes: 1 addition & 1 deletion inc/commonitilcost.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $job)
$data['name'],
Html::convDate($data['begin_date']),
Html::convDate($data['end_date']),
Toolbox::stripTags(Dropdown::getDropdownName(
Toolbox::stripTags(PluginPdfCommon::getDropdownNameCached(
'glpi_budgets',
$data['budgets_id'],
)),
Expand Down
53 changes: 31 additions & 22 deletions inc/config.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ public static function install(Migration $mig)

public static function showConfigForm($item)
{
/** @var array $PDF_DEVICES */
global $PDF_DEVICES;
$config = self::getInstance();

$config->showFormHeader();
Expand All @@ -167,7 +165,7 @@ public static function showConfigForm($item)
}

$options = [];
foreach ($PDF_DEVICES as $option => $value) {
foreach (self::getDevices() as $option => $value) {
$options[$option] = $option . ' - ' . $value[0] . ' (' . $value[1] . ')';
}

Expand Down Expand Up @@ -360,28 +358,42 @@ public static function currency()
'ZWL' => [__s('Zimbabwe Dollar', 'pdf'), '$', false]];
}

public static function formatNumber($value)
/**
* Get the list of the currencies
*
* @return array
*/
public static function getDevices()
{
/** @var array $PDF_DEVICES */
/** @var array|null $PDF_DEVICES */
global $PDF_DEVICES;

return $PDF_DEVICES ??= self::currency();
}

$config = new Config();
$language = '';
foreach ($config->find(['context' => 'core',
'name' => 'language']) as $row) {
$language = $row['value'];
}
$user = new User();
$user->getFromDB($_SESSION['glpiID']);
if (!empty($user->fields['language'])) {
$language = $user->fields['language'];
public static function formatNumber($value)
{
static $formatters = [];

$users_id = (int) ($_SESSION['glpiID'] ?? 0);
if (!isset($formatters[$users_id])) {
$config = new Config();
$language = '';
foreach ($config->find(['context' => 'core',
'name' => 'language']) as $row) {
$language = $row['value'];
}
$user = new User();
$user->getFromDB($_SESSION['glpiID']);
if (!empty($user->fields['language'])) {
$language = $user->fields['language'];
}
$formatters[$users_id] = numfmt_create($language, NumberFormatter::CURRENCY);
}
$currency = PluginPdfConfig::getInstance();

$fmt = numfmt_create($language, NumberFormatter::CURRENCY);
$val = numfmt_format_currency($fmt, (float) $value, $currency->getField('currency'));
foreach ($PDF_DEVICES as $option => $value) {
$val = numfmt_format_currency($formatters[$users_id], (float) $value, $currency->getField('currency'));
foreach (self::getDevices() as $option => $value) {
if ($currency->fields['currency'] == $option) {
$sym = $value[1];

Expand All @@ -392,11 +404,8 @@ public static function formatNumber($value)

public static function currencyName()
{
/** @var array $PDF_DEVICES */
global $PDF_DEVICES;

$config = self::getInstance();
foreach ($PDF_DEVICES as $option => $value) {
foreach (self::getDevices() as $option => $value) {
if ($config->getField('currency') == $option && isset($value[2])) {
return $value[0];
}
Expand Down
5 changes: 2 additions & 3 deletions inc/infocom.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public function __construct(?CommonGLPI $obj = null)
public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item)
{
/** @var array $CFG_GLPI */
/** @var array $PDF_DEVICES */
global $CFG_GLPI, $PDF_DEVICES;
global $CFG_GLPI;

$ID = $item->getField('id');

Expand Down Expand Up @@ -210,7 +209,7 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item)
$currency = PluginPdfConfig::getInstance();
$sym = '';

foreach ($PDF_DEVICES as $option => $value) {
foreach (PluginPdfConfig::getDevices() as $option => $value) {
if ($currency->fields['currency'] == $option) {
$sym = $value[1];
}
Expand Down
70 changes: 33 additions & 37 deletions inc/item_softwareversion.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,38 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, $item)
$title = sprintf(__s('%1$s: %2$s'), $title, count($output));
$pdf->displayTitle($title);

$licenses_by_version = [];
foreach ($DB->request([
'SELECT' => [
'glpi_softwarelicenses.*',
'glpi_softwarelicensetypes.name AS type',
],
'FROM' => 'glpi_items_softwarelicenses',
'INNER JOIN' => [
'glpi_softwarelicenses' => [
'ON' => [
'glpi_items_softwarelicenses' => 'softwarelicenses_id',
'glpi_softwarelicenses' => 'id',
],
],
],
'LEFT JOIN' => [
'glpi_softwarelicensetypes' => [
'ON' => [
'glpi_softwarelicenses' => 'softwarelicensetypes_id',
'glpi_softwarelicensetypes' => 'id',
['AND' => ['glpi_items_softwarelicenses.itemtype' => 'Computer']],
],
],
],
'WHERE' => [
'glpi_items_softwarelicenses.items_id' => $ID,
],
]) as $licdata) {
$version_key = $licdata['softwareversions_id_use'] ?: $licdata['softwareversions_id_buy'];
$licenses_by_version[$version_key][] = $licdata;
}

$cat = -1;
foreach ($output as $soft) {
if ($soft['softwarecategories_id'] != $cat) {
Expand All @@ -459,45 +491,9 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, $item)

// From Item_SoftwareVersion::displaySoftsByCategory()
$verid = $soft['verid'];
$query_license_params = [
'SELECT' => [
'glpi_softwarelicenses.*',
'glpi_softwarelicensetypes.name AS type',
],
'FROM' => 'glpi_items_softwarelicenses',
'INNER JOIN' => [
'glpi_softwarelicenses' => [
'ON' => [
'glpi_items_softwarelicenses' => 'softwarelicenses_id',
'glpi_softwarelicenses' => 'id',
],
],
],
'LEFT JOIN' => [
'glpi_softwarelicensetypes' => [
'ON' => [
'glpi_softwarelicenses' => 'softwarelicensetypes_id',
'glpi_softwarelicensetypes' => 'id',
['AND' => ['glpi_items_softwarelicenses.itemtype' => 'Computer']],
],
],
],
'WHERE' => [
'glpi_items_softwarelicenses.items_id' => $ID,
'OR' => [
'glpi_softwarelicenses.softwareversions_id_use' => $verid,
[
'AND' => [
'glpi_softwarelicenses.softwareversions_id_use' => 0,
'glpi_softwarelicenses.softwareversions_id_buy' => $verid,
],
],
],
],
];

$lic = '';
foreach ($DB->request($query_license_params) as $licdata) {
foreach (($verid !== null ? $licenses_by_version[$verid] ?? [] : []) as $licdata) {
$installed[] = $licdata['id'];
$lic .= (empty($lic) ? '' : ', ') . '<b>' . $licdata['name'] . '</b> ' . $licdata['serial'];
if (!empty($licdata['type'])) {
Expand Down
6 changes: 2 additions & 4 deletions inc/itilfollowup.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item, $pr
/** @var DBmysql $DB */
global $DB;

$dbu = new DbUtils();

$ID = $item->getField('id');
$type = $item->getType();

Expand Down Expand Up @@ -88,14 +86,14 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item, $pr
'<b><i>' . __s('Requester') . '</i></b>'); // Author

foreach ($result as $data) {
$lib = $data['requesttypes_id'] ? Dropdown::getDropdownName('glpi_requesttypes', $data['requesttypes_id']) : '';
$lib = $data['requesttypes_id'] ? PluginPdfCommon::getDropdownNameCached('glpi_requesttypes', $data['requesttypes_id']) : '';
if ($data['is_private']) {
$lib = sprintf(__s('%1$s (%2$s)'), $lib, __s('Private'));
}
$pdf->displayLine(
Toolbox::stripTags($lib),
Html::convDateTime($data['date']),
Toolbox::stripTags($dbu->getUserName($data['users_id'])),
Toolbox::stripTags(PluginPdfCommon::getUserNameCached($data['users_id'])),
);

$content = $data['content'];
Expand Down
6 changes: 2 additions & 4 deletions inc/itilsolution.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item)
/** @var DBmysql $DB */
global $DB;

$dbu = new DbUtils();

$pdf->setColumnsSize(100);

$soluce = $DB->request(
Expand All @@ -63,7 +61,7 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item)
$pdf->displayTitle($title);
foreach ($soluce as $row) {
if ($row['solutiontypes_id']) {
$title = Toolbox::stripTags(Dropdown::getDropdownName(
$title = Toolbox::stripTags(PluginPdfCommon::getDropdownNameCached(
'glpi_solutiontypes',
$row['solutiontypes_id'],
));
Expand All @@ -89,7 +87,7 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, CommonDBTM $item)
sprintf(
__s('%1$s %2$s'),
__s('By'),
Toolbox::stripTags($dbu->getUserName($row['users_id_approval'])),
Toolbox::stripTags(PluginPdfCommon::getUserNameCached($row['users_id_approval'])),
)
. '</i>';
}
Expand Down
Loading
Loading