BuildFormBootstrap.php 34 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
87
88
    /**
     * @param $label
     * @param $input
     * @param $note
     */
    public function fillWrapLabelInputNote($label, $input, $note) {
89
        $this->wrap[WRAP_SETUP_LABEL][WRAP_SETUP_START] = "<div class='col-md-$label qfq-label'>";
90
91
92
        $this->wrap[WRAP_SETUP_LABEL][WRAP_SETUP_END] = "</div>";
        $this->wrap[WRAP_SETUP_INPUT][WRAP_SETUP_START] = "<div class='col-md-$input'>";
        $this->wrap[WRAP_SETUP_INPUT][WRAP_SETUP_END] = "</div>";
Carsten  Rose's avatar
Carsten Rose committed
93
        $this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_START] = "<div class='col-md-$note qfq-note'>";
94
95
96
97
        $this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_END] = "</div>";

    }

Carsten  Rose's avatar
Carsten Rose committed
98
99
100
    /**
     * @return string
     */
101
102
103
104
    public function getProcessFilter() {
        return FORM_ELEMENTS_NATIVE_SUBRECORD;
    }

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

    /**
113
     * @param string $mode
114
     * @return string
115
116
117
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
118
     * @throws UserReportException
119
     */
120
    public function head($mode = FORM_LOAD) {
121
        $html = '';
122
        $title = '';
123

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

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

128
        // Show title / frame only if there is a title given.
129
        if (trim($this->formSpec[F_TITLE]) != '') {
130
            $classTitle = isset($this->formSpec[F_CLASS_TITLE]) ? $this->formSpec[F_CLASS_TITLE] : "qfq-form-title";
131
            $title = Support::wrapTag('<div class="row"><div class="col-md-12">', Support::wrapTag('<div class="' . $classTitle . '">', $this->formSpec[F_TITLE]));
132
        }
133
134

        $html .= $button . $title;
135

136
        $dummy = array();
137
        $pill = $this->buildPillNavigation($mode, OnArray::filter($this->feSpecNative, FE_TYPE, FE_TYPE_PILL), $dummy);
138
        $html .= Support::wrapTag('<div class="row">', $pill);
139

140
141
        $html .= $this->getFormTag();

142
143
144
145
146
        $class = ['tab-content', $this->formSpec[F_CLASS_BODY]];
        if ($pill == '') {
            $class[] = 'col-md-12';
        }
        $html .= "<div " . Support::doAttribute('class', $class) . ">";
147
148
149
150

        return $html;
    }

151
152
153
154
155
156
    /**
     * 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
157
        // EditFormElement Icons
158
159
        $js = '$(".' . CLASS_FORM_ELEMENT_EDIT . '").toggleClass("hidden")';
        $element = "<input type='checkbox' onchange='" . $js . "'>" .
160
            Support::wrapTag("<span title='Toggle: Edit form element icons' class='" . GLYPH_ICON . ' ' . GLYPH_ICON_TASKS . "'>", '');
161
        $element = Support::wrapTag('<label class="btn btn-default navbar-btn">', $element);
Carsten  Rose's avatar
Carsten Rose committed
162

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

Carsten  Rose's avatar
Carsten Rose committed
166
167
168
169
    /**
     * Creates a button to open 'CopyForm' with the current form as source.
     *
     * @return string - the rendered button
170
171
     * @throws CodeException
     * @throws UserFormException
Carsten  Rose's avatar
Carsten Rose committed
172
173
174
175
176
177
178
179
180
181
182
     */
    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
183
            'id' => $this->store->getVar(SYSTEM_EDIT_FORM_PAGE, STORE_SYSTEM),
Carsten  Rose's avatar
Carsten Rose committed
184
            'form' => 'copyForm',
Carsten  Rose's avatar
Carsten Rose committed
185
            'r' => 0,
Carsten  Rose's avatar
Carsten Rose committed
186
187
188
189
190
191
192
193
194
195
196
197
            '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');
    }

198
199
200
201
202
203
204
205
206
    /**
     * 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() {
207
208
209
210
211
212
213
214
215
216
217
218
219
220

        $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;
221
222
    }

Carsten  Rose's avatar
Carsten Rose committed
223
224
225
226
    /**
     * Creates a link to open current form loaded in FormEditor
     *
     * @return string - the rendered Checkbox
227
228
229
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
Carsten  Rose's avatar
Carsten Rose committed
230
231
232
     */
    private function buildViewForm() {

233
234
235
236
        $form = false;
        $url = '';
        $status = '';

Carsten  Rose's avatar
Carsten Rose committed
237
238
239
240
241
        switch ($this->formSpec[F_NAME]) {
            case 'form':
                $form = $this->store->getVar(F_NAME, STORE_RECORD);
                break;
            case 'formElement':
242
                if (false !== ($formId = $this->store->getVar(FE_FORM_ID, STORE_SIP . STORE_RECORD))) {
243
                    $row = $this->dbArray[$this->dbIndexQfq]->sql("SELECT f.name FROM Form AS f WHERE id=" . $formId, ROW_EXPECT_1);
244
245
                    $form = current($row);
                }
Carsten  Rose's avatar
Carsten Rose committed
246
247
248
249
250
251
                break;
            default:
                return '';
        }

        if ($form === false) {
252
253
254
255
256
257
258
            $toolTip = "Form not 'form' or 'formElement'";
            $status = 'disabled';
        } else {

            $queryStringArray = [
                'id' => $this->store->getVar(SYSTEM_EDIT_FORM_PAGE, STORE_SYSTEM),
                'form' => $form,
Carsten  Rose's avatar
Carsten Rose committed
259
                'r' => 0,
260
261
262
263
264
265
            ];
            $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
266
267
        }

268
        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
269
270
    }

271
    /**
272
     * Build Buttons panel on top right corner of form.
273
     * Simulate Submit Button: http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml
274
275
     *
     * @return string
276
     * @throws CodeException
Carsten  Rose's avatar
Carsten Rose committed
277
     * @throws DbException
278
     * @throws UserFormException
279
     * @throws UserReportException
280
281
282
     */
    private function buildButtons() {
        $buttonNew = '';
283
284
285
        $buttonDelete = '';
        $buttonClose = '';
        $buttonSave = '';
286
        $buttonDebugForm = '';
Carsten  Rose's avatar
Carsten Rose committed
287
288
289
        $recordId = $this->store->getVar(SIP_RECORD_ID, STORE_SIP);

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

294
295
            $buttonDebugForm = $this->buildLogForm() .
                $this->buildViewForm() .
Carsten  Rose's avatar
Carsten Rose committed
296
                $this->buildShowEditFormElementCheckbox() .
Carsten  Rose's avatar
Carsten Rose committed
297
                $this->buildButtonCopyForm() .
298
                $this->buildButtonAnchor($url, 'form-edit-button', '', $toolTip, GLYPH_ICON_TOOL, '', 'btn btn-default navbar-btn');
299
300
301
        }

        // Button: Save
Carsten  Rose's avatar
Carsten Rose committed
302
        if (Support::findInSet(FORM_BUTTON_SAVE, $this->formSpec[F_SHOW_BUTTON]) && $this->formSpec[F_SUBMIT_BUTTON_TEXT] === '') {
303
304
            $toolTip = $this->formSpec[F_SAVE_BUTTON_TOOLTIP];
            // In debugMode every button link should show the information behind the SIP.
305
            if ($this->showDebugInfoFlag) {
306
                $toolTip .= PHP_EOL . "table = '" . $this->formSpec[F_TABLE_NAME] . "'" . PHP_EOL . "r = '" . $recordId . "'";
307
308
            }

309
310
            $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]);
311
312
313
        }

        // Button: Close
Carsten  Rose's avatar
Carsten Rose committed
314
        if (Support::findInSet(FORM_BUTTON_CLOSE, $this->formSpec[F_SHOW_BUTTON])) {
315
316
317
            $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
318
        }
319

Carsten  Rose's avatar
Carsten Rose committed
320
        // Button: Delete
Carsten  Rose's avatar
Carsten Rose committed
321
        if (Support::findInSet(FORM_BUTTON_DELETE, $this->formSpec[F_SHOW_BUTTON])) {
322
            $toolTip = $this->formSpec[F_DELETE_BUTTON_TOOLTIP];
323

324
            if ($this->showDebugInfoFlag && $recordId > 0) {
325
                $toolTip .= PHP_EOL . "form = '" . $this->formSpec[F_FINAL_DELETE_FORM] . "'" . PHP_EOL . "r = '" . $recordId . "'";
Carsten  Rose's avatar
Carsten Rose committed
326
            }
327
            $disabled = ($recordId > 0) ? '' : 'disabled';
Carsten  Rose's avatar
Carsten Rose committed
328

329
330
            $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]);
331
332
        }

Carsten  Rose's avatar
Carsten Rose committed
333
        // Button: New
Carsten  Rose's avatar
Carsten Rose committed
334
        if (Support::findInSet(FORM_BUTTON_NEW, $this->formSpec[F_SHOW_BUTTON])) {
335
            $url = $this->deriveNewRecordUrlFromExistingSip($toolTip);
Carsten  Rose's avatar
Carsten Rose committed
336

337
338
            $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]);
339
340
        }

341
342
        // Arrangement: Edit Form / Save / Close / Delete / New
        $html = '';
343
344
345
346
347

        $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);
348

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

351
352
353
        return $html;
    }

354
    /**
355
356
     * Generic function to create a button with a given $buttonHtmlId, $url, $title, $icon (=glyph), $disabled
     *
Carsten  Rose's avatar
Carsten Rose committed
357
     * @param string $url
358
359
360
     * @param string $buttonHtmlId
     * @param string $text
     * @param string $toolTip
Carsten  Rose's avatar
Carsten Rose committed
361
     * @param string $icon
362
     * @param string $disabled
363
     * @param string $class
Carsten  Rose's avatar
Carsten Rose committed
364
     *
365
     * @return string
366
     * @throws CodeException
367
     */
368
369
370
371
372
373
374
    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
375
376

        $attribute = Support::doAttribute('href', $url);
377
378
379
        $attribute .= Support::doAttribute('id', $buttonHtmlId);
        $attribute .= Support::doAttribute('class', "$class $disabled");
        $attribute .= Support::doAttribute('title', $toolTip);
Carsten  Rose's avatar
Carsten Rose committed
380

381
        return Support::wrapTag("<a $attribute>", $element);
382
383
384
    }

    /**
385
386
     * Creates a button with the given attributes. If there is no $icon given, render the button without glyph.
     *
387
388
389
     * @param string $buttonHtmlId
     * @param string $text
     * @param string $tooltip
390
     * @param string $icon
391
     * @param string $disabled
Carsten  Rose's avatar
Carsten Rose committed
392
     *
393
394
     * @param string $buttonOnChangeClass
     * @param string $class
395
     * @return string
396
     * @throws CodeException
397
     */
398
    private function buildButtonCode($buttonHtmlId, $text, $tooltip, $icon, $disabled = '', $buttonOnChangeClass = '', $class = '') {
399
400

        if ($icon === '') {
401
402
            $element = $text;
        } else {
403
            $element = "<span class='glyphicon $icon'></span>" . ' ' . $text;
404
405
        }

406
        $class = Support::doAttribute('class', $class);
407
        $dataClassOnChange = Support::doAttribute('data-class-on-change', $buttonOnChangeClass);
408
409
410
        $tooltip = Support::doAttribute('title', $tooltip);

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

Carsten  Rose's avatar
Carsten Rose committed
413
    /**
414
415
416
     * Builds the BS-pills on top of a form.
     *
     * @param string $mode FORM_LOAD | FORM_UPDATE | FORM_SAVE
417
     * @param array $pillArray
Carsten  Rose's avatar
Carsten Rose committed
418
     *
419
     * @param array $json
Carsten  Rose's avatar
Carsten Rose committed
420
     * @return string
421
     * @throws CodeException
422
     * @throws DbException
423
     * @throws UserFormException
424
     * @throws UserReportException
Carsten  Rose's avatar
Carsten Rose committed
425
     */
426
    private function buildPillNavigation($mode = FORM_LOAD, array $pillArray, array &$json) {
427
428
        $pillButton = '';
        $pillDropdown = '';
429
        $htmlDropdown = '';
430

431
        if ($pillArray == null) {
432
            return '';
433
        }
434
435
436

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

437
438
        $parameterLanguageFieldName = $this->store->getVar(SYSTEM_PARAMETER_LANGUAGE_FIELD_NAME, STORE_SYSTEM);

439
440
        // Iterate over all 'pill'
        $ii = 0;
441
        $isFirstPill = true;
442
        $recordId = $this->store->getVar(COLUMN_ID, STORE_RECORD . STORE_ZERO);
443
        foreach ($pillArray as $formElement) {
444

445
446
447
448
            if ($mode != FORM_LOAD && $formElement[FE_DYNAMIC_UPDATE] !== 'yes') {
                continue; // During save/update: Process only FE dynamic_update=yes
            }

449
450
            $htmlIdLi = $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_PILL_LI;
            $htmlIdLiA = $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_PILL_LI_A;
451
452
453
454

            $formElement = $this->evaluate->parseArray($formElement);
            HelperFormElement::explodeParameter($formElement, F_PARAMETER);
            $formElement = HelperFormElement::setLanguage($formElement, $parameterLanguageFieldName);
455
456
457
            if (!empty($formElement[FE_MODE_SQL])) {
                $formElement[FE_MODE] = $formElement[FE_MODE_SQL];
            }
458

459
460
            $ii++;

461
            if ($formElement[FE_NAME] === '' || $formElement[FE_LABEL] === '') {
462
                $this->store->setVar(SYSTEM_FORM_ELEMENT, Logger::formatFormElementName($formElement), STORE_SYSTEM);
463
                $this->store->setVar(SYSTEM_FORM_ELEMENT_COLUMN, 'name, label', STORE_SYSTEM);
464
                throw new UserFormException("Field 'name' and/or 'label' are empty", ERROR_NAME_LABEL_EMPTY);
465
466
            }

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

470
            $attributeLiA = 'data-toggle="tab" ';
471
472
            $hrefTarget = '#' . $this->createAnker($formElement[FE_ID]);

473
            $htmlFormElementName = HelperFormElement::buildFormElementName($formElement, $recordId);
474
475
476
477
            switch ($formElement[FE_MODE]) {
                case FE_MODE_SHOW:
                case FE_MODE_REQUIRED:
                    $attributeLi = '';
478
479
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLi][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = '';
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLiA][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = '';
480
                    break;
481

482
                case FE_MODE_READONLY:
483
484
                    $hrefTarget = '#';

485
                    $attributeLi = Support::doAttribute('class', 'disabled');
486
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLi][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = 'disabled';
487
488

                    $attributeLiA .= Support::doAttribute('class', 'noclick');
489
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLiA][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = 'noclick';
490
                    break;
491

492
                case FE_MODE_HIDDEN:
493
494
//                    $attributeLi = Support::doAttribute('style', 'display: none');
                    $attributeLi = Support::doAttribute('class', 'hidden');
495
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLi][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = 'hidden';
496
                    break;
497

498
499
500
                default:
                    throw new UserFormException("Unknown Mode: " . $formElement[FE_MODE], ERROR_UNKNOWN_MODE);
            }
501

502
            $attributeLi .= Support::doAttribute(HTML_ATTR_ID, $htmlIdLi);
Elias Villiger's avatar
Elias Villiger committed
503
            $attributeLi .= Support::doAttribute( 'title', $formElement[FE_TOOLTIP]);
504
            $attributeLiA .= Support::doAttribute(HTML_ATTR_ID, $htmlIdLiA);
505
            $a = Support::wrapTag("<a $attributeLiA" . Support::doAttribute('href', $hrefTarget) . ">", $formElement[FE_LABEL]);
506
            $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLiA][API_ELEMENT_CONTENT] = $formElement[FE_LABEL];
507

508
            if ($isFirstPill && $formElement[FE_MODE] != FE_MODE_HIDDEN) {
509
                $attributeLi .= 'class="active" ';
510
511
512
                $isFirstPill = false;
            }

513
            if ($ii <= $maxVisiblePill) {
514
                $pillButton .= '<li role="presentation"' . $attributeLi . ">" . $a . "</li>";
515
            } else {
516
                $pillDropdown .= '<li ' . $attributeLi . '>' . $a . "</li>";
517
518
            }
        }
519

520
521
        // Pill Dropdown necessary?
        if ($ii > $maxVisiblePill) {
522
            $htmlDropdown = Support::wrapTag('<ul class="dropdown-menu qfq-form-pill ' . $this->formSpec[F_CLASS_PILL] . '">', $pillDropdown, true);
523
            $htmlDropdown = '<a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button">more <span class="caret"></span></a>' . $htmlDropdown;
524
            $htmlDropdown = Support::wrapTag('<li role="presentation" class="dropdown">', $htmlDropdown, false);
525
        }
526

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

530
        return $htmlDropdown;
531
532
533
534
535
536
    }

    /**
     * Create an identifier for the pill navigation menu
     *
     * @param $id
Carsten  Rose's avatar
Carsten Rose committed
537
     *
538
539
540
     * @return string
     */
    private function createAnker($id) {
541
        return $this->formSpec[FE_NAME] . '_' . $id;
542
543
    }

Carsten  Rose's avatar
Carsten Rose committed
544
545
546
547
548
549
550
    /**
     * @return string
     */
    private function getTabId() {
        return 'qfqTabs';
    }

551
552
553
554
    /**
     * Builds the complete HTML '<form ...>'-tag
     *
     * @return string
555
556
     * @throws CodeException
     * @throws DbException
557
     * @throws UserFormException
558
     */
559
    public function getFormTag() {
560

561
        $attribute = $this->getFormTagAttributes();
562
563

        $attribute['class'] = 'form-horizontal';
564
        $attribute['data-toggle'] = 'validator';
565
        if (isset($this->formSpec[F_SAVE_BUTTON_ACTIVE]) && $this->formSpec[F_SAVE_BUTTON_ACTIVE] != '0') {
566
567
            $attribute[DATA_ENABLE_SAVE_BUTTON] = 'true';
        }
568
569

        $honeypot = $this->getHoneypotVars();
570
        $md5 = $this->buildInputRecordHashMd5();
571

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

575
576
    /**
     * @return string
577
578
     * @throws CodeException
     * @throws UserFormException
579
     * @throws UserReportException
580
     */
581
    public function tail() {
582

583
        $html = '';
Carsten  Rose's avatar
Carsten Rose committed
584
        $deleteUrl = '';
585

586
587
        $formId = $this->getFormId();

588
589
        // Button Save at bottom of form - only if there is a button text given.
        if ($this->formSpec[F_SUBMIT_BUTTON_TEXT] !== '') {
590
591
592
593

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

594
595
            $buttonText = $this->formSpec[F_SUBMIT_BUTTON_TEXT];

596
597
598
            $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]);
599
600
601
602
603
604
605
606

            $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);
        }

607
        $html .= '</div> <!--class="tab-content" -->';  //  <div class="tab-content">
608
//        $html .= '<input type="submit" value="Submit">';
609

Carsten  Rose's avatar
Carsten Rose committed
610
611
612
        $formId = $this->getFormId();
        $tabId = $this->getTabId();

Carsten  Rose's avatar
Carsten Rose committed
613
        if (0 < ($recordId = $this->store->getVar(SIP_RECORD_ID, STORE_SIP))) {
614
            $deleteUrl = $this->createDeleteUrl($this->formSpec[F_FINAL_DELETE_FORM], $recordId);
Carsten  Rose's avatar
Carsten Rose committed
615
        }
616

617
618
619
        $actionUpload = FILE_ACTION . '=' . FILE_ACTION_UPLOAD;
        $actionDelete = FILE_ACTION . '=' . FILE_ACTION_DELETE;

620
621
622
        $apiDir = API_DIR;
        $apiDeletePhp = API_DIR . '/' . API_DELETE_PHP;

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

625
        $html .= '</form>';  //  <form class="form-horizontal" ...
626
627
628
629
630
631
        $html .= <<<EOF
        <script type="text/javascript">
            $(function () {
                'use strict';
                QfqNS.Log.level = 0;

Carsten  Rose's avatar
Carsten Rose committed
632
633
634
                var qfqPage = new QfqNS.QfqPage({
                    tabsId: '$tabId',
                    formId: '$formId',
635
                    submitTo: '$apiDir/save.php',
Carsten  Rose's avatar
Carsten Rose committed
636
                    $dirtyAction
Carsten  Rose's avatar
Carsten Rose committed
637
                    deleteUrl: '$deleteUrl',
638
639
640
                    refreshUrl: '$apiDir/load.php',
                    fileUploadTo: '$apiDir/file.php?$actionUpload',
                    fileDeleteUrl: '$apiDir/file.php?$actionDelete'
Carsten  Rose's avatar
Carsten Rose committed
641
642
                });

643
               
644
                var qfqRecordList = new QfqNS.QfqRecordList('$apiDeletePhp');
Carsten  Rose's avatar
Carsten Rose committed
645
            })
646
647
         </script>
EOF;
648
        $html .= '</div>';  //  <div class="container-fluid"> === main <div class=...> around everything
649

650
651
652
653
654
        return $html;
    }

    /**
     * @param array $formElement
Carsten  Rose's avatar
Carsten Rose committed
655
656
657
     * @param       $htmlFormElementName
     * @param       $value
     *
658
     * @param array $json
659
     * @return mixed
660
661
     * @throws CodeException
     * @throws DbException
662
     * @throws DownloadException
663
     * @throws UserFormException
664
     * @throws UserReportException
665
666
667
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
668
     */
669
    public function buildPill(array $formElement, $htmlFormElementName, $value, array &$json) {
670
        $html = '';
671
672
673
674
        // save parent processed FE's
        $tmpStore = $this->feSpecNative;

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

Carsten  Rose's avatar
Carsten Rose committed
678
        $html = $this->elements($this->store->getVar(SIP_RECORD_ID, STORE_SIP), FORM_ELEMENTS_NATIVE_SUBRECORD, 0, $json);
679
680
681
682

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

683
684
685
        return $html;
    }

686
    /**
Carsten  Rose's avatar
Carsten Rose committed
687
     * @param array $formElement Complete FormElement, especially some FE_WRAP
688
     * @param string $htmlElement Content to wrap.
Carsten  Rose's avatar
Carsten Rose committed
689
690
     * @param        $htmlFormElementName
     *
691
     * @return string               Wrapped $htmlElement
692
693
     * @throws CodeException
     * @throws UserFormException
694
     */
695
    public function buildRowNative(array $formElement, $htmlElement, $htmlFormElementName) {
696
        $html = '';
697
        $htmlLabel = '';
698
699
        $classHideRow = '';
        $classHideElement = '';
700

701
        if ($formElement[FE_MODE] == FE_MODE_HIDDEN) {
702
            if ($formElement[FE_FLAG_ROW_OPEN_TAG] && $formElement[FE_FLAG_ROW_CLOSE_TAG]) {
703
704
705
706
707
708
                $classHideRow = 'hidden';
            } else {
                $classHideElement = 'hidden';
            }
        }

Carsten  Rose's avatar
Carsten Rose committed
709
        // Label
710
        if ($formElement[FE_BS_LABEL_COLUMNS] > 0) {
711
712
            $addClass = ($formElement[FE_MODE] == FE_MODE_REQUIRED) ? CSS_REQUIRED : '';
            $htmlLabel = $this->buildLabel($htmlFormElementName, $formElement[FE_LABEL], $addClass);
713
        }
714

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

Carsten  Rose's avatar
Carsten Rose committed
718
        // Input
719
        $html .= $this->customWrap($formElement, $htmlElement, FE_WRAP_INPUT, $formElement[FE_BS_INPUT_COLUMNS],
720
721
            [$this->wrap[WRAP_SETUP_INPUT][WRAP_SETUP_START], $this->wrap[WRAP_SETUP_INPUT][WRAP_SETUP_END]],
            $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_INPUT, $classHideElement);
722

Carsten  Rose's avatar
Carsten Rose committed
723
        // Note
Carsten  Rose's avatar
Carsten Rose committed
724
        $note = $formElement[FE_NOTE];
725
        $html .= $this->customWrap($formElement, $note, FE_WRAP_NOTE, $formElement[FE_BS_NOTE_COLUMNS],
726
            [$this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_START], $this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_END]], $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_NOTE);
727

728
        // Row
729
730
        $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] : '';
731
732

        $html = $this->customWrap($formElement, $html, FE_WRAP_ROW, -1, [$openTag, $closeTag], $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_ROW);
733
734
735
736

        return $html;
    }

737
    /**
738
739
740
741
742
743
     * Wrap content with $wrapArray or, if specified use $formElement[$wrapName]. Inject $htmlId in wrap.
     *
     * Result:
     * - if $bsColumns==0 and empty $formElement[$wrapName]: no wrap
     * - if $formElement[$wrapName] is given: wrap with that one. Else: wrap with $wrapArray
     * - if $htmlId is give, inject it in $wrap.
744
     *
Carsten  Rose's avatar
Carsten Rose committed
745
     * @param array $formElement Complete FormElement, especially some FE_WRAP
746
     * @param string $htmlElement Content to wrap.
Carsten  Rose's avatar
Carsten Rose committed
747
748
749
     * @param string $wrapName FE_WRAP_ROW, FE_WRAP_LABEL, FE_WRAP_INPUT, FE_WRAP_NOTE
     * @param int $bsColumns
     * @param array $wrapArray Systemwide Defaults: [ 'open wrap', 'close wrap' ]
750
751
     * @param string $htmlId
     * @param string $class
Carsten  Rose's avatar
Carsten Rose committed
752
     *
753
     * @return string Wrapped $htmlElement
754
     * @throws CodeException
755
756
     * @throws \qfq\UserFormException
     */
757
    private function customWrap(array $formElement, $htmlElement, $wrapName, $bsColumns, array $wrapArray, $htmlId = '', $class = '') {
758

759
        // If $bsColumns==0: do not wrap with default.
760
761
762
763
        if ($bsColumns == 0) {
            $wrapArray[0] = '';
            $wrapArray[1] = '';
        }
764

765
        // If there is a 'per FormElement'-wrap, take it.
766
767
768
769
770
771
772
773
        if (isset($formElement[$wrapName])) {
            $wrapArray = explode('|', $formElement[$wrapName], 2);
        }

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

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

779
780
781
        return $wrapArray[0] . $htmlElement . $wrapArray[1];
    }

782

783
784
785
    /**
     * @param $formElement
     * @param $elementHtml
Carsten  Rose's avatar
Carsten Rose committed
786
     *
787
788
     * @return string
     */
789
    public function buildRowPill(array $formElement, $elementHtml) {
790
791
        $html = '';

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

794
795
796
797
        if ($formElement[FE_MODE] == FE_MODE_HIDDEN) {
            $class = ' hidden';
        } else {
            $class = $this->isFirstPill ? 'active ' : '';
798
799
            $this->isFirstPill = false;
        }
800

801
        $html = Support::wrapTag('<div role="tabpanel" class="tab-pane ' . $class . '" id="' . $this->createAnker($formElement['id']) . '">', $html);
802
803
804
805
806


        return $html;
    }

807
    /**
808
809
     * Builds a fieldset
     *
Carsten  Rose's avatar
Carsten Rose committed
810
     * @param array $formElement
811
     * @param $elementHtml
Carsten  Rose's avatar
Carsten Rose committed
812
     * @return mixed
813
     */
814
    public function buildRowFieldset(array $formElement, $elementHtml) {
815
816
817
        $html = $elementHtml;

        return $html;
818
819
    }

820
821
822
823
824
    /**
     * Builds a templateGroup
     *
     * @param $formElement
     * @param $elementHtml
825
     * @return mixed
826
827
828
829
830
831
832
     */
    public function buildRowTemplateGroup(array $formElement, $elementHtml) {
        $html = $elementHtml;

        return $html;
    }

833
    /**
834
     * @param array $formElement
835
     * @param $elementHtml
Carsten  Rose's avatar
Carsten Rose committed
836
     *
837
     * @return string
838
     * @throws CodeException
839
     */
840
    public function buildRowSubrecord(array $formElement, $elementHtml) {
841

842
        $formElement[FE_LABEL] = Support::wrapTag("<label class='control-label'>", $formElement[FE_LABEL], true);
843
        $html = $this->wrapItem(WRAP_SETUP_ELEMENT, $this->wrapItem(WRAP_SETUP_SUBRECORD, $formElement[FE_LABEL]));