From 22808494e4059a5729a74bf58b45a3690facd3b5 Mon Sep 17 00:00:00 2001 From: Curtis Conard Date: Sun, 11 Aug 2024 17:40:39 -0400 Subject: [PATCH] add type and model dicts --- front/asset/ruledictionnarymodel.form.php | 51 +++++++++ front/asset/ruledictionnarymodel.php | 51 +++++++++ front/asset/ruledictionnarytype.form.php | 51 +++++++++ front/asset/ruledictionnarytype.php | 51 +++++++++ src/Glpi/Asset/AssetDefinition.php | 4 +- src/Glpi/Asset/AssetDefinitionManager.php | 123 ++++++++++++++++++++++ src/RuleCollection.php | 28 ++++- 7 files changed, 355 insertions(+), 4 deletions(-) create mode 100644 front/asset/ruledictionnarymodel.form.php create mode 100644 front/asset/ruledictionnarymodel.php create mode 100644 front/asset/ruledictionnarytype.form.php create mode 100644 front/asset/ruledictionnarytype.php diff --git a/front/asset/ruledictionnarymodel.form.php b/front/asset/ruledictionnarymodel.form.php new file mode 100644 index 000000000000..f4c86ca977cf --- /dev/null +++ b/front/asset/ruledictionnarymodel.form.php @@ -0,0 +1,51 @@ +. + * + * --------------------------------------------------------------------- + */ + +use Glpi\Asset\AssetDefinition; +use Glpi\Http\Response; + +$definition = new AssetDefinition(); +$classname = array_key_exists('class', $_GET) && $definition->getFromDBBySystemName((string)$_GET['class']) + ? $definition->getAssetClassName() + : null; + +if ($classname === null || !class_exists($classname)) { + Response::sendError(400, 'Bad request', Response::CONTENT_TYPE_TEXT_HTML); +} + +$rulecollection_class = 'RuleDictionary' . $classname . 'ModelCollection'; +$rulecollection = new $rulecollection_class(); + +include(GLPI_ROOT . "/front/rule.common.form.php"); diff --git a/front/asset/ruledictionnarymodel.php b/front/asset/ruledictionnarymodel.php new file mode 100644 index 000000000000..71f5f4bc1212 --- /dev/null +++ b/front/asset/ruledictionnarymodel.php @@ -0,0 +1,51 @@ +. + * + * --------------------------------------------------------------------- + */ + +use Glpi\Asset\AssetDefinition; +use Glpi\Http\Response; + +$definition = new AssetDefinition(); +$classname = array_key_exists('class', $_GET) && $definition->getFromDBBySystemName((string)$_GET['class']) + ? $definition->getAssetClassName() + : null; + +if ($classname === null || !class_exists($classname)) { + Response::sendError(400, 'Bad request', Response::CONTENT_TYPE_TEXT_HTML); +} + +$rulecollection_class = 'RuleDictionary' . $classname . 'ModelCollection'; +$rulecollection = new $rulecollection_class(); + +include(GLPI_ROOT . "/front/rule.common.php"); diff --git a/front/asset/ruledictionnarytype.form.php b/front/asset/ruledictionnarytype.form.php new file mode 100644 index 000000000000..db79d4d1d71c --- /dev/null +++ b/front/asset/ruledictionnarytype.form.php @@ -0,0 +1,51 @@ +. + * + * --------------------------------------------------------------------- + */ + +use Glpi\Asset\AssetDefinition; +use Glpi\Http\Response; + +$definition = new AssetDefinition(); +$classname = array_key_exists('class', $_GET) && $definition->getFromDBBySystemName((string)$_GET['class']) + ? $definition->getAssetClassName() + : null; + +if ($classname === null || !class_exists($classname)) { + Response::sendError(400, 'Bad request', Response::CONTENT_TYPE_TEXT_HTML); +} + +$rulecollection_class = 'RuleDictionary' . $classname . 'TypeCollection'; +$rulecollection = new $rulecollection_class(); + +include(GLPI_ROOT . "/front/rule.common.form.php"); diff --git a/front/asset/ruledictionnarytype.php b/front/asset/ruledictionnarytype.php new file mode 100644 index 000000000000..00321c8e834d --- /dev/null +++ b/front/asset/ruledictionnarytype.php @@ -0,0 +1,51 @@ +. + * + * --------------------------------------------------------------------- + */ + +use Glpi\Asset\AssetDefinition; +use Glpi\Http\Response; + +$definition = new AssetDefinition(); +$classname = array_key_exists('class', $_GET) && $definition->getFromDBBySystemName((string)$_GET['class']) + ? $definition->getAssetClassName() + : null; + +if ($classname === null || !class_exists($classname)) { + Response::sendError(400, 'Bad request', Response::CONTENT_TYPE_TEXT_HTML); +} + +$rulecollection_class = 'RuleDictionary' . $classname . 'TypeCollection'; +$rulecollection = new $rulecollection_class(); + +include(GLPI_ROOT . "/front/rule.common.php"); diff --git a/src/Glpi/Asset/AssetDefinition.php b/src/Glpi/Asset/AssetDefinition.php index 78fd736fd645..b86f8ccf4cbd 100644 --- a/src/Glpi/Asset/AssetDefinition.php +++ b/src/Glpi/Asset/AssetDefinition.php @@ -856,7 +856,7 @@ public function getAssetClassName(bool $with_namespace = true): string * Get the definition's concrete asset model class name. * * @param bool $with_namespace - * @return string + * @return class-string */ public function getAssetModelClassName(bool $with_namespace = true): string { @@ -867,7 +867,7 @@ public function getAssetModelClassName(bool $with_namespace = true): string * Get the definition's concrete asset type class name. * * @param bool $with_namespace - * @return string + * @return class-string */ public function getAssetTypeClassName(bool $with_namespace = true): string { diff --git a/src/Glpi/Asset/AssetDefinitionManager.php b/src/Glpi/Asset/AssetDefinitionManager.php index 96fbba2d40d5..270a7e6aace4 100644 --- a/src/Glpi/Asset/AssetDefinitionManager.php +++ b/src/Glpi/Asset/AssetDefinitionManager.php @@ -218,6 +218,10 @@ private function boostrapConcreteClass(AssetDefinition $definition): void public function autoloadAssetClass(string $classname): void { $patterns = [ + '/^Glpi\\\CustomAsset\\\RuleDictionary([A-Za-z]+)ModelCollection$/' => 'loadConcreteModelDictionaryCollectionClass', + '/^Glpi\\\CustomAsset\\\RuleDictionary([A-Za-z]+)TypeCollection$/' => 'loadConcreteTypeDictionaryCollectionClass', + '/^Glpi\\\CustomAsset\\\RuleDictionary([A-Za-z]+)Model$/' => 'loadConcreteModelDictionaryClass', + '/^Glpi\\\CustomAsset\\\RuleDictionary([A-Za-z]+)Type$/' => 'loadConcreteTypeDictionaryClass', '/^Glpi\\\CustomAsset\\\([A-Za-z]+)Model$/' => 'loadConcreteModelClass', '/^Glpi\\\CustomAsset\\\([A-Za-z]+)Type$/' => 'loadConcreteTypeClass', '/^Glpi\\\CustomAsset\\\([A-Za-z]+)$/' => 'loadConcreteClass', @@ -436,4 +440,123 @@ final class {$definition->getAssetTypeClassName(false)} extends AssetType { $reflected_class = new ReflectionClass($definition->getAssetTypeClassName()); $reflected_class->setStaticPropertyValue('definition', $definition); } + + private function loadConcreteModelDictionaryClass(AssetDefinition $definition): void + { + eval(<<getAssetModelClassName()} extends RuleDictionnaryDropdown +{ + public function getCriterias() + { + static \$criterias = []; + + if (count(\$criterias)) { + return \$criterias; + } + + \$criterias['name']['field'] = 'name'; + \$criterias['name']['name'] = _n('Model', 'Models', 1); + \$criterias['name']['table'] = {$definition->getAssetModelClassName()::getTable()}; + + \$criterias['manufacturer']['field'] = 'name'; + \$criterias['manufacturer']['name'] = Manufacturer::getTypeName(1); + \$criterias['manufacturer']['table'] = 'glpi_manufacturers'; + + return \$criterias; + } + + public function getActions() + { + \$actions = []; + \$actions['name']['name'] = _n('Model', 'Models', 1); + \$actions['name']['force_actions'] = ['append_regex_result', 'assign', 'regex_result']; + + return \$actions; + } +PHP + ); + } + + private function loadConcreteTypeDictionaryClass(AssetDefinition $definition): void + { + eval(<<getAssetTypeClassName()} extends RuleDictionnaryDropdown +{ + public function getCriterias() + { + static \$criterias = []; + + if (count(\$criterias)) { + return \$criterias; + } + + \$criterias['name']['field'] = 'name'; + \$criterias['name']['name'] = _n('Type', 'Types', 1); + \$criterias['name']['table'] = {$definition->getAssetTypeClassName()::getTable()}; + + return \$criterias; + } + + public function getActions() + { + \$actions = []; + \$actions['name']['name'] = _n('Type', 'Types', 1); + \$actions['name']['force_actions'] = ['append_regex_result', 'assign','regex_result']; + + return \$actions; + } +} +PHP + ); + } + + private function loadConcreteModelDictionaryCollectionClass(AssetDefinition $definition): void + { + eval(<<getAssetModelClassName()}Collection extends RuleDictionnaryDropdownCollection +{ + public \$item_table = {$definition->getAssetModelClassName()::getTable()}; + public \$menu_option = "model.{$definition->fields['system_name']}"; + + public function getTitle() + { + return sprintf(__('Dictionary of % models'), {$definition->getAssetModelClassName()}::getTypeName()); + } +} +PHP + ); + } + + private function loadConcreteTypeDictionaryCollectionClass(AssetDefinition $definition): void + { + eval(<<getAssetTypeClassName()}Collection extends RuleDictionnaryDropdownCollection +{ + public \$item_table = {$definition->getAssetTypeClassName()::getTable()}; + public \$menu_option = "model.{$definition->fields['system_name']}"; + + public function getTitle() + { + return sprintf(__('Dictionary of % types'), {$definition->getAssetTypeClassName()}::getTypeName()); + } +} +PHP + ); + } } diff --git a/src/RuleCollection.php b/src/RuleCollection.php index d16ef05fd063..b581e19ca635 100644 --- a/src/RuleCollection.php +++ b/src/RuleCollection.php @@ -2076,8 +2076,10 @@ public static function getDictionnaries(): array ]; } + $custom_assets = \Glpi\Asset\AssetDefinitionManager::getInstance()->getDefinitions(true); + if (Session::haveRight("rule_dictionnary_dropdown", READ)) { - $dictionnaries[] = [ + $model_dictionaries = [ 'type' => _n('Model', 'Models', Session::getPluralNumber()), 'entries' => [ [ @@ -2107,10 +2109,21 @@ public static function getDictionnaries(): array ] ] ]; + + foreach ($custom_assets as $custom_asset) { + $model_class = $custom_asset->getAssetModelClassName(); + $model_dictionaries['entries'][] = [ + 'label' => $model_class::getTypeName(Session::getPluralNumber()), + 'link' => 'asset/ruledictionarymodel.php?class=' . $custom_asset->fields['system_name'], + 'icon' => $model_class::getIcon(), + ]; + } + + $dictionnaries[] = $model_dictionaries; } if (Session::haveRight("rule_dictionnary_dropdown", READ)) { - $dictionnaries[] = [ + $type_dictionaries = [ 'type' => _n('Type', 'Types', Session::getPluralNumber()), 'entries' => [ [ @@ -2140,6 +2153,17 @@ public static function getDictionnaries(): array ] ] ]; + + foreach ($custom_assets as $custom_asset) { + $type_class = $custom_asset->getAssetTypeClassName(); + $type_dictionaries['entries'][] = [ + 'label' => $type_class::getTypeName(Session::getPluralNumber()), + 'link' => 'asset/ruledictionarytype.php?class=' . $custom_asset->fields['system_name'], + 'icon' => $type_class::getIcon(), + ]; + } + + $dictionnaries[] = $type_dictionaries; } if (Session::haveRight("rule_dictionnary_dropdown", READ)) {