Update ok-cablereport-v3.php
[racktables-contribs] / ok-cablereport-v3.php
CommitLineData
dd8b2edd
IR
1<?php
2
3///////////////////////////////////////////////////////////
4// Opin Kerfi: CableReport
68cfd931 5// Version: 3.1
dd8b2edd
IR
6//
7// Description:
8// Racktables reports plugin for listing of all linked cables in Racktables.
9// Uses jQuery and DataTables for easy sorting and searching.
68cfd931 10// Tested on Racktables version 0.20.5 and 0.20.7.
dd8b2edd
IR
11//
12// Author: Ingimar Robertsson <ingimar@ok.is>
13//
14// Installation:
68cfd931 15// Copy ok-cablereport-v31.php into the Racktables plugins folder.
dd8b2edd
IR
16//
17// Version History:
68cfd931 18// 3.1 - Replaced SQL query with Racktables API calls and added links to devices and ports
dd8b2edd
IR
19// 3.0 - Major cleanup of version 2.0 and republished to racktables-contrib
20// 2.0 - First version using jQuery/DataTables
21// 1.0 - Initial version, static table
22///////////////////////////////////////////////////////////
23
24// Variables:
25$tabname = 'Cable Report';
26$tableheader = 'Cable Report for Racktables';
68cfd931 27$displaylinks = 1; // 1 = Display HTML links for devices and ports
dd8b2edd
IR
28
29///////////////////////////////////////////////////////////
68cfd931
IR
30$tabhandler['reports']['cablereportv31'] = 'CableReportV31'; // register a report rendering function
31$tab['reports']['cablereportv31'] = $tabname; // title of the report tab
dd8b2edd 32
68cfd931 33function CableReportV31()
dd8b2edd 34{
68cfd931 35 global $tableheader , $displaylinks;
dd8b2edd 36
68cfd931
IR
37 // Remote jQuery and DataTables files:
38 echo '<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.0/css/jquery.dataTables.css">';
39 echo '<script type="text/javascript" charset="utf8" src="https://code.jquery.com/jquery-1.10.2.min.js"></script>';
40 echo '<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.0/js/jquery.dataTables.js"></script>';
dd8b2edd
IR
41
42 // Local jQuery and DataTables files from DataTables-1.10.0 distribution zip:
68cfd931
IR
43 //echo '<link rel="stylesheet" type="text/css" href="/rt/extensions/cablereportv3/DataTables-1.10.0/media/css/jquery.dataTables.css">';
44 //echo '<script type="text/javascript" charset="utf8" src="/rt/extensions/cablereportv3/DataTables-1.10.0/media/js/jquery.js"></script>';
45 //echo '<script type="text/javascript" charset="utf8" src="/rt/extensions/cablereportv3/DataTables-1.10.0/media/js/jquery.dataTables.js"></script>';
dd8b2edd
IR
46
47 echo '<script>
48 $(document).ready(function() {
49 $("#cablereport").dataTable({
68cfd931 50 "bPaginate": "true",
dd8b2edd
IR
51 "bLengthChange": "false",
52 "sPaginationType": "full_numbers",
53 "aaSorting": [[ 0, "desc" ]],
68cfd931
IR
54 "iDisplayLength": 20,
55 "stateSave": false,
56 "oLanguage": {
57 "sLengthMenu": \'Display <select>\'+
58 \'<option value="10">10</option>\'+
59 \'<option value="20">20</option>\'+
60 \'<option value="30">30</option>\'+
61 \'<option value="40">40</option>\'+
62 \'<option value="50">50</option>\'+
63 \'<option value="-1">All</option>\'+
64 \'</select> records\'
65 }
dd8b2edd
IR
66 });
67 });
68 </script>';
69
68cfd931 70 echo "\n";
dd8b2edd
IR
71 echo '<div class=portlet>';
72 echo '<h2>' . $tableheader . '</h2>';
68cfd931 73 echo "\n";
dd8b2edd 74 echo '<table id="cablereport" class="display">';
68cfd931 75 echo "\n";
dd8b2edd
IR
76 echo '<thead><tr>';
77 echo '<th>Cable ID</th>';
78 echo '<th>Device 1</th>';
79 echo '<th>Port 1</th>';
80 echo '<th>Type 1</th>';
81 echo '<th>Device 2</th>';
82 echo '<th>Port 2</th>';
83 echo '<th>Type 2</th>';
68cfd931
IR
84 echo '</tr></thead>';
85 echo "\n";
86 echo '<tbody>';
87 echo "\n";
88
89 $allports = fetchPortList('true');
90 $cid = 0;
91 foreach ( $allports as $port ) {
92 $allporttypes[$port['id']] = $port['oif_name'];
dd8b2edd 93
68cfd931
IR
94 if ( $port['linked'] != 1 ) {
95 continue;
96 }
97
98 if ( $done[$port['id']] == 1 ) {
99 continue;
100 } else {
101 $cid++;
102 $cabletable[$cid]['cableid'] = $port['cableid'];
103 if ( $displaylinks == 1 ) {
104 $cabletable[$cid]['device1'] = formatPortLink($port['object_id'],$port['object_name'],NULL,NULL);
105 $cabletable[$cid]['port1'] = formatPortLink($port['object_id'],NULL,$port['id'],$port['name']);
106 } else {
107 $cabletable[$cid]['device1'] = $port['object_name'];
108 $cabletable[$cid]['port1'] = $port['name'];
109 }
110 $cabletable[$cid]['port1id'] = $port['id'];
111 $cabletable[$cid]['type1'] = $port['oif_name'];
112 if ( $displaylinks == 1 ) {
113 $cabletable[$cid]['device2'] = formatPortLink($port['remote_object_id'],$port['remote_object_name'],NULL,NULL);
114 $cabletable[$cid]['port2'] = formatPortLink($port['remote_object_id'],NULL,$port['remote_id'],$port['remote_name']);
115 } else {
116 $cabletable[$cid]['device2'] = $port['remote_object_name'];
117 $cabletable[$cid]['port2'] = $port['remote_name'];
118 }
119 $cabletable[$cid]['port2id'] = $port['remote_id'];
120 $cabletable[$cid]['type2'] = ''; # missing from fetchPortList() add later from $allporttypes being created;
121 $done[$port['remote_id']] = 1;
122 }
123 }
124
125 foreach ( $cabletable as $cable ) {
dd8b2edd 126 echo '<tr>';
68cfd931
IR
127 echo '<td>';
128 echo $cable['cableid'];
129 echo '</td><td>';
130 echo $cable['device1'];
131 echo '</td><td>';
132 echo $cable['port1'];
133 echo '</td><td>';
134 echo $cable['type1'];
135 echo '</td><td>';
136 echo $cable['device2'];
137 echo '</td><td>';
138 echo $cable['port2'];
139 echo '</td><td>';
140 echo $allporttypes[$cable['port2id']];
141 echo '</td>';
142 echo '</tr>';
143 echo "\n";
dd8b2edd
IR
144 }
145
146 echo '</tbody></table><br/><br/>';
68cfd931 147 echo 'ok-cablereport version 3.1';
dd8b2edd
IR
148 echo '</div>';
149}
68cfd931 150
dd8b2edd 151?>