diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index 3e289d341..9de893701 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -260,6 +260,10 @@ function get_device() if (is_numeric($host_id)) { $device = array_merge($device, array('parent_id' => $host_id)); } + $parents = get_device_parents($device_id); + if (is_array($parents)) { + $device['dependancy'] = $parents; + } api_success(array($device), 'devices'); } @@ -300,17 +304,14 @@ function list_devices() } elseif ($type == 'mac') { $join .= " LEFT JOIN `ports` AS p ON d.`device_id` = p.`device_id` LEFT JOIN `ipv4_mac` AS m ON p.`port_id` = m.`port_id` "; $sql = "m.`mac_address`=?"; - $select .= ",p.* "; $param[] = $query; } elseif ($type == 'ipv4') { $join .= " LEFT JOIN `ports` AS p ON d.`device_id` = p.`device_id` LEFT JOIN `ipv4_addresses` AS a ON p.`port_id` = a.`port_id` "; $sql = "a.`ipv4_address`=?"; - $select .= ",p.* "; $param[] = $query; } elseif ($type == 'ipv6') { $join .= " LEFT JOIN `ports` AS p ON d.`device_id` = p.`device_id` LEFT JOIN `ipv6_addresses` AS a ON p.`port_id` = a.`port_id` "; $sql = "a.`ipv6_address`=? OR a.`ipv6_compressed`=?"; - $select .= ",p.* "; $param = array($query, $query); } else { $sql = '1'; @@ -329,6 +330,17 @@ function list_devices() if (is_numeric($host_id)) { $device['parent_id'] = $host_id; } + if ($device['dependency_parent_id']) { + $tmp_parent_id = explode(',', $device['dependency_parent_id']); + $tmp_parent_hostname = explode(',', $device['dependency_parent_hostname']); + foreach ($tmp_parent_id as $index => $parent_id) { + $device['dependancy'][] = array( + 'parent_hostname' => $tmp_parent_hostname[$index], + 'parent_device_id' => $parent_id, + ); + } + } + unset($device['dependency_parent_id'], $device['dependency_parent_hostname']); $devices[] = $device; } diff --git a/includes/common.php b/includes/common.php index 6b56aaf8c..47bbbbc0e 100644 --- a/includes/common.php +++ b/includes/common.php @@ -1862,3 +1862,11 @@ function array_by_column($array, $column) { return array_combine(array_column($array, $column), $array); } + +/** + * @param $device_id + * @return array + */ +function get_device_parents($device_id) { + return dbFetchRows("SELECT `hostname`, `parent_device_id` FROM `device_relationships` LEFT JOIN `devices` ON `device_relationships`.`parent_device_id`=`devices`.`device_id` WHERE `child_device_id` = ?", array($device_id)); +} \ No newline at end of file