-
Notifications
You must be signed in to change notification settings - Fork 51
Register active custom asset definitions as orderable itemtypes (GLPI 11) #574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
6f94427
5fd0975
13c246c
edcc339
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Capacity that flags a GLPI 11 custom asset definition as orderable | ||
| * through the Order plugin. When enabled on an asset definition (Setup -> | ||
| * Asset definitions -> {your asset} -> Capacities), the corresponding | ||
| * generated asset class is appended to $ORDER_TYPES and becomes selectable | ||
| * as an Item type when creating a Product reference. | ||
| */ | ||
| class PluginOrderOrderableCapacity extends \Glpi\Asset\Capacity\AbstractCapacity | ||
| { | ||
| public function getLabel(): string | ||
| { | ||
| return __('Orderable', 'order'); | ||
| } | ||
|
|
||
| public function getIcon(): string | ||
| { | ||
| return 'ti ti-shopping-cart'; | ||
| } | ||
|
|
||
| public function getDescription(): string | ||
| { | ||
| return __( | ||
| 'Allow this asset to be referenced as a Product reference and ' | ||
| . 'generated from the Generate item massive action.', | ||
| 'order' | ||
| ); | ||
| } | ||
|
|
||
| public function getCapacityUsageDescription(string $classname): string | ||
| { | ||
| $count = 0; | ||
| if (class_exists('PluginOrderReference')) { | ||
| $count = countElementsInTable( | ||
| \PluginOrderReference::getTable(), | ||
| ['itemtype' => $classname] | ||
| ); | ||
| } | ||
| return sprintf( | ||
| _n('Used by %d order reference', 'Used by %d order references', $count, 'order'), | ||
| $count | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Clean up plugin data linked to the asset class when the capacity is | ||
| * disabled on its definition: remove Product references targeting the | ||
| * class and order line items that link orders to instances of the class. | ||
| * Free-form references are intentionally left untouched, as they are | ||
| * standalone records that do not reference any itemtype. | ||
| */ | ||
| public function onCapacityDisabled(string $classname, \Glpi\Asset\CapacityConfig $config): void | ||
| { | ||
| // Delete order line items first: PluginOrderReference::pre_deleteItem() | ||
| // refuses deletion while references are still in use by orders_items, | ||
| // so child records must be removed before parent references. | ||
| if (class_exists('PluginOrderOrder_Item')) { | ||
| (new \PluginOrderOrder_Item())->deleteByCriteria( | ||
| ['itemtype' => $classname], | ||
| force: true, | ||
| history: false | ||
| ); | ||
| } | ||
| if (class_exists('PluginOrderReference')) { | ||
| (new \PluginOrderReference())->deleteByCriteria( | ||
| ['itemtype' => $classname], | ||
| force: true, | ||
| history: false | ||
| ); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -125,6 +125,29 @@ function plugin_init_order() | |||||||||||||||||||||||||||
| 'Pdu', | ||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Register the Orderable capacity for GLPI 11 custom assets and append | ||||||||||||||||||||||||||||
| // any custom asset class that has it enabled to $ORDER_TYPES, provided | ||||||||||||||||||||||||||||
| // the current user is allowed to view it. | ||||||||||||||||||||||||||||
| if (class_exists(\Glpi\Asset\AssetDefinitionManager::class)) { | ||||||||||||||||||||||||||||
| $asset_manager = \Glpi\Asset\AssetDefinitionManager::getInstance(); | ||||||||||||||||||||||||||||
| $orderable_capacity = new PluginOrderOrderableCapacity(); | ||||||||||||||||||||||||||||
| $asset_manager->registerCapacity($orderable_capacity); | ||||||||||||||||||||||||||||
| $asset_manager->bootDefinitions(); | ||||||||||||||||||||||||||||
| foreach ($asset_manager->getDefinitions(true) as $definition) { | ||||||||||||||||||||||||||||
| if (!$definition->hasCapacityEnabled($orderable_capacity)) { | ||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| $custom_asset_class = $definition->getAssetClassName(); | ||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||
| !in_array($custom_asset_class, $ORDER_TYPES, true) | ||||||||||||||||||||||||||||
| && $custom_asset_class::canView() | ||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||
| $ORDER_TYPES[] = $custom_asset_class; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
Comment on lines
+130
to
+153
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good start =) However, the Order plugin should introduce an "Order" capability for the custom Asset, allowing this feature to be enabled or disabled on a per-asset basis. Additionally, this functionality should verify the read permissions of the currently authenticated user for the specified custom Asset before proceeding.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the review @stonebuzz, both points were spot on. The branch has been updated (force-pushed to keep a single clean commit, 1. New 2.
End-to-end re-tested on GLPI 11.0.6 / PHP 8.3:
Also updated the PR description (top of this thread) and the CHANGELOG entry to reflect the new opt-in design. Let me know if you'd like any adjustments — happy to iterate.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You must declare your capacity in order for it to appear in the list. This should be done in the AssetDefinitionManager::getInstance()->registerCapacity(...)In addition, when a capacity is disabled, the associated data must be properly cleaned from the database. In particular, this concerns the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the follow-up @stonebuzz, the new commit On On the cleanup — implemented
The cascade order matters: End-to-end tested on GLPI 11.0.6 / PHP 8.3 on two installations:
Specifically on the second installation, disabling the capacity on a small custom asset definition while keeping a much larger one (~12k order line items, ~600 references) enabled left the larger one fully intact — confirming the targeted, per-class scope of the cleanup. Happy to iterate further if you'd like adjustments. |
||||||||||||||||||||||||||||
| $CFG_GLPI['plugin_order_types'] = $ORDER_TYPES; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| $PLUGIN_HOOKS['pre_item_purge']['order'] = [ | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.