From d69c465c922c53e22f004af085dea8898e625bc2 Mon Sep 17 00:00:00 2001 From: mariano Date: Wed, 20 May 2026 14:17:45 +0200 Subject: [PATCH] modifica lista missing computer --- src/Server.php | 121 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 115 insertions(+), 6 deletions(-) diff --git a/src/Server.php b/src/Server.php index c38392e..b8fd0c6 100644 --- a/src/Server.php +++ b/src/Server.php @@ -12,9 +12,12 @@ use CommonDBTM; use CommonGLPI; use Dropdown; use Entity; +use Group; use Html; use Location; use Session; +use State; +use User; class Server extends CommonDBTM { @@ -1062,6 +1065,12 @@ class Server extends CommonDBTM } } + $cacheEntity = []; + $cacheLocation = []; + $cacheState = []; + $cacheUser = []; + $cacheGroup = []; + $itemtypes = Config::getEnabledItemtypes(); $missingAssets = []; @@ -1100,6 +1109,8 @@ class Server extends CommonDBTM } $key = $itemtype . ':' . $assetRow['id']; + $assetId = (int) $assetRow['id']; + if (isset($linkedAssetKeys[$key])) { continue; } @@ -1107,10 +1118,25 @@ class Server extends CommonDBTM continue; } + $entityName = self::getCachedName('Entity', (int) ($assetRow['entities_id'] ?? 0), $cacheEntity); + $locationName = self::getCachedLocationName($assetLocationId, $cacheLocation); + $stateName = self::getCachedName('State', (int) ($assetRow['states_id'] ?? 0), $cacheState); + $userName = self::getCachedName('User', (int) ($assetRow['users_id'] ?? 0), $cacheUser); + $groupName = self::getCachedName('Group', (int) ($assetRow['groups_id'] ?? 0), $cacheGroup); + + $ip = self::getAssetIp($itemtype, $assetId); + $missingAssets[] = [ - 'itemtype' => $itemtype, - 'items_id' => (int) $assetRow['id'], - 'name' => $name, + 'itemtype' => $itemtype, + 'items_id' => $assetId, + 'name' => $name, + 'entity' => $entityName, + 'location' => $locationName, + 'otherserial' => (string) ($assetRow['otherserial'] ?? ''), + 'ip' => $ip, + 'state' => $stateName, + 'user' => $userName, + 'group' => $groupName, ]; } } @@ -1126,17 +1152,28 @@ class Server extends CommonDBTM echo ''; echo ''; - echo ''; echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; echo ''; echo ''; echo ''; foreach ($missingAssets as $asset) { - $itemtypeLabel = class_exists($asset['itemtype']) ? $asset['itemtype']::getTypeName(1) : $asset['itemtype']; echo ''; - echo ''; echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; echo ''; echo '
' . htmlspecialchars(__('Asset type')) . '' . htmlspecialchars(__('Name')) . '' . htmlspecialchars(Entity::getTypeName(1)) . '' . htmlspecialchars(Location::getTypeName(1)) . '' . htmlspecialchars(__('Inventory number')) . '' . htmlspecialchars(__('IP address', 'urbackup')) . '' . htmlspecialchars(State::getTypeName(1)) . '' . htmlspecialchars(User::getTypeName(1)) . '' . htmlspecialchars(Group::getTypeName(1)) . '' . htmlspecialchars(__('Actions', 'urbackup')) . '
' . htmlspecialchars($itemtypeLabel) . '' . htmlspecialchars($asset['name']) . '' . htmlspecialchars($asset['entity']) . '' . htmlspecialchars($asset['location']) . '' . htmlspecialchars($asset['otherserial']) . '' . htmlspecialchars($asset['ip']) . '' . htmlspecialchars($asset['state']) . '' . htmlspecialchars($asset['user']) . '' . htmlspecialchars($asset['group']) . ''; echo '
'; echo Html::hidden('_glpi_csrf_token', ['value' => Session::getNewCSRFToken()]); @@ -1154,4 +1191,76 @@ class Server extends CommonDBTM echo '
'; } + + private static function getCachedName(string $classname, int $id, array &$cache): string + { + if ($id <= 0) { + return ''; + } + if (!isset($cache[$id])) { + $obj = new $classname(); + if ($obj->getFromDB($id)) { + $cache[$id] = (string) ($obj->fields['name'] ?? ''); + } else { + $cache[$id] = ''; + } + } + return $cache[$id]; + } + + private static function getCachedLocationName(int $id, array &$cache): string + { + if ($id <= 0) { + return ''; + } + if (!isset($cache[$id])) { + $obj = new Location(); + if ($obj->getFromDB($id)) { + $cache[$id] = (string) ($obj->fields['completename'] ?? $obj->fields['name'] ?? ''); + } else { + $cache[$id] = ''; + } + } + return $cache[$id]; + } + + private static function getAssetIp(string $itemtype, int $items_id): string + { + global $DB; + + $iterator = $DB->request([ + 'SELECT' => ['ipa.name'], + 'FROM' => 'glpi_ipaddresses AS ipa', + 'INNER JOIN' => [ + 'glpi_networknames AS nn' => [ + 'ON' => [ + 'nn' => 'items_id', + 'ipa' => 'id', + ['AND' => ['ipa.itemtype' => 'NetworkName']], + ], + ], + 'glpi_networkports AS np' => [ + 'ON' => [ + 'np' => 'id', + 'nn' => 'items_id', + ['AND' => ['nn.itemtype' => 'NetworkPort']], + ], + ], + ], + 'WHERE' => [ + 'np.itemtype' => $itemtype, + 'np.items_id' => $items_id, + ], + 'LIMIT' => 1, + ]); + + foreach ($iterator as $row) { + $ip = (string) ($row['name'] ?? ''); + if ($ip !== '') { + return $ip; + } + } + + return ''; + } } \ No newline at end of file