Skip to content

Commit a15ed25

Browse files
committed
GLPI 11 compatibility
1 parent 1de6d59 commit a15ed25

28 files changed

+158
-58
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"require-dev": {
88
"friendsofphp/php-cs-fixer": "^3.73",
99
"friendsoftwig/twigcs": "^6.1",
10+
"glpi-project/phpstan-glpi": "^1.0",
1011
"glpi-project/tools": "^0.7.4",
1112
"php-parallel-lint/php-parallel-lint": "^1.4",
1213
"phpstan/extension-installer": "^1.4",

composer.lock

Lines changed: 49 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

front/export.massive.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,19 @@
3030
* --------------------------------------------------------------------------
3131
*/
3232

33+
/** @var array $PLUGIN_HOOKS */
34+
global $PLUGIN_HOOKS;
35+
3336
Session::checkRight('plugin_pdf', READ);
3437

3538
Plugin::load('pdf', true);
3639

3740
$type = $_SESSION['plugin_pdf']['type'];
38-
$item = new $type();
41+
$dbu = new DbUtils();
42+
$item = $dbu->getItemForItemtype($type);
43+
if (!$item) {
44+
throw new \InvalidArgumentException('Invalid item type: ' . $type);
45+
}
3946

4047
$tab_id = unserialize($_SESSION['plugin_pdf']['tab_id']);
4148
unset($_SESSION['plugin_pdf']['tab_id']);
@@ -65,8 +72,13 @@
6572
$tab[] = $type . '$main';
6673
}
6774

68-
if (isset($PLUGIN_HOOKS['plugin_pdf'][$type])) {
69-
$itempdf = new $PLUGIN_HOOKS['plugin_pdf'][$type]($item);
75+
if (isset($PLUGIN_HOOKS['plugin_pdf'][$type]) && class_exists($PLUGIN_HOOKS['plugin_pdf'][$type])) {
76+
$pdf_class = $PLUGIN_HOOKS['plugin_pdf'][$type];
77+
if (!is_a($pdf_class, PluginPdfCommon::class, true)) {
78+
throw new \RuntimeException('Invalid PDF plugin class for type: ' . $type);
79+
}
80+
81+
$itempdf = new $pdf_class($item);
7082
$itempdf->generatePDF($tab_id, $tab, (isset($pag) ? $pag : 0));
7183
} else {
7284
throw new \RuntimeException('Missing PDF plugin hook for type: ' . $type);

front/export.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,16 @@
6565
$tab[] = $type . '$main';
6666
}
6767

68-
if (isset($PLUGIN_HOOKS['plugin_pdf'][$type])
69-
&& class_exists($PLUGIN_HOOKS['plugin_pdf'][$type])) {
70-
$itempdf = new $PLUGIN_HOOKS['plugin_pdf'][$type]($item);
68+
if (
69+
isset($PLUGIN_HOOKS['plugin_pdf'][$type])
70+
&& class_exists($PLUGIN_HOOKS['plugin_pdf'][$type])
71+
) {
72+
$pdf_class = $PLUGIN_HOOKS['plugin_pdf'][$type];
73+
if (!is_a($pdf_class, PluginPdfCommon::class, true)) {
74+
throw new \RuntimeException('Invalid PDF plugin class for type: ' . $type);
75+
}
76+
77+
$itempdf = new $pdf_class($item);
7178
$itempdf->generatePDF([$_POST['itemID']], $tab, (isset($_POST['page']) ? $_POST['page'] : 0));
7279
} else {
7380
throw new \RuntimeException('Missing PDF plugin hook for type: ' . $type);

hook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function plugin_pdf_MassiveActions($type)
5151
default:
5252
if (isset($PLUGIN_HOOKS['plugin_pdf'][$type])) {
5353
return ['PluginPdfCommon' . MassiveAction::CLASS_ACTION_SEPARATOR . 'DoIt'
54-
=> __('Print to pdf', 'pdf')];
54+
=> '<i class="ti ti-file-type-pdf"></i>' . __('Print to pdf', 'pdf')];
5555
}
5656
}
5757

inc/appliance.class.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,10 @@ public static function showList_relation($pdf, $relID)
350350

351351
$item = $relation->fields['itemtype'];
352352

353-
$objtype = new $item();
353+
$objtype = $dbu->getItemForItemtype($item);
354+
if (!$objtype) {
355+
return;
356+
}
354357

355358
// selects all the attached relations
356359
$tablename = $dbu->getTableForItemType($item);

inc/common.class.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,7 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
123123
{
124124
if (Session::haveRight('plugin_pdf', READ)) {
125125
if (empty($withtemplate)) {
126-
$icon_html = sprintf('<i class="ti ti-%s"></i>', 'file-type-pdf');
127-
128-
return sprintf(
129-
'<span class="d-flex align-items-center">%s%s</span>',
130-
$icon_html,
131-
__('Print to pdf', 'pdf'),
132-
);
126+
return self::createTabEntry(__('Print to pdf', 'pdf'), 0, $item::getType(), PluginPdfConfig::getIcon());
133127
}
134128
}
135129
return '';
@@ -620,6 +614,9 @@ public static function processMassiveActionsForOneItemtype(
620614
CommonDBTM $item,
621615
array $ids
622616
) {
617+
/** @var array $CFG_GLPI */
618+
global $CFG_GLPI;
619+
623620
switch ($ma->getAction()) {
624621
case 'DoIt':
625622
$tab_id = [];
@@ -630,9 +627,8 @@ public static function processMassiveActionsForOneItemtype(
630627
}
631628
$_SESSION['plugin_pdf']['type'] = $item->getType();
632629
$_SESSION['plugin_pdf']['tab_id'] = serialize($tab_id);
633-
$webDir = Plugin::getPhpDir('pdf', false);
634630
echo "<script type='text/javascript'>
635-
location.href='/plugins/pdf/front/export.massive.php'</script>";
631+
location.href='" . $CFG_GLPI['root_doc'] . "/plugins/pdf/front/export.massive.php'</script>";
636632
break;
637633
}
638634
}

inc/config.class.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,19 @@ public static function canView(): bool
7979

8080
public static function getTypeName($nb = 0)
8181
{
82-
return __('Setup');
82+
return __('PDF settings', 'pdf');
8383
}
8484

8585
public function getName($params = [])
8686
{
8787
return __('Print to pdf', 'pdf');
8888
}
8989

90+
public static function getIcon()
91+
{
92+
return "ti ti-file-type-pdf";
93+
}
94+
9095
/**
9196
* Singleton for the unique config record
9297
*/
@@ -185,13 +190,7 @@ public static function showConfigForm($item)
185190
public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
186191
{
187192
if ($item->getType() == 'Config') {
188-
$icon_html = sprintf('<i class="ti ti-%s"></i>', 'file-type-pdf');
189-
190-
return sprintf(
191-
'<span class="d-flex align-items-center">%s%s</span>',
192-
$icon_html,
193-
self::getName(),
194-
);
193+
return self::createTabEntry(self::getTypeName(), 0, $item::getType(), self::getIcon());
195194
}
196195

197196
return '';

inc/contract_item.class.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,11 @@ public static function pdfForContract(PluginPdfSimplePDF $pdf, Contract $contrac
228228
$prem = true;
229229
$nb = count($datas);
230230
foreach ($datas as $objdata) {
231-
$item = new $itemtype();
231+
$dbu = new DbUtils();
232+
$item = $dbu->getItemForItemtype($itemtype);
233+
if (!$item) {
234+
continue;
235+
}
232236
if ($item instanceof Item_Devices) {
233237
$name = $objdata['name_device'];
234238
} else {

inc/item_device.class.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, $item)
6060

6161
$vide = true;
6262
foreach ($devtypes as $itemtype) {
63-
$devicetypes = new $itemtype();
63+
$dbu_local = new DbUtils();
64+
$devicetypes = $dbu_local->getItemForItemtype($itemtype);
65+
if (!$devicetypes) {
66+
continue;
67+
}
6468
$specificities = $devicetypes->getSpecificities();
6569
$specif_fields = array_keys($specificities);
6670

@@ -89,9 +93,15 @@ public static function pdfForItem(PluginPdfSimplePDF $pdf, $item)
8993
'GROUPBY' => $group_by,
9094
];
9195

92-
$device = new $associated_type();
93-
$itemdevice = new $itemtype();
96+
$dbu = new DbUtils();
97+
// Validate that the types are valid before using them
98+
if (!$dbu->getItemForItemtype($associated_type) || !$dbu->getItemForItemtype($itemtype)) {
99+
continue;
100+
}
101+
94102
foreach ($DB->request($query_params) as $data) {
103+
$device = $dbu->getItemForItemtype($associated_type);
104+
$itemdevice = $dbu->getItemForItemtype($itemtype);
95105
$itemdevice->getFromDB($data['id']);
96106
if ($device->getFromDB($data[$fk])) {
97107
$spec = $device->getAdditionalFields();

0 commit comments

Comments
 (0)