Source for file dblist.php

Documentation is available at dblist.php

  1. <?php
  2. /**
  3. * generic database list/export class
  4. *
  5. @author    Stuart Prescott
  6. @copyright  Copyright Stuart Prescott
  7. @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  8. @version    $Id$
  9. @package    Bumblebee
  10. @subpackage FormsLibrary
  11. */
  12.  
  13. /** Load ancillary functions */
  14. require_once 'inc/typeinfo.php';
  15.  
  16. /** uses an OutputFormatter to format the data */
  17. require_once 'inc/formslib/outputformatter.php';
  18. /** export formatting codes */
  19. require_once 'inc/exportcodes.php';
  20.  
  21. /**
  22. * generic database list/export class
  23. *
  24. @package    Bumblebee
  25. @subpackage FormsLibrary
  26. @todo //TODO: codedoc
  27. */
  28. class DBList {
  29.   var $restriction;
  30.   var $unionrestriction = array();
  31.   var $join = array();
  32.   var $order;
  33.   var $group;
  34.   var $union;
  35.   var $returnFields;
  36.   var $omitFields = array();
  37.   var $fieldOrder;
  38.   var $formatter;
  39.   var $distinct = 0;
  40.   var $table;
  41.   var $data;
  42.   var $formatdata;
  43.   var $outputFormat = EXPORT_FORMAT_CUSTOM;
  44.   var $breakfield;
  45.   var $fatal_sql = 1;
  46.  
  47.   /**
  48.   *  Create a new DBList object
  49.   *
  50.   * @param string $table   name of (primary) table to be interrogated
  51.   * @param mixed  $returnFields   single column name or list of column names to be returned from the db
  52.   * @param mixed  $restriction    single or list of restrictions to be 'AND'ed together
  53.   * @param boolean $distinct      select only DISTINCT rows from the db
  54.   */
  55.   function DBList($table$returnFields$restriction$distinct=false{
  56.     $this->table = $table;
  57.     $this->distinct = $distinct;
  58.     if (is_array($restriction)) {
  59.       $this->restriction = $restriction;
  60.     else {
  61.       $this->restriction = array($restriction);
  62.     }
  63.     if (is_array($returnFields)) {
  64.       $this->returnFields = $returnFields;
  65.     else {
  66.       $this->returnFields = array($returnFields);
  67.     }
  68.     // calculate some values in advance and cache them to speed this class with large datasets
  69.     $this->numReturnFields = count($this->returnFields);
  70.     $this->returnFieldAliases array();
  71.     $this->returnFieldCells array();
  72.     for ($i=0$i<$this->numReturnFields$i++{
  73.       $this->returnFieldAliases[$this->returnFields[$i]->alias$i;
  74.       $cell array();
  75.       $cell['format'$this->returnFields[$i]->format;
  76.       $cell['width'=  isset($this->returnFields[$i]->width$this->returnFields[$i]->width 10;
  77.       $this->returnFieldCells[$this->returnFields[$i]->alias$cell;
  78.     }
  79.   }
  80.  
  81.   /**
  82.   * Fill the object from the database
  83.   */
  84.   function fill({
  85.     // construct the query
  86.     if (is_array($this->union&& count($this->union)) {
  87.       $union array();
  88.       foreach ($this->union as $u{
  89.         $u->restriction array_merge($u->restriction$this->unionrestriction);
  90.         $union[$u->_getSQLsyntax();
  91.       }
  92.       $q '('.join($union') UNION ALL (').')';
  93.       $q .= (is_array($this->order' ORDER BY '.join($this->order,', ''');
  94.       $q .= (is_array($this->group' GROUP BY '.join($this->group,', ''');
  95.     else {
  96.       $q $this->_getSQLsyntax();
  97.     }
  98.     $sql db_get($q$this->fatal_sql);
  99.     $data array();
  100.     while ($g db_fetch_array($sql)) {
  101.       $data[$g;
  102.     }
  103.     if (isset($this->manualGroup&& $this->manualGroup != ''{
  104.       $sumdata array();
  105.       $row=0;
  106.       $numrows count($data);
  107.       while ($row $numrows{
  108.         $current $data[$row][$this->manualGroup];
  109.         $currentRow $data[$row];
  110.         $sums array();
  111.         foreach ($this->manualSum as $col{
  112.           $sums[$col0;
  113.         }
  114.         while ($row $numrows && $data[$row][$this->manualGroup== $current{
  115.           foreach ($this->manualSum as $col{
  116.             $sums[$col+= $data[$row][$col];
  117.           }
  118.           $row++;
  119.         }
  120.         foreach ($this->manualSum as $col{
  121.           $currentRow[$col$sums[$col];
  122.         }
  123.         $sumdata[$currentRow;
  124.       }
  125.       $this->data = $sumdata;
  126.     else {
  127.       $this->data = $data;
  128.     }
  129.   }
  130.  
  131.   /**
  132.   * generate the appropriate SQL syntax for this query
  133.   *
  134.   * @return string SQL query for this object
  135.   */
  136.   function _getSQLsyntax({
  137.     global $TABLEPREFIX;
  138.     $fields array();
  139.     foreach ($this->returnFields as $v{
  140.       $fields[$v->name .(isset($v->alias' AS '.$v->alias '');
  141.     }
  142.     $q 'SELECT '.($this->distinct ? 'DISTINCT ' ' ')
  143.           .join($fields', ')
  144.         .' FROM '.$TABLEPREFIX.$this->table.' AS '.$this->table.' ';
  145.     foreach ($this->join as $t{
  146.       $q .= ' LEFT JOIN '.$TABLEPREFIX.$t['table'].' AS '.(isset($t['alias']$t['alias'$t['table'])
  147.            .' ON '.$t['condition'];
  148.     }
  149.     $q .= ' WHERE 'join($this->restriction' AND ');
  150.     $q .= (is_array($this->order' ORDER BY '.join($this->order,', ''');
  151.     $q .= (is_array($this->group' GROUP BY '.join($this->group,', ''');
  152.     return $q;
  153.   }
  154.  
  155.   /**
  156.   * format the list using the designated formats into another list
  157.   */
  158.   function formatList({
  159.     //preDump($this->omitFields);
  160.     $this->formatdata = array();
  161.     if (is_array($this->fieldOrder)) {
  162.       $this->fieldOrder = array();
  163.       foreach ($this->returnFields as $f{
  164.         $this->fieldOrder[$f->alias;
  165.       }
  166.     }
  167.     $numdata count($this->data);
  168.     for ($i=0$i<$numdata$i++{
  169.       $this->formatdata[$i$this->format($this->data[$i]);
  170.     }
  171.   }
  172.  
  173.   /**
  174.   * format a row of data
  175.   * @param array $data   name=>value pairs of data
  176.   * @return string   formatter line of data
  177.   */
  178.   function format($data/*, $isHeader=false*/{
  179.     $d array();
  180.     foreach ($this->fieldOrder as $f{
  181.       // performance of array_key_exists() can be a problem
  182.       // see  http://bugs.php.net/37563
  183.       //if (! array_key_exists($f, $this->omitFields)) {
  184.       if (isset($this->omitFields[$f])) {
  185.         $d[$f$data[$f];
  186.       }
  187.     }
  188.     if (EXPORT_FORMAT_CSV $this->outputFormat)
  189.       return join(preg_replace(array('/"/',     '/^(.*,.*)$/'),
  190.                                array('\\"',   '"$1"'       )$d)',');
  191.     if (EXPORT_FORMAT_TAB $this->outputFormat)
  192.         return join(preg_replace("/^(.*\t.*)$/"'"$1"'$d)"\t");
  193.     if (EXPORT_FORMAT_USEARRAY $this->outputFormat)
  194.         return $this->_makeArray($d/*, $isHeader*/);
  195.  
  196.     return $this->formatter->format($d);
  197.   }
  198.  
  199.   /**
  200.   * format a header row
  201.   * @return string   formatter header row
  202.   */
  203.   function outputHeader({
  204.     $d array();
  205.     foreach ($this->returnFields as $f{
  206.       $d[$f->alias$f->heading;
  207.     }
  208.     return $this->format($d/*, true*/);
  209.   }
  210.  
  211.   /**
  212.   * create a row of data with the value and some formatting data for use by the Array/HTML/PDF Export
  213.   * @return array   list of array(value=>$value, format=>$format, width=>$width)
  214.   */
  215.   function _makeArray(&$d/*, $isHeader=false*/{
  216.     $row array();
  217.     foreach ($d as $alias => $val{
  218.       $cell $this->returnFieldCells[$alias];
  219.       // use the cached reverse mapping of field aliases/names back to the field number
  220.       //$f = $this->returnFields[$this->returnFieldAliases[$alias]];
  221. //       $cell = array();
  222.       //$cell['value'] = $this->formatVal($val, $f->format, $isHeader);
  223.       $cell['value'$val;
  224. //       $cell['format'] = $f->format;
  225. //       $cell['width'] =  isset($f->width) ? $f->width : 10;
  226.       $row[$cell;
  227.     }
  228.     return $row;
  229.   }
  230.  
  231.  /**
  232.   * Create a set of OutputFormatter objects to handle the display of this object.
  233.   *
  234.   * @param string $f1    sprintf format (see PHP manual)
  235.   * @param array $v1     array of indices that will be used to fill the fields in the sprintf format from a $data array passed to the formatter later.
  236.   */
  237.   function setFormat($f$v{
  238.     $this->formatter = new OutputFormatter($f$v);
  239.   }
  240.  
  241. // class DBList
  242.  
  243.  
  244. ?>

Documentation generated on Tue, 06 Mar 2007 10:01:19 +0000 by phpDocumentor 1.3.0