r3887 copy most recent commits from maintenance into trunk
[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{
99bcce26 37 $qparams = array ($port_id, $port_id);
026a79ee 38 $query = "SELECT id, object_id, name FROM Port WHERE " .
99bcce26
DO
39 "id <> ? " .
40 "AND type IN (SELECT type2 FROM PortCompat WHERE type1 = (SELECT type FROM Port WHERE id = ?)) " .
cd3775e9
DO
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))
99bcce26 45 {
cd3775e9 46 $query .= 'AND object_id IN (SELECT DISTINCT object_id FROM RackSpace WHERE rack_id IN (' .
ec523868 47 questionMarks (count ($only_racks)) . '))';
99bcce26
DO
48 $qparams = array_merge ($qparams, $only_racks);
49 }
67f10c63 50 $query .= ' ORDER BY object_id, name';
99bcce26 51 $result = usePreparedSelectBlade ($query, $qparams);
026a79ee
DO
52 // avoid nested queries
53 $rows = $result->fetchAll (PDO::FETCH_ASSOC);
54 unset ($result);
cd3775e9 55 $ret = array();
026a79ee 56 foreach (array_keys ($rows) as $rkey)
cd3775e9 57 {
026a79ee
DO
58 $object = spotEntity ('object', $rows[$rkey]['object_id']);
59 $ret[$rows[$rkey]['id']] = $object['dname'] . ' ' . $rows[$rkey]['name'];
60 unset ($rows[$rkey]);
cd3775e9 61 }
24089ff9 62 natsort($ret);
cd3775e9
DO
63 return $ret;
64}
65
9a15394d
DO
66function sortObjectAddressesAndNames ($a, $b)
67{
68 $objname_cmp = sortTokenize($a['object_name'], $b['object_name']);
69 if ($objname_cmp == 0)
70 {
71 $name_a = (isset ($a['port_name'])) ? $a['port_name'] : '';
72 $name_b = (isset ($b['port_name'])) ? $b['port_name'] : '';
73 $objname_cmp = sortTokenize($name_a, $name_b);
74 if ($objname_cmp == 0)
75 sortTokenize($a['ip'], $b['ip']);
76 return $objname_cmp;
77 }
78 return $objname_cmp;
79}
80
878512c6 81header ('Content-Type: text/html; charset=UTF-8');
38ee0db8 82?>
39106006
DO
83<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
84<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" style="height: 100%;">
85<head>
b325120a 86<?php
4dc8e857 87 echo "<title>RackTables pop-up</title>\n";
d6adad3e 88 printPageHeaders();
39106006 89 echo '</head><body style="height: 100%;">';
0cc24e9a 90 assertStringArg ('helper');
39106006
DO
91 switch ($_REQUEST['helper'])
92 {
93 case 'portlist':
026a79ee
DO
94 $pageno = 'depot';
95 $tabno = 'default';
4dc8e857
DO
96 fixContext();
97 if (!permitted())
4dc8e857 98 renderAccessDenied();
0cc24e9a 99 assertUIntArg ('port');
0cc24e9a 100 assertStringArg ('in_rack');
cd3775e9 101 $localchoice = $_REQUEST['in_rack'] == 'y';
026a79ee 102 $port_id = $_REQUEST['port'];
cd3775e9 103 echo '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; height: 100%; text-align: center; margin: 5px;"><h2>';
026a79ee
DO
104 echo $localchoice ?
105 ('Nearest spare ports (<a href="popup.php?helper=portlist&port=' . $port_id . '&in_rack=n">show all</a>)') :
106 ('All spare ports (<a href="popup.php?helper=portlist&port=' . $port_id . '&in_rack=y">show nearest</a>)');
8cc289ab 107 echo '</h2><form action="javascript:;" id="portform">';
cd3775e9 108 $only_racks = array();
d0dadd80
DO
109 global $sic;
110 $port_info = getPortInfo ($sic['port']);
026a79ee 111 if ($_REQUEST['in_rack'] == 'y' and $port_info['object_id'])
cd3775e9 112 {
026a79ee
DO
113 $object = spotEntity ('object', $port_info['object_id']);
114 if ($object['rack_id'])
115 $only_racks = getProximateRacks ($object['rack_id'], getConfigVar ('PROXIMITY_RANGE'));
cd3775e9
DO
116 }
117 $spare_ports = findSparePorts ($port_id, $only_racks);
8cc289ab 118
8c224954 119 printSelect ($spare_ports, array ('name' => 'ports', 'size' => getConfigVar ('MAXSELSIZE')));
cd3775e9
DO
120 echo '<br><br>';
121 echo "<input type='submit' value='Link' onclick='".
39106006 122 "if (getElementById(\"ports\").value != \"\") {".
026a79ee 123 " 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
124 " window.close();}'>";
125 echo '</form></div>';
126 break;
127 case 'inet4list':
4dc8e857
DO
128 $pageno = 'ipv4space';
129 $tabno = 'default';
130 fixContext();
131 if (!permitted())
4dc8e857 132 renderAccessDenied();
39106006 133 echo '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; height: 100%; text-align: center; margin: 5px;">';
e8dd0422 134 echo '<h2>Choose a port:</h2><br><br>';
39106006
DO
135 echo '<form action="javascript:;">';
136 echo '<input type=hidden id=ip>';
137 echo '<select size=' . getConfigVar ('MAXSELSIZE') . ' id=addresses>';
23cdc7e9
DO
138 $addresses = getAllIPv4Allocations();
139 usort ($addresses, 'sortObjectAddressesAndNames');
140 foreach ($addresses as $address)
141 echo "<option value='${address['ip']}' onclick='getElementById(\"ip\").value=\"${address['ip']}\";'>" .
142 "${address['object_name']} ${address['name']} ${address['ip']}</option>\n";
39106006
DO
143 echo '</select><br><br>';
144 echo "<input type=submit value='Proceed' onclick='".
145 "if (getElementById(\"ip\")!=\"\") {".
146 " opener.document.getElementById(\"remoteip\").value=getElementById(\"ip\").value;".
147 " window.close();}'>";
148 echo '</form></div>';
149 break;
150 default:
d6b0c986 151 throw new InvalidRequestArgException ('helper', $_REQUEST['helper']);
39106006 152 }
e673ee24 153?>
e673ee24
DO
154</body>
155</html>
0cc24e9a
DY
156<?php
157ob_end_flush();
158}
159catch (Exception $e)
160{
161 ob_end_clean();
162 printException($e);
163}
164?>