<?

define
("FORMHANDLER",$name);

define("EXEC_MODULE_SEP",":");

include_once(
INCLUDE_DIR."/vars.inc.php");
include_once(
INCLUDE_DIR."/content.inc.php");
include_once(
INCLUDE_DIR."/configopts.inc.php");

global 
$REQUEST_METHOD,$HTTP_GET_VARS,$HTTP_POST_VARS;
// Storing the name of the array instead of the array saves memory
$http_vars = ($REQUEST_METHOD=="GET") ? HTTP_GET_VARS HTTP_POST_VARS;

// Create the FormHandler object and make it run.
$modules[FORMHANDLER] = new FormHandler($$http_vars$HTTP_COOKIE_VARS);
unset(
$http_vars);

/**
 * Main execution engine
 *
 * This module creates the different modules that need to be used. It
 * contains the text to be used in the mail, display, etc. It distributes
 * the variables entered in the form and it creates the runlevels and runs
 * the different modules.
 * @package   FormHandler
 * @author    Herman Suijs
 */

class FormHandler extends ModuleBase {
  var 
$name "FormHandler";
  var 
$id FORMHANDLER;
  var 
$opties = array();
  var 
$vars = array(); // complete array of HTTP_VARS
  //var $show_vars = array(); // array of vars to show in mail(s)
  //var $cookies = array();
  
var $content = array(); // strings to use for mailing and displaying
  //var $mail_content = ""; // string to use for mailing
  //var $display_content = ""; // string to use for displaying
  
var $sorted_modules = array();
  var 
$config;
  var 
$translator;

  
/**
   * Constructor
   */
  
function FormHandler($vars,$cookies) {
    global 
$modules,$error;
    
$this->translator = new Translator(TRANSLATE_BY_DEFAULT);
    
$this->ModuleBase();
    
$this->config = new Config();
    
//$this->content[CONTENT] = new Content(CONTENT);

    
$this->set_html(true,ERROR_CONTENT);
    
$this->set_title("Error",ERROR_CONTENT);
    
$this->set_html(true,DEBUG_CONTENT);
    
$this->set_header("<h2>Debug</h2>",DEBUG_CONTENT);
    
$this->set_footer("",DEBUG_CONTENT);
    
$this->set_html();

    if (!
is_array($vars)||empty($vars)) {
      
$error->add_message("No variables submitted. Did you use a form ?",$this->name,3);
      break;
    } 
/* if (!is_array($vars)||empty($v... */
    //$this->vars = $vars;
    
$this->vars = new Vars_Array($vars);

    
/*
    while (list($name,$value) = each($vars)) {
      $this->show_vars[strtolower($name)] = $value;
    }
    */
    
    //$this->cookies = $cookies;
    
if (is_array($cookies)&&!empty($cookies)) {
      
$this->vars->addto_vars($cookies);
      
$this->vars->set_vartype(array_keys($cookies),USR_VARS);
      
$this->vars->set_vartype(array_keys($cookies),COOKIE_VARS);
    } 
/* if (is_array($cookies)&&!empty... */

    
$this->sorted_modules createSortedArray($modules);

  } 
/* function FormHandler($vars,$co... */

  /**
   * Return execlevel
   *
   * For compatibility with other modules
   * @return    zero value
   */
  
function execlevel() {
    return 
0// First execlevel. Is ignored
  
/* function execlevel() */

  /**
   * Translate
   *
   * Translation function
   * @param   $message: message
   * @param   $varx: variables that need to be substituted
   * @return  translated message
   */
  
function translate($message,$var1="",$var2="",$var3="",$var4="",$var5="",$var6="",$var7="",$var8="",$var9="",$var10="",$var11="",$var12="") {
    return 
$this->translator->translate($message,$var1="",$var2="",$var3="",$var4="",$var5="",$var6="",$var7="",$var8="",$var9="",$var10="",$var11="",$var12="");
  } 
/* function translate($message,$v... */

  // Method to create an error
  /*
  function error($name,$level,$var1,$var2="",$var3="",$var4="",$var5="") {
    if (!is_array($var1)) {
      $vars = array($var1,$var2,$var3,$var4,$var5);
    } else {
      $vars=$var1;
   }

  }
  */


  // *************  Execution of modules
  // The action-levels are necessary, because otherwise the deletion of
  // options configured in the form will be done AFTER the mail is sent.
  /**
   * Exec module
   *
   * Execute the modules in this action-step
   * @param   $module: the module that is executed
   * @param   $action: the action-step
   * @return  true if module is executed
   */
  
function exec_module($module$action) {
    global 
$modules,$debug;
    
//$debug->add_message("Running module action $action: $module",$this->name);
    
$debug->add(DB_XFLOW,$this->name,"Running module action %s: %s",$action,$module);
    
$module =& $modules[$module];
    
$skip $module->enabled;
    if (!
$skip) {
      
$debug->add(DB_XFLOW,$this->name,"Module %s is not enabled",$module);
    } 
/* if (!$skip) */
    
switch ($action) {
      case 
:
    
$module->before_action();
    break;
      case 
:
    if (
$skip$module->do_action();
    break;
      case 
:
    
$module->after_action();
    break;
    } 
/* switch ($action) */
    
unset($module);
    return 
$skip// Return if module-action is executed
  
/* function exec_module($module, ... */

  /**
   * Runlevel
   *
   * Get the modules from one runlevel and execute them
   * @param   $levelgroep: runlevel
   */
  
function runlevel($levelgroup) {
    global 
$modules,$debug;
    
$debug->add(3,$this->name,"Entering <font color=green>runlevel</font> %s",$levelgroup);
    for(
$action=0$action<MAX_ACTION$action++) {
      for(
$level=0;$level<STEP_EXECLEVEL$level++) {
    
$exec_module $this->sorted_modules[$level+$levelgroup*STEP_EXECLEVEL];
    
//echo "$levelgroup $level $exec_module <br>";
    
if (!empty($exec_module)) {
      if (!
strpos($exec_module,EXEC_MODULE_SEP)===false) {
        
$exec_module split(EXEC_MODULE_SEP,$exec_module);
        while (list(,
$module) = each($exec_module)) {
          
$this->exec_module($module,$action);
        } 
/* while (list(,$module) = each($... */
      
} else { /* if (!strpos($exec_module,EXEC_... */
        
$this->exec_module($exec_module,$action);
   } 
/* if (!strpos($exec_module,EXEC_... */
    
/* if (!empty($exec_module)) */
      
/* for($level=0;$level<STEP_EXECL... */
    
/* for($action=0; $action<MAX_ACT... */
    
$debug->add(6,$this->name,"Leaving <font color=green>runlevel</font> %s",$levelgroup);
    
$this->sorted_modules createSortedArray($modules);
  } 
/* function runlevel($levelgroup) */


// *********** Config code
  /**
   * Config
   *
   * Method to retrieve config-information
   * @param   $option: the optionname
   * @return  the optionvalue
   */
  
function config($option) {
    return 
$this->config->getOption($option);
  } 
/* function config($option) */

// *********** Content code
  /*
  function addto_content($name,$content) {
    $this->content[$name]->addto_content($content);
  }
  */
  // Functions for content communications
  
function set_html($html=true,$type=CONTENT) {
    if (empty(
$this->content[$type])) {
      
$this->content[$type] = new Content($type);
    } 
/* if (empty($this->content[$type... */
    
$this->content[$type]->set_html($html);
  } 
/* function set_html($html=true,$... */

  
function convert_content($vars,$type=CONTENT) {
    if (empty(
$this->content[$type])) {
      
$this->content[$type] = new Content($type);
    } 
/* if (empty($this->content[$type... */
    
$this->content[$type]->convert_content($vars);
  } 
/* function convert_content($vars... */

  
function set_content($new_content$type=CONTENT) {
    global 
$error,$debug;
    if (!isset(
$this->content[$type])) {
      
$this->content[$type] = new Content($type,$new_content);
    } else { 
/* if (!isset($this->content[$typ... */
      
$this->content[$type]->set_content($new_content);
   } 
/* if (!isset($this->content[$typ... */

    
$debug->add(8,$this->name,"New content (%s) set",$type);
    return 
true;

  } 
/* function set_content($new_cont... */

  
function addto_content($add_content$type=CONTENT) {
    if (empty(
$this->content[$type])) {
      
$this->set_content($type,$add_content);
    } else { 
/* if (empty($this->content[$type... */
      
$this->content[$type]->addto_content($add_content);
   } 
/* if (empty($this->content[$type... */
  
/* function addto_content($add_co... */

  
function insert_content($ins_content$type=CONTENT) {
    if (empty(
$this->content[$type])) {
      
$this->set_content($type,$ins_content);
    } else { 
/* if (empty($this->content[$type... */
      
$this->content[$type]->insert_content($ins_content);
   } 
/* if (empty($this->content[$type... */
  
/* function insert_content($ins_c... */

  /*
  function insert_content($ins_content) {
    $this->text = $ins_content . $this->content;
  }
  */

  
function get_content($type=CONTENT,$final=false,$vars="") {
    global 
$debug;
    if (!empty(
$this->content[$type])) {
      if (
$final) {
    
$debug->add(8,$this->name." get-content","Get final %s","Content[$type]");
      } 
/* if ($final) */
      
return $this->content[$type]->get_content($final,$vars);
    } else { 
/* if (!empty($this->content[$typ... */
      
$debug->add(8,$this->name." get-content","%s not defined yet","Content[$type]");
      return 
"";
   } 
/* if (!empty($this->content[$typ... */ 
  
/* function get_content($type=CON... */

  
function set_header($header,$type=CONTENT) {
    global 
$debug;
    if (!empty(
$this->content[$type])) {
      
$this->content[$type]->set_header($header);
    } else { 
/* if (!empty($this->content[$typ... */
      
$debug->add(8,$this->name." set_header","%s not defined yet","Content[$type]");
   } 
/* if (!empty($this->content[$typ... */

  
/* function set_header($header,$t... */

  
function set_footer($footer,$type=CONTENT) {
    global 
$debug;
    if (!empty(
$this->content[$type])) {
      
$this->content[$type]->set_footer($footer);
    } else { 
/* if (!empty($this->content[$typ... */
      
$debug->add(8,$this->name." set_footer","%s not defined yet","Content[$type]");
   } 
/* if (!empty($this->content[$typ... */

  
/* function set_footer($footer,$t... */

  
function set_title($title,$type=CONTENT) {
    global 
$debug;
    if (!empty(
$this->content[$type])) {
      if (empty(
$title)) {
    
$this->content[$type]->set_title_default($title);
      } else { 
/* if (empty($title)) */
    
$this->content[$type]->set_title($title);
   } 
/* if (empty($title)) { ... } els... */
    
} else { /* if (!empty($this->content[$typ... */
      
$debug->add(8,$this->name." set_title","%s not defined yet","Content[$type]");
   } 
/* if (!empty($this->content[$typ... */
  
/* function set_title($title,$typ... */

  /*
  function content() {
    // This function is going to be phased out.
    return $this->get_content();
  }
  */

  
function isset_content($type=CONTENT) {
    
// return a boolean
    
return $this->content[$type]->isset_content();
  } 
/* function isset_content($type=C... */


// *********** Vars code
  // Functions for variable communications
  
function get_vars($type=VARS) {
    return 
$this->vars->get_vars($type);
  } 
/* function get_vars($type=VARS) */

  
function set_vars($vars) {
    
$this->vars->set_vars($vars);
  } 
/* function set_vars($vars) */

  
function addto_vars($vars,$replace=false) {
    global 
$error;
    if (
is_array($vars)) {
      
//$this->vars[$name]->addto_vars($vars,$replace);
      
$this->vars->addto_vars($vars,$replace);
    } else { 
/* if (is_array($vars)) */
      
$error->add(1,$this->name."addto_vars","No array used");
   } 
/* if (is_array($vars)) { ... } e... */
  
/* function addto_vars($vars,$rep... */

  
function change_vars($name,$var_name,$var_value="") {
    
$this->addto_vars($name,$var_name,$var_value);
  } 
/* function change_vars($name,$va... */

  
function del_vars($name,$var_name) {
    
$this->vars[$name]->del_vars($var_name);
  } 
/* function del_vars($name,$var_n... */

  /*
  function get_vars() {
    return $this->vars;
  }

  function set_default_vars($config) {
    global $debug;
    while (list($name,$value) = each($config)) {
      if (!isset($this->vars[$name])) {
    $this->vars[$name] = $value;
    //$debug->add_message("Value of vars[$name] set to filevalue: $value",$this->name);
    $debug->add(6,$this->name,"Value of %s set to filevalue: %s","vars[$name]",$value);
      }
    }
  }

  function addto_vars($name,$value) {
    global $debug;
    if (isset($this->vars[$name])) {
      //$debug->add_message("Value of vars[$name] changed to $value",$this->name);
      $debug->add(6,$this->name,"Value of %s changed to $value","vars[$name]",$value);
    }
    $this->vars[$name]=$value;
  }

  function get_showvars() {
    return $this->show_vars;
  }
  */

  
function del_var($key) {
    global 
$debug;
    if (!
$debug->enabled) {
      unset(
$this->show_vars[$key]);
    } 
/* if (!$debug->enabled) */
    //$debug->add_message("Delete var $key",$this->name);
    
$debug->add(6,$this->name,"Delete var %s",$key);
  } 
/* function del_var($key) */
  
  // Functions for cookies communications
  /*
  function cookies() {
    return $this->cookies;
  }
  */

/* class FormHandler extends Modu... */