Source for file GuiList1.php

Documentation is available at GuiList1.php

  1. <?php
  2. /**
  3.  * 
  4.  * GuiList1.php
  5.  *
  6.  * Long description for file:
  7.  * Class to Display a generic Query, with sorting and active buttons
  8.  * USAGE:
  9.  *   See also GuiEditDevice_control.php, which manages the Buttons and POST/Submits.
  10.  *   The Constructor create the basic empty report with tailes. The Query()
  11.  *   builts the SQL statement, executes and sends back results.
  12.  *   Features common to other WebGUI parts are outsourced to the parent class WebCommon
  13.  *
  14.  * @package     FreeNAC
  15.  * @author      Sean Boran (FreeNAC Core Team)
  16.  * @copyright   2008 FreeNAC
  17.  * @license     http://www.gnu.org/copyleft/gpl.html   GNU Public License Version 3
  18.  * @version     SVN: $Id$
  19.  * @link        http://freenac.net
  20.  *
  21.  */
  22.  
  23.  
  24. class GuiList1 extends WebCommon 
  25. {
  26.   private $dynamic;           // See also WebCommon and Common
  27.  
  28.  
  29.   function __construct($rep_name=''$dynamic=false $debuglevel=1)
  30.   {
  31.     parent::__construct();     // See also WebCommon and Common
  32.     $this->logger->setDebugLevel($debuglevel);  // 3 for max debugging
  33.     $this->dynamic=$dynamic;
  34.     $this->debug(" __construct() $rep_name, debug=.$debuglevel1);
  35.     echo "<div id='GuiList1Title'><p>{$rep_name}</div>";
  36.   }
  37.  
  38.  
  39.   /*
  40.    * Setup a 'order by' form, that calls back the script that instantiated us
  41.    */
  42.   private function print_report_menu($limit$order$fields$searchby$searchstring
  43.   {
  44.     // Only create the sorting menu if this is a 'dynamic report'
  45.     if ($this->dynamic return;
  46.  
  47.     $this->debug("print_report_menu limit=$limit, order=$order, searchby=$searchby, searchstring=$searchstring,2);
  48.  
  49.     // Use GET rather than post?
  50.     //<form name="GuiList1" action="{$this->calling_script}" method="GET">
  51.     $output=<<<EOF
  52.     \n<form name="GuiList1" action="{$this->calling_script}" method="post">
  53.     <div id="GuiList1form1">
  54. EOF;
  55.        // Search fields
  56.        $text=<<<EOF
  57.     <ul>
  58.        <li>Search:<input type='text' value='$searchstring
  59.            name='searchstring' size='19' maxlength='20' /></li>
  60.        <li>Search Field:<SELECT NAME=searchby>\n
  61. EOF;
  62.        $output.= $text;
  63.        foreach ($fields as $field{
  64.          if ($searchby=="$field->table.$field->orgname"{
  65.            $output.="<OPTION SELECTED VALUE='" .$field->table ."." .$field->orgname "'>" $field->name ."</OPTION>\n";
  66.          else {
  67.            $output.="<OPTION VALUE='"          .$field->table ."." .$field->orgname "'>" $field->name ."</OPTION>\n";
  68.          }
  69.        }
  70.  
  71.        // Change button
  72.        $text=<<<EOF
  73.        </SELECT> </li>
  74.        <li><input class="bluebox" type='submit' name='change' value='Change' /></li>
  75.        </ul>
  76.     <ul>
  77.        <li>Max. records:<input type='text' value='$limit' name='sortlimit' size='11' maxlength='20' /></li>
  78.     </ul>
  79.        </div></form>
  80. EOF;
  81.        //<li><input class="bluebox" type='submit' name='change' value='Change'/></li>
  82.        $output.= $text;
  83.        return($output);
  84.   }
  85.  
  86.  
  87.  
  88.   /**
  89.    * Generic query report
  90.    * Parameters:
  91.    *   query($q, $limit, $order, $action_menu, $action_fieldname, $idx_name, $searchstring, $searchby, $action_confirm)
  92.    *   The (my)SQL query is built as:
  93.    *      $q WHERE $searchby LIKE '%$searchstring%' ORDER BY $order $order_dir LIMIT $limit
  94.    *   action_fieldname is the Title for the index column called idx_name
  95.    *   Buttons in column 1:
  96.    *     action_menu/action_confirm= array of Action button names & confirmation dialogs
  97.    */
  98.   public function query($q$limit=0$order=''$action_menu
  99.     $action_fieldname=''$idx_name$searchstring=''$searchby='',
  100.     $action_confirm=array('')$order_dir='DESC'$like='LIKE'
  101.   {
  102.     $conn=$this->getConnection();     //  make sure we have a DB connection
  103.     $_SESSION['report1_query']=$q;    // save for Report2, for re-use
  104.     
  105.     $output="<table id='GuiList1Table'>"// note the CSS id: do your formatting in CSS!
  106.  
  107.     try {
  108.       $rowcount=0;
  109.       $searchby=preg_replace('/^\./'''$searchby);  // kill leading dot, if any
  110.       $order   =preg_replace('/^\./'''$order);    
  111.       if ((strlen($searchby)>0&& (strlen($searchstring)>0) ) {
  112.         $searchby=$this->sqlescape($searchby);
  113.         $searchstring=$this->sqlescape($searchstring);   // strip/escape unwanted characters
  114.         if ($like == 'LIKE'
  115.           $q.=" WHERE $searchby LIKE '%$searchstring%' "
  116.         else if ($like == '='
  117.           $q.=" WHERE $searchby='$searchstring"
  118.         else
  119.           $q.=" WHERE $searchby LIKE '%$searchstring%' "
  120.       
  121.  
  122.       $order=$this->sqlescape($order);
  123.       $order_dir=$this->sqlescape($order_dir);
  124.       $limit=$this->sqlescape($limit);
  125.       $this->debug("GuiList1.php->query() limit=$limit, order=$order searchby=$searchby, searchstring=$searchstring"2);
  126.  
  127.       if (strlen($order)>0$q.=" ORDER BY $order $order_dir"
  128.       if ($limit>0)         $q.=" LIMIT $limit"
  129.  
  130.       $this->debug("$q"3);
  131.  
  132.       $res $conn->query($q);
  133.       if ($res === FALSE)
  134.         throw new DatabaseErrorException("Query=$q;.$conn->error);
  135.  
  136.       if is_array($action_menu) )
  137.         $output.="<th>Action</th>";         // field for menu icons
  138.  
  139.       // Title: Grab the list of field names
  140.       $new_order_dir $order_dir=='DESC' 'ASC' 'DESC';   // next click reverse order
  141.       $fields=$res->fetch_fields();
  142.       foreach ($fields as $field{
  143.         # Simple titles:
  144.         #$output.="<th>". $field->name . "</th>";
  145.         # other attributes: table, max_length, flags, type
  146.         // Titles with clickable sort links:
  147.         $output.=<<<TXT
  148. <th  class='light'>
  149. <a href="{$this->calling_script}?sortlimit={$limit}&sortby={$field->table}.{$field->orgname}&order_dir={$new_order_dir}&searchstring=$searchstring&searchby=$searchby&change=Change"  title="Sort by this column">$field->name</a>
  150. </th>
  151. TXT;
  152.       
  153.       $output.= "<tr>";
  154.  
  155.       // detail lines
  156.       $shade=false;
  157.       while (($row $res->fetch_assoc()) !== NULL{
  158.         $rowcount++;
  159.         //$this->logit("save: " .$row["$action_fieldname"]); 
  160.         $indices[]=$row["$action_fieldname"];   // remember index values
  161.         $output.= ($shade"<tr class='light'>" "<tr class='dark'>";
  162.  
  163.         if is_array($action_menu) ) {
  164.           if strlen($action_fieldname>// add a field for menu icons?
  165.             $row_id=$row[$action_fieldname];
  166.  
  167.           #Old, URL method
  168.           #$output.="<td><a href='{$this->calling_script}?action={$action}&action_fieldname={$action_fieldname}&action_idx={$row_id}'>{$action}</a></td>";
  169.           #$output.="<td>{$action_menu}</td>";      
  170.  
  171.           $output.='<td>';
  172.           foreach ($action_menu as $key => $menu_item{
  173.             
  174.             // add a Confirmation dialog to this action button?
  175.             if (isset($action_confirm[$key]&& strlen($action_confirm[$key]0)   // Get the confirmation dialog text.
  176.               $confirm="onClick=\"javascript:return confirm('" .$action_confirm[$key."')\"";
  177.             else
  178.               $confirm='';
  179.  
  180.             $output.=<<<EOF
  181.  <form name='Action1' action='{$this->calling_script}' method='post'>
  182.      <input type=hidden name='action_idxname'   value='{$idx_name}' />
  183.      <input type=hidden name='action_fieldname' value='{$action_fieldname}' />
  184.      <input type=hidden name='action_idx'       value='{$row_id}' />
  185.      <input class="greybox" type='submit' name='action' value='{$menu_item}$confirm/>
  186.  </form>
  187. EOF;
  188.           
  189.           $output.='</td>';
  190.         }
  191.  
  192.         foreach ($fields as $field{
  193.           $fname=$field->name;
  194.           #$output.= "<td align='center'>" . $row[$fname] . "</td>";
  195.           ## Escape HTML charaacters in data, as it will either disappear or cause strange behaviour
  196.           $output.= "<td align='center'>" $this->htmlescape($row[$fname]"</td>";
  197.         
  198.         $output.= "</tr>";
  199.         $shade !$shade;  // toggle shading for the next line
  200.       }                    // while row
  201.  
  202.       if (empty($indices))
  203.         $_SESSION['report1_indices']=serialize($indices);  // remember index values
  204.  
  205.       // Inform the user if no data was returned
  206.       if ( ($rowcount)=={
  207.         #$output.= "<br/><tr><td colspan='4' align='center' class='text16red'>The report is empty</td></tr><tr></tr><br/>";
  208.         $output"<table id='GuiList1Table'><tr><td colspan='4' align='center' class='text16red'>The report is empty</td></tr><br/>";
  209.  
  210.       else {
  211.         // There is data, show the Limit/sortOrder menus
  212.         $output.= $this->print_report_menu($limit$order$fields$searchby$searchstring);
  213.         $output.= '<br>' .$rowcount .' matching result(s) found:</br>';
  214.       }
  215.  
  216.       $res->close();
  217.  
  218.     catch (Exception $e{
  219.       if (isset($conn))
  220.         $conn->close();
  221.       throw $e;
  222.     }
  223.  
  224.     // close table, send back text
  225.     $output.="</table>";
  226.     return($output);
  227.   }
  228.  
  229.  
  230.  
  231. // class
  232.  
  233.  
  234.  
  235. if isset($_POST['submit']) ) {             // form submit, check fields
  236. ## Initialise (standard header for all modules)
  237.   dir(dirname(__FILE__))set_include_path("./:../");
  238.   require_once('webfuncs.inc');
  239.   $logger=Logger::getInstance();
  240.   $logger->setDebugLevel(1);
  241.  
  242.   $logger->debug("GuiList1 main -submit");
  243.   #echo handle_submit();
  244.  
  245. else {    
  246.   # Do nothing, we've been included.
  247. }
  248.  
  249. ?>

Documentation generated on Mon, 17 Nov 2008 01:10:36 +0100 by phpDocumentor 1.4.0