File: D:/web/itpartnership.net (cms)/modules/FeedbackForm/classes/Input.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 ffInput {
var $FieldId=-1;
var $FormId;
var $Name;
var $Alias;
var $Type;
var $Length=-1;
var $ValidationType;
var $CSSClass;
var $Required;
var $ShowLabel;
var $OrderBy;
var $Options;
var $DisplayLength;
var $DisplayType;
var $ValidationTypes;
var $DisplayInForm;
var $Value;
var $mod_globals;
var $formRef;
var $loaded;
// special inputs don't get the usual stuff like "required" or CSSClass inputs
var $specialInput;
function ffInput(&$mod_globals, $formRef, $params=array())
{
$this->mod_globals = &$mod_globals;
$this->formRef = &$formRef;
$this->Options = array();
if (ffUtilityFunctions::def($params['form_id']))
{
$this->FormId = $params['form_id'];
}
if (ffUtilityFunctions::def($params['field_id']))
{
$this->FieldId = $params['field_id'];
}
if (ffUtilityFunctions::def($params['name']))
{
$this->Name = $params['name'];
}
if (!ffUtilityFunctions::def($params['alias']))
{
$this->Alias = ffUtilityFunctions::MakeAlias($this->Name);
}
else
{
// trust not!
$this->Alias = ffUtilityFunctions::MakeAlias($params['alias']);
}
if (ffUtilityFunctions::def($params['type']))
{
$this->Type = $params['type'];
}
if (ffUtilityFunctions::def($params['css_class']))
{
$this->CSSClass = $params['css_class'];
}
if (ffUtilityFunctions::def($params['validation_type']))
{
$this->ValidationType = $params['validation_type'];
}
if (ffUtilityFunctions::def($params['length']))
{
$this->Length = $params['length'];
}
if (ffUtilityFunctions::def($params['order_by']))
{
$this->OrderBy = $params['order_by'];
}
if (ffUtilityFunctions::def($params['required']))
{
$this->Required = $params['required'];
}
if (ffUtilityFunctions::def($params['show_label']))
{
$this->ShowLabel = $params['show_label'];
}
if (ffUtilityFunctions::def($params[$this->Alias]))
{
$this->Value = $params[$this->Alias];
}
$this->DisplayLength = 25;
$this->DisplayInForm = true;
$this->IsDisposition = false;
$this->ValidationTypes = array($this->mod_globals->Lang('validation_none')=>'none');
$this->loaded = 'not';
$this->specialInput = false;
}
// override me with a form input string or something
function WriteToPublicForm($id, &$params, $return_id)
{
echo "";
}
// override me with something to show users
function StatusInfo()
{
return '';
}
// override me.
// I return an array of Title / Input hashes.
// The Title will be displayed if it has a length;
// the "Input" should be a Form input for that field attribute/option
function RenderAdminForm($formDescriptor)
{
return array();
}
// override me. Returns an array: first value is a true or false (whether or not
// the value is valid), the second is a message
function Validate()
{
return array(true,'');
}
// override me. Returns the (possibly converted) value of the field.
function GetValue()
{
if (ffUtilityFunctions::def($this->Value))
{
return $this->Value;
}
else
{
return $this->mod_globals->Lang('unspecified');
}
}
function isRequired()
{
return $this->Required;
}
function showLabel()
{
return $this->ShowLabel;
}
function requiresValidation()
{
if ($this->ValidationType == 'none')
{
return false;
}
else
{
return true;
}
}
// override me, but call me first!. Returns an array: first value is a true or
// false (whether or not the value is valid), the second is a message
function AdminValidate()
{
$ret = array(true,'');
if ($this->NameExists())
{
$ret = array(false,$this->mod_globals->Lang('field_name_in_use1').' "'.$this->Name.'" '. $this->mod_globals->Lang('field_name_in_use2'));
}
return $ret;
}
// override me if you're a Form Disposition pseudo-field. This method can do just
// about anything you want it to, in order to handle form contents.
// it receives the rest of the form as an array of name/value arrays.
function DisposeForm($formName, &$config, $results)
{
return false;
}
function DebugDisplay()
{
echo "- Field ID: ".$this->FieldId ."<br />";
echo "- Form ID: ".$this->FormId ."<br />";
echo "- Field Name: ".$this->Name ."<br />";
echo "- Field Alias: ".$this->Alias ."<br />";
echo "- Field Type: ".$this->Type ."<br />";
echo "- Field Show In Form: ".$this->DisplayInForm ."<br />";
echo "- Field is a Disposition: ".$this->IsDisposition ."<br />";
echo "- Field Length: ".$this->Length ."<br />";
echo "- Field Validation Type: ".$this->ValidationType ."<br />";
echo "- Field CSS Class: ".$this->CSSClass ."<br />";
echo "- Field Order By: ".$this->OrderBy ."<br />";
echo "- Field Display Length: ".$this->DisplayLength ."<br />";
echo "- Field Value: ".$this->Value ."<br />";
echo "- Field Required? ".$this->Required."<br />";
echo "- Field Validation Types: <br />";
foreach ($this->ValidationTypes as $vn=>$vv)
{
echo "-- ".$vn."<br />\n";
}
echo "- Field Option Count:".count($this->Options)."<br />";
echo "- Field Options: <br />";
for ($i=0;$i<count($this->Options);$i++)
{
$this->Options[$i]->DebugDisplay();
}
}
function GetOptionNames()
{
$ret = array();
foreach ($this->Options as $opt)
{
$ret[] = $opt->Name;
}
return $ret;
}
// returns an array of Option references. This will be easier in PHP5, where
// I can foreach with references.
function GetOptionByName($optionName)
{
$ret = array();
for($i=0;$i<count($this->Options);$i++)
{
if ($this->Options[$i]->Name == $optionName)
{
$ret[] = &$this->Options[$i];
}
}
return $ret;
}
// returns an array of Option references. This will be easier in PHP5, where
// I can foreach with references.
function GetOptionByKind($optionKind)
{
$ret = array();
for($i=0;$i<count($this->Options);$i++)
{
if ($this->Options[$i]->Kind == $optionKind)
{
$ret[] = &$this->Options[$i];
}
}
return $ret;
}
// returns an array of Option references. This will be easier in PHP5, where
// I can foreach with references.
function GetOptionById($optionId)
{
$ret = array();
for($i=0;$i<count($this->Options);$i++)
{
if ($this->Options[$i]->OptionId == $optionId)
{
$ret[] = &$this->Options[$i];
}
}
return $ret;
}
function GetOptionValues()
{
$ret = array();
foreach ($this->Options as $opt)
{
$ret[] = $opt->Value;
}
return $ret;
}
function LoadField($params)
{
if ($this->FieldId > 0)
{
$this->Load($this->FieldId, $params, true);
}
return;
}
// loadDeep also loads all options for a field.
function Load($fieldId, $params=array(), $loadDeep=false)
{
//$this->mod_globals->DBHandle->debug=true;
$sql = 'SELECT * FROM ' . $this->mod_globals->FieldTableName . ' WHERE field_id=?';
$rs = $this->mod_globals->DBHandle->Execute($sql, array($fieldId));
if($rs && $rs->RecordCount() > 0)
{
$result = $rs->GetArray();
$this->FieldId = $result[0]['field_id'];
$this->FormId = $result[0]['form_id'];
$this->Name = $result[0]['name'];
$this->Alias = ffUtilityFunctions::MakeAlias($result[0]['name']);
$this->Type = $result[0]['type'];
$this->Length = $result[0]['length'];
$this->ValidationType = $result[0]['validation_type'];
$this->OrderBy = $result[0]['order_by'];
if (ffUtilityFunctions::def($result[0]['css_class']))
{
$this->CSSClass = $result[0]['css_class'];
}
else
{
$this->CSSClass = '';
}
if (ffUtilityFunctions::def($result[0]['required']))
{
$this->Required = $result[0]['required'];
}
if (ffUtilityFunctions::def($result[0]['show_label']))
{
$this->ShowLabel = $result[0]['show_label'];
}
}
else
{
return false;
}
$this->loaded = 'summary';
if ($loadDeep)
{
$this->LoadOptions($params);
$this->loaded = 'full';
}
return true;
}
function addListParam($optkind, $optNameParam, $optValParam, $params)
{
if (ffUtilityFunctions::def($params[$optNameParam]) && ffUtilityFunctions::def($params[$optValParam]))
{
if (is_array($params[$optNameParam]))
{
for ($i=0;$i<count($params[$optNameParam]);$i++)
{
if (strlen($params[$optNameParam][$i]) > 0)
{
$this->AddOption($optkind, $params[$optNameParam][$i],$params[$optValParam][$i]);
}
}
}
else
{
$this->AddOption($optkind, $params[$optNameParam],$params[$optValParam]);
}
}
}
function AddOption($optionKind, $optionName, $optionValue)
{
$optCount = count($this->Options);
$this->Options[$optCount] = new ffOption($this->mod_globals, array(
'form_id'=>$this->FormId,
'field_id'=>$this->FieldId,
'option_id'=>-1,
'kind'=>$optionKind,
'name'=>$optionName,
'value'=>$optionValue
)
);
}
function NerfHTML($input)
{
$input = preg_replace("/\</", "<", $input);
$input = preg_replace("/\"/", """, $input);
$input = preg_replace("/\'/", "´", $input);
return $input;
}
function NameExists()
{
$sql = 'SELECT field_id FROM ' . $this->mod_globals->FieldTableName . ' WHERE name=? and form_id=?';
$rs = $this->mod_globals->DBHandle->Execute($sql, array($this->Name,$this->FormId));
if ($rs && $rs->RecordCount() > 0)
{
$result = $rs->GetArray();
if ($result[0]['field_id'] != $this->FieldId)
{
return true;
}
}
return false;
}
function LoadOptions($params)
{
$sql = 'SELECT * FROM ' . $this->mod_globals->OptionTableName . ' WHERE field_id=? ORDER BY option_id';
$rs = $this->mod_globals->DBHandle->Execute($sql, array($this->FieldId));
$results = array();
if ($rs && $rs->RecordCount() > 0)
{
$results = $rs->GetArray();
}
foreach ($results as $result)
{
$this->Options[] = new ffOption($this->mod_globals,array_merge($result,$params));
}
$this->loaded = 'opts';
}
function Store($storeDeep=false)
{
if ($this->FieldId == -1)
{
$this->FieldId = $this->mod_globals->DBHandle->GenID($this->mod_globals->FieldTableName.'_seq');
$sql = 'INSERT INTO ' .
$this->mod_globals->FieldTableName .
" (field_id, form_id, name, type, " .
"length, validation_type,css_class," .
"required,show_label,order_by) ".
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$res = $this->mod_globals->DBHandle->Execute($sql,
array($this->FieldId, $this->FormId,
$this->Name, $this->Type, $this->Length,
$this->ValidationType, $this->CSSClass,
$this->Required?1:0, $this->ShowLabel?1:0, $this->OrderBy));
}
else
{
$sql = 'UPDATE ' . $this->mod_globals->FieldTableName . " set name=?, type=?, " .
"length=?, validation_type=?, css_class=?, required=?, order_by=?, show_label=? where field_id=?";
$res = $this->mod_globals->DBHandle->Execute($sql, array($this->Name,
$this->Type, $this->Length, $this->ValidationType, $this->CSSClass,
$this->Required?1:0, $this->OrderBy, $this->ShowLabel?1:0, $this->FieldId));
}
if ($storeDeep)
{
// drop old options
$sql = 'DELETE FROM ' . $this->mod_globals->OptionTableName . " where field_id=?";
$res = $this->mod_globals->DBHandle->Execute($sql, array($this->FieldId));
foreach ($this->Options as $option)
{
// if this is a new field, we need to give Options their Field ID.
$option->FieldId = $this->FieldId;
$option->Store();
}
}
return $res;
}
function Delete()
{
if ($this->FieldId == -1)
{
return false;
}
// cascade the delete manually... See note in the Delete method of ffForm class.
foreach ($this->Options as $opt)
{
$opt->Delete();
}
$sql = 'DELETE FROM ' . $this->mod_globals->FieldTableName . " where field_id=?";
$res = $this->mod_globals->DBHandle->Execute($sql, array($this->FieldId));
return true;
}
}
?>