[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/web_app/src/command/ -> lmbFormCommand.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/web_app/src/command/lmbActionCommand.class.php');
  10  lmb_require('limb/web_app/src/exception/lmbDelegateHaltException.class.php');
  11  lmb_require('limb/validation/src/lmbValidator.class.php');
  12  lmb_require('limb/validation/src/lmbErrorList.class.php');
  13  lmb_require('limb/core/src/lmbDelegate.class.php');
  14  
  15  /**

  16   * class lmbFormCommand.

  17   *

  18   * @package web_app

  19   * @version $Id: lmbFormCommand.class.php 5945 2007-06-06 08:31:43Z pachanga $

  20   */
  21  class lmbFormCommand extends lmbActionCommand
  22  {
  23    const ON_BEFORE             = 1;
  24    const ON_SHOW               = 2;
  25    const ON_AFTER              = 3;
  26    const ON_BEFORE_VALIDATE    = 4;
  27    const ON_AFTER_VALIDATE     = 5;
  28    const ON_VALID              = 6;
  29    const ON_ERROR              = 7;
  30  
  31    protected $form_id;
  32    protected $listeners = array();
  33  
  34    protected $validator;
  35    protected $error_list;
  36  
  37    function __construct($template_path = '', $form_id = '', $validator = null)
  38    {
  39      parent :: __construct($template_path);
  40  
  41      if($form_id)
  42        $this->form_id = $form_id;
  43      else
  44        $this->form_id = $this->_guessFormId();
  45  
  46      $this->error_list = new lmbErrorList();
  47      $this->validator = $validator ? $validator : new lmbValidator($this->error_list);
  48    }
  49  
  50    protected function _guessFormId()
  51    {
  52      $controller = $this->toolkit->getDispatchedController();
  53      return $controller->getName() . '_form';
  54    }
  55  
  56    function perform()
  57    {
  58      parent :: perform();
  59  
  60      try
  61      {
  62        $this->_doPerform();
  63      }
  64      catch(lmbDelegateHaltException $e){}
  65    }
  66  
  67    protected function _doPerform()
  68    {
  69      $this->toolkit->getView()->setFormErrors($this->form_id, $this->error_list);
  70  
  71      $this->_onBefore();
  72      $this->_invokeListeners(self :: ON_BEFORE);
  73  
  74      if(!$this->isSubmitted())
  75      {
  76        $this->_onShow();
  77        $this->_invokeListeners(self :: ON_SHOW);
  78      }
  79      else
  80      {
  81        $this->_onBeforeValidate();
  82        $this->_invokeListeners(self :: ON_BEFORE_VALIDATE);
  83  
  84        $this->_validate();
  85  
  86        if(!$this->isValid())
  87        {
  88          $this->_onError();
  89          $this->_invokeListeners(self :: ON_ERROR);
  90        }
  91        else
  92        {
  93          $this->_onValid();
  94          $this->_invokeListeners(self :: ON_VALID);
  95        }
  96  
  97        $this->_onAfterValidate();
  98        $this->_invokeListeners(self :: ON_AFTER_VALIDATE);
  99      }
 100  
 101      $this->_onAfter();
 102      $this->_invokeListeners(self :: ON_AFTER);
 103    }
 104  
 105    function setFormDatasource($datasource)
 106    {
 107      $this->toolkit->getView()->setFormDatasource($this->form_id, $datasource);
 108    }
 109  
 110    /**

 111     * @deprecated

 112     */
 113    function setViewFormDatasource($datasource)
 114    {
 115      $this->setFormDatasource($datasource);
 116    }
 117  
 118    function getViewFormDatasource()
 119    {
 120      return $this->toolkit->getView()->getFormDatasource($this->form_id);
 121    }
 122  
 123    protected function _onBefore(){}
 124    protected function _onShow(){}
 125    protected function _onBeforeValidate(){}
 126    protected function _onValid(){}
 127    protected function _onError(){}
 128    protected function _onAfterValidate(){}
 129    protected function _onAfter(){}
 130  
 131    function setErrorList($error_list)
 132    {
 133      $this->error_list = $error_list;
 134    }
 135  
 136    protected function _invokeListeners($type)
 137    {
 138      if(isset($this->listeners[$type]))
 139        lmbDelegate :: invokeChain($this->listeners[$type], array($this));
 140    }
 141  
 142    protected function _validate()
 143    {
 144      $this->validator->setErrorList($this->error_list);
 145      $this->validator->validate($this->request);
 146    }
 147  
 148    function getFormId()
 149    {
 150      return $this->form_id;
 151    }
 152  
 153    function getValidator()
 154    {
 155      return $this->validator;
 156    }
 157  
 158    function isSubmitted()
 159    {
 160      return $this->request->hasPost();
 161    }
 162  
 163    function isValid()
 164    {
 165      return $this->error_list->isValid();
 166    }
 167  
 168    function getErrorList()
 169    {
 170      return $this->error_list;
 171    }
 172  
 173    function addError($message, $fields = array(), $values = array())
 174    {
 175      $this->error_list->addError($message, $fields, $values);
 176    }
 177  
 178    protected function _getRequestData()
 179    {
 180      return $this->request->export();
 181    }
 182  
 183    function registerCallback($type, $callback)
 184    {
 185      $this->listeners[$type][] = $callback;
 186    }
 187  
 188    function registerOnBeforeCallback($object, $method)
 189    {
 190      $this->registerCallback(self :: ON_BEFORE, new lmbDelegate($object, $method));
 191    }
 192  
 193    function registerOnShowCallback($object, $method)
 194    {
 195      $this->registerCallback(self :: ON_SHOW, new lmbDelegate($object, $method));
 196    }
 197  
 198    function registerOnBeforeValidateCallback($object, $method)
 199    {
 200      $this->registerCallback(self :: ON_BEFORE_VALIDATE, new lmbDelegate($object, $method));
 201    }
 202  
 203    function registerOnAfterValidateCallback($object, $method)
 204    {
 205      $this->registerCallback(self :: ON_AFTER_VALIDATE, new lmbDelegate($object, $method));
 206    }
 207  
 208    function registerOnValidCallback($object, $method)
 209    {
 210      $this->registerCallback(self :: ON_VALID, new lmbDelegate($object, $method));
 211    }
 212  
 213    function registerOnErrorCallback($object, $method)
 214    {
 215      $this->registerCallback(self :: ON_ERROR, new lmbDelegate($object, $method));
 216    }
 217  
 218    function registerOnAfterCallback($object, $method)
 219    {
 220      $this->registerCallback(self :: ON_AFTER, new lmbDelegate($object, $method));
 221    }
 222  
 223    function halt()
 224    {
 225      throw new lmbDelegateHaltException('Delegate was halted');
 226    }
 227  }
 228  ?>


Generated: Sat Sep 6 04:46:52 2008 Cross-referenced by PHPXref 0.7