BuildFormBootstrap.php 34.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/25/16
 * Time: 10:00 PM
 */

namespace qfq;

use qfq;

13
14
15
16
17
require_once(__DIR__ . '/Constants.php');
require_once(__DIR__ . '/helper/OnArray.php');
require_once(__DIR__ . '/AbstractBuildForm.php');
require_once(__DIR__ . '/exceptions/UserFormException.php');
require_once(__DIR__ . '/report/Link.php');
18

Carsten  Rose's avatar
Carsten Rose committed
19
20
21
22
/**
 * Class BuildFormBootstrap
 * @package qfq
 */
23
24
class BuildFormBootstrap extends AbstractBuildForm {

25
26
27
28
29
30
    private $isFirstPill;

    /**
     * @param array $formSpec
     * @param array $feSpecAction
     * @param array $feSpecNative
31
     * @param array $db Array of 'Database' instances
32
33
     * @throws CodeException
     * @throws UserFormException
34
     * @throws UserReportException
35
     */
36
37
    public function __construct(array $formSpec, array $feSpecAction, array $feSpecNative, array $db) {
        parent::__construct($formSpec, $feSpecAction, $feSpecNative, $db);
38
39
40
        $this->isFirstPill = true;
    }

Carsten  Rose's avatar
Carsten Rose committed
41
42
43
    /**
     *
     */
44
    public function fillWrap() {
45
46
47
48

//        $this->wrap[WRAP_SETUP_OUTER][WRAP_SETUP_START] = '<div class="tab-content">';
//        $this->wrap[WRAP_SETUP_OUTER][WRAP_SETUP_END] = '</div>';

49
        $this->wrap[WRAP_SETUP_TITLE][WRAP_SETUP_START] = "<div class='row'><div class='col-md-12'><h1>";
50
        $this->wrap[WRAP_SETUP_TITLE][WRAP_SETUP_END] = "</h1></div></div>";
51

52
        // Element: Label + Input + Note
53
        $this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_CLASS] = "form-group clearfix";
54
        $this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_START] = "<div class='" . $this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_CLASS] . "'>";
55
        $this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_END] = "</div>";
56

57
58
        $this->wrap[WRAP_SETUP_SUBRECORD][WRAP_SETUP_START] = "<div class='col-md-12'>";
        $this->wrap[WRAP_SETUP_SUBRECORD][WRAP_SETUP_END] = "</div>";
59

60
61
        $this->wrap[WRAP_SETUP_IN_FIELDSET][WRAP_SETUP_START] = "";
        $this->wrap[WRAP_SETUP_IN_FIELDSET][WRAP_SETUP_END] = "";
62

63
64
65
        $this->wrap[WRAP_SETUP_IN_TEMPLATE_GROUP][WRAP_SETUP_START] = "";
        $this->wrap[WRAP_SETUP_IN_TEMPLATE_GROUP][WRAP_SETUP_END] = "";

66
67
//        $this->feDivClass['radio'] = 'radio';
//        $this->feDivClass['checkbox'] = 'checkbox';
68
69
    }

70
71
    /**
     * @param string $addClass
Carsten  Rose's avatar
Carsten Rose committed
72
     *
73
     * @return string
74
     * @throws CodeException
75
76
77
78
79
80
81
     */
    public function getRowOpenTag($addClass = '') {
        $class = Support::doAttribute('class', [$this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_CLASS], $addClass]);

        return "<div $class>";

    }
82

83
    /**
84
85
86
     * Fill the BS wrapper for Label/Input/Note.
     * For legacy reasons, $label/$input/$note might be a number (0-12) or the bs column classes ('col-md-12 col-lg9')
     *
87
88
89
90
91
     * @param $label
     * @param $input
     * @param $note
     */
    public function fillWrapLabelInputNote($label, $input, $note) {
92
93
94

        $label = is_numeric($label) ? ('col-md-' . $label) : $label;
        $this->wrap[WRAP_SETUP_LABEL][WRAP_SETUP_START] = "<div class='$label qfq-label'>";
95
        $this->wrap[WRAP_SETUP_LABEL][WRAP_SETUP_END] = "</div>";
96
97
98

        $input = is_numeric($input) ? ('col-md-' . $input) : $input;
        $this->wrap[WRAP_SETUP_INPUT][WRAP_SETUP_START] = "<div class='$input'>";
99
        $this->wrap[WRAP_SETUP_INPUT][WRAP_SETUP_END] = "</div>";
100
101
102

        $note = is_numeric($note) ? ('col-md-' . $note) : $note;
        $this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_START] = "<div class='$note qfq-note'>";
103
104
105
106
        $this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_END] = "</div>";

    }

Carsten  Rose's avatar
Carsten Rose committed
107
108
109
    /**
     * @return string
     */
110
111
112
113
    public function getProcessFilter() {
        return FORM_ELEMENTS_NATIVE_SUBRECORD;
    }

Carsten  Rose's avatar
Carsten Rose committed
114
115
116
    /**
     * @return string
     */
117
118
119
120
121
    public function doSubrecords() {
        return '';
    }

    /**
122
     * @param string $mode
123
     * @return string
124
125
126
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
127
     * @throws UserReportException
128
     */
129
    public function head($mode = FORM_LOAD) {
130
        $html = '';
131
        $title = '';
132

Carsten  Rose's avatar
Carsten Rose committed
133
        $html .= '<div ' . Support::doAttribute('class', $this->formSpec[F_CLASS], true) . '>'; // main <div class=...> around everything, Whole FORM; class="container" or class="container-fluid"
134

135
136
        $button = Support::wrapTag('<div class="row"><div class="col-md-12">', $this->buildButtons());

137
        // Show title / frame only if there is a title given.
138
        if (trim($this->formSpec[F_TITLE]) != '') {
139
            $classTitle = isset($this->formSpec[F_CLASS_TITLE]) ? $this->formSpec[F_CLASS_TITLE] : "qfq-form-title";
140
            $title = Support::wrapTag('<div class="row"><div class="col-md-12">', Support::wrapTag('<div class="' . $classTitle . '">', $this->formSpec[F_TITLE]));
141
        }
142
143

        $html .= $button . $title;
144

145
        $dummy = array();
146
        $pill = $this->buildPillNavigation($mode, OnArray::filter($this->feSpecNative, FE_TYPE, FE_TYPE_PILL), $dummy);
147
        $html .= Support::wrapTag('<div class="row">', $pill);
148

149
150
        $html .= $this->getFormTag();

151
152
153
154
155
        $class = ['tab-content', $this->formSpec[F_CLASS_BODY]];
        if ($pill == '') {
            $class[] = 'col-md-12';
        }
        $html .= "<div " . Support::doAttribute('class', $class) . ">";
156
157
158
159

        return $html;
    }

160
161
162
163
164
165
    /**
     * Creates a Checkbox, which toggles 'hide'/'unhide' via JS, on all elements with class= CLASS_FORM_ELEMENT_EDIT.
     *
     * @return string - the rendered Checkbox
     */
    private function buildShowEditFormElementCheckbox() {
Carsten  Rose's avatar
Carsten Rose committed
166
        // EditFormElement Icons
167
168
        $js = '$(".' . CLASS_FORM_ELEMENT_EDIT . '").toggleClass("hidden")';
        $element = "<input type='checkbox' onchange='" . $js . "'>" .
169
            Support::wrapTag("<span title='Toggle: Edit form element icons' class='" . GLYPH_ICON . ' ' . GLYPH_ICON_TASKS . "'>", '');
170
        $element = Support::wrapTag('<label class="btn btn-default navbar-btn">', $element);
Carsten  Rose's avatar
Carsten Rose committed
171

172
173
174
        return Support::wrapTag('<div class="btn-group" data-toggle="buttons">', $element);
    }

Carsten  Rose's avatar
Carsten Rose committed
175
176
177
178
    /**
     * Creates a button to open 'CopyForm' with the current form as source.
     *
     * @return string - the rendered button
179
180
     * @throws CodeException
     * @throws UserFormException
Carsten  Rose's avatar
Carsten Rose committed
181
182
183
184
185
186
187
188
189
190
191
     */
    private function buildButtonCopyForm() {

        // Show copy icon only on form 'form' and only if there is a form loaded (id>0)
        if ($this->formSpec[COLUMN_ID] != 1) {
            return '';
        }
        // current loaded form.
        $formId = $this->store->getVar(COLUMN_ID, STORE_RECORD . STORE_ZERO);

        $queryStringArray = [
Carsten  Rose's avatar
Carsten Rose committed
192
            'id' => $this->store->getVar(SYSTEM_EDIT_FORM_PAGE, STORE_SYSTEM),
Carsten  Rose's avatar
Carsten Rose committed
193
            'form' => 'copyForm',
Carsten  Rose's avatar
Carsten Rose committed
194
            'r' => 0,
Carsten  Rose's avatar
Carsten Rose committed
195
196
197
198
199
200
201
202
203
204
205
206
            'idSrc' => $formId,
        ];
        $queryString = Support::arrayToQueryString($queryStringArray);
        $sip = $this->store->getSipInstance();
        $url = $sip->queryStringToSip($queryString);

        $toolTip = "Duplicate form" . PHP_EOL . PHP_EOL . OnArray::toString($queryStringArray, ' = ', PHP_EOL, "'");
        $status = ($formId == 0) ? 'disabled' : '';

        return $this->buildButtonAnchor($url, 'form-view-' . $this->formSpec[F_ID], '', $toolTip, GLYPH_ICON_DUPLICATE, $status, 'btn btn-default navbar-btn');
    }

207
208
209
210
211
212
213
214
215
    /**
     * Builds a button to open the formLog. The formLog appears on the same page as the form, but the form is not rendered and the log is shown.
     *
     * @return string
     * @throws CodeException
     * @throws UserFormException
     * @throws UserReportException
     */
    private function buildLogForm() {
216
217
218
219
220
221
222
223
224
225
226
227
228
229

        $pageAlias = $this->store::getVar(TYPO3_PAGE_ALIAS, STORE_TYPO3);

        $baseUrl = 'p:' . $pageAlias . '&form=' . $this->formSpec[F_NAME];
        $baseMisc = '|b|G:glyphicon-bell|s|c:btn btn-default navbar-btn';
        $baseTooltip = '|o:Set form in debugmode and show log: ';

        $stateAll = ($this->formSpec[FORM_LOG_FILE_ALL] == '') ? '' : ' btn-warning';
        $stateSession = ($this->formSpec[FORM_LOG_FILE_SESSION] == '') ? '' : ' btn-warning';

        $formLogAll = $this->link->renderLink($baseUrl . "&" . FORM_LOG_MODE . "=" . FORM_LOG_ALL . $baseMisc . $stateAll . $baseTooltip . 'All');
        $formLogSession = $this->link->renderLink($baseUrl . "&" . FORM_LOG_MODE . "=" . FORM_LOG_SESSION . $baseMisc . $stateSession . $baseTooltip . ' Per session');

        return $formLogAll . $formLogSession;
230
231
    }

Carsten  Rose's avatar
Carsten Rose committed
232
233
234
235
    /**
     * Creates a link to open current form loaded in FormEditor
     *
     * @return string - the rendered Checkbox
236
237
238
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
Carsten  Rose's avatar
Carsten Rose committed
239
240
241
     */
    private function buildViewForm() {

242
243
244
245
        $form = false;
        $url = '';
        $status = '';

Carsten  Rose's avatar
Carsten Rose committed
246
247
248
249
250
        switch ($this->formSpec[F_NAME]) {
            case 'form':
                $form = $this->store->getVar(F_NAME, STORE_RECORD);
                break;
            case 'formElement':
251
                if (false !== ($formId = $this->store->getVar(FE_FORM_ID, STORE_SIP . STORE_RECORD))) {
252
                    $row = $this->dbArray[$this->dbIndexQfq]->sql("SELECT f.name FROM Form AS f WHERE id=" . $formId, ROW_EXPECT_1);
253
254
                    $form = current($row);
                }
Carsten  Rose's avatar
Carsten Rose committed
255
256
257
258
259
260
                break;
            default:
                return '';
        }

        if ($form === false) {
261
262
263
            $toolTip = "Form not 'form' or 'formElement'";
            $status = 'disabled';
        } else {
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
            $requiredNew = $this->store->getVar(F_REQUIRED_PARAMETER_NEW, STORE_RECORD . STORE_EMPTY);
            if (trim($requiredNew) !== '') {
                $toolTip = "Form has 'required new' parameters and therefore cannot be previewed.";
                $status = 'disabled';
            } else {
                $queryStringArray = [
                    'id' => $this->store->getVar(SYSTEM_EDIT_FORM_PAGE, STORE_SYSTEM),
                    'form' => $form,
                    'r' => 0,
                ];
                $queryString = Support::arrayToQueryString($queryStringArray);
                $sip = $this->store->getSipInstance();
                $url = $sip->queryStringToSip($queryString);

                $toolTip = "View current form with r=0" . PHP_EOL . PHP_EOL . OnArray::toString($queryStringArray, ' = ', PHP_EOL, "'");
            }
Carsten  Rose's avatar
Carsten Rose committed
280
281
        }

282
        return $this->buildButtonAnchor($url, 'form-view-' . $this->formSpec[F_ID], '', $toolTip, GLYPH_ICON_VIEW, $status, 'btn btn-default navbar-btn');
Carsten  Rose's avatar
Carsten Rose committed
283
284
    }

285
    /**
286
     * Build Buttons panel on top right corner of form.
287
     * Simulate Submit Button: http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml
288
289
     *
     * @return string
290
     * @throws CodeException
Carsten  Rose's avatar
Carsten Rose committed
291
     * @throws DbException
292
     * @throws UserFormException
293
     * @throws UserReportException
294
295
296
     */
    private function buildButtons() {
        $buttonNew = '';
297
298
299
        $buttonDelete = '';
        $buttonClose = '';
        $buttonSave = '';
300
        $buttonDebugForm = '';
Carsten  Rose's avatar
Carsten Rose committed
301
302
303
        $recordId = $this->store->getVar(SIP_RECORD_ID, STORE_SIP);

        // Button: FormEdit
304
        if ($this->showDebugInfoFlag) {
305
            $toolTip = "Edit form" . PHP_EOL . PHP_EOL . OnArray::toString($this->store->getStore(STORE_SIP), ' = ', PHP_EOL, "'");
306
            $url = $this->createFormEditorUrl(FORM_NAME_FORM, $this->formSpec[F_ID]);
307

308
309
            $buttonDebugForm = $this->buildLogForm() .
                $this->buildViewForm() .
Carsten  Rose's avatar
Carsten Rose committed
310
                $this->buildShowEditFormElementCheckbox() .
Carsten  Rose's avatar
Carsten Rose committed
311
                $this->buildButtonCopyForm() .
312
                $this->buildButtonAnchor($url, 'form-edit-button', '', $toolTip, GLYPH_ICON_TOOL, '', 'btn btn-default navbar-btn');
313
314
315
        }

        // Button: Save
Carsten  Rose's avatar
Carsten Rose committed
316
        if (Support::findInSet(FORM_BUTTON_SAVE, $this->formSpec[F_SHOW_BUTTON]) && $this->formSpec[F_SUBMIT_BUTTON_TEXT] === '') {
317
318
            $toolTip = $this->formSpec[F_SAVE_BUTTON_TOOLTIP];
            // In debugMode every button link should show the information behind the SIP.
319
            if ($this->showDebugInfoFlag) {
320
                $toolTip .= PHP_EOL . "table = '" . $this->formSpec[F_TABLE_NAME] . "'" . PHP_EOL . "r = '" . $recordId . "'";
321
322
            }

323
324
            $buttonSave = $this->buildButtonCode('save-button', $this->formSpec[F_SAVE_BUTTON_TEXT], $toolTip,
                $this->formSpec[F_SAVE_BUTTON_GLYPH_ICON], '', $this->formSpec[F_BUTTON_ON_CHANGE_CLASS], $this->formSpec[F_SAVE_BUTTON_CLASS]);
325
326
327
        }

        // Button: Close
Carsten  Rose's avatar
Carsten Rose committed
328
        if (Support::findInSet(FORM_BUTTON_CLOSE, $this->formSpec[F_SHOW_BUTTON])) {
329
330
331
            $buttonClose = $this->buildButtonCode('close-button', $this->formSpec[F_CLOSE_BUTTON_TEXT],
                $this->formSpec[F_CLOSE_BUTTON_TOOLTIP],
                $this->formSpec[F_CLOSE_BUTTON_GLYPH_ICON], '', '', $this->formSpec[F_CLOSE_BUTTON_CLASS]);
Carsten  Rose's avatar
Carsten Rose committed
332
        }
333

Carsten  Rose's avatar
Carsten Rose committed
334
        // Button: Delete
Carsten  Rose's avatar
Carsten Rose committed
335
        if (Support::findInSet(FORM_BUTTON_DELETE, $this->formSpec[F_SHOW_BUTTON])) {
336
            $toolTip = $this->formSpec[F_DELETE_BUTTON_TOOLTIP];
337

338
            if ($this->showDebugInfoFlag && $recordId > 0) {
339
                $toolTip .= PHP_EOL . "form = '" . $this->formSpec[F_FINAL_DELETE_FORM] . "'" . PHP_EOL . "r = '" . $recordId . "'";
Carsten  Rose's avatar
Carsten Rose committed
340
            }
341
            $disabled = ($recordId > 0) ? '' : 'disabled';
Carsten  Rose's avatar
Carsten Rose committed
342

343
344
            $buttonDelete = $this->buildButtonCode('delete-button', $this->formSpec[F_DELETE_BUTTON_TEXT], $toolTip,
                $this->formSpec[F_DELETE_BUTTON_GLYPH_ICON], $disabled, '', $this->formSpec[F_DELETE_BUTTON_CLASS]);
345
346
        }

Carsten  Rose's avatar
Carsten Rose committed
347
        // Button: New
Carsten  Rose's avatar
Carsten Rose committed
348
        if (Support::findInSet(FORM_BUTTON_NEW, $this->formSpec[F_SHOW_BUTTON])) {
349
            $url = $this->deriveNewRecordUrlFromExistingSip($toolTip);
Carsten  Rose's avatar
Carsten Rose committed
350

351
352
            $buttonNew = $this->buildButtonAnchor($url, 'form-new-button', $this->formSpec[F_NEW_BUTTON_TEXT],
                $this->formSpec[F_NEW_BUTTON_TOOLTIP], $this->formSpec[F_NEW_BUTTON_GLYPH_ICON], '', $this->formSpec[F_NEW_BUTTON_CLASS]);
353
354
        }

355
356
        // Arrangement: Edit Form / Save / Close / Delete / New
        $html = '';
357
358
359
360
361

        $html .= Support::wrapTag('<div class="btn-group" role="group">', $buttonDebugForm, true);
        $html .= Support::wrapTag('<div class="btn-group" role="group">', $buttonSave . $buttonClose, true);
        $html .= Support::wrapTag('<div class="btn-group" role="group">', $buttonDelete, true);
        $html .= Support::wrapTag('<div class="btn-group" role="group">', $buttonNew, true);
362

363
        $html = Support::wrapTag('<div class="btn-toolbar pull-right" role="toolbar">', $html);
364

365
366
367
        return $html;
    }

368
    /**
369
370
     * Generic function to create a button with a given $buttonHtmlId, $url, $title, $icon (=glyph), $disabled
     *
Carsten  Rose's avatar
Carsten Rose committed
371
     * @param string $url
372
373
374
     * @param string $buttonHtmlId
     * @param string $text
     * @param string $toolTip
Carsten  Rose's avatar
Carsten Rose committed
375
     * @param string $icon
376
     * @param string $disabled
377
     * @param string $class
Carsten  Rose's avatar
Carsten Rose committed
378
     *
379
     * @return string
380
     * @throws CodeException
381
     */
382
383
384
385
386
387
388
    private function buildButtonAnchor($url, $buttonHtmlId, $text, $toolTip, $icon, $disabled = '', $class = '') {

        if ($icon === '') {
            $element = $text;
        } else {
            $element = Support::wrapTag("<span " . Support::doAttribute('class', "glyphicon $icon") . ">", $text);
        }
Carsten  Rose's avatar
Carsten Rose committed
389
390

        $attribute = Support::doAttribute('href', $url);
391
392
393
        $attribute .= Support::doAttribute('id', $buttonHtmlId);
        $attribute .= Support::doAttribute('class', "$class $disabled");
        $attribute .= Support::doAttribute('title', $toolTip);
Carsten  Rose's avatar
Carsten Rose committed
394

395
396
        // disabled links do not show tooltips -> use a span
        $wrapTag = $disabled == 'disabled' ? 'span' : 'a';
397
398

        return Support::wrapTag("<$wrapTag $attribute>", $element);
399
400
401
    }

    /**
402
403
     * Creates a button with the given attributes. If there is no $icon given, render the button without glyph.
     *
404
405
406
     * @param string $buttonHtmlId
     * @param string $text
     * @param string $tooltip
407
     * @param string $icon
408
     * @param string $disabled
Carsten  Rose's avatar
Carsten Rose committed
409
     *
410
411
     * @param string $buttonOnChangeClass
     * @param string $class
412
     * @return string
413
     * @throws CodeException
414
     */
415
    private function buildButtonCode($buttonHtmlId, $text, $tooltip, $icon, $disabled = '', $buttonOnChangeClass = '', $class = '') {
416
417

        if ($icon === '') {
418
419
            $element = $text;
        } else {
420
            $element = "<span class='glyphicon $icon'></span>" . ' ' . $text;
421
422
        }

423
        $class = Support::doAttribute('class', $class);
424
        $dataClassOnChange = Support::doAttribute('data-class-on-change', $buttonOnChangeClass);
425
426
427
        $tooltip = Support::doAttribute('title', $tooltip);

        return "<button id='$buttonHtmlId' type='button' $class $dataClassOnChange $tooltip $disabled>$element</button>";
428
429
    }

Carsten  Rose's avatar
Carsten Rose committed
430
    /**
431
432
433
     * Builds the BS-pills on top of a form.
     *
     * @param string $mode FORM_LOAD | FORM_UPDATE | FORM_SAVE
434
     * @param array $pillArray
Carsten  Rose's avatar
Carsten Rose committed
435
     *
436
     * @param array $json
Carsten  Rose's avatar
Carsten Rose committed
437
     * @return string
438
     * @throws CodeException
439
     * @throws DbException
440
     * @throws UserFormException
441
     * @throws UserReportException
Carsten  Rose's avatar
Carsten Rose committed
442
     */
443
    private function buildPillNavigation($mode = FORM_LOAD, array $pillArray, array &$json) {
444
445
        $pillButton = '';
        $pillDropdown = '';
446
        $htmlDropdown = '';
447

448
        if ($pillArray == null) {
449
            return '';
450
        }
451
452
453

        $maxVisiblePill = (isset($this->formSpec['maxVisiblePill']) && $this->formSpec['maxVisiblePill'] !== '') ? $this->formSpec['maxVisiblePill'] : 1000;

454
455
        $parameterLanguageFieldName = $this->store->getVar(SYSTEM_PARAMETER_LANGUAGE_FIELD_NAME, STORE_SYSTEM);

456
457
        // Iterate over all 'pill'
        $ii = 0;
458
        $isFirstPill = true;
459
        $recordId = $this->store->getVar(COLUMN_ID, STORE_RECORD . STORE_ZERO);
460
        foreach ($pillArray as $formElement) {
461

462
463
464
465
            if ($mode != FORM_LOAD && $formElement[FE_DYNAMIC_UPDATE] !== 'yes') {
                continue; // During save/update: Process only FE dynamic_update=yes
            }

466
467
            $htmlIdLi = $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_PILL_LI;
            $htmlIdLiA = $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_PILL_LI_A;
468
469
470
471

            $formElement = $this->evaluate->parseArray($formElement);
            HelperFormElement::explodeParameter($formElement, F_PARAMETER);
            $formElement = HelperFormElement::setLanguage($formElement, $parameterLanguageFieldName);
472
473
474
            if (!empty($formElement[FE_MODE_SQL])) {
                $formElement[FE_MODE] = $formElement[FE_MODE_SQL];
            }
475

476
477
            $ii++;

478
            if ($formElement[FE_NAME] === '' || $formElement[FE_LABEL] === '') {
479
                $this->store->setVar(SYSTEM_FORM_ELEMENT, Logger::formatFormElementName($formElement), STORE_SYSTEM);
480
                $this->store->setVar(SYSTEM_FORM_ELEMENT_COLUMN, 'name, label', STORE_SYSTEM);
481
                throw new UserFormException("Field 'name' and/or 'label' are empty", ERROR_NAME_LABEL_EMPTY);
482
483
            }

484
            // Anker for pill navigation
485
486
//            $a = '<a ' . Support::doAttribute('href', '#' . $this->createAnker($formElement[FE_ID])) . ' data-toggle="tab">' . $formElement[FE_LABEL] . '</a>';

487
            $attributeLiA = 'data-toggle="tab" ';
488
489
            $hrefTarget = '#' . $this->createAnker($formElement[FE_ID]);

490
            $htmlFormElementName = HelperFormElement::buildFormElementName($formElement, $recordId);
491
492
493
494
            switch ($formElement[FE_MODE]) {
                case FE_MODE_SHOW:
                case FE_MODE_REQUIRED:
                    $attributeLi = '';
495
496
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLi][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = '';
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLiA][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = '';
497
                    break;
498

499
                case FE_MODE_READONLY:
500
501
                    $hrefTarget = '#';

502
                    $attributeLi = Support::doAttribute('class', 'disabled');
503
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLi][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = 'disabled';
504
505

                    $attributeLiA .= Support::doAttribute('class', 'noclick');
506
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLiA][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = 'noclick';
507
                    break;
508

509
                case FE_MODE_HIDDEN:
510
511
//                    $attributeLi = Support::doAttribute('style', 'display: none');
                    $attributeLi = Support::doAttribute('class', 'hidden');
512
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLi][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = 'hidden';
513
                    break;
514

515
516
517
                default:
                    throw new UserFormException("Unknown Mode: " . $formElement[FE_MODE], ERROR_UNKNOWN_MODE);
            }
518

519
            $attributeLi .= Support::doAttribute(HTML_ATTR_ID, $htmlIdLi);
Elias Villiger's avatar
Elias Villiger committed
520
            $attributeLi .= Support::doAttribute( 'title', $formElement[FE_TOOLTIP]);
521
            $attributeLiA .= Support::doAttribute(HTML_ATTR_ID, $htmlIdLiA);
522
            $a = Support::wrapTag("<a $attributeLiA" . Support::doAttribute('href', $hrefTarget) . ">", $formElement[FE_LABEL]);
523
            $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLiA][API_ELEMENT_CONTENT] = $formElement[FE_LABEL];
524

525
            if ($isFirstPill && $formElement[FE_MODE] != FE_MODE_HIDDEN) {
526
                $attributeLi .= 'class="active" ';
527
528
529
                $isFirstPill = false;
            }

530
            if ($ii <= $maxVisiblePill) {
531
                $pillButton .= '<li role="presentation"' . $attributeLi . ">" . $a . "</li>";
532
            } else {
533
                $pillDropdown .= '<li ' . $attributeLi . '>' . $a . "</li>";
534
535
            }
        }
536

537
538
        // Pill Dropdown necessary?
        if ($ii > $maxVisiblePill) {
539
            $htmlDropdown = Support::wrapTag('<ul class="dropdown-menu qfq-form-pill ' . $this->formSpec[F_CLASS_PILL] . '">', $pillDropdown, true);
540
            $htmlDropdown = '<a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button">more <span class="caret"></span></a>' . $htmlDropdown;
541
            $htmlDropdown = Support::wrapTag('<li role="presentation" class="dropdown">', $htmlDropdown, false);
542
        }
543

544
        $htmlDropdown = Support::wrapTag('<ul id="' . $this->getTabId() . '" class="nav nav-pills qfq-form-pill ' . $this->formSpec[F_CLASS_PILL] . '" role="tablist">', $pillButton . $htmlDropdown);
545
        $htmlDropdown = Support::wrapTag('<div class="col-md-12">', $htmlDropdown);
546

547
        return $htmlDropdown;
548
549
550
551
552
553
    }

    /**
     * Create an identifier for the pill navigation menu
     *
     * @param $id
Carsten  Rose's avatar
Carsten Rose committed
554
     *
555
556
557
     * @return string
     */
    private function createAnker($id) {
558
        return $this->formSpec[FE_NAME] . '_' . $id;
559
560
    }

Carsten  Rose's avatar
Carsten Rose committed
561
562
563
564
565
566
567
    /**
     * @return string
     */
    private function getTabId() {
        return 'qfqTabs';
    }

568
569
570
571
    /**
     * Builds the complete HTML '<form ...>'-tag
     *
     * @return string
572
573
     * @throws CodeException
     * @throws DbException
574
     * @throws UserFormException
575
     */
576
    public function getFormTag() {
577

578
        $attribute = $this->getFormTagAttributes();
579
580

        $attribute['class'] = 'form-horizontal';
581
        $attribute['data-toggle'] = 'validator';
582
        if (isset($this->formSpec[F_SAVE_BUTTON_ACTIVE]) && $this->formSpec[F_SAVE_BUTTON_ACTIVE] != '0') {
583
584
            $attribute[DATA_ENABLE_SAVE_BUTTON] = 'true';
        }
585
586

        $honeypot = $this->getHoneypotVars();
587
        $md5 = $this->buildInputRecordHashMd5();
588

Carsten  Rose's avatar
Carsten Rose committed
589
        return '<form ' . OnArray::toString($attribute, '=', ' ', "'") . '>' . $honeypot . $md5;
590
591
    }

592
593
    /**
     * @return string
594
595
     * @throws CodeException
     * @throws UserFormException
596
     * @throws UserReportException
597
     */
598
    public function tail() {
599

600
        $html = '';
Carsten  Rose's avatar
Carsten Rose committed
601
        $deleteUrl = '';
602

603
604
        $formId = $this->getFormId();

605
606
        // Button Save at bottom of form - only if there is a button text given.
        if ($this->formSpec[F_SUBMIT_BUTTON_TEXT] !== '') {
607
608
609
610

            // Default setzen:
            $this->fillWrapLabelInputNote($this->formSpec[F_BS_LABEL_COLUMNS], $this->formSpec[F_BS_INPUT_COLUMNS], $this->formSpec[F_BS_NOTE_COLUMNS]);

611
612
            $buttonText = $this->formSpec[F_SUBMIT_BUTTON_TEXT];

613
614
615
            $htmlElement = $this->buildButtonCode('save-button', $buttonText, $this->formSpec[F_SUBMIT_BUTTON_TOOLTIP],
                $this->formSpec[F_SUBMIT_BUTTON_GLYPH_ICON], '', $this->formSpec[F_BUTTON_ON_CHANGE_CLASS],
                $this->formSpec[F_SUBMIT_BUTTON_CLASS]);
616
617
618
619
620
621
622
623

            $html .= $this->wrapItem(WRAP_SETUP_LABEL, '');
            $html .= $this->wrapItem(WRAP_SETUP_INPUT, $htmlElement);
            $html .= $this->wrapItem(WRAP_SETUP_NOTE, '');

            $html = $this->wrapItem(WRAP_SETUP_ELEMENT, $html);
        }

624
        $html .= '</div> <!--class="tab-content" -->';  //  <div class="tab-content">
625
//        $html .= '<input type="submit" value="Submit">';
626

Carsten  Rose's avatar
Carsten Rose committed
627
628
629
        $formId = $this->getFormId();
        $tabId = $this->getTabId();

Carsten  Rose's avatar
Carsten Rose committed
630
        if (0 < ($recordId = $this->store->getVar(SIP_RECORD_ID, STORE_SIP))) {
631
            $deleteUrl = $this->createDeleteUrl($this->formSpec[F_FINAL_DELETE_FORM], $recordId);
Carsten  Rose's avatar
Carsten Rose committed
632
        }
633

634
635
636
        $actionUpload = FILE_ACTION . '=' . FILE_ACTION_UPLOAD;
        $actionDelete = FILE_ACTION . '=' . FILE_ACTION_DELETE;

637
638
639
        $apiDir = API_DIR;
        $apiDeletePhp = API_DIR . '/' . API_DELETE_PHP;

640
        $dirtyAction = ($this->formSpec[F_DIRTY_MODE] == DIRTY_MODE_NONE) ? '' : "dirtyUrl: '$apiDir/dirty.php',";
Carsten  Rose's avatar
Carsten Rose committed
641

642
        $html .= '</form>';  //  <form class="form-horizontal" ...
643
644
645
646
647
648
        $html .= <<<EOF
        <script type="text/javascript">
            $(function () {
                'use strict';
                QfqNS.Log.level = 0;

Carsten  Rose's avatar
Carsten Rose committed
649
650
651
                var qfqPage = new QfqNS.QfqPage({
                    tabsId: '$tabId',
                    formId: '$formId',
652
                    submitTo: '$apiDir/save.php',
Carsten  Rose's avatar
Carsten Rose committed
653
                    $dirtyAction
Carsten  Rose's avatar
Carsten Rose committed
654
                    deleteUrl: '$deleteUrl',
655
656
657
                    refreshUrl: '$apiDir/load.php',
                    fileUploadTo: '$apiDir/file.php?$actionUpload',
                    fileDeleteUrl: '$apiDir/file.php?$actionDelete'
Carsten  Rose's avatar
Carsten Rose committed
658
659
                });

660
               
661
                var qfqRecordList = new QfqNS.QfqRecordList('$apiDeletePhp');
Carsten  Rose's avatar
Carsten Rose committed
662
            })
663
664
         </script>
EOF;
665
        $html .= '</div>';  //  <div class="container-fluid"> === main <div class=...> around everything
666

667
668
669
670
671
        return $html;
    }

    /**
     * @param array $formElement
Carsten  Rose's avatar
Carsten Rose committed
672
673
674
     * @param       $htmlFormElementName
     * @param       $value
     *
675
     * @param array $json
676
     * @return mixed
677
678
     * @throws CodeException
     * @throws DbException
679
     * @throws DownloadException
680
     * @throws UserFormException
681
     * @throws UserReportException
682
683
684
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
685
     */
686
    public function buildPill(array $formElement, $htmlFormElementName, $value, array &$json) {
687
        $html = '';
688
689
690
691
        // save parent processed FE's
        $tmpStore = $this->feSpecNative;

        // child FE's
692
        $this->feSpecNative = $this->dbArray[$this->dbIndexQfq]->getNativeFormElements(SQL_FORM_ELEMENT_SPECIFIC_CONTAINER,
693
694
            ['yes', $this->formSpec["id"], 'native,container', $formElement['id']], $this->formSpec);

Carsten  Rose's avatar
Carsten Rose committed
695
        $html = $this->elements($this->store->getVar(SIP_RECORD_ID, STORE_SIP), FORM_ELEMENTS_NATIVE_SUBRECORD, 0, $json);
696
697
698
699

        // restore parent processed FE's
        $this->feSpecNative = $tmpStore;

700
701
702
        return $html;
    }

703
    /**
Carsten  Rose's avatar
Carsten Rose committed
704
     * @param array $formElement Complete FormElement, especially some FE_WRAP
705
     * @param string $htmlElement Content to wrap.
Carsten  Rose's avatar
Carsten Rose committed
706
707
     * @param        $htmlFormElementName
     *
708
     * @return string               Wrapped $htmlElement
709
710
     * @throws CodeException
     * @throws UserFormException
711
     */
712
    public function buildRowNative(array $formElement, $htmlElement, $htmlFormElementName) {
713
        $html = '';
714
        $htmlLabel = '';
715
716
        $classHideRow = '';
        $classHideElement = '';
717

718
        if ($formElement[FE_MODE] == FE_MODE_HIDDEN) {
719
            if ($formElement[FE_FLAG_ROW_OPEN_TAG] && $formElement[FE_FLAG_ROW_CLOSE_TAG]) {
720
721
722
723
724
725
                $classHideRow = 'hidden';
            } else {
                $classHideElement = 'hidden';
            }
        }

Carsten  Rose's avatar
Carsten Rose committed
726
        // Label
727
        if ($formElement[FE_BS_LABEL_COLUMNS] != '0') {
728
729
            $addClass = ($formElement[FE_MODE] == FE_MODE_REQUIRED) ? CSS_REQUIRED : '';
            $htmlLabel = $this->buildLabel($htmlFormElementName, $formElement[FE_LABEL], $addClass);
730
        }
731

732
733
        $html .= $this->customWrap($formElement, $htmlLabel, FE_WRAP_LABEL, $formElement[FE_BS_LABEL_COLUMNS],
            [$this->wrap[WRAP_SETUP_LABEL][WRAP_SETUP_START], $this->wrap[WRAP_SETUP_LABEL][WRAP_SETUP_END]], $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_LABEL);
734

Carsten  Rose's avatar
Carsten Rose committed
735
        // Input
736
737
        $html .= $this->customWrap($formElement, $htmlElement, FE_WRAP_INPUT, $formElement[FE_BS_INPUT_COLUMNS],
            [$this->wrap[WRAP_SETUP_INPUT][WRAP_SETUP_START], $this->wrap[WRAP_SETUP_INPUT][WRAP_SETUP_END]],
738
            $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_INPUT, $classHideElement);
739

Carsten  Rose's avatar
Carsten Rose committed
740
        // Note
Carsten  Rose's avatar
Carsten Rose committed
741
        $note = $formElement[FE_NOTE];
742
743
        $html .= $this->customWrap($formElement, $note, FE_WRAP_NOTE, $formElement[FE_BS_NOTE_COLUMNS],
            [$this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_START], $this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_END]], $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_NOTE);
744

745
        // Row
746
747
748
749
        $openTag = $formElement[FE_FLAG_ROW_OPEN_TAG] ? $this->getRowOpenTag($classHideRow) : '';
        $closeTag = $formElement[FE_FLAG_ROW_CLOSE_TAG] ? $this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_END] : '';

        $html = $this->customWrap($formElement, $html, FE_WRAP_ROW, -1, [$openTag, $closeTag], $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_ROW);
750
751
752
753

        return $html;
    }

754
    /**
755
     * Wrap content with $wrapArray or, if specified use $formElement[$wrapName]. Inject $htmlId in wrap.
756
757
758
     *
     * Result:
     * - if $bsColumns==0 and empty $formElement[$wrapName]: no wrap
759
760
     * - if $formElement[$wrapName] is given: wrap with that one. Else: wrap with $wrapArray
     * - if $htmlId is give, inject it in $wrap.
761
     *
Carsten  Rose's avatar
Carsten Rose committed
762
     * @param array $formElement Complete FormElement, especially some FE_WRAP
763
     * @param string $htmlElement Content to wrap.
Carsten  Rose's avatar
Carsten Rose committed
764
765
     * @param string $wrapName FE_WRAP_ROW, FE_WRAP_LABEL, FE_WRAP_INPUT, FE_WRAP_NOTE
     * @param int $bsColumns
766
     * @param array $wrapArray Systemwide Defaults: [ 'open wrap', 'close wrap' ]
767
768
     * @param string $htmlId
     * @param string $class
Carsten  Rose's avatar
Carsten Rose committed
769
     *
770
     * @return string Wrapped $htmlElement
771
     * @throws CodeException
772
773
     * @throws \qfq\UserFormException
     */
774
    private function customWrap(array $formElement, $htmlElement, $wrapName, $bsColumns, array $wrapArray, $htmlId = '', $class = '') {
775

776
        // If $bsColumns==0: do not wrap with default.
777
        if ($bsColumns == '0') {
778
779
            $wrapArray[0] = '';
            $wrapArray[1] = '';
780
        }
781

782
        // If there is a 'per FormElement'-wrap, take it.
783
784
785
786
        if (isset($formElement[$wrapName])) {
            $wrapArray = explode('|', $formElement[$wrapName], 2);
        }

787
788
789
790
791
792
793
        if (count($wrapArray) != 2) {
            throw new UserFormException("Need open & close wrap token for FormElement.parameter" . $wrapName . " - E.g.: <div ...>|</div>", ERROR_MISSING_VALUE);
        }

        if ($wrapArray[0] != '') {
            $wrapArray[0] = Support::insertAttribute($wrapArray[0], 'id', $htmlId);
            $wrapArray[0] = Support::insertAttribute($wrapArray[0], 'class', $class); // might be problematic, if there is already a 'class' defined.
794
795
        }

796
        return $wrapArray[0] . $htmlElement . $wrapArray[1];
797
798
    }

799

800
801
802
    /**
     * @param $formElement
     * @param $elementHtml
Carsten  Rose's avatar
Carsten Rose committed
803
     *
804
805
     * @return string
     */
806
    public function buildRowPill(array $formElement, $elementHtml) {
807
808
        $html = '';

809
        $html .= Support::wrapTag('<div class="col-md-12 qfq-form-body ' . $this->formSpec[F_CLASS_BODY] . '">', $elementHtml);
810

811
812
813
814
        if ($formElement[FE_MODE] == FE_MODE_HIDDEN) {
            $class = ' hidden';
        } else {
            $class = $this->isFirstPill ? 'active ' : '';
815
816
            $this->isFirstPill = false;
        }
817

818
        $html = Support::wrapTag('<div role="tabpanel" class="tab-pane ' . $class . '" id="' . $this->createAnker($formElement['id']) . '">', $html);
819
820
821
822
823


        return $html;
    }

824
    /**
825
826
     * Builds a fieldset
     *
Carsten  Rose's avatar
Carsten Rose committed
827
     * @param array $formElement
828
     * @param $elementHtml
Carsten  Rose's avatar
Carsten Rose committed
829
     * @return mixed
830
     */
831
    public function buildRowFieldset(array $formElement, $elementHtml) {
832
833
834
        $html = $elementHtml;

        return $html;
835
836
    }

837
838
839
840
841
    /**
     * Builds a templateGroup
     *
     * @param $formElement
     * @param $elementHtml
842
     * @return mixed
843
844
845
846
847
848
849
     */
    public function buildRowTemplateGroup(array $formElement, $elementHtml) {
        $html = $elementHtml;

        return $html;
    }

850
    /**
851
     * @param array $formElement
852