r4178 bugfix: PHP warnings were appearing before rackspace on SLB object page, defaul...
[racktables] / popup.php
index 8664e74..14424f5 100644 (file)
--- a/popup.php
+++ b/popup.php
@@ -34,17 +34,21 @@ function getProximateRacks ($rack_id, $proximity = 0)
 
 function findSparePorts ($port_id, $only_racks = array())
 {
+       $qparams = array ($port_id, $port_id);
        $query = "SELECT id, object_id, name FROM Port WHERE " .
-               "id <> ${port_id} " .
-               "AND type IN (SELECT type2 FROM PortCompat WHERE type1 = (SELECT type FROM Port WHERE id = ${port_id})) " .
+               "id <> ? " .
+               "AND type IN (SELECT type2 FROM PortCompat WHERE type1 = (SELECT type FROM Port WHERE id = ?)) " .
                "AND reservation_comment IS NULL " .
                "AND id NOT IN (SELECT porta FROM Link) " .
                "AND id NOT IN (SELECT portb FROM Link) ";
        if (count ($only_racks))
+       {
                $query .= 'AND object_id IN (SELECT DISTINCT object_id FROM RackSpace WHERE rack_id IN (' .
-                       implode (', ', $only_racks) . '))';
+                       questionMarks (count ($only_racks)) . '))';
+               $qparams = array_merge ($qparams, $only_racks);
+       }
        $query .= ' ORDER BY object_id, name';
-       $result = useSelectBlade ($query);
+       $result = usePreparedSelectBlade ($query, $qparams);
        // avoid nested queries
        $rows = $result->fetchAll (PDO::FETCH_ASSOC);
        unset ($result);
@@ -55,9 +59,62 @@ function findSparePorts ($port_id, $only_racks = array())
                $ret[$rows[$rkey]['id']] = $object['dname'] . ' ' . $rows[$rkey]['name'];
                unset ($rows[$rkey]);
        }
+       natsort($ret);
        return $ret;
 }
 
+// Return a list of all objects which are possible parents
+//    Special case for VMs and VM Virtual Switches
+//        - only select Servers with the Hypervisor attribute set to Yes
+function findRackObjectParentCandidates ($object_id)
+{
+       $object = spotEntity ('object', $object_id);
+       $args = array ($object['objtype_id'], $object_id, $object_id);
+
+       $query  = "SELECT RO.id, RO.name FROM RackObject RO ";
+       $query .= "LEFT JOIN ObjectParentCompat OPC ON RO.objtype_id = OPC.parent_objtype_id ";
+       $query .= "WHERE OPC.child_objtype_id = ? ";
+       $query .= "AND RO.id != ? ";
+       // exclude existing parents
+       $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 = ?) ";
+       if ($object['objtype_id'] == 1504 || $object['objtype_id'] == 1507)
+       {
+               array_push($args, $object['objtype_id'], $object_id, $object_id);
+               $query .= "AND OPC.parent_objtype_id != 4 ";
+               $query .= "UNION ";
+               $query .= "SELECT RO.id, RO.name FROM RackObject RO  ";
+               $query .= "LEFT JOIN ObjectParentCompat OPC ON RO.objtype_id = OPC.parent_objtype_id ";
+               $query .= "LEFT JOIN AttributeValue AV ON RO.id = AV.object_id ";
+               $query .= "WHERE OPC.child_objtype_id = ? ";
+               $query .= "AND (RO.objtype_id = 4 AND AV.attr_id = 26 AND AV.uint_value = 1501) ";
+               $query .= "AND RO.id != ? ";
+               // exclude existing parents
+               $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 = ?) ";
+       }
+       $query .= "ORDER BY 2";
+
+       $result = usePreparedSelectBlade ($query, $args);
+       $ret = array();
+       while ($row = $result->fetch (PDO::FETCH_ASSOC))
+               $ret[$row['id']] = $row['name'];
+       return $ret;
+}
+
+function sortObjectAddressesAndNames ($a, $b)
+{
+       $objname_cmp = sortTokenize($a['object_name'], $b['object_name']);
+       if ($objname_cmp == 0)
+       {
+               $name_a = (isset ($a['port_name'])) ? $a['port_name'] : '';
+               $name_b = (isset ($b['port_name'])) ? $b['port_name'] : '';
+               $objname_cmp = sortTokenize($name_a, $name_b);
+               if ($objname_cmp == 0)
+                       sortTokenize($a['ip'], $b['ip']);
+               return $objname_cmp;
+       }
+       return $objname_cmp;
+}
+
 header ('Content-Type: text/html; charset=UTF-8');
 ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
@@ -70,6 +127,26 @@ header ('Content-Type: text/html; charset=UTF-8');
        assertStringArg ('helper');
        switch ($_REQUEST['helper'])
        {
+               case 'objlist':
+                       $pageno = 'object';
+                       $tabno = 'default';
+                       fixContext();
+                       if (!permitted())
+                               renderAccessDenied();
+                       assertUIntArg ('object_id');
+                       $object_id = $_REQUEST['object_id'];
+                       echo '<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; height: 100%; text-align: center; margin: 5px;">';
+                       echo '<h2>Choose a container:</h2>';
+                       echo '<form action="javascript:;">';
+                       $parents = findRackObjectParentCandidates($object_id);
+                       printSelect ($parents, array ('name' => 'parents', 'size' => getConfigVar ('MAXSELSIZE')));
+                       echo '<br>';
+                       echo "<input type=submit value='Proceed' onclick='".
+                       "if (getElementById(\"parents\").value != \"\") {".
+                       "       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; ".
+                       "       window.close();}'>";
+                       echo '</form></div>';
+                       break;
                case 'portlist':
                        $pageno = 'depot';
                        $tabno = 'default';
@@ -96,12 +173,13 @@ header ('Content-Type: text/html; charset=UTF-8');
                        }
                        $spare_ports = findSparePorts ($port_id, $only_racks);
 
-                       printSelect ($spare_ports, array ('name' => 'ports', 'id' => 'ports'));
-                       comboFromSelect (array ('ports'));
-                       echo '<br><br>';
+                       printSelect ($spare_ports, array ('name' => 'ports', 'size' => getConfigVar ('MAXSELSIZE')));
+                       echo '<br>';
+                       echo "Cable ID: <input type=text id=cable>";
+                       echo '<br>';
                        echo "<input type='submit' value='Link' onclick='".
                        "if (getElementById(\"ports\").value != \"\") {".
-                       "       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; ".
+                       "       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; ".
                        "       window.close();}'>";
                        echo '</form></div>';
                        break;
@@ -129,7 +207,7 @@ header ('Content-Type: text/html; charset=UTF-8');
                        echo '</form></div>';
                        break;
                default:
-                       throw new RuntimeException ('Invalid parameter or internal error');
+                       throw new InvalidRequestArgException ('helper', $_REQUEST['helper']);
        }
 ?>
 </body>