setFormAction($GLOBALS['tplHttpsUrl']); //$form->setFormMethod('REQUEST'); //$form->setDatabaseTable(DB_T_PREFIX . 'example_table'); //$form->setRequiredMarkPosition(FORM_REQ_MARK_POS_RIGHT); //$form->setRequiredMark('HTML:  *'); //$form->setQuickPrintLabelSuffix(''); $fldName =& new formFieldText($form, 'Name'); $fldName->setMinLength('Your name is required.', 1); $fldName->setMaxLength('Your name cannot be longer than XXX characters.', 250); //$fldName->setDatabaseField('name'); //$fldName->setFieldClass('myClass'); //$fldName->setLabelClass('myClass'); //$fldName->quickPrintShow(false); $fldEmail =& new formFieldEmail($form, 'Email'); $fldEmail->setFormatError('Your email does not appear to be correct.'); $fldEmail->setMinLength('Your email is required.', 1); $fldEmail->setMaxLength('Your email cannot be longer than XXX characters.', 250); //-------------------------------------------------- // If the user has submitted the form if (isset($_POST['act']) && $_POST['act'] != '') { //-------------------------------------------------- // Additional validation //$fldName->setError('Set the error - e.g. This username is already in use'); //$form->addError('General form error - e.g. System error, please try again later'); //-------------------------------------------------- // If the data is valid if (!$form->hasError()) { //-------------------------------------------------- // Process //$fldName->getValue(); //$fldEmail->getValue(); //-------------------------------------------------- // Send the user onto the thank-you page redirect('./thankYou/'); } //-------------------------------------------------- // Return the error list $htmlErrorList = $form->getHtmlErrorList(); } //-------------------------------------------------- // Create the form ?>
htmlQuickPrint() ?>
getHtmlLabel() ?>: getHtmlField() ?>
//-------------------------------------------------- // End of example setup ***************************************************/ //-------------------------------------------------- // Script configuration $GLOBALS['tplErrorListId'] = 'errorList'; //-------------------------------------------------- // Easy to use constants define('FORM_REQ_MARK_POS_LEFT', 0); define('FORM_REQ_MARK_POS_RIGHT', 1); define('FORM_REQ_MARK_POS_NONE', 2); define('FORM_DATABASE_FIELD_VALUE', 0); define('FORM_DATABASE_FIELD_KEY', 1); //-------------------------------------------------- // Master form class class form { var $fields; var $fieldCount; var $formMethod; var $formAction; var $csrfToken; var $csrfError; var $htmlErrors; var $htmlRequiredMark; var $requiredMarkPosition; var $hasRunPostProcessValidation; var $databaseTableName; var $databaseTableAlias; var $databaseRecordSelect; var $databaseFieldDetails; var $databaseFieldValues; var $quickPrintLabelSuffix; function form() { //-------------------------------------------------- // Defaults $this->fields = array(); $this->fieldCount = 0; $this->formMethod = 'POST'; $this->formAction = (isset($GLOBALS['tplHttpsUrl']) ? $GLOBALS['tplHttpsUrl'] : './'); $this->csrfToken = ''; $this->csrfError = 'The request did not appear to come from a trusted source, please try again.'; $this->htmlErrors = array(); $this->htmlRequiredMark = NULL; $this->requiredMarkPosition = FORM_REQ_MARK_POS_LEFT; $this->hasRunPostProcessValidation = false; $this->databaseTableName = NULL; $this->databaseTableAlias = NULL; $this->databaseRecordSelect = NULL; $this->databaseFieldDetails = array(); $this->databaseFieldValues = array(); $this->quickPrintLabelSuffix = ':'; } function setFormMethod($method) { $this->formMethod = $method; } function getFormMethod() { return $this->formMethod; } function setFormAction($url) { //-------------------------------------------------- // Set action $this->formAction = $url; //-------------------------------------------------- // Generate a csrfToken if one does not exist. // When this function is not used, we won't be // adding CSRF protection. $this->csrfToken = returnSubmittedValue('csrfToken', 'COOKIE'); if ($this->csrfToken == '') { $this->csrfToken = rand(1000000, 9999999); } setcookie('csrfToken', $this->csrfToken, 0, '/'); // Would like to set an expiry time of 1 hour, but IE compares expiry date against computer clock (which may be wrong!). $_COOKIE['csrfToken'] = $this->csrfToken; // If there are multiple forms on the page. } function getFormAction() { if ($this->csrfToken != '') { return urlReplaceVariable('csrfToken', $this->csrfToken, $this->formAction); } else { return $this->formAction; } } function _addField(&$fieldObj) { $fieldId = $this->fieldCount++; $this->fields[$fieldId] =& $fieldObj; return $fieldId; } function getFields() { return $this->fields; } function getField($id) { return $this->fields[$id]; } function _htmlEncode($string) { if (substr($string, 0, 5) == 'HTML:') { if (substr($string, 5, 1) == ' ') { // Optional separating space return substr($string, 6); } else { return substr($string, 5); } } else { return html($string); } } function setRequiredMark($requiredMark) { $this->htmlRequiredMark = $this->_htmlEncode($requiredMark); } function setRequiredMarkPosition($position) { if ($position == FORM_REQ_MARK_POS_LEFT || $position == FORM_REQ_MARK_POS_RIGHT || $position == FORM_REQ_MARK_POS_NONE) { $this->requiredMarkPosition = $position; } else { exit('

Invalid required mark position specified - use FORM_REQ_MARK_POS_* constants

'); } } function getRequiredMark($requiredMarkPosition = FORM_REQ_MARK_POS_LEFT) { if ($this->htmlRequiredMark !== NULL) { return $this->htmlRequiredMark; } else if ($requiredMarkPosition == FORM_REQ_MARK_POS_RIGHT) { return ' *'; } else { return '* '; } } function getRequiredMarkPosition() { return $this->requiredMarkPosition; } function setQuickPrintLabelSuffix($suffix) { $this->quickPrintLabelSuffix = $suffix; } function _runPostProcessValidation() { //-------------------------------------------------- // Don't run more than once if ($this->hasRunPostProcessValidation) { return NULL; } //-------------------------------------------------- // CSRF check if ($this->csrfToken != '') { // Only test if a csrfToken has been generated by setFormAction $csrfToken = returnSubmittedValue('csrfToken', 'GET'); if ($this->csrfToken != $csrfToken) { $note = 'C:' . $this->csrfToken . ' != G:' . $csrfToken; $this->_addFieldError(-1, $this->csrfError, $note); } } //-------------------------------------------------- // Fields for ($fieldId = 0; $fieldId < $this->fieldCount; $fieldId++) { $this->fields[$fieldId]->_postProcessValidation(); } //-------------------------------------------------- // Remember this has been done $this->hasRunPostProcessValidation = true; } function getHtmlErrors() { $this->_runPostProcessValidation(); $htmlErrorsFlat = array(); for ($fieldId = -1; $fieldId < $this->fieldCount; $fieldId++) { // In field order, starting with -1 for general form errors if (isset($this->htmlErrors[$fieldId])) { foreach ($this->htmlErrors[$fieldId] as $htmlError) { $htmlErrorsFlat[] = $htmlError; } } } return $htmlErrorsFlat; } function getHtmlErrorList() { $htmlErrorsFlat = $this->getHtmlErrors(); $htmlOutput = ''; if (count($htmlErrorsFlat) > 0) { $htmlOutput = ''; } return $htmlOutput; } function _addFieldError($fieldId, $error, $hiddenInfo = NULL) { if (function_exists('formErrorOverride')) { if ($fieldId == -1) { $error = formErrorOverride($error, $this, NULL); } else { $error = formErrorOverride($error, $this, $this->getField($fieldId)); } } if (!isset($this->htmlErrors[$fieldId])) { $this->htmlErrors[$fieldId] = array(); } $htmlError = $this->_htmlEncode($error); if ($hiddenInfo !== NULL) { $htmlError .= ' '; } $this->htmlErrors[$fieldId][] = $htmlError; } function _setFieldError($fieldId, $error, $hiddenInfo = NULL) { $this->htmlErrors[$fieldId] = array(); $this->_addFieldError($fieldId, $error, $hiddenInfo); } function addError($error) { $this->_addFieldError(-1, $error); // -1 is for general errors, not really linked to a field } function addHtmlError($htmlError) { // Backwards compatibility $this->addError('HTML:' . $htmlError); } function setCsrfError($error) { $this->csrfError = $error; } function _fieldHasError($fieldId) { return ($this->hasRunPostProcessValidation && isset($this->htmlErrors[$fieldId])); } function _fieldGetErrors($fieldId) { if ($this->hasRunPostProcessValidation && isset($this->htmlErrors[$fieldId])) { return $this->htmlErrors[$fieldId]; } else { return array(); } } function hasError() { $this->_runPostProcessValidation(); return (count($this->htmlErrors) > 0); } function setDatabaseTable($sqlTable, $sqlAlias = NULL, $db = NULL) { //-------------------------------------------------- // Store $this->databaseTableName = $sqlTable; $this->databaseTableAlias = $sqlAlias; //-------------------------------------------------- // Field details $this->databaseFields = array(); if ($db == NULL) { $db = $GLOBALS['db']; } $rst = $db->query('SELECT * FROM ' . $sqlTable . ' LIMIT 0', false); // Don't return ANY data, and don't run debug (can't have it asking for "deleted" columns). for ($k = (mysql_num_fields($rst) - 1); $k >= 0; $k--) { $mysqlField = mysql_fetch_field($rst, $k); if (strpos(mysql_field_flags($rst, $k), 'enum') !== false) { $type = 'enum'; $values = $db->enumValues($sqlTable, $mysqlField->name); } else if (strpos(mysql_field_flags($rst, $k), 'set') !== false) { $type = 'set'; $values = $db->enumValues($sqlTable, $mysqlField->name); } else { $type = $mysqlField->type; $values = NULL; } $length = mysql_field_len($rst, $k); // $mysqlField->max_length returns 0 if ($length < 0) { $db->query('SHOW COLUMNS FROM ' . $sqlTable . ' LIKE "' . $db->escape($mysqlField->name) . '"'); // Backup when longtext returns -1 (Latin) or -3 (UFT8). if ($row = $db->fetchAssoc()) { if ($row['Type'] == 'tinytext') $length = 255; if ($row['Type'] == 'text') $length = 65535; if ($row['Type'] == 'longtext') $length = 4294967295; } } if ($GLOBALS['pageCharset'] == 'UTF-8' && ($type == 'blob' || $type == 'string')) { $length = ($length / 3); } $this->databaseFields[$mysqlField->name]['length'] = $length; $this->databaseFields[$mysqlField->name]['type'] = $type; $this->databaseFields[$mysqlField->name]['values'] = $values; } } function getDatabaseTable() { return $this->databaseTableName; } function getDatabaseTableFields() { return $this->databaseFields; } function setDatabaseRecordSelect($sqlWhere) { $this->databaseRecordSelect = $sqlWhere; } function getDatabaseRecordValues($db = NULL) { //-------------------------------------------------- // Validation if ($this->databaseTableName === NULL) exit('

You need to call "setDatabaseTable", on the form object

'); if ($this->databaseRecordSelect === NULL) exit('

You need to call "setDatabaseRecordSelect", on the form object

'); //-------------------------------------------------- // Fields if ($this->fieldCount == 0) { return false; } $fields = array(); for ($fieldId = 0; $fieldId < $this->fieldCount; $fieldId++) { if ($this->fields[$fieldId]->databaseFieldName !== NULL) { $fields[$fieldId] = $this->fields[$fieldId]->databaseFieldName; } } //-------------------------------------------------- // Get if ($db == NULL) { $db = $GLOBALS['db']; } $sqlTable = $this->databaseTableName . ($this->databaseTableAlias === NULL ? '' : ' AS ' . $this->databaseTableAlias); $db->select($sqlTable, $fields, $this->databaseRecordSelect); if ($row = $db->fetchAssoc()) { foreach ($row as $cField => $cValue) { $key = array_search($cField, $fields); if ($key !== false) { if ($this->fields[$key]->databaseFieldKey == FORM_DATABASE_FIELD_KEY) { $this->fields[$key]->setValueKey($cValue); } else { $this->fields[$key]->setValue($cValue); } } } } } function setDatabaseFieldValue($name, $value) { $this->databaseFieldValues[$name] = $value; } function databaseSave($db = NULL) { //-------------------------------------------------- // Validation if ($this->databaseTableName === NULL) exit('

You need to call "setDatabaseTable", on the form object

'); //-------------------------------------------------- // Values $values = array(); for ($fieldId = 0; $fieldId < $this->fieldCount; $fieldId++) { if ($this->fields[$fieldId]->databaseFieldName !== NULL) { $fieldName = $this->fields[$fieldId]->databaseFieldName; $fieldKey = $this->fields[$fieldId]->databaseFieldKey; $fieldType = $this->databaseFields[$fieldName]['type']; if ($fieldType == 'datetime' || $fieldType == 'date') { $values[$fieldName] = $this->fields[$fieldId]->getValueDate(); } else if ($fieldKey == FORM_DATABASE_FIELD_KEY) { $values[$fieldName] = $this->fields[$fieldId]->getValueKey(); } else { $values[$fieldName] = $this->fields[$fieldId]->getValue(); } } } foreach ($this->databaseFieldValues as $cName => $cValue) { // More reliable array_merge at keeping keys $values[$cName] = $cValue; } if (isset($this->databaseFields['edited'])) { $values['edited'] = date('Y-m-d H:i:s'); } if (isset($this->databaseFields['created']) && $this->databaseRecordSelect === NULL) { $values['created'] = date('Y-m-d H:i:s'); } //-------------------------------------------------- // Save if ($db == NULL) { $db = $GLOBALS['db']; } $sqlTable = $this->databaseTableName . ($this->databaseTableAlias === NULL ? '' : ' AS ' . $this->databaseTableAlias); if ($this->databaseRecordSelect === NULL) { $db->insert($sqlTable, $values); } else { $db->update($sqlTable, $values, $this->databaseRecordSelect); } } function htmlQuickPrint($group = NULL) { //-------------------------------------------------- // Backwards compatibility, for an array of fields // being passed in if (is_array($group)) { $fields = $group; $group = NULL; } else { $fields = $this->fields; } //-------------------------------------------------- // Create the output $k = 0; $htmlOutputMain = ''; $htmlOutputHidden = ''; foreach (array_keys($fields) as $fieldId) { if ($fields[$fieldId]->quickPrintShow()) { if ($group === NULL || $group == $fields[$fieldId]->getQuickPrintGroup()) { $quickPrintType = $fields[$fieldId]->getQuickPrintType(); $k++; if ($k == 1) { $fields[$fieldId]->addQuickPrintCssClass('firstChild odd'); } else if ($k % 2) { $fields[$fieldId]->addQuickPrintCssClass('odd'); } else { $fields[$fieldId]->addQuickPrintCssClass('even'); } if ($quickPrintType == 'hidden') { $htmlOutputHidden .= $fields[$fieldId]->htmlQuickPrint(); } else { $htmlOutputMain .= $fields[$fieldId]->htmlQuickPrint(); } } } } return $htmlOutputMain . $htmlOutputHidden; } } //-------------------------------------------------- // Basic form field functionality class formFieldBase { var $formObj; var $name; var $htmlLabel; var $htmlRequiredMark; var $requiredMarkPosition; var $required; var $id; var $cssClassField; var $cssClassLabel; var $databaseFieldName; var $databaseFieldKey; var $quickPrintShow; var $quickPrintGroup; var $quickPrintCssClass; var $quickPrintType; function formFieldBase(&$formObj, $label, $name = NULL) { $this->_setup($formObj, $label, $name); } function _setup(&$formObj, $label, $name) { //-------------------------------------------------- // Add this field to the form, and return a "UID" // NOTE: The "ID" is a string for the HTML $formFieldUid = $formObj->_addField($this); //-------------------------------------------------- // Get the HTML label if (function_exists('formLabelOverride')) { $label = formLabelOverride($label, $this); } $this->htmlLabel = $formObj->_htmlEncode($label); //-------------------------------------------------- // Field name if ($name === NULL) { $this->name = human2camel(substr($this->htmlLabel, 0, 20)) . $formFieldUid; } else { $this->name = $name; } //-------------------------------------------------- // Field configuration $this->formObj =& $formObj; $this->formFieldUid = $formFieldUid; $this->htmlRequiredMark = NULL; $this->requiredMarkPosition = NULL; $this->required = false; $this->id = 'fld' . ucfirst($this->name); $this->cssClassField = NULL; $this->cssClassLabel = NULL; $this->databaseFieldName = NULL; $this->databaseFieldKey = FORM_DATABASE_FIELD_VALUE; $this->quickPrintShow = true; $this->quickPrintGroup = 0; $this->quickPrintCssClass = ''; $this->quickPrintType = 'unknown'; $this->quickPrintInfoHtml = ''; $this->quickPrintLabelSuffix = $formObj->quickPrintLabelSuffix; } function setRequiredMark($requiredMark) { $this->htmlRequiredMark = $this->formObj->_htmlEncode($requiredMark); } function setRequiredMarkPosition($position) { if ($position == FORM_REQ_MARK_POS_LEFT || $position == FORM_REQ_MARK_POS_RIGHT || $position == FORM_REQ_MARK_POS_NONE) { $this->requiredMarkPosition = $position; } else { exit('

Invalid required mark position specified - use FORM_REQ_MARK_POS_* constants

'); } } function _setDatabaseField($fieldName, $fieldKey = FORM_DATABASE_FIELD_VALUE) { if (!isset($this->formObj->databaseFields[$fieldName])) { exit('

Invalid database field "' . html($fieldName) . '" set for field "' . $this->htmlLabel . '"

'); } $this->databaseFieldName = $fieldName; $this->databaseFieldKey = $fieldKey; } function setDatabaseField($field) { $this->_setDatabaseField($field); } function getDatabaseFieldName() { return $this->databaseFieldName; } function getId() { return $this->id; } function setId($id) { $this->id = $id; } function setQuickPrintCssClass($class) { $this->quickPrintCssClass = $class; } function addQuickPrintCssClass($class) { $this->quickPrintCssClass .= ($this->quickPrintCssClass == '' ? '' : ' ') . $class; } function getQuickPrintCssClass() { $class = 'row '; $class .= $this->quickPrintType . ' '; $class .= $this->quickPrintCssClass . ' '; $class .= $this->name . ' '; if ($this->hasError()) { $class .= 'error '; } return trim($class); } function getQuickPrintShow() { return $this->quickPrintShow; } function setQuickPrintShow($show) { $this->quickPrintShow = ($show == true); } function getQuickPrintType() { return $this->quickPrintType; } function getQuickPrintGroup() { return $this->quickPrintGroup; } function setQuickPrintGroup($group) { $this->quickPrintGroup = $group; } function setQuickPrintLabelSuffix($suffix) { $this->quickPrintLabelSuffix = $suffix; } function setQuickPrintInfo($info) { $this->quickPrintInfoHtml = html($info); } function setQuickPrintInfoHtml($infoHtml) { $this->quickPrintInfoHtml = $infoHtml; } function getQuickPrintInfoHtml($indent = 0) { if ($this->quickPrintInfoHtml == '') { return ''; } else { return ($indent > 0 ? "\n" : '') . str_repeat("\t", $indent) . '' . $this->quickPrintInfoHtml . ''; } } function quickPrintShow($show = NULL) { // Backwards compatibility, see getQuickPrintShow/setQuickPrintShow if ($show === true || $show === false) { $this->quickPrintShow = $show; } return $this->quickPrintShow; } function htmlQuickPrint() { $htmlOutput = '
' . $this->getHtmlLabel() . $this->quickPrintLabelSuffix . ' ' . $this->getHtmlField() . '' . $this->getQuickPrintInfoHtml(5) . '
' . "\n"; return $htmlOutput; } function getName() { return $this->name; } function setFieldClass($cssClass) { $this->cssClassField = $cssClass; } function setLabelClass($cssClass) { $this->cssClassLabel = $cssClass; } function setHtmlLabel($htmlLabel) { // No need for 'setLabel' as this is called on init $this->htmlLabel = $htmlLabel; } function getTextLabel() { return htmlDecode($this->htmlLabel); } function getHtmlLabel($htmlLabel = NULL) { //-------------------------------------------------- // Required mark position $requiredMarkPosition = $this->requiredMarkPosition; if ($requiredMarkPosition === NULL) { $requiredMarkPosition = $this->formObj->getRequiredMarkPosition(); } //-------------------------------------------------- // If this field is required, try to get a required // mark of some form if ($this->required) { $htmlRequiredMark = $this->htmlRequiredMark; if ($htmlRequiredMark === NULL) { $htmlRequiredMark = $this->formObj->getRequiredMark($requiredMarkPosition); } } else { $htmlRequiredMark = NULL; } //-------------------------------------------------- // Return the HTML for the label if ($htmlLabel === NULL) { return ''; } else { return $htmlLabel; } } function getHtmlField() { return 'ERROR'; } function _postProcessValidation() { } function setError($error) { $this->formObj->_setFieldError($this->formFieldUid, $error); } function setHtmlError($htmlError) { // Backwards compatibility $this->setError('HTML:' . $error); } function addError($error) { $this->formObj->_addFieldError($this->formFieldUid, $error); } function addHtmlError($htmlError) { // Backwards compatibility $this->addError('HTML:' . $htmlError); } function hasError() { return $this->formObj->_fieldHasError($this->formFieldUid); } function getHtmlErrors() { return $this->formObj->_fieldGetErrors($this->formFieldUid); } } //-------------------------------------------------- // Child text field class formFieldText extends formFieldBase { var $value; var $minLength; var $maxLength; function formFieldText(&$formObj, $label, $name = NULL) { $this->_setupText($formObj, $label, $name); } function _setupText(&$formObj, $label, $name = NULL) { //-------------------------------------------------- // Perform the standard field setup $this->_setup($formObj, $label, $name); //-------------------------------------------------- // Value $this->value = returnSubmittedValue($this->name, $formObj->getFormMethod()); //-------------------------------------------------- // Default configuration $this->minLength = NULL; $this->maxLength = NULL; $this->size = NULL; $this->quickPrintType = 'text'; } function setMinLength($error, $size = 1) { // Default is "required" if (strlen($this->value) < $size) { $this->formObj->_setFieldError($this->formFieldUid, str_replace('XXX', $size, $error)); } $this->minLength = $size; $this->required = ($size > 0); } function setMaxLength($error, $size = NULL) { if ($size === NULL) { if ($this->databaseFieldName === NULL) { exit('

You need to call "setDatabaseField", on the field "' . $this->htmlLabel . '"

'); } $size = $this->formObj->databaseFields[$this->databaseFieldName]['length']; } if (strlen($this->value) > $size) { $this->formObj->_setFieldError($this->formFieldUid, str_replace('XXX', $size, $error)); } $this->maxLength = $size; } function setSize($size) { $this->size = $size; } function setValue($value) { $this->value = $value; } function getValue() { return $this->value; } function getValueFormatted() { return $this->value; } function getHtmlField() { return 'size === NULL ? '' : ' size="' . intval($this->size) . '"') . ($this->cssClassField === NULL ? '' : ' class="' . html($this->cssClassField) . '"') . ' />'; } function getHtmlFieldHiddenWithValue($value) { return ''; } function getHtmlFieldHidden() { return $this->getHtmlFieldHiddenWithValue($this->value); } function _postProcessValidation() { if ($this->maxLength === NULL) { exit('

You need to call "setMaxLength", on the field "' . $this->htmlLabel . '"

'); } } } //-------------------------------------------------- // Child hidden field class formFieldHidden extends formFieldText { function formFieldHidden(&$formObj, $name) { //-------------------------------------------------- // Perform the standard field setup $this->_setupText($formObj, $name, $name); //-------------------------------------------------- // Default configuration $this->quickPrintType = 'hidden'; } function getHtmlLabel($htmlLabel = NULL) { return ''; } function getHtmlField() { return ''; } function htmlQuickPrint() { $htmlOutput = ' ' . $this->getHtmlField() . "\n"; return $htmlOutput; } function _postProcessValidation() { // Disable max length check. } } //-------------------------------------------------- // Child password field class formFieldPassword extends formFieldText { function formFieldPassword(&$formObj, $label, $name = NULL) { //-------------------------------------------------- // Perform the standard field setup $this->_setupText($formObj, $label, $name); //-------------------------------------------------- // Additional field configuration $this->quickPrintType = 'password'; } function getHtmlField() { return 'size === NULL ? '' : ' size="' . intval($this->size) . '"') . ($this->cssClassField === NULL ? '' : ' class="' . html($this->cssClassField) . '"') . ' />'; } } //-------------------------------------------------- // Child text-area field class formFieldTextArea extends formFieldText { var $textareaRows; var $textareaCols; function formFieldTextArea(&$formObj, $label, $name = NULL) { //-------------------------------------------------- // Perform the standard field setup $this->_setupText($formObj, $label, $name); //-------------------------------------------------- // Additional field configuration $this->textareaRows = 5; $this->textareaCols = 40; $this->quickPrintType = 'textarea'; } function setRows($rows) { $this->textareaRows = $rows; } function setCols($cols) { $this->textareaCols = $cols; } function getHtmlField() { return ''; } } //-------------------------------------------------- // Child number field class formFieldNumber extends formFieldText { var $formatErrorSet; function formFieldNumber(&$formObj, $label, $name = NULL) { //-------------------------------------------------- // Perform the standard field setup $this->_setupText($formObj, $label, $name); //-------------------------------------------------- // Additional field configuration $this->formatErrorSet = false; $this->setZeroToBlank = false; $this->quickPrintType = 'number'; } function setFormatError($error) { if ($this->value != '' && !is_numeric($this->value)) { $this->formObj->_setFieldError($this->formFieldUid, $error); } $this->formatErrorSet = true; } function setRequiredError($error) { $this->setMinLength($error); } function setMinValue($error, $value) { if (floatval($this->value) < $value) { $this->formObj->_setFieldError($this->formFieldUid, str_replace('XXX', $value, $error)); } } function setMaxValue($error, $value) { if (floatval($this->value) > $value) { $this->formObj->_setFieldError($this->formFieldUid, str_replace('XXX', $value, $error)); } $this->maxLength = (strlen($value) + 6); // Allow for a decimal place, plus an arbitrary 5 digits. if ($this->size === NULL && $this->maxLength < 20) { $this->size = $this->maxLength; } } function setZeroToBlank($blank) { $this->setZeroToBlank = ($blank == true); } function getValueFormatted($decimalPlaces = 2) { if ($this->value == 0 && $this->setZeroToBlank) { return ''; } else { return $this->value; } } function _postProcessValidation() { parent::_postProcessValidation(); if ($this->formatErrorSet == false) { exit('

You need to call "setFormatError", on the field "' . $this->htmlLabel . '"

'); } } } //-------------------------------------------------- // Child currency field class formFieldCurrency extends formFieldNumber { var $formatErrorSet; var $currencyChar; function formFieldCurrency(&$formObj, $label, $name = NULL) { //-------------------------------------------------- // Perform the standard field setup $this->_setupText($formObj, $label, $name); //-------------------------------------------------- // Strip currency symbol from input value $this->setValue($this->value); //-------------------------------------------------- // Additional field configuration $this->formatErrorSet = false; $this->setZeroToBlank = false; $this->currencyChar = '£'; $this->quickPrintType = 'currency'; } function setMinValue($error, $value) { if (floatval($this->value) < $value) { if ($value < 0) { $valueText = '-' . $this->currencyChar . number_format(floatval(0 - $value), 2); } else { $valueText = $this->currencyChar . number_format(floatval($value), 2); } $this->formObj->_setFieldError($this->formFieldUid, str_replace('XXX', $valueText, $error)); } $this->minLength = $value; } function setMaxValue($error, $value) { if (floatval($this->value) > $value) { if ($value < 0) { $valueText = '-' . $this->currencyChar . number_format(floatval(0 - $value), 2); } else { $valueText = $this->currencyChar . number_format(floatval($value), 2); } $this->formObj->_setFieldError($this->formFieldUid, str_replace('XXX', $valueText, $error)); } $this->maxLength = strlen(floor($value)); $this->maxLength += (intval($this->minLength) < 0 ? 1 : 0); // Negative numbers $this->maxLength += (function_exists('mb_strlen') ? mb_strlen($this->currencyChar, $GLOBALS['pageCharset']) : strlen($this->currencyChar)); $this->maxLength += (floor((strlen(floor($value)) - 1) / 3)); // Thousand separators $this->maxLength += 3; // Decimal place char, and 2 digits if ($this->size === NULL && $this->maxLength < 20) { $this->size = $this->maxLength; } } function setCurrencyChar($char) { $this->currencyChar = $char; } function setValue($value) { $this->value = preg_replace('/[^0-9\-\.]+/', '', $value); // Deletes commas (thousand separators) and currency symbols } function getValueFormatted($decimalPlaces = 2) { if ($this->value == 0 && $this->setZeroToBlank) { return ''; } else if ($this->value < 0) { return '-' . $this->currencyChar . number_format(floatval(0 - $this->value), $decimalPlaces); } else { return $this->currencyChar . number_format(floatval($this->value), $decimalPlaces); } } } //-------------------------------------------------- // Child email field class formFieldEmail extends formFieldText { var $formatErrorSet; function formFieldEmail(&$formObj, $label, $name = NULL) { //-------------------------------------------------- // Perform the standard field setup $this->_setupText($formObj, $label, $name); //-------------------------------------------------- // Additional field configuration $this->formatErrorSet = false; $this->quickPrintType = 'email'; } function setFormatError($error) { if ($this->value != '' && !isemail($this->value)) { $this->formObj->_setFieldError($this->formFieldUid, $error); } $this->formatErrorSet = true; } function _postProcessValidation() { parent::_postProcessValidation(); if ($this->formatErrorSet == false) { exit('

You need to call "setFormatError", on the field "' . $this->htmlLabel . '"

'); } } } //-------------------------------------------------- // Child URL field class formFieldUrl extends formFieldText { var $formatErrorSet; function formFieldUrl(&$formObj, $label, $name = NULL) { //-------------------------------------------------- // Perform the standard field setup $this->_setupText($formObj, $label, $name); //-------------------------------------------------- // Additional field configuration $this->formatErrorSet = false; $this->quickPrintType = 'url'; } function setFormatError($error) { if ($this->value != '') { $parts = @parse_url($this->value); if ($parts === false || !isset($parts['scheme']) || !isset($parts['host'])) { $this->formObj->_setFieldError($this->formFieldUid, $error); } } $this->formatErrorSet = true; } function setAllowedSchemes($error, $schemes) { if ($this->value != '') { $parts = @parse_url($this->value); if (isset($parts['scheme']) && !in_array($parts['scheme'], $schemes)) { $this->formObj->_setFieldError($this->formFieldUid, $error); $this->formObj->_setFieldError($this->formFieldUid, $error, 'Scheme: ' . $parts['scheme']); } } } function _postProcessValidation() { parent::_postProcessValidation(); if ($this->formatErrorSet == false) { exit('

You need to call "setFormatError", on the field "' . $this->htmlLabel . '"

'); } } } //-------------------------------------------------- // Child post code field class formFieldPostCode extends formFieldText { var $formatErrorSet; function formFieldPostCode(&$formObj, $label, $name = NULL) { //-------------------------------------------------- // Perform the standard field setup $this->_setupText($formObj, $label, $name); //-------------------------------------------------- // Additional field configuration $this->maxLength = 8; // Bypass required setMaxLength call, and to set the $this->formatErrorSet = false; $this->quickPrintType = 'postcode'; } function setFormatError($error) { if ($this->value != '') { $postcodeClean = formatBritishPostcode($this->value); if ($postcodeClean === NULL) { $this->formObj->_setFieldError($this->formFieldUid, $error); } } $this->formatErrorSet = true; } function setRequiredError($error) { if ($this->value == '') { $this->formObj->_setFieldError($this->formFieldUid, $error); } $this->required = ($error !== NULL); } function getValue() { return formatBritishPostcode($this->value); } function _postProcessValidation() { parent::_postProcessValidation(); if ($this->formatErrorSet == false) { exit('

You need to call "setFormatError", on the field "' . $this->htmlLabel . '"

'); } } } //-------------------------------------------------- // Child check box field class formFieldCheckBox extends formFieldText { var $textValueTrue; var $textValueFalse; function formFieldCheckBox(&$formObj, $label, $name = NULL) { //-------------------------------------------------- // Perform the standard field setup $this->_setupText($formObj, $label, $name); //-------------------------------------------------- // Additional field configuration $this->value = ($this->value == 'true'); $this->maxLength = -1; // Bypass the _postProcessValidation on the text field (not used) $this->quickPrintType = 'check'; $this->quickPrintInputFirst = false; $this->textValueTrue = NULL; $this->textValueFalse = NULL; } function quickPrintInputFirst($first = NULL) { if ($first === true || $first === false) { $this->quickPrintInputFirst = $first; } return $this->quickPrintInputFirst; } function setTextValues($true, $false) { $this->textValueTrue = $true; $this->textValueFalse = $false; } function setRequiredError($error) { if ($this->value !== true) { $this->formObj->_setFieldError($this->formFieldUid, $error); } $this->required = ($error !== NULL); } function getHtmlField() { return 'value ? ' checked="checked"' : '') . ($this->cssClassField === NULL ? '' : ' class="' . html($this->cssClassField) . '"') . ' />'; } function htmlQuickPrint() { if ($this->quickPrintInputFirst) { $htmlOutput = '
' . $this->getHtmlField() . ' ' . $this->getHtmlLabel() . $this->quickPrintLabelSuffix . '' . $this->getQuickPrintInfoHtml(5) . '
' . "\n"; } else { $htmlOutput = '
' . $this->getHtmlLabel() . $this->quickPrintLabelSuffix . ' ' . $this->getHtmlField() . '' . $this->getQuickPrintInfoHtml(5) . '
' . "\n"; } return $htmlOutput; } function setValue($value) { if ($this->textValueTrue !== NULL) { $this->value = ($value == $this->textValueTrue); } else { $this->value = ($value == true); } } function getValue() { if ($this->textValueTrue !== NULL) { return ($this->value ? $this->textValueTrue : $this->textValueFalse); } else { return $this->value; } } } //-------------------------------------------------- // Child check fields class formFieldCheckBoxes extends formFieldBase { function formFieldCheckBoxes(&$formObj, $label, $name = NULL) { //-------------------------------------------------- // Perform the standard field setup $this->_setup($formObj, $label, $name); //-------------------------------------------------- // Additional field configuration $this->values = array(); $this->optionValues = array(); $this->optionKeys = array(); $this->reIndexKeysInHtml = true; $this->quickPrintType = 'checkboxes'; } function setDatabaseField($field, $fieldKey = FORM_DATABASE_FIELD_VALUE) { $this->_setDatabaseField($field, $fieldKey); $fieldSetup = $this->formObj->databaseFields[$field]; if ($fieldSetup['type'] == 'enum' || $fieldSetup['type'] == 'set') { $this->setOptions($fieldSetup['values']); } } function reIndexKeysInHtml($reIndex) { // Doing this makes detection of the label option more error prone $this->reIndexKeysInHtml = ($reIndex == true); } function setOptions($options) { //-------------------------------------------------- // Store $this->optionValues = array_values($options); $this->optionKeys = array_keys($options); //-------------------------------------------------- // Update the values $this->values = array(); foreach ($this->optionKeys as $fieldId => $cKey) { if ($this->reIndexKeysInHtml) { $name = $this->name . '_' . $fieldId; } else { $name = $this->name . '_' . $cKey; } $selected = (returnSubmittedValue($name, $this->formObj->getFormMethod()) == 'true'); if ($selected) { $this->values[] = $fieldId; } } if (count($this->values) == 0) { // From hidden text field $value = returnSubmittedValue($this->name, $this->formObj->getFormMethod()); if ($value != '') { $this->setValueKey($value); } } } function setValue($value) { return $this->setValues(explode(',', $value)); } function setValues($values) { $this->values = array(); foreach ($values as $cValue) { $key = array_search($cValue, $this->optionValues); if ($key !== false && $key !== NULL) { $this->values[] = $key; } } } function setValueKey($value) { return $this->setValuesKey(explode(',', $value)); } function setValuesKey($values) { $this->values = array(); foreach ($values as $cValue) { $key = array_search($cValue, $this->optionKeys); if ($key !== false && $key !== NULL) { $this->values[] = $key; } } } function getValue() { return implode(',', $this->getValues()); } function getValues() { $return = array(); foreach ($this->values as $cId) { $return[$this->optionKeys[$cId]] = $this->optionValues[$cId]; } return $return; } function getValueKey() { return implode(',', $this->getValuesKey()); } function getValuesKey() { $return = array(); foreach ($this->values as $cId) { $return[] = $this->optionKeys[$cId]; } return $return; } function htmlQuickPrint() { $htmlOutput = '
' . $this->getHtmlLabel() . $this->quickPrintLabelSuffix . ''; foreach ($this->optionKeys as $key) { $htmlOutput .= ' ' . $this->getHtmlFieldByKey($key) . ' ' . $this->getHtmlLabelByKey($key) . ' '; } $htmlOutput .= $this->getQuickPrintInfoHtml(5) . '
' . "\n"; return $htmlOutput; } function getHtmlLabel($htmlLabel = NULL) { if ($htmlLabel === NULL) { $htmlLabel = parent::getHtmlLabel(); $htmlLabel = preg_replace('/^]+>(.*)<\/label>$/', '$1', $htmlLabel); // Ugly, but better than duplication } return $htmlLabel; } function getHtmlLabelByValue($value, $htmlLabel = NULL) { $id = array_search($value, $this->optionValues); if ($id !== false && $id !== NULL) { return $this->_getHtmlLabelById($id, $htmlLabel); } else { return 'Unknown value "' . html($value) . '"'; } } function getHtmlLabelByKey($key, $htmlLabel = NULL) { $id = array_search($key, $this->optionKeys); if ($id !== false && $id !== NULL) { return $this->_getHtmlLabelById($id, $htmlLabel); } else if ($key === NULL) { return $this->_getHtmlLabelById(NULL, $htmlLabel); // labelOption } else { return 'Unknown key "' . html($key) . '"'; } } function _getHtmlLabelById($fieldId, $htmlLabel) { if ($htmlLabel === NULL) { if ($fieldId === NULL) { $label = $this->labelOption; } else { $label = $this->optionValues[$fieldId]; } if (function_exists('formRadioLabelOverride')) { $label = formRadioLabelOverride($label, $this); } $htmlLabel = $this->formObj->_htmlEncode($label); } if ($this->reIndexKeysInHtml) { $inputId = $this->id . '_' . $fieldId; } else { $inputId = $this->id . '_' . $this->optionKeys[$fieldId]; } return ''; } function getHtmlField() { return 'Please use getHtmlFieldByValue or getHtmlFieldByKey'; } function getHtmlFieldByValue($value) { $id = array_search($value, $this->optionValues); if ($id !== false && $id !== NULL) { return $this->_getHtmlFieldById($id); } else { return 'Unknown value "' . html($value) . '"'; } } function getHtmlFieldByKey($key) { $id = array_search($key, $this->optionKeys); if ($id !== false && $id !== NULL) { return $this->_getHtmlFieldById($id); } else if ($key === NULL) { return $this->_getHtmlFieldById(-1); // labelOption } else { return 'Unknown key "' . html($key) . '"'; } } function _getHtmlFieldById($fieldId) { if ($this->reIndexKeysInHtml) { $inputId = $this->id . '_' . $fieldId; $inputName = $this->name . '_' . $fieldId; } else { $inputId = $this->id . '_' . $this->optionKeys[$fieldId]; $inputName = $this->name . '_' . $this->optionKeys[$fieldId]; } return 'values) ? ' checked="checked"' : '') . ($this->cssClassField === NULL ? '' : ' class="' . html($this->cssClassField) . '"') . ' />'; } function getFieldIdByValue($value) { $id = array_search($value, $this->optionValues); if ($id !== false && $id !== NULL) { if ($this->reIndexKeysInHtml) { return $this->id . '_' . $id; } else { return $this->id . '_' . $this->optionKeys[$id]; } } else { return 'Unknown value "' . html($value) . '"'; } } function getFieldIdByKey($key) { $id = array_search($key, $this->optionKeys); if ($id !== false && $id !== NULL) { if ($this->reIndexKeysInHtml) { return $this->id . '_' . $id; } else { return $this->id . '_' . $key; } } else { return 'Unknown key "' . html($key) . '"'; } } function getHtmlFieldHidden() { return ''; } } //-------------------------------------------------- // Child select field class formFieldSelect extends formFieldText { var $optionValues; var $optionKeys; var $labelOption; var $requiredErrorSet; var $invalidErrorSet; function formFieldSelect(&$formObj, $label, $name = NULL) { $this->_setupSelect($formObj, $label, $name); } function _setupSelect(&$formObj, $label, $name) { //-------------------------------------------------- // Perform the standard field setup $this->_setupText($formObj, $label, $name); //-------------------------------------------------- // Additional field configuration $this->maxLength = -1; // Bypass the _postProcessValidation on the text field (not used) $this->selectSize = 1; $this->optionValues = array(); $this->optionKeys = array(); $this->optGroups = NULL; $this->labelOption = NULL; $this->requiredErrorSet = false; $this->invalidErrorSet = false; $this->reIndexKeysInHtml = true; $this->quickPrintType = 'select'; } function setDatabaseField($field, $fieldKey = FORM_DATABASE_FIELD_VALUE) { $this->_setDatabaseField($field, $fieldKey); $fieldSetup = $this->formObj->databaseFields[$field]; if ($fieldSetup['type'] == 'enum') { $this->setOptions($fieldSetup['values']); } } function reIndexKeysInHtml($reIndex) { // Doing this makes detection of the label option more error prone $this->reIndexKeysInHtml = ($reIndex == true); } function setLabelOption($text = NULL) { $this->labelOption = $text; } function setOptions($options) { $this->optionValues = array_values($options); $this->optionKeys = array_keys($options); } function setOptGroups($optGroups) { $this->optGroups = $optGroups; } function setRequiredError($error) { if ($this->reIndexKeysInHtml) { $isLabel = (intval($this->value) == 0); } else { $isLabel = ($this->value == ''); // Best guess } if ($isLabel) { $this->formObj->_setFieldError($this->formFieldUid, $error); } $this->required = ($error !== NULL); $this->requiredErrorSet = true; } function setInvalidError($error) { if ($this->reIndexKeysInHtml) { $isLabel = (intval($this->value) == 0); $key = (intval($this->value) - 1); } else { $isLabel = ($this->value == ''); // Best guess $key = array_search($this->value, $this->optionKeys); } if (!$isLabel && !isset($this->optionValues[$key])) { $this->formObj->_setFieldError($this->formFieldUid, $error); } $this->invalidErrorSet = true; } function setSize($size) { $this->selectSize = $size; } function setValue($value) { $key = array_search($value, $this->optionValues); if ($key !== false && $key !== NULL) { if ($this->reIndexKeysInHtml) { $this->value = ($key + 1); } else { $this->value = $this->optionKeys[$key]; } } } function setValueKey($value) { if ($value === NULL) { if ($this->reIndexKeysInHtml) { $this->value = 0; } else { $this->value = ''; } } else { $key = array_search($value, $this->optionKeys); if ($key !== false && $key !== NULL) { if ($this->reIndexKeysInHtml) { $this->value = ($key + 1); } else { $this->value = $value; } } } } function getValue() { if ($this->reIndexKeysInHtml) { $key = (intval($this->value) - 1); return (isset($this->optionValues[$key]) ? $this->optionValues[$key] : NULL); } else { $key = array_search($this->value, $this->optionKeys); if ($key !== false && $key !== NULL) { return $this->optionValues[$key]; } else { return NULL; } } } function getValueKey() { if ($this->reIndexKeysInHtml) { $key = (intval($this->value) - 1); return (isset($this->optionKeys[$key]) ? $this->optionKeys[$key] : NULL); } else { $key = array_search($this->value, $this->optionKeys); if ($key !== false && $key !== NULL) { return $this->value; } else { return NULL; } } } function getHtmlField() { $html = ' ' . "\n\t\t\t\t\t"; return $html; } function getHtmlFieldHidden() { if ($this->labelOption === NULL && $this->value === 0 && count($this->optionValues) > 0) { return parent::getHtmlFieldHiddenWithValue(1); } else { return parent::getHtmlFieldHidden(); } } function _postProcessValidation() { parent::_postProcessValidation(); if ($this->invalidErrorSet == false) { $this->setInvalidError('An invalid option has been selected for "' . strtolower($this->htmlLabel) . '"'); } } } //-------------------------------------------------- // Child radio fields class formFieldRadios extends formFieldSelect { function formFieldRadios(&$formObj, $label, $name = NULL) { //-------------------------------------------------- // Perform the select field setup $this->_setupSelect($formObj, $label, $name); //-------------------------------------------------- // Additional field configuration $this->quickPrintType = 'radios'; } function htmlQuickPrint() { $htmlOutput = '
' . $this->getHtmlLabel() . $this->quickPrintLabelSuffix . ''; foreach ($this->optionKeys as $id => $key) { $htmlOutput .= ' ' . $this->getHtmlFieldByKey($key) . ' ' . $this->getHtmlLabelByKey($key) . ' '; } $htmlOutput .= $this->getQuickPrintInfoHtml(5) . '
' . "\n"; return $htmlOutput; } function getHtmlLabel($htmlLabel = NULL) { if ($htmlLabel === NULL) { $htmlLabel = parent::getHtmlLabel(); $htmlLabel = preg_replace('/^]+>(.*)<\/label>$/', '$1', $htmlLabel); // Ugly, but better than duplication } return $htmlLabel; } function getHtmlLabelByValue($value, $htmlLabel = NULL) { $id = array_search($value, $this->optionValues); if ($id !== false && $id !== NULL) { return $this->_getHtmlLabelById($id, $htmlLabel); } else { return 'Unknown value "' . html($value) . '"'; } } function getHtmlLabelByKey($key, $htmlLabel = NULL) { $id = array_search($key, $this->optionKeys); if ($id !== false && $id !== NULL) { return $this->_getHtmlLabelById($id, $htmlLabel); } else if ($key === NULL) { return $this->_getHtmlLabelById(NULL, $htmlLabel); // labelOption } else { return 'Unknown key "' . html($key) . '"'; } } function _getHtmlLabelById($fieldId, $htmlLabel) { if ($htmlLabel === NULL) { if ($fieldId === NULL) { $label = $this->labelOption; } else { $label = $this->optionValues[$fieldId]; } if (function_exists('formRadioLabelOverride')) { $label = formRadioLabelOverride($label, $this); } $htmlLabel = $this->formObj->_htmlEncode($label); } if ($this->reIndexKeysInHtml) { $inputId = $this->id . '_' . ($fieldId + 1); } else { $inputId = $this->id . '_' . $this->optionKeys[$fieldId]; } return ''; } function getHtmlField() { return 'Please use getHtmlFieldByValue or getHtmlFieldByKey'; } function getHtmlFieldByValue($value) { $id = array_search($value, $this->optionValues); if ($id !== false && $id !== NULL) { return $this->_getHtmlFieldById($id); } else { return 'Unknown value "' . html($value) . '"'; } } function getHtmlFieldByKey($key) { $id = array_search($key, $this->optionKeys); if ($id !== false && $id !== NULL) { return $this->_getHtmlFieldById($id); } else if ($key === NULL) { return $this->_getHtmlFieldById(-1); // labelOption } else { return 'Unknown key "' . html($key) . '"'; } } function _getHtmlFieldById($fieldId) { if ($this->reIndexKeysInHtml) { $inputId = $this->id . '_' . ($fieldId + 1); $inputValue = ($fieldId + 1); } else { $inputId = $this->id . '_' . $this->optionKeys[$fieldId]; $inputValue = $this->optionKeys[$fieldId]; } return 'value ? ' checked="checked"' : '') . ($this->cssClassField === NULL ? '' : ' class="' . html($this->cssClassField) . '"') . ' />'; } function getFieldIdByValue($value) { $id = array_search($value, $this->optionValues); if ($id !== false && $id !== NULL) { if ($this->reIndexKeysInHtml) { return $this->id . '_' . ($id + 1); } else { return $this->id . '_' . $this->optionKeys[$id]; } } else { return 'Unknown value "' . html($value) . '"'; } } function getFieldIdByKey($key) { $id = array_search($key, $this->optionKeys); if ($id !== false && $id !== NULL) { if ($this->reIndexKeysInHtml) { return $this->id . '_' . ($id + 1); } else { return $this->id . '_' . $key; } } else { return 'Unknown key "' . html($key) . '"'; } } function _postProcessValidation() { parent::_postProcessValidation(); if ($this->requiredErrorSet == false && $this->labelOption === NULL) { exit('

You need to call "setRequiredError" or "setLabelOption", on the field "' . $this->htmlLabel . '"

'); } } } //-------------------------------------------------- // Child date field class formFieldDate extends formFieldBase { var $value; var $valueProvided; var $invalidErrorSet; var $invalidErrorFound; function formFieldDate(&$formObj, $label, $name = NULL) { //-------------------------------------------------- // General setup $this->_setup($formObj, $label, $name); //-------------------------------------------------- // Field configuration $this->value = array(); $this->value['D'] = intval(returnSubmittedValue($this->name . '_D', $formObj->getFormMethod())); $this->value['M'] = intval(returnSubmittedValue($this->name . '_M', $formObj->getFormMethod())); $this->value['Y'] = intval(returnSubmittedValue($this->name . '_Y', $formObj->getFormMethod())); $this->valueProvided = ($this->value['D'] != 0 || $this->value['M'] != 0 || $this->value['Y'] != 0); //-------------------------------------------------- // Default configuration $this->invalidErrorSet = false; $this->invalidErrorFound = false; $this->quickPrintType = 'date'; } function htmlQuickPrint() { $htmlOutput = '
' . $this->getHtmlLabel() . $this->quickPrintLabelSuffix . ' ' . $this->getHtmlField('D') . ' ' . $this->getHtmlField('M') . ' ' . $this->getHtmlField('Y') . ' ' . $this->getHtmlLabelForDate() . '' . $this->getQuickPrintInfoHtml(5) . '
' . "\n"; return $htmlOutput; } function valueProvided() { return $this->valueProvided; } function setRequiredError($error) { if (!$this->valueProvided) { $this->formObj->_setFieldError($this->formFieldUid, $error); } $this->required = ($error !== NULL); } function setInvalidError($error) { $value = $this->getValueTimeStamp(); // Check upper bound to time-stamp, 2037 on 32bit systems if ($this->valueProvided && (!checkdate($this->value['M'], $this->value['D'], $this->value['Y']) || $value === false)) { $this->formObj->_setFieldError($this->formFieldUid, $error); $this->invalidErrorFound = true; } $this->invalidErrorSet = true; } function setMinDate($error, $timestamp) { if ($this->valueProvided && $this->invalidErrorFound == false) { $value = $this->getValueTimeStamp(); if ($value !== false && $value < intval($timestamp)) { $this->formObj->_setFieldError($this->formFieldUid, $error); } } } function setMaxDate($error, $timestamp) { if ($this->valueProvided && $this->invalidErrorFound == false) { $value = $this->getValueTimeStamp(); if ($value !== false && $value > intval($timestamp)) { $this->formObj->_setFieldError($this->formFieldUid, $error); } } } function setValue($value, $month = NULL, $year = NULL) { if ($month === NULL && $year === NULL) { if (!is_numeric($value)) { if ($value == '0000-00-00' || $value == '0000-00-00 00:00:00') { $value = NULL; } else { $value = strtotime($value); if ($value == 943920000) { // "1999-11-30 00:00:00", same as the database "0000-00-00 00:00:00" $value = NULL; } } } if (is_numeric($value)) { $this->value['D'] = date('j', $value); $this->value['M'] = date('n', $value); $this->value['Y'] = date('Y', $value); } } else { $this->value['D'] = intval($value); $this->value['M'] = intval($month); $this->value['Y'] = intval($year); } } function getValue($part = NULL) { if ($part == 'D' || $part == 'M' || $part == 'Y') { return $this->value[$part]; } else { return 'The date part must be set to "D", "M" or "Y"... or you could use getValueDate() or getValueTimeStamp()'; } } function getValueDate() { return str_pad(intval($this->value['Y']), 4, '0', STR_PAD_LEFT) . '-' . str_pad(intval($this->value['M']), 2, '0', STR_PAD_LEFT) . '-' . str_pad(intval($this->value['D']), 2, '0', STR_PAD_LEFT); } function getValueTimeStamp() { if ($this->value['M'] == 0 && $this->value['D'] == 0 && $this->value['Y'] == 0) { $timestamp = false; } else { $timestamp = mktime(0, 0, 0, $this->value['M'], $this->value['D'], $this->value['Y']); if ($timestamp === -1) { $timestamp = false; // If the arguments are invalid, the function returns FALSE (before PHP 5.1 it returned -1). } } return $timestamp; } function getHtmlLabel($part = 'D', $htmlLabel = NULL) { //-------------------------------------------------- // Check the part if ($part != 'D' && $part != 'M' && $part != 'Y') { return 'The date part must be set to "D", "M" or "Y"'; } //-------------------------------------------------- // Required mark position $requiredMarkPosition = $this->requiredMarkPosition; if ($requiredMarkPosition === NULL) { $requiredMarkPosition = $this->formObj->getRequiredMarkPosition(); } //-------------------------------------------------- // If this field is required, try to get a required // mark of some form if ($this->required) { $htmlRequiredMark = $this->htmlRequiredMark; if ($htmlRequiredMark === NULL) { $htmlRequiredMark = $this->formObj->getRequiredMark($requiredMarkPosition); } } else { $htmlRequiredMark = NULL; } //-------------------------------------------------- // Return the HTML for the label return ''; } function getHtmlLabelForDate($htmlSeparator = '/', $htmlDay = 'DD', $htmlMonth = 'MM', $htmlYear = 'YYYY') { return '' . $htmlSeparator . '' . $htmlSeparator . ''; } function getHtmlField($part = NULL) { if ($part == 'D' || $part == 'M' || $part == 'Y') { return 'cssClassField === NULL ? '' : ' class="' . html($this->cssClassField) . '"') . ' />'; } else { return 'The date part must be set to "D", "M" or "Y"'; } } function getHtmlFieldHidden() { $output = ''; $output .= ''; $output .= ''; return $output; } function _postProcessValidation() { if ($this->invalidErrorSet == false) { exit('

You need to call "setInvalidError", on the field "' . $this->htmlLabel . '"

'); } } } //-------------------------------------------------- // Child file field class formFieldFile extends formFieldBase { var $maxSize; var $emptyFileErrorSet; var $partialFileErrorSet; var $blankNameErrorSet; var $hasUploaded; var $valueExt; var $valueName; var $valueSize; var $valueMime; function formFieldFile(&$formObj, $label, $name = NULL) { $this->_setupFile($formObj, $label, $name); } function _setupFile(&$formObj, $label, $name = NULL) { //-------------------------------------------------- // Perform the standard field setup $this->_setup($formObj, $label, $name); //-------------------------------------------------- // Additional field configuration $this->quickPrintType = 'file'; //-------------------------------------------------- // Default validation configuration $this->maxSize = 0; $this->emptyFileErrorSet = false; $this->partialFileErrorSet = false; $this->blankNameErrorSet = false; //-------------------------------------------------- // If uploaded $this->hasUploaded = (isset($_FILES[$this->name]) && $_FILES[$this->name]['error'] != 4); // 4 = No file was uploaded (UPLOAD_ERR_NO_FILE) //-------------------------------------------------- // File values $this->valueExt = NULL; $this->valueName = NULL; $this->valueSize = NULL; $this->valueMime = NULL; if ($this->hasUploaded) { if (preg_match('/\.([a-z0-9]+)$/i', $_FILES[$this->name]['name'], $matches)) { $this->valueExt = strtolower($matches[1]); } $this->valueName = $_FILES[$this->name]['name']; $this->valueSize = $_FILES[$this->name]['size']; $this->valueMime = $_FILES[$this->name]['type']; } } function setRequiredError($error) { if (!$this->hasUploaded) { $this->formObj->_setFieldError($this->formFieldUid, $error); } $this->required = ($error !== NULL); } function setMaxSize($error, $size) { $this->maxSize = intval($size); if ($this->hasUploaded) { $error = str_replace('XXX', fileSize2human($this->maxSize), $error); if ($_FILES[$this->name]['error'] == 1) $this->formObj->_setFieldError($this->formFieldUid, $error, 'ERROR: Exceeds "upload_max_filesize" ' . ini_get('upload_max_filesize')); if ($_FILES[$this->name]['error'] == 2) $this->formObj->_setFieldError($this->formFieldUid, $error, 'ERROR: Exceeds "MAX_FILE_SIZE" specified in the html form'); if ($_FILES[$this->name]['size'] >= $this->maxSize) { $this->formObj->_setFieldError($this->formFieldUid, $error); } } } function getMaxSize() { return $this->maxSize; } function setPartialFileError($error) { if ($this->hasUploaded) { if ($_FILES[$this->name]['error'] == 3) $this->formObj->_setFieldError($this->formFieldUid, $error); } $this->partialFileErrorSet = true; } function setAllowedFileTypesMime($error, $types) { if ($this->hasUploaded && !in_array($this->valueMime, $types)) { $this->formObj->_setFieldError($this->formFieldUid, $error, 'MIME: ' . $this->valueMime); } } function setAllowedFileTypesExt($error, $types) { if ($this->hasUploaded && !in_array($this->valueExt, $types)) { $this->formObj->_setFieldError($this->formFieldUid, $error, 'EXT: ' . $this->valueExt); } } function setEmptyFileError($error) { if ($this->hasUploaded && $_FILES[$this->name]['size'] == 0) { $this->formObj->_setFieldError($this->formFieldUid, $error); } $this->emptyFileErrorSet = true; } function setBlankNameError($error) { if ($this->hasUploaded && $_FILES[$this->name]['name'] == '') { $this->formObj->_setFieldError($this->formFieldUid, $error); } $this->blankNameErrorSet = true; } function getHasUploaded() { return $this->hasUploaded; } function getValue() { exit('

Do you mean getFilePath?

'); } function getFilePath() { return (!$this->hasUploaded ? NULL: $_FILES[$this->name]['tmp_name']); } function getFileExt() { return (!$this->hasUploaded ? NULL: $this->valueExt); } function getFileName() { return (!$this->hasUploaded ? NULL: $this->valueName); } function getFileSize() { return (!$this->hasUploaded ? NULL: $this->valueSize); } function getFileMime() { return (!$this->hasUploaded ? NULL: $this->valueMime); } function getHtmlField() { return 'cssClassField === NULL ? '' : ' class="' . html($this->cssClassField) . '"') . ' />'; } function saveFileTo($path) { if ($this->hasUploaded && is_writable(dirname($path))) { return move_uploaded_file($_FILES[$this->name]['tmp_name'], $path); } return false; } function _postProcessValidation() { if ($this->maxSize == 0) { exit('

You need to call "setMaxSize", on the field "' . $this->htmlLabel . '"

'); } if (isset($_SERVER['CONTENT_TYPE']) && $_SERVER['CONTENT_TYPE'] == 'application/x-www-form-urlencoded') { // If not set, assume its correct exit('

The form needs the attribute: enctype="multipart/form-data"

'); } if ($this->emptyFileErrorSet == false) { // Provide default $this->setEmptyFileError('The uploaded file for "' . strtolower($this->htmlLabel) . '" is empty'); } if ($this->partialFileErrorSet == false) { // Provide default $this->setPartialFileError('The uploaded file for "' . strtolower($this->htmlLabel) . '" was only partially uploaded'); } if ($this->blankNameErrorSet == false) { // Provide default $this->setBlankNameError('The uploaded file for "' . strtolower($this->htmlLabel) . '" does not have a filename'); } } } //-------------------------------------------------- // Child file image field class formFieldImage extends formFieldFile { var $minWidthSize; var $maxWidthSize; var $minHeightSize; var $maxHeightSize; var $fileTypeErrorSet; var $valueImageWidth; var $valueImageHeight; var $valueImageType; function formFieldImage(&$formObj, $label, $name = NULL) { //-------------------------------------------------- // Perform the standard field setup $this->_setupFile($formObj, $label, $name); //-------------------------------------------------- // Additional field configuration $this->quickPrintType = 'image'; //-------------------------------------------------- // Default validation configuration $this->minWidthSize = 0; $this->maxWidthSize = 0; $this->minHeightSize = 0; $this->maxHeightSize = 0; $this->fileTypeErrorSet = false; //-------------------------------------------------- // File values $this->valueImageWidth = NULL; $this->valueImageHeight = NULL; $this->valueImageType = NULL; if ($this->hasUploaded) { $dimensions = getimagesize($this->getFilePath()); if ($dimensions !== false) { $this->valueImageWidth = $dimensions[0]; $this->valueImageHeight = $dimensions[1]; $this->valueImageType = $dimensions[2]; } } } function setMinWidth($error, $size) { $size = intval($size); if ($this->valueImageWidth !== NULL && $this->valueImageWidth < $size) { $this->formObj->_addFieldError($this->formFieldUid, str_replace('XXX', $size . 'px', $error)); } $this->minWidthSize = $size; } function setMaxWidth($error, $size) { $size = intval($size); if ($this->valueImageWidth !== NULL && $this->valueImageWidth > $size) { $this->formObj->_addFieldError($this->formFieldUid, str_replace('XXX', $size . 'px', $error)); } $this->maxWidthSize = $size; } function setRequiredWidth($error, $size) { $size = intval($size); if ($this->valueImageWidth !== NULL && $this->valueImageWidth != $size) { $this->formObj->_addFieldError($this->formFieldUid, str_replace('XXX', $size . 'px', $error)); } $this->minWidthSize = $size; $this->maxWidthSize = $size; } function getRequiredWidthMin() { return $this->minWidthSize; } function getRequiredWidthMax() { return $this->maxWidthSize; } function getRequiredWidth() { return $this->minWidthSize; } function setMinHeight($error, $size) { $size = intval($size); if ($this->valueImageHeight !== NULL && $this->valueImageHeight < $size) { $this->formObj->_addFieldError($this->formFieldUid, str_replace('XXX', $size . 'px', $error)); } $this->minHeightSize = $size; } function setMaxHeight($error, $size) { $size = intval($size); if ($this->valueImageHeight !== NULL && $this->valueImageHeight > $size) { $this->formObj->_addFieldError($this->formFieldUid, str_replace('XXX', $size . 'px', $error)); } $this->maxHeightSize = $size; } function setRequiredHeight($error, $size) { $size = intval($size); if ($this->valueImageHeight !== NULL && $this->valueImageHeight != $size) { $this->formObj->_addFieldError($this->formFieldUid, str_replace('XXX', $size . 'px', $error)); } $this->minHeightSize = $size; $this->maxHeightSize = $size; } function getRequiredHeightMin() { return $this->minHeightSize; } function getRequiredHeightMax() { return $this->maxHeightSize; } function getRequiredHeight() { return $this->minHeightSize; } function setFileTypeError($error, $types = NULL) { //-------------------------------------------------- // Types if ($types === NULL) { $types = array('gif', 'jpg', 'png'); } //-------------------------------------------------- // Validate the mime type $mimeTypes = array(); if (in_array('gif', $types)) { $mimeTypes[] = 'image/gif'; } if (in_array('jpg', $types)) { $mimeTypes[] = 'image/jpeg'; $mimeTypes[] = 'image/pjpeg'; // The wonderful world of IE } if (in_array('png', $types)) { $mimeTypes[] = 'image/png'; $mimeTypes[] = 'image/x-png'; // The wonderful world of IE } parent::setAllowedFileTypesMime($error, $mimeTypes); //-------------------------------------------------- // Could not use getimagesize if ($this->hasUploaded) { if ($this->valueImageType == NULL) { $this->formObj->_setFieldError($this->formFieldUid, $error, 'ERROR: Failed getimagesize'); } else { $valid = false; if (in_array('gif', $types) && $this->valueImageType == IMAGETYPE_GIF) $valid = true; if (in_array('jpg', $types) && $this->valueImageType == IMAGETYPE_JPEG) $valid = true; if (in_array('png', $types) && $this->valueImageType == IMAGETYPE_PNG) $valid = true; if (!$valid) { $this->formObj->_setFieldError($this->formFieldUid, $error, 'ERROR: Non valid type (' . implode(', ', $types) . ')'); } } } //-------------------------------------------------- // Done $this->fileTypeErrorSet = true; } function setUnrecognisedFileType($error) { // Backwards compatibility $this->setFileTypeError($error); } function getImageWidth() { return $this->valueImageWidth; } function getImageHeight() { return $this->valueImageHeight; } function getImageType() { return $this->valueImageType; } function _postProcessValidation() { parent::_postProcessValidation(); if ($this->fileTypeErrorSet == false) { exit('

You need to call "setFileTypeError", on the field "' . $this->htmlLabel . '"

'); } } } //-------------------------------------------------- // Copyright (c) 2007, Craig Francis All rights // reserved. // // Redistribution and use in source and binary forms, // with or without modification, are permitted provided // that the following conditions are met: // // * Redistributions of source code must retain the // above copyright notice, this list of // conditions and the following disclaimer. // * Redistributions in binary form must reproduce // the above copyright notice, this list of // conditions and the following disclaimer in the // documentation and/or other materials provided // with the distribution. // * Neither the name of the author nor the names // of its contributors may be used to endorse or // promote products derived from this software // without specific prior written permission. // // This software is provided by the copyright holders // and contributors "as is" and any express or implied // warranties, including, but not limited to, the // implied warranties of merchantability and fitness // for a particular purpose are disclaimed. In no event // shall the copyright owner or contributors be liable // for any direct, indirect, incidental, special, // exemplary, or consequential damages (including, but // not limited to, procurement of substitute goods or // services; loss of use, data, or profits; or business // interruption) however caused and on any theory of // liability, whether in contract, strict liability, or // tort (including negligence or otherwise) arising in // any way out of the use of this software, even if // advised of the possibility of such damage. //-------------------------------------------------- ?>