Files
urbackup/src/Config.php
T
2026-08-07 14:27:33 +02:00

302 lines
9.0 KiB
PHP

<?php
declare(strict_types=1);
/**
* -------------------------------------------------------------------------
* UrBackup plugin for GLPI
* -------------------------------------------------------------------------
*/
namespace GlpiPlugin\Urbackup;
use CommonDBTM;
use Dropdown;
use Glpi\Asset\Asset;
use Glpi\Asset\AssetDefinitionManager;
use GlpiPlugin\Urbackup\Capacity\UrBackupCapacity;
use Html;
use Session;
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.
*
* @param int $nb Number
*
* @return string
*/
public static function getTypeName($nb = 0): string
{
return __('UrBackup configuration', 'urbackup');
}
/**
* Get table name.
*
* @param string|null $classname Class name
*
* @return string
*/
public static function getTable($classname = null): string
{
return 'glpi_plugin_urbackup_configs';
}
/**
* Get assettypes table name.
*
* @return string
*/
public static function getAssetTypesTable(): string
{
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.
*
* @param string $itemtype Itemtype
*
* @return bool
*/
public static function isItemtypeEnabled(string $itemtype): bool
{
if ($itemtype === '') {
return false;
}
if ($itemtype === 'Computer') {
return self::getEnableComputer();
}
// All Asset subclasses are enabled by default.
// The UrBackupCapacity is still registered for cleanup (onCapacityDisabled)
// and usage tracking in the Capacities UI, but visibility is always on
// so the tab appears out of the box on any Asset Definition.
if (is_a($itemtype, Asset::class, true)) {
return true;
}
return false;
}
/**
* Get enabled itemtypes.
*
* @return array<int, string>
*/
public static function getEnabledItemtypes(): array
{
$itemtypes = [];
if (self::getEnableComputer()) {
$itemtypes[] = 'Computer';
}
if (class_exists(AssetDefinitionManager::class)) {
$manager = AssetDefinitionManager::getInstance();
foreach ($manager->getDefinitions() as $definition) {
$classname = $definition->getAssetClassName();
if (!in_array($classname, $itemtypes, true)) {
$itemtypes[] = $classname;
}
}
}
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.
*
* @param int $ID ID
* @param array $options Options
*
* @return bool
*/
public function showForm($ID, array $options = []): bool
{
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>";
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'
)
);
echo "</div>";
echo "</div>";
return true;
}
}