From 84e2a5b66aa9ce2fb558f829c8f51946df49ca70 Mon Sep 17 00:00:00 2001 From: Carsten Rose <carsten.rose@math.uzh.ch> Date: Mon, 9 May 2016 22:33:53 +0200 Subject: [PATCH] FillStoreForm.php: Recode. Class Evaluate will be instantiated only once. collectMultiValues() recoded not to use arrays anymore, instead a string with the final values, seperated by comma, is returned. --- extension/qfq/qfq/store/FillStoreForm.php | 47 ++++++++++------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/extension/qfq/qfq/store/FillStoreForm.php b/extension/qfq/qfq/store/FillStoreForm.php index 6c6514827..8f6308b38 100644 --- a/extension/qfq/qfq/store/FillStoreForm.php +++ b/extension/qfq/qfq/store/FillStoreForm.php @@ -31,13 +31,21 @@ class FillStoreForm { */ private $feSpecNative = array(); + /** + * @var Evaluate + */ + private $evaluate = null; + /** * */ public function __construct() { + $this->store = Store::getInstance(); $this->db = new Database(); $this->feSpecNative = $this->loadFormElementsBasedOnSIP(); + $this->evaluate = new Evaluate($this->store, $this->db); + } /** @@ -106,21 +114,16 @@ class FillStoreForm { // Preparation for Log, Debug $this->store->setVar(SYSTEM_FORM_ELEMENT, Logger::formatFormElementName($formElement), STORE_SYSTEM); - // evaluate current FormElement: e.g. FE_MODE_SQL - $evaluate = new Evaluate($this->store, $this->db); - $formElement = $evaluate->parseArray($formElement, $debugStack); + // Evaluate current FormElement: e.g. FE_MODE_SQL + $formElement = $this->evaluate->parseArray($formElement, $debugStack); - // Get related formElement. - // construct the field name used in the form + // Get related formElement. Construct the field name used in the form. $clientFieldName = HelperFormElement::buildFormElementId($formElement['name'], $sipValues[SIP_RECORD_ID]); // Some Defaults $formElement = Support::setFeDefaults($formElement); - // Preparation for Log, Debug -// $this->store->setVar(SYSTEM_FORM_ELEMENT, $formElement['name'] . ' / ' . $formElement['id'], STORE_SYSTEM); - - if ($formElement[FE_TYPE] == 'hidden') { + if ($formElement[FE_TYPE] === 'hidden') { // Hidden elements will be transferred by SIP if (!isset($sipValues[$formElement['name']])) { throw new CodeException("Missing the hidden field '" . $formElement['name'] . "' in SIP.", ERROR_MISSING_HIDDEN_FIELD_IN_SIP); @@ -131,8 +134,8 @@ class FillStoreForm { } // Checkbox Multi: collect values - if ($formElement[FE_TYPE] == 'checkbox') { - $clientValues[$clientFieldName] = $this->collectCheckboxMultiValue($clientFieldName, $clientValues); + if ($formElement[FE_TYPE] === 'checkbox') { + $clientValues[$clientFieldName] = $this->collectMultiValues($clientFieldName, $clientValues); } if ($formElement[FE_MODE] === FE_MODE_REQUIRED) { @@ -146,16 +149,6 @@ class FillStoreForm { case FE_MODE_SHOW: if (isset($clientValues[$clientFieldName])) { - // CHECKBOX MULTI (SELECT MULTI ?): delivered as array - implode them. - if (is_array($clientValues[$clientFieldName])) { - // E.g. Checkboxes needs a 'HIDDEN' HTML input to detect 'unset' of values. These 'HIDDEN' element - // needs to be removed, if there is at least one checkbox is checked (=submitted) - if (count($clientValues[$clientFieldName]) > 1) - array_shift($clientValues[$clientFieldName]); - - $clientValues[$clientFieldName] = implode(',', $clientValues[$clientFieldName]); - } - switch ($formElement[FE_TYPE]) { case 'date': case 'datetime': @@ -183,21 +176,23 @@ class FillStoreForm { } /** + * Steps through all $clientValues (POST vars) and collect all with the name _?_${clientFieldName} in a comma seperated string (MYSQL ENUM type). + * If there is no element '_h_${clientFieldName}', than there no multi values - return the already given `$clientValues[$clientFieldName]`. + * * @param $clientFieldName * @param array $clientValues - * @return mixed + * @return string */ - private function collectCheckboxMultiValue($clientFieldName, array $clientValues) { - $checkboxValue = array(); + private function collectMultiValues($clientFieldName, array $clientValues) { $checkboxKey = HelperFormElement::prependFormElementIdCheckBoxMulti($clientFieldName, 'h'); // Check there is a hidden value with naming in checkbox multi syntax if (isset($clientValues[$checkboxKey])) { - $checkboxValue[] = $clientValues[$checkboxKey]; + $checkboxValue = $clientValues[$checkboxKey]; $pattern = '/' . HelperFormElement::prependFormElementIdCheckBoxMulti($clientFieldName, '\d+') . '/'; foreach ($clientValues as $key => $value) { if (1 === preg_match($pattern, $key)) { - $checkboxValue[] = $value; + $checkboxValue .= ',' . $value; } } -- GitLab