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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ vendor/
.gh_token
*.min.*

# PHPUnit
.phpunit.cache/
.phpunit.result.cache
tests/files/_logs/*.log
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unrealese] - XXXX-XX-XX

### Added

- CustomAsset compatibility

## [2.9.0] - 2025-09-30

### Added
Expand Down
2 changes: 1 addition & 1 deletion front/config.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* You should have received a copy of the GNU General Public License
* along with GenInventoryNumber. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2008-2022 by GenInventoryNumber plugin team.
* @copyright Copyright (C) 2008-2025 by GenInventoryNumber plugin team.
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/pluginsGLPI/geninventorynumber
* -------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion front/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* You should have received a copy of the GNU General Public License
* along with GenInventoryNumber. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2008-2022 by GenInventoryNumber plugin team.
* @copyright Copyright (C) 2008-2025 by GenInventoryNumber plugin team.
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/pluginsGLPI/geninventorynumber
* -------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion front/configfield.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* You should have received a copy of the GNU General Public License
* along with GenInventoryNumber. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2008-2022 by GenInventoryNumber plugin team.
* @copyright Copyright (C) 2008-2025 by GenInventoryNumber plugin team.
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/pluginsGLPI/geninventorynumber
* -------------------------------------------------------------------------
Expand Down
14 changes: 12 additions & 2 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
* You should have received a copy of the GNU General Public License
* along with GenInventoryNumber. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2008-2022 by GenInventoryNumber plugin team.
* @copyright Copyright (C) 2008-2025 by GenInventoryNumber plugin team.
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/pluginsGLPI/geninventorynumber
* -------------------------------------------------------------------------
*/

use Glpi\Asset\AssetDefinition;
use Glpi\Plugin\Hooks;

function plugin_geninventorynumber_postinit()
{
/** @var array $GENINVENTORYNUMBER_TYPES */
Expand All @@ -40,6 +43,13 @@ function plugin_geninventorynumber_postinit()
$PLUGIN_HOOKS['pre_item_update']['geninventorynumber'][$type]
= ['PluginGeninventorynumberGeneration', 'preItemUpdate'];
}

$PLUGIN_HOOKS[Hooks::ITEM_ADD]['geninventorynumber'][AssetDefinition::class]
= ['PluginGeninventorynumberConfigField', 'registerAssetDefinitionConfigField'];
$PLUGIN_HOOKS[Hooks::ITEM_UPDATE]['geninventorynumber'][AssetDefinition::class]
= ['PluginGeninventorynumberConfigField', 'registerAssetDefinitionConfigField'];
$PLUGIN_HOOKS[Hooks::ITEM_PURGE]['geninventorynumber'][AssetDefinition::class]
= ['PluginGeninventorynumberConfigField', 'unregisterAssetDefinitionConfigField'];
}

function plugin_geninventorynumber_MassiveActions($type)
Expand Down Expand Up @@ -91,10 +101,10 @@ function plugin_geninventorynumber_uninstall()
include_once($php_dir . '/inc/config.class.php');
include_once($php_dir . '/inc/profile.class.php');
include_once($php_dir . '/inc/configfield.class.php');
PluginGeninventorynumberConfigField::uninstall($migration);
PluginGeninventorynumberConfig::uninstall($migration);
PluginGeninventorynumberProfile::removeRightsFromSession();
PluginGeninventorynumberProfile::uninstallProfile();
PluginGeninventorynumberConfigField::uninstall($migration);

return true;
}
2 changes: 1 addition & 1 deletion inc/config.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* You should have received a copy of the GNU General Public License
* along with GenInventoryNumber. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2008-2022 by GenInventoryNumber plugin team.
* @copyright Copyright (C) 2008-2025 by GenInventoryNumber plugin team.
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/pluginsGLPI/geninventorynumber
* -------------------------------------------------------------------------
Expand Down
172 changes: 136 additions & 36 deletions inc/configfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,20 @@
* You should have received a copy of the GNU General Public License
* along with GenInventoryNumber. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2008-2022 by GenInventoryNumber plugin team.
* @copyright Copyright (C) 2008-2025 by GenInventoryNumber plugin team.
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/pluginsGLPI/geninventorynumber
* -------------------------------------------------------------------------
*/

use Glpi\Asset\AssetDefinitionManager;
use GlpiPlugin\Geninventorynumber\Capacity\HasInventoryNumberGenerationCapacity;
use Glpi\Asset\AssetDefinition;
use Glpi\DBAL\QueryExpression;

use function Safe\strtotime;

/**
* -------------------------------------------------------------------------
* GenInventoryNumber plugin for GLPI
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GenInventoryNumber.
*
* GenInventoryNumber 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.
*
* GenInventoryNumber 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 GenInventoryNumber. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2008-2022 by GenInventoryNumber plugin team.
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/pluginsGLPI/geninventorynumber
* -------------------------------------------------------------------------
*/
use function Safe\preg_replace;
use function Safe\json_decode;

class PluginGeninventorynumberConfigField extends CommonDBChild
{
Expand Down Expand Up @@ -158,17 +135,31 @@ public static function install(Migration $migration)
}
}

public static function uninstall(Migration $migration)
public static function registerAssetDefinitionConfigField(AssetDefinition $item): void
{
if ($item->fields['is_active'] === "0") {
self::unregisterNewItemType($item->getAssetClassName());
} else {
self::registerNewItemType($item->getAssetClassName());
}
}

public static function unregisterAssetDefinitionConfigField(AssetDefinition $item): void
{
self::unregisterNewItemType($item->getAssetClassName());
}

public static function uninstall(Migration $migration): void
{
$definitions = AssetDefinitionManager::getInstance()->getDefinitions();
foreach ($definitions as $definition) {
self::disableCapacityForAsset($definition);
}
$migration->dropTable(getTableForItemType(self::class));
}

public static function showForConfig($id)
{
/** @var array $CFG_GLPI */
/** @var DBmysql $DB */
global $CFG_GLPI, $DB;

$config = new PluginGeninventorynumberConfig();
$config->getFromDB($id);
$target = Toolbox::getItemTypeFormUrl(self::class);
Expand Down Expand Up @@ -249,6 +240,104 @@ public function prepareInputForUpdate($input)
return $input;
}

public function post_updateItem($history = true)
{
parent::post_updateItem($history);

// Check if itemtype is a custom asset and if is_active changed
if (isset($this->fields['itemtype']) && in_array('is_active', $this->updates)) {
$itemtype = $this->fields['itemtype'];

// Check if it's a custom asset by verifying if it's in the Glpi\CustomAsset\Asset namespace
if (str_starts_with($itemtype, 'Glpi\\CustomAsset\\')) {
// Get the asset definition manager
$asset_manager = AssetDefinitionManager::getInstance();
$parts = explode('\\', $itemtype);
$system_name = end($parts);
// Remove 'Asset' at the end if present
$system_name = preg_replace('/Asset$/', '', $system_name);

// Get the asset definition
$definition = $asset_manager->getDefinition($system_name);

if ($definition !== null) {
$this->updateCapacity($definition);
}
}
}
}

public function updateCapacity(AssetDefinition $definition): bool
{
if ($this->fields['is_active']) {
return $this->enableCapacityForAsset($definition);
} else {
return static::disableCapacityForAsset($definition);
}
}

/**
* Enable a capacity for a custom asset definition
*
* @param AssetDefinition $definition
* @return bool
*/
public function enableCapacityForAsset(AssetDefinition $definition): bool
{
$capacity_classname = HasInventoryNumberGenerationCapacity::class;

// Get current capacities (decoded from JSON)
$current_capacities = json_decode($definition->fields['capacities'] ?? '[]', true) ?: [];

// Check if capacity is already enabled
if (in_array($capacity_classname, array_column($current_capacities, 'name'), true)) {
return true;
}

// Add the capacity (GLPI expects an array with 'name' and 'config' keys)
$current_capacities[] = [
'name' => $capacity_classname,
'config' => [],
];

// Update the definition
return $definition->update([
'id' => $definition->getID(),
'capacities' => $current_capacities,
]);
}

/**
* Disable a capacity for a custom asset definition
*
* @param AssetDefinition $definition
* @return bool
*/
public static function disableCapacityForAsset(AssetDefinition $definition): bool
{
$capacity_classname = HasInventoryNumberGenerationCapacity::class;

// Get current capacities (decoded from JSON)
$current_capacities = json_decode($definition->fields['capacities'] ?? '[]', true) ?: [];

// Check if capacity is not enabled
if (!in_array($capacity_classname, array_column($current_capacities, 'name'), true)) {
return true;
}

// Remove the capacity
$current_capacities = array_filter(
$current_capacities,
fn($capacity) => !(isset($capacity['name']) && $capacity['name'] === $capacity_classname),
);

// Update the definition
return $definition->update([
'id' => $definition->getID(),
'capacities' => $current_capacities,
]);
}

public static function getEnabledItemTypes()
{
/** @var DBmysql $DB */
Expand Down Expand Up @@ -379,6 +468,9 @@ public static function updateIndex($itemtype)

public static function registerNewItemType($itemtype)
{
/** @var array $GENINVENTORYNUMBER_TYPES */
global $GENINVENTORYNUMBER_TYPES;

if (!class_exists($itemtype)) {
return;
}
Expand All @@ -390,15 +482,23 @@ public static function registerNewItemType($itemtype)
$input['template'] = '&lt;#######&gt;';
$input['is_active'] = 0;
$input['index'] = 0;
$config->add($input);
if ($config->add($input) && !in_array($itemtype, $GENINVENTORYNUMBER_TYPES, true)) {
$GENINVENTORYNUMBER_TYPES[] = $itemtype;
}
}
}

public static function unregisterNewItemType($itemtype)
{
/** @var array $GENINVENTORYNUMBER_TYPES */
global $GENINVENTORYNUMBER_TYPES;

if (countElementsInTable(getTableForItemType(self::class), ['itemtype' => $itemtype])) {
$config = new self();
$config->deleteByCriteria(['itemtype' => $itemtype]);
$is_delete = $config->deleteByCriteria(['itemtype' => $itemtype]);
if ($is_delete) {
unset($GENINVENTORYNUMBER_TYPES[$itemtype]);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion inc/generation.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* You should have received a copy of the GNU General Public License
* along with GenInventoryNumber. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2008-2022 by GenInventoryNumber plugin team.
* @copyright Copyright (C) 2008-2025 by GenInventoryNumber plugin team.
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/pluginsGLPI/geninventorynumber
* -------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion inc/profile.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* You should have received a copy of the GNU General Public License
* along with GenInventoryNumber. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2008-2022 by GenInventoryNumber plugin team.
* @copyright Copyright (C) 2008-2025 by GenInventoryNumber plugin team.
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/pluginsGLPI/geninventorynumber
* -------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ parameters:
paths:
- front
- inc
- src
- tests
- hook.php
- setup.php
scanDirectories:
- ../../src
- ../../tests
bootstrapFiles:
- ../../stubs/glpi_constants.php
- ../../vendor/autoload.php
Expand Down
7 changes: 7 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<phpunit bootstrap="tests/bootstrap.php" colors="true" testdox="true">
<testsuites>
<testsuite name="Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
4 changes: 3 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* You should have received a copy of the GNU General Public License
* along with GenInventoryNumber. If not, see <http://www.gnu.org/licenses/>.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2008-2022 by GenInventoryNumber plugin team.
* @copyright Copyright (C) 2008-2025 by GenInventoryNumber plugin team.
* @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
* @link https://github.com/pluginsGLPI/geninventorynumber
* -------------------------------------------------------------------------
Expand All @@ -40,6 +40,8 @@
->withPaths([
__DIR__ . '/front',
__DIR__ . '/inc',
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withPhpVersion(PhpVersion::PHP_82)
->withCache(
Expand Down
Loading