Source for file nondbrow.php

Documentation is available at nondbrow.php

  1. <?php
  2. /**
  3. * Equivalent to DBRow where the database is not involved
  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. /** database uber-object that we will emulate */
  17. require_once 'dbobject.php';
  18. /** status codes for success/failure of database actions */
  19. require_once 'inc/statuscodes.php';
  20.  
  21.  
  22. /**
  23. * Equivalent to DBRow where the database is not involved
  24. *
  25. * Object representing a NON-database row (and extensible to represent joined rows)
  26. *
  27. * Usage:<code>
  28. *   #set database connection parameters
  29. *   $obj = new nonDBRow();
  30. *   #set the fields required and their attributes
  31. *   $obj->addElement(....);
  32. *   #check to see if user data changes some values
  33. *   $obj->update($POST);
  34. *   $obj->checkValid();
  35. * </code>
  36. @package    Bumblebee
  37. @subpackage FormsLibrary
  38. */
  39.  
  40. class nonDBRow {
  41.   /** @var string   name of this field */
  42.   var $name;
  43.   /** @var string   label for the field in the display */
  44.   var $longname;
  45.   /** @var string   description for the field in the mouse-over */
  46.   var $description;
  47.   /** @var boolean  this is a new object the form for which has not yet been shown to the user */
  48.   var $newObject = 0;
  49.   /** @var boolean  this row is editable */
  50.   var $editable = 1;
  51.   /** @var string   prefixed to all name="$field[name]" sections of the html code */
  52.   var $namebase;
  53.   /** @var string   current error message  */
  54.   var $errorMessage = '';
  55.   /** @var boolean  the fields in this row have changed cf the database */
  56.   var $changed = 0;
  57.   /** @var boolean  the data in the fields are valid */
  58.   var $isValid = 0;
  59.   /** @var boolean   don't to validation on this row */
  60.   var $suppressValidation = 0;
  61.   /** @var string   output when doing text dumps of the object */
  62.   var $dumpheader = 'nonDBRow object';
  63.   /** @var boolean  sql errors should be considered fatal and the script will die()  */
  64.   var $fatal_sql = 1;
  65.   /** @var array    additional rows to be included at the end of the display table */
  66.   var $extrarows;
  67.   /** @var array    list of Field objects in this row */
  68.   var $fields;
  69.   /** @var array    list of column headings for tabular display */
  70.   var $headings;
  71.  
  72.   /** @var integer   debug level 0=off    */
  73.   var $DEBUG = 0;
  74.  
  75.  
  76.   /**
  77.   *  Create a new generic field/row object not linked to the db
  78.   *
  79.   * @param string $name   the name of the field (db name, and html field name
  80.   * @param string $longname  long name to be used in the label of the field in display
  81.   * @param string $description  used in the html title or longdesc for the field
  82.   */
  83.   function nonDBRow($name$longname$description{
  84.     $this->name = $name;
  85.     $this->longname = $longname;
  86.     $this->description = $description;
  87.   }
  88.  
  89.   /**
  90.   *  Update the object with the user-submitted data
  91.   *
  92.   *  update the value of each of the objects fields according to the user
  93.   *  input data, and validate the data if appropriate
  94.   *  @param array user supplied data (field => $value)
  95.   *  @return boolean data is valid
  96.   */
  97.   function update($data{
  98.     // We're a new object, but has the user filled the form in, or is the
  99.     // user about to fill the form in?
  100.     $this->newObject = 1;
  101.     foreach (array_keys($this->fieldsas $k{
  102.       if (isset($data[$this->namebase.$k])) {
  103.         $this->log('I AM NOT NEW '.$k.':changed');
  104.         $this->newObject = 0;
  105.         break;
  106.       else {
  107.         $this->log('Still new '.$k.':unchanged');
  108.       }
  109.     }
  110.  
  111.     // check each field in turn to allow it to update its data
  112.     foreach (array_keys($this->fieldsas $k{
  113.       $this->log("Check $k ov:".$this->fields[$k]->value
  114.                             .'('.$this->fields[$k]->useNullValues .'/'$this->newObject.')');
  115.       if (!($this->fields[$k]->useNullValues && $this->newObject)) {
  116.         $this->changed += $this->fields[$k]->update($data);
  117.       }
  118.       $this->log('nv:'.$this->fields[$k]->value.' '.($this->changed ? 'changed' 'not changed'));
  119.     }
  120.     #$this->checkValid();
  121.     return $this->changed;
  122.   }
  123.  
  124.  
  125.   /**
  126.   * check the validity of the data
  127.   * @return boolean data is valid
  128.   */
  129.   function checkValid({
  130.     $this->isValid = 1;
  131.     if (is_array($this->fields&& count($this->fields1return true;
  132.     // check each field in turn to allow it to update its data
  133.     // if this object has not been filled in by the user, then
  134.     // suppress validation
  135.     foreach (array_keys($this->fieldsas $k{
  136.       if ($this->newObject{
  137.         $this->log('Checking valid '.$this->fields[$k]->namebase $k);
  138.         if ($this->fields[$k]->isValid()) {
  139.           $this->errorMessage .= T_('Invalid data:').' '.$this->fields[$k]->longname
  140.                                     .'('.$this->fields[$k]->name.')'
  141.                                   .' = "'$this->fields[$k]->getValue(.'"<br />';
  142.           $this->isValid = false;
  143.         }
  144.       }
  145.     }
  146.     if ($this->isValid{
  147.       $this->errorMessage .= '<br />'
  148.             .T_('Some values entered into the form are not valid and should be highlighted in the form below. Please check your data entry and try again.');
  149.     }
  150.     return $this->isValid;
  151.   }
  152.  
  153.   /**
  154.   * Add a new field to the row
  155.   *
  156.   * Add an element into the fields[] array. The element must conform
  157.   * to the Fields class (or at least its interface!) as that will be
  158.   * assumed elsewhere in this object.
  159.   * Inheritable attributes are also set here.
  160.   *
  161.   * @param Field $el the field to add
  162.   */
  163.   function addElement($el{
  164.     $this->fields[$el->name$el;
  165.     if ($this->fields[$el->name]->editable == -1{
  166.       $this->fields[$el->name]->editable $this->editable;
  167.     }
  168.     if (isset($this->fields[$el->name]->namebase)) {
  169.       $this->fields[$el->name]->namebase $this->namebase;
  170.       #echo "Altered field $el->name to $this->namebase\n";
  171.     }
  172.     if ($this->fields[$el->name]->suppressValidation == -1{
  173.       $this->fields[$el->name]->suppressValidation $this->suppressValidation;
  174.       #echo "Altered field $el->name to $this->namebase\n";
  175.     }
  176.     #echo $el->name;
  177.     #echo "foo:".$this->fields[$el->name]->name.":bar";
  178.   }
  179.  
  180.   /**
  181.   * Add multiple new fields to the row
  182.   *
  183.   * Adds multiple elements into the fields[] array.
  184.   *
  185.   * @param array $els array of Field objects
  186.   */
  187.   function addElements($els{
  188.     foreach ($els as $e{
  189.       #echo $e->text_dump();
  190.       $this->addElement($e);
  191.     }
  192.   }
  193.  
  194.   /**
  195.   * Create a quick text representation of the object
  196.   *
  197.   * @return string text representation
  198.   */
  199.   function text_dump({
  200.     $t  "<pre>$this->dumpheader $this->table (id=$this->id)\n{\n";
  201.     foreach ($this->fields as $v{
  202.       $t .= "\t".$v->text_dump();
  203.     }
  204.     $t .= "}\n</pre>";
  205.     return $t;
  206.   }
  207.  
  208.   /**
  209.   * Display the object
  210.   *
  211.   * @return string object representation
  212.   */
  213.   function display($data=NULL{
  214.     if ($data !== NULL$this->update($data);
  215.     return $this->text_dump();
  216.   }
  217.  
  218.   /**
  219.   * Display the row as a form in a table
  220.   *
  221.   * @param integer $j      (optional) number of columns in the table (will pad as necessary)
  222.   * @return string  html table
  223.   */
  224.   function displayInTable($numCols=2$extraClass=''{
  225.     $t '';
  226.     if ($this->longname !== NULL$t  '<h3>'.$this->longname.'</h3>';
  227.     $t .= '<table class="tabularobject '.$extraClass.'" title="'.$this->description.'">';
  228.     if (is_array($this->headings)) {
  229.       $t .= '<tr>';
  230.       foreach ($this->headings as $h{
  231.         $t .= '<th>'.$h.'</th>';
  232.       }
  233.       $t .= '</tr>';
  234.     }
  235.     foreach ($this->fields as $v{
  236.       $t .= $v->displayInTable($numCols);
  237.     }
  238.     if (is_array($this->extrarows)) {
  239.       foreach ($this->extrarows as $v{
  240.         $t .= '<tr>';
  241.         foreach ($v as $c{
  242.           $t .= '<td>'.$c.'</td>';
  243.         }
  244.         $t .= '</tr>';
  245.       }
  246.     }
  247.     $t .= '</table>';
  248.     return $t;
  249.   }
  250.  
  251.   /**
  252.   * Debug print function.
  253.   *
  254.   * @param string $logstring debug message to be output
  255.   * @param integer $priority  priority of the message, will not be written unless $priority <= $this->DEBUG
  256.   */
  257.   function log($logstring$priority=10{
  258.     if ($priority <= $this->DEBUG{
  259.       echo $logstring."<br />\n";
  260.     }
  261.   }
  262.  
  263. // class dbrow
  264.  
  265. ?>

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