File: D:/web/itpartnership.net (cms)/modules/FeedbackForm/classes/Form.class.php
<?php
// Feedback Form. 02/2005 SjG <feedbackform_cmsmodule@fogbound.net>
// A Module for CMS Made Simple, (c)2005 by Ted Kulp (wishy@cmsmadesimple.org)
// This project's homepage is: http://www.cmsmadesimple.org
class ffForm {
var $FormId = -1;
var $Name = '';
var $Alias = '';
var $SubmitButtonText = '';
var $NextButtonText = '';
var $ThanksMessage = '';
var $CSSClass = '';
var $RequiredFieldSymbol = '';
var $mod_globals;
var $loaded;
var $formTotalPages;
var $thisPage;
var $Fields;
function ffForm(&$mod_globals, &$params)
{
$this->mod_globals = &$mod_globals;
$this->Fields = array();
if (ffUtilityFunctions::def($params['form_id']))
{
$this->FormId = $params['form_id'];
}
if (ffUtilityFunctions::def($params['form_name']))
{
$this->Name = $params['form_name'];
}
if (ffUtilityFunctions::def($params['form_alias']))
{
$this->Alias = $params['form_alias'];
}
else if (ffUtilityFunctions::def($params['form_name']))
{
$this->Alias = ffUtilityFunctions::MakeAlias($this->Name, true);
}
if (ffUtilityFunctions::def($params['form_submit_button_text']))
{
$this->SubmitButtonText = $params['form_submit_button_text'];
}
if (ffUtilityFunctions::def($params['form_next_button_text']))
{
$this->NextButtonText = $params['form_next_button_text'];
}
if (ffUtilityFunctions::def($params['form_css_class']))
{
$this->CSSClass = $params['form_css_class'];
}
if (ffUtilityFunctions::def($params['form_required_field_symbol']))
{
$this->RequiredFieldSymbol = $params['form_required_field_symbol'];
}
if (ffUtilityFunctions::def($params['form_thanks_message']))
{
$this->ThanksMessage = $params['form_thanks_message'];
}
if (ffUtilityFunctions::def($params['continue']))
{
$this->thisPage = $params['continue'];
}
$this->loaded = 'not';
$this->formTotalPages = 1;
}
function Validate()
{
$validated = true;
$message = '';
$formPageCount=1;
for($i=0;$i<count($this->Fields);$i++)
{
if ($this->Fields[$i]->Type == 'PageBreak')
{
$formPageCount++;
}
//echo 'validate: '.$formPageCount . ' ' . $this->thisPage . ' ' . $thisField->Name.'<br>';
if (ffUtilityFunctions::def($this->thisPage) && $this->thisPage != $formPageCount)
{
continue;
}
//echo 'validating '. $thisField->Name.'<br>';
if (! $this->Fields[$i]->IsDisposition &&
$this->Fields[$i]->Required &&
! ffUtilityFunctions::def($this->Fields[$i]->Value))
{
$message .= "<h4>".$this->mod_globals->Lang('please_enter_a_value').' "'. $this->Fields[$i]->Name."\"</h4>\n";
$validated = false;
}
else
{
$res = $this->Fields[$i]->Validate();
if ($res[0] != true)
{
$message .= "<h4>". $res[1]."</h4>\n";
$validated = false;
}
}
}
return array($validated, $message);
}
function HasDisposition()
{
$hasDisp = false;
for($i=0;$i<count($this->Fields);$i++)
{
if ($this->Fields[$i]->IsDisposition)
{
$hasDisp = true;
}
}
return $hasDisp;
}
function Dispose(&$config)
{
$resArray = array();
// build a results array of field names/field values
for($i=0;$i<count($this->Fields);$i++)
{
//debug_display($config->FromAddress);
if (! $this->Fields[$i]->IsDisposition)
{
$resArray[] = array($this->Fields[$i]->Name,
$this->Fields[$i]->GetValue());
}
//debug_display($config->FromAddress);
}
$retCode = true;
// for each form disposition pseudo-field, dispose the form results
for($i=0;$i<count($this->Fields);$i++)
{
if ($this->Fields[$i]->IsDisposition)
{
if (! $this->Fields[$i]->DisposeForm($this->Name, $config, $resArray))
{
$retCode = false;
}
}
}
return $retCode;
}
function RenderFormHeader($version)
{
if (! $this->mod_globals->HideErrors)
{
echo "\n<!-- Start FeedbackForm Module (".$version.") -->\n";
}
}
function RenderFormFooter()
{
if (! $this->mod_globals->HideErrors)
{
echo "\n<!-- End FeedbackForm Module -->\n";
}
}
function RenderForm($id, &$params, $return_id)
{
$module =& $this->mod_globals->selfptr;
if ($this->FormId == -1)
{
echo "<!-- no form -->\n";
return;
}
if ($this->loaded != 'full')
{
$this->LoadForm($params);
}
$this->thisPage++;
if ($this->formTotalPages > 1)
{
echo "<p>Page ".$this->thisPage." of ".$this->formTotalPages."</p>";
}
echo '<div class="feedbackform_hidden">';
echo $module->CreateInputHidden($id, 'form_id', $this->FormId);
echo $module->CreateInputHidden($id, 'continue', $this->thisPage);
if ($this->thisPage == $this->formTotalPages)
{
echo $module->CreateInputHidden($id, 'done', 1);
}
echo "</div>";
if ($this->mod_globals->UseCSS)
{
echo "<div";
}
else
{
echo "<table";
}
if (strlen($this->CSSClass)>0)
{
echo " class=\"".$this->CSSClass."\"";
}
echo ">";
$formPageCount = 1;
for ($i=0; $i < count($this->Fields); $i++)
{
$thisField = $this->Fields[$i];
if ($thisField->Type == 'PageBreak')
{
$formPageCount++;
}
//echo 'render: '.$formPageCount . ' ' . $this->thisPage . ' ' . $thisField->Name.'<br>';
if ($formPageCount != $this->thisPage)
{
if (is_array($params[$this->Fields[$i]->Alias]))
{
foreach ($params[$this->Fields[$i]->Alias] as $val)
{
echo $module->CreateInputHidden($id, $this->Fields[$i]->Alias.'[]', htmlspecialchars($val,ENT_QUOTES));
}
}
else
{
echo $module->CreateInputHidden($id, $this->Fields[$i]->Alias, htmlspecialchars($params[$this->Fields[$i]->Alias],ENT_QUOTES));
}
continue;
}
if ($thisField->DisplayInForm)
{
if ($this->mod_globals->UseCSS)
{
if (! $thisField->ShowLabel)
{
if ($thisField->Required)
{
echo "<p class=\"required\">";
}
else
{
echo "<p>";
}
echo $thisField->Name;
if ($thisField->Required && strlen($this->RequiredFieldSymbol)>0)
{
echo " ".$this->RequiredFieldSymbol;
}
echo "</p>";
}
echo $thisField->WriteToPublicForm($id, $params, $return_id);
}
else
{
if ($this->mod_globals->TitlePosition == 0)
{
echo "<tr><td align=\"right\" valign=\"top\"";
}
else if ($this->mod_globals->TitlePosition == 1)
{
echo "<tr><td";
}
if (! $thisField->ShowLabel)
{
if ($thisField->Required)
{
echo " class=\"required";
if (isset($thisField->CSSClass) && $thisField->CSSClass != '')
{
echo ','.$thisField->CSSClass.'"';
}
else
{
echo '"';
}
}
else if (isset($thisField->CSSClass) && $thisField->CSSClass != '')
{
echo ' class="'.$thisField->CSSClass.'"';
}
echo ">".$thisField->Name;
if ($thisField->Required && strlen($this->RequiredFieldSymbol)>0)
{
echo " ".$this->RequiredFieldSymbol;
}
}
else
{
echo "> ";
}
if ($this->mod_globals->TitlePosition == 0)
{
echo "</td><td align=\"left\" valign=\"top\">";
}
else if ($this->mod_globals->TitlePosition == 1)
{
echo "</td></tr><tr><td>";
}
echo $thisField->WriteToPublicForm($id, $params, $return_id);
echo "</td></tr>\n";
}
}
else if ($thisField->Type == 'FunctionCallInput')
{
echo $thisField->WriteToPublicForm($id, $params, $return_id);
}
}
if (! $this->mod_globals->UseCSS)
{
echo "<tr>";
if ($this->mod_globals->TitlePosition == 0)
{
echo "<td> </td><td>";
}
else
{
echo "<td>";
}
}
if ($this->thisPage < $formPageCount)
{
echo $module->CreateInputSubmit($id, 'submit', $this->NextButtonText,$this->mod_globals->UseIDAndName?'id="'.$this->Alias.'_sub" class="ffsubmit"':'class="ffsubmit"');
}
else
{
echo $module->CreateInputSubmit($id, 'submit', $this->SubmitButtonText,$this->mod_globals->UseIDAndName?'id="'.$this->Alias.'_sub" class="ffsubmit"':'class="ffsubmit"');
}
if ($this->mod_globals->UseCSS)
{
echo "</div>\n";
}
else
{
echo "</td></tr></table>\n";
}
}
function DebugDisplay()
{
echo "Form ID: ".$this->FormId."<br />\n";
echo "Form Name: ".$this->Name."<br />\n";
echo "Form Alias: ".$this->Alias."<br />\n";
echo "Form Submit Button Text: ".$this->SubmitButtonText."<br />\n";
echo "Form Thanks Message: ".$this->ThanksMessage."<br />\n";
echo "Form CSS Class: ".$this->CSSClass."<br />\n";
echo "Form Table Name: ".$this->mod_globals->FormTableName."<br />";
echo "Field Table Name: ".$this->mod_globals->FieldTableName."<br />";
echo "Option Table Name: ".$this->mod_globals->OptionTableName."<br />";
echo "Number of fields: ".count($this->Fields)."<br />";
for($i=0;$i<count($this->Fields);$i++)
{
echo $this->Fields[$i]->DebugDisplay();
}
}
// TODO: returns the index of the added field
function AddField($fieldInfo=array())
{
$className = $this->MakeClassName($fieldInfo['type'], '');
// create the field object
$this->Fields[] = new $className($this->mod_globals, $fieldInfo);
}
function LoadForm(&$params)
{
return $this->Load($this->FormId, $params, true);
}
// loadDeep also loads all fields and options for a form.
function Load($formId, &$params, $loadDeep=false)
{
$sql = 'SELECT * FROM ' . $this->mod_globals->FormTableName . ' WHERE form_id=?';
$rs = $this->mod_globals->DBHandle->Execute($sql, array($formId));
if($rs && $rs->RecordCount() > 0)
{
$result = $rs->GetArray();
$this->FormId = $result[0]['form_id'];
$this->Name = $result[0]['name'];
$this->Alias = $result[0]['alias'];
$this->SubmitButtonText = $result[0]['submit_button_text'];
$this->ThanksMessage = $result[0]['thanks_message'];
if (ffUtilityFunctions::def($result[0]['css_class']))
{
$this->CSSClass = $result[0]['css_class'];
}
if (ffUtilityFunctions::def($result[0]['next_button_text']))
{
$this->NextButtonText = $result[0]['next_button_text'];
}
if (ffUtilityFunctions::def($result[0]['required_field_symbol']))
{
$this->RequiredFieldSymbol = $result[0]['required_field_symbol'];
}
}
else
{
return false;
}
$this->loaded = 'summary';
if ($loadDeep)
{
$sql = 'SELECT * FROM ' . $this->mod_globals->FieldTableName .
' WHERE form_id=? ORDER BY order_by';
$rs = $this->mod_globals->DBHandle->Execute($sql, array($formId));
$result = array();
if ($rs && $rs->RecordCount() > 0)
{
$result = $rs->GetArray();
}
$fieldCount = 0;
if (count($result) > 0)
{
foreach($result as $thisRes)
{
$className = $this->MakeClassName($thisRes['type'], '');
// create the field object
$tmpArray = array_merge($thisRes,$params);
$this->Fields[$fieldCount] = new $className($this->mod_globals,$this, $tmpArray);
// load its options
$this->Fields[$fieldCount]->LoadOptions($params);
$fieldCount++;
}
}
$this->loaded = 'full';
}
for ($i=0; $i < count($this->Fields); $i++)
{
//echo $this->Fields[$i]->Type.' ';
if ($this->Fields[$i]->Type == 'PageBreak')
{
$this->formTotalPages++;
}
}
return true;
}
function ListSavedFields($formId)
{
$sql = 'SELECT * FROM ' . $this->mod_globals->FieldTableName .
' WHERE form_id=? ORDER BY order_by';
$rs = $this->mod_globals->DBHandle->Execute($sql, array( $formId ));
$result = array();
if ($rs && $rs->RecordCount() > 0)
{
$result=$rs->GetRows();
}
return $result;
}
// storeDeep also stores all fields and options for a form
function Store($storeDeep=false)
{
if (intval($this->FormId) != $this->FormId)
{
return false;
}
if ($this->FormId == -1)
{
$this->FormId = $this->mod_globals->DBHandle->GenID($this->mod_globals->FormTableName.'_seq');
$sql = 'INSERT INTO ' . $this->mod_globals->FormTableName . " (form_id, name, alias, " .
"submit_button_text, next_button_text, thanks_message, css_class, required_field_symbol) ".
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
$res = $this->mod_globals->DBHandle->Execute($sql, array($this->FormId, $this->Name,
$this->Alias, $this->SubmitButtonText, $this->NextButtonText,
$this->ThanksMessage, $this->CSSClass,
$this->RequiredFieldSymbol));
}
else
{
$sql = 'UPDATE ' . $this->mod_globals->FormTableName . " set name=?, alias=?, " .
"submit_button_text=?, next_button_text=?, thanks_message=?, css_class=?, ".
"required_field_symbol=? where form_id=?";
$res = $this->mod_globals->DBHandle->Execute($sql, array($this->Name,
$this->Alias, $this->SubmitButtonText, $this->NextButtonText,
$this->ThanksMessage, $this->CSSClass,
$this->RequiredFieldSymbol, $this->FormId));
}
if ($storeDeep)
{
foreach ($this->Fields as $field)
{
$field->Store($storeDeep);
}
}
return $res;
}
function Delete()
{
if (intval($this->FormId) != $this->FormId)
{
return false;
}
if ($this->FormId == -1)
{
return false;
}
/* cascade the delete manually...
due to the table structure, we could theoretically just delete from
the FormTable, FieldTable, and OptionTable where the form_id = $this->FormId
which would obviously be faster, but we do it in this object-oriented way
in case we ever need to add some cleanup or something to this process.
*/
foreach ($this->Fields as $field)
{
$field->Delete();
}
$sql = 'DELETE FROM ' . $this->mod_globals->FormTableName . " where form_id=?";
$res = $this->mod_globals->DBHandle->Execute($sql, array($this->FormId));
return true;
}
// returns a class name, and makes sure the file where the class is
// defined has been loaded.
function MakeClassName($type, $classDirPrefix)
{
// perform rudimentary security, since Type could come in from a form
$type = preg_replace("/[\W]|\.\./", "_", $type);
if (strlen($classDirPrefix) > 0)
{
$classFile = $classDirPrefix .'/'.$type.'.class.php';
}
else
{
$classFile = $type.'.class.php';
}
require_once $classFile;
// class names are prepended with "ff" to prevent namespace clash.
return ( 'ff'.$type );
}
}
?>