📄 forms.inc.php
字号:
}
function render() {
$sWidgets = $this->renderWidgets();
$sButtons = $this->renderButtons();
return $this->renderContaining($sWidgets . ' ' . $sButtons);
}
function renderPage($sTitle = null, $sDescription = null) {
if ($sTitle == null) {
$sTitle = $this->sLabel;
}
$pageval = $this->render();
$sHelpText = '';
if (!is_null($sDescription)) {
$sHelpText = sprintf('<p class="descriptiveText">%s</p>', $sDescription);
}
return sprintf('<h2>%s</h2> %s %s', sanitizeForHTML($sTitle), $sHelpText, $pageval);
}
function getErrors() {
$aErrors = array();
$old_data = KTUtil::arrayGet((array) $_SESSION['_kt_old_data'],
$this->_kt_form_name, array());
if (KTUtil::arrayGet($old_data, 'identifier') == $this->sIdentifier) {
$aErrors = (array) unserialize(KTUtil::arrayGet($old_data, 'errors'));
}
return $aErrors;
}
function renderWidgets() {
if (empty($this->_widgets)) {
return ' ';
}
// do this all at the *last* possible moment
// now we need to do two things:
//
// 1. inform each "widget" that it needs to wrap itself inside
// the "data" var
// 2. replace the widget's default values with the ones from the
// failed request, as appropriate.
$bUseOld = false;
$aOldData = array();
$aErrors = array();
$old_data = KTUtil::arrayGet((array) $_SESSION['_kt_old_data'],
$this->_kt_form_name, array());
if (KTUtil::arrayGet($old_data, 'identifier') == $this->sIdentifier) {
$bUseOld = true;
$aStoredData = (array) unserialize(KTUtil::arrayGet($old_data, 'data'));
$aOldData = array();
foreach ($aStoredData as $k => $v) {
$aOldData[$k] = unserialize($v);
}
$aErrors = (array) unserialize(KTUtil::arrayGet($old_data, 'errors'));
}
foreach ($this->_widgets as $k => $v) {
if (PEAR::isError($v)) {
continue; // error, handle it in render.
}
$widget =& $this->_widgets[$k]; // reference needed since we're changing them
$widget->wrapName('data');
if ($bUseOld) {
$widget->setDefault(KTUtil::arrayGet($aOldData, $widget->getBasename(),
$widget->getDefault(), false));
$widget->setErrors(KTUtil::arrayGet($aErrors, $widget->getBasename()));
}
}
// too much overhead by half to use a template here
// so we do it the "old fashioned" way.
$rendered = array();
foreach ($this->_widgets as $v) {
if (PEAR::isError($v)) {
$rendered[] = sprintf(_kt('<div class="ktError"><p>Unable to show widget — %s</p></div>'), $v->getMessage());
} else {
$rendered[] = $v->render();
}
}
return implode(' ', $rendered);
}
function renderButtons() {
$oKTTemplating =& KTTemplating::getSingleton();
$oTemplate = $oKTTemplating->loadTemplate('ktcore/forms/buttons');
// now do the render.
$oTemplate->setData(array(
'context' => &$this,
));
return $oTemplate->render();
}
function renderContaining() {
$args = func_get_args();
$sInner = implode(' ', $args);
$oKTTemplating =& KTTemplating::getSingleton();
$oTemplate = $oKTTemplating->loadTemplate('ktcore/forms/outerform');
// remove inner "action" var from extraargs
// if its there at all.
unset($this->_extraargs[$this->_event]);
$this->_extraargs['_kt_form_name'] = $this->_kt_form_name;
// now do the render.
$oTemplate->setData(array(
'context' => &$this,
'inner' => $sInner,
));
return $oTemplate->render();
}
function generateFormName($sIdentifier = null) {
if (!is_null($sIdentifier)) {
// try use the existing one from the request.
$existing = KTUtil::arrayGet($_REQUEST, '_kt_form_name');
if (!empty($existing)) {
// check that its the same form
$data = KTUtil::arrayGet($_SESSION['_kt_old_data'], $existing);
if ($data['identifier'] == $sIdentifier) {
return $existing;
}
}
}
return KTUtil::randomString(32); // unique 32 char string
}
function validate() {
// we first ask each widget to pull its data out.
// while we do that, we create the storage set for the session
// that widgets can call on later.
$raw_data = KTUtil::arrayGet($_REQUEST, 'data');
$processed_data = array();
foreach ($this->_widgets as $oWidget) {
if (PEAR::isError($oWidget)) {
continue;
}
// widgets are expected to place their data in the "basename"
// entry in the processed data area
//
// they should also be able to reconstruct their inputs from this
// since its what they get later.
$res = $oWidget->process($raw_data);
$processed_data = kt_array_merge($processed_data, $res);
}
// before we validate ANYTHING we store data into the session
$store_data = array(); // we only want to store serialized values here
foreach ($processed_data as $k => $v) {
$store_data[$k] = serialize($v);
}
$_SESSION['_kt_old_data'][$this->_kt_form_name]['data'] = serialize($store_data);
$_SESSION['_kt_old_data'][$this->_kt_form_name]['identifier'] =
$this->sIdentifier;
$_SESSION['_kt_old_data'][$this->_kt_form_name]['created'] =
getCurrentDateTime();
$results = array();
$errors = array();
// some things can be checked by the actual widgets involved. These
// are obvious (e.g. required) and shouldn't require the developer to
// think about them.
//
// to accomplish this, we call each widget's "getValidators" method.
//
// note that autovalidation can be turned off for a widget by passing
// "autovalidate" => "false" in the widget's config.
$extra_validators = array();
foreach ($this->_widgets as $oWidget) {
if (PEAR::isError($oWidget)) {
continue;
}
$res = $oWidget->getValidators();
if (!is_null($res)) {
if (is_array($res)) {
$extra_validators = kt_array_merge($extra_validators, $res);
} else {
$extra_validators[] = $res;
}
}
}
$validators = kt_array_merge($extra_validators, $this->_validators);
foreach ($validators as $oValidator) {
if (PEAR::isError($oValidator)) {
// don't bother with broken validators, but warn the user/dev
$errors['_kt_global'][] = $oValidator->getMessage();
continue;
}
$res = $oValidator->validate($processed_data);
// results comes out with a set of names and values.
// these *shouldn't* overlap, so just merge them
$extra_results = KTUtil::arrayGet($res, 'results', array());
$results = kt_array_merge($results, $extra_results);
// errors *can* overlap
// the format is:
// basename => array(errors)
// so that a given field can have multiple errors
// from multiple validators
//
// there is also a "global" error notice stored against the var
// _kt_global
$extra_errors = KTUtil::arrayGet($res, 'errors', array());
foreach ($extra_errors as $varname => $aErrors) {
if (is_string($aErrors)) {
$errors[$varname][] = $aErrors;
} else {
$errors[$varname] = kt_array_merge($errors[$varname], $aErrors);
}
}
}
$this->_errors = $errors; // store for later use without unserialising
if (!empty($errors)) {
$_SESSION['_kt_old_data'][$this->_kt_form_name]['errors'] =
serialize($errors);
}
//var_dump($errors); exit(0);
return array(
'errors' => $errors,
'results' => $results,
);
}
function handleError($sGlobalError = null, $aSimplerErrors = null) {
if (!is_null($sGlobalError)) {
$this->_errors['_kt_global'][] = $sGlobalError;
}
if (!is_null($aSimplerErrors)) {
foreach ($aSimplerErrors as $k => $v) {
$this->_errors[$k] = kt_array_merge($this->_errors[$k], $v);
}
// since we've changed them, update the stored version
$_SESSION['_kt_old_data'][$this->_kt_form_name]['errors'] =
serialize($this->_errors);
}
if (is_array($this->_errors)) {
$global_errors = KTUtil::arrayGet($this->_errors, '_kt_global', array());
$_SESSION['KTErrorMessage'] = kt_array_merge($_SESSION['KTErrorMessage'], $global_errors);
}
if (!empty($this->_failaction) && !is_null($this->_context)) {
$this->_context->errorRedirectTo($this->_failaction,
_kt("Please correct the errors indicated."),
sprintf("_kt_form_name=%s",$this->_kt_form_name));
exit(0);
} else if ($this->_failurl){
redirect(KTUtil::addQueryString($this->_failurl,
sprintf("_kt_form_name=%s",$this->_kt_form_name)));
exit(0);
} else {
return '<div class="ktError"><p>' . _kt("An error occured, and no error handlers were configured.") . '</p></div>';
exit(0);
}
}
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -