BuildFormBootstrap.php 14 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
52


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

53
54
        $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>';
55

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

60
61
62
63
64
65
66
67
68
        $this->wrap[WRAP_SETUP_LABEL][WRAP_SETUP_START] = '<div class="col-md-2">';
        $this->wrap[WRAP_SETUP_LABEL][WRAP_SETUP_END] = '</div>';
        $this->wrap[WRAP_SETUP_INPUT][WRAP_SETUP_START] = '<div class="col-md-6">';
        $this->wrap[WRAP_SETUP_INPUT][WRAP_SETUP_END] = '</div>';
        $this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_START] = '<div class="col-md-4">';
        $this->wrap[WRAP_SETUP_NOTE][WRAP_SETUP_END] = '</div>';

        $this->wrap[WRAP_SETUP_SUBRECORD][WRAP_SETUP_START] = '<div class="col-md-12">';
        $this->wrap[WRAP_SETUP_SUBRECORD][WRAP_SETUP_END] = '</div>';
69
70
71
72

        $this->wrap[WRAP_SETUP_IN_FIELDSET][WRAP_SETUP_START] = '<p>';
        $this->wrap[WRAP_SETUP_IN_FIELDSET][WRAP_SETUP_END] = '</p>';

73
74
//        $this->feDivClass['radio'] = 'radio';
//        $this->feDivClass['checkbox'] = 'checkbox';
75
76
    }

Carsten  Rose's avatar
Carsten Rose committed
77
78
79
    /**
     * @return string
     */
80
81
82
83
    public function getProcessFilter() {
        return FORM_ELEMENTS_NATIVE_SUBRECORD;
    }

Carsten  Rose's avatar
Carsten Rose committed
84
85
86
    /**
     * @return string
     */
87
88
89
90
91
92
93
94
    public function doSubrecords() {
        return '';
    }

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

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

99
100
101
        $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);
102
103


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

107
108
        $html .= $this->getFormTag();

109
        $html .= '<div class="tab-content">';
110
111
112
113

        return $html;
    }

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


        // Button: FormEdit
        if ($this->showDebugInfo) {
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
            $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) {
                $toolTip .= PHP_EOL . "table = '" . $this->formSpec['tableName'] . "'" . PHP_EOL . "r = '" . $recordId . "'";
            }

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

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

            if ($this->showDebugInfo) {
                $toolTip .= PHP_EOL . "table = '" . $this->formSpec['tableName'] . "'" . PHP_EOL . "r = '" . $recordId . "'";
            }

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

Carsten  Rose's avatar
Carsten Rose committed
158
        // Button: Delete
159
        if (Support::findInSet(FORM_BUTTON_DELETE, $this->formSpec['showButton'])) {
160
161
            $toolTip = 'Delete';

Carsten  Rose's avatar
Carsten Rose committed
162
            if ($this->showDebugInfo && $recordId > 0) {
163
                $toolTip .= PHP_EOL . "table = '" . $this->formSpec['tableName'] . "'" . PHP_EOL . "r = '" . $recordId . "'";
Carsten  Rose's avatar
Carsten Rose committed
164
            }
165
            $disabled = ($recordId > 0) ? '' : 'disabled';
Carsten  Rose's avatar
Carsten Rose committed
166

167
            $buttonDelete = $this->buildButtonCode('delete-button', $toolTip, 'glyphicon-trash', $disabled);
168
169
        }

Carsten  Rose's avatar
Carsten Rose committed
170
        // Button: New
171
        if (Support::findInSet(FORM_BUTTON_NEW, $this->formSpec['showButton'])) {
172
173
            $toolTip = 'New';
            $url = $this->deriveNewRecordUrlFromExistingSip($toolTip);
Carsten  Rose's avatar
Carsten Rose committed
174

175
            $buttonNew = $this->buildButtonAnchor('form-new-button', $url, $toolTip, 'glyphicon-plus');
176
177
        }

178
179
180
181
182
183
184
        $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);

185
186
187
        return $html;
    }

188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
    /**
     * @param $id
     * @param $url
     * @param $title
     * @param $icon
     * @param string $disabled
     * @return string
     */
    private function buildButtonAnchor($id, $url, $title, $icon, $disabled = '') {
        return "<a href='$url' id='$id' class='btn btn-default navbar-btn $disabled' " . Support::doAttribute('title', $title) . "><span class='glyphicon $icon'></span></a>";
    }

    /**
     * @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
211
    /**
212
     * @param $pillArray
Carsten  Rose's avatar
Carsten Rose committed
213
     * @return string
214
     * @throws UserFormException
Carsten  Rose's avatar
Carsten Rose committed
215
     */
216
217
218
    private function buildPillNavigation($pillArray) {
        $pillButton = '';
        $pillDropdown = '';
219
        $htmlDropdown = '';
220
221
222
223
224
225
226
227
228
229
230
231
232

        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'] === '') {
233
                $this->store->setVar(SYSTEM_FORM_ELEMENT, Logger::formatFormElementName($formElement), STORE_SYSTEM);
234
                $this->store->setVar(SYSTEM_FORM_ELEMENT_COLUMN, 'name, label', STORE_SYSTEM);
235
                throw new UserFormException("Field 'name' and/or 'label' are empty", ERROR_NAME_LABEL_EMPTY);
236
237
            }

238
            // Anker for pill navigation
239
            $a = '<a href="#' . $this->createAnker($formElement['id']) . '" data-toggle="tab">' . $formElement['label'] . '</a>';
240

241
242
243
244
245
246
247
            if ($ii <= $maxVisiblePill) {
                $pillButton .= '<li role="presentation" ' . $active . '>' . $a . '</li>';
            } else {
                $pillDropdown .= '<li>' . $a . '</li>';
            }
            $active = '';
        }
248

249
250
        // Pill Dropdown necessary?
        if ($ii > $maxVisiblePill) {
251
252
            $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;
253
            $htmlDropdown = Support::wrapTag('<li role="presentation" class="dropdown">', $htmlDropdown, false);
254
        }
255

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

259
        return $htmlDropdown;
260
261
262
263
264
265
266
267
268
269
270
271
    }

    /**
     * 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
272
273
274
275
276
277
278
    /**
     * @return string
     */
    private function getTabId() {
        return 'qfqTabs';
    }

279
280
281
282
283
    /**
     * Builds the complete HTML '<form ...>'-tag
     *
     * @return string
     */
284
    public function getFormTag() {
285
286
287
288

        $attribute = $this->getFormTagAtrributes();

        $attribute['class'] = 'form-horizontal';
289
        $attribute['data-toggle'] = 'validator';
290
291
292
293

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

294
295
296
297
298
    /**
     * @return string
     */
    public function tail() {
        $html = '';
299
//        $html .= $this->buildNewSip();
Carsten  Rose's avatar
Carsten Rose committed
300
        $deleteUrl = '';
301

302
303
        $formId = $this->getFormId();

304
        $html .= '</div> <!--class="tab-content" -->';  //  <div class="tab-content">
305
//        $html .= '<input type="submit" value="Submit">';
306

Carsten  Rose's avatar
Carsten Rose committed
307
308
309
        $formId = $this->getFormId();
        $tabId = $this->getTabId();

Carsten  Rose's avatar
Carsten Rose committed
310
311
312
        if (0 < ($recordId = $this->store->getVar(SIP_RECORD_ID, STORE_SIP))) {
            $deleteUrl = $this->createDeleteUrl($this->formSpec['tableName'], $recordId);
        }
313

314
        $html .= '</form>';  //  <form class="form-horizontal" ...
315
316
317
318
319
320
        $html .= <<<EOF
        <script type="text/javascript">
            $(function () {
                'use strict';
                QfqNS.Log.level = 0;

Carsten  Rose's avatar
Carsten Rose committed
321
322
323
324
                var qfqPage = new QfqNS.QfqPage({
                    tabsId: '$tabId',
                    formId: '$formId',
                    submitTo: 'typo3conf/ext/qfq/qfq/api/save.php',
Carsten  Rose's avatar
Carsten Rose committed
325
                    deleteUrl: '$deleteUrl',
Carsten  Rose's avatar
Carsten Rose committed
326
327
                    refreshUrl: "typo3conf/ext/qfq/qfq/api/load.php",
                    fileUploadTo: 'typo3conf/ext/qfq/qfq/api/file.php'
Carsten  Rose's avatar
Carsten Rose committed
328
329
330
                });

                var qfqRecordList = new QfqNS.QfqRecordList('typo3conf/ext/qfq/qfq/api/delete.php');
Carsten  Rose's avatar
Carsten Rose committed
331
            })
332
333
         </script>
EOF;
334
        $html .= '</div>';  //  <div class="container-fluid"> === main <div class=...> around everything
335

336
337
338
339
340
341
342
343
344
        return $html;
    }

    /**
     * @param array $formElement
     * @param $htmlFormElementId
     * @param $value
     * @return mixed
     */
Carsten  Rose's avatar
Carsten Rose committed
345
    public function buildPill(array $formElement, $htmlFormElementId, $value, &$json) {
346
        $html = '';
347
348
349
350
        // save parent processed FE's
        $tmpStore = $this->feSpecNative;

        // child FE's
351
        $sql = SQL_FORM_ELEMENT_SPECIFIC_CONTAINER;
352
        $this->feSpecNative = $this->db->sql($sql, ROW_REGULAR, ['yes', $this->formSpec["id"], 'native,container', $formElement['id']]);
353
        HelperFormElement::explodeParameterInArrayElements($this->feSpecNative);
Carsten  Rose's avatar
Carsten Rose committed
354
        $html = $this->elements($this->store->getVar(SIP_RECORD_ID, STORE_SIP), FORM_ELEMENTS_NATIVE_SUBRECORD, 0, $json);
355
356
357
358

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

359
360
361
        return $html;
    }

362
    /**
363
364
     * @param array $formElement
     * @param string $htmlElement
365
366
     * @return string
     */
367
    public function buildRowNative(array $formElement, $htmlElement, $htmlFormElementId) {
368
369
        $html = '';

370
371
372
373
        $htmlLabel = $this->buildLabel($htmlFormElementId, $formElement['label']);

        $html .= $this->wrapItem(WRAP_SETUP_LABEL, $htmlLabel);
        $html .= $this->wrapItem(WRAP_SETUP_INPUT, $htmlElement);
374
375
376
377
378
379
380
        $html .= $this->wrapItem(WRAP_SETUP_NOTE, $formElement['note'], true);

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

        return $html;
    }

381
382
383
384
385
    /**
     * @param $formElement
     * @param $elementHtml
     * @return string
     */
386
    public function buildRowPill(array $formElement, $elementHtml) {
387
388
389
390
391
392
        $html = '';

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

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

393
        $html = Support::wrapTag('<div role="tabpanel" class="tab-pane' . $active . '" id="' . $this->createAnker($formElement['id']) . '">', $html);
394
395
396
397
398
399

        $this->isFirstPill = false;

        return $html;
    }

400
401
402
403
    /**
     * @param $formElement
     * @param $elementHtml
     */
404
    public function buildRowFieldset(array $formElement, $elementHtml) {
405
406
    }

407
408
409
410
411
    /**
     * @param $formElement
     * @param $elementHtml
     * @return string
     */
412
    public function buildRowSubrecord(array $formElement, $elementHtml) {
413
414
415
416
        $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']));
417
418
419

        return $html;
    }
420
}