Commit 6327062a authored by Carsten  Rose's avatar Carsten Rose
Browse files

Load templateGroup values during form load.

AbstractBuildForm.php: implemented function templateGroupCollectFilledElements(). Add function.
parent b14087ac
......@@ -2318,6 +2318,64 @@ abstract class AbstractBuildForm {
return $html;
}
/**
* Build real FormElements based on the current templateGroup FormElements in $this->feSpecNative.
* Take a group, if at least one of the elements is filled with a non default value.
* Stop filling elements at the first complete empty group.
*
* @param $max
* @return string
* @throws CodeException
* @throws \qfq\UserFormException
*/
private function templateGroupCollectFilledElements($max) {
$record = $this->store->getStore(STORE_RECORD); // current values
if($record===false || count($record)===0) {
return '';
}
$default = $this->store->getStore(STORE_TABLE_DEFAULT); // current defaults
$feSpecNativeFilled = array();
for($ii = 1; $ii < $max; $ii++) {
$flagFound = false; // Marker if there is at least one element per row who is set.
$feSpecNativeNew = array();
// Iterate over all templateGroup FormElements
foreach($this->feSpecNative as $fe) {
$columnName = str_replace(FE_TEMPLATE_GROUP_NAME_PATTERN, $ii, $fe[FE_NAME]);
if(isset($record[$columnName])) {
if($record[$columnName] != $default[$columnName]) {
$flagFound = true;
}
} else {
throw new UserFormException("Not implemented: templateGroup FormElement-columns outside of primary table.", ERROR_NOT_IMPLEMENTED);
}
$fe[FE_NAME] = $columnName;
$feSpecNativeNew[] = $fe; // Build array with current copy of templateGroup.
}
if(!$flagFound) {
break;
}
$feSpecNativeFilled = array_merge($feSpecNativeFilled, $feSpecNativeNew);
}
// Nothing found: return
if(count($feSpecNativeFilled)==0){
return '';
}
$feSpecNativeSave = $this->feSpecNative;
$this->feSpecNative = $feSpecNativeFilled;
$json=array();
$html = $this->elements($this->store->getVar(SIP_RECORD_ID, STORE_SIP), FORM_ELEMENTS_NATIVE, 0, $json);
$this->feSpecNative = $feSpecNativeSave;
return $html;
}
/**
* Build a HTML fieldset. Renders all assigned FormElements inside the fieldset.
*
......@@ -2341,7 +2399,7 @@ abstract class AbstractBuildForm {
$classCustom = Support::setIfNotSet($formElement, FE_TEMPLATE_GROUP_CLASS);
// save parent processed FE's
$tmpStore = $this->feSpecNative;
$feSpecNativeSave = $this->feSpecNative;
$qfqFieldsName = 'qfq_fields_' . $formElement[FE_ID]; // ='qfq-fields'
$templateName = 'template_' . $formElement[FE_ID]; // ='template'
......@@ -2371,12 +2429,28 @@ EOT;
</div>
EOT;
// child FE's
$this->feSpecNative = $this->db->getNativeFormElements(SQL_FORM_ELEMENT_SPECIFIC_CONTAINER,
['yes', $this->formSpec["id"], 'native,container', $formElement['id']], $this->formSpec);
// Count defined FormElements in the current templateGroup
$last = count($this->feSpecNative) - 1;
if ($last < 1) {
$this->feSpecNative = $feSpecNativeSave;
return '';
}
// Append 'delete' Button at the note of the last element
$this->feSpecNative[$last][FE_NOTE] .= $htmlDelete;
// If there are already elements filled, take them.
$html = $this->templateGroupCollectFilledElements($max);
$attribute = Support::doAttribute('class', $qfqFieldsName);
$attribute .= Support::doAttribute('id', $targetName);
$attribute .= Support::doAttribute('data-qfq-line-template', '#' . $templateName);
// Element where the effective FormElements will be copied to. The BS 'col-md-* Classes are inside the template, not here. This here should be pure data without wrapping.
$html = Support::wrapTag("<div $attribute>", '', false);
$html = Support::wrapTag("<div $attribute>", $html, false);
// Add button, row below: The label & note of the FormElement 'templateGroup' will be used for the add button row.
$tmpFe = $formElement;
$tmpFe[FE_NAME] = '_add';
......@@ -2386,16 +2460,6 @@ EOT;
$html = $this->wrap[WRAP_SETUP_IN_TEMPLATE_GROUP][WRAP_SETUP_START] . $html . $this->wrap[WRAP_SETUP_IN_TEMPLATE_GROUP][WRAP_SETUP_END];
// child FE's
$this->feSpecNative = $this->db->getNativeFormElements(SQL_FORM_ELEMENT_SPECIFIC_CONTAINER,
['yes', $this->formSpec["id"], 'native,container', $formElement['id']], $this->formSpec);
$last = count($this->feSpecNative) - 1;
if ($last < 1) {
return '';
}
$this->feSpecNative[$last][FE_NOTE] .= $htmlDelete;
// Get FE natives
$template = $this->elements($this->store->getVar(SIP_RECORD_ID, STORE_SIP), FORM_ELEMENTS_NATIVE, 0, $json);
......@@ -2407,7 +2471,7 @@ EOT;
$html .= $template . $codeJs;
// restore parent processed FE's
$this->feSpecNative = $tmpStore;
$this->feSpecNative = $feSpecNativeSave;
//TODO: nicht klar ob das hier noetig ist.
// $json = $this->getJsonElementUpdate($htmlFormElementId, $value, $formElement[FE_MODE]);
......
......@@ -562,7 +562,7 @@ const FE_TEMPLATE_GROUP_REMOVE_CLASS = 'tgRemoveClass';
const FE_TEMPLATE_GROUP_REMOVE_TEXT = 'tgRemoveText';
const FE_TEMPLATE_GROUP_CLASS = 'tgClass';
const FE_TEMPLATE_GROUP_DEFAULT_MAX_LENGTH = 5;
const FE_TEMPLATE_GROUP_NAME_PATTERN = '%d';
const RETYPE_FE_NAME_EXTENSION = 'RETYPE';
// FormElement Types
......
......@@ -79,7 +79,7 @@ class FillStoreForm {
// Copy each native FormElement
$template = $fe[FE_NAME];
for ($ii = 1; $ii <= $maxCopies; $ii++) {
$fe[FE_NAME] = str_replace('%d', $ii, $template);
$fe[FE_NAME] = str_replace(FE_TEMPLATE_GROUP_NAME_PATTERN, $ii, $template);
$expanded[] = $fe;
}
}
......
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