$tabhandler['reports']['custom'] = 'renderCustomReport'; // register a report rendering function $tab['reports']['custom'] = 'Custom'; // The title of the report tab require_once "reportExtensionLib.php"; function renderCustomReport() { # Get object list $phys_typelist = readChapter (CHAP_OBJTYPE, 'o'); $attibutes = getAttrMap(); $aTagList = getTagList(); if ( ( $_SERVER['REQUEST_METHOD'] == 'POST' ) && ( isset( $_POST['csv'] ) ) ) { header('Content-type: text/csv'); header('Content-Disposition: attachment; filename=export_'.date("Ymdhis").'.csv'); header('Pragma: no-cache'); header('Expires: 0'); $outstream = fopen("php://output", "w"); $aResult = getResult($_POST); // Get Result $_POST['name'] = validateColums($_POST); // Fix empty colums $csvDelimiter = (isset( $_POST['csvDelimiter'] )) ? $_POST['csvDelimiter'] : ','; /* Create Header */ $aCSVRow = array(); if ( isset( $_POST['sName'] ) && $_POST['sName'] ) array_push($aCSVRow, "Name"); if ( isset( $_POST['label'] ) ) array_push($aCSVRow, "Label"); if ( isset( $_POST['type'] ) ) array_push($aCSVRow, "Type"); if ( isset( $_POST['asset_no'] ) ) array_push($aCSVRow, "Asset Tag"); if ( isset( $_POST['has_problems'] ) ) array_push($aCSVRow, "Has Problems"); if ( isset( $_POST['comment'] ) ) array_push($aCSVRow, "Comment"); if ( isset( $_POST['runs8021Q'] ) ) array_push($aCSVRow, "Runs 8021Q"); if ( isset( $_POST['location'] ) ) array_push($aCSVRow, "Location"); if ( isset( $_POST['MACs'] ) ) array_push($aCSVRow, "MACs"); if ( isset( $_POST['IPs'] ) ) array_push($aCSVRow, "IPs"); if ( isset( $_POST['attributeIDs'] ) ) { foreach ( $_POST['attributeIDs'] as $attributeID ) array_push($aCSVRow, $attibutes[$attributeID]['name']); } if ( isset( $_POST['Tags'] ) ) array_push($aCSVRow, "Tags"); if ( isset( $_POST['Ports'] ) ) array_push($aCSVRow, "Ports"); if ( isset( $_POST['Containers'] ) ) array_push($aCSVRow, "Containers"); if ( isset( $_POST['Childs'] ) ) array_push($aCSVRow, "Child objects"); fputcsv( $outstream, $aCSVRow, $csvDelimiter ); /* Create data rows */ foreach ( $aResult as $Result ) { $aCSVRow = array(); if ( isset( $_POST['sName'] ) ) array_push($aCSVRow, $Result['name']); if ( isset( $_POST['label'] ) ) array_push($aCSVRow, $Result['label']); if ( isset( $_POST['type'] ) ) array_push($aCSVRow, $phys_typelist[$Result['objtype_id']]); if ( isset( $_POST['asset_no'] ) ) array_push($aCSVRow, $Result['asset_no']); if ( isset( $_POST['has_problems'] ) ) array_push($aCSVRow, $Result['has_problems']); if ( isset( $_POST['comment'] ) ) array_push($aCSVRow, str_replace('"',"'",$Result['comment'])); if ( isset( $_POST['runs8021Q'] ) ) array_push($aCSVRow, $Result['runs8021Q']); if ( isset( $_POST['location'] ) ) array_push($aCSVRow, preg_replace('/]*>(.*)<\/a>/iU', '$1', getLocation($Result))); if ( isset( $_POST['MACs'] ) ) { $sTemp = ''; foreach ( getObjectPortsAndLinks($Result['id']) as $portNumber => $aPortDetails ) { if ( trim($aPortDetails['l2address']) != '') $sTemp .= $aPortDetails['l2address'].' '; } array_push($aCSVRow, $sTemp); } if ( isset( $_POST['IPs'] ) ) { $sTemp = ''; foreach ( getObjectIPv4AllocationList($Result['id']) as $key => $aDetails ) { if ( function_exists('ip4_format') ) $key = ip4_format($key); if ( trim($key) != '') $sTemp .= $key.' '; } foreach ( getObjectIPv6AllocationList($Result['id']) as $key => $aDetails ) { if ( function_exists('ip6_format') ) $key = ip6_format($key); else $key = new IPv6Address($key); if ( trim($key) != '') $sTemp .= $key.' '; } array_push($aCSVRow, $sTemp); } if ( isset( $_POST['attributeIDs'] ) ) { $attributes = getAttrValues ($Result['id']); foreach ( $_POST['attributeIDs'] as $attributeID ) { if ( isset( $attributes[$attributeID]['a_value'] ) ) array_push($aCSVRow,$attributes[$attributeID]['a_value']); elseif ( ($attributes[$attributeID]['value'] != '') && ( $attributes[$attributeID]['type'] == 'date' ) ) array_push($aCSVRow,date("Y-m-d",$attributes[$attributeID]['value'])); else array_push($aCSVRow,''); } } if ( isset( $_POST['Tags'] ) ) { $sTemp = ''; foreach ( $Result['tags'] as $aTag ) { $sTemp .= $aTag['tag'].' '; } if ( count($Result['itags']) > 0 ) { $sTemp .= '('; foreach ( $Result['itags'] as $aTag ) { $sTemp .= $aTag['tag'].' '; } $sTemp .= ')'; } array_push($aCSVRow, $sTemp); } if ( isset( $_POST['Ports'] ) ) { $sTemp = ''; foreach ( $Result['portsLinks'] as $port ) { $sTemp .= $port['name'].': '.$port['remote_object_name']; if ( trim($port['cableid']) != '') $sTemp .= ' Cable ID: '.$port['cableid']; $sTemp .= ' '; } $sTemp = trim($sTemp); array_push($aCSVRow, $sTemp); } if ( isset( $_POST['Containers'] ) ) { $sTemp = ''; foreach ( getObjectContainerList($Result['id']) as $key => $aDetails ) { $sTemp .= trim($aDetails['container_name']).' '; } $sTemp = trim($sTemp); array_push($aCSVRow, $sTemp); } if ( isset( $_POST['Childs'] ) ) { $sTemp = ''; foreach ( getObjectChildObjectList($Result['id']) as $key => $aDetails ) { $sTemp .= trim($aDetails['object_name']).' '; } $sTemp = trim($sTemp); array_push($aCSVRow, $sTemp); } fputcsv( $outstream, $aCSVRow, $csvDelimiter ); } fclose($outstream); exit(0); # Exit normally after send CSV to browser } echo '

Custom report