Source for file actionaction.php

Documentation is available at actionaction.php

  1. <?php
  2. /**
  3. * Base class inherited by all actions from the action-triage
  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/actionaction.php
  13. */
  14.  
  15. /** Load ancillary functions */
  16. require_once 'inc/typeinfo.php';
  17.  
  18. /** status codes for success/failure of database actions */
  19. require_once 'inc/statuscodes.php';
  20.  
  21. /**
  22. * Base class inherited by all actions from the action-triage
  23. *
  24. * An Action is a single operation requested by the user. What action is to be performed
  25. * is determined by the action-triage mechanism in the class ActionFactory.
  26. *
  27. *
  28. * Since http is a stateless protocol, what seems like just one activity
  29. * (e.g. "edit a booking") actually includes multiple http requests
  30. * ("show the user the current values" and "sync changes to disk"). Additionally,
  31. * the one application suite will have many different things to do (e.g. edit/create
  32. * users, edit/create foobar objects). There are two approaches to this multiple
  33. * functions problem: have many .php files that are called directly by the user
  34. * for each function (i.e. links to user.php and foobar.php) but then you can end up
  35. * with a lot of repeated code in each file to control the page layout, load themes,
  36. * control login etc. Alternatively, you can use just the one index.php and include
  37. * an extra control variable in each link that decides what the script should do this time.
  38. *
  39. * In either case, it is convenient to have a standard "action" object that can be created
  40. * by some ActionFactory which then obeys a standard "action" interface to then
  41. * be used by the internals of the application.
  42. *
  43. * Typical usage:
  44. * <code>
  45. * $action = new ActionFactory($params);
  46. * $action->go();
  47. * </code>
  48. *
  49. @abstract
  50. @package    Bumblebee
  51. @subpackage Actions
  52. */
  53. class ActionAction {
  54.   /**
  55.   * Authorisation object
  56.   * @var    BumblebeeAuth 
  57.   */
  58.   var $auth;
  59.   /**
  60.   * Unparsed path data from the CGI call
  61.   * @var    array 
  62.   */
  63.   var $PDATA;
  64.   /**
  65.   * Parsed input data (combined PATH data and POST data)
  66.   * @var    array 
  67.   */
  68.   var $PD;
  69.   /**
  70.   * Permit normal HTML output
  71.   *
  72.   * Allows previous output (from the HTML template) to be flushed or suppress it so
  73.   * a PDF can be output.
  74.   * @var    boolean 
  75.   */
  76.   var $ob_flush_ok = 1;
  77.   /**
  78.   * The action should be read-only; no data should be changed
  79.   * @var    boolean 
  80.   */
  81.   var $readOnly = true;
  82.   /**
  83.   * Default status messages that are returned to the user.
  84.   * @var    array 
  85.   */
  86.   var $stdmessages;
  87.  
  88.   /**
  89.   * Turn on debugging messages from the Action* classes
  90.   * @var    integer 
  91.   */
  92.   var $DEBUG=0;
  93.  
  94.   /**
  95.   * Initialising the class
  96.   *
  97.   * Variable assignment only in this constructor, the child class would normally:
  98.   * - use parent's constructor
  99.   * - parse input data
  100.   *
  101.   * @param  BumblebeeAuth $auth  Authorisation object
  102.   * @param  array $pdata   extra state data from the call path
  103.   * @return void nothing
  104.   */
  105.   function ActionAction($auth,$pdata{
  106.     $this->auth = $auth;
  107.     $this->PDATA = $pdata;
  108.     $this->stdmessages = array(
  109.       STATUS_NOOP => '',
  110.       STATUS_OK   => T_('Operation completed successfully'),
  111.       STATUS_WARN => T_('Warnings produced during operation'),
  112.       STATUS_ERR  => T_('Error. Could not complete operation'),
  113.     );
  114.   }
  115.  
  116.   /**
  117.   * Actually perform the action that this Action* class is to perform
  118.   *
  119.   * this is an abstract class and this function <b>must</b> be overridden
  120.   *
  121.   * @return void nothing
  122.   */
  123.   function go({
  124.   }
  125.  
  126.   /**
  127.   * Parse the input data sources
  128.   *
  129.   * @return void nothing
  130.   */
  131.   function mungeInputData({
  132.     $this->PD = $this->PDATA;
  133. /*    foreach ($_POST as $k => $v) {
  134.       $this->PD[$k] = $v;
  135.     }*/
  136.     if (isset($this->PD['id']&& $this->PD['id'== 'showdeleted'{
  137.       $this->PD['showdeleted'true;
  138.       unset($this->PD['id']);
  139.     }
  140. /*    if (isset($this->PDATA[1]) && $this->PDATA[1] !== '') {
  141.       if ($this->PDATA[1] != 'showdeleted') {
  142.         $this->PD['id'] = $this->PDATA[1];
  143.       } else {
  144.         $this->PD['showdeleted'] = true;
  145.       }
  146.     }*/
  147.     #$PD['defaultclass'] = 12;
  148.     echoData($this->PD);
  149.   }
  150.  
  151.   /**
  152.   *  Reports to the user whether an action was successful
  153.   *
  154.   *   @param integer $status   success or otherwise of action
  155.   *   @param array $messages  (optional) messages to be reported, indexed by $status
  156.   *
  157.   *    $status codes as per file statuscodes.php
  158.   */
  159.   function reportAction($status$messages=''{
  160.     //$this->log('ActionStatus: '.$status);
  161.     #echo 'final='.$status;
  162.     if ($status == STATUS_NOOPreturn '';
  163.     $message '';
  164.     if (isset($messages[$status])) {
  165.       $message .= $messages[$status];
  166.     else {
  167.       foreach ($messages as $code => $msg{
  168.         if ($status $code{
  169.           $message .= $msg;
  170.         }
  171.       }
  172.     }
  173.     if ($message{
  174.       foreach ($this->stdmessages as $code => $msg{
  175.         if ($status $code{
  176.           $message .= $msg;
  177.         }
  178.       }
  179.     }
  180.     if ($message{
  181.       $message T_('Unknown status code. Error:').' '$status;
  182.     }
  183.     $t '<div class="'.($status STATUS_OK 'msgsuccess' 'msgerror').'">'
  184.          .$message
  185.          ."</div>\n";
  186.     return $t;
  187.   }
  188.  
  189.   /**
  190.   * Select which item to edit for this action
  191.   *
  192.   * @param boolean $deleted  (optional) show deleted items
  193.   * @return void nothing
  194.   */
  195.   function select($deleted=false{
  196.   }
  197.  
  198.   /**
  199.   * Edit the selected item
  200.   *
  201.   * @return void nothing
  202.   */
  203.   function edit({
  204.   }
  205.  
  206.   /**
  207.   * Delete the selected item
  208.   *
  209.   * @return void nothing
  210.   */
  211.   function delete({
  212.   }
  213.  
  214.   /**
  215.   *  Generic logging function for use by all Action classes
  216.   *
  217.   *   The higher the value of $priority, the less likely the message is to
  218.   *   be output. The normal range for priority is [1-10] with messages with
  219.   *   ($priority <= $this->DEBUG) being displayed.
  220.   *
  221.   *   @param string $message  the message to be logged to the browser
  222.   *   @param integer $priority  (optional) the priority level of the message
  223.   */
  224.   function log ($message$priority=10{
  225.     if ($priority <= $this->DEBUG{
  226.       echo $message."<br />\n";
  227.     }
  228.   }
  229.  
  230.   /**
  231.   * Display a message to the user explaining that the requested action cannot be
  232.   * performed as the object is "readonly".
  233.   *
  234.   * This is designed to be used with the magic token verification system that requires
  235.   * that the form that is submitted by the user contains some additional secret data that
  236.   * was provided by the installation. This is designed to prevent attacks through getting
  237.   * users to visit specially crafted URLs or to submit malicious forms.
  238.   *
  239.   * @param  string    $message    message to show to the user explaining that the action cannot be processed
  240.   * @see    http://www.debian-administration.org/articles/465
  241.   */
  242.   function readOnlyError($message=null{
  243.     if ($message !== null{
  244.       print $message;
  245.     else {
  246.       printf('<div class="error">%s</div>',
  247.           T_('The requested objects are read-only as I can\'t verify the validity of the form you submitted.')
  248.         );
  249.     }
  250.   }
  251.  
  252.   /**
  253.   * Cleanse the input data of all fields except for the specifed whitelisted fields
  254.   *
  255.   * Removes all user-submitted data fields from the $ActionAction::PD array except for
  256.   * the fields that are explicitly whitelisted in the function call.
  257.   *
  258.   * @param mixed   $fields     single field name or list of fields to whitelist
  259.   */
  260.   function _dataCleanse($fields{
  261.     if (is_array($fields)) $fields array($fields);
  262.     $data $this->PD;
  263.     $this->PD = array();
  264.  
  265.     foreach ($fields as $f{
  266.       if (isset($data[$f])) {
  267.         $this->PD[$f$data[$f];
  268.       }
  269.     }
  270.   }
  271.  
  272. //class ActionAction
  273.  
  274. ?>

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