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

namespace qfq;

use qfq;

require_once(__DIR__ . '/../qfq/Constants.php');
14
require_once(__DIR__ . '/../qfq/helper/OnArray.php');
15
require_once(__DIR__ . '/../qfq/AbstractBuildForm.php');
16
require_once(__DIR__ . '/../qfq/exceptions/UserFormException.php');
17

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

24
25
26
27
28
29
    private $isFirstPill;

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

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

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

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

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

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

58
59
        $this->wrap[WRAP_SETUP_IN_FIELDSET][WRAP_SETUP_START] = "";
        $this->wrap[WRAP_SETUP_IN_FIELDSET][WRAP_SETUP_END] = "";
60

61
62
63
        $this->wrap[WRAP_SETUP_IN_TEMPLATE_GROUP][WRAP_SETUP_START] = "";
        $this->wrap[WRAP_SETUP_IN_TEMPLATE_GROUP][WRAP_SETUP_END] = "";

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

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

        return "<div $class>";

    }
80

81
82
83
84
85
86
    /**
     * @param $label
     * @param $input
     * @param $note
     */
    public function fillWrapLabelInputNote($label, $input, $note) {
87
        $this->wrap[WRAP_SETUP_LABEL][WRAP_SETUP_START] = "<div class='col-md-$label qfq-label'>";
88
89
90
        $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
91
        $this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_START] = "<div class='col-md-$note qfq-note'>";
92
93
94
95
        $this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_END] = "</div>";

    }

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

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

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

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

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

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

        $html .= $button . $title;
132

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

137
138
        $html .= $this->getFormTag();

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

        return $html;
    }

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

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

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

Carsten  Rose's avatar
Carsten Rose committed
195
196
197
198
    /**
     * Creates a link to open current form loaded in FormEditor
     *
     * @return string - the rendered Checkbox
199
200
201
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
Carsten  Rose's avatar
Carsten Rose committed
202
203
204
     */
    private function buildViewForm() {

205
206
207
208
        $form = false;
        $url = '';
        $status = '';

Carsten  Rose's avatar
Carsten Rose committed
209
210
211
212
213
        switch ($this->formSpec[F_NAME]) {
            case 'form':
                $form = $this->store->getVar(F_NAME, STORE_RECORD);
                break;
            case 'formElement':
214
                if (false !== ($formId = $this->store->getVar(FE_FORM_ID, STORE_SIP . STORE_RECORD))) {
215
                    $row = $this->dbArray[$this->dbIndexQfq]->sql("SELECT f.name FROM Form AS f WHERE id=" . $formId, ROW_EXPECT_1);
216
217
                    $form = current($row);
                }
Carsten  Rose's avatar
Carsten Rose committed
218
219
220
221
222
223
                break;
            default:
                return '';
        }

        if ($form === false) {
224
225
226
227
228
229
230
            $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
231
                'r' => 0,
232
233
234
235
236
237
            ];
            $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
238
239
        }

240
        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
241
242
    }

243
    /**
244
     * Build Buttons panel on top right corner of form.
245
     * Simulate Submit Button: http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml
246
247
     *
     * @return string
248
     * @throws CodeException
Carsten  Rose's avatar
Carsten Rose committed
249
     * @throws DbException
250
     * @throws UserFormException
251
252
253
     */
    private function buildButtons() {
        $buttonNew = '';
254
255
256
257
        $buttonDelete = '';
        $buttonClose = '';
        $buttonSave = '';
        $buttonEditForm = '';
Carsten  Rose's avatar
Carsten Rose committed
258
259
260
        $recordId = $this->store->getVar(SIP_RECORD_ID, STORE_SIP);

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

Carsten  Rose's avatar
Carsten Rose committed
265
266
            $buttonEditForm = $this->buildViewForm() .
                $this->buildShowEditFormElementCheckbox() .
Carsten  Rose's avatar
Carsten Rose committed
267
                $this->buildButtonCopyForm() .
268
                $this->buildButtonAnchor($url, 'form-edit-button', '', $toolTip, GLYPH_ICON_TOOL, '', 'btn btn-default navbar-btn');
269
270
271
        }

        // Button: Save
Carsten  Rose's avatar
Carsten Rose committed
272
        if (Support::findInSet(FORM_BUTTON_SAVE, $this->formSpec[F_SHOW_BUTTON]) && $this->formSpec[F_SUBMIT_BUTTON_TEXT] === '') {
273
274
            $toolTip = $this->formSpec[F_SAVE_BUTTON_TOOLTIP];
            // In debugMode every button link should show the information behind the SIP.
275
            if ($this->showDebugInfoFlag) {
276
                $toolTip .= PHP_EOL . "table = '" . $this->formSpec[F_TABLE_NAME] . "'" . PHP_EOL . "r = '" . $recordId . "'";
277
278
            }

279
280
            $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]);
281
282
283
        }

        // Button: Close
Carsten  Rose's avatar
Carsten Rose committed
284
        if (Support::findInSet(FORM_BUTTON_CLOSE, $this->formSpec[F_SHOW_BUTTON])) {
285
286
287
            $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
288
        }
289

Carsten  Rose's avatar
Carsten Rose committed
290
        // Button: Delete
Carsten  Rose's avatar
Carsten Rose committed
291
        if (Support::findInSet(FORM_BUTTON_DELETE, $this->formSpec[F_SHOW_BUTTON])) {
292
            $toolTip = $this->formSpec[F_DELETE_BUTTON_TOOLTIP];
293

294
            if ($this->showDebugInfoFlag && $recordId > 0) {
295
                $toolTip .= PHP_EOL . "form = '" . $this->formSpec[F_FINAL_DELETE_FORM] . "'" . PHP_EOL . "r = '" . $recordId . "'";
Carsten  Rose's avatar
Carsten Rose committed
296
            }
297
            $disabled = ($recordId > 0) ? '' : 'disabled';
Carsten  Rose's avatar
Carsten Rose committed
298

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

Carsten  Rose's avatar
Carsten Rose committed
303
        // Button: New
Carsten  Rose's avatar
Carsten Rose committed
304
        if (Support::findInSet(FORM_BUTTON_NEW, $this->formSpec[F_SHOW_BUTTON])) {
305
            $url = $this->deriveNewRecordUrlFromExistingSip($toolTip);
Carsten  Rose's avatar
Carsten Rose committed
306

307
308
            $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]);
309
310
        }

311
        // Arrangement: Edit Form / Save / Close / Delete / New
Carsten  Rose's avatar
Carsten Rose committed
312
        // Specified in reverse order cause 'pull-right' inverts the order. http://getbootstrap.com/css/#helper-classes-floats
313
        $html = '';
314
315
316
317
318
//        $html .= Support::wrapTag('<div class="btn-group" role="group">', $buttonNew);
//        $html .= Support::wrapTag('<div class="btn-group" role="group">', $buttonDelete);
//        $html .= Support::wrapTag('<div class="btn-group" role="group">', $buttonSave . $buttonClose);
//        $html .= Support::wrapTag('<div class="btn-group" role="group">', $buttonEditForm);

319
        $html .= Support::wrapTag('<div class="btn-group" role="group">', $buttonEditForm);
320
321
322
        $html .= Support::wrapTag('<div class="btn-group" role="group">', $buttonSave . $buttonClose);
        $html .= Support::wrapTag('<div class="btn-group" role="group">', $buttonDelete);
        $html .= Support::wrapTag('<div class="btn-group" role="group">', $buttonNew);
323

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

326
327
328
        return $html;
    }

329
    /**
330
331
     * Generic function to create a button with a given $buttonHtmlId, $url, $title, $icon (=glyph), $disabled
     *
Carsten  Rose's avatar
Carsten Rose committed
332
     * @param string $url
333
334
335
     * @param string $buttonHtmlId
     * @param string $text
     * @param string $toolTip
Carsten  Rose's avatar
Carsten Rose committed
336
     * @param string $icon
337
     * @param string $disabled
338
     * @param string $class
Carsten  Rose's avatar
Carsten Rose committed
339
     *
340
     * @return string
341
     * @throws CodeException
342
     */
343
344
345
346
347
348
349
    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
350
351

        $attribute = Support::doAttribute('href', $url);
352
353
354
        $attribute .= Support::doAttribute('id', $buttonHtmlId);
        $attribute .= Support::doAttribute('class', "$class $disabled");
        $attribute .= Support::doAttribute('title', $toolTip);
Carsten  Rose's avatar
Carsten Rose committed
355

356
        return Support::wrapTag("<a $attribute>", $element);
357
358
359
    }

    /**
360
361
     * Creates a button with the given attributes. If there is no $icon given, render the button without glyph.
     *
362
363
364
     * @param string $buttonHtmlId
     * @param string $text
     * @param string $tooltip
365
     * @param string $icon
366
     * @param string $disabled
Carsten  Rose's avatar
Carsten Rose committed
367
     *
368
369
     * @param string $buttonOnChangeClass
     * @param string $class
370
     * @return string
371
     * @throws CodeException
372
     */
373
    private function buildButtonCode($buttonHtmlId, $text, $tooltip, $icon, $disabled = '', $buttonOnChangeClass = '', $class = '') {
374
375

        if ($icon === '') {
376
377
            $element = $text;
        } else {
378
            $element = "<span class='glyphicon $icon'></span>" . ' ' . $text;
379
380
        }

381
        $class = Support::doAttribute('class', $class);
382
        $dataClassOnChange = Support::doAttribute('data-class-on-change', $buttonOnChangeClass);
383
384
385
        $tooltip = Support::doAttribute('title', $tooltip);

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

Carsten  Rose's avatar
Carsten Rose committed
388
    /**
389
390
391
     * Builds the BS-pills on top of a form.
     *
     * @param string $mode FORM_LOAD | FORM_UPDATE | FORM_SAVE
392
     * @param array $pillArray
Carsten  Rose's avatar
Carsten Rose committed
393
     *
394
     * @param array $json
Carsten  Rose's avatar
Carsten Rose committed
395
     * @return string
396
     * @throws CodeException
397
     * @throws DbException
398
     * @throws UserFormException
399
     * @throws UserReportException
Carsten  Rose's avatar
Carsten Rose committed
400
     */
401
    private function buildPillNavigation($mode = FORM_LOAD, array $pillArray, array &$json) {
402
403
        $pillButton = '';
        $pillDropdown = '';
404
        $htmlDropdown = '';
405

406
        if ($pillArray == null) {
407
            return '';
408
        }
409
410
411

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

412
413
        $parameterLanguageFieldName = $this->store->getVar(SYSTEM_PARAMETER_LANGUAGE_FIELD_NAME, STORE_SYSTEM);

414
415
416
        // Iterate over all 'pill'
        $ii = 0;
        $active = 'class="active"';
417
        $recordId = $this->store->getVar(COLUMN_ID, STORE_RECORD . STORE_ZERO);
418
        foreach ($pillArray as $formElement) {
419

420
421
422
423
            if ($mode != FORM_LOAD && $formElement[FE_DYNAMIC_UPDATE] !== 'yes') {
                continue; // During save/update: Process only FE dynamic_update=yes
            }

424
425
            $htmlIdLi = $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_PILL_LI;
            $htmlIdLiA = $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_PILL_LI_A;
426
427
428
429

            $formElement = $this->evaluate->parseArray($formElement);
            HelperFormElement::explodeParameter($formElement, F_PARAMETER);
            $formElement = HelperFormElement::setLanguage($formElement, $parameterLanguageFieldName);
430
431
432
            if (!empty($formElement[FE_MODE_SQL])) {
                $formElement[FE_MODE] = $formElement[FE_MODE_SQL];
            }
433

434
435
            $ii++;

436
            if ($formElement[FE_NAME] === '' || $formElement[FE_LABEL] === '') {
437
                $this->store->setVar(SYSTEM_FORM_ELEMENT, Logger::formatFormElementName($formElement), STORE_SYSTEM);
438
                $this->store->setVar(SYSTEM_FORM_ELEMENT_COLUMN, 'name, label', STORE_SYSTEM);
439
                throw new UserFormException("Field 'name' and/or 'label' are empty", ERROR_NAME_LABEL_EMPTY);
440
441
            }

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

445
            $attributeLiA = 'data-toggle="tab" ';
446
447
            $hrefTarget = '#' . $this->createAnker($formElement[FE_ID]);

448
            $htmlFormElementName = HelperFormElement::buildFormElementName($formElement, $recordId);
449
450
451
452
            switch ($formElement[FE_MODE]) {
                case FE_MODE_SHOW:
                case FE_MODE_REQUIRED:
                    $attributeLi = '';
453
454
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLi][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = '';
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLiA][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = '';
455
                    break;
456

457
                case FE_MODE_READONLY:
458
459
                    $hrefTarget = '#';

460
                    $attributeLi = Support::doAttribute('class', 'disabled');
461
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLi][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = 'disabled';
462
463

                    $attributeLiA .= Support::doAttribute('class', 'noclick');
464
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLiA][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = 'noclick';
465
                    break;
466

467
                case FE_MODE_HIDDEN:
468
469
//                    $attributeLi = Support::doAttribute('style', 'display: none');
                    $attributeLi = Support::doAttribute('class', 'hidden');
470
                    $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLi][API_ELEMENT_ATTRIBUTE][HTML_ATTR_CLASS] = 'hidden';
471
472
                    $a = '';
                    break;
473

474
475
476
                default:
                    throw new UserFormException("Unknown Mode: " . $formElement[FE_MODE], ERROR_UNKNOWN_MODE);
            }
477

478
479
            $attributeLi .= Support::doAttribute(HTML_ATTR_ID, $htmlIdLi);
            $attributeLiA .= Support::doAttribute(HTML_ATTR_ID, $htmlIdLiA);
480
            $a = Support::wrapTag("<a $attributeLiA" . Support::doAttribute('href', $hrefTarget) . ">", $formElement[FE_LABEL]);
481
            $json[$htmlFormElementName][API_ELEMENT_UPDATE][$htmlIdLiA][API_ELEMENT_CONTENT] = $formElement[FE_LABEL];
482

483
            if ($ii <= $maxVisiblePill) {
484
                $pillButton .= '<li role="presentation"' . $attributeLi . $active . ">" . $a . "</li>";
485
            } else {
486
                $pillDropdown .= '<li ' . $attributeLi . '>' . $a . "</li>";
487
            }
488

489
490
            $active = '';
        }
491

492
493
        // Pill Dropdown necessary?
        if ($ii > $maxVisiblePill) {
494
            $htmlDropdown = Support::wrapTag('<ul class="dropdown-menu qfq-form-pill ' . $this->formSpec[F_CLASS_PILL] . '">', $pillDropdown, true);
495
            $htmlDropdown = '<a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button">more <span class="caret"></span></a>' . $htmlDropdown;
496
            $htmlDropdown = Support::wrapTag('<li role="presentation" class="dropdown">', $htmlDropdown, false);
497
        }
498

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

502
        return $htmlDropdown;
503
504
505
506
507
508
    }

    /**
     * Create an identifier for the pill navigation menu
     *
     * @param $id
Carsten  Rose's avatar
Carsten Rose committed
509
     *
510
511
512
     * @return string
     */
    private function createAnker($id) {
513
        return $this->formSpec[FE_NAME] . '_' . $id;
514
515
    }

Carsten  Rose's avatar
Carsten Rose committed
516
517
518
519
520
521
522
    /**
     * @return string
     */
    private function getTabId() {
        return 'qfqTabs';
    }

523
524
525
526
    /**
     * Builds the complete HTML '<form ...>'-tag
     *
     * @return string
527
528
     * @throws CodeException
     * @throws DbException
529
     * @throws UserFormException
530
     */
531
    public function getFormTag() {
532

533
        $attribute = $this->getFormTagAttributes();
534
535

        $attribute['class'] = 'form-horizontal';
536
        $attribute['data-toggle'] = 'validator';
537
        if (isset($this->formSpec[F_SAVE_BUTTON_ACTIVE]) && $this->formSpec[F_SAVE_BUTTON_ACTIVE] != '0') {
538
539
            $attribute[DATA_ENABLE_SAVE_BUTTON] = 'true';
        }
540
541

        $honeypot = $this->getHoneypotVars();
542
        $md5 = $this->buildInputRecordHashMd5();
543

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

547
548
    /**
     * @return string
549
550
     * @throws CodeException
     * @throws UserFormException
551
     */
552
    public function tail() {
553

554
        $html = '';
Carsten  Rose's avatar
Carsten Rose committed
555
        $deleteUrl = '';
556

557
558
        $formId = $this->getFormId();

559
560
        // Button Save at bottom of form - only if there is a button text given.
        if ($this->formSpec[F_SUBMIT_BUTTON_TEXT] !== '') {
561
562
563
564

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

565
566
            $buttonText = $this->formSpec[F_SUBMIT_BUTTON_TEXT];

567
            $htmlElement = $this->buildButtonCode('save-button', $buttonText, $buttonText, '', '', $this->formSpec[F_BUTTON_ON_CHANGE_CLASS], 'btn btn-default');
568
569
570
571
572
573
574
575

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

576
        $html .= '</div> <!--class="tab-content" -->';  //  <div class="tab-content">
577
//        $html .= '<input type="submit" value="Submit">';
578

Carsten  Rose's avatar
Carsten Rose committed
579
580
581
        $formId = $this->getFormId();
        $tabId = $this->getTabId();

Carsten  Rose's avatar
Carsten Rose committed
582
        if (0 < ($recordId = $this->store->getVar(SIP_RECORD_ID, STORE_SIP))) {
583
            $deleteUrl = $this->createDeleteUrl($this->formSpec[F_FINAL_DELETE_FORM], $recordId);
Carsten  Rose's avatar
Carsten Rose committed
584
        }
585

586
587
588
        $actionUpload = FILE_ACTION . '=' . FILE_ACTION_UPLOAD;
        $actionDelete = FILE_ACTION . '=' . FILE_ACTION_DELETE;

589
590
591
        $apiDir = API_DIR;
        $apiDeletePhp = API_DIR . '/' . API_DELETE_PHP;

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

594
        $html .= '</form>';  //  <form class="form-horizontal" ...
595
596
597
598
599
600
        $html .= <<<EOF
        <script type="text/javascript">
            $(function () {
                'use strict';
                QfqNS.Log.level = 0;

Carsten  Rose's avatar
Carsten Rose committed
601
602
603
                var qfqPage = new QfqNS.QfqPage({
                    tabsId: '$tabId',
                    formId: '$formId',
604
                    submitTo: '$apiDir/save.php',
Carsten  Rose's avatar
Carsten Rose committed
605
                    $dirtyAction
Carsten  Rose's avatar
Carsten Rose committed
606
                    deleteUrl: '$deleteUrl',
607
608
609
                    refreshUrl: '$apiDir/load.php',
                    fileUploadTo: '$apiDir/file.php?$actionUpload',
                    fileDeleteUrl: '$apiDir/file.php?$actionDelete'
Carsten  Rose's avatar
Carsten Rose committed
610
611
                });

612
               
613
                var qfqRecordList = new QfqNS.QfqRecordList('$apiDeletePhp');
Carsten  Rose's avatar
Carsten Rose committed
614
            })
615
616
         </script>
EOF;
617
        $html .= '</div>';  //  <div class="container-fluid"> === main <div class=...> around everything
618

619
620
621
622
623
        return $html;
    }

    /**
     * @param array $formElement
Carsten  Rose's avatar
Carsten Rose committed
624
625
626
     * @param       $htmlFormElementName
     * @param       $value
     *
627
     * @param array $json
628
     * @return mixed
629
630
631
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
632
     * @throws UserReportException
633
     */
634
    public function buildPill(array $formElement, $htmlFormElementName, $value, array &$json) {
635
        $html = '';
636
637
638
639
        // save parent processed FE's
        $tmpStore = $this->feSpecNative;

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

Carsten  Rose's avatar
Carsten Rose committed
643
        $html = $this->elements($this->store->getVar(SIP_RECORD_ID, STORE_SIP), FORM_ELEMENTS_NATIVE_SUBRECORD, 0, $json);
644
645
646
647

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

648
649
650
        return $html;
    }

651
    /**
Carsten  Rose's avatar
Carsten Rose committed
652
     * @param array $formElement Complete FormElement, especially some FE_WRAP
653
     * @param string $htmlElement Content to wrap.
Carsten  Rose's avatar
Carsten Rose committed
654
655
     * @param        $htmlFormElementName
     *
656
     * @return string               Wrapped $htmlElement
657
658
     * @throws CodeException
     * @throws UserFormException
659
     */
660
    public function buildRowNative(array $formElement, $htmlElement, $htmlFormElementName) {
661
        $html = '';
662
        $htmlLabel = '';
663
664
        $classHideRow = '';
        $classHideElement = '';
665

666
        if ($formElement[FE_MODE] == FE_MODE_HIDDEN) {
667
            if ($formElement[FE_FLAG_ROW_OPEN_TAG] && $formElement[FE_FLAG_ROW_CLOSE_TAG]) {
668
669
670
671
672
673
                $classHideRow = 'hidden';
            } else {
                $classHideElement = 'hidden';
            }
        }

Carsten  Rose's avatar
Carsten Rose committed
674
        // Label
675
        if ($formElement[FE_BS_LABEL_COLUMNS] > 0) {
676
677
            $addClass = ($formElement[FE_MODE] == FE_MODE_REQUIRED) ? CSS_REQUIRED : '';
            $htmlLabel = $this->buildLabel($htmlFormElementName, $formElement[FE_LABEL], $addClass);
678
        }
679

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

Carsten  Rose's avatar
Carsten Rose committed
683
        // Input
684
        $html .= $this->customWrap($formElement, $htmlElement, FE_WRAP_INPUT, $formElement[FE_BS_INPUT_COLUMNS],
685
686
            [$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);
687

Carsten  Rose's avatar
Carsten Rose committed
688
        // Note
Carsten  Rose's avatar
Carsten Rose committed
689
        $note = $formElement[FE_NOTE];
690
        $html .= $this->customWrap($formElement, $note, FE_WRAP_NOTE, $formElement[FE_BS_NOTE_COLUMNS],
691
            [$this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_START], $this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_END]], $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_NOTE);
692

693
        // Row
694
695
        $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] : '';
696
697

        $html = $this->customWrap($formElement, $html, FE_WRAP_ROW, -1, [$openTag, $closeTag], $formElement[FE_HTML_ID] . HTML_ID_EXTENSION_ROW);
698
699
700
701

        return $html;
    }

702
    /**
703
704
705
706
707
708
     * 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.
709
     *
Carsten  Rose's avatar
Carsten Rose committed
710
     * @param array $formElement Complete FormElement, especially some FE_WRAP
711
     * @param string $htmlElement Content to wrap.
Carsten  Rose's avatar
Carsten Rose committed
712
713
714
     * @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' ]
715
716
     * @param string $htmlId
     * @param string $class
Carsten  Rose's avatar
Carsten Rose committed
717
     *
718
     * @return string Wrapped $htmlElement
719
     * @throws CodeException
720
721
     * @throws \qfq\UserFormException
     */
722
    private function customWrap(array $formElement, $htmlElement, $wrapName, $bsColumns, array $wrapArray, $htmlId = '', $class = '') {
723

724
        // If $bsColumns==0: do not wrap with default.
725
726
727
728
        if ($bsColumns == 0) {
            $wrapArray[0] = '';
            $wrapArray[1] = '';
        }
729

730
        // If there is a 'per FormElement'-wrap, take it.
731
732
733
734
735
736
737
738
        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);
        }

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

744
745
746
        return $wrapArray[0] . $htmlElement . $wrapArray[1];
    }

747

748
749
750
    /**
     * @param $formElement
     * @param $elementHtml
Carsten  Rose's avatar
Carsten Rose committed
751
     *
752
753
     * @return string
     */
754
    public function buildRowPill(array $formElement, $elementHtml) {
755
756
        $html = '';

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

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

761
        $html = Support::wrapTag('<div role="tabpanel" class="tab-pane' . $active . '" id="' . $this->createAnker($formElement['id']) . '">', $html);
762
763
764
765
766
767

        $this->isFirstPill = false;

        return $html;
    }

768
    /**
769
770
     * Builds a fieldset
     *
Carsten  Rose's avatar
Carsten Rose committed
771
     * @param array $formElement
772
     * @param $elementHtml
Carsten  Rose's avatar
Carsten Rose committed
773
     * @return mixed
774
     */
775
    public function buildRowFieldset(array $formElement, $elementHtml) {
776
777
778
        $html = $elementHtml;

        return $html;
779
780
    }

781
782
783
784
785
    /**
     * Builds a templateGroup
     *
     * @param $formElement
     * @param $elementHtml
786
     * @return mixed
787
788
789
790
791
792
793
     */
    public function buildRowTemplateGroup(array $formElement, $elementHtml) {
        $html = $elementHtml;

        return $html;
    }

794
    /**
795
     * @param array $formElement
796
     * @param $elementHtml
Carsten  Rose's avatar
Carsten Rose committed
797
     *
798
     * @return string
799
     * @throws CodeException
800
     */
801
    public function buildRowSubrecord(array $formElement, $elementHtml) {
802

803
        $formElement[FE_LABEL] = Support::wrapTag("<label class='control-label'>", $formElement[FE_LABEL], true);
804
        $html = $this->wrapItem(WRAP_SETUP_ELEMENT, $this->wrapItem(WRAP_SETUP_SUBRECORD, $formElement[FE_LABEL]));
805
        $html .= $this->wrapItem(WRAP_SETUP_ELEMENT, $this->wrapItem(WRAP_SETUP_SUBRECORD, $elementHtml));
806
        $html .= $this->wrapItem(WRAP_SETUP_ELEMENT, $this->wrapItem(WRAP_SETUP_SUBRECORD, $formElement[FE_NOTE]));
807

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

811

812
        return Support::wrapTag("<span name='qfq-subrecord' $attribute>", $html);
813
    }
814
815
816
817
818
819
820


    /**
     * Builds complete 'form'.
     *
     * @param string $mode FORM_LOAD | FORM_UPDATE | FORM_SAVE
     *
821
822
     * @param bool $htmlElementNameIdZero
     * @param array $latestFeSpecNative
823
824
825
826
     * @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
827
     * @throws UserFormException
828
     * @throws UserReportException
829
830
831
832
     */
    public function process($mode, $htmlElementNameIdZero = false, $latestFeSpecNative = array()) {

        $json = array();
833
834
        $data = parent::process($mode, $htmlElementNameIdZero, $latestFeSpecNative);

835
836
837
        switch ($mode) {
            case FORM_SAVE:
            case FORM_UPDATE:
838
839
                $pillArray = OnArray::filter($this->feSpecNative, FE_TYPE, FE_TYPE_PILL);
                $this->buildPillNavigation($mode, $pillArray, $json);
840
841
842
843
844
845
846
847
                $data = array_merge($data, $json);
                break;
            default:
                break;
        }

        return $data;
    }