_template = $template; $this->_data = array(); $this->_checks = array(); $this->_errors = array(); $this->errorsArray = array(); $this->errorFields = array(); } /** * Display the form. */ function display() { $templateMgr = &TemplateManager::getManager(); $templateMgr->register_function('fieldLabel', array(&$this, 'smartyFieldLabel')); $templateMgr->assign($this->_data); $templateMgr->assign('isError', !$this->isValid()); $templateMgr->assign('errors', $this->getErrorsArray()); $templateMgr->display($this->_template); } /** * Get the value of a form field. * @param $key string * @return mixed */ function getData($key) { return isset($this->_data[$key]) ? $this->_data[$key] : null; } /** * Set the value of a form field. * @param $key * @param $value */ function setData($key, $value) { if (is_string($value)) { // check for Windows-1252 encoding, and transliterate if necessary if ($value === utf8_decode($value) && $value !== utf8_encode($value)) { // string is cp1252 // transliterate cp1252->utf8 to work in utf-8 // utf8_decode to work in latin-1 (information may be lost) import('core.Transcoder'); $trans =& new Transcoder('CP1252', 'UTF-8'); $value = $trans->trans($value); } elseif ($value !== utf8_decode($value) && $value !== utf8_encode($value)) { // string is not within utf-8(?) // normalize to ASCII (lowest common encoding) - information will be lost import('core.Transcoder'); $trans =& new Transcoder('UTF-8', 'ASCII'); $value = $trans->trans($value); } } $this->_data[$key] = $value; } /** * Initialize form data for a new form. */ function initData() { } /** * Assign form data to user-submitted data. */ function readInputData() { } /** * Validate form data. */ function validate($callHooks = true) { if (!isset($this->errorsArray)) { $this->getErrorsArray(); } foreach ($this->_checks as $check) { if (!isset($this->errorsArray[$check->getField()]) && !$check->isValid()) { $this->addError($check->getField(), $check->getMessage()); $this->errorFields[$check->getField()] = 1; if (method_exists($check, 'getErrorFields')) { $errorFields = call_user_func(array(&$check, 'getErrorFields')); for ($i=0, $count=count($errorFields); $i < $count; $i++) { $this->errorFields[$errorFields[$i]] = 1; } } } } if ($callHooks === true && checkPhpVersion('4.3.0')) { $trace = debug_backtrace(); // Call hooks based on the calling entity, assuming // this method is only called by a subclass. Results // in hook calls named e.g. "articlegalleyform::validate" // Note that class and function names are always lower // case. $value = null; if (HookRegistry::call(strtolower($trace[0]['class'] . '::' . $trace[0]['function']), array(&$this, &$value))) { return $value; } } return $this->isValid(); } /** * Execute the form's action. * (Note that it is assumed that the form has already been validated.) */ function execute() { } /** * Adds specified user variables to input data. * @param $vars array the names of the variables to read */ function readUserVars($vars) { foreach ($vars as $k) { $this->setData($k, Request::getUserVar($k)); } } /** * Adds specified user date variables to input data. * @param $vars array the names of the date variables to read */ function readUserDateVars($vars) { foreach ($vars as $k) { $this->setData($k, Request::getUserDateVar($k)); } } /** * Add a validation check to the form. * @param $formValidator FormValidator */ function addCheck($formValidator) { $this->_checks[] = &$formValidator; } /** * Add an error to the form. * Errors are typically assigned as the form is validated. * @param $field string the name of the field where the error occurred * @param $message string the error message (i18n key) */ function addError($field, $message) { $this->_errors[] = &new FormError($field, $message); } /** * Add an error field for highlighting on form * @param $field string the name of the field where the error occurred */ function addErrorField($field) { $this->errorFields[$field] = 1; } /** * Check if form passes all validation checks. * @return boolean */ function isValid() { return empty($this->_errors); } /** * Return set of errors that occurred in form validation. * If multiple errors occurred processing a single field, only the first error is included. * @return array erroneous fields and associated error messages */ function getErrorsArray() { $this->errorsArray = array(); foreach ($this->_errors as $error) { if (!isset($this->errorsArray[$error->getField()])) { $this->errorsArray[$error->getField()] = $error->getMessage(); } } return $this->errorsArray; } /** * Custom Smarty function for labelling/highlighting of form fields. * @param $params array can contain 'name' (field name/ID), 'required' (required field), 'key' (localization key), 'label' (non-localized label string), 'suppressId' (boolean) * @param $smarty Smarty */ function smartyFieldLabel($params, &$smarty) { if (isset($params) && !empty($params)) { if (isset($params['key'])) { $params['label'] = Locale::translate($params['key']); } if (isset($this->errorFields[$params['name']])) { $class = ' class="error"'; } else { $class = ''; } echo '', $params['label'], (isset($params['required']) && !empty($params['required']) ? '*' : ''), ''; } } } ?>