BuildFormBootstrap.php 32.7 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
129
        // Show title / frame only if there is a title given.
        if ($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
207
208
209
210
211
    /**
     * 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() {
        $pageAlias=$this->store::getVar(TYPO3_PAGE_ALIAS, STORE_TYPO3);
        return $this->link->renderLink("p:" . $pageAlias . "&form=" . $this->formSpec[F_NAME] . "&" . FORM_LOG_MODE . "=" . FORM_LOG_SESSION .
            '|b|G:glyphicon-bell|s|o:Set form in debugmode and show log|c:btn btn-default navbar-btn');
    }

Carsten  Rose's avatar
Carsten Rose committed
212
213
214
215
    /**
     * Creates a link to open current form loaded in FormEditor
     *
     * @return string - the rendered Checkbox
216
217
218
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
Carsten  Rose's avatar
Carsten Rose committed
219
220
221
     */
    private function buildViewForm() {

222
223
224
225
        $form = false;
        $url = '';
        $status = '';

Carsten  Rose's avatar
Carsten Rose committed
226
227
228
229
230
        switch ($this->formSpec[F_NAME]) {
            case 'form':
                $form = $this->store->getVar(F_NAME, STORE_RECORD);
                break;
            case 'formElement':
231
                if (false !== ($formId = $this->store->getVar(FE_FORM_ID, STORE_SIP . STORE_RECORD))) {
232
                    $row = $this->dbArray[$this->dbIndexQfq]->sql("SELECT f.name FROM Form AS f WHERE id=" . $formId, ROW_EXPECT_1);
233
234
                    $form = current($row);
                }
Carsten  Rose's avatar
Carsten Rose committed
235
236
237
238
239
240
                break;
            default:
                return '';
        }

        if ($form === false) {
241
242
243
244
245
246
247
            $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
248
                'r' => 0,
249
250
251
252
253
254
            ];
            $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
255
256
        }

257
        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
258
259
    }

260
    /**
261
     * Build Buttons panel on top right corner of form.
262
     * Simulate Submit Button: http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml
263
264
     *
     * @return string
265
     * @throws CodeException
Carsten  Rose's avatar
Carsten Rose committed
266
     * @throws DbException
267
     * @throws UserFormException
268
     * @throws UserReportException
269
270
271
     */
    private function buildButtons() {
        $buttonNew = '';
272
273
274
        $buttonDelete = '';
        $buttonClose = '';
        $buttonSave = '';
275
        $buttonDebugForm = '';
Carsten  Rose's avatar
Carsten Rose committed
276
277
278
        $recordId = $this->store->getVar(SIP_RECORD_ID, STORE_SIP);

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

283
284
            $buttonDebugForm = $this->buildLogForm() .
                $this->buildViewForm() .
Carsten  Rose's avatar
Carsten Rose committed
285
                $this->buildShowEditFormElementCheckbox() .
Carsten  Rose's avatar
Carsten Rose committed
286
                $this->buildButtonCopyForm() .
287
                $this->buildButtonAnchor($url, 'form-edit-button', '', $toolTip, GLYPH_ICON_TOOL, '', 'btn btn-default navbar-btn');
288
289
290
        }

        // Button: Save
Carsten  Rose's avatar
Carsten Rose committed
291
        if (Support::findInSet(FORM_BUTTON_SAVE, $this->formSpec[F_SHOW_BUTTON]) && $this->formSpec[F_SUBMIT_BUTTON_TEXT] === '') {
292
293
            $toolTip = $this->formSpec[F_SAVE_BUTTON_TOOLTIP];
            // In debugMode every button link should show the information behind the SIP.
294
            if ($this->showDebugInfoFlag) {
295
                $toolTip .= PHP_EOL . "table = '" . $this->formSpec[F_TABLE_NAME] . "'" . PHP_EOL . "r = '" . $recordId . "'";
296
297
            }

298
299
            $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]);
300
301
302
        }

        // Button: Close
Carsten  Rose's avatar
Carsten Rose committed
303
        if (Support::findInSet(FORM_BUTTON_CLOSE, $this->formSpec[F_SHOW_BUTTON])) {
304
305
306
            $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
307
        }
308

Carsten  Rose's avatar
Carsten Rose committed
309
        // Button: Delete
Carsten  Rose's avatar
Carsten Rose committed
310
        if (Support::findInSet(FORM_BUTTON_DELETE, $this->formSpec[F_SHOW_BUTTON])) {
311
            $toolTip = $this->formSpec[F_DELETE_BUTTON_TOOLTIP];
312

313
            if ($this->showDebugInfoFlag && $recordId > 0) {
314
                $toolTip .= PHP_EOL . "form = '" . $this->formSpec[F_FINAL_DELETE_FORM] . "'" . PHP_EOL . "r = '" . $recordId . "'";
Carsten  Rose's avatar
Carsten Rose committed
315
            }
316
            $disabled = ($recordId > 0) ? '' : 'disabled';
Carsten  Rose's avatar
Carsten Rose committed
317

318
319
            $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]);
320
321
        }

Carsten  Rose's avatar
Carsten Rose committed
322
        // Button: New
Carsten  Rose's avatar
Carsten Rose committed
323
        if (Support::findInSet(FORM_BUTTON_NEW, $this->formSpec[F_SHOW_BUTTON])) {
324
            $url = $this->deriveNewRecordUrlFromExistingSip($toolTip);
Carsten  Rose's avatar
Carsten Rose committed
325

326
327
            $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]);
328
329
        }

330
331
        // Arrangement: Edit Form / Save / Close / Delete / New
        $html = '';
332
333
334
335
336

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

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

340
341
342
        return $html;
    }

343
    /**
344
345
     * Generic function to create a button with a given $buttonHtmlId, $url, $title, $icon (=glyph), $disabled
     *
Carsten  Rose's avatar
Carsten Rose committed
346
     * @param string $url
347
348
349
     * @param string $buttonHtmlId
     * @param string $text
     * @param string $toolTip
Carsten  Rose's avatar
Carsten Rose committed
350
     * @param string $icon
351
     * @param string $disabled
352
     * @param string $class
Carsten  Rose's avatar
Carsten Rose committed
353
     *
354
     * @return string
355
     * @throws CodeException
356
     */
357
358
359
360
361
362
363
    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
364
365

        $attribute = Support::doAttribute('href', $url);
366
367
368
        $attribute .= Support::doAttribute('id', $buttonHtmlId);
        $attribute .= Support::doAttribute('class', "$class $disabled");
        $attribute .= Support::doAttribute('title', $toolTip);
Carsten  Rose's avatar
Carsten Rose committed
369

370
        return Support::wrapTag("<a $attribute>", $element);
371
372
373
    }

    /**
374
375
     * Creates a button with the given attributes. If there is no $icon given, render the button without glyph.
     *
376
377
378
     * @param string $buttonHtmlId
     * @param string $text
     * @param string $tooltip
379
     * @param string $icon
380
     * @param string $disabled
Carsten  Rose's avatar
Carsten Rose committed
381
     *
382
383
     * @param string $buttonOnChangeClass
     * @param string $class
384
     * @return string
385
     * @throws CodeException
386
     */
387
    private function buildButtonCode($buttonHtmlId, $text, $tooltip, $icon, $disabled = '', $buttonOnChangeClass = '', $class = '') {
388
389

        if ($icon === '') {
390
391
            $element = $text;
        } else {
392
            $element = "<span class='glyphicon $icon'></span>" . ' ' . $text;
393
394
        }

395
        $class = Support::doAttribute('class', $class);
396
        $dataClassOnChange = Support::doAttribute('data-class-on-change', $buttonOnChangeClass);
397
398
399
        $tooltip = Support::doAttribute('title', $tooltip);

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

Carsten  Rose's avatar
Carsten Rose committed
402
    /**
403
404
405
     * Builds the BS-pills on top of a form.
     *
     * @param string $mode FORM_LOAD | FORM_UPDATE | FORM_SAVE
406
     * @param array $pillArray
Carsten  Rose's avatar
Carsten Rose committed
407
     *
408
     * @param array $json
Carsten  Rose's avatar
Carsten Rose committed
409
     * @return string
410
     * @throws CodeException
411
     * @throws DbException
412
     * @throws UserFormException
413
     * @throws UserReportException
Carsten  Rose's avatar
Carsten Rose committed
414
     */
415
    private function buildPillNavigation($mode = FORM_LOAD, array $pillArray, array &$json) {
416
417
        $pillButton = '';
        $pillDropdown = '';
418
        $htmlDropdown = '';
419

420
        if ($pillArray == null) {
421
            return '';
422
        }
423
424
425

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

426
427
        $parameterLanguageFieldName = $this->store->getVar(SYSTEM_PARAMETER_LANGUAGE_FIELD_NAME, STORE_SYSTEM);

428
429
430
        // Iterate over all 'pill'
        $ii = 0;
        $active = 'class="active"';
431
        $recordId = $this->store->getVar(COLUMN_ID, STORE_RECORD . STORE_ZERO);
432
        foreach ($pillArray as $formElement) {
433

434
435
436
437
            if ($mode != FORM_LOAD && $formElement[FE_DYNAMIC_UPDATE] !== 'yes') {
                continue; // During save/update: Process only FE dynamic_update=yes
            }

438
439
            $htmlIdLi = $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_PILL_LI;
            $htmlIdLiA = $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_PILL_LI_A;
440
441
442
443

            $formElement = $this->evaluate->parseArray($formElement);
            HelperFormElement::explodeParameter($formElement, F_PARAMETER);
            $formElement = HelperFormElement::setLanguage($formElement, $parameterLanguageFieldName);
444
445
446
            if (!empty($formElement[FE_MODE_SQL])) {
                $formElement[FE_MODE] = $formElement[FE_MODE_SQL];
            }
447

448
449
            $ii++;

450
            if ($formElement[FE_NAME] === '' || $formElement[FE_LABEL] === '') {
451
                $this->store->setVar(SYSTEM_FORM_ELEMENT, Logger::formatFormElementName($formElement), STORE_SYSTEM);
452
                $this->store->setVar(SYSTEM_FORM_ELEMENT_COLUMN, 'name, label', STORE_SYSTEM);
453
                throw new UserFormException("Field 'name' and/or 'label' are empty", ERROR_NAME_LABEL_EMPTY);
454
455
            }

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

459
            $attributeLiA = 'data-toggle="tab" ';
460
461
            $hrefTarget = '#' . $this->createAnker($formElement[FE_ID]);

462
            $htmlFormElementName = HelperFormElement::buildFormElementName($formElement, $recordId);
463
464
465
466
            switch ($formElement[FE_MODE]) {
                case FE_MODE_SHOW:
                case FE_MODE_REQUIRED:
                    $attributeLi = '';
467
468
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLi][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = '';
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLiA][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = '';
469
                    break;
470

471
                case FE_MODE_READONLY:
472
473
                    $hrefTarget = '#';

474
                    $attributeLi = Support::doAttribute('class', 'disabled');
475
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLi][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = 'disabled';
476
477

                    $attributeLiA .= Support::doAttribute('class', 'noclick');
478
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLiA][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = 'noclick';
479
                    break;
480

481
                case FE_MODE_HIDDEN:
482
483
//                    $attributeLi = Support::doAttribute('style', 'display: none');
                    $attributeLi = Support::doAttribute('class', 'hidden');
484
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLi][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = 'hidden';
485
486
                    $a = '';
                    break;
487

488
489
490
                default:
                    throw new UserFormException("Unknown Mode: " . $formElement[FE_MODE], ERROR_UNKNOWN_MODE);
            }
491

492
493
            $attributeLi .= Support::doAttribute(HTML_ATTR_ID, $htmlIdLi);
            $attributeLiA .= Support::doAttribute(HTML_ATTR_ID, $htmlIdLiA);
494
            $a = Support::wrapTag("<a $attributeLiA" . Support::doAttribute('href', $hrefTarget) . ">", $formElement[FE_LABEL]);
495
            $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLiA][API_ELEMENT_CONTENT] = $formElement[FE_LABEL];
496

497
            if ($ii <= $maxVisiblePill) {
498
                $pillButton .= '<li role="presentation"' . $attributeLi . $active . ">" . $a . "</li>";
499
            } else {
500
                $pillDropdown .= '<li ' . $attributeLi . '>' . $a . "</li>";
501
            }
502

503
504
            $active = '';
        }
505

506
507
        // Pill Dropdown necessary?
        if ($ii > $maxVisiblePill) {
508
            $htmlDropdown = Support::wrapTag('<ul class="dropdown-menu qfq-form-pill ' . $this->formSpec[F_CLASS_PILL] . '">', $pillDropdown, true);
509
            $htmlDropdown = '<a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button">more <span class="caret"></span></a>' . $htmlDropdown;
510
            $htmlDropdown = Support::wrapTag('<li role="presentation" class="dropdown">', $htmlDropdown, false);
511
        }
512

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

516
        return $htmlDropdown;
517
518
519
520
521
522
    }

    /**
     * Create an identifier for the pill navigation menu
     *
     * @param $id
Carsten  Rose's avatar
Carsten Rose committed
523
     *
524
525
526
     * @return string
     */
    private function createAnker($id) {
527
        return $this->formSpec[FE_NAME] . '_' . $id;
528
529
    }

Carsten  Rose's avatar
Carsten Rose committed
530
531
532
533
534
535
536
    /**
     * @return string
     */
    private function getTabId() {
        return 'qfqTabs';
    }

537
538
539
540
    /**
     * Builds the complete HTML '<form ...>'-tag
     *
     * @return string
541
542
     * @throws CodeException
     * @throws DbException
543
     * @throws UserFormException
544
     */
545
    public function getFormTag() {
546

547
        $attribute = $this->getFormTagAttributes();
548
549

        $attribute['class'] = 'form-horizontal';
550
        $attribute['data-toggle'] = 'validator';
551
        if (isset($this->formSpec[F_SAVE_BUTTON_ACTIVE]) && $this->formSpec[F_SAVE_BUTTON_ACTIVE] != '0') {
552
553
            $attribute[DATA_ENABLE_SAVE_BUTTON] = 'true';
        }
554
555

        $honeypot = $this->getHoneypotVars();
556
        $md5 = $this->buildInputRecordHashMd5();
557

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

561
562
    /**
     * @return string
563
564
     * @throws CodeException
     * @throws UserFormException
565
     */
566
    public function tail() {
567

568
        $html = '';
Carsten  Rose's avatar
Carsten Rose committed
569
        $deleteUrl = '';
570

571
572
        $formId = $this->getFormId();

573
574
        // Button Save at bottom of form - only if there is a button text given.
        if ($this->formSpec[F_SUBMIT_BUTTON_TEXT] !== '') {
575
576
577
578

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

579
580
            $buttonText = $this->formSpec[F_SUBMIT_BUTTON_TEXT];

581
            $htmlElement = $this->buildButtonCode('save-button', $buttonText, $buttonText, '', '', $this->formSpec[F_BUTTON_ON_CHANGE_CLASS], 'btn btn-default');
582
583
584
585
586
587
588
589

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

590
        $html .= '</div> <!--class="tab-content" -->';  //  <div class="tab-content">
591
//        $html .= '<input type="submit" value="Submit">';
592

Carsten  Rose's avatar
Carsten Rose committed
593
594
595
        $formId = $this->getFormId();
        $tabId = $this->getTabId();

Carsten  Rose's avatar
Carsten Rose committed
596
        if (0 < ($recordId = $this->store->getVar(SIP_RECORD_ID, STORE_SIP))) {
597
            $deleteUrl = $this->createDeleteUrl($this->formSpec[F_FINAL_DELETE_FORM], $recordId);
Carsten  Rose's avatar
Carsten Rose committed
598
        }
599

600
601
602
        $actionUpload = FILE_ACTION . '=' . FILE_ACTION_UPLOAD;
        $actionDelete = FILE_ACTION . '=' . FILE_ACTION_DELETE;

603
604
605
        $apiDir = API_DIR;
        $apiDeletePhp = API_DIR . '/' . API_DELETE_PHP;

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

608
        $html .= '</form>';  //  <form class="form-horizontal" ...
609
610
611
612
613
614
        $html .= <<<EOF
        <script type="text/javascript">
            $(function () {
                'use strict';
                QfqNS.Log.level = 0;

Carsten  Rose's avatar
Carsten Rose committed
615
616
617
                var qfqPage = new QfqNS.QfqPage({
                    tabsId: '$tabId',
                    formId: '$formId',
618
                    submitTo: '$apiDir/save.php',
Carsten  Rose's avatar
Carsten Rose committed
619
                    $dirtyAction
Carsten  Rose's avatar
Carsten Rose committed
620
                    deleteUrl: '$deleteUrl',
621
622
623
                    refreshUrl: '$apiDir/load.php',
                    fileUploadTo: '$apiDir/file.php?$actionUpload',
                    fileDeleteUrl: '$apiDir/file.php?$actionDelete'
Carsten  Rose's avatar
Carsten Rose committed
624
625
                });

626
               
627
                var qfqRecordList = new QfqNS.QfqRecordList('$apiDeletePhp');
Carsten  Rose's avatar
Carsten Rose committed
628
            })
629
630
         </script>
EOF;
631
        $html .= '</div>';  //  <div class="container-fluid"> === main <div class=...> around everything
632

633
634
635
636
637
        return $html;
    }

    /**
     * @param array $formElement
Carsten  Rose's avatar
Carsten Rose committed
638
639
640
     * @param       $htmlFormElementName
     * @param       $value
     *
641
     * @param array $json
642
     * @return mixed
643
644
645
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
646
     * @throws UserReportException
647
     */
648
    public function buildPill(array $formElement, $htmlFormElementName, $value, array &$json) {
649
        $html = '';
650
651
652
653
        // save parent processed FE's
        $tmpStore = $this->feSpecNative;

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

Carsten  Rose's avatar
Carsten Rose committed
657
        $html = $this->elements($this->store->getVar(SIP_RECORD_ID, STORE_SIP), FORM_ELEMENTS_NATIVE_SUBRECORD, 0, $json);
658
659
660
661

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

662
663
664
        return $html;
    }

665
    /**
Carsten  Rose's avatar
Carsten Rose committed
666
     * @param array $formElement Complete FormElement, especially some FE_WRAP
667
     * @param string $htmlElement Content to wrap.
Carsten  Rose's avatar
Carsten Rose committed
668
669
     * @param        $htmlFormElementName
     *
670
     * @return string               Wrapped $htmlElement
671
672
     * @throws CodeException
     * @throws UserFormException
673
     */
674
    public function buildRowNative(array $formElement, $htmlElement, $htmlFormElementName) {
675
        $html = '';
676
        $htmlLabel = '';
677
678
        $classHideRow = '';
        $classHideElement = '';
679

680
        if ($formElement[FE_MODE] == FE_MODE_HIDDEN) {
681
            if ($formElement[FE_FLAG_ROW_OPEN_TAG] && $formElement[FE_FLAG_ROW_CLOSE_TAG]) {
682
683
684
685
686
687
                $classHideRow = 'hidden';
            } else {
                $classHideElement = 'hidden';
            }
        }

Carsten  Rose's avatar
Carsten Rose committed
688
        // Label
689
        if ($formElement[FE_BS_LABEL_COLUMNS] > 0) {
690
691
            $addClass = ($formElement[FE_MODE] == FE_MODE_REQUIRED) ? CSS_REQUIRED : '';
            $htmlLabel = $this->buildLabel($htmlFormElementName, $formElement[FE_LABEL], $addClass);
692
        }
693

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

Carsten  Rose's avatar
Carsten Rose committed
697
        // Input
698
        $html .= $this->customWrap($formElement, $htmlElement, FE_WRAP_INPUT, $formElement[FE_BS_INPUT_COLUMNS],
699
700
            [$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);
701

Carsten  Rose's avatar
Carsten Rose committed
702
        // Note
Carsten  Rose's avatar
Carsten Rose committed
703
        $note = $formElement[FE_NOTE];
704
        $html .= $this->customWrap($formElement, $note, FE_WRAP_NOTE, $formElement[FE_BS_NOTE_COLUMNS],
705
            [$this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_START], $this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_END]], $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_NOTE);
706

707
        // Row
708
709
        $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] : '';
710
711

        $html = $this->customWrap($formElement, $html, FE_WRAP_ROW, -1, [$openTag, $closeTag], $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_ROW);
712
713
714
715

        return $html;
    }

716
    /**
717
718
719
720
721
722
     * 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.
723
     *
Carsten  Rose's avatar
Carsten Rose committed
724
     * @param array $formElement Complete FormElement, especially some FE_WRAP
725
     * @param string $htmlElement Content to wrap.
Carsten  Rose's avatar
Carsten Rose committed
726
727
728
     * @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' ]
729
730
     * @param string $htmlId
     * @param string $class
Carsten  Rose's avatar
Carsten Rose committed
731
     *
732
     * @return string Wrapped $htmlElement
733
     * @throws CodeException
734
735
     * @throws \qfq\UserFormException
     */
736
    private function customWrap(array $formElement, $htmlElement, $wrapName, $bsColumns, array $wrapArray, $htmlId = '', $class = '') {
737

738
        // If $bsColumns==0: do not wrap with default.
739
740
741
742
        if ($bsColumns == 0) {
            $wrapArray[0] = '';
            $wrapArray[1] = '';
        }
743

744
        // If there is a 'per FormElement'-wrap, take it.
745
746
747
748
749
750
751
752
        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);
        }

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

758
759
760
        return $wrapArray[0] . $htmlElement . $wrapArray[1];
    }

761

762
763
764
    /**
     * @param $formElement
     * @param $elementHtml
Carsten  Rose's avatar
Carsten Rose committed
765
     *
766
767
     * @return string
     */
768
    public function buildRowPill(array $formElement, $elementHtml) {
769
770
        $html = '';

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

        $active = $this->isFirstPill ? ' active' : '';

775
        $html = Support::wrapTag('<div role="tabpanel" class="tab-pane' . $active . '" id="' . $this->createAnker($formElement['id']) . '">', $html);
776
777
778
779
780
781

        $this->isFirstPill = false;

        return $html;
    }

782
    /**
783
784
     * Builds a fieldset
     *
Carsten  Rose's avatar
Carsten Rose committed
785
     * @param array $formElement
786
     * @param $elementHtml
Carsten  Rose's avatar
Carsten Rose committed
787
     * @return mixed
788
     */
789
    public function buildRowFieldset(array $formElement, $elementHtml) {
790
791
792
        $html = $elementHtml;

        return $html;
793
794
    }

795
796
797
798
799
    /**
     * Builds a templateGroup
     *
     * @param $formElement
     * @param $elementHtml
800
     * @return mixed
801
802
803
804
805
806
807
     */
    public function buildRowTemplateGroup(array $formElement, $elementHtml) {
        $html = $elementHtml;

        return $html;
    }

808
    /**
809
     * @param array $formElement
810
     * @param $elementHtml
Carsten  Rose's avatar
Carsten Rose committed
811
     *
812
     * @return string
813
     * @throws CodeException
814
     */
815
    public function buildRowSubrecord(array $formElement, $elementHtml) {
816

817
        $formElement[FE_LABEL] = Support::wrapTag("<label class='control-label'>", $formElement[FE_LABEL], true);
818
        $html = $this->wrapItem(WRAP_SETUP_ELEMENT, $this->wrapItem(WRAP_SETUP_SUBRECORD, $formElement[FE_LABEL]));
819
        $html .= $this->wrapItem(WRAP_SETUP_ELEMENT, $this->wrapItem(WRAP_SETUP_SUBRECORD, $elementHtml));
820
        $html .= $this->wrapItem(WRAP_SETUP_ELEMENT, $this->wrapItem(WRAP_SETUP_SUBRECORD, $formElement[FE_NOTE]));
821

822
823
824
        $attribute = ($formElement[FE_MODE] == FE_MODE_HIDDEN) ? ' style="display: none;"' : '';
        $attribute .= Support::doAttribute('id', $formElement[FE_HTML_ID]);

825

826
        return Support::wrapTag("<span name='qfq-subrecord' $attribute>", $html);
827
    }
828
829
830
831
832
833
834


    /**
     * Builds complete 'form'.
     *
     * @param string $mode FORM_LOAD | FORM_UPDATE | FORM_SAVE
     *
835
836
     * @param bool $htmlElementNameIdZero
     * @param array $latestFeSpecNative
837
838
839
840
     * @return string|array   $mode=LOAD_FORM: The whole form as HTML, $mode=FORM_UPDATE /FORM_SAVE: array of all
     *                        formElement.dynamicUpdate-yes  values/states
     * @throws CodeException
     * @throws DbException
841
     * @throws UserFormException
842
     * @throws UserReportException
843
844
845
846
     */
    public function process($mode, $htmlElementNameIdZero = false, $latestFeSpecNative = array()) {

        $json = array();
847
848
        $data = parent::process($mode, $htmlElementNameIdZero, $latestFeSpecNative);