<?php
// +----------------------------------------------------------------------+
// | PHP version 4.0.6 |
// | Netcentralen Shared v4 |
// +----------------------------------------------------------------------+
// | This source code is completely free. |
// +----------------------------------------------------------------------+
// | abstraction.php |
// | Abstract PHP functions to replace HTML |
// +----------------------------------------------------------------------+
// | Authors: Allan Hansen <hide@address.com> |
// | Authors: Radek Maciaszek <hide@address.com> |
// +----------------------------------------------------------------------+
//
// $Id: abstraction.php,v 1.2 2002/06/15 12:48:36 chief Exp $
function QuoteString( $str ) {
return stripslashes( ereg_replace( "\"", """, strip_tags($str) ) );
}
//////////////////////////////////////////////////////////////////////////////
// Simple HTML Abstraction ///////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* Outputs text with class.
*/
function TextOut($text, $class = "std", $attr = false)
{
echo "<span class=\"$class\"";
if(!empty($attr)) echo " $attr>";
else echo ">";
echo "$text</span>";
}
/**
* Outputs linefeeds.
*/
function Linefeed($n=1, $abs='')
{
if ($abs)
$abs= " clear=all";
echo str_repeat("<br$abs />\n", $n);
}
//////////////////////////////////////////////////////////////////////////////
// Forms /////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
Class for generating forms.
@version 2
*/
class Form
{
// private: variables
var $Name; // Name of form.
var $Elements; // Array of elements: array ( array("fisk", "radio"), ...
var $LastDateFieldType; // YMD order of last date field
var $FocusElement = -1; // Element on which to focus with javascript.
var $CheckCode; // Javascript code for CheckFormX()
var $ExtraCodePre; // Extra javascript when drawing form
var $ExtraCodePost; // Extra javascript before submit
var $Fclass; // Standard class for fields
var $Bclass; // Standard class for buttons
var $Lclass; // Standard class for labels
var $RFclass; // Standard class for readonly fields
var $RLclass; // Standard class for "readonly" labels
var $ValuesFrom; // Object or array containing default values.
/**
* Constructor
*
* @access public
* @param string action URL to POST or GET
* @param string name HTML name of form.
* @param string attr Additional HTML attributes, e.g. "target=main"
*/
function Form($action = './', $name = false, $attr = false)
{
// Init
$this->Elements = array ();
// Default values from $_REQUEST
$this->ValuesFrom = &$GLOBALS["_REQUEST"];
// Generate name if none is specified.
if (!$name)
$name = "Form" . uniqid(42);
$this->Name = $name;
$this->CheckCode = "";
$this->ExtraCodePre = "";
// Look for method=GET in $attr, set to POST is not present
if (!stristr(" $attr", " method=") || !strstr($attr, "GET"))
$attr = "enctype=\"multipart/form-data\" method=\"POST\" $attr";
// Output form header
echo "\n\n<form action=\"$action\" name=$name $attr onsubmit=\"return Check$name();\">\n";
}
/**
* Destructor - finalises table
*
* @access public
*/
function done( $blockade = false )
{
// Output form footer
echo "\n</form>\n\n";
// Output javascript
echo "<script>\n";
if($blockade) echo "var submitted = 0;\n";
// Focus
if ($this->FocusElement != -1) {
// Get element name from FocusElement number
foreach ($this->Elements as $array) {
list($element, $type) = $array;
if (!$this->FocusElement)
break;
$this->FocusElement--;
}
// Set focus on selected or first element in form
echo "if (!document.$this->Name.$element.disabled)\n";
echo " document.$this->Name.elements['$element'].focus();\n\n";
}
// Output Additional code
if(!empty($this->ExtraCodePre))
echo $this->ExtraCodePre;
// Output CheckForm code
echo "function Check" . $this->Name . "()\n{\n";
if(!empty($this->CheckCode))
echo $this->CheckCode;
echo "\n";
if(!empty($this->ExtraCodePost))
echo $this->ExtraCodePost;
echo "\n";
if($blockade) {
echo "if(!submitted) {\n";
echo "submitted = 1;\n";
echo "return true;\n";
echo "} else return false;\n";
} else echo " return true;\n";
echo "}\n\n</script>\n\n";
}
/**
* Define standard CSS classes for form elements.
*
* @access public
* @param string fields Standard class used on text and password fields + text areas + combo boxes.
* @param string buttons Standard class used on buttons
* @param string labels Standard class used on labels (checkboxes and radiobuttons)
* @param string freadonly Standard class used on readonly fields
* @param string lreadonly Standard class used on readonly labels (checkboxes and radiobuttons)
*/
function DefClasses($fields, $buttons, $labels = false, $freadonly = false, $lreadonly = false)
{
$this->Fclass = $fields;
$this->Bclass = $buttons;
$this->Lclass = $labels;
$this->RFclass = $freadonly;
$this->RLclass = $freadonly;
}
/**
* Set object or array to get default values from.
*
* Defaults to $_REQUEST.
*
* @access public
* @param mixed from Array or object.
*/
function ValuesFrom(&$from)
{
$this->ValuesFrom = &$from;
}
/**
* Get default value for object.
*
* @access private
* @param string key Array index or class variable.
*/
function GetValue($key)
{
//echo "key: ".$key." val: ".$this->ValuesFrom[$key];
if (is_array($this->ValuesFrom) && !strstr($key, "[")) {
if(isset($this->ValuesFrom[$key]))
return $this->ValuesFrom[$key];
else return null;
} else if($true_key = strstr($key, "[")) {
$true_key = substr( $true_key, 1, strlen($true_key)-2 );
$strlen = strlen((string)$key) - strlen((string)$true_key) - 2;
$true_name = substr( $key, 0, $strlen );
if(isset($this->ValuesFrom[$true_name][$true_key]))
return $this->ValuesFrom[$true_name][$true_key];
else return null;
}
if (isset($this->ValuesFrom->$key)) {
return $this->ValuesFrom->$key;
} else return null;
}
/**
* Execute javascript before submitting form.
*
* @access public
* @param string code Javascript
*/
function JavascriptBeforeSubmit($code)
{
$this->ExtraCodePost .= "\n$code\n";
}
/**
* Set focus on last element.
*
* @access public
*/
function SetFocus() // Set focus on last element
{
$this->FocusElement = sizeof($this->Elements) - 1;
}
/**
* Convert fail criteria to javascript.
*
* @access private
* @return string javascript code
*/
function BuildFailCriteria($fail_criteria)
{
// Get number of form elements
$num = sizeof($this->Elements);
$result = "";
foreach (explode("\r", ereg_replace("(¤+)", "\r\\1\r", $fail_criteria)) as $part) {
if ($part && $part[0] == '¤') {
$len = strlen($part);
list($element) = $this->Elements[$num - $len];
if (strstr($element, "[")) // element name has index, not supported by js, use element number instead
$element = 'elements['.($num - $len).']';
$result .= 'document.' . $this->Name . ".$element";
} else {
$result .= $part;
}
}
return $result;
}
/**
* Validate form with javascript.
*
* @access public
* @param string fail_criteria Partial javascript code. Eg. ¤.value>5 && ¤.value.lengt>3
* ¤ = this element ¤¤ = previous element ¤¤¤ = ....
* @param string errmsg Show this message if last criteria fails.
* @param string focus_element Element on which to focus after error message was displayed.
*/
function Validate($fail_criteria, $errmsg, $focus_element= '¤')
{
// Build criteria.
$fail_criteria = $this->BuildFailCriteria($fail_criteria);
// Get name and type of focus element. (assume it is only ¤¤¤s)
list($element, $type) = $this->Elements[sizeof($this->Elements) - strlen($focus_element)];
$element = "document." . $this->Name . ".elements['$element']";
if (stristr($errmsg, "default::")) {
$errmsg = ereg_replace( "default::", "", $errmsg );
$errmsg = "Wypełnienie pola '$errmsg' jest obowi±kowe.";
}
// Output checkcode
$this->CheckCode .= " if(!$element.disabled) {\n";
$this->CheckCode .= " if ($fail_criteria)\n";
$this->CheckCode .= " { alert(\"$errmsg\");\n";
if (in_array($type, array ("text", "password", "textarea", "file")))
$this->CheckCode .= " $element.select();\n";
$this->CheckCode .= " $element.focus();\n";
$this->CheckCode .= " return false;\n";
$this->CheckCode .= " }\n}\n\n";
}
/**
* Validate (with javascript) last field as e-mail address.
*
* @access public
* @param string errmsg Error message to show if e-mail is incorrect.
* @param string allowblank Allow user not to enter e-mail.
*/
function ValidateEmail($errmsg, $allowblank=false)
{
if (!strstr($this->ExtraCodePre, "function email_ok(field)")) {
$this->ExtraCodePre .= "function email_ok(field)\n{\n";
$this->ExtraCodePre .= " var email= field.value;\n";
$this->ExtraCodePre .= " var reg1 = /^[-\.\{\}\|\*\+\$\^\?!#%&'/=_`~a-zA-Z0-9]+@([-\.a-zA-Z0-9]+\.[a-zA-Z]{2,6})$/;\n"; // Valid match
$this->ExtraCodePre .= " var reg2 = /\.\.|-\.|\.-|^-/;\n"; // Invalid match on @... part
$this->ExtraCodePre .= " if (reg1.test(email))\n";
$this->ExtraCodePre .= " { dom= email.substr(email.indexOf('@')+1, email.length);\n";
$this->ExtraCodePre .= " return (!reg2.test(dom));\n";
$this->ExtraCodePre .= " }\n";
$this->ExtraCodePre .= " return false;\n";
$this->ExtraCodePre .= "}\n\n";
}
if (!$allowblank)
$this->Validate("!email_ok(¤)", $errmsg, '¤');
else
$this->Validate("¤.value.length && !email_ok(¤)", $errmsg, '¤');
}
/**
* Validate (with javascript) last field as login
*
* @access public
* @param string errmsg Error message to show if e-mail is incorrect.
* @param string allowblank Allow user not to enter e-mail.
*/
function ValidateLogin($min, $max, $errmsg)
{
$this->Validate('!(/^[a-z0-9_\-]*$/.test(¤.value))', $errmsg);
if ($min != "NONE")
$this->Validate("¤.value.length < $min", $errmsg, "¤");
if ($max != "NONE")
$this->Validate("¤.value.length > $max", $errmsg, "¤");
}
/**
* Validate (with javascript) last three fields as a date set.
*
* @access public
* @param string min Minimum value (ISO format) or NONE.
* @param string max Maximum value (ISO format) or NONE.
* @param string errmsg Show this message if last criteria fails.
*/
function ValidateDate($min, $max, $errmsg)
{
// Get order
list($y, $m, $d) = $this->LastDateFieldType;
// Validate
if ($min != "NONE")
$this->Validate("('0000' + $y.value).substr($y.value.length, 4) + '-' + ('00' + $m.value).substr($m.value.length, 2) + '-' + ('00' + $d.value).substr($d.value.length, 2) < '$min'", $errmsg);
if ($max != "NONE")
$this->Validate("('0000' + $y.value).substr($y.value.length, 4) + '-' + ('00' + $m.value).substr($m.value.length, 2) + '-' + ('00' + $d.value).substr($d.value.length, 2) > '$max'", $errmsg);
}
/**
* Validate (with javascript) last field as an integer.
*
* @access public
* @param string min Minimum value or NONE.
* @param string max Maximum value or NONE.
* @param string errmsg Show this message if last criteria fails.
*/
function ValidateInteger($min, $max, $errmsg)
{
$this->Validate('!(/^[0-9]*$/.test(¤.value))', $errmsg);
if ($min != "NONE")
$this->Validate("¤.value < $min", $errmsg, "¤");
if ($max != "NONE")
$this->Validate("¤.value > $max", $errmsg, "¤");
}
/**
* Validate (with javascript) last field as a string.
*
* @access public
* @param string min Minimum value (ISO format) or NONE.
* @param string max Maximum value (ISO format) or NONE.
* @param string errmsg Show this message if last criteria fails.
*/
function ValidateString($min, $max, $errmsg)
{
if ($min != "NONE")
$this->Validate("¤.value.length < $min", $errmsg, "¤");
if ($max != "NONE")
$this->Validate("¤.value.length > $max", $errmsg, "¤");
}
/**
* Validate (with javascript) last field as combobox.
*
* Fails if first entry is selected.
*
* @access public
* @param string min Minimum value (ISO format) or NONE.
* @param string max Maximum value (ISO format) or NONE.
* @param string errmsg Show this message if last criteria fails.
*/
function ValidateSelectBox($errmsg)
{
$this->Validate('¤.selectedIndex == 0', $errmsg);
}
/**
* Validate (with javascript) last fields as group of radio checkboxes.
*
* Fails if first entry is selected.
*
* @access public
* @param string num Number of fields in group.
* @param string min Minimum number of checked fields or NONE.
* @param string max Maximum number of checked fields or NONE.
* @param string errmsg Show this message if last criteria fails.
*/
function ValidateCheckGroup($num, $min = NONE, $max = NONE, $errmsg)
{
// Count number of checked boxes
$e = "";
$v = "";
$p = "";
while ($num--) {
$e .= '¤';
$v .= "$p$e.checked";
$p = " + ";
}
// Validate
if ($min != "NONE")
$this->Validate("$v < $min", $errmsg, "$e");
if ($max != "NONE")
$this->Validate("$v > $max", $errmsg, "$e");
}
/**
* Output a hidden field.
*
* Value comes from array or object defined with ValuesFrom.
* Default from $_REQUEST[$name].
*
* @access public
* @param string name Name of hidden field.
* @param string value Static value, ignore $_REQUEST[$name] and $GLOBALS[$name]
*/
function HiddenField($name, $value = null)
{
// Get value
if (is_null($value))
$value = $this->GetValue($name);
// Convert to HTML
$value = QuoteString($value);
// Output hidden field
echo "<input type=hidden name=$name value=\"".$value."\">";
// Store element in Elements array
array_push($this->Elements, array ($name, "hidden"));
}
/**
* Output a radio button.
*
* Checked button comes from array or object defined with ValuesFrom.
* Default from $_REQUEST[$name].
*
* @access public
* @param string name Name of radio group.
* @param string value Value for this radio button.
* @param string label Text label.
* @param string attr Additional HTML attributes, e.g. "id=main"
*/
function RadioButton($name, $value, $label, $attr = false, $js = false )
{
// Get value from name[]
if (ereg("^(.*)\[(.*)\]$", $name, $regs)) {
$val = $this->GetValue($regs[1]);
$check = $val[$regs[2]] == $value;
// Get value from name
} else {
$val = $this->GetValue($name);
$check = $val == $value;
}
// Convert $check to HTML
$check = ($check) ? "checked" : "";
// Render label grey if attr contains "disabled".
if (stristr(" $attr ", " disabled "))
$label = "<span style=\"color: #999999\"><font color=\"#999999\">$label</font></span>";
elseif (!empty($this->Lclass))
$label = "<span class=" . $this->Lclass . ">$label</span>";
// Output radio button
echo "\n<input type=radio name=$name value=\"$value\" $js $check>$label";
// Store element in Elements array
array_push($this->Elements, array ($name, "radio"));
}
/**
* Output a checkbox
*
* Checked comes from array or object defined with ValuesFrom.
* Default from $_REQUEST[$name].
*
* @access public
* @param string name Name of check box.
* @param string label Text label.
* @param string attr Additional HTML attributes, e.g. "id=main"
*/
function CheckBox($name, $label = null, $attr = false, $value = null)
{
$val = $this->GetValue($name);
// Get value from name[]
if (ereg("^(.*)\[(.*)\]$", $name, $regs)) {
$val = $this->GetValue($regs[1]);
$check = (isset($val[$regs[2]])) ? $val[$regs[2]] : null;
// Get value from name
} else
$check = $this->GetValue($name);
// Convert $check to HTML
$check = $check ? "checked" : "";
// Render label grey if attr contains "disabled".
if (stristr(" $attr ", " disabled "))
$label = "<span style=\"color: #999999\"><font color=\"#999999\">$label</font></span>";
elseif (isset($this->Lclass))
$label = "<span class=".$this->Lclass.">$label</span>";
// Output radio button
echo "\n<input type=checkbox name=$name $check $attr";
if($value!==null) echo " value=$value";
echo ">$label";
// Store element in Elements array
array_push($this->Elements, array ($name, "checkbox"));
}
/**
* Output a select box
*
* Selected element comes from array or object defined with ValuesFrom.
* Default from $_REQUEST[$name].
*
* @access public
* @param string name Name of select box.
* @param array items Associative array with items: $value => $label.
* @param string attr Additional HTML attributes, e.g. "id=main"
*/
function SelectBox($name, $items, $attr = false)
{
// Get value
$select = $this->GetValue($name);
//echo "<br>name: ".$name;
// Use RFclass if no class or style is specified in attr and field readonly.
if (!empty($this->RFclass) && stristr(" $attr", " readonly") && !stristr(" $attr", " class=") && !stristr(" $attr", " style="))
$attr .= " class=$this->RFclass";
// Use Fclass if no class or style is specified in attr.
elseif (!empty($this->Fclass) && !stristr(" $attr", " class=") && !stristr(" $attr", " style="))
$attr .= " class=$this->Fclass";
// Output box header
echo "<select name=$name $attr>";
// Output box elements
foreach ($items as $value => $label) {
$selected = $select == $value ? "selected" : "";
echo "<option value=\"$value\" $selected>$label</option>";
}
// Output box footer
echo "</select>";
// Store element in Elements array
array_push($this->Elements, array ($name, "select"));
}
/**
* Output a submit button.
*
* @access public
* @param string label Label on button.
* @param string attr Additional HTML attributes, e.g. "id=main"
* @param string $change_action Change action to this value before submit.
*/
function SubmitButton($label, $attr = false, $change_action = false)
{
// Use Bclass if no class or style is specified in attr.
if (!empty($this->Bclass) && !stristr(" $attr", " class=") && !stristr(" $attr", " style="))
$attr .= " class=".$this->Bclass;
// Change form action
if ($change_action)
$change_action = "onclick=\"$this->Name.action='$change_action';\"";
// Output button
echo "<input type=submit value=\"$label\" $attr $change_action>";
// Store element in Elements array
//array_push($this->Elements, array ($name, "submit"));
}
/**
* Output a reset button.
*
* @access public
* @param string label Label on button.
* @param string attr Additional HTML attributes, e.g. "id=main"
*/
function ResetButton($label, $attr = false)
{
// Use Bclass if no class or style is specified in attr.
if (!empty($this->Bclass) && !stristr(" $attr", " class=") && !stristr(" $attr", " style="))
$attr .= " class=".$this->Bclass;
// Output button
echo "<input type=reset value=\"$label\" $attr>";
// Store element in Elements array
// array_push($this->Elements, array ($name, "reset"));
}
/**
* Output a cancel button.
*
* @access public
* @param string label Label on button.
* @param string attr Additional HTML attributes, e.g. "id=main"
*/
function CancelButton($label, $attr = false, $url = false)
{
// Use Bclass if no class or style is specified in attr.
if (!empty($this->Bclass) && !stristr(" $attr", " class=") && !stristr(" $attr", " style="))
$attr .= " class=".$this->Bclass;
if($url) {
global $SERVER_NAME;
$js_back = "window.location.href='http://".$SERVER_NAME."/".$url."'";
} else $js_back = "javascript:history.back()";
// Output button
echo "<input type=\"Button\" onclick=\"".$js_back."\" value=\"$label\" $attr>";
// Store element in Elements array
// array_push($this->Elements, array ($name, "reset"));
}
/**
* Output a button.
*
* @access public
* @param string label Label on button.
* @param string attr Additional HTML attributes, e.g. "id=main"
*/
function Button($label, $attr = false)
{
// Use Bclass if no class or style is specified in attr.
if (!empty($this->Bclass) && !stristr(" $attr", " class=") && !stristr(" $attr", " style="))
$attr .= " class=".$this->Bclass;
// Output button
echo "<input type=button value=\"$label\" $attr>";
// Store element in Elements array
//array_push($this->Elements, array ($name, "reset"));
}
/**
* Output a text field.
*
* Value comes from array or object defined with ValuesFrom.
* Default from $_REQUEST[$name].
*
* @access public
* @param string name Name of text field.
* @param string attr Additional HTML attributes, e.g. "id=main"
*/
function TextField($name, $attr = false)
{
// Get value
$value = $this->GetValue($name);
// Convert to HTML
$value = QuoteString($value);
// Use RFclass if no class or style is specified in attr and field readonly.
if (isset($this->RFclass) && stristr(" $attr", " readonly") && !stristr(" $attr", " class=") && !stristr(" $attr", " style="))
$attr .= " class=$this->RFclass";
// Use Fclass if no class or style is specified in attr.
if (isset($this->Fclass) && !stristr(" $attr", " class=") && !stristr(" $attr", " style="))
$attr .= " class=".$this->Fclass;
// Output text field
echo "<input type=text name=$name value=\"".$value."\" $attr>";
// Store element in Elements array
array_push($this->Elements, array ($name, "text"));
}
/**
* Output a password field.
*
* Value comes from array or object defined with ValuesFrom.
* Default from $_REQUEST[$name].
*
* @access public
* @param string name Name of password field.
* @param string attr Additional HTML attributes, e.g. "id=main"
*/
function PasswordField($name, $attr = false)
{
// Get value
$value = $this->GetValue($name);
// Convert to HTML
$value = QuoteString($value);
// Use RFclass if no class or style is specified in attr and field readonly.
if (isset($this->RFclass) && stristr(" $attr", " readonly") && !stristr(" $attr", " class=") && !stristr(" $attr", " style="))
$attr .= " class=$this->RFclass";
// Use Fclass if no class or style is specified in attr.
if (isset($this->Fclass) && !stristr(" $attr", " class=") && !stristr(" $attr", " style="))
$attr .= " class=".$this->Fclass;
// Output text field
echo "<input type=password name=$name value=\"$value\" $attr>";
// Store element in Elements array
array_push($this->Elements, array ($name, "password"));
}
/**
* Output a text area.
*
* Value comes from array or object defined with ValuesFrom.
* Default from $_REQUEST[$name].
*
* @access public
* @param string name Name of text area.
* @param string attr Additional HTML attributes, e.g. "id=main"
*/
function TextArea($name, $attr = false, $quote = true)
{
// Get value
$value = $this->GetValue($name);
// Convert to HTML
if($quote) {
$value = QuoteString($value);
} else {
$value = ereg_replace( "\"", """, $value );
}
// Use RFclass if no class or style is specified in attr and field readonly.
if (!empty($this->RFclass) && stristr(" $attr", " readonly") && !stristr(" $attr", " class=") && !stristr(" $attr", " style="))
$attr .= " class=$this->RFclass";
// Use Fclass if no class or style is specified in attr.
if (!empty($this->Fclass) && !stristr(" $attr", " class=") && !stristr(" $attr", " style="))
$attr .= " class=".$this->Fclass;
// Output text field
echo "<textarea name=\"$name\" $attr>$value</textarea>";
// Store element in Elements array
array_push($this->Elements, array ($name, "textarea"));
}
/**
* Output a file upload field.
*
* @access public
* @param string name Name of file upload field.
* @param string attr Additional HTML attributes, e.g. "id=main"
*/
function FileUpload($name, $attr = false)
{
// Use RFclass if no class or style is specified in attr and field readonly.
if (!empty($this->RFclass) && stristr(" $attr", " readonly") && !stristr(" $attr", " class=") && !stristr(" $attr", " style="))
$attr .= " class=$this->RFclass";
// Use Fclass if no class or style is specified in attr.
if (!empty($this->Fclass) && !stristr(" $attr", " class=") && !stristr(" $attr", " style="))
$attr .= " class=".$this->Fclass;
// Output text field
echo "<input type=file name=$name $attr>";
// Store element in Elements array
array_push($this->Elements, array ($name, "file"));
}
/**
* Output three form fields that act as a date contruction with javascript validation.
*
* type: ISO YYYY-MM-DD
* UK MM/DD/YYYY
* DK DD-MM-YYYY
*
* validation: NONE no validation.
* NORMAL require valid date.
* EMPTY require valid date or empty.
*
* Value comes from array or object defined with ValuesFrom.
* Default from $_REQUEST[$name].
*
* @access public
* @param string name Name of file upload field.
* @param integer type Type of field.
* @param integer validation Type of validation.
* @param string attr Additional HTML attributes, e.g. "id=main"
*/
function DateField($name, $type = "ISO1", $validate = "NORMAL", $attr = false)
{
// Get value
$value = $this->GetValue($name);
// Extract parts and save in global vars.
$GLOBALS["${name}__yy"] = substr($value, 0, 4);
$GLOBALS["${name}__mm"] = substr($value, 5, 2);
$GLOBALS["${name}__dd"] = substr($value, 8, 2);
// Output a hidden field that will contain date afterwards. Require javascript.
$this->HiddenField($name, 'javascript required');
$fname = $this->Name;
$this->JavascriptBeforeSubmit("document.$fname.$name.value = ('0000' + document.$fname.${name}__yy.value).substr(document.$fname.${name}__yy.value.length, 4) + '-' + ('00' + document.$fname.${name}__mm.value).substr(document.$fname.${name}__mm.value.length, 2) + '-' + ('00' + document.$fname.${name}__dd.value).substr(document.$fname.${name}__dd.value.length, 2);");
// Output fields according to type
if ($type == "ISO") {
// Save order for validation
list($y, $m, $d) = array ("¤¤¤", "¤¤", "¤");
// Output fields
$this->Textfield("${name}__yy", "size=4");
echo " - ";
$this->Textfield("${name}__mm", "size=2");
echo " - ";
$this->Textfield("{$name}__dd", "size=2");
} elseif ($type == "DK") {
// Save order for validation
list($y, $m, $d) = array ("¤", "¤¤", "¤¤¤");
// Output fields
$this->Textfield("{$name}__dd", "size=2");
echo " - ";
$this->Textfield("${name}__mm", "size=2");
echo " - ";
$this->Textfield("${name}__yy", "size=4");
} elseif ($type == "UK") {
// Save order for validation
list($y, $m, $d) = array ("¤", "¤¤¤", "¤¤");
// Output fields
$this->Textfield("${name}__mm", "size=2");
echo " / ";
$this->Textfield("{$name}__dd", "size=2");
echo " / ";
$this->Textfield("${name}__yy", "size=4");
}
// Validate, correct date or empty
if ($validate != "NONE") {
// Validate in correct order
for ($i = 3; $i >= 1; $i--) {
if (strlen($y) == $i)
$this->Validate("!((!$d.value.length && !$m.value.length & !$y.value.length) || ($y.value >= 1900 && $y.value <= 2100))", $this->msg(INCORRECT_YEAR), "$y");
if (strlen($m) == $i)
$this->Validate("!((!$d.value.length && !$m.value.length & !$y.value.length) || ($m.value > 0 && $m.value < 13))", $this->msg(INCORRECT_MONTH), "$m");
if (strlen($d) == $i) {
$this->Validate("!((!$d.value.length && !$m.value.length & !$y.value.length) || ($d.value > 0 && $d.value < 32))", $this->msg(INCORRECT_DATE), "$d");
$this->Validate("$d.value == 31 && ($m.value == 4 || $m.value == 6 || $m.value == 9 || $m.value == 11)", $this->msg(INCORRECT_DATEMONTH), "$d");
$this->Validate("$d.value > 29 && $m.value == 2", $this->msg(INCORRECT_DATEMONTH), "$d");
$this->Validate("$d.value == 29 && $m.value == 2 && ($y.value % 4) > 0", $this->msg(INCORRECT_DATE), "$d");
}
}
}
// Not empty
if ($validate == "NORMAL")
$this->Validate("!$d.value.length && !$m.value.length && !$y.value.length", $this->msg(MUST_SPECIFY_DATE), "¤¤¤");
// Save type if we desire to validate further
$this->LastDateFieldType = array($y, $m, $d);
}
/**
* Error messages in different languages.
*
* Set language with define("CFG_LANGUAGE", "EN");
* EN is two letter ISO language code.
*
* @access private
* @param string msg Name of message.
*/
function msg($label)
{
// Messages
static $messages = array (
INCORRECT_DATE => array ( DA => "Dato angivet forkert.",
EN => "Specified date is incorrect." ),
INCORRECT_MONTH => array ( DA => "Mĺned angivet forkert.",
EN => "Specified month is incorrect." ),
INCORRECT_YEAR => array ( DA => "Ĺr angivet forkert.",
EN => "Specified year is incorrect." ),
INCORRECT_DATEMONTH => array ( DA => "Dato/mĺned angivet forkert.",
EN => "Specified month/date is incorrect."),
MUST_SPECIFY_DATE => array ( DA => "Dato skal angives.",
EN => "Date must be specified." )
);
// Default language to English
define("CFG_LANGUAGE", "EN");
return $messages[$label][CFG_LANGUAGE];
}
}
?>