Source for file htmlexport.php

Documentation is available at htmlexport.php

  1. <?php
  2. /**
  3. * Construct an HTML export from array representation
  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/htmlexport.php
  13. */
  14.  
  15. /** Load ancillary functions */
  16. require_once 'inc/typeinfo.php';
  17.  
  18.  
  19. /** constants for defining export formatting and codes */
  20. require_once 'inc/exportcodes.php';
  21.  
  22. /**
  23. * Construct an HTML export from array representation
  24. *
  25. @package    Bumblebee
  26. @subpackage Export
  27. */
  28. class HTMLExport {
  29.   /** @var string       html-rendered data   */
  30.   var $export;
  31.   /** @var boolean      export the data as one big table with header rows between sections  */
  32.   var $bigtable = true;
  33.   /** @var ArrayExport  data to export    */
  34.   var $ea;
  35.   /** @var string       header to the report  */
  36.   var $header;
  37.  
  38.   /**
  39.   *  Create the HTMLExport object
  40.   *
  41.   * @param ArrayExport  &$exportArray 
  42.   */
  43.   function HTMLExport(&$exportArray{
  44.     $this->ea =$exportArray;
  45.   }
  46.  
  47.   /**
  48.   * convert the 2D array into an HTML table representation of the data
  49.   */
  50.   function makeHTMLBuffer({
  51.     //$this->log('Making HTML representation of data');
  52.     $ea =$this->ea->export;
  53.     $eol "\n";
  54.     $metaData $ea['metadata'];
  55.     unset($ea['metadata']);
  56.     $buf '';
  57.     $numrows count($ea);
  58.     for ($i=0$i<$numrows$i++{
  59.       if ($this->bigtable{
  60.         switch ($ea[$i]['type']{
  61.           case EXPORT_REPORT_START:
  62.             $buf .= '<div id="bumblebeeExport">';
  63.             break;
  64.           case EXPORT_REPORT_END:
  65.             $buf .= '</div>';
  66.             break;
  67.           case EXPORT_REPORT_HEADER:
  68.             $buf .= '<div class="exportHeader">'.xssqw($ea[$i]['data']).'</div>'.$eol;
  69.             $this->header = $ea[$i]['data'];
  70.             break;
  71.           case EXPORT_REPORT_SECTION_HEADER:
  72.             $buf .= '<div class="exportSectionHeader">'.xssqw($ea[$i]['data']).'</div>'.$eol;
  73.             break;
  74.           case EXPORT_REPORT_TABLE_START:
  75.             $tableMetaData $ea[$i]['metadata'];
  76.             $numcols $tableMetaData['numcols'];
  77.             $buf .= '<table class="exportdata">'.$eol;
  78.             break;
  79.           case EXPORT_REPORT_TABLE_END:
  80.             $buf .= '</table>'.$eol;
  81.             break;
  82.           case EXPORT_REPORT_TABLE_HEADER:
  83.             $buf .= '<tr class="header">'
  84.                         .$this->_formatRowHTML($ea[$i]['data']true)
  85.                     .'</tr>'.$eol;
  86.             break;
  87.           case EXPORT_REPORT_TABLE_TOTAL:
  88.             $buf .= '<tr class="totals">'
  89.                         .$this->_formatRowHTML($ea[$i]['data'])
  90.                     .'</tr>'.$eol;
  91.             break;
  92.           case EXPORT_REPORT_TABLE_FOOTER:
  93.             $buf .= '<tr class="footer">'
  94.                         .$this->_formatRowHTML($ea[$i]['data'])
  95.                     .'</tr>'.$eol;
  96.             break;
  97.           case EXPORT_REPORT_TABLE_ROW:
  98.             $buf .= '<tr>'
  99.                         .$this->_formatRowHTML($ea[$i]['data'])
  100.                     .'</tr>'.$eol;
  101.             break;
  102.         }
  103.       else {
  104.         switch ($ea[$i]['type']{
  105.           case EXPORT_REPORT_START:
  106.             $buf .= '<div id="bumblebeeExport">';
  107.             break;
  108.           case EXPORT_REPORT_END:
  109.             $buf .= '</table></div>';
  110.             break;
  111.           case EXPORT_REPORT_HEADER:
  112.             $buf .= '<div class="exportHeader">'.xssqw($ea[$i]['data']).'</div>'.$eol;
  113.             $buf .= '<table class="exportdata">'.$eol;
  114.             break;
  115.           case EXPORT_REPORT_SECTION_HEADER:
  116.             $tableMetaData $ea[$i]['metadata'];
  117.             $numcols $tableMetaData['numcols'];
  118.             $buf .= '<tr class="exportSectionHeader"><td colspan="'.$numcols.'" class="exportSectionHeader">'
  119.                         .xssqw($ea[$i]['data']).'</td></tr>'.$eol;
  120.             break;
  121.           case EXPORT_REPORT_TABLE_START:
  122.             break;
  123.           case EXPORT_REPORT_TABLE_END:
  124.             break;
  125.           case EXPORT_REPORT_TABLE_HEADER:
  126.             $buf .= '<tr class="header">'
  127.                         .$this->_formatRowHTML($ea[$i]['data']true)
  128.                     .'</tr>'.$eol;
  129.             break;
  130.           case EXPORT_REPORT_TABLE_FOOTER:
  131.             $buf .= '<tr class="footer">'
  132.                         .$this->_formatRowHTML($ea[$i]['data'])
  133.                     .'</tr>'.$eol;
  134.             break;
  135.           case EXPORT_REPORT_TABLE_TOTAL:
  136.             $buf .= '<tr class="totals">'
  137.                         .$this->_formatRowHTML($ea[$i]['data'])
  138.                     .'</tr>'.$eol;
  139.             break;
  140.           case EXPORT_REPORT_TABLE_ROW:
  141.             $buf .= '<tr>'
  142.                         .$this->_formatRowHTML($ea[$i]['data'])
  143.                     .'</tr>'.$eol;
  144.             break;
  145.         }
  146.       }
  147.       # unset($ea[$i]); //FIXME: try to free memory as we are using it
  148.     }
  149.     $this->export =$buf;
  150.   }
  151.  
  152.   /**
  153.   * generate the HTML for a row
  154.   *
  155.   * @return string row
  156.   */
  157.   function _formatRowHTML($row$isHeader=false{
  158.     $b '';
  159.     $numfields count($row);  //FIXME: can this be cached?
  160.     for ($j=0$j<$numfields$j++{
  161.       $b .= $this->_formatCellHTML($row[$j]$isHeader);
  162.     }
  163.     return $b;
  164.   }
  165.  
  166.   /**
  167.   * generate the HTML for a single cell
  168.   *
  169.   * @return string cell
  170.   */
  171.   function _formatCellHTML($d$isHeader{
  172.     $t '';
  173.     $val $d['value'];
  174.     if ($isHeader{
  175.       switch($d['format'EXPORT_HTML_ALIGN_MASK{
  176.         case EXPORT_HTML_CENTRE:
  177.           $align='center';
  178.           break;
  179.         case EXPORT_HTML_LEFT:
  180.           $align='left';
  181.           break;
  182.         case EXPORT_HTML_RIGHT:
  183.           $align='right';
  184.           break;
  185.         default:
  186.           $align='';
  187.       }
  188.       $align ($align!='' 'align='.$align '');
  189.       $t .= '<td '.$align.'>'.htmlentities($valENT_QUOTES'UTF-8').'</td>';
  190.     else {
  191.       $t .= '<th>'.htmlentities($val).'</th>';
  192.     }
  193.     return $t;
  194.   }
  195.  
  196.   /**
  197.   * embed the html within a blank page to create the report in a separate window
  198.   *
  199.   * @return string  html snippet that will open a new window with the html report
  200.   * @todo //TODO: potential memory hog (stores HTML output in three places at once)
  201.   */
  202.   function wrapHTMLBuffer({
  203.     $conf ConfigReader::getInstance();
  204.     $filename $conf->value('export''htmlWrapperFile');
  205.     $fd fopen($filename'r');
  206.     $contents fread($fdfilesize ($filename));
  207.     fclose($fd);
  208.     $title T_('Data export');
  209.     $table preg_replace('/\$/''&#'.ord('$').';'$this->export);
  210.     $contents preg_replace('/__TITLE__/'$title$contents);
  211.     $contents preg_replace('/__BASEPATH__/'$conf->BasePath$contents);
  212.     //return $contents;
  213.     //preDump($contents);
  214.     $contents preg_replace('/__CONTENTS__/'$table$contents);
  215.     //encode the HTML so that it doesn't get interpreted by the browser and cause big problems
  216.     //the PHP function rawurlencode() can be reversed by the JavaScript function unescape()
  217.     //which is then a convenient pairing to use rather than replacing everything manually.
  218.     $enchtml rawurlencode($contents);
  219.     $jsbuf '<script type="text/javascript">
  220. <!--
  221.   function BBwriteAll(data) {
  222.     bboutwin = window.open("", "bumblebeeOutput", "");
  223.     bboutwin.document.write(unescape(data));
  224.     bboutwin.document.close();
  225.   }
  226.  
  227.   data = "'.$enchtml.'";
  228.  
  229.   BBwriteAll(data);
  230.  
  231. //-->
  232. </script><a href="javascript:BBwriteAll(data)">'.T_('Open Window').'</a>';
  233.     return $jsbuf;
  234.   }
  235.  
  236.  
  237. // class HTMLExport
  238.  
  239. ?>

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