File: D:/HostingSpaces/PvdBoogaard/indoorski.nl/backup/oude-site/cms/api/form/class.field.php
<?php
/**
* This file contains the iwp_field class
*
* @version $Id$
*
*
* @package IWP
* @subpackage IWP_FormFields
*/
/**
* Field Base Class
* This class is extended on by all the form field types. It contains all the common functions and variables.
*
* @package IWP
* @subpackage IWP_FormFields
*/
class iwp_field extends iwp_base {
/**
* Attributes
* This holds any attributes that need to be placed on the field's tag. E.g. class="classname", onKeyUp="JFunction();"
*
* @var array
*/
protected $Attributes = array();
/**
* Language
* This holds the names of the language pack variables to be used when parsing the template.
*
* @var array
*/
protected $Language = array('Name'=>'', 'Label'=>'', 'HelpText'=>'');
protected $OverwriteLabel = false;
/**
* FieldName
* This is the name of the field. Its used in the name attribute and also the ID attribute
*
* @var string
*/
protected $FieldName = '';
protected $showName = true;
protected $showLabel = true;
protected $showHelpTip = true;
/**
* Append
* Any text or HTML to place straight after the field tag.
*
* @var string
*/
protected $Append = '';
/**
* Prepend
* Any text or HTML to place straight before the field tag.
*
* @var string
*/
protected $Prepend = '';
/**
* FieldValue
* This is the value of the field. This is set with either a default value or the actual value when editing something.
*
* @var string
*/
protected $FieldValue = '';
/**
* And array of methods used to validate a field. These methods are called in the validation class.
*
* @var array
*/
protected $Validation = array();
/**
* The name of the module being used if this field is apart of a module
*
* @var string
*/
protected $moduleName = '';
/**
* Flag to show the required asterisk label. For visual purposes only, does not affect validation.
*
* @var boolean
*/
public $Required = false;
/**
* An array of javascript files that are required by this field. This is used to keep the requests for each javascript file down to one.
*
* @var array
*/
public $RequiredJs = array();
/**
* FieldOptions
* This is the list of possible options for any multi-opotioned fields
*/
public $FieldOptions = array();
/**
* __construct
* Sets up the field name if it is passed in during the initialization
*
* @param Name string The name of the field being created
*/
public function __construct($name=null){
if(!is_null($name)){
$this->FieldName = $name;
}
}
/**
* SetLang
* The form.row template has placeholders for language text in different places. This function sets the Language variable which
* links those placeholders with the language pack variables for this
*
* @return iwp_field
*
* @see Language
*/
public function SetLang($langName, $LangPackName){
$this->Language[$langName] = $LangPackName;
return $this;
}
/**
* Sets the label override value.
*
* @param string $value The string to use as a label instead of language text.
* @return iwp_field
*/
public function SetLabel($value){
$this->OverwriteLabel = $value;
return $this;
}
/**
* GetOutput
* Returns the HTML for this field. Includes the field, language variables and helptip.
*
* @return string Returns the HTML from the form.row template
*/
public function GetOutput(){
return '';
}
/**
* SetAttribute
* This sets the values of the class variable Attributes. These are used for any additional atttributes on the field tag.
*
* @return iwp_field Doesn't return anything
*
* @see Attributes
*/
public function SetAttribute($name, $value, &$form=false){
$this->Attributes[$name] = $value;
if($form !== false){
$form->RunJsOnPageLoad($value);
}
return $this;
}
/**
* Append
* This sets Append variable, which is any HTML of text that is placed straight after the input field tag
*
* @return iwp_field
*
* @see Append
*/
public function Append($text){
$this->Append = $this->Append . $text;
return $this;
}
/**
* Prepend
* This sets Prepend variable, which is any HTML of text that is placed straight before the input field tag
*
* @return iwp_field
*
* @see Prepend
*/
public function Prepend($text){
$this->Prepend = $text . $this->Prepend;
return $this;
}
/**
* Sets the value of $FieldValue
*
* @return iwp_field
*
* @see FieldValue
**/
public function Value($value){
$this->FieldValue = $value;
return $this;
}
/**
* Returns the value of $FieldValue
*
* @return mixed
*
* @see FieldValue
*/
public function GetValue () {
return $this->FieldValue;
}
/**
* Sets the value of $moduleName
*
* @return void
*
* @see ModuleName
**/
public function SetModuleName ($value) {
$this->moduleName = $value;
}
/**
* Gets the value of $FieldName
*
* @return void
*
* @see FieldValue
**/
public function FieldName(){
return $this->FieldName;
}
/**
* Gets the key used in the Post data. Usually this is the field name, for some fields it prepended by different text.
*
* @return void
*
* @see FieldValue
**/
public function PostFieldName(){
return $this->FieldName;
}
/**
* This function extracts this objects value from an array of request data
*
* @param array $arrData Either a POST or GET request, filtered or not. The keys must be the field names
*
* @return iwp_field
*
* @see $FieldValue
*/
public function LoadValue($arrData){
if(isset($arrData[$this->FieldName])){
// $this->Append('<!-- here in exists for ' .$this->FieldName .' -->');
// $this->Append('<!-- value ' . $arrData[$this->FieldName] .' -->');
$this->FieldValue = $arrData[$this->FieldName];
}
return $this;
}
/**
* This function returns the value of a given attribute
*
* @param string $name The name of the attribute to get the value for
*
* @return mixed String value if its a valid attribute, false if its invalid.
*/
public function GetAttribute($name){
if (is_Array($this->Attributes) && sizeof($this->Attributes)>0 && array_key_exists($name, $this->Attributes)) {
return $this->Attributes[$name];
}
return false;
}
/**
* This gets the attributes for the current field in a formatted way ready for use in a HTML tag
*
* @return string A string of attributes with values ready for use in a HTML tag
*/
public function GetAttributes(){
$inputField = '';
if(is_array($this->Attributes) && sizeof($this->Attributes)>0){
foreach($this->Attributes as $name=>$value){
$inputField .= ' '.$name.'="'. iwp_htmlspecialchars($value, '"') .'"';
}
}
return $inputField;
}
/**
* This function should be extended on to output the field in HTML. This parent/base function can be used to run common actions for all fields.
*
* @return void
*/
public function GetFieldOutput(){
$this->LoadLanguage();
$this->template->Assign('helpid', substr(md5(mt_rand(3200,15926)),0, 5));
$this->template->Assign('required', $this->Required);
$this->template->Assign('fieldType', $this->type);
$this->template->Assign('showLabel', $this->showLabel);
$this->template->Assign('showName', $this->showName);
$this->template->Assign('showHelpTip', $this->showHelpTip);
}
/**
* Helper and chaining function that sets the Required flag to true
* @return iwp_field The current instance of this object
*/
public function IsRequired() {
$this->Required = true;
return $this;
}
/**
* This function loads the language file for this field
*
* @return iwp_field
*/
public function LoadLanguage () {
foreach ($this->Language as $langName=>$LangPackName) {
if(iwp_strtolower($langName) == 'label' && $this->OverwriteLabel !== false) {
$this->template->Assign($langName, $this->OverwriteLabel);
continue;
}
if(iwp_strtolower($langName) == 'label' && !$this->showLabel) {
continue;
}
if(iwp_strtolower($langName) == 'name' && !$this->showName) {
continue;
}
if($LangPackName == ''){
$LangPackName = 'field_'.$this->FieldName.'_'.$langName;
$this->Language[$langName] = $LangPackName;
}
$this->template->Assign($langName, $this->lang->Get($LangPackName));
}
return $this;
}
/**
* This adds an option to the array of field options
*
* @param string|array $name The name of the option. Or, key/value associative array of field options to add.
* @param string $value The value of the option. This parameter is ignored if the first parameter is an array.
*
* @return iwp_field
*
* @see $FieldOptions
*/
public function AddFieldOption($name, $value = ''){
if (is_array($name)) {
foreach ($name as $key => $val) {
$this->FieldOptions[$key] = $val;
}
} else {
$this->FieldOptions[$name] = $value;
}
return $this;
}
/**
* This function generates the javascript validation code for this field
*
* @return string Returns a javascript string ready for output between some script tags
*/
public function GetFieldValidation(){
$return = '';
if(is_array($this->Validation) && sizeof($this->Validation) > 0){
$return = "result = validate('".$this->FieldName."',['";
$return .= implode("', '", $this->Validation);
$return .= "']);
if(result != true){
errormsgs.push(result);
}
";
}
return $return;
}
/**
* GetNameJavascript
* This sets up the name and language value of the field for use in javascript. At present its used in validation error messages
*
* @return array An array of fieldname and language pack value for the field name
*/
public function GetNameJavascript(){
if(!$this->showName){
return '';
}
$LangPackName = 'field_'.$this->FieldName.'_Name';
return array($this->FieldName, iwp_js($this->lang->Get($LangPackName)));
}
/**
* AddValidation
* Adds a validation method to the member array, Validation
*
* @return iwp_field
*/
public function AddValidation($val){
$this->Validation[] = $val;
return $this;
}
/**
* RemoveValidation
* Removes a validation by name.
*
* @param string $remove The validation to remove
* @return iwp_field
*/
public function RemoveValidation ($remove) {
foreach ($this->Validation as $index => $val) {
if ($remove === $val) {
array_splice($this->Validation, $index, 1);
}
}
return $this;
}
/**
* HasValidation
* Returns true if this field has the given named validation
*
* @param string $check The validation to check
* @return boolean
*/
public function HasValidation ($check) {
foreach ($this->Validation as $val) {
if ($val === $check) {
return true;
}
}
return false;
}
/**
* SaveField
* Base function that might not do anything, only some fields will use it, but it will be called on all fields when looping.
*
* @return void
*/
public function SaveField($array){
if($array) {
$array = $array;
}
return;
}
/**
* GetValidations
* Returns the member variable Validation, which is an array of validation methods set for this field
*
* @return array Returns the array of validation methods
*/
public function GetValidations() {
return $this->Validation;
}
/**
* GetFieldOptions
* Returns the value of the FieldOptions member variable
*
* @return array Returns the array of field options
*/
public function GetFieldOptions() {
return $this->FieldOptions;
}
/**
* DisableLabel
* To turn off the label for the field given
*
* @return iwp_field
**/
public function DisableLabel () {
$this->showLabel = false;
return $this;
}
/**
* DisableName
* To turn off the name for the field given
*
* @return iwp_field
*/
public function DisableName () {
$this->showName = false;
return $this;
}
/**
* DisableHelpTip
* To turn off the help tip for the field given
*
* @return iwp_field
*/
public function DisableHelpTip () {
$this->showHelpTip = false;
return $this;
}
/**
* EnableLabel
* To turn on the label for the field given
*
* @return iwp_field
**/
public function EnableLabel () {
$this->showLabel = true;
return $this;
}
/**
* EnableName
* To turn on the name for the field given
*
* @return iwp_field
*/
public function EnableName () {
$this->showName = true;
return $this;
}
/**
* EnableHelpTip
* To turn on the help tip for the field given
*
* @return iwp_field
*/
public function EnableHelpTip () {
$this->showHelpTip = true;
return $this;
}
}