Commit 8de2cf29 authored by Carsten  Rose's avatar Carsten Rose
Browse files

Constants.php: New Constants to render subrecords to Plain and Table Style

BuildForm.php: New implemented rendering in plain and table mode. Subrecords will be displayed at the correct place. In table mode the submit button is directly below the form.
parent d6d88dce
...@@ -19,7 +19,7 @@ require_once(__DIR__ . '/../qfq/Database.php'); ...@@ -19,7 +19,7 @@ require_once(__DIR__ . '/../qfq/Database.php');
* Date: 1/6/16 * Date: 1/6/16
* Time: 8:02 PM * Time: 8:02 PM
*/ */
class FormBuildPlain { class FormBuild {
protected $formSpec = array(); // copy of the loaded form protected $formSpec = array(); // copy of the loaded form
protected $feSpecAction = array(); // copy of all formElement.class='action' of the loaded form protected $feSpecAction = array(); // copy of all formElement.class='action' of the loaded form
protected $feSpecNative = array(); // copy of all formElement.class='native' of the loaded form protected $feSpecNative = array(); // copy of all formElement.class='native' of the loaded form
...@@ -37,18 +37,53 @@ class FormBuildPlain { ...@@ -37,18 +37,53 @@ class FormBuildPlain {
$this->evaluate = new Evaluate($this->store, $this->db); $this->evaluate = new Evaluate($this->store, $this->db);
// $sip = $this->store->getVar(CLIENT_SIP, STORE_CLIENT); // $sip = $this->store->getVar(CLIENT_SIP, STORE_CLIENT);
$this->wrap[WRAP_SETUP_TITLE][WRAP_SETUP_START] = '<h3>'; switch ($formSpec['render']) {
$this->wrap[WRAP_SETUP_TITLE][WRAP_SETUP_END] = '</h3>'; case 'plain':
$this->wrap[WRAP_SETUP_TITLE][WRAP_SETUP_START] = '<h3>';
$this->wrap[WRAP_SETUP_TITLE][WRAP_SETUP_END] = '</h3>';
$this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_START] = '<p>'; $this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_START] = '<p>';
$this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_END] = '</p>'; $this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_END] = '</p>';
$this->wrap[WRAP_SETUP_LABEL][WRAP_SETUP_START] = ''; $this->wrap[WRAP_SETUP_LABEL][WRAP_SETUP_START] = '';
$this->wrap[WRAP_SETUP_LABEL][WRAP_SETUP_END] = ''; $this->wrap[WRAP_SETUP_LABEL][WRAP_SETUP_END] = '';
$this->wrap[WRAP_SETUP_INPUT][WRAP_SETUP_START] = ''; $this->wrap[WRAP_SETUP_INPUT][WRAP_SETUP_START] = '';
$this->wrap[WRAP_SETUP_INPUT][WRAP_SETUP_END] = ''; $this->wrap[WRAP_SETUP_INPUT][WRAP_SETUP_END] = '';
$this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_START] = ''; $this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_START] = '';
$this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_END] = ''; $this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_END] = '';
break;
case 'table':
$this->wrap[WRAP_SETUP_TITLE][WRAP_SETUP_START] = '<h3>';
$this->wrap[WRAP_SETUP_TITLE][WRAP_SETUP_END] = '</h3>';
$this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_START] = '<tr>';
$this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_END] = '</tr>';
$this->wrap[WRAP_SETUP_LABEL][WRAP_SETUP_START] = '<td align="right">';
$this->wrap[WRAP_SETUP_LABEL][WRAP_SETUP_END] = '</td>';
$this->wrap[WRAP_SETUP_INPUT][WRAP_SETUP_START] = '<td>';
$this->wrap[WRAP_SETUP_INPUT][WRAP_SETUP_END] = '</td>';
$this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_START] = '<td>';
$this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_END] = '</td>';
$this->wrap[WRAP_SETUP_SUBRECORD][WRAP_SETUP_START] = '<p>';
$this->wrap[WRAP_SETUP_SUBRECORD][WRAP_SETUP_END] = '</p>';
break;
case 'bootstrap':
$this->wrap[WRAP_SETUP_TITLE][WRAP_SETUP_START] = '<h3>';
$this->wrap[WRAP_SETUP_TITLE][WRAP_SETUP_END] = '</h3>';
$this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_START] = '<p>';
$this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_END] = '</p>';
$this->wrap[WRAP_SETUP_LABEL][WRAP_SETUP_START] = '';
$this->wrap[WRAP_SETUP_LABEL][WRAP_SETUP_END] = '';
$this->wrap[WRAP_SETUP_INPUT][WRAP_SETUP_START] = '';
$this->wrap[WRAP_SETUP_INPUT][WRAP_SETUP_END] = '';
$this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_START] = '';
$this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_END] = '';
break;
}
$this->buildElementFunctionName = [ $this->buildElementFunctionName = [
'checkbox' => 'Checkbox', 'checkbox' => 'Checkbox',
...@@ -85,19 +120,27 @@ class FormBuildPlain { ...@@ -85,19 +120,27 @@ class FormBuildPlain {
$html = $this->head(); $html = $this->head();
$mode = ($this->formSpec['render']) == 'bootstrap' ? FORM_ELEMENTS_NATIVE_SUBRECORD : FORM_ELEMENTS_NATIVE;
if ($this->formSpec['multiMode'] !== 'none') { if ($this->formSpec['multiMode'] !== 'none') {
$parentRecords = $this->db->sql($this->formSpec['multiSql']); $parentRecords = $this->db->sql($this->formSpec['multiSql']);
foreach ($parentRecords as $row) { foreach ($parentRecords as $row) {
$this->store->setVarArray($row, STORE_PARENT_RECORD); $this->store->setVarArray($row, STORE_PARENT_RECORD);
$html .= $this->elements($row['_id']); $html .= $this->elements($row['_id'], $mode);
} }
} else { } else {
$html .= $this->elements($this->store->getVar(SIP_RECORD_ID, STORE_SIP)); $html .= $this->elements($this->store->getVar(SIP_RECORD_ID, STORE_SIP), $mode);
} }
// close the form
$html .= $this->tail(); $html .= $this->tail();
// render 'plain' and 'table': do subrecords below the form.
if ($this->formSpec['render'] !== 'bootstrap') {
$html .= $this->elements($this->store->getVar(SIP_RECORD_ID, STORE_SIP), FORM_ELEMENTS_SUBRECORD);
}
return $html; return $html;
} }
...@@ -105,8 +148,26 @@ class FormBuildPlain { ...@@ -105,8 +148,26 @@ class FormBuildPlain {
* @return string * @return string
*/ */
public function head() { public function head() {
$html = $this->wrapItem(WRAP_SETUP_TITLE, $this->formSpec['title'], true); $html = '';
$html .= '<form action="?" method="post" target="_top" accept-charset="UTF-8">';
switch ($this->formSpec['render']) {
case 'plain':
$html .= $this->wrapItem(WRAP_SETUP_TITLE, $this->formSpec['title'], true);
$html .= '<form action="?" method="post" target="_top" accept-charset="UTF-8">';
break;
case 'table':
$html .= $this->wrapItem(WRAP_SETUP_TITLE, $this->formSpec['title'], true);
$html .= '<table>';
$html .= '<form action="?" method="post" target="_top" accept-charset="UTF-8">';
break;
case 'bootstrap':
$html .= $this->wrapItem(WRAP_SETUP_TITLE, $this->formSpec['title'], true);
$html .= '<form action="?" method="post" target="_top" accept-charset="UTF-8">';
break;
default:
throw new qfq\exceptions\CodeException('Unknown Form.render: \'' . $this->formSpec['render'] . '\'', ERROR_UNKNOWN_FORM_RENDER);
}
return $html; return $html;
} }
...@@ -125,28 +186,36 @@ class FormBuildPlain { ...@@ -125,28 +186,36 @@ class FormBuildPlain {
/** /**
* @param $recordId * @param $recordId
* @param string $elementType FORM_ELEMENTS_NATIVE | FORM_ELEMENTS_SUBRECORD | FORM_ELEMENTS_NATIVE_SUBRECORD
* @return string * @return string
* @throws exceptions\CodeException * @throws exceptions\CodeException
* @throws exceptions\DbException * @throws exceptions\DbException
*/ */
public function elements($recordId) { public function elements($recordId, $elementType = FORM_ELEMENTS_NATIVE) {
$html = ''; $html = '';
// get current data record // get current data record
if ($recordId > 0) { if ($recordId > 0 && $this->store->getVar('id', STORE_RECORD) === false) {
$row = $this->db->sql("SELECT * FROM `" . $this->formSpec['tableName'] . "` WHERE id = ?", ROW_REGULAR, array($recordId)); $row = $this->db->sql("SELECT * FROM `" . $this->formSpec['tableName'] . "` WHERE id = ?", ROW_REGULAR, array($recordId));
$this->store->setVarArray($row[0], STORE_RECORD); $this->store->setVarArray($row[0], STORE_RECORD);
} }
// Iterate over all FormElements // Iterate over all FormElements
foreach ($this->feSpecNative as $fe) { foreach ($this->feSpecNative as $fe) {
if (($elementType === FORM_ELEMENTS_NATIVE && $fe['type'] === 'subrecord') ||
($elementType === FORM_ELEMENTS_SUBRECORD && $fe['type'] !== 'subrecord')
) {
continue; // skip this FE
}
// Log / Debug // Log / Debug
$this->store->setVar(SYSTEM_FORM_ELEMENT, $fe['name'] . ' / ' . $fe['id'], STORE_SYSTEM); $this->store->setVar(SYSTEM_FORM_ELEMENT, $fe['name'] . ' / ' . $fe['id'], STORE_SYSTEM);
// evaluate current FormElement // evaluate current FormElement
$formElement = $this->evaluate->parseArray($fe); $formElement = $this->evaluate->parseArray($fe);
// Default Value // Get default value
$value = $formElement['value'] === '' ? $this->store->getVar($formElement['name']) : $value = $formElement['value']; $value = $formElement['value'] === '' ? $this->store->getVar($formElement['name']) : $value = $formElement['value'];
$htmlFormElementId = $formElement['name'] . ':' . $recordId; $htmlFormElementId = $formElement['name'] . ':' . $recordId;
...@@ -154,13 +223,35 @@ class FormBuildPlain { ...@@ -154,13 +223,35 @@ class FormBuildPlain {
// Construct Marshaller Name // Construct Marshaller Name
$buildElementFunctionName = 'build' . $this->buildElementFunctionName[$formElement['type']]; $buildElementFunctionName = 'build' . $this->buildElementFunctionName[$formElement['type']];
$html .= $this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_START]; switch ($this->formSpec['render']) {
$html .= $this->wrapItem(WRAP_SETUP_LABEL, $this->buildLabel($htmlFormElementId, $formElement['label'])); case 'plain':
// Call Marschaller $html .= $this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_START];
$html .= $this->wrapItem(WRAP_SETUP_INPUT, $this->$buildElementFunctionName($formElement, $htmlFormElementId, $value)); $html .= $this->wrapItem(WRAP_SETUP_LABEL, $this->buildLabel($htmlFormElementId, $formElement['label']));
$html .= $this->wrapItem(WRAP_SETUP_NOTE, $formElement['note']); $html .= $this->wrapItem(WRAP_SETUP_INPUT, $this->$buildElementFunctionName($formElement, $htmlFormElementId, $value));
$html .= $this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_END]; $html .= $this->wrapItem(WRAP_SETUP_NOTE, $formElement['note']);
$html .= $this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_END];
break;
case 'table':
if ($formElement['type'] === 'subrecord') {
// subrecord in render='table' are outside the table
$html .= $this->wrapItem(WRAP_SETUP_SUBRECORD, $formElement['label']);
$html .= $this->wrapItem(WRAP_SETUP_SUBRECORD, $this->$buildElementFunctionName($formElement, $htmlFormElementId, $value));
$html .= $this->wrapItem(WRAP_SETUP_SUBRECORD, $formElement['note']);
} else {
$html .= $this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_START];
$html .= $this->wrapItem(WRAP_SETUP_LABEL, $this->buildLabel($htmlFormElementId, $formElement['label']));
$html .= $this->wrapItem(WRAP_SETUP_INPUT, $this->$buildElementFunctionName($formElement, $htmlFormElementId, $value));
$html .= $this->wrapItem(WRAP_SETUP_NOTE, $formElement['note']);
$html .= $this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_END];
}
break;
case 'bootstrap':
$html .= $this->wrapItem(WRAP_SETUP_TITLE, $this->formSpec['title'], true);
$html .= '<form action="?" method="post" target="_top" accept-charset="UTF-8">';
break;
default:
throw new qfq\exceptions\CodeException('Unknown Form.render: \'' . $this->formSpec['render'] . '\'', ERROR_UNKNOWN_FORM_RENDER);
}
} }
return $html; return $html;
} }
...@@ -182,16 +273,45 @@ class FormBuildPlain { ...@@ -182,16 +273,45 @@ class FormBuildPlain {
public function tail() { public function tail() {
$html = ''; $html = '';
$recordId = $this->store->getVar(SIP_RECORD_ID, STORE_SIP); // $recordId = $this->store->getVar(SIP_RECORD_ID, STORE_SIP);
$sip = $this->store->getVar(CLIENT_SIP, STORE_CLIENT); $sip = $this->store->getVar(CLIENT_SIP, STORE_CLIENT);
$sipName = CLIENT_SIP . ':' . $recordId; // $sipName = CLIENT_SIP . ':' . $recordId;
$html .= '<input type="hidden" name="' . $sipName . '" value="' . $sip . '">'; $sipName = CLIENT_SIP;
// $html .= '<input type="hidden" name="' . $sipName . '" value="' . $sip . '">';
$html .= '<input type="submit" value="Submit"></form>'; $html .= $this->buildHidden(array(), $sipName, $sip);
switch ($this->formSpec['render']) {
case 'plain':
$html .= $this->wrapItem(WRAP_SETUP_INPUT, '<input type="submit" value="Submit">');
$html = $this->wrapItem(WRAP_SETUP_ELEMENT, $html);
$html .= '</form>';
break;
case 'table':
$html .= $this->wrapItem(WRAP_SETUP_LABEL, '', false);
$html .= $this->wrapItem(WRAP_SETUP_INPUT, '<input type="submit" value="Submit">');
$html = $this->wrapItem(WRAP_SETUP_ELEMENT, $html);
$html .= '</table></form>';
break;
case 'bootstrap':
// TODO: bootstrap
break;
default:
throw new qfq\exceptions\CodeException('Unknown Form.render: \'' . $this->formSpec['render'] . '\'', ERROR_UNKNOWN_FORM_RENDER);
}
return $html; return $html;
} }
/**
* @param array $formElement
* @param $htmlFormElementId
* @param $value
* @return string
*/
private function buildHidden(array $formElement, $htmlFormElementId, $value) {
return '<input type="hidden" name="' . $htmlFormElementId . '" value="' . htmlentities($value) . '">';
}
/** /**
* Builds HTML 'input' element. * Builds HTML 'input' element.
* Format: <input name="$htmlFormElementId" <type="email|input|password|url" [autocomplete="autocomplete"] [autofocus="autofocus"] * Format: <input name="$htmlFormElementId" <type="email|input|password|url" [autocomplete="autocomplete"] [autofocus="autofocus"]
...@@ -533,16 +653,6 @@ class FormBuildPlain { ...@@ -533,16 +653,6 @@ class FormBuildPlain {
return $html; return $html;
} }
/**
* @param array $formElement
* @param $htmlFormElementId
* @param $value
* @return string
*/
private function buildHidden(array $formElement, $htmlFormElementId, $value) {
return '<input type="hidden" name="' . $htmlFormElementId . '" value="' . htmlentities($value) . '">';
}
/** /**
* @param array $formElement * @param array $formElement
* @param $htmlFormElementId * @param $htmlFormElementId
......
...@@ -33,6 +33,7 @@ const WRAP_SETUP_ELEMENT = 'element'; ...@@ -33,6 +33,7 @@ const WRAP_SETUP_ELEMENT = 'element';
const WRAP_SETUP_LABEL = 'label'; const WRAP_SETUP_LABEL = 'label';
const WRAP_SETUP_INPUT = 'input'; const WRAP_SETUP_INPUT = 'input';
const WRAP_SETUP_NOTE = 'note'; const WRAP_SETUP_NOTE = 'note';
const WRAP_SETUP_SUBRECORD = 'subrecord';
const WRAP_SETUP_START = 'start'; const WRAP_SETUP_START = 'start';
const WRAP_SETUP_END = 'end'; const WRAP_SETUP_END = 'end';
...@@ -46,8 +47,11 @@ const ROW_EMPTY_IS_OK = "empty_is_ok"; ...@@ -46,8 +47,11 @@ const ROW_EMPTY_IS_OK = "empty_is_ok";
const IF_VALUE_EMPTY_COPY_KEY = 'if_value_empty_copy_key'; const IF_VALUE_EMPTY_COPY_KEY = 'if_value_empty_copy_key';
const VALUE_GIVEN = 'value_given'; const VALUE_GIVEN = 'value_given';
// FormBuildPlain // BuildForm
const SUBRECORD_COLUMN_WIDTH = 20; const SUBRECORD_COLUMN_WIDTH = 20;
const FORM_ELEMENTS_NATIVE = 'native';
const FORM_ELEMENTS_SUBRECORD = 'subrecord';
const FORM_ELEMENTS_NATIVE_SUBRECORD = 'native_subrecord';
// QFQ Error Codes // QFQ Error Codes
const ERROR_UNKNOW_SANATIZE_CLASS = 1001; const ERROR_UNKNOW_SANATIZE_CLASS = 1001;
...@@ -77,6 +81,7 @@ const ERROR_CHECKBOXMODE_UNKNOWN = 1024; ...@@ -77,6 +81,7 @@ const ERROR_CHECKBOXMODE_UNKNOWN = 1024;
const ERROR_MISSING_SQL1 = 1025; const ERROR_MISSING_SQL1 = 1025;
const ERROR_CHECKBOX_EQUAL = 1026; const ERROR_CHECKBOX_EQUAL = 1026;
const ERROR_MISSING_ITEM_VALUES = 1027; const ERROR_MISSING_ITEM_VALUES = 1027;
const ERROR_UNKNOWN_FORM_RENDER = 1028;
// DB Errors // DB Errors
//const ERROR_DB_QUERY_SIMPLE = 2000; //const ERROR_DB_QUERY_SIMPLE = 2000;
......
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