getFromDB($items_id)) { return false; } $table = self::getTable(); $date = $_SESSION['glpi_currenttime'] ?? date('Y-m-d H:i:s'); $existing = self::getLinkForAsset($itemtype, $items_id, false); if ($existing !== null) { return $DB->update( $table, [ 'plugin_urbackup_servers_id' => $server_id, 'client_name' => (string) ($item->fields['name'] ?? ''), 'client_ip' => self::extractAssetIp($item), 'is_active' => 1, 'date_mod' => $date, ], [ 'id' => (int) $existing['id'], ] ); } return $DB->insert( $table, [ 'plugin_urbackup_servers_id' => $server_id, 'itemtype' => $itemtype, 'items_id' => $items_id, ] ); } /** * Disconnect asset from server. * * @param string $itemtype Itemtype * @param int $items_id Item ID * * @return bool */ public static function disconnectAsset(string $itemtype, int $items_id): bool { global $DB; if (!Profile::canCurrentUser(UPDATE)) { return false; } $link = self::getLinkForAsset($itemtype, $items_id, true); if ($link === null) { return true; } return $DB->delete( self::getTable(), [ 'id' => (int) $link['id'], ] ); } /** * Get active link for asset. * * @param string $itemtype Itemtype * @param int $items_id Item ID * @param bool $active_only Active only * * @return array|null */ public static function createForAsset( string $itemtype, int $items_id, int $servers_id ): bool { global $DB; $item = getItemForItemtype($itemtype); if (!$item || !$item->getFromDB($items_id)) { return false; } $result = $DB->insert(self::getTable(), [ 'plugin_urbackup_servers_id' => $servers_id, 'itemtype' => $itemtype, 'items_id' => $items_id, ]); return $result; } public static function getAssetName(string $itemtype, int $items_id): string { $item = getItemForItemtype($itemtype); if (!$item || !$item->getFromDB($items_id)) { return ''; } return (string) ($item->fields['name'] ?? ''); } public static function getLinkForAsset( string $itemtype, int $items_id, bool $active_only = true ): ?array { global $DB; $iterator = $DB->request([ 'FROM' => self::getTable(), 'WHERE' => [ 'itemtype' => $itemtype, 'items_id' => $items_id, ], 'LIMIT' => 1, ]); if (count($iterator) === 0) { return null; } return $iterator->current(); } /** * Extract asset IP address. * * @param CommonDBTM $item Item * * @return string */ public static function extractAssetIp(CommonDBTM $item): string { if (!isset($item->fields['ip_address']) || $item->fields['ip_address'] === '') { return ''; } $ip = $item->fields['ip_address']; if (is_array($ip)) { return $ip[0] ?? ''; } return (string) $ip; } /** * Display tab content for item. * * @param CommonGLPI $item Server item * @param int $tabnum Tab number * @param int $withtemplate Template * * @return bool */ public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0): bool { global $DB; if (!$item instanceof Server) { return false; } echo "
"; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; $iterator = $DB->request([ 'FROM' => self::getTable(), 'WHERE' => [ 'plugin_urbackup_servers_id' => (int) $item->fields['id'], 'is_active' => 1, ], 'ORDER' => 'client_name', ]); foreach ($iterator as $row) { $asset_label = (string) $row['client_name']; $itemtype = (string) $row['itemtype']; $items_id = (int) $row['items_id']; if (class_exists($itemtype)) { $asset = new $itemtype(); if ($asset instanceof CommonDBTM && $asset->getFromDB($items_id)) { $asset_label = $asset->getLink(); } } echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
" . htmlspecialchars(__('Linked assets', 'urbackup')) . "
" . htmlspecialchars(__('Asset', 'urbackup')) . "" . htmlspecialchars(__('Type')) . "" . htmlspecialchars(__('IP address', 'urbackup')) . "" . htmlspecialchars(__('Last file backup', 'urbackup')) . "" . htmlspecialchars(__('Last image backup', 'urbackup')) . "
" . $asset_label . "" . htmlspecialchars($itemtype) . "" . htmlspecialchars((string) $row['client_ip']) . "" . htmlspecialchars((string) $row['last_file_backup']) . "" . htmlspecialchars((string) $row['last_image_backup']) . "
"; echo "
"; return true; } }