📄 forms.inc.php
字号:
<?php
/**
* $Id: forms.inc.php 8387 2008-04-22 16:36:04Z kevin_fourie $
*
* KnowledgeTree Community Edition
* Document Management Made Simple
* Copyright (C) 2008 KnowledgeTree Inc.
* Portions copyright The Jam Warehouse Software (Pty) Limited
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 3 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco,
* California 94120-7775, or email info@knowledgetree.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* KnowledgeTree" logo and retain the original copyright notice. If the display of the
* logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
* must display the words "Powered by KnowledgeTree" and retain the original
* copyright notice.
* Contributor( s): ______________________________________
*/
/* handle basic machinery for form handling, including working with
* widgets, sessions and validation
*/
require_once(KT_LIB_DIR . "/widgets/widgetfactory.inc.php");
require_once(KT_LIB_DIR . "/validation/validatorfactory.inc.php");
class KTForm {
// serialisation info
var $_kt_form_name;
var $sIdentifier; // a simple identifier.
// visual options
var $sLabel;
var $sDescription;
// core storage options
var $_widgets; // what widgets get stored
var $_validators; // validators
var $_submitlabel; // what is the "submit" button called
var $_action; // where does the success message go
var $_event; // where does the success message go
var $_extraargs; // various extra arguments
var $_failaction; // should this error out, which action handles it
var $_failurl; // if we don't have a failaction, try this url
var $_cancelurl; // where do we get redirected if we cancel?
var $bCancel;
var $_context;
var $_errors;
var $_method;
var $_noframe;
var $_oVF;
var $_oWF;
// we don't use a constructor here, rather use aOptions
function setOptions($aOptions) {
// we grab the "context" dispatcher(ish) object here
$context =& KTUtil::arrayGet($aOptions, 'context');
$this->_context =& $context;
// form identifier (namespace)
$this->sIdentifier = KTUtil::arrayGet($aOptions, 'identifier','kt.default');
// form name
$this->_kt_form_name = KTUtil::arrayGet($aOptions, '_kt_form_name',
$this->generateFormName($this->sIdentifier), false);
// form labelling
$this->sLabel = KTUtil::arrayGet($aOptions, 'label');
$this->sDescription = KTUtil::arrayGet($aOptions, 'description');
// actions
$this->_action = KTUtil::arrayGet($aOptions, 'action');
$qs = KTUtil::arrayGet($aOptions, 'actionparams','');
$this->_enctype = KTUtil::arrayGet($aOptions, 'encoding');
if (empty($this->_enctype)) {
if (KTUtil::arrayGet($aOptions, 'file_upload', false)) {
$this->_enctype="multipart/form-data";
}
}
$targeturl = KTUtil::arrayGet($aOptions, 'targeturl', false);
if($targeturl === false) {
$this->_actionurl = KTUtil::addQueryStringSelf($qs);
} else {
$this->_actionurl = KTUtil::addQueryString($targeturl, $qs);
}
$this->_failaction = KTUtil::arrayGet($aOptions, 'fail_action');
$this->_failurl = KTUtil::arrayGet($aOptions, 'fail_url');
$this->_submitlabel = KTUtil::arrayGet($aOptions, 'submit_label',
_kt('Submit'));
$this->_event = KTUtil::arrayGet($aOptions, 'event');
if (empty($this->_event)) {
if (!is_null($context)) {
$this->_event = $context->event_var;
} else {
$this->_event = "action";
}
}
$this->_noframe = KTUtil::arrayGet($aOptions, 'noframe', false);
// cancel
// there are a few options here:
// 1. cancel_action
// 2. cancel_url
$cancel_action = KTUtil::arrayGet($aOptions, 'cancel_action');
$cancel_url = KTUtil::arrayGet($aOptions, 'cancel_url');
if (!empty($cancel_action)) {
$this->bCancel = true;
// there are two cases here - if we have a context, we can
// use the meldPersistQuery to create the url.
if (!is_null($context)) {
$sQuery = $context->meldPersistQuery("",
$cancel_action);
$this->_cancelurl =
KTUtil::addQueryString($_SERVER['PHP_SELF'], $sQuery);
} else {
// give it a try using addQSSelf
$this->_cancelurl = KTUtil::addQueryStringSelf(
sprintf('%s=%s', $this->_event, $cancel_action));
}
} else if (!empty($cancel_url)) {
$this->bCancel = true;
$this->_cancelurl = $cancel_url;
} else {
$this->bCancel = false;
}
// FIXME process extra arguments more intelligently
$default_args = array();
if (!is_null($this->_context)) {
$default_args = $this->_context->meldPersistQuery("","",true);
}
$this->_extraargs = KTUtil::arrayGet($aOptions,
'extraargs', $default_args);
// method
$this->_method = KTUtil::arrayGet($aOptions, 'method', 'post');
$this->_extraargs['postReceived'] = 1;
}
function getWidget(&$aInfo) {
if (is_null($this->_oWF)) {
$this->_oWF =& KTWidgetFactory::getSingleton();
}
if (is_null($aInfo)) {
$widget = null;
} else if (is_object($aInfo)) {
// assume this is a fully configured object
$widget =& $aInfo;
} else {
$namespaceOrObject = $aInfo[0];
$config = (array) $aInfo[1];
$widget =& $this->_oWF->get($namespaceOrObject, $config);
}
return $widget;
}
function getValidator($aInfo) {
if (is_null($this->_oVF)) {
$this->_oVF =& KTValidatorFactory::getSingleton();
}
$validator = null;
// we don't want to expose the factory stuff to the user - its an
// arbitrary distinction to the user. Good point from NBM ;)
if (is_null($aInfo)) {
$validator = null;
} else if (is_object($aInfo)) {
// assume this is a fully configured object
$validator =& $aInfo;
} else {
$namespaceOrObject = $aInfo[0];
$config = (array) $aInfo[1];
$validator =& $this->_oVF->get($namespaceOrObject, $config);
}
return $validator;
}
// set the "form widgets" that will be used.
// these are pushed into the "data" component
function setWidgets($aWidgets) {
$this->_widgets = array();
if (is_null($this->_oWF)) {
$this->_oWF =& KTWidgetFactory::getSingleton();
}
$this->addWidgets($aWidgets);
}
function addWidgets($aWidgets) {
foreach ($aWidgets as $aInfo) {
$widget = $this->getWidget($aInfo);
if (is_null($widget)) {
continue;
} else {
$this->_widgets[] = $widget;
}
}
}
function setValidators($aValidators) {
$this->_validators = array();
if (is_null($this->_oVF)) {
$this->_oVF =& KTValidatorFactory::getSingleton();
}
$this->addValidators($aValidators);
}
function addValidators($aValidators) {
// we don't want to expose the factory stuff to the user - its an
// arbitrary distinction to the user. Good point from NBM ;)
foreach ($aValidators as $aInfo) {
$validator = $this->getValidator($aInfo);
if (is_null($validator)) {
continue;
} else {
$this->_validators[] = $validator;
}
}
}
function addValidator($aInfo) {
$validator = $this->getValidator($aInfo);
if (is_null($validator)) {
return false;
} else {
$this->_validators[] =& $validator;
}
}
function addWidget($aInfo) {
$widget = $this->getWidget($aInfo);
if (is_null($widget)) {
return false;
} else {
$this->_widgets[] =& $widget;
}
}
function addInitializedWidget($oWidget) {
$this->_widgets[] = $oWidget;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -