<?php
/**
 * FormVerifier class for verifying form data.
 * See formtest.php for usage examples.
 *
 * @author Ed Parrish
 * @version 1.1 11/23/05
 */
class FormVerifier {
    /*--- private variables ---*/
    var $_errorList = array();
    var $_errColor;

    /*--- General purpose functions ---*/

    /**
     * Constructor
     *
     * @param $errColor The color used to display error messages.
     */
    function FormVerifier($errColor = "red") {
        $this->_errColor = $errColor;
    }

    /**
     * Returns the value of a form field (name).
     *
     * @param $field The form field to get the value from.
     * @param $defaultVal The initial value for a form control.
     */
    function getValue($field, $defaultVal = "") {
        if (isset($_REQUEST[$field])) return $_REQUEST[$field];
        return $defaultVal;
    }

    /*--- Error tracking and reporting functions ---*/

    /**
     * Add errors to the error list
     *
     * @param $field The form field where the error occurred.
     * @param $value The value of the form field with the error.
     * @param $msg The error message presented to the user.
     */
    function addError($field, $value, $msg) {
        if (empty($this->_errorList[$field])) {
            $this->_errorList[$field] = array(
                "field" => $field,
                "value" => $value,
                "msg" => $msg);
        } else { // allow for multiple errors
            $errArray = $this->_errorList[$field];
            // Do not add if a duplicate message
            if ($errArray["msg"] != $msg) {
                $this->_errorList[] = array(
                    "field" => $field,
                    "value" => $value,
                    "msg" => $msg);
             }
        }
    }

    /**
     * Returns true if there are any error on the list, otherwise returns
     * false.
     */
    function isError() {
        if ($this->_errorList) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Reset the error list to an empty list.
     */
    function resetErrorList() {
        $this->_errorList = array();
    }

    /**
     * Highlight $formElement if the $field has an error
     *
     * @param $field The form field where the error might have occurred.
     * @param $formElement The HTML code to display.
     */
    // NTR: $html = "<span class=\"error\">";
    // NTR: $html .= '<h1>(!)</h1>'.$formElement.'</span>';
    function formatOnError($field, $formElement) {
        $html = $formElement;
        if (isset($this->_errorList[$field])) {
            $html = "<font color=\"$this->_errColor\">";
            $html .= $formElement.'</font>';
        }
        return $html;
    }

    /**
     * Returns a default HTML message listing the errors found.
     */
    function reportErrors() {
        $html = "";
        if ($this->isError()) {
            $html = "<b>We found some error(s) in the data.</b>
            <p>Please resubmit after making these changes:";
            $html .= "<ul>";
            foreach ($this->_errorList as $err) {
                $html .= '<li>'.$err['msg'];
            }
            $html .= "</ul>";
        }
        return $html;
    }

    /*--- User input verification functions ---*/
    // isXXX or else add to error list
    // if isXXX then add to error list

    /**
     * Adds a $msg to the list if the form control $field is empty.
     *
     * @param $field The form field to check.
     * @param $msg The error message presented to the user.
     */
    // NTR: should be isNotEmpty
    function isEmpty($field, $msg) {
        $value = $this->getValue($field);
        if (!is_array($value) and trim($value) == "") {
            $this->addError($field, $value, $msg);
            return false;
        } elseif (is_array($value) and empty($value)) {
            $this->addError($field, $value, $msg);
            return false;
        } elseif (is_array($value)) {
            foreach ($value as $item) {
                if ($item == "") {
                    $this->addError($field, $value, $msg);
                    return false;
                }
            }
        } else {
            return true;
        }
    }

    /**
     * Adds a $msg to the list if the form control $field is not numeric.
     *
     * @param $field The form field to check.
     * @param $msg The error message presented to the user.
     */
    function isNumeric($field, $msg) {
        $value = $this->getValue($field);
        if(!is_numeric($value)) {
            $this->addError($field, $value, $msg);
            return false;
        } else {
            return true;
        }
    }

    /**
     * Adds a $msg to the list if the form control $field is not an integer.
     *
     * @param $field The form field to check.
     * @param $msg The error message presented to the user.
     */
    function isInteger($field, $msg) {
        $value = $this->getValue($field);
        if(!is_numeric($value) || $value != intval($value)) {
            $this->addError($field, $value, $msg);
            return false;
        } else {
            return true;
        }
    }

    /**
     * Adds a $msg to the list if the form control $field is not within a
     * numeric range.
     *
     * @param $field The form field to check.
     * @param $msg The error message presented to the user.
     */
    function isWithinRange($field, $msg, $min, $max) {
        $value = $this->getValue($field);
        if(!is_numeric($value) OR $value < $min OR $value > $max) {
            $this->addError($field, $value, $msg);
            return false;
        } else {
            return true;
        }
    }

    // Verify input is a valid email address
    /**
     * Adds a $msg to the list if the form control $field is not a valid
     * email address.
     *
     * @param $field The form field to check.
     * @param $msg The error message presented to the user.
     */
    function isEmailAddress($field, $msg) {
        $value = $this->getValue($field);
        $pattern = "/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*";
        $pattern .= "@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/";
        if(preg_match($pattern, $value)) {
            return true;
        } else {
            $this->addError($field, $value, $msg);
            return false;
        }
    }
}
?>