_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->setCacheability(CACHEABILITY_NO_STORE); $templateMgr->register_function('fieldLabel', array(&$this, 'smartyFieldLabel')); $templateMgr->register_function('form_language_chooser', array(&$this, 'smartyFormLanguageChooser')); $templateMgr->assign($this->_data); $templateMgr->assign('isError', !$this->isValid()); $templateMgr->assign('errors', $this->getErrorsArray()); $templateMgr->assign('formLocales', Locale::getSupportedLocales()); // Determine the current locale to display fields with $formLocale = Request::getUserVar('formLocale'); if (empty($formLocale) || !in_array($formLocale, array_keys(Locale::getAllLocales()))) { $formLocale = Locale::getLocale(); } $templateMgr->assign('formLocale', $formLocale); $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)) $value = Core::cleanVar($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()) { if (method_exists($check, 'getErrorFields') && method_exists($check, 'isArray') && call_user_func(array(&$check, 'isArray'))) { $errorFields = call_user_func(array(&$check, 'getErrorFields')); for ($i=0, $count=count($errorFields); $i < $count; $i++) { $this->addError($errorFields[$i], $check->getMessage()); $this->errorFields[$errorFields[$i]] = 1; } } else { $this->addError($check->getField(), $check->getMessage()); $this->errorFields[$check->getField()] = 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. "papergalleyform::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() { } /** * Get the list of field names that need to support multiple locales * @return array */ function getLocaleFieldNames() { return array(); } /** * Determine whether or not the current request results from a resubmit * of locale data resulting from a form language change. * @return boolean */ function isLocaleResubmit() { $formLocale = Request::getUserVar('formLocale'); return (!empty($formLocale)); } /** * Get the current form locale. * @return string */ function getFormLocale() { $formLocale = Request::getUserVar('formLocale'); if (empty($formLocale)) $formLocale = Locale::getLocale(); return $formLocale; } /** * 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 */ 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'], $params); } if (isset($this->errorFields[$params['name']])) { $class = ' class="error"'; } else { $class = ''; } echo '', $params['label'], (isset($params['required']) && !empty($params['required']) ? '*' : ''), ''; } } function _decomposeArray($name, $value, $stack) { if (is_array($value)) { foreach ($value as $key => $subValue) { $newStack = $stack; $newStack[] = $key; $this->_decomposeArray($name, $subValue, $newStack); } } else { $name = htmlentities($name, ENT_COMPAT, LOCALE_ENCODING); $value = htmlentities($value, ENT_COMPAT, LOCALE_ENCODING); echo '\n"; } } /** * Add hidden form parameters for the localized fields for this form * and display the language chooser field * @param $params array * @param $smarty object */ function smartyFormLanguageChooser($params, &$smarty) { // Echo back all non-current language field values so that they // are not lost. $formLocale = $smarty->get_template_vars('formLocale'); foreach ($this->getLocaleFieldNames() as $field) { $values = $this->getData($field); if (!is_array($values)) continue; foreach ($values as $locale => $value) { if ($locale != $formLocale) $this->_decomposeArray($field, $value, array($locale)); } } // Display the language selector widget. $formLocale = $smarty->get_template_vars('formLocale'); echo '
'; } } ?>