Commit afe12ade authored by Carsten  Rose's avatar Carsten Rose
Browse files

Form.php: Implemented fakeSip for Forms without SIP. Implemented checkFormLoad().

parent 119fdfd7
......@@ -25,15 +25,37 @@ require_once(__DIR__ . '/../qfq/exceptions/DbException.php');
require_once(__DIR__ . '/../qfq/Database.php');
require_once(__DIR__ . '/../qfq/FormBuildPlain.php');
/*
* Form will be called
* a) with a SIP identifier, or
* b) without a SIP identifier (form setting has to allow this) and will create on the fly a new SIP.
*
* The SIP-Store stores:
* form=<formname>
* r=<record id> (table.id for a single record form)
* keySemId,keySemIduser
* <further individual variables>
*/
class Form {
protected $config = null;
protected $store = null;
protected $db = null;
protected $formDef = array(); // copy of the loaded form
protected $feDefAction = array(); // copy of all formElement.class='action' of the loaded form
protected $feDefNative = array(); // copy of all formElement.class='native' of the loaded form
protected $fe = array(); // current processed formElement
protected $formDef = array(); // Form Definition: copy of the loaded form
protected $feDefAction = array(); // FormEelement Definition: all formElement.class='action' of the loaded form
protected $feDefNative = array(); // FormEelement Definition: all formElement.class='native' of the loaded form
/*
* TODO:
* Preparation: setup logging, database access, record locking
* fill stores
* Check permission_create / permission_update
* Multi: iterate over all records, Single: activate record
* Check mode: Load | Save
* doActions 'Before'
* Do all FormElements
* doActions 'After'
*/
// protected $formElements = null;
......@@ -44,29 +66,17 @@ class Form {
*/
public function __construct($bodytext = '') {
/*
* TODO:
* Preparation: setup logging, database access, record locking
* fill stores
* Check permission_create / permission_update
* Multi: iterate over all records, Single: activate record
* Check mode: Load | Save
* doActions 'Before'
* Do all FormElements
* doActions 'After'
*/
$this->store = \qfq\store\Store::getInstance($bodytext);
$this->db = new Database();
}
/*
*
/**
* @return string
*/
public function process() {
$html = '';
$build = null;
$master = null;
// render:
// plain
......@@ -76,7 +86,12 @@ class Form {
// Form action: load or save?
$mode = ($this->store->getVar(CLIENT_POST_SIP, STORE_CLIENT) === false) ? FORM_LOAD : FORM_SAVE;
$this->loadFormDefinition();
$formName = $this->loadFormDefinition();
$sipFound = $this->checkFormLoad();
if (!$sipFound) {
$this->store->createFakeSipAfterFormLoad($formName);
}
// TODO: replace switch() by Marschaller
// render: FormPlain | FormBootstrap
......@@ -86,9 +101,10 @@ class Form {
break;
case 'Bootstrap':
//TODO: implement bootstrap rendering: FormBuildBootstrap
// $build = new FormBuildBootstrap($this->formDef, $this->feDefAction, $this->feDefNative);
break;
default:
throw new CodeException("this statement should never be reached", ERROR_CODE_SHOULD_NOT_HAPPEN);
throw new CodeException("This statement should never be reached", ERROR_CODE_SHOULD_NOT_HAPPEN);
}
switch ($mode) {
......@@ -102,7 +118,7 @@ class Form {
case FORM_SAVE:
break;
default:
throw new CodeException("this statement should never be reached", ERROR_CODE_SHOULD_NOT_HAPPEN);
throw new CodeException("This statement should never be reached", ERROR_CODE_SHOULD_NOT_HAPPEN);
}
return $html;
......@@ -116,11 +132,13 @@ class Form {
echo "Generic Exception: " . $e->getMessage();
}
return $html;
}
/**
* @return string formName
* @throws DbException
* @throws UserException
*/
private function loadFormDefinition() {
......@@ -130,6 +148,8 @@ class Form {
$sql = "SELECT * FROM FormElement AS fe WHERE fe.formId = ? AND fe.deleted='no' AND fe.class = ? AND fe.enabled='yes' ORDER BY fe.order, fe.id";
$this->feDefAction = $this->db->sql($sql, ROW_REGULAR, [$this->formDef["id"], 'action']);
$this->feDefNative = $this->db->sql($sql, ROW_REGULAR, [$this->formDef["id"], 'native']);
return $formName;
}
/**
......@@ -140,23 +160,56 @@ class Form {
private function getFormName() {
// store: T3 Bodytext
$formName = $this->store->getVar(T3_BODYTEXT_FORM, STORE_T3_BODYTEXT);
$formName = $this->store->getVar(T3_BODYTEXT_FORM, STORE_T3_BODYTEXT . STORE_SIP . STORE_CLIENT);
if ($formName !== false)
return $formName;
// store: SIP
// $fomName = $this->store->getVar(????_FORM, STORE_SIP);
// if($formName !== false)
// return $formName;
// TODO: phpunit tests all - missing formname always fires this exception
throw new CodeException("Missing form name. Not found in T3_BODYTEXT / SIP / GET", ERROR_MISSING_FORM_NAME);
}
// store: CLIENT
$formName = $this->store->getVar(CLIENT_FORM, STORE_CLIENT);
if ($formName !== false)
return $formName;
/**
* @return bool - 'true' if SIP exists, else 'false'
* @throws CodeException
* @throws UserException
*/
private function checkFormLoad() {
// Retrieve record_id either from SIP (prefered) or via URL
$r = $this->store->getVar(SIP_RECORD_ID, STORE_SIP . STORE_CLIENT);
// If there is a record_id>0: EDIT else NEW
$mode = ($r > 0) ? $this->formDef['permitEdit'] : $this->formDef['permitNew'];
$feUserLoggedIn = isset($GLOBALS["TSFE"]->fe_user->user["uid"]) && $GLOBALS["TSFE"]->fe_user->user["uid"] > 0;
$sipFound = $this->store->getVar(SIP_SIP, STORE_SIP) !== false;
switch ($mode) {
case FORM_PERMISSION_SIP:
if (!$sipFound) {
throw new UserException("SIP Parameter needed for this form.", ERROR_SIP_NEEDED_FOR_THIS_FORM);
}
break;
case FORM_PERMISSION_LOGGED_IN:
if (!$feUserLoggedIn) {
throw new UserException("User not logged in.", ERROR_USER_NOT_LOGGED_IN);
}
break;
case FORM_PERMISSION_LOGGED_OUT:
if ($feUserLoggedIn) {
throw new UserException("User logged in.", ERROR_USER_LOGGED_IN);
}
break;
case FORM_PERMISSION_ALWAYS:
break;
case FORM_PERMISSION_NEVER:
throw new UserException("Loading form forbidden.", ERROR_FORM_FORBIDDEN);
default:
throw new CodeException("Unknown permission mode: '" . $mode . "'", ERROR_FORM_UNKNOWN_PERMISSION_MODE);
}
// TODO: phpunit tests all - missing formname always fires this exception
// throw new UserException("Missing form name. Not found in T3_BODYTEXT / SIP / GET", ERROR_MISSING_FORM_NAME);
return $sipFound;
}
}
\ No newline at end of file
......@@ -19,7 +19,7 @@ class FormTest extends PHPUnit_Framework_TestCase {
public function testProcess() {
$form = new qfq\Form("\n; some comment\n".T3_BODYTEXT_FORM."=testformnameDoNotChange\n".T3_BODYTEXT_DEBUG_SAVE."=6\n".T3_BODYTEXT_DEBUG_LOAD."=7\n");
$this->assertEquals("Hello World from class Form", $form->process());
$this->assertEquals("", $form->process());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment