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

namespace qfq;

use qfq;
12
use qfq\UserFormException;
13
14

require_once(__DIR__ . '/../qfq/Constants.php');
15
require_once(__DIR__ . '/../qfq/helper/OnArray.php');
16
require_once(__DIR__ . '/../qfq/AbstractBuildForm.php');
17
require_once(__DIR__ . '/../qfq/exceptions/UserFormException.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
31
32
33
34
    private $isFirstPill;

    /**
     * @param array $formSpec
     * @param array $feSpecAction
     * @param array $feSpecNative
     */
    public function __construct(array $formSpec, array $feSpecAction, array $feSpecNative) {
        parent::__construct($formSpec, $feSpecAction, $feSpecNative);
        $this->isFirstPill = true;
35
36

        // Set some defaults
37
38
39
//        if (!isset($this->formSpec['class'])) {
//            $this->formSpec['class'] = 'container';
//        }
Carsten  Rose's avatar
Carsten Rose committed
40

41
//        $this->formSpec['class'] = 'none';
42
43
    }

Carsten  Rose's avatar
Carsten Rose committed
44
45
46
    /**
     *
     */
47
    public function fillWrap() {
48
49
50
51

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

52
53
        $this->wrap[WRAP_SETUP_TITLE][WRAP_SETUP_START] = "<div class='row hidden-xs'><div class='col-md-12'><h1>";
        $this->wrap[WRAP_SETUP_TITLE][WRAP_SETUP_END] = "</h1></div></div>";
54

55
        // Element: Label + Input + Note
56
57
        $this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_START] = "<div class='form-group'>";
        $this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_END] = "</div>";
58

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

62
63
        $this->wrap[WRAP_SETUP_IN_FIELDSET][WRAP_SETUP_START] = "<p>";
        $this->wrap[WRAP_SETUP_IN_FIELDSET][WRAP_SETUP_END] = "</p>";
64

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

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    /**
     * @param $label
     * @param $input
     * @param $note
     */
    public function fillWrapLabelInputNote($label, $input, $note) {
        $this->wrap[WRAP_SETUP_LABEL][WRAP_SETUP_START] = "<div class='col-md-$label'>";
        $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>";
        $this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_START] = "<div class='col-md-$note'>";
        $this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_END] = "</div>";

    }

Carsten  Rose's avatar
Carsten Rose committed
84
85
86
    /**
     * @return string
     */
87
88
89
90
    public function getProcessFilter() {
        return FORM_ELEMENTS_NATIVE_SUBRECORD;
    }

Carsten  Rose's avatar
Carsten Rose committed
91
92
93
    /**
     * @return string
     */
94
95
96
97
98
99
100
101
    public function doSubrecords() {
        return '';
    }

    /**
     * @return string
     */
    public function head() {
102
        $html = '';
103

104
        $html .= '<div ' . Support::doAttribute('class', $this->formSpec['class'], TRUE) . '>'; // main <div class=...> around everything, Whole FORM; class="container" or class="container-fluid"
105

106
107
108
        $title = Support::wrapTag('<div class="hidden-xs col-sm-6 col-md-8">', Support::wrapTag('<h3>', $this->formSpec['title']));
        $button = Support::wrapTag('<div class="col-xs-12 col-sm-6 col-md-4">', $this->buildButtons());
        $html .= Support::wrapTag('<div class="row">', $title . $button);
109
110


111
112
        $pill = $this->buildPillNavigation(OnArray::filter($this->feSpecNative, 'type', 'pill'));
        $html .= Support::wrapTag('<div class="row">', $pill);
113

114
115
        $html .= $this->getFormTag();

116
        $html .= '<div class="tab-content">';
117
118
119
120

        return $html;
    }

121
    /**
122
     * Simulate Submit Button: http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml
123
124
125
126
127
     *
     * @return string
     */
    private function buildButtons() {
        $buttonNew = '';
128
129
130
131
        $buttonDelete = '';
        $buttonClose = '';
        $buttonSave = '';
        $buttonEditForm = '';
Carsten  Rose's avatar
Carsten Rose committed
132
133
134
135
136
        $recordId = $this->store->getVar(SIP_RECORD_ID, STORE_SIP);


        // Button: FormEdit
        if ($this->showDebugInfo) {
137
138
139
140
141
142
143
144
145
146
147
            $toolTip = "Edit form" . PHP_EOL . PHP_EOL . OnArray::toString($this->store->getStore(STORE_SIP), ' = ', PHP_EOL, "'");
            $url = $this->createFormEditUrl();

            $buttonEditForm = $this->buildButtonAnchor('form-edit-button', $url, $toolTip, 'glyphicon-wrench');
        }

        // Button: Save
        if (Support::findInSet(FORM_BUTTON_SAVE, $this->formSpec['showButton'])) {
            $toolTip = 'Save';

            if ($this->showDebugInfo) {
148
                $toolTip .= PHP_EOL . "table = '" . $this->formSpec[F_TABLE_NAME] . "'" . PHP_EOL . "r = '" . $recordId . "'";
149
150
151
152
153
154
155
156
157
158
            }

            $buttonSave = $this->buildButtonCode('save-button', $toolTip, 'glyphicon-ok');
        }

        // Button: Close
        if (Support::findInSet(FORM_BUTTON_CLOSE, $this->formSpec['showButton'])) {
            $toolTip = 'Close';

            if ($this->showDebugInfo) {
159
                $toolTip .= PHP_EOL . "table = '" . $this->formSpec[F_TABLE_NAME] . "'" . PHP_EOL . "r = '" . $recordId . "'";
160
161
162
            }

            $buttonClose = $this->buildButtonCode('close-button', 'Close', 'glyphicon-remove');
Carsten  Rose's avatar
Carsten Rose committed
163
        }
164

Carsten  Rose's avatar
Carsten Rose committed
165
        // Button: Delete
166
        if (Support::findInSet(FORM_BUTTON_DELETE, $this->formSpec['showButton'])) {
167
168
            $toolTip = 'Delete';

Carsten  Rose's avatar
Carsten Rose committed
169
            if ($this->showDebugInfo && $recordId > 0) {
170
                $toolTip .= PHP_EOL . "table = '" . $this->formSpec[F_TABLE_NAME] . "'" . PHP_EOL . "r = '" . $recordId . "'";
Carsten  Rose's avatar
Carsten Rose committed
171
            }
172
            $disabled = ($recordId > 0) ? '' : 'disabled';
Carsten  Rose's avatar
Carsten Rose committed
173

174
            $buttonDelete = $this->buildButtonCode('delete-button', $toolTip, 'glyphicon-trash', $disabled);
175
176
        }

Carsten  Rose's avatar
Carsten Rose committed
177
        // Button: New
178
        if (Support::findInSet(FORM_BUTTON_NEW, $this->formSpec['showButton'])) {
179
180
            $toolTip = 'New';
            $url = $this->deriveNewRecordUrlFromExistingSip($toolTip);
Carsten  Rose's avatar
Carsten Rose committed
181

182
            $buttonNew = $this->buildButtonAnchor('form-new-button', $url, $toolTip, 'glyphicon-plus');
183
184
        }

185
186
187
188
189
190
191
        $html = Support::wrapTag('<div class="btn-group" role="group">', $buttonEditForm);
        $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);

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

192
193
194
        return $html;
    }

195
196
197
198
199
200
201
202
203
    /**
     * @param $id
     * @param $url
     * @param $title
     * @param $icon
     * @param string $disabled
     * @return string
     */
    private function buildButtonAnchor($id, $url, $title, $icon, $disabled = '') {
204
        return "<a " . Support::doAttribute('href', $url) . " id='$id' class='btn btn-default navbar-btn $disabled' " . Support::doAttribute('title', $title) . "><span class='glyphicon $icon'></span></a>";
205
206
207
208
209
210
211
212
213
214
215
216
217
    }

    /**
     * @param $id
     * @param $title
     * @param $icon
     * @param string $disabled
     * @return string
     */
    private function buildButtonCode($id, $title, $icon, $disabled = '') {
        return "<button id='$id' type='button' class='btn btn-default navbar-btn $disabled' " . Support::doAttribute('title', $title) . "><span class='glyphicon $icon'></span></button>";
    }

Carsten  Rose's avatar
Carsten Rose committed
218
    /**
219
     * @param $pillArray
Carsten  Rose's avatar
Carsten Rose committed
220
     * @return string
221
     * @throws UserFormException
Carsten  Rose's avatar
Carsten Rose committed
222
     */
223
224
225
    private function buildPillNavigation($pillArray) {
        $pillButton = '';
        $pillDropdown = '';
226
        $htmlDropdown = '';
227
228
229
230
231
232
233
234
235
236
237
238
239

        if ($pillArray == null)
            return '';

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

        // Iterate over all 'pill'
        $ii = 0;
        $active = 'class="active"';
        foreach ($pillArray as $formElement) {
            $ii++;

            if ($formElement['name'] === '' || $formElement['label'] === '') {
240
                $this->store->setVar(SYSTEM_FORM_ELEMENT, Logger::formatFormElementName($formElement), STORE_SYSTEM);
241
                $this->store->setVar(SYSTEM_FORM_ELEMENT_COLUMN, 'name, label', STORE_SYSTEM);
242
                throw new UserFormException("Field 'name' and/or 'label' are empty", ERROR_NAME_LABEL_EMPTY);
243
244
            }

245
            // Anker for pill navigation
246
            $a = '<a ' . Support::doAttribute('href', '#' . $this->createAnker($formElement['id'])) . ' data-toggle="tab">' . $formElement['label'] . '</a>';
247

248
249
250
251
252
253
254
            if ($ii <= $maxVisiblePill) {
                $pillButton .= '<li role="presentation" ' . $active . '>' . $a . '</li>';
            } else {
                $pillDropdown .= '<li>' . $a . '</li>';
            }
            $active = '';
        }
255

256
257
        // Pill Dropdown necessary?
        if ($ii > $maxVisiblePill) {
258
259
            $htmlDropdown = Support::wrapTag('<ul class="dropdown-menu">', $pillDropdown, true);
            $htmlDropdown = '<a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button">more <span class="caret"></span></a>' . $htmlDropdown;
260
            $htmlDropdown = Support::wrapTag('<li role="presentation" class="dropdown">', $htmlDropdown, false);
261
        }
262

263
        $htmlDropdown = Support::wrapTag('<ul id="' . $this->getTabId() . '" class="nav nav-pills" role="tablist">', $pillButton . $htmlDropdown);
264
        $htmlDropdown = Support::wrapTag('<div class="col-md-12">', $htmlDropdown);
265

266
        return $htmlDropdown;
267
268
269
270
271
272
273
274
275
276
277
278
    }

    /**
     * Create an identifier for the pill navigation menu
     *
     * @param $id
     * @return string
     */
    private function createAnker($id) {
        return $this->formSpec['name'] . '_' . $id;
    }

Carsten  Rose's avatar
Carsten Rose committed
279
280
281
282
283
284
285
    /**
     * @return string
     */
    private function getTabId() {
        return 'qfqTabs';
    }

286
287
288
289
290
    /**
     * Builds the complete HTML '<form ...>'-tag
     *
     * @return string
     */
291
    public function getFormTag() {
292
293
294
295

        $attribute = $this->getFormTagAtrributes();

        $attribute['class'] = 'form-horizontal';
296
        $attribute['data-toggle'] = 'validator';
297
298
299
300

        return '<form ' . OnArray::toString($attribute, '=', ' ', "'") . '>';
    }

301
302
303
304
305
    /**
     * @return string
     */
    public function tail() {
        $html = '';
306
//        $html .= $this->buildNewSip();
Carsten  Rose's avatar
Carsten Rose committed
307
        $deleteUrl = '';
308

309
310
        $formId = $this->getFormId();

311
        $html .= '</div> <!--class="tab-content" -->';  //  <div class="tab-content">
312
//        $html .= '<input type="submit" value="Submit">';
313

Carsten  Rose's avatar
Carsten Rose committed
314
315
316
        $formId = $this->getFormId();
        $tabId = $this->getTabId();

Carsten  Rose's avatar
Carsten Rose committed
317
        if (0 < ($recordId = $this->store->getVar(SIP_RECORD_ID, STORE_SIP))) {
318
            $deleteUrl = $this->createDeleteUrl($this->formSpec[F_TABLE_NAME], $recordId);
Carsten  Rose's avatar
Carsten Rose committed
319
        }
320

321
322
323
        $actionUpload = FILE_ACTION . '=' . FILE_ACTION_UPLOAD;
        $actionDelete = FILE_ACTION . '=' . FILE_ACTION_DELETE;

324
        $html .= '</form>';  //  <form class="form-horizontal" ...
325
326
327
328
329
330
        $html .= <<<EOF
        <script type="text/javascript">
            $(function () {
                'use strict';
                QfqNS.Log.level = 0;

Carsten  Rose's avatar
Carsten Rose committed
331
332
333
334
                var qfqPage = new QfqNS.QfqPage({
                    tabsId: '$tabId',
                    formId: '$formId',
                    submitTo: 'typo3conf/ext/qfq/qfq/api/save.php',
Carsten  Rose's avatar
Carsten Rose committed
335
                    deleteUrl: '$deleteUrl',
Carsten  Rose's avatar
Carsten Rose committed
336
                    refreshUrl: "typo3conf/ext/qfq/qfq/api/load.php",
337
338
                    fileUploadTo: 'typo3conf/ext/qfq/qfq/api/file.php?$actionUpload',
                    fileDeleteUrl: 'typo3conf/ext/qfq/qfq/api/file.php?$actionDelete'
Carsten  Rose's avatar
Carsten Rose committed
339
340
341
                });

                var qfqRecordList = new QfqNS.QfqRecordList('typo3conf/ext/qfq/qfq/api/delete.php');
Carsten  Rose's avatar
Carsten Rose committed
342
            })
343
344
         </script>
EOF;
345
        $html .= '</div>';  //  <div class="container-fluid"> === main <div class=...> around everything
346

347
348
349
350
351
352
353
354
355
        return $html;
    }

    /**
     * @param array $formElement
     * @param $htmlFormElementId
     * @param $value
     * @return mixed
     */
Carsten  Rose's avatar
Carsten Rose committed
356
    public function buildPill(array $formElement, $htmlFormElementId, $value, &$json) {
357
        $html = '';
358
359
360
361
        // save parent processed FE's
        $tmpStore = $this->feSpecNative;

        // child FE's
362
        $sql = SQL_FORM_ELEMENT_SPECIFIC_CONTAINER;
363
        $this->feSpecNative = $this->db->sql($sql, ROW_REGULAR, ['yes', $this->formSpec["id"], 'native,container', $formElement['id']]);
364
        HelperFormElement::explodeParameterInArrayElements($this->feSpecNative);
Carsten  Rose's avatar
Carsten Rose committed
365
        $html = $this->elements($this->store->getVar(SIP_RECORD_ID, STORE_SIP), FORM_ELEMENTS_NATIVE_SUBRECORD, 0, $json);
366
367
368
369

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

370
371
372
        return $html;
    }

373
    /**
374
375
     * @param array $formElement
     * @param string $htmlElement
376
377
     * @return string
     */
378
    public function buildRowNative(array $formElement, $htmlElement, $htmlFormElementId) {
379
380
        $html = '';

381
382
383
384
        $htmlLabel = $this->buildLabel($htmlFormElementId, $formElement['label']);

        $html .= $this->wrapItem(WRAP_SETUP_LABEL, $htmlLabel);
        $html .= $this->wrapItem(WRAP_SETUP_INPUT, $htmlElement);
385
386
387
388
389
390
391
        $html .= $this->wrapItem(WRAP_SETUP_NOTE, $formElement['note'], true);

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

        return $html;
    }

392
393
394
395
396
    /**
     * @param $formElement
     * @param $elementHtml
     * @return string
     */
397
    public function buildRowPill(array $formElement, $elementHtml) {
398
399
400
401
402
403
        $html = '';

        $html .= $this->wrapItem(WRAP_SETUP_INPUT, $elementHtml);

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

404
        $html = Support::wrapTag('<div role="tabpanel" class="tab-pane' . $active . '" id="' . $this->createAnker($formElement['id']) . '">', $html);
405
406
407
408
409
410

        $this->isFirstPill = false;

        return $html;
    }

411
412
413
414
    /**
     * @param $formElement
     * @param $elementHtml
     */
415
    public function buildRowFieldset(array $formElement, $elementHtml) {
416
417
    }

418
419
420
421
422
    /**
     * @param $formElement
     * @param $elementHtml
     * @return string
     */
423
    public function buildRowSubrecord(array $formElement, $elementHtml) {
424
425
426
427
        $html = '';
        $html .= $this->wrapItem(WRAP_SETUP_ELEMENT, $this->wrapItem(WRAP_SETUP_SUBRECORD, $formElement['label']));
        $html .= $this->wrapItem(WRAP_SETUP_ELEMENT, $this->wrapItem(WRAP_SETUP_SUBRECORD, $elementHtml));
        $html .= $this->wrapItem(WRAP_SETUP_ELEMENT, $this->wrapItem(WRAP_SETUP_SUBRECORD, $formElement['note']));
428
429
430

        return $html;
    }
431
}