connessione con hardware in assets
This commit is contained in:
+169
-33
@@ -11,9 +11,10 @@ declare(strict_types=1);
|
||||
namespace GlpiPlugin\Urbackup;
|
||||
|
||||
use CommonDBTM;
|
||||
use Dropdown;
|
||||
use Glpi\Asset\Asset;
|
||||
use Glpi\Asset\AssetDefinitionManager;
|
||||
use DBmysql;
|
||||
use GlpiPlugin\Urbackup\Capacity\UrBackupCapacity;
|
||||
use Html;
|
||||
use Session;
|
||||
|
||||
@@ -21,6 +22,16 @@ class Config extends CommonDBTM
|
||||
{
|
||||
public static $rightname = 'config';
|
||||
|
||||
/**
|
||||
* Config key storing whether the UrBackup tab is enabled on Computer.
|
||||
*/
|
||||
private const CONFIG_ENABLE_COMPUTER = 'enable_computer';
|
||||
|
||||
/**
|
||||
* In-memory cache of the `enable_computer` config value.
|
||||
*/
|
||||
private static ?bool $enable_computer_cache = null;
|
||||
|
||||
/**
|
||||
* Get type name.
|
||||
*
|
||||
@@ -45,17 +56,6 @@ class Config extends CommonDBTM
|
||||
return 'glpi_plugin_urbackup_configs';
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure default configuration.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function ensureDefaultConfiguration(): void
|
||||
{
|
||||
// Computer is always enabled by default.
|
||||
// Asset Definition types are managed via the native Capacities UI.
|
||||
}
|
||||
|
||||
/**
|
||||
* Get assettypes table name.
|
||||
*
|
||||
@@ -66,6 +66,47 @@ class Config extends CommonDBTM
|
||||
return 'glpi_plugin_urbackup_assettypes';
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the UrBackup tab is enabled on Computer items.
|
||||
*
|
||||
* Stored in glpi_plugin_urbackup_configs under the `enable_computer` key.
|
||||
* Enabled by default; falls back to true when the table does not exist yet
|
||||
* (e.g. during plugin install) or when the row is missing (backward
|
||||
* compatibility with versions where Computer was always enabled).
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function getEnableComputer(): bool
|
||||
{
|
||||
if (self::$enable_computer_cache !== null) {
|
||||
return self::$enable_computer_cache;
|
||||
}
|
||||
|
||||
global $DB;
|
||||
|
||||
$table = static::getTable();
|
||||
|
||||
if (!$DB->tableExists($table)) {
|
||||
self::$enable_computer_cache = true;
|
||||
|
||||
return self::$enable_computer_cache;
|
||||
}
|
||||
|
||||
try {
|
||||
$iterator = $DB->request([
|
||||
'FROM' => $table,
|
||||
'WHERE' => ['name' => self::CONFIG_ENABLE_COMPUTER],
|
||||
'LIMIT' => 1,
|
||||
]);
|
||||
$row = $iterator->current();
|
||||
self::$enable_computer_cache = $row === false || (string) ($row['value'] ?? '') === '1';
|
||||
} catch (\Throwable) {
|
||||
self::$enable_computer_cache = true;
|
||||
}
|
||||
|
||||
return self::$enable_computer_cache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether itemtype is enabled for UrBackup.
|
||||
*
|
||||
@@ -80,7 +121,7 @@ class Config extends CommonDBTM
|
||||
}
|
||||
|
||||
if ($itemtype === 'Computer') {
|
||||
return true;
|
||||
return self::getEnableComputer();
|
||||
}
|
||||
|
||||
// All Asset subclasses are enabled by default.
|
||||
@@ -101,7 +142,11 @@ class Config extends CommonDBTM
|
||||
*/
|
||||
public static function getEnabledItemtypes(): array
|
||||
{
|
||||
$itemtypes = ['Computer'];
|
||||
$itemtypes = [];
|
||||
|
||||
if (self::getEnableComputer()) {
|
||||
$itemtypes[] = 'Computer';
|
||||
}
|
||||
|
||||
if (class_exists(AssetDefinitionManager::class)) {
|
||||
$manager = AssetDefinitionManager::getInstance();
|
||||
@@ -116,6 +161,42 @@ class Config extends CommonDBTM
|
||||
return $itemtypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Asset Definitions that have the UrBackup capacity enabled.
|
||||
*
|
||||
* @return array<int, \Glpi\Asset\AssetDefinition>
|
||||
*/
|
||||
public static function getEnabledAssetDefinitions(): array
|
||||
{
|
||||
if (!class_exists(AssetDefinitionManager::class)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$manager = AssetDefinitionManager::getInstance();
|
||||
$capacities = $manager->getAvailableCapacities();
|
||||
$urbackup_capacity = $capacities[UrBackupCapacity::class] ?? null;
|
||||
|
||||
$definitions = [];
|
||||
foreach ($manager->getDefinitions() as $definition) {
|
||||
if ($urbackup_capacity !== null) {
|
||||
if ($definition->hasCapacityEnabled($urbackup_capacity)) {
|
||||
$definitions[] = $definition;
|
||||
}
|
||||
} else {
|
||||
// Fallback: decode the raw capacities JSON.
|
||||
$decoded = json_decode((string) ($definition->fields['capacities'] ?? '[]'), true);
|
||||
if (
|
||||
is_array($decoded)
|
||||
&& in_array(UrBackupCapacity::class, array_column($decoded, 'name'), true)
|
||||
) {
|
||||
$definitions[] = $definition;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $definitions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show config form.
|
||||
*
|
||||
@@ -128,38 +209,93 @@ class Config extends CommonDBTM
|
||||
{
|
||||
Session::checkRight('config', UPDATE);
|
||||
|
||||
$enable_computer = self::getEnableComputer();
|
||||
|
||||
echo "<div class='center'>";
|
||||
echo "<form method='post' action='" . static::getFormURL() . "'>";
|
||||
|
||||
echo "<table class='tab_cadre_fixe'>";
|
||||
echo "<tr><th colspan='2'>" . htmlspecialchars(__('UrBackup configuration', 'urbackup')) . "</th></tr>";
|
||||
|
||||
echo "<tr class='tab_bg_1'>";
|
||||
echo "<td><strong>" . htmlspecialchars(__('Computer', 'urbackup')) . "</strong></td>";
|
||||
echo "<td><span class='badge bg-success'>" . htmlspecialchars(__('Always enabled', 'urbackup')) . "</span></td>";
|
||||
echo "<td>";
|
||||
echo "<strong>" . htmlspecialchars(__('Enable UrBackup on Computer', 'urbackup')) . "</strong><br>";
|
||||
echo "<span class='text-muted'>" . htmlspecialchars(
|
||||
__(
|
||||
'Show the UrBackup tab on Computer items. When disabled, Computer is ignored everywhere (tabs, massive actions, links).',
|
||||
'urbackup'
|
||||
)
|
||||
) . "</span>";
|
||||
echo "</td>";
|
||||
echo "<td>";
|
||||
Dropdown::showYesNo(self::CONFIG_ENABLE_COMPUTER, (int) $enable_computer);
|
||||
echo "</td>";
|
||||
echo "</tr>";
|
||||
|
||||
echo "<tr class='tab_bg_2'>";
|
||||
echo "<td class='center' colspan='2'>";
|
||||
echo Html::hidden('_glpi_csrf_token', ['value' => Session::getNewCSRFToken()]);
|
||||
echo Html::submit(__('Save'), ['name' => 'update', 'class' => 'btn btn-primary']);
|
||||
echo "</td>";
|
||||
echo "</tr>";
|
||||
|
||||
echo "</table>";
|
||||
echo "</form>";
|
||||
|
||||
echo "<br>";
|
||||
echo "<table class='tab_cadre_fixe'>";
|
||||
echo "<tr><th colspan='3'>";
|
||||
echo htmlspecialchars(__('Custom assets with "Urbackup" capacity enabled', 'urbackup'));
|
||||
echo "</th></tr>";
|
||||
|
||||
$definitions = self::getEnabledAssetDefinitions();
|
||||
|
||||
if (count($definitions) === 0) {
|
||||
echo "<tr class='tab_bg_1'>";
|
||||
echo "<td class='center' colspan='3'>";
|
||||
echo "<span class='alert alert-info'>";
|
||||
echo htmlspecialchars(
|
||||
__('No custom asset with the "Urbackup" capacity enabled.', 'urbackup')
|
||||
);
|
||||
echo "</span>";
|
||||
echo "</td>";
|
||||
echo "</tr>";
|
||||
} else {
|
||||
echo "<tr class='tab_bg_1'>";
|
||||
echo "<th>" . htmlspecialchars(__('Name')) . "</th>";
|
||||
echo "<th>" . htmlspecialchars(__('System name')) . "</th>";
|
||||
echo "<th>" . htmlspecialchars(__('Active')) . "</th>";
|
||||
echo "</tr>";
|
||||
|
||||
foreach ($definitions as $definition) {
|
||||
$is_active = (bool) ($definition->fields['is_active'] ?? 0);
|
||||
echo "<tr class='tab_bg_1'>";
|
||||
echo "<td>" . htmlspecialchars((string) ($definition->fields['label'] ?? '')) . "</td>";
|
||||
echo "<td>" . htmlspecialchars((string) ($definition->fields['system_name'] ?? '')) . "</td>";
|
||||
echo "<td>";
|
||||
if ($is_active) {
|
||||
echo "<span class='badge bg-success'>" . htmlspecialchars(__('Yes')) . "</span>";
|
||||
} else {
|
||||
echo "<span class='badge bg-secondary'>" . htmlspecialchars(__('No')) . "</span>";
|
||||
}
|
||||
echo "</td>";
|
||||
echo "</tr>";
|
||||
}
|
||||
}
|
||||
|
||||
echo "</table>";
|
||||
|
||||
echo "<br>";
|
||||
echo "<div class='alert alert-info'>";
|
||||
echo htmlspecialchars(
|
||||
__('For Asset Definition types, enable/disable UrBackup via Config > Asset definitions > Capacities.', 'urbackup')
|
||||
__(
|
||||
'For Asset Definition types, enable/disable UrBackup via Config > Asset definitions > Capacities.',
|
||||
'urbackup'
|
||||
)
|
||||
);
|
||||
echo "</div>";
|
||||
echo "</div>";
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save configuration.
|
||||
*
|
||||
* In the new capacity system, asset types are managed via the native Capacities UI.
|
||||
* This method is kept for backward compatibility but does nothing.
|
||||
*
|
||||
* @param array<string, mixed> $input Input data
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function saveConfiguration(array $input): void
|
||||
{
|
||||
Session::checkRight('config', UPDATE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user