Source for file arraychoicelist.php

Documentation is available at arraychoicelist.php

  1. <?php
  2. /**
  3. * a choice list like DBChoiceList *not* based on an SQL statement
  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. /**
  17. * Provide a non-database filled (i.e. array-filled) version of DBChoiceList
  18. *
  19. * See DBChoiceList for more details of interface.
  20. @see DBChoiceList
  21. @package    Bumblebee
  22. @subpackage FormsLibrary
  23. */
  24. class ArrayChoiceList {
  25.   /** @var string  id value currently selected  */
  26.   var $id=-1;
  27.   /** @var boolean  list is editable  */
  28.   var $editable = 0;
  29.   /** @var boolean  list is extensible by user */
  30.   var $extendable = 0;
  31.   /** @var boolean  list has changed */
  32.   var $changed = 0;
  33.   /** @var array    list of array($listKey => $unique_name, $displayKey => $displayed_value) */
  34.   var $choicelist;
  35.   /** @var integer  number of items in the list */
  36.   var $length;
  37.   /** @var string   array index in which to store the unique_name */
  38.   var $listKey;
  39.   /** @var string   array index in which to store the displayed_value */
  40.   var $displayKey;
  41.   /** @var integer  status code for out-of-band error reporting  */
  42.   var $oob_status = STATUS_NOOP;
  43.   /** @var string   error message for out-of-band error reporting  */
  44.   var $oob_errorMessage = '';
  45.  
  46.   /** @var integer  debug level for object  0=off.  */
  47.   var $DEBUG = 0;
  48.   
  49.   /**
  50.   *  Create a new ArrayChoiceList object
  51.   *
  52.   * @param array $array  list of unique_name => displayed_value
  53.   * @param string $iv    index in which the unique_name should be stored in the ChoiceList
  54.   * @param string $dv    index in which the displayed_value should be stored in the ChoiceList
  55.   */
  56.   function ArrayChoiceList($array$iv$dv{
  57.     $this->choicelist = array();
  58.     $this->listKey = $iv;
  59.     $this->displayKey = $dv;
  60.     $item=0;
  61.     foreach ($array as $internalVal => $displayVal{
  62.       $this->choicelist[$itemarray();
  63.       $this->choicelist[$item][$this->listKey$internalVal;
  64.       $this->choicelist[$item][$this->displayKey$displayVal;
  65.       $this->choicelist[$item]['_field'0;
  66.       $item++;
  67.     }
  68.   }
  69.   
  70.   /**
  71.    * Construct an array suitable for storing the field and the values it takes for later reuse
  72.    * @param array $values list of values to be displayed
  73.    * @param Field $field  Field object associated with these values
  74.    */
  75.   function _mkaddedarray($values$field=''{
  76.     $a array();
  77.     $a[$this->listKey$values[0];
  78.     $a[$this->displayKey$values[1];
  79.     $a['_field'$field;
  80.     return $a;
  81.   }
  82.  
  83.   /**
  84.   * append a special field (such as "Create new:") to the choicelist
  85.   *
  86.   * This method is much simpler than the DB equivalent as we won't need to remember them.
  87.   *
  88.   * @see DBChoiceList::append()
  89.   * @param array $values list of values to be displayed
  90.   * @param Field $field (optional) a field class object to be placed next to this entry, if possible
  91.   */
  92.   function append($values$field=''{
  93.     $fa $this->_mkaddedarray($values$field);
  94.     array_push($this->choicelist$fa);
  95.   }
  96.  
  97.   /**
  98.   * prepend a special field (such as "Create new:") to the choicelist
  99.   *
  100.   * This method is much simpler than the DB equivalent as we won't need to remember them.
  101.   *
  102.   * @param array $values list of values to be displayed
  103.   * @param Field $field (optional) a field class object to be placed next to this entry, if possible
  104.   */
  105.   function prepend($values$field=''{
  106.     $fa $this->_mkaddedarray($values$field);
  107.     array_unshift($this->choicelist$fa);
  108.   }
  109.  
  110.   /**
  111.   * display the contents of the list
  112.   * @return string text representation of the field
  113.   */
  114.   function display({
  115.     return $this->text_dump();
  116.   }
  117.  
  118.   /**
  119.   * display the contents of the list
  120.   * @return string text representation of the field
  121.   */
  122.   function text_dump({
  123.     return "<pre>ArrayChoiceList:\n".print_r($this->choicelisttrue).'</pre>';
  124.   }
  125.  
  126.   /** 
  127.   * update the value of the list based on user data:
  128.   *   - if it is within the range of current values, then take the value
  129.   *   - if the field contains a new value (and is allowed to) then keep
  130.   *     an illegal value, mark as being changed, and wait until later for
  131.   *     the field to be updated
  132.   *   - if the field contains a new value (and is not allowed to) or an
  133.   *     out-of-range value, then flag as being invalid
  134.   * 
  135.   * @param string $newval the (possibly) new value for the field
  136.   * @param array ancillary user data (passed on to any appended or prepended fields)
  137.   */
  138.   function update($newval$data{
  139.     if ($this->DEBUG{
  140.       echo 'ArrayChoiceList update: ';
  141.       echo "(changed=$this->changed)";
  142.       echo "(id=$this->id)";
  143.       echo "(newval=$newval)";
  144.     }
  145.     if (isset($newval)) {
  146.       //check to see if the newval is legal (does it exist on our choice list?)
  147.       $isExisting 0;
  148.       foreach ($this->choicelist as $v{
  149.         if ($this->DEBUGecho "($isExisting:".$v['id'].":$newval)";
  150.         if ($v['id'== $newval && $v['id'>= 0{
  151.           $isExisting 1;
  152.           break;
  153.         }
  154.       }
  155.       if ($isExisting{
  156.         // it is a legal, existing value, so we adopt it 
  157.         if ($this->DEBUGecho 'isExisting';
  158.         $this->changed += ($newval != $this->id);
  159.         $this->id = $newval;
  160.         $this->isValid 1;
  161.         //isValid handling done by the Field that inherits it
  162.       elseif ($this->extendable{
  163.         // then it is a new value and we should accept it
  164.         if ($this->DEBUGecho 'isExtending';
  165.         $this->changed += 1;
  166.         //$this->id = $newval;
  167.         //If we are extending the list, then we should have a negative
  168.         //number as the current value to trip the creation of the new
  169.         //entry later on in sync()
  170.         $this->id = -1;
  171.         foreach ($this->choicelist as $k => $v{
  172.           //preDump($v);
  173.           if (isset($v['_field']&& $v['_field'!= ""{
  174.             $this->choicelist[$k]['_field']->update($data);
  175.             $this->isValid += $this->choicelist[$k]['_field']->isValid();
  176.           }
  177.         }
  178.       else {
  179.         if ($this->DEBUGecho 'isInvalid';
  180.         // else, it's a new value and we should not accept it
  181.         $this->isValid 0;
  182.       }
  183.     }
  184.     #echo " DBchoiceList::changed=$this->changed<br />";
  185.     return $this->isValid;
  186.   }
  187.  
  188.   /**
  189.   * set the currently selected value
  190.   *
  191.   * @param string $value new value to use as curretly selected
  192.   */
  193.   function set($value{
  194.     #echo "DBchoiceList::set = $value<br/>";
  195.     $this->id = $value;
  196.   }
  197.  
  198.   /**
  199.   * synchronise: but we have nothing to do as we are not attached to the db
  200.   * (provided to give interface to normal Field database)
  201.   * @return boolean false -- there was nothing to do
  202.   */
  203.   function sync({
  204.     return false;
  205.   }
  206.  
  207.   /**
  208.   * generate name=value pairs in case someone actually wants them!
  209.   * 
  210.   * @return string of form name='value'
  211.   */
  212.   function _sqlvals({
  213.     $vals array();
  214.     if ($this->changed{
  215.       #echo "This has changed";
  216.       foreach ($this->choicelist as $v{
  217.         if (isset($v['_field'])) {
  218.           $vals[$v['_field']->name ."="qw($v['_field']->value);
  219.         }
  220.       }
  221.     }
  222.     #echo "<pre>"; print_r($this->choicelist); echo "</pre>";
  223.     #echo "<pre>"; print_r($this->fields); echo "</pre>";
  224.     #echo "<pre>"; print_r($vals); echo "</pre>";
  225.     return join(",",$vals);
  226.   }
  227.  
  228.   /**
  229.   * determine whih values are selected and return them
  230.   * @param boolean $returnArray  return an array of values
  231.   * @return mixed list of selected values (array) or current value (string)
  232.   */
  233.   function selectedValue({
  234.     $val array();
  235.     foreach ($this->choicelist as $v{
  236.       //echo "H:$this->idfield, $k, $v, $this->id";
  237.       if ($v[$this->listKey== $this->id{
  238.         foreach ($this->fields as $f{
  239.           //echo "G=$f";
  240.           $val[$v[$f];
  241.         }
  242.       }
  243.     }
  244.     return implode(' '$val);
  245.   }
  246.  
  247.   /**
  248.   * set which option in the selection list is the default option
  249.   * @param string $val   default value to use
  250.   */
  251.   function setDefault($val{
  252.     //echo "ArrayChoiceList::setDefault: $val";
  253.     if (isset($this->id|| $this->id < 0{
  254.       $this->id = $val;
  255.     }
  256.     //echo $this->id;
  257.   }
  258.  
  259. // class ArrayChoiceList
  260.  
  261. ?>

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