-
Notifications
You must be signed in to change notification settings - Fork 52
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
Open
bygadd
wants to merge
4
commits into
pluginsGLPI:main
Choose a base branch
from
bygadd:fix/glpi11-custom-assets-and-generate-item
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6f94427
Add Orderable capacity and per-user permission filtering for GLPI 11 …
bygadd 5fd0975
Clean up linked plugin data when Orderable capacity is disabled
bygadd 13c246c
Update CHANGELOG.md
bygadd edcc339
Fix code style: licence header, namespace imports, statement spacing
bygadd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * ------------------------------------------------------------------------- | ||
| * Order plugin for GLPI | ||
| * ------------------------------------------------------------------------- | ||
| * | ||
| * LICENSE | ||
| * | ||
| * This file is part of Order. | ||
| * | ||
| * Order is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation; either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * Order is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with Order. If not, see <http://www.gnu.org/licenses/>. | ||
| * ------------------------------------------------------------------------- | ||
| * @copyright Copyright (C) 2009-2023 by Order plugin team. | ||
| * @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html | ||
| * @link https://github.com/pluginsGLPI/order | ||
| * ------------------------------------------------------------------------- | ||
| */ | ||
|
|
||
| use Glpi\Asset\Capacity\AbstractCapacity; | ||
| use Glpi\Asset\CapacityConfig; | ||
|
|
||
| /** | ||
| * 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 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 order line items first (parent | ||
| * Product references refuse deletion via pre_deleteItem() while still | ||
| * referenced by orders_items), then remove the references themselves. | ||
| * Free-form references are intentionally left untouched, as they are | ||
| * standalone records that do not reference any itemtype. | ||
| */ | ||
| public function onCapacityDisabled(string $classname, CapacityConfig $config): void | ||
| { | ||
| 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, | ||
| ); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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,
6f94427). Summary of the changes:1. New
Orderablecapacity —inc/orderablecapacity.class.phpintroducesPluginOrderOrderableCapacityextending\Glpi\Asset\Capacity\AbstractCapacity. Admins now toggle it per asset definition under Setup → Asset definitions → {asset} → Capacities. Provides label, icon (ti ti-shopping-cart), description, and a usage count based on existingPluginOrderReferencerows.2.
setup.phpfiltering — registers the capacity withAssetDefinitionManager, then iterates active definitions and appends a class to$ORDER_TYPESonly when:Orderablecapacity is enabled on that definition, and$class::canView()passes for the current user (which delegates to the asset'sAssignableItemREAD check + the definition'sis_activeflag).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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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
setup.phpfile using:In addition, when a capacity is disabled, the associated data must be properly cleaned from the database.
In particular, this concerns the
glpi_plugins_orderorder_itemtable, which—if I recall correctly—stores the relationship between an asset and an order.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the follow-up @stonebuzz, the new commit
5fd0975addresses the data cleanup point.On
registerCapacity()— small clarification: the current branch (6f94427and now5fd0975) already declares the capacity through\Glpi\Asset\AssetDefinitionManager::getInstance()->registerCapacity(...)insetup.php, so it appears in the Capacities tab on GLPI 11 asset definitions and the Orderable toggle is admin-controlled. The inline review thread above is anchored to the originalea13dd1push (the auto-register block) which is no longer in the branch — that may be why it reads as if the registration is missing.On the cleanup — implemented
onCapacityDisabled()onPluginOrderOrderableCapacity. When an admin disables Orderable on a definition, the callback removes:glpi_plugin_order_orders_itemswhoseitemtypematches the disabled custom asset class;glpi_plugin_order_referenceswhoseitemtypematches the disabled custom asset class.The cascade order matters:
PluginOrderReference::pre_deleteItem()rejects deletion while a reference is still in use byorders_items, so child records are deleted before parents. Free-form references (PluginOrderReferenceFree) are intentionally left untouched as they carry noitemtypereference.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.