_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'); $this->_actionurl = KTUtil::addQueryStringSelf($qs); $this->_failaction = KTUtil::arrayGet($aOptions, 'fail_action'); $this->_failurl = KTUtil::arrayGet($aOptions, 'fail_url'); $this->_submitlabel = KTUtil::arrayGet($aOptions, 'submit_label', _kt('Submit')); // 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('action=%s', $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); $this->_extraargs['postReceived'] = 1; } // 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(); } // we don't want to expose the factory stuff to the user - its an // arbitrary distinction to the user. Good point from NBM ;) foreach ($aWidgets as $aInfo) { if (is_null($aInfo)) { continue; } else if (is_object($aInfo)) { // assume this is a fully configured object $this->_widgets[] =& $aInfo; } else { $namespaceOrObject = $aInfo[0]; $config = (array) $aInfo[1]; $this->_widgets[] =& $this->_oWF->get($namespaceOrObject, $config); } } } function setValidators($aValidators) { $this->_validators = array(); if (is_null($this->_oVF)) { $this->_oVF =& KTValidatorFactory::getSingleton(); } // 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) { if (is_null($aInfo)) { continue; } else if (is_object($aInfo)) { // assume this is a fully configured object $this->_validators[] =& $aInfo; } else { $namespaceOrObject = $aInfo[0]; $config = (array) $aInfo[1]; $this->_validators[] =& $this->_oVF->get($namespaceOrObject, $config); } } } function addValidator($aInfo) { if (is_null($this->_oVF)) { $this->_oVF =& KTValidatorFactory::getSingleton(); } if (is_null($aInfo)) { continue; } else if (is_object($aInfo)) { // assume this is a fully configured object $this->_validators[] =& $aInfo; } else { $namespaceOrObject = $aInfo[0]; $config = (array) $aInfo[1]; $this->_validators[] =& $this->_oVF->get($namespaceOrObject, $config); } } function addWidget($aInfo) { if (is_null($this->_oWF)) { $this->_oWF =& KTWidgetFactory::getSingleton(); } if (is_null($aInfo)) { continue; } else if (is_object($aInfo)) { // assume this is a fully configured object $this->_widgets[] =& $aInfo; } else { $namespaceOrObject = $aInfo[0]; $config = (array) $aInfo[1]; $this->_widgets[] =& $this->_oWF->get($namespaceOrObject, $config); } } function render() { $sWidgets = $this->renderWidgets(); $sButtons = $this->renderButtons(); return $this->renderContaining($sWidgets . ' ' . $sButtons); } 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('
Unable to show widget — %s
' . _kt("An error occured, and no error handlers were configured.") . '