NewsNewsFeaturesDownloadsDevelopmentSupportForumDocumentsAbout Us

action.class.php

查看本檔案說明文件.
00001 <?php
00002 
00003 
00024     include_once( PLOG_CLASS_PATH."class/object/object.class.php" );
00025     include_once( PLOG_CLASS_PATH."class/action/actioninfo.class.php" );
00026     include_once( PLOG_CLASS_PATH."class/config/properties.class.php" );
00027     include_once( PLOG_CLASS_PATH."class/net/request.class.php" );
00028     include_once( PLOG_CLASS_PATH."class/object/observable.class.php" );
00029     include_once( PLOG_CLASS_PATH."class/data/forms/formvalidator.class.php" );
00030 
00056     class Action extends Object
00057     {
00058 
00059         // this is the pointer to the view associated with this action
00060         var $_view;
00061         var $_request;
00062         var $_actionInfo;
00063         var $_fieldValidators;
00064         var $_validationErrorView;
00065         var $_previousAction;
00066         var $_isSuccess;
00067 
00074         function Action( $actionInfo, $httpRequest )
00075         {
00076             $this->Object();
00077 
00078             $this->_request = new Request( $httpRequest );
00079 
00080             $this->_actionInfo = $actionInfo;
00081             
00082             $this->_fieldValidators = Array();
00083             
00084             // initialize the views to empty values
00085             $this->_view = null;
00086             $this->_validationErrorView = null;
00087             
00088             // form object, used for data validation
00089             $this->_form = new FormValidator();
00090             
00091             // no previous action
00092             $this->_previousAction = null;
00093             
00094             // by default, the action is successful
00095             $this->_isSuccess = true;
00096         }
00097         
00104         function setPreviousAction( $previousAction )
00105         {
00106             $this->_previousAction = $previousAction;
00107         }
00108         
00115         function getPreviousAction()
00116         {
00117             return $this->_previousAction;
00118         }
00119         
00131         function perform()
00132         {   
00133             // to be implemented by child classes...
00134         }
00135         
00152         function validate()
00153         {
00154             // use the FormValidator object to validate the data
00155             $validationOk =  $this->_form->validate( $this->_request );
00156             
00157             // if something went wrong... let's do somethinga about it :)
00158             if( !$validationOk ) {
00159                 $this->validationErrorProcessing();
00160             }
00161             
00162             return $validationOk;
00163         }
00164         
00172         function validationErrorProcessing()
00173         {
00174             // if there was a validaton error, then inform the view
00175             $this->_view->setError( true );
00176                 
00177             // and  export all the data to the view so that it can be reused in the error view
00178             $fieldValues = $this->_form->getFieldValues();
00179             foreach( $fieldValues as $fieldName => $fieldValue ) {
00180                 $this->_view->setValue( "$fieldName", $fieldValue );
00181             }
00182                 
00183             $this->_form->setFormIsValid( false );
00184             $this->setCommonData();
00185     
00186             return true;
00187         }
00188         
00199         function setValidationErrorView( $view )
00200         {
00201             $this->_view = $view;
00202             
00203             return true;
00204         }
00205         
00217         function registerFieldValidator( $fieldName, $validator, $onlyIfAvailable = false )
00218         {
00219             $this->_form->registerFieldValidator( $fieldName, $validator, $onlyIfAvailable );
00220             
00221             return true;
00222         }
00223         
00232         function registerField( $fieldName )
00233         {
00234             $this->_form->registerField( $fieldName );
00235             
00236             return true;
00237         }
00238         
00239 
00246         function getView()
00247         {
00248             return $this->_view;
00249         }
00250         
00263         function setCommonData( $copyFormValues = false )
00264         {
00265             $this->_view->setValue( "form", $this->_form );
00266             
00267             if( $copyFormValues ) {
00268                 // in case we'd like to copy the values from the form
00269                 $fieldValues = $this->_form->getFieldValues();
00270                 foreach( $fieldValues as $fieldName => $fieldValue ) {
00271                     $this->_view->setValue( "$fieldName", $fieldValue );
00272                 }
00273             }
00274             
00275             return true;
00276         }
00277         
00292          function setForwardAction( $nextActionKey )
00293          {       
00294             Controller::setForwardAction( $nextActionKey, $this );
00295             
00296             return true;
00297          }
00298          
00308          function setForwardActionParameter( $param, $value )
00309          {
00310             return true;
00311          }
00312          
00319          function setSuccess( $success )
00320          {
00321              $this->_isSuccess = $success;
00322              $this->_form->setFormIsValid( $success );   
00323          }
00324     }
00325 ?>