r4541 bugfix: unset array key PHP notices in searchEntitiesByText
[racktables] / wwwroot / inc / popup.php
CommitLineData
38ee0db8 1<?php
cd3775e9
DO
2
3// Return a list of rack IDs, which are P or less positions
4// far from the given rack in its row.
5function getProximateRacks ($rack_id, $proximity = 0)
6{
7 $rack = spotEntity ('rack', $rack_id);
8 $rackList = listCells ('rack', $rack['row_id']);
9 doubleLink ($rackList);
10 $ret = array ($rack_id);
11 $todo = $proximity;
12 $cur_item = $rackList[$rack_id];
13 while ($todo and array_key_exists ('prev_key', $cur_item))
14 {
15 $cur_item = $rackList[$cur_item['prev_key']];
16 $ret[] = $cur_item['id'];
17 $todo--;
18 }
19 $todo = $proximity;
20 $cur_item = $rackList[$rack_id];
21 while ($todo and array_key_exists ('next_key', $cur_item))
22 {
23 $cur_item = $rackList[$cur_item['next_key']];
24 $ret[] = $cur_item['id'];
25 $todo--;
26 }
27 return $ret;
28}
29
30function findSparePorts ($port_id, $only_racks = array())
31{
99bcce26 32 $qparams = array ($port_id, $port_id);
026a79ee 33 $query = "SELECT id, object_id, name FROM Port WHERE " .
99bcce26
DO
34 "id <> ? " .
35 "AND type IN (SELECT type2 FROM PortCompat WHERE type1 = (SELECT type FROM Port WHERE id = ?)) " .
cd3775e9
DO
36 "AND reservation_comment IS NULL " .
37 "AND id NOT IN (SELECT porta FROM Link) " .
38 "AND id NOT IN (SELECT portb FROM Link) ";
39 if (count ($only_racks))
99bcce26 40 {
cd3775e9 41 $query .= 'AND object_id IN (SELECT DISTINCT object_id FROM RackSpace WHERE rack_id IN (' .
ec523868 42 questionMarks (count ($only_racks)) . '))';
99bcce26
DO
43 $qparams = array_merge ($qparams, $only_racks);
44 }
67f10c63 45 $query .= ' ORDER BY object_id, name';
99bcce26 46 $result = usePreparedSelectBlade ($query, $qparams);
026a79ee
DO
47 // avoid nested queries
48 $rows = $result->fetchAll (PDO::FETCH_ASSOC);
49 unset ($result);
cd3775e9 50 $ret = array();
026a79ee 51 foreach (array_keys ($rows) as $rkey)
cd3775e9 52 {
026a79ee
DO
53 $object = spotEntity ('object', $rows[$rkey]['object_id']);
54 $ret[$rows[$rkey]['id']] = $object['dname'] . ' ' . $rows[$rkey]['name'];
55 unset ($rows[$rkey]);
cd3775e9 56 }
24089ff9 57 natsort($ret);
cd3775e9
DO
58 return $ret;
59}
60
0682218d
AD
61// Return a list of all objects which are possible parents
62// Special case for VMs and VM Virtual Switches
63// - only select Servers with the Hypervisor attribute set to Yes
9b8174d7 64function findObjectParentCandidates ($object_id)
0682218d
AD
65{
66 $object = spotEntity ('object', $object_id);
67 $args = array ($object['objtype_id'], $object_id, $object_id);
68
9b8174d7
AD
69 $query = "SELECT O.id, O.name FROM Object O ";
70 $query .= "LEFT JOIN ObjectParentCompat OPC ON O.objtype_id = OPC.parent_objtype_id ";
0682218d 71 $query .= "WHERE OPC.child_objtype_id = ? ";
9b8174d7 72 $query .= "AND O.id != ? ";
0682218d 73 // exclude existing parents
9b8174d7 74 $query .= "AND O.id NOT IN (SELECT parent_entity_id FROM EntityLink WHERE parent_entity_type = 'object' AND child_entity_type = 'object' AND child_entity_id = ?) ";
0682218d
AD
75 if ($object['objtype_id'] == 1504 || $object['objtype_id'] == 1507)
76 {
77 array_push($args, $object['objtype_id'], $object_id, $object_id);
78 $query .= "AND OPC.parent_objtype_id != 4 ";
79 $query .= "UNION ";
9b8174d7
AD
80 $query .= "SELECT O.id, O.name FROM Object O ";
81 $query .= "LEFT JOIN ObjectParentCompat OPC ON O.objtype_id = OPC.parent_objtype_id ";
82 $query .= "LEFT JOIN AttributeValue AV ON O.id = AV.object_id ";
0682218d 83 $query .= "WHERE OPC.child_objtype_id = ? ";
9b8174d7
AD
84 $query .= "AND (O.objtype_id = 4 AND AV.attr_id = 26 AND AV.uint_value = 1501) ";
85 $query .= "AND O.id != ? ";
0682218d 86 // exclude existing parents
9b8174d7 87 $query .= "AND O.id NOT IN (SELECT parent_entity_id FROM EntityLink WHERE parent_entity_type = 'object' AND child_entity_type = 'object' AND child_entity_id = ?) ";
0682218d
AD
88 }
89 $query .= "ORDER BY 2";
90
91 $result = usePreparedSelectBlade ($query, $args);
92 $ret = array();
93 while ($row = $result->fetch (PDO::FETCH_ASSOC))
94 $ret[$row['id']] = $row['name'];
95 return $ret;
96}
97
9a15394d
DO
98function sortObjectAddressesAndNames ($a, $b)
99{
100 $objname_cmp = sortTokenize($a['object_name'], $b['object_name']);
101 if ($objname_cmp == 0)
102 {
103 $name_a = (isset ($a['port_name'])) ? $a['port_name'] : '';
104 $name_b = (isset ($b['port_name'])) ? $b['port_name'] : '';
105 $objname_cmp = sortTokenize($name_a, $name_b);
106 if ($objname_cmp == 0)
107 sortTokenize($a['ip'], $b['ip']);
108 return $objname_cmp;
109 }
110 return $objname_cmp;
111}
112
e0ce8064
DO
113function renderPopupHTML()
114{
115 global $pageno, $tabno;
878512c6 116header ('Content-Type: text/html; charset=UTF-8');
e0ce8064 117?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
39106006
DO
118<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" style="height: 100%;">
119<head>
b325120a 120<?php
4dc8e857 121 echo "<title>RackTables pop-up</title>\n";
d6adad3e 122 printPageHeaders();
39106006 123 echo '</head><body style="height: 100%;">';
0cc24e9a 124 assertStringArg ('helper');
39106006
DO
125 switch ($_REQUEST['helper'])
126 {
0682218d
AD
127 case 'objlist':
128 $pageno = 'object';
129 $tabno = 'default';
130 fixContext();
3ec33017 131 assertPermission();
e0ce8064 132 $object_id = getBypassValue();
0682218d
AD
133 echo '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; height: 100%; text-align: center; margin: 5px;">';
134 echo '<h2>Choose a container:</h2>';
135 echo '<form action="javascript:;">';
9b8174d7 136 $parents = findObjectParentCandidates($object_id);
0682218d
AD
137 printSelect ($parents, array ('name' => 'parents', 'size' => getConfigVar ('MAXSELSIZE')));
138 echo '<br>';
139 echo "<input type=submit value='Proceed' onclick='".
140 "if (getElementById(\"parents\").value != \"\") {".
87c744a9 141 " opener.location=\"?module=redirect&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; ".
0682218d
AD
142 " window.close();}'>";
143 echo '</form></div>';
144 break;
39106006 145 case 'portlist':
026a79ee
DO
146 $pageno = 'depot';
147 $tabno = 'default';
4dc8e857 148 fixContext();
3ec33017 149 assertPermission();
0cc24e9a 150 assertUIntArg ('port');
0cc24e9a 151 assertStringArg ('in_rack');
cd3775e9 152 $localchoice = $_REQUEST['in_rack'] == 'y';
026a79ee 153 $port_id = $_REQUEST['port'];
cd3775e9 154 echo '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; height: 100%; text-align: center; margin: 5px;"><h2>';
026a79ee 155 echo $localchoice ?
e0ce8064
DO
156 ('Nearest spare ports (<a href="?module=popup&helper=portlist&port=' . $port_id . '&in_rack=n">show all</a>)') :
157 ('All spare ports (<a href="?module=popup&helper=portlist&port=' . $port_id . '&in_rack=y">show nearest</a>)');
8cc289ab 158 echo '</h2><form action="javascript:;" id="portform">';
cd3775e9 159 $only_racks = array();
d0dadd80
DO
160 global $sic;
161 $port_info = getPortInfo ($sic['port']);
026a79ee 162 if ($_REQUEST['in_rack'] == 'y' and $port_info['object_id'])
cd3775e9 163 {
026a79ee
DO
164 $object = spotEntity ('object', $port_info['object_id']);
165 if ($object['rack_id'])
166 $only_racks = getProximateRacks ($object['rack_id'], getConfigVar ('PROXIMITY_RANGE'));
cd3775e9
DO
167 }
168 $spare_ports = findSparePorts ($port_id, $only_racks);
8cc289ab 169
8c224954 170 printSelect ($spare_ports, array ('name' => 'ports', 'size' => getConfigVar ('MAXSELSIZE')));
0c7c9f8b
DO
171 echo '<br>';
172 echo "Cable ID: <input type=text id=cable>";
173 echo '<br>';
cd3775e9 174 echo "<input type='submit' value='Link' onclick='".
39106006 175 "if (getElementById(\"ports\").value != \"\") {".
66e1cdfb 176 " opener.location=\"?module=redirect&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
177 " window.close();}'>";
178 echo '</form></div>';
179 break;
180 case 'inet4list':
4dc8e857
DO
181 $pageno = 'ipv4space';
182 $tabno = 'default';
183 fixContext();
3ec33017 184 assertPermission();
39106006 185 echo '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; height: 100%; text-align: center; margin: 5px;">';
e8dd0422 186 echo '<h2>Choose a port:</h2><br><br>';
39106006
DO
187 echo '<form action="javascript:;">';
188 echo '<input type=hidden id=ip>';
189 echo '<select size=' . getConfigVar ('MAXSELSIZE') . ' id=addresses>';
23cdc7e9
DO
190 $addresses = getAllIPv4Allocations();
191 usort ($addresses, 'sortObjectAddressesAndNames');
192 foreach ($addresses as $address)
193 echo "<option value='${address['ip']}' onclick='getElementById(\"ip\").value=\"${address['ip']}\";'>" .
194 "${address['object_name']} ${address['name']} ${address['ip']}</option>\n";
39106006
DO
195 echo '</select><br><br>';
196 echo "<input type=submit value='Proceed' onclick='".
197 "if (getElementById(\"ip\")!=\"\") {".
198 " opener.document.getElementById(\"remoteip\").value=getElementById(\"ip\").value;".
199 " window.close();}'>";
200 echo '</form></div>';
201 break;
202 default:
d6b0c986 203 throw new InvalidRequestArgException ('helper', $_REQUEST['helper']);
39106006 204 }
e673ee24 205?>
e673ee24
DO
206</body>
207</html>
0cc24e9a 208<?php
0cc24e9a
DY
209}
210?>