tableExists($table)) { return; } $existing = $DB->request([ 'FROM' => $table, 'WHERE' => [ 'itemtype' => $itemtype, ], 'LIMIT' => 1, ]); if (count($existing) > 0) { $row = $existing->current(); $DB->update( $table, [ 'is_active' => $is_active ? 1 : 0, 'is_default' => $is_default ? 1 : 0, 'date_mod' => $_SESSION['glpi_currenttime'] ?? date('Y-m-d H:i:s'), ], [ 'id' => (int) $row['id'], ] ); return; } $DB->insert( $table, [ 'itemtype' => $itemtype, 'is_active' => $is_active ? 1 : 0, 'is_default' => $is_default ? 1 : 0, 'date_creation' => $_SESSION['glpi_currenttime'] ?? date('Y-m-d H:i:s'), 'date_mod' => $_SESSION['glpi_currenttime'] ?? date('Y-m-d H:i:s'), ] ); } /** * Get assettypes table name. * * @return string */ public static function getAssetTypesTable(): string { return 'glpi_plugin_urbackup_assettypes'; } /** * Check whether itemtype is enabled for UrBackup. * * @param string $itemtype Itemtype * * @return bool */ public static function isItemtypeEnabled(string $itemtype): bool { global $DB; if ($itemtype === '') { return false; } if ($itemtype === 'Computer') { return true; } $table = self::getAssetTypesTable(); if (!$DB->tableExists($table)) { return false; } $iterator = $DB->request([ 'FROM' => $table, 'WHERE' => [ 'itemtype' => $itemtype, 'is_active' => 1, ], 'LIMIT' => 1, ]); return count($iterator) > 0; } /** * Get enabled itemtypes. * * @return array */ public static function getEnabledItemtypes(): array { global $DB; $types = ['Computer']; $table = self::getAssetTypesTable(); if (!$DB->tableExists($table)) { return $types; } $iterator = $DB->request([ 'FROM' => $table, 'WHERE' => [ 'is_active' => 1, ], 'ORDER' => 'itemtype', ]); foreach ($iterator as $row) { $itemtype = (string) $row['itemtype']; if ($itemtype !== '' && !in_array($itemtype, $types, true)) { $types[] = $itemtype; } } return $types; } /** * Register tabs on enabled asset types. * * @return void */ public static function registerAssetTabs(): void { foreach (self::getEnabledItemtypes() as $itemtype) { if (!class_exists($itemtype)) { continue; } if (!is_a($itemtype, CommonDBTM::class, true)) { continue; } \Plugin::registerClass(AssetTab::class, [ 'addtabon' => $itemtype, ]); } } /** * Show config form. * * @param int $ID ID * @param array $options Options * * @return bool */ public function showForm($ID, array $options = []): bool { Session::checkRight('config', UPDATE); echo "
"; echo "
"; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; foreach (self::getConfigurableAssetTypes() as $itemtype => $label) { $enabled = self::isItemtypeEnabled($itemtype); $is_default = ($itemtype === 'Computer'); $is_asset_definition = self::isAssetDefinition($itemtype); echo ""; echo ""; if ($itemtype === 'Computer') { echo ""; echo ""; } elseif ($is_asset_definition) { // GLPI 11 Asset Definition echo ""; echo ""; echo ""; } else { // Legacy type echo ""; echo ""; echo ""; } echo ""; } echo ""; echo ""; echo ""; echo "
" . htmlspecialchars(__('UrBackup configuration', 'urbackup')) . "
" . htmlspecialchars(__('Asset type', 'urbackup')) . "" . htmlspecialchars(__('Enabled', 'urbackup')) . "" . htmlspecialchars(__('Default', 'urbackup')) . "" . htmlspecialchars(__('Type', 'urbackup')) . "
" . htmlspecialchars($label) . "" . htmlspecialchars(__('Always', 'urbackup')) . "" . htmlspecialchars(__('Yes', 'urbackup')) . ""; Html::showCheckbox([ 'name' => 'assettypes[' . htmlspecialchars($itemtype) . '][is_active]', 'checked' => $enabled, ]); echo ""; Html::showCheckbox([ 'name' => 'assettypes[' . htmlspecialchars($itemtype) . '][is_default]', 'checked' => $is_default, ]); echo "Asset Definition"; Html::showCheckbox([ 'name' => 'assettypes[' . htmlspecialchars($itemtype) . ']', 'checked' => $enabled, ]); echo "" . ($is_default ? htmlspecialchars(__('Yes', 'urbackup')) : '') . "Legacy
"; echo Html::submit(__('Save', 'urbackup'), [ 'name' => 'update', 'class' => 'btn btn-primary', ]); echo "
"; echo "
"; Html::closeForm(); return true; } /** * Save configuration. * * @param array $input Input data * * @return void */ public static function saveConfiguration(array $input): void { Session::checkRight('config', UPDATE); $selected = $input['assettypes'] ?? []; foreach (self::getConfigurableAssetTypes() as $itemtype => $label) { if ($itemtype === 'Computer') { self::ensureAssetType('Computer', true, true); continue; } // Check if it's an Asset Definition (new format with is_active/is_default) if (self::isAssetDefinition($itemtype)) { $is_active = isset($selected[$itemtype]['is_active']) && (bool) $selected[$itemtype]['is_active']; $is_default = isset($selected[$itemtype]['is_default']) && (bool) $selected[$itemtype]['is_default']; self::ensureAssetType($itemtype, $is_active, $is_default); } else { // Legacy format (simple checkbox) $is_enabled = isset($selected[$itemtype]) && (bool) $selected[$itemtype]; self::ensureAssetType($itemtype, $is_enabled, false); } } } /** * Get configurable asset types (legacy + GLPI 11 Asset Definition). * * @return array */ public static function getConfigurableAssetTypes(): array { $types = [ 'Computer' => Computer::getTypeName(Session::getPluralNumber()), ]; // Legacy types $known_types = [ 'Printer', 'Peripheral', 'NetworkEquipment', 'Phone', 'Monitor', ]; foreach ($known_types as $itemtype) { if (!class_exists($itemtype)) { continue; } if (!is_a($itemtype, CommonDBTM::class, true)) { continue; } $types[$itemtype] = $itemtype::getTypeName(Session::getPluralNumber()); } // GLPI 11 Asset Definition if (class_exists(AssetDefinitionManager::class)) { try { $assetDefinitions = AssetDefinitionManager::getInstance()->getDefinitions(true); foreach ($assetDefinitions as $definition) { // Access fields directly like CommonDBTM $system_name = $definition->fields['system_name'] ?? ''; $name = $definition->fields['name'] ?? ''; if (!empty($system_name)) { $types[$system_name] = !empty($name) ? $name : $system_name; } } } catch (\Throwable $e) { // If AssetDefinitionManager fails, skip } } return $types; } /** * Check if itemtype is a GLPI 11 Asset Definition. * * @param string $itemtype Itemtype or system name * * @return bool */ public static function isAssetDefinition(string $itemtype): bool { if (!class_exists(AssetDefinitionManager::class)) { return false; } try { $assetDefinitions = AssetDefinitionManager::getInstance()->getDefinitions(true); foreach ($assetDefinitions as $definition) { if (($definition->fields['system_name'] ?? '') === $itemtype) { return true; } } } catch (\Throwable $e) { // If fails, return false } return false; } }