r3827 sortObjectAddressesAndNames(): move to popup.php, where it belongs
[racktables] / popup.php
1 <?php
2 ob_start();
3 try {
4
5 require 'inc/interface.php';
6 require 'inc/init.php';
7
8 // Return a list of rack IDs, which are P or less positions
9 // far from the given rack in its row.
10 function getProximateRacks ($rack_id, $proximity = 0)
11 {
12 $rack = spotEntity ('rack', $rack_id);
13 $rackList = listCells ('rack', $rack['row_id']);
14 doubleLink ($rackList);
15 $ret = array ($rack_id);
16 $todo = $proximity;
17 $cur_item = $rackList[$rack_id];
18 while ($todo and array_key_exists ('prev_key', $cur_item))
19 {
20 $cur_item = $rackList[$cur_item['prev_key']];
21 $ret[] = $cur_item['id'];
22 $todo--;
23 }
24 $todo = $proximity;
25 $cur_item = $rackList[$rack_id];
26 while ($todo and array_key_exists ('next_key', $cur_item))
27 {
28 $cur_item = $rackList[$cur_item['next_key']];
29 $ret[] = $cur_item['id'];
30 $todo--;
31 }
32 return $ret;
33 }
34
35 function findSparePorts ($port_id, $only_racks = array())
36 {
37 $qparams = array ($port_id, $port_id);
38 $query = "SELECT id, object_id, name FROM Port WHERE " .
39 "id <> ? " .
40 "AND type IN (SELECT type2 FROM PortCompat WHERE type1 = (SELECT type FROM Port WHERE id = ?)) " .
41 "AND reservation_comment IS NULL " .
42 "AND id NOT IN (SELECT porta FROM Link) " .
43 "AND id NOT IN (SELECT portb FROM Link) ";
44 if (count ($only_racks))
45 {
46 $query .= 'AND object_id IN (SELECT DISTINCT object_id FROM RackSpace WHERE rack_id IN (' .
47 implode (', ', array_fill (0, count ($only_racks), '?')) . '))';
48 $qparams = array_merge ($qparams, $only_racks);
49 }
50 $query .= ' ORDER BY object_id, name';
51 $result = usePreparedSelectBlade ($query, $qparams);
52 // avoid nested queries
53 $rows = $result->fetchAll (PDO::FETCH_ASSOC);
54 unset ($result);
55 $ret = array();
56 foreach (array_keys ($rows) as $rkey)
57 {
58 $object = spotEntity ('object', $rows[$rkey]['object_id']);
59 $ret[$rows[$rkey]['id']] = $object['dname'] . ' ' . $rows[$rkey]['name'];
60 unset ($rows[$rkey]);
61 }
62 return $ret;
63 }
64
65 function sortObjectAddressesAndNames ($a, $b)
66 {
67 $objname_cmp = sortTokenize($a['object_name'], $b['object_name']);
68 if ($objname_cmp == 0)
69 {
70 $name_a = (isset ($a['port_name'])) ? $a['port_name'] : '';
71 $name_b = (isset ($b['port_name'])) ? $b['port_name'] : '';
72 $objname_cmp = sortTokenize($name_a, $name_b);
73 if ($objname_cmp == 0)
74 sortTokenize($a['ip'], $b['ip']);
75 return $objname_cmp;
76 }
77 return $objname_cmp;
78 }
79
80 header ('Content-Type: text/html; charset=UTF-8');
81 ?>
82 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
83 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" style="height: 100%;">
84 <head>
85 <?php
86 echo "<title>RackTables pop-up</title>\n";
87 printPageHeaders();
88 echo '</head><body style="height: 100%;">';
89 assertStringArg ('helper');
90 switch ($_REQUEST['helper'])
91 {
92 case 'portlist':
93 $pageno = 'depot';
94 $tabno = 'default';
95 fixContext();
96 if (!permitted())
97 renderAccessDenied();
98 assertUIntArg ('port');
99 assertStringArg ('in_rack');
100 $localchoice = $_REQUEST['in_rack'] == 'y';
101 $port_id = $_REQUEST['port'];
102 echo '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; height: 100%; text-align: center; margin: 5px;"><h2>';
103 echo $localchoice ?
104 ('Nearest spare ports (<a href="popup.php?helper=portlist&port=' . $port_id . '&in_rack=n">show all</a>)') :
105 ('All spare ports (<a href="popup.php?helper=portlist&port=' . $port_id . '&in_rack=y">show nearest</a>)');
106 echo '</h2><form action="javascript:;" id="portform">';
107 $only_racks = array();
108 global $sic;
109 $port_info = getPortInfo ($sic['port']);
110 if ($_REQUEST['in_rack'] == 'y' and $port_info['object_id'])
111 {
112 $object = spotEntity ('object', $port_info['object_id']);
113 if ($object['rack_id'])
114 $only_racks = getProximateRacks ($object['rack_id'], getConfigVar ('PROXIMITY_RANGE'));
115 }
116 $spare_ports = findSparePorts ($port_id, $only_racks);
117
118 printSelect ($spare_ports, array ('name' => 'ports', 'size' => getConfigVar ('MAXSELSIZE')));
119 echo '<br><br>';
120 echo "<input type='submit' value='Link' onclick='".
121 "if (getElementById(\"ports\").value != \"\") {".
122 " opener.location=\"process.php?page=object&tab=ports&op=linkPort&object_id=${port_info['object_id']}&port_id=$port_id&remote_port_id=\"+getElementById(\"ports\").value; ".
123 " window.close();}'>";
124 echo '</form></div>';
125 break;
126 case 'inet4list':
127 $pageno = 'ipv4space';
128 $tabno = 'default';
129 fixContext();
130 if (!permitted())
131 renderAccessDenied();
132 echo '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; height: 100%; text-align: center; margin: 5px;">';
133 echo '<h2>Choose a port:</h2><br><br>';
134 echo '<form action="javascript:;">';
135 echo '<input type=hidden id=ip>';
136 echo '<select size=' . getConfigVar ('MAXSELSIZE') . ' id=addresses>';
137 $addresses = getAllIPv4Allocations();
138 usort ($addresses, 'sortObjectAddressesAndNames');
139 foreach ($addresses as $address)
140 echo "<option value='${address['ip']}' onclick='getElementById(\"ip\").value=\"${address['ip']}\";'>" .
141 "${address['object_name']} ${address['name']} ${address['ip']}</option>\n";
142 echo '</select><br><br>';
143 echo "<input type=submit value='Proceed' onclick='".
144 "if (getElementById(\"ip\")!=\"\") {".
145 " opener.document.getElementById(\"remoteip\").value=getElementById(\"ip\").value;".
146 " window.close();}'>";
147 echo '</form></div>';
148 break;
149 default:
150 throw new InvalidRequestArgException ('helper', $_REQUEST['helper']);
151 }
152 ?>
153 </body>
154 </html>
155 <?php
156 ob_end_flush();
157 }
158 catch (Exception $e)
159 {
160 ob_end_clean();
161 printException($e);
162 }
163 ?>