<?php

  define ('HTML_QUICKFORM_EDIT', 1);
  define ('HTML_QUICKFORM_VIEW', 2);
  define ('HTML_QUICKFORM_ADD',  3);
  
  class QuickForm_Multi extends HTML_QuickForm
  {
    var $_mode;

    var $_formBuilt;

    /*
     * Sets the mode of the form.  should be done before the form is rendered
     *
     * @param integer  A constant containing the form mode.
     */
    function setMode($mode)
    {
      $this->_mode = $mode;  
    }                    
    
    /*
     * Function stub. This should be overloaded with a function that builds the form.
     * Should set $this->_formbuilt to true to avoid the form being built twice under
     * certain circumstances.
     */
    function buildForm()
    {
      if (!$this->_mode)
      {
        $this->addElement('header',    null, 'No Mode Set');
        $this->addElement('static',     null, null,  'You must set a mode using the setMode() function');
        return;
      }

      $this->_formbuilt = true;
    }
    
    /*
     * Renders the form
     * Adds a hidden elemenet which stores the form mode
     * Automatically loads the form template if one exists. 
     */
    function display()
    {
        if (!$this->_formbuilt)
        {
          $this->buildForm();
        }
        $this->_modifyDefaults();
        $this->addElement('hidden', '_qf_multi_mode', $this->_mode);
        
        global $_HTML_QUICKFORM_WRAPPER;
        if (empty($_HTML_QUICKFORM_WRAPPER['CONFIG'])) {
            HTML_QuickForm_Wrapper::_loadConfig();
        }
        $template = $_HTML_QUICKFORM_WRAPPER['CONFIG']['template_location'] . '/' . get_class($this) . '.tpl';
        if (file_exists($template))
        {
            $tpl =& new HTML_Template_Sigma($_HTML_QUICKFORM_WRAPPER['CONFIG']['template_location']);
            $tpl->loadTemplateFile(get_class($this) . '.tpl');

            $renderer =& new HTML_QuickForm_Renderer_ITDynamic($tpl);
  
            $this->accept($renderer);
            $tpl->show();  
        }
        else
        {
            parent::display();
        }
    }
    
    /*
     * Calls the correct handling function based upon the mode of the form
     */
    function process()
    {
        $mode = $this->exportValue('_qf_multi_mode');
        switch ($mode)
        {
            case HTML_QUICKFORM_EDIT:
                $this->processEdit();
                break;
            case HTML_QUICKFORM_VIEW:
                $this->processView();
                break;
            case HTML_QUICKFORM_ADD:
                $this->processAdd();
                break;
        }
    }  
    
    /*
     * Function stub should be overloaded
     */
    function processAdd()
    {
        
    }

    /*
     * Function stub should be overloaded
     */    
    function processView()
    {
        
    }

    /*
     * Function stub should be overloaded
     */    
    function processEdit()
    {
        
    }

    /*
     * Function stub should be overloaded
     * This function if here incase any extra processing is required to
     * set the default options.  This is useful when using DB_Dataobject, and 
     * you are setting defaults via $form->setDefaults($do->toArray())
     */
    function _modifyDefaults()
    {
      return;
    }      
  }
?>
