protected function _showForm() { $this->view->formName = $this->_form->getName(); $this->view->method = 'post'; $this->view->submitButtonText = trlKwf('Submit'); $this->view->action = ''; $values = $this->_form->load(null, $this->_getParam('formPostData')); $this->view->form = $this->_form->getTemplateVars($values, '', $this->view->formName . '_'); $errors = $this->_getParam('formErrors'); if (!$errors) { $errors = array(); } $dec = new Kwc_Form_Decorator_Label(); $this->view->form = $dec->processItem($this->view->form, $errors); $this->view->errors = Kwf_Form::formatValidationErrors($errors); $this->view->errorsHtml = ''; if ($this->view->errors) { $this->view->errorsHtml .= '<div class="kwfUp-webStandard kwfUp-kwcFormError kwfUp-webFormError">'; $this->view->errorsHtml .= '<p class="error">' . trlKwf('An error has occurred') . ':</p>'; $this->view->errorsHtml .= '<ul>'; foreach ($this->view->errors as $error) { $this->view->errorsHtml .= '<li>' . htmlspecialchars($error) . '</li>'; } $this->view->errorsHtml .= '</ul>'; $this->view->errorsHtml .= '</div>'; } }
public function getTemplateVars() { $ret = Kwc_Abstract::getTemplateVars(); $this->_checkWasProcessed(); $ret['isPosted'] = $this->_posted; $ret['showSuccess'] = false; $ret['errors'] = Kwf_Form::formatValidationErrors($this->getErrors()); if ($this->isSaved()) { if (!$ret['errors'] && $this->getSuccessComponent()) { $ret['showSuccess'] = true; } } if ($ret['showSuccess']) { $ret['success'] = $this->getSuccessComponent(); } else { foreach ($this->getData()->getChildComponents(array('generator' => 'child')) as $c) { if ($c->id != 'success') { $ret[$c->id] = $c; } } } if (!$ret['showSuccess']) { $values = $this->getForm()->load(null, $this->_postData); $ret['form'] = $this->getForm()->getTemplateVars($values, '', $this->getData()->componentId . '_'); $dec = $this->_getSetting('decorator'); if ($dec && is_string($dec)) { $dec = new $dec(); $ret['form'] = $dec->processItem($ret['form'], $this->getErrors()); } $ret['formName'] = $this->getData()->componentId; $ret['buttonClass'] = $this->_getSetting('buttonClass'); $ret['formId'] = $this->getForm()->getId(); if ($ret['formId']) { $ret['formIdHash'] = Kwf_Util_Hash::hash($ret['formId']); } $page = $this->getData()->getPage(); if (!$page) { throw new Kwf_Exception('Form must have an url so it must be on a page but is on "' . $this->getData()->componentId . '". (If component is a box it must not be unique)'); } $cachedContent = Kwf_Component_Cache::getInstance()->load($page->componentId, 'componentLink'); if ($cachedContent) { $targetPage = unserialize($cachedContent); $ret['action'] = $targetPage[0]; } else { $ret['action'] = $this->getData()->url; } if (isset($_SERVER["QUERY_STRING"])) { $ret['action'] .= '?' . $_SERVER["QUERY_STRING"]; } $ret['method'] = $this->_getSetting('method'); } $ret['isUpload'] = false; foreach (new RecursiveIteratorIterator(new Kwf_Collection_Iterator_RecursiveFormFields($this->getForm()->fields)) as $f) { if ($f instanceof Kwf_Form_Field_File) { $ret['isUpload'] = true; break; } } $ret['message'] = null; $cacheId = 'kwcFormCu-' . get_class($this); $controllerUrl = Kwf_Cache_SimpleStatic::fetch($cacheId); if (!$controllerUrl) { $controllerUrl = Kwc_Admin::getInstance(get_class($this))->getControllerUrl('FrontendForm'); Kwf_Cache_SimpleStatic::add($cacheId, $controllerUrl); } $hideForValue = array(); foreach ($this->_form->getHideForValue() as $v) { $hideForValue[] = array('field' => $v['field']->getFieldName(), 'value' => $v['value'], 'hide' => $v['hide']->getFieldName()); } $baseParams = $this->_getBaseParams(); $baseParams['componentId'] = $this->getData()->componentId; $fieldConfig = array(); $iterator = new RecursiveIteratorIterator(new Kwf_Collection_Iterator_RecursiveFormFields($this->_form->fields), RecursiveIteratorIterator::SELF_FIRST); foreach ($iterator as $field) { if ($field->getFieldName()) { $fieldConfig[$field->getFieldName()] = (object) $field->getFrontendMetaData(); } } $errorStyle = $this->_getSetting('errorStyle'); if (!$errorStyle) { $errorStyle = Kwf_Config::getValue('kwc.form.errorStyle'); } $ret['config'] = array('controllerUrl' => $controllerUrl, 'useAjaxRequest' => $this->_getSetting('useAjaxRequest'), 'hideFormOnSuccess' => $this->_getSetting('hideFormOnSuccess'), 'componentId' => $this->getData()->componentId, 'hideForValue' => $hideForValue, 'fieldConfig' => (object) $fieldConfig, 'errorStyle' => $errorStyle, 'baseParams' => $baseParams); $ret['uniquePrefix'] = Kwf_Config::getValue('application.uniquePrefix'); if ($ret['uniquePrefix']) { $ret['uniquePrefix'] .= '-'; } return $ret; }
public function jsonSaveAction() { if (!isset($this->_permissions['save']) || !$this->_permissions['save']) { throw new Kwf_Exception("Save is not allowed."); } $success = false; $data = Zend_Json::decode($this->getRequest()->getParam("data")); if (!$data) { $data = array(); } $addedIds = array(); ignore_user_abort(true); if (Zend_Registry::get('db')) { Zend_Registry::get('db')->beginTransaction(); } foreach ($data as $submitRow) { $id = $submitRow[$this->_primaryKey]; $row = $this->_getRowById($id); if (!$row) { throw new Kwf_Exception("Can't find row with id '{$id}'."); } foreach ($this->_columns as $column) { if (!($column->getShowIn() & Kwf_Grid_Column::SHOW_IN_GRID)) { continue; } $invalid = $column->validate($row, $submitRow); if ($invalid) { $invalid = Kwf_Form::formatValidationErrors($invalid); throw new Kwf_Exception_Client(implode("<br />", $invalid)); } $column->prepareSave($row, $submitRow); } if (!$id) { $this->_beforeInsert($row, $submitRow); } $this->_beforeSave($row, $submitRow); if (!$this->_hasPermissions($row, 'save')) { throw new Kwf_Exception("You don't have the permissions to save this row."); } $row->save(); if (!$id) { $this->_afterInsert($row, $submitRow); } $this->_afterSave($row, $submitRow); if (!$id) { $addedIds[] = $row->id; } } if (Zend_Registry::get('db')) { Zend_Registry::get('db')->commit(); } $success = true; if ($addedIds) { $this->view->addedIds = $addedIds; } $this->view->success = $success; }
public function jsonSaveAction() { ignore_user_abort(true); $db = Zend_Registry::get('db'); if ($db) { $db->beginTransaction(); } // zuvor war statt diesem kommentar das $row = $this->_form->getRow(); // drin und wurde bei processInput und validate übergeben, aber die form // weiß selbst das model, deshalb passt NULL // Runtergeschoben wurde das $this->_form->getRow() weil bei der Kwf_User_Form // die row im processInput gefaket wird, da hier ->createUserRow() aufgerufen // wird anstatt ->createRow() und diese dann im _form->getRow() zurück kommt $postData = $this->_form->processInput(null, $this->getRequest()->getParams()); $this->_beforeValidate($postData); $invalid = $this->_form->validate(null, $postData); if ($invalid) { $invalid = Kwf_Form::formatValidationErrors($invalid); throw new Kwf_ClientException(implode("<br />", $invalid)); } $data = $this->_form->prepareSave(null, $postData); $row = $this->_form->getRow(); $insert = false; $primaryKey = $this->_form->getPrimaryKey(); $skip = false; if ($row && $primaryKey) { if (is_array($primaryKey)) { $primaryKey = $primaryKey[1]; } if (!$row->{$primaryKey}) { $insert = true; } if ($insert) { $sessionFormId = new Kwf_Session_Namespace('avoid_reinsert_id'); if ($this->_getParam('avoid_reinsert_id') && isset($sessionFormId->avoid[$this->_getParam('avoid_reinsert_id')])) { $skip = true; } if (!isset($this->_permissions['add']) || !$this->_permissions['add']) { throw new Kwf_Exception('Add is not allowed.'); } if (!$skip) { $this->_beforeInsert($row); } } else { if (!isset($this->_permissions['save']) || !$this->_permissions['save']) { throw new Kwf_Exception('Save is not allowed.'); } } if (!$skip) { $this->_beforeSave($row); } } if (!$skip) { //erst hier unten Berechtigungen überprüfen, damit beforeInsert usw vorher noch ausgeführt //wird und eventuelle Daten gesetzt werden if (!$this->_hasPermissions($row, 'save')) { throw new Kwf_Exception("Save is not allowed for this row."); } $data = $this->_form->save(null, $postData); $this->_form->afterSave(null, $postData); if ($row) { if ($insert) { $this->_afterInsert($row); } $this->_afterSave($row); } if ($db) { $db->commit(); } $this->view->data = $data; $sessionFormId = new Kwf_Session_Namespace('avoid_reinsert_id'); if (!isset($sessionFormId->avoid)) { $avoid = array(); } else { $avoid = $sessionFormId->avoid; } $avoid[$this->_getParam('avoid_reinsert_id')] = $data; $sessionFormId->avoid = $avoid; } else { $this->view->data = $sessionFormId->avoid[$this->_getParam('avoid_reinsert_id')]; } }