Source for file arrayexport.php

Documentation is available at arrayexport.php

  1. <?php
  2. /**
  3. * Construct an array for exporting the data
  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 Export
  11. *
  12. *  path (bumblebee root)/inc/export/arrayexport.php
  13. */
  14.  
  15. /** Load ancillary functions */
  16. require_once 'inc/typeinfo.php';
  17.  
  18. require_once 'inc/bb/configreader.php';
  19.  
  20. /** constants for defining export formatting and codes */
  21. require_once 'inc/exportcodes.php';
  22.  
  23. /**
  24. * Construct an array for exporting the data
  25. *
  26. * Create a monolithic array with all the data for export. The array is an
  27. * intermediary form for creating PDF and HTML tables of data.
  28. *
  29. @package    Bumblebee
  30. @subpackage Export
  31. */
  32. class ArrayExport {
  33.   /** @var DBList  raw data list */
  34.   var $dblist;
  35.   /** @var string  field name on which the report should be broken into sections */
  36.   var $breakfield;
  37.   /** @var         unknown? unused? */
  38.   var $exporter;
  39.   /** @var array   data array of exported data */
  40.   var $export = array();
  41.   /** @var string  header for the report */
  42.   var $header;
  43.   /** @var string  report Author (report metadata) */
  44.   var $author = 'Bumblebee';
  45.   /** @var string  report Creator (report metadata) */
  46.   var $creator = 'Bumblebee Instrument Management System : bumblebeeman.sf.net';
  47.   /** @var string  report Subject (report metadata) */
  48.   var $subject = 'Instrument and consumable usage report';
  49.   /** @var string  report Keywords (report metadata) */
  50.   var $keywords = 'instruments, consumables, usage, report, billing, invoicing';
  51.   /** @var array   list of subtotals for each section of the report for fields that are totalled  */
  52.   var $_totals;
  53.   /** @var boolean calculate column totals */
  54.   var $_doingTotalCalcs = false;
  55.  
  56.   /**
  57.   *  Create a new array export object to be used by both HTML and PDF export
  58.   *
  59.   * @param DBList &$dblist data to be exported (passed by reference for efficiency only)
  60.   * @param string $breakfield   name of field to use to break report into sections
  61.   */
  62.   function ArrayExport(&$dblist$breakfield{
  63.     $this->dblist   =$dblist;
  64.     $this->breakfield = $breakfield;
  65.   }
  66.  
  67.   /**
  68.   *  Parsed the exported data and create the marked-up array of data
  69.   */
  70.   function makeExportArray({
  71.     $ea array();   //export array
  72.     $ea[array('type' => EXPORT_REPORT_START,
  73.                   'data' => '');
  74.     $ea[array('type' => EXPORT_REPORT_HEADER,
  75.                   'data' => $this->header);
  76.     $entry 0;
  77.     $numcols count($this->dblist->formatdata[0]);
  78.     $breakfield $this->breakfield;
  79.     $breakReport (!empty($breakfield&& isset($this->dblist->data[$entry][$breakfield]));
  80.     //echo $breakReport ? 'Breaking' : 'Not breaking';
  81.     $numrows count($this->dblist->formatdata);
  82.     while ($entry $numrows{
  83.       //$this->log('Row: '.$entry);
  84.       $this->_resetTotals();
  85.       $ea[array('type' => EXPORT_REPORT_SECTION_HEADER,
  86.                     'data' => $this->_sectionHeader($this->dblist->data[$entry]),
  87.                     'metadata' => $this->_getColWidths($numcols$entry));
  88.       if ($breakReport{
  89.         $initial $this->dblist->data[$entry][$breakfield];
  90.       }
  91.       $ea[array('type' => EXPORT_REPORT_TABLE_START,
  92.                     'data' => '');
  93.       $ea[array('type' => EXPORT_REPORT_TABLE_HEADER,
  94.                     'data' => $this->dblist->outputHeader());
  95.       while ($entry $numrows
  96.                 && ($breakReport
  97.                     || $initial == $this->dblist->data[$entry][$breakfield]) ) {
  98.         $ea[array('type' => EXPORT_REPORT_TABLE_ROW,
  99.                       'data' => $this->_formatRowData($this->dblist->formatdata[$entry]));
  100.         $this->_incrementTotals($this->dblist->formatdata[$entry]);
  101.         //if ($entry>0) unset($this->dblist->formatdata[$entry]);  //FIXME: this can save a couple of MB
  102.         $entry++;
  103.       }
  104.       if ($this->_doingTotalCalcs{
  105.         $ea[array('type' => EXPORT_REPORT_TABLE_TOTAL,
  106.                       'data' => $this->_getTotals());
  107.       }
  108.       $ea[array('type' => EXPORT_REPORT_TABLE_END,
  109.                     'data' => '');
  110.     }
  111.     $ea[array('type' => EXPORT_REPORT_END,
  112.                   'data' => '');
  113.     $ea['metadata'$this->_getMetaData();
  114.     //preDump($ea);
  115.     $this->export =$ea;
  116.   }
  117.  
  118.   /**
  119.   * create the section header
  120.   *
  121.   * @param array  current data row
  122.   * @return string header string
  123.   */
  124.   function _sectionHeader($row{
  125.     $s '';
  126.     if (empty($this->breakfield)) {
  127.       //$s .= $this->header;
  128.     else {
  129.       $s .= $row[$this->breakfield];
  130.     }
  131.     return $s;
  132.   }
  133.  
  134.   /**
  135.   * get the column widths for the columns (if defined)
  136.   *
  137.   * @param integer  number of columns
  138.   * @param array    a row from the table
  139.   * @return array   number of columns and a picture of the column widths spec
  140.   */
  141.   function _getColWidths($numcols$entry{
  142.     $columns array();
  143.     foreach ($this->dblist->formatdata[$entryas $f{
  144.       $columns[$f['width'];
  145.     }
  146.     return array(
  147.                   'numcols' => $numcols,
  148.                   'colwidths' => $columns
  149.                 );
  150.   }
  151.  
  152.   /**
  153.   * create an array of metadata to include in the output
  154.   *
  155.   * @return array  key => value metadata
  156.   */
  157.   function _getMetaData({
  158.     return array(
  159.                   'author'  => $this->author,
  160.                   'creator' => $this->creator,
  161.                   'title'   => $this->header,
  162.                   'keywords' => $this->keywords,
  163.                   'subject' => $this->subject
  164.                 );
  165.   }
  166.  
  167.   /**
  168.   * reset the column subtotals to 0
  169.   */
  170.   function _resetTotals({
  171.     foreach ($this->dblist->formatdata[0as $key => $val{
  172.       $this->_totals[$key$val;
  173.       if ($val['format'EXPORT_CALC_TOTAL{
  174.         $this->_totals[$key]['value'0;
  175.         $this->_doingTotalCalcs = true;
  176.       else {
  177.         $this->_totals[$key]['value''';
  178.       }
  179.     }
  180.   }
  181.  
  182.   /**
  183.   * increment each column subtotal
  184.   */
  185.   function _incrementTotals($row{
  186.     if ($this->_doingTotalCalcsreturn;
  187.     foreach ($row as $key => $val{
  188.       if ($val['format'EXPORT_CALC_TOTAL{
  189.         $this->_totals[$key]['value'+= $val['value'];
  190.       }
  191.     }
  192.   }
  193.  
  194.   /**
  195.   * get the column subtotals
  196.   */
  197.   function _getTotals({
  198.     $total $this->_totals;
  199.     foreach ($total as $key => $val{
  200.       if ($val['format'EXPORT_CALC_TOTAL{
  201.         $total[$key]['value'$this->_formatVal($val['value'],$val['format']);
  202.       }
  203.     }
  204.     return $total;
  205.   }
  206.  
  207.   /**
  208.   * format a row of data using the formmatting information defined
  209.   *
  210.   * @param array  &$row   data row
  211.   * @return array   formatted data row
  212.   */
  213.   function _formatRowData(&$row{
  214.     $newrow array();
  215.     foreach ($row as $key => $val{
  216.       $newrow[$key$val;
  217.       $newrow[$key]['value'$this->_formatVal($val['value']$val['format']);
  218.     }
  219.     return $newrow;
  220.   }
  221.  
  222.   /**
  223.   * format a data value according to the defined rules for decimal places and currency
  224.   *
  225.   * @param string  $val   value to be formatted
  226.   * @return string formatted value
  227.   */
  228.   function _formatVal($val$format{
  229.     $conf ConfigReader::getInstance();
  230.     switch ($format EXPORT_HTML_NUMBER_MASK{
  231.       case EXPORT_HTML_MONEY:
  232.         $val currencyFormatter($val);
  233.         break;
  234.       case EXPORT_HTML_INTEGER:
  235.         $val numberFormatter($val0);
  236.         break;
  237.       case EXPORT_HTML_DECIMAL_1:
  238.         $val numberFormatter($val1);
  239.         break;
  240.       case EXPORT_HTML_DECIMAL_2:
  241.         $val numberFormatter($val2);
  242.         break;
  243.       default:
  244.         //echo ($format& EXPORT_HTML_NUMBER_MASK).'<br/>';
  245.     }
  246.     if ($format EXPORT_HTML_DATETIME{
  247.       $date new SimpleDate($val);
  248.       $val $date->getShortDateTimeString();
  249.     elseif ($format EXPORT_HTML_DATE{
  250.       $date new SimpleDate($val);
  251.       $val $date->getShortDateString();
  252.     elseif ($format EXPORT_HTML_TIME{
  253.       $time new SimpleTime($val);
  254.       $val $time->getShortString();
  255.     }
  256.     return $val;
  257.   }
  258.  
  259.   /**
  260.   * join another ArrayExport object into this one.
  261.   *
  262.   * @param ArrayExport &$ea  ArrayExport object to be appended to this one
  263.   */
  264.   function appendEA(&$ea{
  265.     $this->export = array_merge($this->export$ea->export);
  266.   }
  267.  
  268.  
  269. // class ArrayExport
  270.  
  271. ?>

Documentation generated on Tue, 06 Mar 2007 10:00:35 +0000 by phpDocumentor 1.3.0