From 7d21f74a492a6915c9dc911f47820d5d70fac69c Mon Sep 17 00:00:00 2001
From: Carsten  Rose <carsten.rose@math.uzh.ch>
Date: Sat, 30 Jan 2016 17:52:26 +0100
Subject: [PATCH] AbstractBuiltForm, BuildFormBootstrap: generalize
 getActionUrl().

---
 qfq/AbstractBuildForm.php  | 86 +++++++++++++++++++++++++++++++-------
 qfq/BuildFormBootstrap.php | 21 ++++++++--
 2 files changed, 88 insertions(+), 19 deletions(-)

diff --git a/qfq/AbstractBuildForm.php b/qfq/AbstractBuildForm.php
index 778b859c8..27ddf5805 100644
--- a/qfq/AbstractBuildForm.php
+++ b/qfq/AbstractBuildForm.php
@@ -483,6 +483,16 @@ abstract class AbstractBuildForm {
         if (count($itemKey) === 0) {
             $itemKey = $itemValue;
         }
+
+        if (isset($formElement['emptyItemAtStart'])) {
+            array_unshift($itemKey, '');
+            array_unshift($itemValue, '');
+        }
+
+        if (isset($formElement['emptyItemAtEnd'])) {
+            $itemValue[] = '';
+            $itemKey[] = '';
+        }
     }
 
     /**
@@ -728,14 +738,6 @@ abstract class AbstractBuildForm {
             $option .= '>' . $itemValue[$ii] . '</option>';
         }
 
-        if (isset($formElement['emptyItemAtStart'])) {
-            $option = "<option $selected></option>";
-            $selected = '';
-        }
-
-        if (isset($formElement['emptyItemAtEnd']))
-            $option = "<option $selected></option>";
-
         return '<select ' . $attribute . '>' . $option . '</select>';
     }
 
@@ -752,15 +754,12 @@ 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);
         }
@@ -783,7 +782,7 @@ abstract class AbstractBuildForm {
         foreach ($formElement['sql1'] as $row) {
 
             $html .= '<tr>';
-            $html .= '<td>' . $this->editLink($formElement, $page, $row['id'], $primaryRecord) . '</td>';
+            $html .= '<td>' . $this->editLink($formElement, $row['id'], $primaryRecord) . '</td>';
 
             foreach ($row as $columnName => $value) {
                 $html .= '<td>' . $this->formatColumn($control, $columnName, $value) . '</td>';
@@ -839,16 +838,14 @@ abstract class AbstractBuildForm {
 
     /**
      * @param $formElement
-     * @param $page
      * @param $targetRecordId
      * @param $record
      * @return string
      * @throws UserException
      */
-    private function editLink($formElement, $page, $targetRecordId, $record) {
+    private function editLink($formElement, $targetRecordId, $record) {
 
         $queryStringArray = [
-            "id" => $page,
             'form' => $formElement['form'],
             'r' => $targetRecordId,
 
@@ -1004,5 +1001,64 @@ abstract class AbstractBuildForm {
         return $html;
     }
 
+    /**
+     * Rreturns complete '<form ...>'
+     *
+     * @return string
+     */
+    private function getFormTag() {
+
+        $attribute = $this->getFormTagAtrributes();
+
+        return '<form ' . OnArray::toString($attribute, '=', ' ', "'") . '>';
+    }
+
+    /**
+     * Build an assoc array with standard form attributes.
+     *
+     * @return mixed
+     */
+    public function getFormTagAtrributes() {
+
+        $attribute['method'] = 'post';
+        $attribute['action'] = $this->getActionUrl();
+        $attribute['target'] = '_top';
+        $attribute['accept-charset'] = 'UTF-8';
+        $attribute['autocomplete'] = 'on';
+        $attribute['enctype'] = $this->getEncType();
+
+        return $attribute;
+    }
+
+    /**
+     * Builds the HTML 'form'-tag inlcuding all attributes and target.
+     *
+     * @return string
+     * @throws DbException
+     */
+    public function getActionUrl() {
+
+        $queryStringArray['s'] = $this->store->getVar(SIP_SIP, STORE_SIP);
+
+        Support::appendTypo3ParameterToArray($queryStringArray);
+
+        return basename($_SERVER['SCRIPT_NAME']) . "?" . Support::arrayToQueryString($queryStringArray);
+    }
+
+    /**
+     * Determines the enctype.
+     *
+     * See: https://www.w3.org/wiki/HTML/Elements/form#HTML_Attributes
+     *
+     * @return string
+     * @throws DbException
+     */
+    public function getEncType() {
+
+        $result = $this->db->sql("SELECT id FROM FormElement AS fe WHERE fe.formId=? AND fe.type='upload' LIMIT 1", ROW_EMPTY_IS_OK, [$this->formSpec['id']], 'Look for Formelement.type="upload"');
+        return (count($result) === 1) ? 'multipart/form-data' : 'application/x-www-form-urlencoded';
+
+    }
+
 
 }
\ No newline at end of file
diff --git a/qfq/BuildFormBootstrap.php b/qfq/BuildFormBootstrap.php
index b4d047f95..a04d5f6ea 100644
--- a/qfq/BuildFormBootstrap.php
+++ b/qfq/BuildFormBootstrap.php
@@ -85,9 +85,6 @@ class BuildFormBootstrap extends AbstractBuildForm {
     public function head() {
         $html = '';
 
-//        $html .= $this->wrapItem(WRAP_SETUP_TITLE, $this->formSpec['title'], true);
-//        $html .= '<form action="?" method="post" target="_top" accept-charset="UTF-8">';
-
 
         $html .= '<div class="container-fluid">'; //Whole FORM
         $html .= '<div class="row hidden-xs"><div class="col-md-12"><h2>' . $this->formSpec['title'] . '</h2></div></div>'; // Form Title
@@ -97,9 +94,11 @@ class BuildFormBootstrap extends AbstractBuildForm {
 
         $html .= $this->wrapTag('<div class="row">', $pill . $button);
 
-        $html .= '<form class="form-horizontal"  action="?" method="post" target="_top" accept-charset="UTF-8">';
+        $html .= $this->getFormTag();
+
         $html .= '<div class="tab-content">';
 
+
         return $html;
     }
 
@@ -182,6 +181,20 @@ BUTTONS;
         return $html;
     }
 
+    /**
+     * Builds the complete HTML '<form ...>'-tag
+     *
+     * @return string
+     */
+    private function getFormTag() {
+
+        $attribute = $this->getFormTagAtrributes();
+
+        $attribute['class'] = 'form-horizontal';
+
+        return '<form ' . OnArray::toString($attribute, '=', ' ', "'") . '>';
+    }
+
     /**
      * @return string
      */
-- 
GitLab