Commit 595a1d2a authored by Carsten  Rose's avatar Carsten Rose
Browse files

Form.php: name space adjusted

FormBuildPlain.php: first simple version of subrecord implemented
parent c7708a9d
......@@ -14,7 +14,8 @@ use qfq\exceptions\UserException;
use qfq\exceptions\CodeException;
use qfq\exceptions\DbException;
use qfq\helper;
use qfq\store;
use qfq\store\Store;
require_once(__DIR__ . '/../qfq/store/Store.php');
require_once(__DIR__ . '/../qfq/Constants.php');
......@@ -41,7 +42,7 @@ require_once(__DIR__ . '/../qfq/FormBuildPlain.php');
class Form {
/**
* @var Store instantiated class
* @var \qfq\store\Store instantiated class
*/
protected $store = null;
/**
......@@ -78,7 +79,7 @@ class Form {
*/
public function __construct($bodytext = '') {
$this->store = \qfq\store\Store::getInstance($bodytext);
$this->store = Store::getInstance($bodytext);
$this->db = new Database();
$this->eval = new Evaluate($this->store, $this->db);
}
......
......@@ -61,7 +61,7 @@ class FormBuildPlain {
'radio' => 'Radio',
'select' => 'Select',
'subrecord' => 'subrecord',
'upload' => 'Upload',
'upload' => 'File',
'url' => 'Input'
];
......@@ -203,7 +203,7 @@ class FormBuildPlain {
*/
private function buildInput(array $formElement, $htmlFormElementId, $value) {
$attribute = $this->buildAttribute('name', $htmlFormElementId);
$attribute = $this->getAttribute('name', $htmlFormElementId);
$htmlTag = '<input';
$htmlTagClosing = '';
......@@ -213,24 +213,24 @@ class FormBuildPlain {
$htmlTag = '<textarea';
$htmlTagClosing = '</textarea>';
$attribute .= $this->buildAttribute('cols', $colsRows[0]);
$attribute .= $this->buildAttribute('rows', $colsRows[1]);
$attribute .= $this->getAttribute('cols', $colsRows[0]);
$attribute .= $this->getAttribute('rows', $colsRows[1]);
} else {
$attribute .= $this->buildAttribute('type', $formElement['type']);
$attribute .= $this->buildAttribute('size', $formElement['size']);
$attribute .= $this->getAttribute('type', $formElement['type']);
$attribute .= $this->getAttribute('size', $formElement['size']);
}
// 'maxLength' needs an upper 'L': naming convention for DB tables!
$attribute .= $this->buildAttributeList($formElement, ['autocomplete', 'autofocus', 'maxLength', 'placeholder']);
$attribute .= $this->getAttributeList($formElement, ['autocomplete', 'autofocus', 'maxLength', 'placeholder']);
if ($formElement['type'] === 'email' && $formElement['checkType'] === '')
$formElement['checkType'] = 'email';
$attribute .= $this->buildInputCheckPattern($formElement['checkType'], $formElement['checkPattern']);
$attribute .= $this->getInputCheckPattern($formElement['checkType'], $formElement['checkPattern']);
$attribute .= $this->buildModeAttribute($formElement);
$attribute .= $this->getAttributeMode($formElement);
$attribute .= $this->buildAttribute('value', addslashes($value), false);
$attribute .= $this->getAttribute('value', addslashes($value), false);
return "$htmlTag $attribute>$htmlTagClosing";
}
......@@ -241,7 +241,7 @@ class FormBuildPlain {
* @param bool|false $flagOmitEmpty
* @return string
*/
private function buildAttribute($type, $value, $flagOmitEmpty = true) {
private function getAttribute($type, $value, $flagOmitEmpty = true) {
if ($flagOmitEmpty && $value === "")
return '';
......@@ -257,11 +257,11 @@ class FormBuildPlain {
* @param array $attributeList
* @return string
*/
private function buildAttributeList(array $formElement, array $attributeList) {
private function getAttributeList(array $formElement, array $attributeList) {
$attribute = '';
foreach ($attributeList as $item) {
if (isset($formElement[$item]))
$attribute .= $this->buildAttribute(strtolower($item), $formElement[$item]);
$attribute .= $this->getAttribute(strtolower($item), $formElement[$item]);
}
return $attribute;
}
......@@ -281,7 +281,7 @@ class FormBuildPlain {
* @param $data
* @return string
*/
private function buildInputCheckPattern($type, $data) {
private function getInputCheckPattern($type, $data) {
if ($type === '') {
return '';
}
......@@ -303,22 +303,22 @@ class FormBuildPlain {
* @return string
* @throws UserException
*/
private function buildModeAttribute(array $formElement) {
private function getAttributeMode(array $formElement) {
$attribute = '';
switch ($formElement['mode']) {
case 'show':
break;
case 'readonly':
$attribute .= $this->buildAttribute('readonly', 'readonly');
$attribute .= $this->getAttribute('readonly', 'readonly');
break;
case 'required':
$attribute .= $this->buildAttribute('required', 'required');
$attribute .= $this->getAttribute('required', 'required');
break;
case 'lock':
break;
case 'disabled':
$attribute .= $this->buildAttribute('disabled', 'disabled');
$attribute .= $this->getAttribute('disabled', 'disabled');
break;
default:
$this->store->setVar(SYSTEM_FORM_ELEMENT, $formElement['name'] . ' / ' . $formElement['id'], STORE_SYSTEM);
......@@ -364,9 +364,9 @@ class FormBuildPlain {
$this->prepareCheckboxCheckedUncheckedValue($itemKey, $formElement);
}
$attributeBase = $this->buildModeAttribute($formElement);
$attributeBase .= $this->buildAttribute('name', $htmlFormElementId);
$attributeBase .= $this->buildAttribute('type', $formElement['type']);
$attributeBase = $this->getAttributeMode($formElement);
$attributeBase .= $this->getAttribute('name', $htmlFormElementId);
$attributeBase .= $this->getAttribute('type', $formElement['type']);
switch ($formElement['checkBoxMode']) {
case 'single':
......@@ -383,7 +383,7 @@ class FormBuildPlain {
}
/**
* Extract key/value list from `sql1`, `parameter:itemList`, column definition (in this order, first match counts)
* Look for key/value list in `sql1`, `parameter:itemList`, column definition (in this order, first match counts)
* Copies the found keys to &$itemKey and the values to &$itemValue
*
* @param array $formElement
......@@ -403,11 +403,11 @@ class FormBuildPlain {
$itemKey = array_column($formElement['sql1'], 'id');
} elseif (isset($formElement['itemList'])) {
$arr = qfq\helper\KeyValueStringParser::parse($formElement['itemList'], ':', ',', KEY_VALUE_SINGLE);
$arr = qfq\helper\KeyValueStringParser::parse($formElement['itemList'], ':', ',', IF_VALUE_EMPTY_COPY_KEY);
$itemValue = array_values($arr);
$itemKey = array_keys($arr);
} elseif ($fieldType === 'enum' || $fieldType === 'set') {
// already done
// already done at the beginning with '$this->getItemsForEnumOrSet($formElement['name'], $fieldType);'
} else {
throw new UserException("Missing definition (nothing in 'sql1', 'parameter:itemValues, 'enum-' or 'set-definition')", ERROR_MISSING_ITEM_VALUES);
}
......@@ -500,15 +500,15 @@ class FormBuildPlain {
*/
private function buildCheckboxSingle(array $formElement, $htmlFormElementId, $attribute, $value) {
$attribute .= $this->buildAttribute('name', $htmlFormElementId);
$attribute .= $this->buildAttribute('value', $formElement['checked'], false);
$attribute .= $this->getAttribute('name', $htmlFormElementId);
$attribute .= $this->getAttribute('value', $formElement['checked'], false);
if ($formElement['checked'] === $value) {
$attribute .= $this->buildAttribute('checked', 'checked');
$attribute .= $this->getAttribute('checked', 'checked');
}
$attribute .= $this->buildAttributeList($formElement, ['autofocus']);
$attribute .= $this->getAttributeList($formElement, ['autofocus']);
$html = $this->buildHidden($htmlFormElementId, $formElement['unchecked']);
$html = $this->buildHidden($formElement, $htmlFormElementId, $formElement['unchecked']);
$html .= '<input ' . $attribute . '>';
if (isset($formElement['label2'])) {
......@@ -519,11 +519,12 @@ class FormBuildPlain {
}
/**
* @param array $formElement
* @param $htmlFormElementId
* @param $value
* @return string
*/
private function buildHidden($htmlFormElementId, $value) {
private function buildHidden(array $formElement, $htmlFormElementId, $value) {
return '<input type="hidden" name="' . $htmlFormElementId . '" value="' . addslashes($value) . '">';
}
......@@ -540,9 +541,9 @@ class FormBuildPlain {
// Defines which of the checkboxes will be checked.
$values = explode($value, ',');
$attributeBase .= $this->buildAttribute('name', $htmlFormElementId);
$attributeBase .= $this->getAttribute('name', $htmlFormElementId);
$html = $this->buildHidden($htmlFormElementId, '');
$html = $this->buildHidden($formElement, $htmlFormElementId, $value);
$flagFirst = true;
$ii = 0;
......@@ -554,11 +555,11 @@ class FormBuildPlain {
if ($flagFirst) {
$flagFirst = false;
if (isset($formElement['autofocus']))
$attribute .= $this->buildAttribute('autofocus', $formElement['autofocus']);
$attribute .= $this->getAttribute('autofocus', $formElement['autofocus']);
}
$attribute .= $this->buildAttribute('value', $item);
$attribute .= $this->getAttribute('value', $item);
if ($item === $values[$jj]) {
$attribute .= $this->buildAttribute('checked', 'checked');
$attribute .= $this->getAttribute('checked', 'checked');
}
$html .= '<input ' . $attribute . '>';
$html .= $itemValue[$ii];
......@@ -570,14 +571,6 @@ class FormBuildPlain {
return $html;
}
/**
* @param $value
* @return mixed
*/
private function buildNote($value) {
return $value;
}
/**
* Build HTML 'radio' element.
*
......@@ -600,26 +593,26 @@ class FormBuildPlain {
// Fill $itemKey & $itemValue
$this->extractKeyValueList($formElement, $itemKey, $itemValue);
$attributeBase = $this->buildModeAttribute($formElement);
$attributeBase .= $this->buildAttribute('name', $htmlFormElementId);
$attributeBase .= $this->buildAttribute('type', $formElement['type']);
$attributeBase = $this->getAttributeMode($formElement);
$attributeBase .= $this->getAttribute('name', $htmlFormElementId);
$attributeBase .= $this->getAttribute('type', $formElement['type']);
$jj = 0;
$flagFirst = true;
$html = $this->buildHidden($htmlFormElementId, '');
$html = $this->buildHidden($formElement, $htmlFormElementId, $value);
for ($ii = 0; $ii < count($itemValue); $ii++) {
$jj++;
$attribute = $attributeBase;
if ($flagFirst) {
$flagFirst = false;
if (isset($formElement['autofocus']))
$attribute .= $this->buildAttribute('autofocus', $formElement['autofocus']);
$attribute .= $this->getAttribute('autofocus', $formElement['autofocus']);
}
$attribute .= $this->buildAttribute('value', $itemKey[$ii]);
$attribute .= $this->getAttribute('value', $itemKey[$ii]);
if ($itemKey[$ii] === $value) {
$attribute .= $this->buildAttribute('checked', 'checked');
$attribute .= $this->getAttribute('checked', 'checked');
}
$html .= '<input ' . $attribute . '>';
$html .= $itemValue[$ii];
......@@ -631,22 +624,167 @@ class FormBuildPlain {
return $html;
}
private function buildSelect() {
/**
* @param array $formElement
* @param $htmlFormElementId
* @param $value
* @return mixed
*/
private function buildSelect(array $formElement, $htmlFormElementId, $value) {
$itemKey = array();
$itemValue = array();
// Fill $itemKey & $itemValue
$this->extractKeyValueList($formElement, $itemKey, $itemValue);
$attribute = $this->getAttributeMode($formElement);
$attribute .= $this->getAttribute('name', $htmlFormElementId);
$attribute .= $this->getAttributeList($formElement, ['type', 'autofocus']);
if (isset($formElement['size']) && $formElement['size'] > 1) {
$attribute .= $this->getAttribute('size', $formElement['size']);
$attribute .= $this->getAttribute('multiple', 'multiple');
}
$option = '';
if (isset($formElement['emptyItemAtStart']))
$option = '<option></option>';
for ($ii = 0; $ii < count($itemValue); $ii++) {
$option .= '<option ';
$option .= $this->getAttribute('value', $itemKey[$ii]);
if ($itemKey[$ii] === $value) {
$attribute .= 'selected ';
}
$option .= '>' . $itemValue[$ii] . '</option>';
}
if (isset($formElement['emptyItemAtEnd']))
$option = '<option></option>';
return '<select ' . $attribute . '>' . $option . '</select>';
}
private function buildSubrecord() {
/**
* Constuct a HTML table of the subrecord data.
* Column syntax definition: https://wikiit.math.uzh.ch/it/projekt/qfq/qfq-jqwidgets/Documentation#Type:_subrecord
*
* @param array $formElement
* @param $htmlFormElementId
* @param $value
* @return string
* @throws UserException
*/
private function buildSubrecord(array $formElement, $htmlFormElementId, $value) {
if (!is_array($formElement['sql1'])) {
throw new UserException('Missing \'sql1\' Query', ERROR_MISSING_SQL1);
}
// construct column attributes
$control = $this->getSubrecordColumnControl(array_keys($formElement['sql1'][0]));
$html = '<b>' . $formElement['label'] . '</b>';
$html .= '<table>';
$html .= '<tr><th>' . implode('</th><th>', $control['title']) . '</th></tr>';
foreach ($formElement['sql1'] as $row) {
$html .= '<tr>';
foreach ($row as $col => $value) {
if (isset($control['icon'][$col])) {
$cell = ($value === '') ? '' : "<image src='fileadmin/icons/$value'>";
} else {
$cell = isset($control['nostrip'][$col]) ? $value : strip_tags($value);
$cell = substr($cell, 0, $control['width'][$col]);
}
$html .= "<td>$cell</td>";
}
$html .= '</tr>';
}
$html .= '</table>';
return "<p>$html</p>";
}
private function buildUpload() {
/**
* @param $titleRaw
* @return array
* @throws UserException
*/
private function getSubrecordColumnControl($titleRaw) {
$control = array();
for ($ii = 0; $ii < count($titleRaw); $ii++) {
// foreach ($titleRaw AS $col) {
$col = $titleRaw[$ii];
$control['width'][$col] = SUBRECORD_COLUMN_WIDTH;
// a) 'City@width=40', b) 'Status@icon', c) 'Mailto@width=80@nostrip'
$arr = qfq\helper\KeyValueStringParser::parse($titleRaw[$ii], '=', '@', IF_VALUE_EMPTY_COPY_KEY);
foreach ($arr as $key => $value) {
switch ($key) {
case 'width':
case 'nostrip':
case 'icon':
break;
default:
$key = 'title';
break;
}
$control[$key][$col] = $value;
}
}
return $control;
}
private function buildDateJQW() {
/**
* @param array $formElement
* @param $htmlFormElementId
* @param $value
* @return string
* @throws UserException
*/
private function buildFile(array $formElement, $htmlFormElementId, $value) {
$attribute = $this->getAttributeMode($formElement);
$attribute .= $this->getAttribute('type', 'file');
$attribute .= $this->getAttribute('name', $htmlFormElementId);
$attribute .= $this->getAttributeList($formElement, ['autofocus', 'accept']);
return '<input ' . $attribute . '>';
}
/**
* @param array $formElement
* @param $htmlFormElementId
* @param $value
* @throws UserException
*/
private function buildDateJQW(array $formElement, $htmlFormElementId, $value) {
// TODO: implement
throw new UserException("Not implemented yet: buildDateJQW()");
}
private function buildGridJQW() {
/**
* @param array $formElement
* @param $htmlFormElementId
* @param $value
* @throws UserException
*/
private function buildGridJQW(array $formElement, $htmlFormElementId, $value) {
// TODO: implement
throw new UserException("Not implemented yet: buildGridJQW()");
}
/**
* @param array $formElement
* @param $htmlFormElementId
* @param $value
* @return mixed
*/
private function buildNote(array $formElement, $htmlFormElementId, $value) {
return $value;
}
}
\ No newline at end of file
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