Implementing the FormProcessor class
But this was still quite far from our original subject, we were talking about
object-oriented forms here, remember? Let’s have a look at FormProcessor and
see what’s under its hood. I had to strip the code of PHPDoc-style comments,
because I think it requires step-by-step explanations, while reading through
the comments in arbitrary order might confuse. Version of this source with PHPDoc
comments is available for download from
here.
object-oriented forms here, remember? Let’s have a look at FormProcessor and
see what’s under its hood. I had to strip the code of PHPDoc-style comments,
because I think it requires step-by-step explanations, while reading through
the comments in arbitrary order might confuse. Version of this source with PHPDoc
comments is available for download from
here.
<?php
/**
* An abstract class implementing generic functionality for processing user's input
*
* This class encapsulates generic functions for working
* with data coming from user forms. Descendants must only override certain
* functions that perform context-specific tasks, like custom checking of
* data, storing correct data, etc.
*
* @author Yuri Makassiouk <[email protected]>,
* Mission & Media <[email protected]>
*/
class FormProcessor {
var $Name;
var $FirstTime;
var $Values;
var $Errors = array();
var $ErrorMessageFormat = '<br>%s';
var $wizardPage;
var $fieldPrefix = 'mm_form_data_';
function
FormProcessor($Name, $wPage='') {
$this->Name = $Name;
$this->wizardPage = $wPage;
$this->Values = $GLOBALS[$this->fieldPrefix.$this->Name];
$this->FirstTime = (count($this->Values) == 0);
if (!
$this->FirstTime)
$this->CustomCheck();
if ($this->IsCompleted()) {
$this->StoreData();
$GLOBALS['wizardPage'] = $this->NextWizardPage();
}
else
$this->DisplayForm();
}
function IsCompleted() {
return (!$this->FirstTime && count($this->Errors)<=0);
}
function CustomCheck() {}
//abstract
function DisplayForm() {}
//abstract
function NextWizardPage() {}
//abstract
function StoreData() {}
//abstract
function Additional() {
if ($this->wizardPage) :
?>
<input type="Hidden" name="wizardPage" value="<?php echo $this->wizardPage?>">
<?php endif;
}
function Set($Name, $Value) {
$this->$Name = $Value;
}
function ErrorReport($Name) {
if (isset($this->Errors[$Name]))
printf($this->ErrorMessageFormat, $this->Errors[$Name]);
}
function GetInitialValue($Name) {
if (isset($this->Values[$Name]))
return $this->Values[$Name];
else
return false;
}
function InitialValue($Name) {
echo $this->GetInitialValue($Name);
}
}
?>