Source for file choicelist.php

Documentation is available at choicelist.php

  1. <?php
  2. /**
  3. * a choice list field from which a select, list of hrefs etc can be built
  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. /** parent object */
  17. require_once 'field.php';
  18. /** formatter helper object */
  19. require_once 'outputformatter.php';
  20. /** list (db) object */
  21. require_once 'dbchoicelist.php';
  22. /** list (non-db) object */
  23. require_once 'arraychoicelist.php';
  24.  
  25. /**
  26. * a choice list field from which a select, list of hrefs etc can be built
  27. *
  28. * A field class that can only take a restricted set of values, such as
  29. * a radio list or a drop-down list.
  30. *
  31. * This class cannot adequately represent itself and is designed to be
  32. * inherited by a representation (such as RadioList or DropDownList)
  33. @package    Bumblebee
  34. @subpackage FormsLibrary
  35. */
  36. class ChoiceList extends Field {
  37.   /** @var DBChoiceList  contains the DBChoiceList object which has the restricted choices */
  38.   var $list;
  39.   /** @var array         array of OutputFormatter objects  */
  40.   var $formatter;
  41.   /** @var integer       name of the id field for the value of the field */
  42.   var $formatid;
  43.   /** @var boolean       additional items can be added to the choicelist */
  44.   var $extendable = 0;
  45.   /** @var tristate      in filling list, include only deleted=true, deleted=false or both (true, false, NULL) */
  46.   var $deleted=false//deleted=true/false in SQL; NULL means don't restrict
  47.  
  48.   
  49.   /**
  50.   *  Create a new choice list
  51.   *
  52.   * @param string $name   the name of the field (db name, and html field name
  53.   * @param string $description  used in the html title or longdesc for the field
  54.   */
  55.   function ChoiceList($name$description=''{
  56.     parent::Field($name''$description);
  57.     //$this->DEBUG=10;
  58.   }
  59.  
  60.   /**
  61.   * Create a DBList object within this class that is connected to the
  62.   * available choices in the database and will handle the actual parsing
  63.   * of user input etc
  64.   *
  65.   * If php supported multiple inheritance, then $this->list would not be
  66.   * a member of the class, rather DBList would be multiply inherited along
  67.   * with Field.
  68.   *
  69.   * @param string $table the name of the DB table to query
  70.   * @param mixed $fields array or string specifying the field(s) to query
  71.   * @param string $restriction a restriction that will be applied to the query (WHERE...)
  72.   * @param string $order the order to display the results (ORDER BY...)
  73.   * @param string $idfield the name of the field that will be used to identify the selected item (value=)
  74.   * @param string $limit limits that will be applied to the query (LIMIT...)
  75.   * @param string $join any tables that should be LEFT JOINed in the query to make sense of it
  76.   * @param boolean $distinct return only DISTINCT rows (default: false)
  77.   *
  78.   */
  79.   function connectDB($table$fields=''$restriction=''$order='name',
  80.                       $idfield='id'$limit=''$join=''$distinct=false{
  81.     $this->list = new DBChoiceList($table$fields$restriction$order,
  82.                       $idfield$limit$join$distinct$this->deleted);
  83.   }
  84.  
  85.   /**
  86.   * Provides a set of values for the droplist rather than filling it from a db query
  87.   *
  88.   * cf. ChoiceList::connectDB
  89.   *
  90.   * @param array $list List of label=>value pairs
  91.   */
  92.   function setValuesArray($list$idfield='id'$valfield='iv'){
  93.     $this->list = new ArrayChoiceList($list$idfield$valfield);
  94.   }
  95.  
  96.   function text_dump({
  97.     return $this->list->text_dump();
  98.   }
  99.  
  100.   function display({
  101.     return $this->text_dump();
  102.   }
  103.  
  104.   /**
  105.   * Create a set of OutputFormatter objects to handle the display of this object.
  106.   *
  107.   *  called as: setFormat($id, $f1, $v1, $f2, $v2, ...) {
  108.   *    - f1, v1 etc must be in pairs.
  109.   *    - f1 is an sprintf format (see PHP manual)
  110.   *    - v1 is an array of array indices that will be used to fill the
  111.   *      fields in the sprintf format from a $data array passed to the
  112.   *      formatter when asked to display itself
  113.   */
  114.   function setFormat({
  115.     $argc func_num_args();
  116.     $argv func_get_args();
  117.     $this->formatid = $argv[0];
  118.     $this->formatter = array();
  119.     for ($i 1$i $argc$i+=2{
  120.       $this->formatter[new OutputFormatter($argv[$i]$argv[$i+1]);
  121.     }
  122.   }
  123.  
  124.   /**
  125.   * A test text-based format function for the object.
  126.   *
  127.   * @param array $data the data to be formatted by this object's formatter object
  128.   * @return string text-based representation
  129.   */
  130.   function format($data{
  131.     $s $data[$this->formatid.":"$this->formatter[0]->format($data)
  132.         ."("$this->formatter[1]->format($data).")";
  133.     return $s;
  134.   }
  135.  
  136.   /**
  137.   * Display the field inside a table
  138.   *
  139.   * @param integer $cols the number of columns to include in the table (extras are padded out)
  140.   * @return string a single row HTML representation of the field
  141.   */
  142.   function displayInTable($cols=3{
  143.     if ($this->hidden{
  144.       $errorclass ($this->isValid ? "" "class='inputerror'");
  145.       $t "<tr $errorclass><td>{$this->description}</td>\n"
  146.           ."<td title='{$this->description}'>";
  147.       if ($this->editable{
  148.         $t .= $this->selectable();
  149.       } else {
  150.         $t .= $this->selectedValue();
  151.         $t .= "<input type='hiddenname='{$this->name}value='{$this->value}' />";
  152.       }
  153.       $t .= "</td>\n";
  154.       for ($i=0; $i<$cols-2; $i++) {
  155.         $t .= "<td></td>";
  156.       }
  157.       $t .= "</tr>";
  158.       return $t;
  159.     }
  160.   }
  161.  
  162.   function selectedValue() {
  163.     return $this->list->selectedValue();
  164.   }
  165.  
  166.   /**
  167.   * Update the value of the field and also the complex field within based on the user data.
  168.   *
  169.   * This does *not* create new elements within the complex field (list)
  170.   * at this stage: that is deferred until the latest possible point
  171.   * for all error checking to be performed.
  172.   *
  173.   * @param array $data a set of name => value pairs from which the value of this field
  174.   *               will be extracted
  175.   * @return boolean did the field change?
  176.   */
  177.   function update($data) {
  178.     parent::update($data);
  179.     $this->list->editable = $this->editable;
  180.     $this->list->extendable = $this->extendable;
  181.     #echo "ID=".$this->list->id;
  182.     if ($this->changed{
  183.       $this->log('haschanged');
  184.       // only update the list if things have changed
  185.       $this->list->update($this->value$data);
  186.       $this->changed = $this->list->changed;
  187.       $this->isValid = $this->list->isValid;
  188.     }
  189.     $this->log('ChoiceList::Update->isValid= '.$this->isValid);
  190.     #Field::set($this->list->id);
  191.     #echo $this->list->id;
  192.     #echo " (nv: $this->value)";
  193.     return $this->changed;
  194.   }
  195.  
  196.   /**
  197.   * Set the value of this field, both in the Field and in the DBList
  198.   * @param string $value  value to be set
  199.   */
  200.   function set($value) {
  201.     $this->list->set($value);
  202.     parent::set($value);
  203.   }
  204.  
  205.   /**
  206.    * Check the validity of the data.
  207.    *
  208.    * Return TRUE iff the DBList isValid and the Field isValid.
  209.    * This permits two rounds of checks on the data to be performed.
  210.    *
  211.    * @return boolean the field's current value is valid?
  212.    */
  213.   function isValid() {
  214.     $this->log('ChoiceList::isValid='.$this->isValid);
  215.     return $this->isValid && Field::isValid();
  216.   }
  217.  
  218.   /**
  219.   * Obtain the SQL data necessary for including the foreign key in
  220.   * the DBRow to which we belong.
  221.   * trip the complex field within us to sync(), which allows us
  222.   * to then know our actual value (at last).
  223.   *
  224.   * @param boolean $force force the field to return a name=value statement even
  225.   *                if it would prefer not to
  226.   * @return string name='value' from parent class
  227.   */
  228.   function sqlSetStr($name='', $force=false) {
  229.     #echo "Choicelist::sqlSetStr";
  230.     $this->oob_status = $this->list->sync();
  231.     //preDump($this->list);
  232.     $this->oob_errorMessage = $this->list->oob_errorMessage;
  233.     $this->value = $this->list->id;
  234.     #preDump($this);
  235.     return Field::sqlSetStr($name$force);
  236.   }
  237.  
  238.   /**
  239.   * prepends a value to the dropdown list
  240.   *
  241.   * @param array $a see DBChoiceList for details
  242.   */
  243.   function prepend($a) {
  244.     // templist is a horrible hack around a PHP 4.3 bug
  245.     // This is all we want to do:
  246.     //    $this->list->prepend($a);
  247.     // but that causes $this->list to suddenly become a reference: Object not &Object (see a var_dump)
  248.     // see http://bugs.php.net/bug.php?id=24485 and http://bugs.php.net/bug.php?id=30787
  249.     // Note that PHP 4.4.x claims to have fixed this bug. (although 4.4.0 does not)
  250. /*    $templist = $this->list;
  251.     $templist->prepend($a);
  252.     $this->list = $templist;*/
  253.     $this->list->prepend($a);
  254.   }
  255.  
  256.   /**
  257.   * appends a value to the dropdown list
  258.   *
  259.   * @param array $a see DBChoiceList for details
  260.   */
  261.   function append($a) {
  262.     // templist is a horrible hack around a PHP 4.3 bug, see above
  263.     // This is all we want to do:
  264.     //    $this->list->append($a);
  265. /*    $templist = $this->list;
  266.     $templist->append($a);
  267.     $this->list = $templist;*/
  268.     $this->list->append($a);
  269.   }
  270.  
  271.   /**
  272.   * set the default value for this object
  273.   */
  274.   function setDefault($val) {
  275.     // templist is a horrible hack around a PHP 4.3 bug, see above
  276.     // This is all we want to do:
  277.     //    $this->list->setDefault($val);
  278.     $this->defaultValue = $val;
  279. /*    $templist = $this->list;
  280.     $templist->setDefault($val);
  281.     $this->list = $templist;*/
  282.     $this->list->setDefault($val);
  283.   }
  284.  
  285.   function getValue() {
  286.     return (isset($this->value$this->value : $this->defaultValue);
  287.   }
  288.  
  289.   /**
  290.   * PHP5 clone method
  291.   *
  292.   * PHP5 clone statement will perform only a shallow copy of the object. Any subobjects must also be cloned
  293.   */
  294.   function __clone() {
  295.     parent::__clone();
  296.     // Force a copy of contents of $this->list
  297.     if (is_object($this->list)) $this->list clone($this->list);
  298.     foreach ($this->formatter as $k => $f{
  299.       $this->formatter[$kclone($f);
  300.     }
  301.   }
  302.  
  303.  
  304. } // class ChoiceList

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