r1281 + setConfigVar() didn't work ever, fixed
authorDenis Ovsienko <infrastation@yandex.ru>
Wed, 14 Nov 2007 22:41:27 +0000 (22:41 +0000)
committerDenis Ovsienko <infrastation@yandex.ru>
Wed, 14 Nov 2007 22:41:27 +0000 (22:41 +0000)
+ new extra option to setConfigVar() to avoid unnecessary call to die()
+ getConfigVar() was a little broken also
+ updateUI(): use stock functions, clean up arg list
+ renderUIConfig()/renderUIConfigEditForm: don't show the user too much data
+ getObjectInfo(): print more detailed error message
+ getObjectAddresses(): fix two queries trying to use the same DB link
+ brand new dragons on the reports page

inc/config.php
inc/database.php
inc/interface.php
inc/ophandlers.php
pi.css

index f23d9f8..69a3a93 100644 (file)
@@ -35,7 +35,7 @@ function getConfigVar ($varname = '')
        if (isset ($configCache[$varname]))
        {
                // Try casting to int, if possible.
-               if ($configCache[$varname]['vartype'] == 'unit')
+               if ($configCache[$varname]['vartype'] == 'uint')
                        return 0 + $configCache[$varname]['varvalue'];
                else
                        return $configCache[$varname]['varvalue'];
@@ -43,7 +43,9 @@ function getConfigVar ($varname = '')
        return NULL;
 }
 
-function setConfigVar ($varname = '', $varvalue = '')
+// In softfail mode die only on fatal errors, letting the user check
+// and resubmit his input.
+function setConfigVar ($varname = '', $varvalue = '', $softfail = FALSE)
 {
        global $configCache;
        if (!isset ($configCache))
@@ -64,12 +66,29 @@ function setConfigVar ($varname = '', $varvalue = '')
        }
        if (empty ($varvalue) && $configCache[$varname]['emptyok'] != 'yes')
        {
-               showError ("'${varname}' is configured to take non-empty value. Perhaps there was a reason to do so.");
+               $errormsg = "'${varname}' is configured to take non-empty value. Perhaps there was a reason to do so.";
+               if ($softfail)
+                       return $errormsg;
+               showError ($errormsg);
+               die;
+       }
+       if ($configCache[$varname]['vartype'] == 'uint' && (!is_numeric ($varvalue) or $varvalue < 0 ))
+       {
+               $errormsg = "'${varname}' can accept UINT values only";
+               if ($softfail)
+                       return $errormsg;
+               showError ($errormsg);
                die;
        }
        // Update cache only if the changes went into DB.
        if (storeConfigVar ($varname, $varvalue))
+       {
                $configCache[$varname]['varvalue'] = $varvalue;
+               if ($softfail)
+                       return '';
+       }
+       elseif ($softfail)
+               return "storeConfigVar() failed in setConfigVar()";
 }
 
 ?>
index 5ade8c5..2ab0dcf 100644 (file)
@@ -185,7 +185,8 @@ function getObjectInfo ($object_id = 0)
        $result = $dbxlink->query ($query);
        if ($result == NULL)
        {
-               showError ('SQL query failure in getObjectInfo()');
+               $ei = $dbxlink->errorInfo();
+               showError ("SQL query failed in getObjectInfo (${object_id}) with error ${ei[1]} (${ei[2]})");
                return NULL;
        }
        if (($row = $result->fetch (PDO::FETCH_ASSOC)) == NULL)
@@ -925,7 +926,12 @@ function getObjectAddresses ($object_id = 0)
                $count=0;
                $refcount=0;
                $prev_ip = 0;
-               while ($row = $result->fetch (PDO::FETCH_ASSOC))
+               // We are going to call getObjectInfo() for some rows,
+               // hence the connector must be unloaded from the
+               // current data.
+               $rows = $result->fetchAll (PDO::FETCH_ASSOC);
+               $result->closeCursor();
+               foreach ($rows as $row)
                {
                        if ($prev_ip != $row['IPBonds_ip'])
                        {
@@ -945,13 +951,17 @@ function getObjectAddresses ($object_id = 0)
                                $ret[$count]['references'][$refcount]['type'] = $row['RemoteBonds_type'];
                                $ret[$count]['references'][$refcount]['name'] = $row['RemoteBonds_name'];
                                $ret[$count]['references'][$refcount]['object_id'] = $row['RemoteBonds_object_id'];
-                               $oi = getObjectInfo ($row['RemoteBonds_object_id']);
-                               $ret[$count]['references'][$refcount]['object_name'] = displayedName ($oi);
+                               if (empty ($row['RemoteBonds_object_id']))
+                                       $ret[$count]['references'][$refcount]['object_name'] = $row['RemoteObject_name'];
+                               else
+                               {
+                                       $oi = getObjectInfo ($row['RemoteBonds_object_id']);
+                                       $ret[$count]['references'][$refcount]['object_name'] = displayedName ($oi);
+                               }
                                $refcount++;
                        }
                }
        }
-       $result->closeCursor();
        return $ret;
 }
 
@@ -1765,25 +1775,6 @@ function commitUseupPort ($port_id = 0)
        
 }
 
-function commitUpdateUI ($varname, $vartype, $emptyok, $varvalue, $description)
-{
-       if (empty ($varname) || empty ($vartype) || empty ($emptyok) || empty ($varvalue)|| empty ($description))
-       {
-               showError ('Not all required args to commitUpdateUI() are present.');
-               return FALSE;
-       }
-       global $dbxlink;
-       $query = "UPDATE Config SET vartype='${vartype}', emptyok='${emptyok}', varvalue='${varvalue}', description='${description}' " .
-                       "WHERE varname='${varname}'";
-       $result = $dbxlink->query ($query);
-       if ($result->rowCount() != 1)
-       {
-               showError ('Error updating config in commitUpdateUI()');
-               return FALSE;
-       }
-       return TRUE;
-}
-
 // This is a swiss-knife blade to insert a record into a table.
 // The first argument is table name.
 // The second argument is an array of "name" => "value" pairs.
@@ -1844,6 +1835,7 @@ function loadConfigCache ()
 // setConfigVar() is expected to perform all necessary filtering
 function storeConfigVar ($varname = NULL, $varvalue = NULL)
 {
+       global $dbxlink;
        if ($varname == NULL || $varvalue == NULL)
        {
                showError ('Invalid arguments to storeConfigVar()');
index f7b2528..4b551c5 100644 (file)
@@ -2863,7 +2863,32 @@ function getFaviconURL ()
 
 function renderReportSummary ()
 {
-       echo "Here be dragons :-P";
+       startPortlet ("Here be dragons");
+       dragon();
+       dragon();
+       dragon();
+       echo 'ASCII art &copy; Daniel C. Au';
+       finishPortlet();
+}
+
+function dragon ()
+{
+?>
+<div class=dragon><pre><font color="#00ff33">
+                 \||/
+                 |  <font color="#ff0000">@</font>___oo  
+       /\  /\   / (__<font color=yellow>,,,,</font>|
+      ) /^\) ^\/ _)
+      )   /^\/   _) 
+      )   _ /  / _)
+  /\  )/\/ ||  | )_)    
+ &lt;  &gt;      |(<font color=white>,,</font>) )__)
+  ||      /    \)___)\
+  | \____(      )___) )___
+   \______(_______<font color=white>;;;</font> __<font color=white>;;;</font>
+
+</font></pre></div>
+<?php
 }
 
 function renderUIConfig ()
@@ -2871,21 +2896,17 @@ function renderUIConfig ()
        global $configCache, $nextorder;
        showMessageOrError();
        startPortlet ('Current configuration');
-       echo '<table class=cooltable border=0 cellpadding=5 cellspacing=0 align=center>';
-       echo '<tr><th class=tdleft>Name</th><th class=tdleft>Type</th><th class=tdleft>Ok<br>if<br>empty?</th><th class=tdleft>Value</th><th class=tdleft>Description</th></tr>';
+       echo '<table class=cooltable border=0 cellpadding=5 cellspacing=0 align=center width="50%">';
+       echo '<tr><th class=tdleft>Option</th><th class=tdleft>Value</th></tr>';
        $order = 'odd';
        foreach ($configCache as $v)
        {
-               if ($v['is_hidden'] == 'no')
-               {
-                       echo "<tr class=row_${order}>";
-                       echo "<td class=tdleft>${v['varname']}</td>";
-                       echo "<td class=tdleft>${v['vartype']}</td>";
-                       echo "<td class=tdleft>${v['emptyok']}</td>"; 
-                       echo "<td class=tdleft>${v['varvalue']}</td>";
-                       echo "<td class=tdleft>${v['description']}</td></tr>\n";
-                       $order = $nextorder[$order];
-               }
+               if ($v['is_hidden'] != 'no')
+                       continue;
+               echo "<tr class=row_${order}>";
+               echo "<td class=tdright>${v['description']}</td>\n";
+               echo "<td class=tdleft>${v['varvalue']}</td></tr>";
+               $order = $nextorder[$order];
        }
        echo "</table>\n";
        finishPortlet();
@@ -2893,58 +2914,30 @@ function renderUIConfig ()
 
 function renderUIConfigEditForm ()
 {
-       global $root, $configCache;
+       global $root, $pageno, $tabno, $configCache;
        showMessageOrError();
        startPortlet ('Current configuration');
-       echo "<table cellspacing=0 cellpadding=5 align=center class=widetable>\n";
-       echo "<tr><th class=tdleft>Name</th>";
-       echo "<th class=tdleft>Type</th>";
-       echo "<th class=tdleft>Ok<br>if<br>empty?</th>";
-       echo "<th class=tdleft>Value</th>";
-       echo "<th class=tdleft>Description</th></tr>\n";
+       echo "<table cellspacing=0 cellpadding=5 align=center class=widetable width='50%'>\n";
+       echo "<tr><th class=tdleft>Option</th>";
+       echo "<th class=tdleft>Value</th></tr>";
        echo "<form action='${root}process.php'>";
        echo "<input type=hidden name=op value='upd'>";
-       echo "<input type=hidden name=page value='ui'>\n";
-       echo "<input type=hidden name=tab value='edit'>\n";
+       echo "<input type=hidden name=page value='${pageno}'>\n";
+       echo "<input type=hidden name=tab value='${tabno}'>\n";
 
        $i = 0;
        foreach ($configCache as $v)
        {
-               if ($v['is_hidden'] == 'no')
-               {
-                       echo "<input type=hidden name=${i}_varname value='${v['varname']}'>";
-                       echo "<tr><td class=tdleft>${v['varname']}</td>";
-
-                       echo "<td class=tdleft><select name=${i}_vartype>";
-                       echo "<option value=string";
-                       if ($v['vartype'] == 'string')
-                               echo " selected";
-                       echo ">string</option>";
-                       echo "<option value=uint";
-                       if ($v['vartype'] == 'uint')
-                               echo " selected";
-                       echo ">uint</option>";
-                       echo "</select></td>";
-
-                       echo "<td class=tdleft><select name=${i}_emptyok>";
-                       echo "<option value=no";
-                       if ($v['emptyok'] == 'no')
-                               echo " selected";
-                       echo ">no</option>";
-                       echo "<option value=yes";
-                       if ($v['emptyok'] == 'yes')
-                               echo " selected";
-                       echo ">yes</option>";
-                       echo "</select></td>";
-                       
-                       echo "<td><input type=text name=${i}_varvalue value='${v['varvalue']}' size=24></td>";
-                       echo "<td><input type=text name=${i}_description value='${v['description']}' size=64></td>";
-                       echo "</tr>\n";
-                       $i++;
-               }
+               if ($v['is_hidden'] != 'no')
+                       continue;
+               echo "<input type=hidden name=${i}_varname value='${v['varname']}'>";
+               echo "<tr><td class=tdright>${v['description']}</td>";
+               echo "<td class=tdleft><input type=text name=${i}_varvalue value='${v['varvalue']}' size=24></td>";
+               echo "</tr>\n";
+               $i++;
        }
        echo "<input type=hidden name=num_vars value=${i}>\n";
-       echo "<tr><td colspan=5><input type=submit value='Save changes'></td></tr>";
+       echo "<tr><td colspan=2><input type=submit value='Save changes'></td></tr>";
        echo "</form>";
        finishPortlet();
 }
index e6ea5f0..f24e243 100644 (file)
@@ -911,41 +911,34 @@ function updateVLANMembership ()
 
 function updateUI ()
 {
-       global $root;
+       global $root, $pageno, $tabno;
        $oldvalues = loadConfigCache();
 
        assertUIntArg ('num_vars');
        $num_vars = $_REQUEST['num_vars'];
-       $result = array();
+       $error = '';
 
        for ($i = 0; $i < $num_vars; $i++)
        {
                assertStringArg ("${i}_varname");
-               assertStringArg ("${i}_vartype");
-               assertStringArg ("${i}_emptyok");
                assertStringArg ("${i}_varvalue");
-               assertStringArg ("${i}_description");
                $varname = $_REQUEST["${i}_varname"];
-               $vartype = $_REQUEST["${i}_vartype"];
-               $emptyok = $_REQUEST["${i}_emptyok"];
                $varvalue = $_REQUEST["${i}_varvalue"];
-               $description = $_REQUEST["${i}_description"];
 
-               // If form values = values in DB, don't bother updating DB
-               if ($oldvalues[$varname]['vartype'] == $vartype && 
-                       $oldvalues[$varname]['emptyok'] == $emptyok && 
-                       $oldvalues[$varname]['varvalue'] == $varvalue && 
-                       $oldvalues[$varname]['description'] == $description)
+               // If form value = value in DB, don't bother updating DB
+               if ($varvalue == getConfigVar ($varname))
                        continue;
 
                // Note if the queries succeed or not, it determines which page they see.
-               $result[] = commitUpdateUI ($varname, $vartype, $emptyok, $varvalue, $description);
+               $error = setConfigVar ($varname, $varvalue, TRUE);
+               if (!empty ($error))
+                       break;
        }
 
-       if (in_array(false, $result))
-               return "${root}?page=ui&tab=default&error=" . urlencode ("Update failed!");
+       if ($error != '')
+               return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Update failed with error: " . $error);
 
-       return "${root}?page=ui&tab=default&message=" . urlencode ("Update succeeded.");
+       return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Update succeeded.");
 }
 
 ?>
diff --git a/pi.css b/pi.css
index 6e600b2..25a5bb6 100644 (file)
--- a/pi.css
+++ b/pi.css
@@ -53,6 +53,11 @@ table.molecule {
        text-align: left;
 }
 
+.dragon {
+       background-color: #000033;
+       text-align: left;
+}
+
 .tdright {
        text-align: right;
 }