From 6a428e62d9862164ab6dc81de38402081bd37ca4 Mon Sep 17 00:00:00 2001 From: Carsten Rose <carsten.rose@math.uzh.ch> Date: Thu, 28 Jan 2016 15:48:35 +0100 Subject: [PATCH] AbstractBuildForm: Select Statements might contain empty lists. First version of editlink(). Subrecords with links to detailform. --- qfq/AbstractBuildForm.php | 110 +++++++++++++++++++++++++++++++------ qfq/BuildFormBootstrap.php | 2 +- 2 files changed, 93 insertions(+), 19 deletions(-) diff --git a/qfq/AbstractBuildForm.php b/qfq/AbstractBuildForm.php index c299e2f5b..b8628d2bd 100644 --- a/qfq/AbstractBuildForm.php +++ b/qfq/AbstractBuildForm.php @@ -18,6 +18,7 @@ require_once(__DIR__ . '/../qfq/exceptions/DbException.php'); require_once(__DIR__ . '/../qfq/exceptions/UserException.php'); require_once(__DIR__ . '/../qfq/Database.php'); require_once(__DIR__ . '/../qfq/helper/HelperFormElement.php'); +require_once(__DIR__ . '/../qfq/helper/Support.php'); /** @@ -158,7 +159,11 @@ abstract class AbstractBuildForm { $this->store->setVar(SYSTEM_FORM_ELEMENT, $fe['name'] . ' / ' . $fe['id'], STORE_SYSTEM); // evaluate current FormElement - $formElement = $this->evaluate->parseArray($fe); + $evaluate = new Evaluate($this->store, $this->db); + $formElement = $evaluate->parseArray($fe, $fe['debug'] === 'yes'); + if ($fe['debug'] === 'yes') { + throw new UserException($evaluate->getDebug(), ERROR_DEBUG); + } // Get default value $value = $formElement['value'] === '' ? $this->store->getVar($formElement['name']) : $value = $formElement['value']; @@ -396,7 +401,7 @@ abstract class AbstractBuildForm { // Get fallback, if 'checkBoxMode' is not defined: if (!isset($formElement['checkBoxMode'])) { - // This fallback is problematic if 'set' or 'enum' has 2 elements: defaults to single but maybe multi is meant. + // This fallback is problematic if 'set' or 'enum' has 2 : defaults to single but maybe multi is meant. $formElement['checkBoxMode'] = (count($itemKey) > 2) ? 'multi' : 'single'; } @@ -447,25 +452,28 @@ abstract class AbstractBuildForm { $itemValue = $this->getItemsForEnumOrSet($formElement['name'], $fieldType); if (is_array($formElement['sql1'])) { - $keys = array_keys($formElement['sql1'][0]); - $itemKey = array_column($formElement['sql1'], 'id'); + if (count($formElement['sql1']) > 0) { + $keys = array_keys($formElement['sql1'][0]); + $itemKey = array_column($formElement['sql1'], 'id'); - // If there is no column 'id' and at least two columns in total - if (count($itemKey) === 0 && count($keys) >= 2) { - $itemKey = array_column($formElement['sql1'], $keys[0]); - } + // If there is no column 'id' and at least two columns in total + if (count($itemKey) === 0 && count($keys) >= 2) { + $itemKey = array_column($formElement['sql1'], $keys[0]); + } - $itemValue = array_column($formElement['sql1'], 'label'); - // If there is no column 'label' (e.g.: SHOW tables) - if (count($itemValue) === 0) { - $idx = count($keys) == 1 ? 0 : 1; - $itemValue = array_column($formElement['sql1'], $keys[$idx]); + $itemValue = array_column($formElement['sql1'], 'label'); + // If there is no column 'label' (e.g.: SHOW tables) + if (count($itemValue) === 0) { + $idx = count($keys) == 1 ? 0 : 1; + $itemValue = array_column($formElement['sql1'], $keys[$idx]); + } } - } elseif (isset($formElement['itemList'])) { - $arr = KeyValueStringParser::parse($formElement['itemList'], ':', ',', IF_VALUE_EMPTY_COPY_KEY); - $itemValue = array_values($arr); - $itemKey = array_keys($arr); + if (count($formElement['itemList']) > 0) { + $arr = KeyValueStringParser::parse($formElement['itemList'], ':', ',', IF_VALUE_EMPTY_COPY_KEY); + $itemValue = array_values($arr); + $itemKey = array_keys($arr); + } } elseif ($fieldType === 'enum' || $fieldType === 'set') { // already done at the beginning with '$this->getItemsForEnumOrSet($formElement['name'], $fieldType);' } else { @@ -744,17 +752,39 @@ abstract class AbstractBuildForm { public function buildSubrecord(array $formElement, $htmlFormElementId, $value) { $html = ''; + + $primaryRecord = $this->store->getStore(STORE_RECORD); + + if (!isset($primaryRecord['id'])) { + return 'Please save main record fist.'; + } + + $page = Support::getCurrentPage(); + if (!is_array($formElement['sql1'])) { throw new UserException('Missing \'sql1\' Query', ERROR_MISSING_SQL1); } + // No records? + if (count($formElement['sql1']) == 0) { + return ''; + } + + if (!isset($formElement['sql1'][0]['id'])) { + throw new UserException('Missing column \'id\' in \'sql1\' Query', ERROR_MISSING_COLUMN_ID); + } + // construct column attributes $control = $this->getSubrecordColumnControl(array_keys($formElement['sql1'][0])); + // $html .= '<b>' . $formElement['label'] . '</b>'; $html .= '<table border="1">'; - $html .= '<tr><th>' . implode('</th><th>', $control['title']) . '</th></tr>'; + $html .= '<tr><th></th><th>' . implode('</th><th>', $control['title']) . '</th></tr>'; foreach ($formElement['sql1'] as $row) { + $html .= '<tr>'; + $html .= '<td>' . $this->editLink($formElement, $page, $row['id'], $primaryRecord) . '</td>'; + foreach ($row as $columnName => $value) { $html .= '<td>' . $this->formatColumn($control, $columnName, $value) . '</td>'; } @@ -807,6 +837,50 @@ abstract class AbstractBuildForm { return $control; } + /** + * @param $formElement + * @param $page + * @param $targetRecordId + * @param $record + * @return string + * @throws UserException + */ + private function editLink($formElement, $page, $targetRecordId, $record) { + + $queryStringArray = [ + "id" => $page, + 'form' => $formElement['form'], + 'r' => $targetRecordId, + + ]; + + // Add custom query parameter + if (isset($formElement['detail'])) { + $detailParam = KeyValueStringParser::parse($formElement['detail']); + foreach ($detailParam as $src => $dest) { + if ($src[0] == '#') { + $queryStringArray[$dest] = substr($src, 1); + continue; + } + + if (isset($record[$src])) { + $queryStringArray[$dest] = $record[$src]; + } + } + } + + Support::appendTypo3ParameterToArray($queryStringArray); + + //----------------- + $queryString = Support::arrayToQueryString($queryStringArray); + + $sip = $this->store->getSip(); + $url = $formElement['page'] . $sip->queryStringToSip($queryString); + + return "<a href='$url'><span class='glyphicon glyphicon-pencil'></span></a>"; + + } + /** * @param array $control * @param $columnName diff --git a/qfq/BuildFormBootstrap.php b/qfq/BuildFormBootstrap.php index dd4b762af..f8b07a562 100644 --- a/qfq/BuildFormBootstrap.php +++ b/qfq/BuildFormBootstrap.php @@ -130,7 +130,7 @@ class BuildFormBootstrap extends AbstractBuildForm { throw new UserException("Field 'name' and/or 'label' are empty", ERROR_NAME_LABEL_EMPTY); } - $a = '<a href="#' . $this->createAnker($formElement['id']) . '" data-toggle="tab">' . $formElement['name'] . '</a>'; + $a = '<a href="#' . $this->createAnker($formElement['id']) . '" data-toggle="tab">' . $formElement['label'] . '</a>'; if ($ii <= $maxVisiblePill) { $pillButton .= '<li role="presentation" ' . $active . '>' . $a . '</li>'; -- GitLab