Cleans a little bit the code and add the total number of rows in paginated mode r1
authorGuillaume (ioguix) de Rorthais <ioguix@free.fr>
Sun, 12 Oct 2008 03:39:20 +0000 (23:39 -0400)
committerGuillaume (ioguix) de Rorthais <ioguix@free.fr>
Sun, 12 Oct 2008 03:39:20 +0000 (23:39 -0400)
 * remove bad named $str and $str2 vars
 * use booleans $has_object and $has_subject instead of reapeting
   isset() many times
 * add some code comments
 * code cleaning around printTitle in doBrowse
 * add total number of rows in the relation (see #782258 feature request)

classes/database/Postgres.php
display.php

index e421be6fab505f0212062cab8fb61fef003f1883..3b1a16329dd227d3a0b69d188f3ba18889eef2f0 100755 (executable)
@@ -4324,6 +4324,7 @@ class Postgres extends ADODB_base {
         * @param $page The page of the relation to retrieve
         * @param $page_size The number of rows per page
         * @param &$max_pages (return-by-ref) The max number of pages in the relation
+        * @param &$nbrows (return-by-ref) The number of row in the relation
         * @return A recordset on success
         * @return -1 transaction error
         * @return -2 counting error
@@ -4331,7 +4332,7 @@ class Postgres extends ADODB_base {
         * @return -4 unknown type
         * @return -5 failed setting transaction read only
         */
-       function browseQuery($type, $table, $query, $sortkey, $sortdir, $page, $page_size, &$max_pages) {
+       function browseQuery($type, $table, $query, $sortkey, $sortdir, $page, $page_size, &$max_pages, &$nbrows) {
                // Check that we're not going to divide by zero
                if (!is_numeric($page_size) || $page_size != (int)$page_size || $page_size <= 0) return -3;
 
@@ -4373,14 +4374,14 @@ class Postgres extends ADODB_base {
 
 
                // Count the number of rows
-               $total = $this->browseQueryCount($query, $count);
-               if ($total < 0) {
+               $nbrows = $this->browseQueryCount($query, $count);
+               if ($nbrows < 0) {
                        $this->rollbackTransaction();
                        return -2;
                }
 
                // Calculate max pages
-               $max_pages = ceil($total / $page_size);
+               $max_pages = ceil($nbrows / $page_size);
 
                // Check that page is less than or equal to max pages
                if (!is_numeric($page) || $page != (int)$page || $page > $max_pages || $page < 1) {
index 31887ffb3addb418cbefe1616251f6fb759036d3..d045acbd93e52fbb2c972e2fb0fdb44c9284b173 100644 (file)
 
                        $attrs = $data->getTableAttributes($_REQUEST['table']);
                        $rs = $data->browseRow($_REQUEST['table'], $key);
-                       
+
                        echo "<form action=\"display.php\" method=\"post\" id=\"ac_form\">\n";
                        $elements = 0;
-                       $error = true;                  
+                       $error = true;
                        if ($rs->recordCount() == 1 && $attrs->recordCount() > 0) {
                                echo "<table>\n";
 
                                        }
                                        $attrs->fields['attnotnull'] = $data->phpBool($attrs->fields['attnotnull']);
                                        $id = (($i % 2) == 0 ? '1' : '2');
-                                       
+
                                        // Initialise variables
                                        if (!isset($_REQUEST['format'][$attrs->fields['attname']]))
                                                $_REQUEST['format'][$attrs->fields['attname']] = 'VALUE';
-                                       
+
                                        echo "<tr>\n";
                                        echo "<td class=\"data{$id}\" style=\"white-space:nowrap;\">", $misc->printVal($attrs->fields['attname']), "</td>";
                                        echo "<td class=\"data{$id}\" style=\"white-space:nowrap;\">\n";
                                        echo $misc->printVal($data->formatType($attrs->fields['type'], $attrs->fields['atttypmod']));
-                                       echo "<input type=\"hidden\" name=\"types[", htmlspecialchars($attrs->fields['attname']), "]\" value=\"", 
+                                       echo "<input type=\"hidden\" name=\"types[", htmlspecialchars($attrs->fields['attname']), "]\" value=\"",
                                                htmlspecialchars($attrs->fields['type']), "\" /></td>";
                                        $elements++;
                                        echo "<td class=\"data{$id}\" style=\"white-space:nowrap;\">\n";
 
                                        echo "<td class=\"data{$id}\" id=\"aciwp{$i}\" style=\"white-space:nowrap;\">";
                                        // If the column allows nulls, then we put a JavaScript action on the data field to unset the
-                                       // NULL checkbox as soon as anything is entered in the field.  We use the $elements variable to 
+                                       // NULL checkbox as soon as anything is entered in the field.  We use the $elements variable to
                                        // keep track of which element offset we're up to.  We can't refer to the null checkbox by name
                                        // as it contains '[' and ']' characters.
                                        if (!$attrs->fields['attnotnull']) {
-                                               echo $data->printField($szValueName, $rs->fields[$attrs->fields['attname']], $attrs->fields['type'], 
+                                               echo $data->printField($szValueName, $rs->fields[$attrs->fields['attname']], $attrs->fields['type'],
                                                                                                        array('onChange' => 'elements[' . ($elements - 1) . '].checked = false;'),$szEvents) . $szDivPH;
                                        }
                                        else {
                                $error = false;
                        }
                        elseif ($rs->recordCount() != 1) {
-                               echo "<p>{$lang['strrownotunique']}</p>\n";                             
+                               echo "<p>{$lang['strrownotunique']}</p>\n";
                        }
                        else {
                                echo "<p>{$lang['strinvalidparam']}</p>\n";
                else {
                        if (!isset($_POST['values'])) $_POST['values'] = array();
                        if (!isset($_POST['nulls'])) $_POST['nulls'] = array();
-                       
-                       $status = $data->editRow($_POST['table'], $_POST['values'], $_POST['nulls'], 
+
+                       $status = $data->editRow($_POST['table'], $_POST['values'], $_POST['nulls'],
                                                                                                $_POST['format'], $_POST['types'], unserialize($_POST['key']));
                        if ($status == 0)
                                doBrowse($lang['strrowupdated']);
                                doEditRow(true, $lang['strrowupdatedbad']);
                }
 
-       }       
+       }
 
        /**
         * Show confirmation of drop and perform actual drop
                        $misc->printTitle($lang['strdeleterow']);
 
                        echo "<p>{$lang['strconfdeleterow']}</p>\n";
-                       
+
                        echo "<form action=\"display.php\" method=\"post\">\n";
                        echo "<input type=\"hidden\" name=\"action\" value=\"delrow\" />\n";
                        echo $misc->form;
                                doBrowse($lang['strrowdeleted']);
                        elseif ($status == -2)
                                doBrowse($lang['strrownotunique']);
-                       else                    
+                       else
                                doBrowse($lang['strrowdeletedbad']);
                }
-               
+
        }
 
-       /** 
+       /**
         * Displays requested data
         */
        function doBrowse($msg = '') {
                global $data, $conf, $misc, $lang;
-               
+
                $save_history = false;
                // If current page is not set, default to first page
                if (!isset($_REQUEST['page']))
                        $_REQUEST['page'] = 1;
                if (!isset($_REQUEST['nohistory']))
                        $save_history = true;
-               
+
+               /* check if we are working with a relation
+                * and set the type of browsing we are doing accordingly*/
+               $has_object = false;
+               $has_subject = false;
                if (isset($_REQUEST['subject'])) {
                        $subject = $_REQUEST['subject'];
-                       if (isset($_REQUEST[$subject])) $object = $_REQUEST[$subject];
+                       $has_subject = true;
+                       if (isset($_REQUEST[$subject])) {
+                               $object = $_REQUEST[$subject];
+                               $has_object = true;
+                               if (isset($_REQUEST['query']))
+                                       $type = 'SELECT'; /* custom request on the table */
+                               else
+                                       $type = 'TABLE'; /* we are browsing the table */
+                       }
                }
                else {
                        $subject = '';
+                       $type = 'QUERY'; /* custom query */
                }
-       
-               $misc->printTrail(isset($subject) ? $subject : 'database');
-               
-               if (isset($object)) {
-                       if (isset($_REQUEST['query'])) {
+
+               $misc->printTrail($has_subject ? $subject : 'database');
+               switch($type) {
+                       case 'SELECT':
                                $misc->printTitle($lang['strselect']);
-                               $type = 'SELECT';
-                       } else {
+                               break;
+                       case 'TABLE':
                                $misc->printTitle($lang['strbrowse']);
-                               $type = 'TABLE';
-                       }
-               } else {
-                       $misc->printTitle($lang['strqueryresults']);
-                       $type = 'QUERY';
+                               break;
+                       case 'QUERY':
+                               $misc->printTitle($lang['strqueryresults']);
                }
 
                $misc->printMsg($msg);
 
                // If 'sortkey' is not set, default to ''
                if (!isset($_REQUEST['sortkey'])) $_REQUEST['sortkey'] = '';
-       
+
                // If 'sortdir' is not set, default to ''
                if (!isset($_REQUEST['sortdir'])) $_REQUEST['sortdir'] = '';
-       
-               // If 'strings' is not set, default to collapsed 
+
+               // If 'strings' is not set, default to collapsed
                if (!isset($_REQUEST['strings'])) $_REQUEST['strings'] = 'collapsed';
-       
-               // Fetch unique row identifier, if this is a table browse request.
-               if (isset($object))
+
+               // Fetch unique row identifier, if this is not a custom request.
+               if ($has_object)
                        $key = $data->getRowIdentifier($object);
                else
                        $key = array();
-               
+
                // Set the schema search path
                if ($data->hasSchemas() && isset($_REQUEST['search_path'])) {
                        if ($data->setSearchPath(array_map('trim',explode(',',$_REQUEST['search_path']))) != 0) {
                        }
                }
 
-               // Retrieve page from query.  $max_pages is returned by reference.
-               $rs = $data->browseQuery($type, 
-                       isset($object) ? $object : null, 
-                       isset($_REQUEST['query']) ? $_REQUEST['query'] : null, 
+               // Retrieve page from query.  $max_pages & $nbrows are returned by reference.
+               $rs = $data->browseQuery($type,
+                       $has_object ? $object : null,
+                       isset($_REQUEST['query']) ? $_REQUEST['query'] : null,
                        $_REQUEST['sortkey'], $_REQUEST['sortdir'], $_REQUEST['page'],
-                       $conf['max_rows'], $max_pages);
-       
+                       $conf['max_rows'], $max_pages, $nbrows);
+
                // Build strings for GETs
-               $str =  $misc->href; // . "&amp;page=" . urlencode($_REQUEST['page']);
-               if (isset($object)) $str .= "&amp;" . urlencode($subject) . '=' . urlencode($object);
-               if (isset($subject)) $str .= "&amp;subject=" . urlencode($subject);
-               if (isset($_REQUEST['query'])) $str .= "&amp;query=" . urlencode($_REQUEST['query']);
-               if (isset($_REQUEST['count'])) $str .= "&amp;count=" . urlencode($_REQUEST['count']);
-               if (isset($_REQUEST['return_url'])) $str .= "&amp;return_url=" . urlencode($_REQUEST['return_url']);
-               if (isset($_REQUEST['return_desc'])) $str .= "&amp;return_desc=" . urlencode($_REQUEST['return_desc']);
-               if (isset($_REQUEST['search_path'])) $str .= "&amp;search_path=" . urlencode($_REQUEST['search_path']);
-               if (isset($_REQUEST['table'])) $str .= "&amp;table=" . urlencode($_REQUEST['table']);
-               
+               $get_str =      $misc->href; // . "&amp;page=" . urlencode($_REQUEST['page']);
+               if ($has_object) $get_str .= "&amp;" . urlencode($subject) . '=' . urlencode($object);
+               if ($has_subject) $get_str .= "&amp;subject=" . urlencode($subject);
+               if (isset($_REQUEST['query'])) $get_str .= "&amp;query=" . urlencode($_REQUEST['query']);
+               if (isset($_REQUEST['count'])) $get_str .= "&amp;count=" . urlencode($_REQUEST['count']);
+               if (isset($_REQUEST['return_url'])) $get_str .= "&amp;return_url=" . urlencode($_REQUEST['return_url']);
+               if (isset($_REQUEST['return_desc'])) $get_str .= "&amp;return_desc=" . urlencode($_REQUEST['return_desc']);
+               if (isset($_REQUEST['search_path'])) $get_str .= "&amp;search_path=" . urlencode($_REQUEST['search_path']);
+               if (isset($_REQUEST['table'])) $get_str .= "&amp;table=" . urlencode($_REQUEST['table']);
+
                // This string just contains sort info
-               $str2 = "sortkey=" . urlencode($_REQUEST['sortkey']) . 
+               $sort_str = "sortkey=" . urlencode($_REQUEST['sortkey']) .
                        "&amp;sortdir=" . urlencode($_REQUEST['sortdir']);
-                       
-               if ($save_history && is_object($rs) && ($type == 'QUERY')) //{
+
+               if ($save_history && is_object($rs) && ($type == 'QUERY'))
                        $misc->saveScriptHistory($_REQUEST['query']);
 
                if (is_object($rs) && $rs->recordCount() > 0) {
                        // Show page navigation
-                       $misc->printPages($_REQUEST['page'], $max_pages, "display.php?page=%s&amp;{$str}&amp;{$str2}&amp;nohistory=t&amp;strings=" . urlencode($_REQUEST['strings']));
+                       $misc->printPages($_REQUEST['page'], $max_pages, "display.php?page=%s&amp;{$get_str}&amp;{$sort_str}&amp;nohistory=t&amp;strings=" . urlencode($_REQUEST['strings']));
                        echo "<table>\n<tr>";
-       
-                       // Check that the key is actually in the result set.  This can occur for select
-                       // operations where the key fields aren't part of the select.  XXX:  We should
-                       // be able to support this, somehow.
+
+                       /* Check that the key is actually in the result set.  This can occur for select
+                        * operations where the key fields aren't part of the select.
+                        * XXX:  We should be able to support this, somehow.
+                        */
                        foreach ($key as $v) {
                                // If a key column is not found in the record set, then we
                                // can't use the key.
                                        break;
                                }
                        }
+
+                       /** Display header **/
                        // Display edit and delete actions if we have a key
                        if (sizeof($key) > 0)
                                echo "<th colspan=\"2\" class=\"data\">{$lang['stractions']}</th>\n";
 
-                       $j = 0;         
+                       $j = 0;
                        foreach ($rs->fields as $k => $v) {
-                               if (isset($object) && $k == $data->id && !$conf['show_oids']) {
+                               /* hide oids according to the user conf */
+                               if ($has_object && $k == $data->id && !$conf['show_oids']) {
                                        $j++;
                                        continue;
                                }
                                $finfo = $rs->fetchField($j);
-                               // Display column headers with sorting options, unless we're PostgreSQL
-                               // 7.0 and it's a non-TABLE mode
+                               /* Display column headers with sorting options, unless we're PostgreSQL
+                                * 7.0 and it's a non-TABLE mode*/
                                if (!$data->hasFullSubqueries() && $type != 'TABLE') {
                                        echo "<th class=\"data\">", $misc->printVal($finfo->name), "</th>\n";
                                }
                                else {
-                                       echo "<th class=\"data\"><a href=\"display.php?{$str}&amp;sortkey=", ($j + 1), "&amp;sortdir=";
+                                       echo "<th class=\"data\"><a href=\"display.php?{$get_str}&amp;sortkey=", ($j + 1), "&amp;sortdir=";
                                        // Sort direction opposite to current direction, unless it's currently ''
                                        echo ($_REQUEST['sortdir'] == 'asc' && $_REQUEST['sortkey'] == ($j + 1)) ? 'desc' : 'asc';
-                                       echo "&amp;strings=", urlencode($_REQUEST['strings']), 
-                                               "&amp;page=" . urlencode($_REQUEST['page']), "\">", 
+                                       echo "&amp;strings=", urlencode($_REQUEST['strings']),
+                                               "&amp;page=" . urlencode($_REQUEST['page']), "\">",
                                                $misc->printVal($finfo->name), "</a></th>\n";
                                }
                                $j++;
                        }
-       
+
                        echo "</tr>\n";
-       
-                       $i = 0;         
+
+                       /** Display Datas **/
+                       $i = 0;
                        reset($rs->fields);
                        while (!$rs->EOF) {
                                $id = (($i % 2) == 0 ? '1' : '2');
                                echo "<tr>\n";
                                // Display edit and delete links if we have a key
                                if (sizeof($key) > 0) {
+                                       /* Build unique key parameter for URL
+                                        * and check if the key has null.*/
                                        $key_str = '';
                                        $has_nulls = false;
                                        foreach ($key as $v) {
                                                $key_str .= urlencode("key[{$v}]") . '=' . urlencode($rs->fields[$v]);
                                        }
                                        if ($has_nulls) {
+                                               /* With null(s) we cannot show the actions links. */
                                                echo "<td class=\"data{$id}\" colspan=\"2\">&nbsp;</td>\n";
                                        } else {
-                                               echo "<td class=\"opbutton{$id}\"><a href=\"display.php?action=confeditrow&amp;strings=", 
-                                                       urlencode($_REQUEST['strings']), "&amp;page=", 
-                                                       urlencode($_REQUEST['page']), "&amp;{$key_str}&amp;{$str}&amp;{$str2}\">{$lang['stredit']}</a></td>\n";
-                                               echo "<td class=\"opbutton{$id}\"><a href=\"display.php?action=confdelrow&amp;strings=", 
-                                                       urlencode($_REQUEST['strings']), "&amp;page=", 
-                                                       urlencode($_REQUEST['page']), "&amp;{$key_str}&amp;{$str}&amp;{$str2}\">{$lang['strdelete']}</a></td>\n";
+                                               echo "<td class=\"opbutton{$id}\"><a href=\"display.php?action=confeditrow&amp;strings=",
+                                                       urlencode($_REQUEST['strings']), "&amp;page=",
+                                                       urlencode($_REQUEST['page']), "&amp;{$key_str}&amp;{$get_str}&amp;{$sort_str}\">{$lang['stredit']}</a></td>\n";
+                                               echo "<td class=\"opbutton{$id}\"><a href=\"display.php?action=confdelrow&amp;strings=",
+                                                       urlencode($_REQUEST['strings']), "&amp;page=",
+                                                       urlencode($_REQUEST['page']), "&amp;{$key_str}&amp;{$get_str}&amp;{$sort_str}\">{$lang['strdelete']}</a></td>\n";
                                        }
                                }
                                $j = 0;
                                $rs->moveNext();
                                $i++;
                        }
-                       echo "</table>\n";                      
-                       echo "<p>", $rs->recordCount(), " {$lang['strrows']}</p>\n";
+                       echo "</table>\n";
+                       echo "<p>", $rs->recordCount(), " / $nbrows {$lang['strrows']}</p>\n";
                        // Show page navigation
-                       $misc->printPages($_REQUEST['page'], $max_pages, "display.php?page=%s&amp;{$str}&amp;{$str2}&amp;strings=" . urlencode($_REQUEST['strings']));
+                       $misc->printPages($_REQUEST['page'], $max_pages, "display.php?page=%s&amp;{$get_str}&amp;{$sort_str}&amp;strings=" . urlencode($_REQUEST['strings']));
                }
                else echo "<p>{$lang['strnodata']}</p>\n";
 
-               // Navigation links     
+               // Navigation links
                echo "<ul class=\"navlink\">\n";
                // Return
                if (isset($_REQUEST['return_url']) && isset($_REQUEST['return_desc']))
 
                // Expand/Collapse
                if ($_REQUEST['strings'] == 'expanded')
-                       echo "\t<li><a href=\"display.php?{$str}&amp;{$str2}&amp;strings=collapsed&amp;page=", 
+                       echo "\t<li><a href=\"display.php?{$get_str}&amp;{$sort_str}&amp;strings=collapsed&amp;page=",
                                urlencode($_REQUEST['page']), "\">{$lang['strcollapse']}</a></li>\n";
                else
-                       echo "\t<li><a href=\"display.php?{$str}&amp;{$str2}&amp;strings=expanded&amp;page=", 
+                       echo "\t<li><a href=\"display.php?{$get_str}&amp;{$sort_str}&amp;strings=expanded&amp;page=",
                                urlencode($_REQUEST['page']), "\">{$lang['strexpand']}</a></li>\n";
 
                // Create report
                // Create view and download
                if (isset($_REQUEST['query']) && isset($rs) && is_object($rs) && $rs->recordCount() > 0) {
                        // Report views don't set a schema, so we need to disable create view in that case
-                       if (isset($_REQUEST['schema'])) 
+                       if (isset($_REQUEST['schema']))
                                echo "\t<li><a href=\"views.php?action=create&amp;formDefinition=",
                                        urlencode($_REQUEST['query']), "&amp;{$misc->href}\">{$lang['strcreateview']}</a></li>\n";
                        echo "\t<li><a href=\"dataexport.php?query=", urlencode($_REQUEST['query']);
                }
 
                // Insert
-               if (isset($object) && (isset($subject) && $subject == 'table'))
+               if ($has_object && ($has_subject && $subject == 'table'))
                        echo "\t<li><a href=\"tables.php?action=confinsertrow&amp;table=",
                                urlencode($object), "&amp;{$misc->href}\">{$lang['strinsert']}</a></li>\n";
 
                // Refresh
-               echo "\t<li><a href=\"display.php?{$str}&amp;{$str2}&amp;strings=", urlencode($_REQUEST['strings']), 
+               echo "\t<li><a href=\"display.php?{$get_str}&amp;{$sort_str}&amp;strings=", urlencode($_REQUEST['strings']),
                        "&amp;page=" . urlencode($_REQUEST['page']),
                        "\">{$lang['strrefresh']}</a></li>\n";
                echo "</ul>\n";
        }
-       
+
        // If a table is specified, then set the title differently
        if (isset($_REQUEST['subject']) && isset($_REQUEST[$_REQUEST['subject']]))
                $misc->printHeader($lang['strtables']);
-       else    
+       else
                $misc->printHeader($lang['strqueryresults']);
 
        $misc->printBody();
                        break;
                case 'confdelrow':
                        doDelRow(true);
-                       break;                  
+                       break;
                default:
                        doBrowse();
                        break;