From a7a8982262bd6465d7d7f6df206b60240163b562 Mon Sep 17 00:00:00 2001
From: Carsten  Rose <carsten.rose@math.uzh.ch>
Date: Fri, 8 Apr 2016 16:10:17 +0200
Subject: [PATCH] Documentation/UsersManual/index.rst: example for page
 'search'. Small udpates. BuildFormBootstrap.php, Constants.php,
 formEditor.sql: Buttons 'close' and 'save' are optinal now. buildButtons()
 refactored to be more generic. All (but 'close') buttons now show debug
 tooltips.

---
 extension/Documentation/UsersManual/Index.rst |   8 +-
 extension/qfq/qfq/BuildFormBootstrap.php      | 108 ++++++++++++------
 extension/qfq/qfq/Constants.php               |   2 +
 extension/qfq/sql/formEditor.sql              |   4 +-
 4 files changed, 78 insertions(+), 44 deletions(-)

diff --git a/extension/Documentation/UsersManual/Index.rst b/extension/Documentation/UsersManual/Index.rst
index 51d675f7b..e9306a6bc 100644
--- a/extension/Documentation/UsersManual/Index.rst
+++ b/extension/Documentation/UsersManual/Index.rst
@@ -2168,15 +2168,15 @@ QFQ content record::
   # Creates a small form that redirects back to this page
   10 {
     sql = SELECT '_'
-    head = <form action='#' method='get'><input type='hidden' name='id' value='{{pageId:T}}'>Search for person by name: <input type='text' name='search' value='{{search:CE:all}}'><input type='submit'  value='Search'></form>
+    head = <form action='#' method='get'><input type='hidden' name='id' value='{{pageId:T}}'>Search: <input type='text' name='search' value='{{search:CE:all}}'><input type='submit' value='Submit'></form>
   }
 
-  # SQL statement will find and list all the relevant persons
+  # SQL statement will find and list all the relevant forms
   20 {
-    sql = SELECT CONCAT('?detail&form=form&r=', f.id) AS Pagee, f.name
+    sql = SELECT CONCAT('?detail&form=form&r=', f.id) AS Pagee, f.id, f.name, f.title
               FROM Form AS f
               WHERE f.name LIKE  '%{{search:CE:all}}%'
-    head = <table>
+    head = <table class='table'>
     tail = </table>
     rbeg = <tr>
     rend = </tr>
diff --git a/extension/qfq/qfq/BuildFormBootstrap.php b/extension/qfq/qfq/BuildFormBootstrap.php
index aa94e6795..ff688059f 100644
--- a/extension/qfq/qfq/BuildFormBootstrap.php
+++ b/extension/qfq/qfq/BuildFormBootstrap.php
@@ -10,6 +10,7 @@ namespace qfq;
 
 use qfq;
 use qfq\UserFormException;
+use TYPO3\CMS\Form\Domain\Model\Element\ButtonElement;
 
 require_once(__DIR__ . '/../qfq/Constants.php');
 require_once(__DIR__ . '/../qfq/helper/OnArray.php');
@@ -112,71 +113,102 @@ class BuildFormBootstrap extends AbstractBuildForm {
     }
 
     /**
-     * Simlute Submit Button: http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml
+     * Simulate Submit Button: http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml
      *
      * @return string
      */
     private function buildButtons() {
-        $toolTipNew = 'New';
-        $toolTipDelete = 'Delete';
-        $buttonDelete = '';
         $buttonNew = '';
-        $buttonDebug = '';
+        $buttonDelete = '';
+        $buttonClose = '';
+        $buttonSave = '';
+        $buttonEditForm = '';
         $recordId = $this->store->getVar(SIP_RECORD_ID, STORE_SIP);
 
 
         // Button: FormEdit
         if ($this->showDebugInfo) {
-            $tooltipFormEdit = "Edit form" . PHP_EOL . PHP_EOL . OnArray::toString($this->store->getStore(STORE_SIP), ' = ', PHP_EOL, "'");
-            $urlFormEdit = $this->createFormEditUrl();
-            // Edit Form
-            $buttonDebug = <<<BUTTON
-                <div class="btn-group" role="group">
-                    <a href="$urlFormEdit" id="form-edit-button" class="btn btn-default navbar-btn" title="$tooltipFormEdit"><span class="glyphicon glyphicon-wrench"></span></a>
-                </div>
-BUTTON;
+            $toolTip = "Edit form" . PHP_EOL . PHP_EOL . OnArray::toString($this->store->getStore(STORE_SIP), ' = ', PHP_EOL, "'");
+            $url = $this->createFormEditUrl();
+
+            $buttonEditForm = $this->buildButtonAnchor('form-edit-button', $url, $toolTip, 'glyphicon-wrench');
+        }
+
+        // Button: Save
+        if (Support::findInSet(FORM_BUTTON_SAVE, $this->formSpec['showButton'])) {
+            $toolTip = 'Save';
+
+            if ($this->showDebugInfo) {
+                $toolTip .= PHP_EOL . "table = '" . $this->formSpec['tableName'] . "'" . PHP_EOL . "r = '" . $recordId . "'";
+            }
+
+            $buttonSave = $this->buildButtonCode('save-button', $toolTip, 'glyphicon-ok');
+        }
+
+        // Button: Close
+        if (Support::findInSet(FORM_BUTTON_CLOSE, $this->formSpec['showButton'])) {
+            $toolTip = 'Close';
+
+            if ($this->showDebugInfo) {
+                $toolTip .= PHP_EOL . "table = '" . $this->formSpec['tableName'] . "'" . PHP_EOL . "r = '" . $recordId . "'";
+            }
+
+            $buttonClose = $this->buildButtonCode('close-button', 'Close', 'glyphicon-remove');
         }
 
         // Button: Delete
         if (Support::findInSet(FORM_BUTTON_DELETE, $this->formSpec['showButton'])) {
+            $toolTip = 'Delete';
+
             if ($this->showDebugInfo && $recordId > 0) {
-                $toolTipDelete .= PHP_EOL . "table = '" . $this->formSpec['tableName'] . "'" . PHP_EOL . "r = '" . $recordId . "'";
+                $toolTip .= PHP_EOL . "table = '" . $this->formSpec['tableName'] . "'" . PHP_EOL . "r = '" . $recordId . "'";
             }
-            $buttonDeleteDisabled = ($recordId > 0) ? '' : 'disabled';
+            $disabled = ($recordId > 0) ? '' : 'disabled';
 
-            $buttonDelete = <<<BUTTON
-                <div class="btn-group" role="group">
-                            <button id="delete-button" type="button" class="btn btn-default navbar-btn $buttonDeleteDisabled" title="$toolTipDelete"><span class="glyphicon glyphicon-trash"></span></button>
-                </div>
-BUTTON;
+            $buttonDelete = $this->buildButtonCode('delete-button', $toolTip, 'glyphicon-trash', $disabled);
         }
 
         // Button: New
         if (Support::findInSet(FORM_BUTTON_NEW, $this->formSpec['showButton'])) {
-            $formNewUrl = $this->deriveNewRecordUrlFromExistingSip($toolTipNew);
+            $toolTip = 'New';
+            $url = $this->deriveNewRecordUrlFromExistingSip($toolTip);
 
-            $buttonNew = <<<BUTTON
-                <div class="btn-group" role="group">
-                            <a href="$formNewUrl" id="form-new-button" class="btn btn-default navbar-btn" title="$toolTipNew"><span class="glyphicon glyphicon-plus"></span></a>
-                </div>
-BUTTON;
+            $buttonNew = $this->buildButtonAnchor('form-new-button', $url, $toolTip, 'glyphicon-plus');
         }
 
-//        <div class="btn-toolbar pull-right" role="toolbar">
-        $html = <<<BUTTON
-        <div class="btn-toolbar" role="toolbar">
-            $buttonDebug
-            <div class="btn-group" role="group">
-                <button id="save-button" type="button" class="btn btn-default navbar-btn" title="Save"><span class="glyphicon glyphicon-ok"></span></button>
-                <button id="close-button" type="button" class="btn btn-default navbar-btn" title="Close"><span class="glyphicon glyphicon-remove"></span></button>
-            </div>
-            $buttonDelete
-            $buttonNew
-        </div>
-BUTTON;
+        $html = Support::wrapTag('<div class="btn-group" role="group">', $buttonEditForm);
+        $html .= Support::wrapTag('<div class="btn-group" role="group">', $buttonSave . $buttonClose);
+        $html .= Support::wrapTag('<div class="btn-group" role="group">', $buttonDelete);
+        $html .= Support::wrapTag('<div class="btn-group" role="group">', $buttonNew);
+
+        $html = Support::wrapTag('<div class="btn-toolbar" role="toolbar">', $html);
+
         return $html;
     }
 
+    /**
+     * @param $id
+     * @param $url
+     * @param $title
+     * @param $icon
+     * @param string $disabled
+     * @return string
+     */
+    private function buildButtonAnchor($id, $url, $title, $icon, $disabled = '') {
+        return "<a href='$url' id='$id' class='btn btn-default navbar-btn $disabled' " . Support::doAttribute('title', $title) . "><span class='glyphicon $icon'></span></a>";
+    }
+
+    /**
+     * @param $id
+     * @param $title
+     * @param $icon
+     * @param string $disabled
+     * @return string
+     */
+    private function buildButtonCode($id, $title, $icon, $disabled = '') {
+        return "<button id='$id' type='button' class='btn btn-default navbar-btn $disabled' " . Support::doAttribute('title', $title) . "><span class='glyphicon $icon'></span></button>";
+    }
+
     /**
      * @param $pillArray
      * @return string
diff --git a/extension/qfq/qfq/Constants.php b/extension/qfq/qfq/Constants.php
index 7abfcf1e2..cd76e178b 100644
--- a/extension/qfq/qfq/Constants.php
+++ b/extension/qfq/qfq/Constants.php
@@ -26,6 +26,8 @@ const FORM_PERMISSION_ALWAYS = 'always';
 const FORM_PERMISSION_NEVER = 'never';
 const FORM_BUTTON_NEW = 'new';
 const FORM_BUTTON_DELETE = 'delete';
+const FORM_BUTTON_CLOSE = 'close';
+const FORM_BUTTON_SAVE = 'save';
 
 const FORM_FORWARD_MODE_NO = 'no';
 const FORM_FORWARD_MODE_AUTO = 'auto';
diff --git a/extension/qfq/sql/formEditor.sql b/extension/qfq/sql/formEditor.sql
index 32285d41f..a3e78d7a6 100644
--- a/extension/qfq/sql/formEditor.sql
+++ b/extension/qfq/sql/formEditor.sql
@@ -10,7 +10,7 @@ CREATE TABLE IF NOT EXISTS `Form` (
   `permitNew`                ENUM('sip', 'logged_in', 'logged_out', 'always', 'never') NOT NULL  DEFAULT 'sip',
   `permitEdit`               ENUM('sip', 'logged_in', 'logged_out', 'always', 'never') NOT NULL  DEFAULT 'sip',
   `render`                   ENUM('plain', 'table', 'bootstrap')                       NOT NULL  DEFAULT 'plain',
-  `showButton` SET('new', 'delete') NOT NULL  DEFAULT 'new,delete',
+  `showButton` SET('new', 'delete', 'close', 'save') NOT NULL  DEFAULT 'new,delete,close,save',
   `multiMode`                ENUM('none', 'horizontal', 'vertical')                    NOT NULL  DEFAULT 'none',
   `multiSql`                 TEXT                                                      NOT NULL,
   `multiDetailForm`          VARCHAR(255)                                              NOT NULL  DEFAULT '',
@@ -120,7 +120,7 @@ CREATE TABLE IF NOT EXISTS `FormElement` (
 # FormEditor: Form
 INSERT INTO Form (name, title, noteInternal, tableName, permitNew, permitEdit, render, multiSql, parameter) VALUES
   ('form', 'Form Editor: {{SELECT id, " / ", name FROM Form WHERE id = {{r:S0}}}}', 'Please secure the form',
-   'Form', 'always', 'always', 'bootstrap', '', 'maxVisiblePill=3\nclass=container-fluid');
+   'Form', 'always', 'always', 'bootstrap', '', 'maxVisiblePill=5\nclass=container-fluid');
 
 # FormEditor: FormElements
 INSERT INTO FormElement (formId, name, label, mode, type, checkType, class, ord, size, maxLength, note, clientJs, value, sql1, sql2, parameter, feIdContainer, subrecordOption)
-- 
GitLab