Source for file field.php

Documentation is available at field.php

  1. <?php
  2. /**
  3. * database primitive object for an individual field within a row
  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. /** type checking and data manipulation */
  17. require_once 'validtester.php';
  18. /** status codes for success/failure of database actions */
  19. require_once 'inc/statuscodes.php';
  20.  
  21. /**
  22. * Field object that corresponds to one field in a SQL table row.
  23. *
  24. * A number of fields would normally be held together in a DBRow,
  25. * with the DBRow object controlling the updating to the SQL database.
  26. *
  27. * Typical usage is through inheritance, see example for DBRow.
  28. * <code>
  29. *     $f = new TextField("name", "Name");
  30. * </code>
  31. *
  32. @package    Bumblebee
  33. @subpackage FormsLibrary
  34. */
  35. class Field {
  36.   /** @var string   name of this field */
  37.   var $name;
  38.   /** @var string   label for the field in the display */
  39.   var $longname;
  40.   /** @var string   description for the field in the mouse-over */
  41.   var $description;
  42.   /** @var boolean  field is required */
  43.   var $required = 0;
  44.   /** @var string   value of this field */
  45.   var $value;
  46.   /** @var string   previous value of this field*/
  47.   var $ovalue;
  48.   /** @var string   default value of the field */
  49.   var $defaultValue = '';
  50.   /** @var string   duplicate the value of this field (when hidden) in a hidden field with this name  */
  51.   var $duplicateName;
  52.   /** @var tristate  this field is editable */
  53.   var $editable = -1;
  54.   /** @var string   the value of this field has changed */
  55.   var $changed = 0;
  56.   /** @var string   the field is a hidden widget */
  57.   var $hidden;
  58.   /** @var string   the data contained by the field is valid */
  59.   var $isValid = 1;
  60.   /** @var string   if the ID changes, then notify descendents */
  61.   var $notifyIdChange = 0;
  62.   /** @var tristate   don't to validation on this field */
  63.   var $suppressValidation = -1;
  64.   /** @var boolean  permit NULL values in this field */
  65.   var $useNullValues = 0;
  66.   /** @var array    display attributes */
  67.   var $attr = array();
  68.   /** @var string   CSS class to be used for marking fields with errors */
  69.   var $errorclass = 'error';
  70.   /** @var string   prepended to the name of the field in the html id and hence in the data array */
  71.   var $namebase;
  72.   /** @var string   function to call to check that the data is valid */
  73.   var $isValidTest = 'isset';
  74.   /** @var string   function to call to clean the data string before testing if valid */
  75.   var $valueCleaner = null;
  76.   /** @var boolean  don't generate an SQL name=value representation for this field  */
  77.   var $sqlHidden = 0;
  78.   /** @var boolean  this field requires two-stage sync */
  79.   var $requiredTwoStage = 0;
  80.   /** @var string   status code of the descendents of this field (out-of-band data reporting) */
  81.   var $oob_status = STATUS_NOOP;
  82.   /** @var string   error message from this field or its descendents */
  83.   var $oob_errorMessage = '';
  84.   /** @var integer  debug log level */
  85.   var $DEBUG = 0;
  86.  
  87.   /**
  88.   *  Create a new generic field object, designed to be superclasses
  89.   *
  90.   * @param string $name   the name of the field (db name, and html field name
  91.   * @param string $longname  long name to be used in the label of the field in display
  92.   * @param string $description  used in the html title or longdesc for the field
  93.   */
  94.   function Field($name$longname=''$description=''{
  95.     $this->name = $name;
  96.     $this->longname = $longname;
  97.     $this->description = $description;
  98.   }
  99.  
  100.   /**
  101.   * update the value of the object with the user-supplied data in $data
  102.   *
  103.   * $data is most probably from POST data etc.
  104.   *
  105.   * The validity of the data is *not* checked at this stage, the object
  106.   * only takes the user-supplied value.
  107.   *
  108.   * $data is an array with the data relevant to this field being
  109.   * in the key $this->namebase.$this->name.
  110.   *
  111.   * For example:
  112.   * <code>
  113.   * $this->namebase = 'person-';
  114.   * $this->name     = 'phonenumber';
  115.   * $data['person-phonenumber'] // used for the value
  116.   * </code>
  117.   *
  118.   * @param array $data is name => value pairs as above
  119.   * @return boolean  did the value of this object change?
  120.   */
  121.   function update($data{
  122.     if (isset($data["$this->namebase$this->name"]|| $this->useNullValues{
  123.       $newval issetSet($data"$this->namebase$this->name");
  124.       $this->log("$this->name$this->value$newval ($this->useNullValues)");
  125.       if (isset($this->valueCleaner&& is_callable($this->valueCleaner)) {
  126.         $newval call_user_func($this->valueCleaner$newval);
  127.       }
  128.  
  129.       if ($this->editable{
  130.         // we ignore new values if the field is not editable
  131.         if ($this->changed = ($this->getValue(!= $newval)) {
  132.           $this->ovalue = $this->getValue();
  133.           $this->value = $newval;
  134.         }
  135.       else {
  136.         //ensure that the value is copied in from the default value if
  137.         //it is unset.
  138.         $this->value = $this->getValue();
  139.       }
  140.     else {
  141.       if ($this->getValue(!= $this->value{
  142.         $this->changed = 0;
  143.         $this->value = $this->getValue();
  144.       }
  145.     }
  146.     $this->log($this->name . ($this->changed ? ' CHANGED' ' SAME'));
  147.     return $this->changed;
  148.   }
  149.  
  150.   /**
  151.   * Check the validity of the current data value.
  152.   *
  153.   * This also checks the validity of the data even if the data is not newly-entered.
  154.   * Returns true if the specified validity tests are passed:
  155.   *  -   is the field required to be filled in && is it filled in?
  156.   *  -   is there a validity test && is the data valid?
  157.   *
  158.   * @return boolean  is the data value valid
  159.   */
  160.   function isValid({
  161.      $this->isValid = 1;
  162.     if ($this->required{
  163.       #$this->isValid = (isset($this->value) && $this->value != "");
  164.       $this->isValid = ($this->getValue(!= '');
  165.       $this->log($this->name . ' Required: '.($this->isValid ? ' VALID' ' INVALID'));
  166.     }
  167.     if ($this->isValid && $this->suppressValidation == 0{
  168.       $this->isValid = ValidTester($this->isValidTest$this->getValue()$this->DEBUG);
  169.     }
  170.     $this->log($this->name . ($this->isValid ? ' VALID' ' INVALID'));
  171.     return $this->isValid;
  172.   }
  173.  
  174.   /**
  175.   * set the value of this field
  176.   *
  177.   * <b>without</b> validation or checking to see whether the field has changed.
  178.   *
  179.   * @param string   the new value for this field
  180.   */
  181.   function set($value{
  182.     //echo "Updating field $this->name. New value=$value\n";
  183.     $this->value = $value;
  184.   }
  185.  
  186.   /**
  187.   * create an SQL-injection-cleansed string for db statements
  188.   *
  189.   * Generates a string that represents this field that can be used in an SQL
  190.   * UPDATE or INSERT statement. i.e. "name='Stuart'".
  191.   *
  192.   * @param string  optional SQL name to use to change the default
  193.   * @param boolean optional force the generation of the command
  194.   * @return string  in SQL assignable form
  195.   */
  196.   function sqlSetStr($name=''$force=false{
  197.     if ($this->sqlHidden{
  198.       if (empty($name)) {
  199.         $name $this->name;
  200.       }
  201.       return $name .'='qw($this->getValue());
  202.     else {
  203.       return '';
  204.     }
  205.   }
  206.  
  207.   /**
  208.   * Set display attributes for the field.
  209.   *
  210.   * the attribute fielde are parsed differently for each different field subclass
  211.   *
  212.   * @param array $attrs attribute_name => value
  213.   * @access public
  214.   */
  215.   function setattr($attrs{
  216.     if (is_array($attrs)) return;
  217.     $this->attr = array_merge($this->attr$attrs);
  218.   }
  219.  
  220.   /**
  221.   * Quick and dirty display of the field status
  222.   *
  223.   * @return string simple text representation of the class's value and attributes
  224.   */
  225.   function text_dump({
  226.     $t  "$this->name =&gt".$this->getValue();
  227.     $t .= ($this->editable ? "(editable)" "(read-only)");
  228.     $t .= ($this->isValid ? "" "(invalid)");
  229.     $t .= "\n";
  230.     return $t;
  231.   }
  232.  
  233.   /**
  234.   * Generic display function
  235.   */
  236.   function display({
  237.     return $this->text_dump();
  238.   }
  239.  
  240.   /**
  241.   * html representation of this field as a "hidden" form widget
  242.   */
  243.   function hidden({
  244.     return "<input type='hiddenname='$this->namebase$this->name"
  245.            ."value='".xssqw($this->getValue())."' />";
  246.   }
  247.  
  248.   /**
  249.   * render this form widget in an html table
  250.   *
  251.   * @param integer $cols  number of columns to be included in table (padding cols will be added)
  252.   * @abstract
  253.   */
  254.   function displayInTable($cols=3{
  255.   }
  256.  
  257.   /**
  258.   * return the current value as text and the widget as a hidden form element
  259.   *
  260.   * @return string current value
  261.   * @abstract
  262.   */
  263.   function selectedValue({
  264.   }
  265.  
  266.   /**
  267.   * return an html representation of the widget
  268.   *
  269.   * @return string html widget
  270.   * @abstract
  271.   */
  272.   function selectable({
  273.   }
  274.  
  275.   /**
  276.   * Obtain the value of the field, taking account of default values
  277.   *
  278.   * @return mixed field value
  279.   */
  280.   function getValue({
  281.     //echo "FIELD $this->name: ".$this->value.":".$this->defaultValue."<br />";
  282.     return (isset($this->value$this->value : $this->defaultValue);
  283.   }
  284.  
  285.   /**
  286.   * set whether this field is editable or not
  287.   *
  288.   * @param boolean $editable  new editable state
  289.   */
  290.   function setEditable($editable=1{
  291.     $this->editable = $editable;
  292.   }
  293.  
  294.   /**
  295.   * set the namebase for the data storage in the html form
  296.   *
  297.   * @param boolean $editable  new editable state
  298.   */
  299.   function setNamebase($namebase=''{
  300.     $this->namebase = $namebase;
  301.   }
  302.  
  303.   /**
  304.   * Generic logging function
  305.   *
  306.   * @param string $logstring   data to log
  307.   * @param integer $priority   log level for this message
  308.   */
  309.   function log($logstring$priority=10{
  310.     if ($priority <= $this->DEBUG{
  311.       echo $logstring."<br />\n";
  312.     }
  313.   }
  314.  
  315.   /**
  316.   * PHP5 clone method
  317.   *
  318.   * PHP5 clone statement will perform only a shallow copy of the object. Any subobjects must also be cloned
  319.   */
  320.   function __clone({
  321.     // Force a copy of contents of $this->list
  322.     //echo "Cloning field";
  323.     if (is_object($this->attr)) $this->attr = clone($this->attr);
  324.   }
  325.  
  326. // class Field
  327.  
  328. ?>

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