Source for file bufferedaction.php

Documentation is available at bufferedaction.php

  1. <?php
  2. /**
  3. * Primitive class that allows output to be suppressed
  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 Actions
  11. *
  12. *  path (bumblebee root)/inc/actions/bufferedaction.php
  13. */
  14.  
  15. /** Load ancillary functions */
  16. require_once 'inc/typeinfo.php';
  17.  
  18. require_once 'inc/bb/configreader.php';
  19.  
  20. /** status codes for success/failure of database actions */
  21. require_once 'inc/statuscodes.php';
  22. /** parent object */
  23. require_once 'inc/actions/actionaction.php';
  24.  
  25. /**
  26. * Primitive class that allows output to be suppressed
  27. @package    Bumblebee
  28. @subpackage Actions
  29. */
  30. class BufferedAction extends ActionAction  {
  31.   /**
  32.   * data stream to be buffered then output
  33.   * @var string 
  34.   */
  35.   var $bufferedStream;
  36.   /**
  37.   * filename to suggest to the browser for saving the stream
  38.   * @var string 
  39.   */
  40.   var $filename;
  41.   /**
  42.   * error message (if any) for display instead of the stream
  43.   * @var string 
  44.   */
  45.   var $errorMessage;
  46.   /**
  47.   * mime type for instructing the browser what to do with the stream
  48.   * @var string 
  49.   */
  50.   var $mimetype = 'text/plain';
  51.   /**
  52.   * whether the file is to be displayed by the browser automatically or offered to be saved
  53.   * @var boolean 
  54.   */
  55.   var $inline = true;
  56.  
  57.   /**
  58.   * Initialising the class
  59.   *
  60.   * @param  BumblebeeAuth $auth  Authorisation object
  61.   * @param  array $pdata   extra state data from the call path
  62.   * @return void nothing
  63.   */
  64.   function BufferedAction($auth$pdata{
  65.     parent::ActionAction($auth$pdata);
  66.     $this->mungeInputData();
  67.     $this->ob_flush_ok = 0;
  68.   }
  69.  
  70.   /**
  71.   * Unbuffer the stream and allow the data to be output
  72.   */
  73.   function unbuffer({
  74.     // only run this once.
  75.     if ($this->ob_flush_ok{
  76.       $this->ob_flush_ok = 1;
  77.       ob_end_flush();
  78.     }
  79.   }
  80.  
  81.   /**
  82.   * Unbuffer the stream and get ready to drop the error message to the user
  83.   * @param string $err error message to be output
  84.   */
  85.   function unbufferForError($err{
  86.     $this->errorMessage = '<p>'.$err.'</p>';
  87.     $this->unbuffer();
  88.     return STATUS_ERR;
  89.   }
  90.  
  91.   /**
  92.   * Send the data back to the user now
  93.   */
  94.   function sendBufferedStream({
  95.     $this->outputTextFile($this->filename$this->bufferedStream);
  96.   }
  97.  
  98.   /**
  99.   * send headers to the browser with the filename and the mimetype
  100.   * @param string $filename the suggested filename to give to the browser
  101.   */
  102.   function startOutputTextFile($filename{
  103.     header('Content-type: '.$this->mimetype);
  104.     $disposition ($this->inline ? 'inline' 'attachment');
  105.     header("Content-Disposition$dispositionfilename=$filename");
  106.   }
  107.  
  108.   /**
  109.   * Dump a data stream to the user
  110.   * @param string $filename the suggested filename to give to the browser
  111.   * @param string $stream the data stream to be sent
  112.   */
  113.   function outputTextFile($filename$stream{
  114.     $this->startOutputTextFile($filename);
  115.     echo $stream;
  116.   }
  117.  
  118.   /**
  119.   * Save the datastream to a local file
  120.   * @param string $filename the filename to save the data to on the server
  121.   * @param string $stream the data stream to be saved
  122.   */
  123.   function saveTextFile($filename$stream{
  124.     $fp fopen($filename'w');
  125.     fputs($fp$stream);
  126.     fclose($fp);
  127.   }
  128.  
  129.   /**
  130.   * Work out an appropriate (and hopefully unique) filename for the data
  131.   * Uses the Config option in bumblebee.ini [export]::filename
  132.   * The following parameters are replaced: __date__ __action__ __what__
  133.   *
  134.   * @param string $action the action verb
  135.   * @param string $what the nature of the export
  136.   * @param string $ext  the file extension (pdf, csv etc) without the dot.
  137.   */
  138.   function getFilename($action$what$ext{
  139.     $conf ConfigReader::getInstance();
  140.     $name $conf->value('export''filename');
  141.     $name preg_replace('/__date__/'strftime('%Y%m%d-%H%M%S'time())$name);
  142.     $name preg_replace('/__action__/'$action$name);
  143.     $name preg_replace('/__what__/'$what$name);
  144.     return $name.'.'.$ext;
  145.   }
  146.  
  147. //class BufferedAction
  148.  
  149. ?>

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