r3769 listCells(): switch to prepared query
[racktables] / popup.php
CommitLineData
38ee0db8 1<?php
0cc24e9a
DY
2ob_start();
3try {
4
f77323f1 5 require 'inc/interface.php';
38ee0db8 6 require 'inc/init.php';
cd3775e9
DO
7
8// Return a list of rack IDs, which are P or less positions
9// far from the given rack in its row.
10function 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
35function findSparePorts ($port_id, $only_racks = array())
36{
026a79ee 37 $query = "SELECT id, object_id, name FROM Port WHERE " .
cd3775e9
DO
38 "id <> ${port_id} " .
39 "AND type IN (SELECT type2 FROM PortCompat WHERE type1 = (SELECT type FROM Port WHERE id = ${port_id})) " .
40 "AND reservation_comment IS NULL " .
41 "AND id NOT IN (SELECT porta FROM Link) " .
42 "AND id NOT IN (SELECT portb FROM Link) ";
43 if (count ($only_racks))
44 $query .= 'AND object_id IN (SELECT DISTINCT object_id FROM RackSpace WHERE rack_id IN (' .
45 implode (', ', $only_racks) . '))';
67f10c63 46 $query .= ' ORDER BY object_id, name';
8a5a4555 47 $result = useSelectBlade ($query);
026a79ee
DO
48 // avoid nested queries
49 $rows = $result->fetchAll (PDO::FETCH_ASSOC);
50 unset ($result);
cd3775e9 51 $ret = array();
026a79ee 52 foreach (array_keys ($rows) as $rkey)
cd3775e9 53 {
026a79ee
DO
54 $object = spotEntity ('object', $rows[$rkey]['object_id']);
55 $ret[$rows[$rkey]['id']] = $object['dname'] . ' ' . $rows[$rkey]['name'];
56 unset ($rows[$rkey]);
cd3775e9
DO
57 }
58 return $ret;
59}
60
878512c6 61header ('Content-Type: text/html; charset=UTF-8');
38ee0db8 62?>
39106006
DO
63<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
64<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" style="height: 100%;">
65<head>
b325120a 66<?php
4dc8e857 67 echo "<title>RackTables pop-up</title>\n";
d6adad3e 68 printPageHeaders();
39106006 69 echo '</head><body style="height: 100%;">';
0cc24e9a 70 assertStringArg ('helper');
39106006
DO
71 switch ($_REQUEST['helper'])
72 {
73 case 'portlist':
026a79ee
DO
74 $pageno = 'depot';
75 $tabno = 'default';
4dc8e857
DO
76 fixContext();
77 if (!permitted())
4dc8e857 78 renderAccessDenied();
0cc24e9a 79 assertUIntArg ('port');
0cc24e9a 80 assertStringArg ('in_rack');
cd3775e9 81 $localchoice = $_REQUEST['in_rack'] == 'y';
026a79ee 82 $port_id = $_REQUEST['port'];
cd3775e9 83 echo '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; height: 100%; text-align: center; margin: 5px;"><h2>';
026a79ee
DO
84 echo $localchoice ?
85 ('Nearest spare ports (<a href="popup.php?helper=portlist&port=' . $port_id . '&in_rack=n">show all</a>)') :
86 ('All spare ports (<a href="popup.php?helper=portlist&port=' . $port_id . '&in_rack=y">show nearest</a>)');
8cc289ab 87 echo '</h2><form action="javascript:;" id="portform">';
cd3775e9 88 $only_racks = array();
d0dadd80
DO
89 global $sic;
90 $port_info = getPortInfo ($sic['port']);
026a79ee 91 if ($_REQUEST['in_rack'] == 'y' and $port_info['object_id'])
cd3775e9 92 {
026a79ee
DO
93 $object = spotEntity ('object', $port_info['object_id']);
94 if ($object['rack_id'])
95 $only_racks = getProximateRacks ($object['rack_id'], getConfigVar ('PROXIMITY_RANGE'));
cd3775e9
DO
96 }
97 $spare_ports = findSparePorts ($port_id, $only_racks);
8cc289ab 98
8c224954 99 printSelect ($spare_ports, array ('name' => 'ports', 'size' => getConfigVar ('MAXSELSIZE')));
cd3775e9
DO
100 echo '<br><br>';
101 echo "<input type='submit' value='Link' onclick='".
39106006 102 "if (getElementById(\"ports\").value != \"\") {".
026a79ee 103 " 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; ".
39106006
DO
104 " window.close();}'>";
105 echo '</form></div>';
106 break;
107 case 'inet4list':
4dc8e857
DO
108 $pageno = 'ipv4space';
109 $tabno = 'default';
110 fixContext();
111 if (!permitted())
4dc8e857 112 renderAccessDenied();
39106006 113 echo '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; height: 100%; text-align: center; margin: 5px;">';
e8dd0422 114 echo '<h2>Choose a port:</h2><br><br>';
39106006
DO
115 echo '<form action="javascript:;">';
116 echo '<input type=hidden id=ip>';
117 echo '<select size=' . getConfigVar ('MAXSELSIZE') . ' id=addresses>';
23cdc7e9
DO
118 $addresses = getAllIPv4Allocations();
119 usort ($addresses, 'sortObjectAddressesAndNames');
120 foreach ($addresses as $address)
121 echo "<option value='${address['ip']}' onclick='getElementById(\"ip\").value=\"${address['ip']}\";'>" .
122 "${address['object_name']} ${address['name']} ${address['ip']}</option>\n";
39106006
DO
123 echo '</select><br><br>';
124 echo "<input type=submit value='Proceed' onclick='".
125 "if (getElementById(\"ip\")!=\"\") {".
126 " opener.document.getElementById(\"remoteip\").value=getElementById(\"ip\").value;".
127 " window.close();}'>";
128 echo '</form></div>';
129 break;
130 default:
d6b0c986 131 throw new InvalidRequestArgException ('helper', $_REQUEST['helper']);
39106006 132 }
e673ee24 133?>
e673ee24
DO
134</body>
135</html>
0cc24e9a
DY
136<?php
137ob_end_flush();
138}
139catch (Exception $e)
140{
141 ob_end_clean();
142 printException($e);
143}
144?>