BuildFormBootstrap.php 33.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
        // 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
            $toolTip = "Form not 'form' or 'formElement'";
            $status = 'disabled';
        } else {
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
            $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
271
272
        }

273
        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
274
275
    }

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

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

299
300
            $buttonDebugForm = $this->buildLogForm() .
                $this->buildViewForm() .
Carsten  Rose's avatar
Carsten Rose committed
301
                $this->buildShowEditFormElementCheckbox() .
Carsten  Rose's avatar
Carsten Rose committed
302
                $this->buildButtonCopyForm() .
303
                $this->buildButtonAnchor($url, 'form-edit-button', '', $toolTip, GLYPH_ICON_TOOL, '', 'btn btn-default navbar-btn');
304
305
306
        }

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

314
315
            $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]);
316
317
318
        }

        // Button: Close
Carsten  Rose's avatar
Carsten Rose committed
319
        if (Support::findInSet(FORM_BUTTON_CLOSE, $this->formSpec[F_SHOW_BUTTON])) {
320
321
322
            $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
323
        }
324

Carsten  Rose's avatar
Carsten Rose committed
325
        // Button: Delete
Carsten  Rose's avatar
Carsten Rose committed
326
        if (Support::findInSet(FORM_BUTTON_DELETE, $this->formSpec[F_SHOW_BUTTON])) {
327
            $toolTip = $this->formSpec[F_DELETE_BUTTON_TOOLTIP];
328

329
            if ($this->showDebugInfoFlag && $recordId > 0) {
330
                $toolTip .= PHP_EOL . "form = '" . $this->formSpec[F_FINAL_DELETE_FORM] . "'" . PHP_EOL . "r = '" . $recordId . "'";
Carsten  Rose's avatar
Carsten Rose committed
331
            }
332
            $disabled = ($recordId > 0) ? '' : 'disabled';
Carsten  Rose's avatar
Carsten Rose committed
333

334
335
            $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]);
336
337
        }

Carsten  Rose's avatar
Carsten Rose committed
338
        // Button: New
Carsten  Rose's avatar
Carsten Rose committed
339
        if (Support::findInSet(FORM_BUTTON_NEW, $this->formSpec[F_SHOW_BUTTON])) {
340
            $url = $this->deriveNewRecordUrlFromExistingSip($toolTip);
Carsten  Rose's avatar
Carsten Rose committed
341

342
343
            $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]);
344
345
        }

346
347
        // Arrangement: Edit Form / Save / Close / Delete / New
        $html = '';
348
349
350
351
352

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

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

356
357
358
        return $html;
    }

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

        $attribute = Support::doAttribute('href', $url);
382
383
384
        $attribute .= Support::doAttribute('id', $buttonHtmlId);
        $attribute .= Support::doAttribute('class', "$class $disabled");
        $attribute .= Support::doAttribute('title', $toolTip);
Carsten  Rose's avatar
Carsten Rose committed
385

386
387
388
389
        // disabled links do not show tooltips
        $wrapTag = $disabled == 'disabled' ? 'button' : 'a';

        return Support::wrapTag("<$wrapTag $attribute>", $element);
390
391
392
    }

    /**
393
394
     * Creates a button with the given attributes. If there is no $icon given, render the button without glyph.
     *
395
396
397
     * @param string $buttonHtmlId
     * @param string $text
     * @param string $tooltip
398
     * @param string $icon
399
     * @param string $disabled
Carsten  Rose's avatar
Carsten Rose committed
400
     *
401
402
     * @param string $buttonOnChangeClass
     * @param string $class
403
     * @return string
404
     * @throws CodeException
405
     */
406
    private function buildButtonCode($buttonHtmlId, $text, $tooltip, $icon, $disabled = '', $buttonOnChangeClass = '', $class = '') {
407
408

        if ($icon === '') {
409
410
            $element = $text;
        } else {
411
            $element = "<span class='glyphicon $icon'></span>" . ' ' . $text;
412
413
        }

414
        $class = Support::doAttribute('class', $class);
415
        $dataClassOnChange = Support::doAttribute('data-class-on-change', $buttonOnChangeClass);
416
417
418
        $tooltip = Support::doAttribute('title', $tooltip);

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

Carsten  Rose's avatar
Carsten Rose committed
421
    /**
422
423
424
     * Builds the BS-pills on top of a form.
     *
     * @param string $mode FORM_LOAD | FORM_UPDATE | FORM_SAVE
425
     * @param array $pillArray
Carsten  Rose's avatar
Carsten Rose committed
426
     *
427
     * @param array $json
Carsten  Rose's avatar
Carsten Rose committed
428
     * @return string
429
     * @throws CodeException
430
     * @throws DbException
431
     * @throws UserFormException
432
     * @throws UserReportException
Carsten  Rose's avatar
Carsten Rose committed
433
     */
434
    private function buildPillNavigation($mode = FORM_LOAD, array $pillArray, array &$json) {
435
436
        $pillButton = '';
        $pillDropdown = '';
437
        $htmlDropdown = '';
438

439
        if ($pillArray == null) {
440
            return '';
441
        }
442
443
444

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

445
446
        $parameterLanguageFieldName = $this->store->getVar(SYSTEM_PARAMETER_LANGUAGE_FIELD_NAME, STORE_SYSTEM);

447
448
449
        // Iterate over all 'pill'
        $ii = 0;
        $active = 'class="active"';
450
        $recordId = $this->store->getVar(COLUMN_ID, STORE_RECORD . STORE_ZERO);
451
        foreach ($pillArray as $formElement) {
452

453
454
455
456
            if ($mode != FORM_LOAD && $formElement[FE_DYNAMIC_UPDATE] !== 'yes') {
                continue; // During save/update: Process only FE dynamic_update=yes
            }

457
458
            $htmlIdLi = $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_PILL_LI;
            $htmlIdLiA = $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_PILL_LI_A;
459
460
461
462

            $formElement = $this->evaluate->parseArray($formElement);
            HelperFormElement::explodeParameter($formElement, F_PARAMETER);
            $formElement = HelperFormElement::setLanguage($formElement, $parameterLanguageFieldName);
463
464
465
            if (!empty($formElement[FE_MODE_SQL])) {
                $formElement[FE_MODE] = $formElement[FE_MODE_SQL];
            }
466

467
468
            $ii++;

469
            if ($formElement[FE_NAME] === '' || $formElement[FE_LABEL] === '') {
470
                $this->store->setVar(SYSTEM_FORM_ELEMENT, Logger::formatFormElementName($formElement), STORE_SYSTEM);
471
                $this->store->setVar(SYSTEM_FORM_ELEMENT_COLUMN, 'name, label', STORE_SYSTEM);
472
                throw new UserFormException("Field 'name' and/or 'label' are empty", ERROR_NAME_LABEL_EMPTY);
473
474
            }

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

478
            $attributeLiA = 'data-toggle="tab" ';
479
480
            $hrefTarget = '#' . $this->createAnker($formElement[FE_ID]);

481
            $htmlFormElementName = HelperFormElement::buildFormElementName($formElement, $recordId);
482
483
484
485
            switch ($formElement[FE_MODE]) {
                case FE_MODE_SHOW:
                case FE_MODE_REQUIRED:
                    $attributeLi = '';
486
487
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLi][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = '';
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLiA][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = '';
488
                    break;
489

490
                case FE_MODE_READONLY:
491
492
                    $hrefTarget = '#';

493
                    $attributeLi = Support::doAttribute('class', 'disabled');
494
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLi][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = 'disabled';
495
496

                    $attributeLiA .= Support::doAttribute('class', 'noclick');
497
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLiA][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = 'noclick';
498
                    break;
499

500
                case FE_MODE_HIDDEN:
501
502
//                    $attributeLi = Support::doAttribute('style', 'display: none');
                    $attributeLi = Support::doAttribute('class', 'hidden');
503
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLi][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = 'hidden';
504
505
                    $a = '';
                    break;
506

507
508
509
                default:
                    throw new UserFormException("Unknown Mode: " . $formElement[FE_MODE], ERROR_UNKNOWN_MODE);
            }
510

511
            $attributeLi .= Support::doAttribute(HTML_ATTR_ID, $htmlIdLi);
Elias Villiger's avatar
Elias Villiger committed
512
            $attributeLi .= Support::doAttribute( 'title', $formElement[FE_TOOLTIP]);
513
            $attributeLiA .= Support::doAttribute(HTML_ATTR_ID, $htmlIdLiA);
514
            $a = Support::wrapTag("<a $attributeLiA" . Support::doAttribute('href', $hrefTarget) . ">", $formElement[FE_LABEL]);
515
            $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLiA][API_ELEMENT_CONTENT] = $formElement[FE_LABEL];
516

517
            if ($ii <= $maxVisiblePill) {
518
                $pillButton .= '<li role="presentation"' . $attributeLi . $active . ">" . $a . "</li>";
519
            } else {
520
                $pillDropdown .= '<li ' . $attributeLi . '>' . $a . "</li>";
521
            }
522

523
524
            $active = '';
        }
525

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

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

536
        return $htmlDropdown;
537
538
539
540
541
542
    }

    /**
     * Create an identifier for the pill navigation menu
     *
     * @param $id
Carsten  Rose's avatar
Carsten Rose committed
543
     *
544
545
546
     * @return string
     */
    private function createAnker($id) {
547
        return $this->formSpec[FE_NAME] . '_' . $id;
548
549
    }

Carsten  Rose's avatar
Carsten Rose committed
550
551
552
553
554
555
556
    /**
     * @return string
     */
    private function getTabId() {
        return 'qfqTabs';
    }

557
558
559
560
    /**
     * Builds the complete HTML '<form ...>'-tag
     *
     * @return string
561
562
     * @throws CodeException
     * @throws DbException
563
     * @throws UserFormException
564
     */
565
    public function getFormTag() {
566

567
        $attribute = $this->getFormTagAttributes();
568
569

        $attribute['class'] = 'form-horizontal';
570
        $attribute['data-toggle'] = 'validator';
571
        if (isset($this->formSpec[F_SAVE_BUTTON_ACTIVE]) && $this->formSpec[F_SAVE_BUTTON_ACTIVE] != '0') {
572
573
            $attribute[DATA_ENABLE_SAVE_BUTTON] = 'true';
        }
574
575

        $honeypot = $this->getHoneypotVars();
576
        $md5 = $this->buildInputRecordHashMd5();
577

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

581
582
    /**
     * @return string
583
584
     * @throws CodeException
     * @throws UserFormException
585
     */
586
    public function tail() {
587

588
        $html = '';
Carsten  Rose's avatar
Carsten Rose committed
589
        $deleteUrl = '';
590

591
592
        $formId = $this->getFormId();

593
594
        // Button Save at bottom of form - only if there is a button text given.
        if ($this->formSpec[F_SUBMIT_BUTTON_TEXT] !== '') {
595
596
597
598

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

599
600
            $buttonText = $this->formSpec[F_SUBMIT_BUTTON_TEXT];

601
            $htmlElement = $this->buildButtonCode('save-button', $buttonText, $buttonText, '', '', $this->formSpec[F_BUTTON_ON_CHANGE_CLASS], 'btn btn-default');
602
603
604
605
606
607
608
609

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

610
        $html .= '</div> <!--class="tab-content" -->';  //  <div class="tab-content">
611
//        $html .= '<input type="submit" value="Submit">';
612

Carsten  Rose's avatar
Carsten Rose committed
613
614
615
        $formId = $this->getFormId();
        $tabId = $this->getTabId();

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

620
621
622
        $actionUpload = FILE_ACTION . '=' . FILE_ACTION_UPLOAD;
        $actionDelete = FILE_ACTION . '=' . FILE_ACTION_DELETE;

623
624
625
        $apiDir = API_DIR;
        $apiDeletePhp = API_DIR . '/' . API_DELETE_PHP;

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

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

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

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

653
654
655
656
657
        return $html;
    }

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

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

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

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

682
683
684
        return $html;
    }

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

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

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

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

Carsten  Rose's avatar
Carsten Rose committed
717
        // Input
718
        $html .= $this->customWrap($formElement, $htmlElement, FE_WRAP_INPUT, $formElement[FE_BS_INPUT_COLUMNS],
719
720
            [$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);
721

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

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

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

        return $html;
    }

736
    /**
737
738
739
740
741
742
     * 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.
743
     *
Carsten  Rose's avatar
Carsten Rose committed
744
     * @param array $formElement Complete FormElement, especially some FE_WRAP
745
     * @param string $htmlElement Content to wrap.
Carsten  Rose's avatar
Carsten Rose committed
746
747
748
     * @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' ]
749
750
     * @param string $htmlId
     * @param string $class
Carsten  Rose's avatar
Carsten Rose committed
751
     *
752
     * @return string Wrapped $htmlElement
753
     * @throws CodeException
754
755
     * @throws \qfq\UserFormException
     */
756
    private function customWrap(array $formElement, $htmlElement, $wrapName, $bsColumns, array $wrapArray, $htmlId = '', $class = '') {
757

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

764
        // If there is a 'per FormElement'-wrap, take it.
765
766
767
768
769
770
771
772
        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);
        }

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

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

781

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

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

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

795
        $html = Support::wrapTag('<div role="tabpanel" class="tab-pane' . $active . '" id="' . $this->createAnker($formElement['id']) . '">', $html);
796
797
798
799
800
801

        $this->isFirstPill = false;

        return $html;
    }

802
    /**
803
804
     * Builds a fieldset
     *
Carsten  Rose's avatar
Carsten Rose committed
805
     * @param array $formElement
806
     * @param $elementHtml
Carsten  Rose's avatar
Carsten Rose committed
807
     * @return mixed
808
     */
809
    public function buildRowFieldset(array $formElement, $elementHtml) {
810
811
812
        $html = $elementHtml;

        return $html;
813
814
    }

815
816
817
818
819
    /**
     * Builds a templateGroup
     *
     * @param $formElement
     * @param $elementHtml
820
     * @return mixed
821
822
823
824
825
826
827
     */
    public function buildRowTemplateGroup(array $formElement, $elementHtml) {
        $html = $elementHtml;

        return $html;
    }

828
    /**
829
     * @param array $formElement
830
     * @param $elementHtml
Carsten  Rose's avatar
Carsten Rose committed
831
     *
832
     * @return string
833
     * @throws CodeException
834
     */
835
    public function buildRowSubrecord(array $formElement, $elementHtml) {
836

837
        $formElement[FE_LABEL] = Support::wrapTag("<label class='control-label'>", $formElement[FE_LABEL], true);
838
        $html = $this->wrapItem(WRAP_SETUP_ELEMENT, $this->wrapItem(WRAP_SETUP_SUBRECORD, $formElement[FE_LABEL]));
839
        $html .= $this->wrapItem(WRAP_SETUP_ELEMENT, $this->wrapItem(WRAP_SETUP_SUBRECORD, $elementHtml));
840
        $html .= $this->wrapItem(WRAP_SETUP_ELEMENT, $this->wrapItem(WRAP_SETUP_SUBRECORD, $formElement[FE_NOTE]));
841

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

845

846
        return Support::wrapTag("<span name='qfq-subrecord' $attribute>", $html);
847
    }
848
849
850
851
852
853
854