DBA_Relational
[ class tree: DBA_Relational ] [ index: DBA_Relational ] [ all elements ]

Source for file Toolbox.php

Documentation is available at Toolbox.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | Copyright (c) 2002-2003 Brent Cook                                        |
  5. // +----------------------------------------------------------------------+
  6. // | This library is free software; you can redistribute it and/or        |
  7. // | modify it under the terms of the GNU Lesser General Public           |
  8. // | License as published by the Free Software Foundation; either         |
  9. // | version 2.1 of the License, or (at your option) any later version.   |
  10. // |                                                                      |
  11. // | This library is distributed in the hope that it will be useful,      |
  12. // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
  13. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
  14. // | Lesser General Public License for more details.                      |
  15. // |                                                                      |
  16. // | You should have received a copy of the GNU Lesser General Public     |
  17. // | License along with this library; if not, write to the Free Software  |
  18. // | Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA|
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: Toolbox.php,v 1.6 2003/01/27 04:33:37 busterb Exp $
  22. //
  23. require_once 'PEAR.php';
  24.  
  25. /**
  26.  * Insert widgets into a quickform object suitable for updating a row in a DBA
  27.  * table.
  28.  *
  29.  * @author  Brent Cook <busterb@mail.utexas.edu>
  30.  * @version 0.19
  31.  * @access  public
  32.  * @package DBA
  33.  */
  34. function dbaToQuickform(&$form$schema$auxMeta)
  35. {
  36.     foreach ($schema as $name=>$meta{
  37.         if (isset($auxMeta['default'])) {
  38.             $defaults[$name$auxMeta['default'];
  39.         }
  40.         if (isset($auxMeta[$name])) {
  41.             $desc = isset($auxMeta[$name]['desc']?
  42.                         $auxMeta[$name]['desc'$name;
  43.             switch($meta[DBA_TYPE]{
  44.                 case DBA_INTEGER:
  45.                     if (isset($auxMeta[$name]['min']&&
  46.                         isset($auxMeta[$name]['max'])) {
  47.                         $form->addElement('select'$name$desc,
  48.                                           range($auxMeta[$name]['min'],
  49.                                           $auxMeta[$name]['max']));
  50.                     else {
  51.                         $form->addElement('text'$name$desc,
  52.                                           array('size'=>4'maxlength'=>4));
  53.                     }
  54.                     break;
  55.                 case DBA_VARCHAR:
  56.                     if ($meta[DBA_SIZE<= 60{
  57.                         $form->addElement('text'$name$desc,
  58.                                         array('size'=>$meta[DBA_SIZE]));
  59.                     else {
  60.                         $form->addElement('textarea'$name$desc,
  61.                                 array('rows'=>4'wrap'=>'soft''cols'=>45));
  62.                     }
  63.                     break;
  64.                 case DBA_BOOLEAN:
  65.                     $form->addElement('select'$name$desc,
  66.                                       array('yes'=>'Yes''no'=>'No'));
  67.                     break;
  68.                 case DBA_TEXT:
  69.                     $form->addElement('textarea'$name$desc,
  70.                                       array('rows'=>4'wrap'=>'soft''cols'=>45));
  71.                     break;
  72.             }
  73.         }
  74.     }
  75. }
  76.  
  77. /**
  78.  * Postprocess $_POST variables that were left by a form using addQuickformDBA
  79.  * @return array DBA row suitable for inserting into a DBA table
  80.  */
  81. function quickformToDBA($schema$auxMeta)
  82. {
  83.     foreach ($schema as $name=>$meta{
  84.         if (isset($auxMeta[$name]&& isset($_POST[$name])) {
  85.             if (($meta[DBA_TYPE== DBA_INTEGER&&
  86.                 isset($auxMeta['min'])) {
  87.                 $data[$name$_POST[$name$auxMeta['min'];
  88.             else {
  89.                 $data[$name$_POST[$name];
  90.             }
  91.         }
  92.     }
  93.     return $data;
  94. }
  95.  
  96. /**
  97.  * Generates a text table from a results set, a-la MySQL
  98.  *
  99.  * @param   array $rows 
  100.  * @param   array $fields list of fields to display
  101.  * @param   string $style style to display table in; 'oracle', 'mysql'
  102.  * @return  string text-formatted results set
  103.  */
  104. function formatTextTable($rows$fields = null$style 'oracle')
  105. {
  106.     $corner ($style == 'oracle'' ' '+';
  107.     $wall ($style == 'oracle'' ' '|';
  108.     $separator '';
  109.     $buffer '';
  110.  
  111.     if (is_array($rows&& sizeof($rows)) {
  112.  
  113.         if (is_null($fields)) {
  114.             $fields array_keys(current($rows));
  115.         }
  116.  
  117.         // get the maximum length of each field
  118.         foreach ($fields as $key=>$field{
  119.             $longest[$keystrlen($field+ 1;
  120.             foreach ($rows as $row{
  121.                 $rowLen strlen($row[$field]+ 1;
  122.                 if ($rowLen $longest[$key]{
  123.                     $longest[$key$rowLen;
  124.                 }
  125.             }
  126.         }
  127.  
  128.         // generate separator line
  129.         foreach ($longest as $length{
  130.             $separator .= "$corner-".str_repeat('-',$length);
  131.         }
  132.         $separator .= "$corner\n";
  133.  
  134.         $buffer ($style == 'oracle''' $separator;
  135.  
  136.         // print fields
  137.         foreach ($fields as $key=>$field{
  138.             $buffer .= "$wall ".str_pad($field$longest[$key]);
  139.         }
  140.         $buffer .= "$wall\n$separator";
  141.  
  142.         // print rows
  143.         foreach ($rows as $row{
  144.             foreach ($fields as $key=>$field{
  145.                 $buffer .= "$wall ".str_pad($row[$field],
  146.                         $longest[$key]);
  147.             }
  148.             $buffer .= "$wall\n";
  149.             $buffer .= ($style == 'oracle''' $separator;
  150.         }
  151.     }
  152.     return $buffer;
  153. }
  154.  
  155. /**
  156.  * Generates an HTML table from a results set
  157.  *
  158.  * This function uses custom CSS classes to define the table style.
  159.  * A recommended CSS is as follows:
  160.  *
  161.  * .dbatable {
  162.  *   border: solid;
  163.  *   border-color: gray;
  164.  *   border-collapse: collapse;
  165.  * }
  166.  * .dbatablefield {
  167.  *   font-weight: bold;
  168.  *   text-align: center;
  169.  *   color: black;
  170.  *   background-color: #CCCCFF;
  171.  *   border: solid;
  172.  *   border-color: gray;
  173.  *   border-width: thin;
  174.  * }
  175.  * .dbatablerow0 {
  176.  *   font-weight: normal;
  177.  *   color: black;
  178.  *   background-color: #FFFFFF;
  179.  *   border: solid;
  180.  *   border-color: gray;
  181.  *   border-width: thin;
  182.  * }
  183.  * .dbatablerow1 {
  184.  *   font-weight: normal;
  185.  *   color: black;
  186.  *   background-color: #F4F4F4;
  187.  *   border: solid;
  188.  *   border-color: gray;
  189.  *   border-width: thin;
  190.  * }
  191.  *
  192.  * @param   array $rows 
  193.  * @param   array $fields list of fields to display
  194.  * @return  string HTML-formatted results set
  195.  */
  196. function formatHtmlTable($rows$fields = null)
  197. {
  198.     if (is_numeric($rowsor is_string($rows)) return "$rows<br>";
  199.     if (is_array($rows&& sizeof($rows)) {
  200.         if (is_null($fields)) {
  201.             $fields array_keys(current($rows));
  202.         }
  203.         $buffer "<table class=\"dbatable\">\n";
  204.  
  205.         // print fields
  206.         $buffer .= "  <tr>\n";
  207.         foreach ($fields as $field{
  208.             $buffer .= "    <th class=\"dbatablefield\">&nbsp;&nbsp;"
  209.                        ."$field&nbsp;&nbsp;</th>\n";
  210.         }
  211.         $buffer .= "  </tr>\n";
  212.  
  213.         // print rows
  214.         $rowStyle = 0;
  215.         foreach ($rows as $row{
  216.             $buffer .= "  <tr>\n";
  217.             foreach ($fields as $field{
  218.                 $buffer .= "    <td class=\"dbatablerow$rowStyle\">&nbsp;&nbsp;"
  219.                            .$row[$field]."&nbsp;&nbsp;</td>\n";
  220.             }
  221.             $rowStyle = 1 - $rowStyle;
  222.             $buffer .= "  </tr>\n";
  223.         }
  224.  
  225.         $buffer .= "</table>\n";
  226.     }
  227.     return $buffer;
  228. }
  229. ?>

Documentation generated on Mon, 11 Mar 2019 14:59:55 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.