<?

/**
 * Option object
 *
 * This class represents a field that is used in a form. The option can be
 * created in a module and at the execution of the module it checks the
 * existence and the value of the option.
 * @package   Modules
 * @author    Herman Suijs
 */
class Option extends ModuleBase {
  
//var $name; // Defined in ModuleBase
  //var $id; // Defined in ModuleBase
  
var $aliases;
  var 
$regexp;
  var 
$required;
  var 
$default;
  var 
$value;
  
// Has different meaning:
  // module stays enabled if option is missing
  
var $enabled;
  var 
$visible;

  
// By default no regular expression, not required and no default value
  
function Option($names$regexp=""$required=false$enabled=true$default="",$visible=false) {
    
$name split(ALIAS_SEP,$names);
    
$this->name $name[0];
    
$this->id OPTION_ID// Defined in ModuleBase
    
$this->aliases $names;
    
$this->regexp $regexp;
    
$this->required $required;
    
$this->default $default;
    
$this->value "";
    
$this->enabled $enabled;
    
$this->visible $visible;
    
$this->ModuleBase();
  } 
/* function Option($names, $regex... */

  
function set_value($value) {
    global 
$debug;
    
//$debug->add_message("Value (".$this->value .") changed to $value",$this->name);
    
$debug->add(DB_FLOW,$this->name,"Value (%s) changed to %s",$this->value,$value);
    
$this->value=$value;
  } 
/* function set_value($value) */

  
function set_visible($visible=true) {
    
$this->visible=$visible;
  } 
/* function set_visible($visible=... */

  
function check_existence($vars) {
    global 
$error,$debug,$formHandler;
    
$keys_array array_keys($vars);
    
$found false;
    if (!
strpos($this->aliases,ALIAS_SEP)===false) {
      
$tmp_aliases split(ALIAS_SEP,$this->aliases);
      while (list(,
$alias) = each($tmp_aliases)) {
    if (empty(
$this->value)) {
      if (
in_array($alias,$keys_array)) {
        
$found=true;
        if (
$this->name != $alias) {
          
//$debug->add_message("Change optionname ".$this->name." to ".$alias,$this->name,3);
          
$debug->add(DB_XFLOW,$this->name,"Change optionname %s to %s",$this->name,$alias);
          
$this->name $alias;
        } 
/* if ($this->name !=
        $alias) */
        
$this->value $vars[$this->name];
        break;
      } 
/* if (in_array($alias,$keys_arra... */
    
/* if (empty($this->value)) */
      
/* while (list(,$alias) = each($t...  */
    
/* if (!strpos($this->aliases,ALI... */
    
elseif (in_array($this->name,$keys_array)) {
      
$found=true;
      
$this->value $vars[$this->name];
    } 
/* elseif (in_array($this->name,$... */
    
elseif (!empty($this->value)) {
      
$found=true;
    } 
/* elseif (!empty($this->value)) */

    
if ($found) {
      if (!
$this->visible$formHandler->del_var($this->name);
      return 
$found;
    } elseif (
$this->required) { /* if ($found) */
      
if ($this->name=="recipient") {
    
$error->add_message("<p>You probably used a wrong form or entrance to this script.<br> Please read the documentation of the formhandler script.<br> You can find it in the package on http://formhandler.sourceforge.net<p>","See documentation",2);
      } 
/* if ($this->name=="recipient") */
      
$error->add_message("Required option ".$this->name." missing",$this->name,3);
   
/* if ($found) { ... } elseif ($t... */  } elseif ($this->default!="") {
      
$this->value $this->default;
      
//$debug->add_message("Option set to default".$this->name,$this->name,3);
      
$debug->add(DB_FLOW,$this->name,"Option set to default %s",$this->name);
      return 
true;
    } 
//end if found /* /* if ($found) { ... } elseif ... */

    
return $found;
  } 
/* function check_existence($vars... */

  
function check_value() {
    global 
$error;
    if (
$this->regexp) {
      if (
ereg($this->regexp,$this->value)) {
    return 
true;
      } else { 
/* if (ereg($this->regexp,$this->... */
    
$error->add_message("Option ".$this->name ." is not valid",$this->name,3);
    return 
false;
   
/* if (ereg($this->regexp,$this->... */    // end if ereg
    
// end if this /* if ($this->regexp) */
  
// end function /* function check_value() */

// end class Option /* class Option extends ModuleBas... */

?>