r4178 bugfix: PHP warnings were appearing before rackspace on SLB object page, defaul...
[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
0682218d
AD
66// Return a list of all objects which are possible parents
67// Special case for VMs and VM Virtual Switches
68// - only select Servers with the Hypervisor attribute set to Yes
69function findRackObjectParentCandidates ($object_id)
70{
71 $object = spotEntity ('object', $object_id);
72 $args = array ($object['objtype_id'], $object_id, $object_id);
73
74 $query = "SELECT RO.id, RO.name FROM RackObject RO ";
75 $query .= "LEFT JOIN ObjectParentCompat OPC ON RO.objtype_id = OPC.parent_objtype_id ";
76 $query .= "WHERE OPC.child_objtype_id = ? ";
77 $query .= "AND RO.id != ? ";
78 // exclude existing parents
79 $query .= "AND RO.id NOT IN (SELECT parent_entity_id FROM EntityLink WHERE parent_entity_type = 'object' AND child_entity_type = 'object' AND child_entity_id = ?) ";
80 if ($object['objtype_id'] == 1504 || $object['objtype_id'] == 1507)
81 {
82 array_push($args, $object['objtype_id'], $object_id, $object_id);
83 $query .= "AND OPC.parent_objtype_id != 4 ";
84 $query .= "UNION ";
85 $query .= "SELECT RO.id, RO.name FROM RackObject RO ";
86 $query .= "LEFT JOIN ObjectParentCompat OPC ON RO.objtype_id = OPC.parent_objtype_id ";
87 $query .= "LEFT JOIN AttributeValue AV ON RO.id = AV.object_id ";
88 $query .= "WHERE OPC.child_objtype_id = ? ";
89 $query .= "AND (RO.objtype_id = 4 AND AV.attr_id = 26 AND AV.uint_value = 1501) ";
90 $query .= "AND RO.id != ? ";
91 // exclude existing parents
92 $query .= "AND RO.id NOT IN (SELECT parent_entity_id FROM EntityLink WHERE parent_entity_type = 'object' AND child_entity_type = 'object' AND child_entity_id = ?) ";
93 }
94 $query .= "ORDER BY 2";
95
96 $result = usePreparedSelectBlade ($query, $args);
97 $ret = array();
98 while ($row = $result->fetch (PDO::FETCH_ASSOC))
99 $ret[$row['id']] = $row['name'];
100 return $ret;
101}
102
9a15394d
DO
103function sortObjectAddressesAndNames ($a, $b)
104{
105 $objname_cmp = sortTokenize($a['object_name'], $b['object_name']);
106 if ($objname_cmp == 0)
107 {
108 $name_a = (isset ($a['port_name'])) ? $a['port_name'] : '';
109 $name_b = (isset ($b['port_name'])) ? $b['port_name'] : '';
110 $objname_cmp = sortTokenize($name_a, $name_b);
111 if ($objname_cmp == 0)
112 sortTokenize($a['ip'], $b['ip']);
113 return $objname_cmp;
114 }
115 return $objname_cmp;
116}
117
878512c6 118header ('Content-Type: text/html; charset=UTF-8');
38ee0db8 119?>
39106006
DO
120<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
121<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" style="height: 100%;">
122<head>
b325120a 123<?php
4dc8e857 124 echo "<title>RackTables pop-up</title>\n";
d6adad3e 125 printPageHeaders();
39106006 126 echo '</head><body style="height: 100%;">';
0cc24e9a 127 assertStringArg ('helper');
39106006
DO
128 switch ($_REQUEST['helper'])
129 {
0682218d
AD
130 case 'objlist':
131 $pageno = 'object';
132 $tabno = 'default';
133 fixContext();
134 if (!permitted())
135 renderAccessDenied();
136 assertUIntArg ('object_id');
137 $object_id = $_REQUEST['object_id'];
138 echo '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; height: 100%; text-align: center; margin: 5px;">';
139 echo '<h2>Choose a container:</h2>';
140 echo '<form action="javascript:;">';
141 $parents = findRackObjectParentCandidates($object_id);
142 printSelect ($parents, array ('name' => 'parents', 'size' => getConfigVar ('MAXSELSIZE')));
143 echo '<br>';
144 echo "<input type=submit value='Proceed' onclick='".
145 "if (getElementById(\"parents\").value != \"\") {".
146 " opener.location=\"process.php?page=object&tab=edit&op=linkEntities&object_id=${object_id}&child_entity_type=object&child_entity_id=${object_id}&parent_entity_type=object&parent_entity_id=\"+getElementById(\"parents\").value; ".
147 " window.close();}'>";
148 echo '</form></div>';
149 break;
39106006 150 case 'portlist':
026a79ee
DO
151 $pageno = 'depot';
152 $tabno = 'default';
4dc8e857
DO
153 fixContext();
154 if (!permitted())
4dc8e857 155 renderAccessDenied();
0cc24e9a 156 assertUIntArg ('port');
0cc24e9a 157 assertStringArg ('in_rack');
cd3775e9 158 $localchoice = $_REQUEST['in_rack'] == 'y';
026a79ee 159 $port_id = $_REQUEST['port'];
cd3775e9 160 echo '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; height: 100%; text-align: center; margin: 5px;"><h2>';
026a79ee
DO
161 echo $localchoice ?
162 ('Nearest spare ports (<a href="popup.php?helper=portlist&port=' . $port_id . '&in_rack=n">show all</a>)') :
163 ('All spare ports (<a href="popup.php?helper=portlist&port=' . $port_id . '&in_rack=y">show nearest</a>)');
8cc289ab 164 echo '</h2><form action="javascript:;" id="portform">';
cd3775e9 165 $only_racks = array();
d0dadd80
DO
166 global $sic;
167 $port_info = getPortInfo ($sic['port']);
026a79ee 168 if ($_REQUEST['in_rack'] == 'y' and $port_info['object_id'])
cd3775e9 169 {
026a79ee
DO
170 $object = spotEntity ('object', $port_info['object_id']);
171 if ($object['rack_id'])
172 $only_racks = getProximateRacks ($object['rack_id'], getConfigVar ('PROXIMITY_RANGE'));
cd3775e9
DO
173 }
174 $spare_ports = findSparePorts ($port_id, $only_racks);
8cc289ab 175
8c224954 176 printSelect ($spare_ports, array ('name' => 'ports', 'size' => getConfigVar ('MAXSELSIZE')));
0c7c9f8b
DO
177 echo '<br>';
178 echo "Cable ID: <input type=text id=cable>";
179 echo '<br>';
cd3775e9 180 echo "<input type='submit' value='Link' onclick='".
39106006 181 "if (getElementById(\"ports\").value != \"\") {".
0c7c9f8b 182 " 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+\"&cable=\"+getElementById(\"cable\").value; ".
39106006
DO
183 " window.close();}'>";
184 echo '</form></div>';
185 break;
186 case 'inet4list':
4dc8e857
DO
187 $pageno = 'ipv4space';
188 $tabno = 'default';
189 fixContext();
190 if (!permitted())
4dc8e857 191 renderAccessDenied();
39106006 192 echo '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; height: 100%; text-align: center; margin: 5px;">';
e8dd0422 193 echo '<h2>Choose a port:</h2><br><br>';
39106006
DO
194 echo '<form action="javascript:;">';
195 echo '<input type=hidden id=ip>';
196 echo '<select size=' . getConfigVar ('MAXSELSIZE') . ' id=addresses>';
23cdc7e9
DO
197 $addresses = getAllIPv4Allocations();
198 usort ($addresses, 'sortObjectAddressesAndNames');
199 foreach ($addresses as $address)
200 echo "<option value='${address['ip']}' onclick='getElementById(\"ip\").value=\"${address['ip']}\";'>" .
201 "${address['object_name']} ${address['name']} ${address['ip']}</option>\n";
39106006
DO
202 echo '</select><br><br>';
203 echo "<input type=submit value='Proceed' onclick='".
204 "if (getElementById(\"ip\")!=\"\") {".
205 " opener.document.getElementById(\"remoteip\").value=getElementById(\"ip\").value;".
206 " window.close();}'>";
207 echo '</form></div>';
208 break;
209 default:
d6b0c986 210 throw new InvalidRequestArgException ('helper', $_REQUEST['helper']);
39106006 211 }
e673ee24 212?>
e673ee24
DO
213</body>
214</html>
0cc24e9a
DY
215<?php
216ob_end_flush();
217}
218catch (Exception $e)
219{
220 ob_end_clean();
221 printException($e);
222}
223?>