Update ok-cablereport-v3.php
[racktables-contribs] / ok-cablereport-v3.php
1 <?php
2
3 ///////////////////////////////////////////////////////////
4 // Opin Kerfi: CableReport
5 // Version: 3.1
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.
10 // Tested on Racktables version 0.20.5 and 0.20.7.
11 //
12 // Author: Ingimar Robertsson <ingimar@ok.is>
13 //
14 // Installation:
15 // Copy ok-cablereport-v31.php into the Racktables plugins folder.
16 //
17 // Version History:
18 // 3.1 - Replaced SQL query with Racktables API calls and added links to devices and ports
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';
27 $displaylinks = 1; // 1 = Display HTML links for devices and ports
28
29 ///////////////////////////////////////////////////////////
30 $tabhandler['reports']['cablereportv31'] = 'CableReportV31'; // register a report rendering function
31 $tab['reports']['cablereportv31'] = $tabname; // title of the report tab
32
33 function CableReportV31()
34 {
35 global $tableheader , $displaylinks;
36
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>';
41
42 // Local jQuery and DataTables files from DataTables-1.10.0 distribution zip:
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>';
46
47 echo '<script>
48 $(document).ready(function() {
49 $("#cablereport").dataTable({
50 "bPaginate": "true",
51 "bLengthChange": "false",
52 "sPaginationType": "full_numbers",
53 "aaSorting": [[ 0, "desc" ]],
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 }
66 });
67 });
68 </script>';
69
70 echo "\n";
71 echo '<div class=portlet>';
72 echo '<h2>' . $tableheader . '</h2>';
73 echo "\n";
74 echo '<table id="cablereport" class="display">';
75 echo "\n";
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>';
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'];
93
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 ) {
126 echo '<tr>';
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";
144 }
145
146 echo '</tbody></table><br/><br/>';
147 echo 'ok-cablereport version 3.1';
148 echo '</div>';
149 }
150
151 ?>