Skip to content

Commit 6f94427

Browse files
committed
Add Orderable capacity and per-user permission filtering for GLPI 11 custom assets
Following review feedback on #574, replace the unconditional auto-registration of all active custom asset definitions with an explicit, admin-controlled opt-in via a new Orderable capacity: - Introduce PluginOrderOrderableCapacity (extends \Glpi\Asset\Capacity\AbstractCapacity). Admins enable/disable it per asset definition under Setup -> Asset definitions -> {asset} -> Capacities. Provides label, icon, description, and a usage count based on existing PluginOrderReference rows. - In plugin_init_order(), register the capacity with AssetDefinitionManager, then iterate active definitions and append the generated asset class to $ORDER_TYPES only when: 1. the Orderable capacity is enabled on that definition, AND 2. the current user passes $class::canView() (which delegates to the asset's AssignableItem READ check + is_active). - The class_exists() guard preserves backward compatibility on GLPI <= 10.x. End-to-end tested on GLPI 11.0.6, PHP 8.3, Order plugin 2.12.6: - Capacity appears in the Capacities tab with proper label/icon/description. - With capacity disabled: custom asset is absent from the Item type dropdown. - With capacity enabled: custom asset appears, references can be created, Generate item massive action renders and submits, items reach Taken delivery state, and generated assets appear in the Assets section. - Native itemtypes remain unaffected. Addresses review comments from @stonebuzz on #574.
1 parent 43f4e95 commit 6f94427

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [unreleased]
99

10+
### Added
11+
- Add an `Orderable` capacity for GLPI 11 custom asset definitions, allowing each definition to opt-in to being used as a Product reference itemtype. Asset classes are filtered by per-user read permission.
12+
1013
### Fixed
1114

1215
- Fix generate associated item massive action

inc/orderablecapacity.class.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* Capacity that flags a GLPI 11 custom asset definition as orderable
5+
* through the Order plugin. When enabled on an asset definition (Setup ->
6+
* Asset definitions -> {your asset} -> Capacities), the corresponding
7+
* generated asset class is appended to $ORDER_TYPES and becomes selectable
8+
* as an Item type when creating a Product reference.
9+
*/
10+
class PluginOrderOrderableCapacity extends \Glpi\Asset\Capacity\AbstractCapacity
11+
{
12+
public function getLabel(): string
13+
{
14+
return __('Orderable', 'order');
15+
}
16+
17+
public function getIcon(): string
18+
{
19+
return 'ti ti-shopping-cart';
20+
}
21+
22+
public function getDescription(): string
23+
{
24+
return __(
25+
'Allow this asset to be referenced as a Product reference and '
26+
. 'generated from the Generate item massive action.',
27+
'order'
28+
);
29+
}
30+
31+
public function getCapacityUsageDescription(string $classname): string
32+
{
33+
$count = 0;
34+
if (class_exists('PluginOrderReference')) {
35+
$count = countElementsInTable(
36+
\PluginOrderReference::getTable(),
37+
['itemtype' => $classname]
38+
);
39+
}
40+
return sprintf(
41+
_n('Used by %d order reference', 'Used by %d order references', $count, 'order'),
42+
$count
43+
);
44+
}
45+
}

setup.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,29 @@ function plugin_init_order()
125125
'Pdu',
126126
];
127127

128+
129+
// Register the Orderable capacity for GLPI 11 custom assets and append
130+
// any custom asset class that has it enabled to $ORDER_TYPES, provided
131+
// the current user is allowed to view it.
132+
if (class_exists(\Glpi\Asset\AssetDefinitionManager::class)) {
133+
$asset_manager = \Glpi\Asset\AssetDefinitionManager::getInstance();
134+
$orderable_capacity = new PluginOrderOrderableCapacity();
135+
$asset_manager->registerCapacity($orderable_capacity);
136+
$asset_manager->bootDefinitions();
137+
foreach ($asset_manager->getDefinitions(true) as $definition) {
138+
if (!$definition->hasCapacityEnabled($orderable_capacity)) {
139+
continue;
140+
}
141+
$custom_asset_class = $definition->getAssetClassName();
142+
if (
143+
!in_array($custom_asset_class, $ORDER_TYPES, true)
144+
&& $custom_asset_class::canView()
145+
) {
146+
$ORDER_TYPES[] = $custom_asset_class;
147+
}
148+
}
149+
}
150+
128151
$CFG_GLPI['plugin_order_types'] = $ORDER_TYPES;
129152

130153
$PLUGIN_HOOKS['pre_item_purge']['order'] = [

0 commit comments

Comments
 (0)