BuildFormBootstrap.php 12.9 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\UserException;
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/UserException.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
        if (!isset($this->formSpec['class'])) {
38
39
            $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 ' . $this->getAttribute('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
116
117
118
119
120
121
122
123
    /**
     * Simlute Submit Button: http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml
     *
     * @return string
     */
    private function buildButtons() {
        $toolTipNew = 'New';
        $toolTipDelete = 'Delete';
        $buttonDelete = '';
        $buttonNew = '';
Carsten  Rose's avatar
Carsten Rose committed
124
125
126
127
128
129
130
131
132
133
134
135
136
        $buttonDebug = '';
        $recordId = $this->store->getVar(SIP_RECORD_ID, STORE_SIP);


        // Button: FormEdit
        if ($this->showDebugInfo) {
            $tooltipFormEdit = "Edit form" . PHP_EOL . PHP_EOL . OnArray::toString($this->store->getStore(STORE_SIP), ' = ', PHP_EOL, "'");
            $urlFormEdit = $this->createFormEditUrl();
            // Edit Form
            $buttonDebug = <<<BUTTON
                <div class="btn-group" role="group">
                    <a href="$urlFormEdit" id="form-edit-button" class="btn btn-default navbar-btn" title="$tooltipFormEdit"><span class="glyphicon glyphicon-wrench"></span></a>
                </div>
137
BUTTON;
Carsten  Rose's avatar
Carsten Rose committed
138
        }
139

Carsten  Rose's avatar
Carsten Rose committed
140
        // Button: Delete
141
        if (Support::findInSet(FORM_BUTTON_DELETE, $this->formSpec['showButton'])) {
Carsten  Rose's avatar
Carsten Rose committed
142
143
144
145
146
            if ($this->showDebugInfo && $recordId > 0) {
                $toolTipDelete .= PHP_EOL . "table = '" . $this->formSpec['tableName'] . "'" . PHP_EOL . "r = '" . $recordId . "'";
            }
            $buttonDeleteDisabled = ($recordId > 0) ? '' : 'disabled';

147
            $buttonDelete = <<<BUTTON
Carsten  Rose's avatar
Carsten Rose committed
148
149
150
                <div class="btn-group" role="group">
                            <button id="delete-button" type="button" class="btn btn-default navbar-btn $buttonDeleteDisabled" title="$toolTipDelete"><span class="glyphicon glyphicon-trash"></span></button>
                </div>
151
152
153
BUTTON;
        }

Carsten  Rose's avatar
Carsten Rose committed
154
        // Button: New
155
        if (Support::findInSet(FORM_BUTTON_NEW, $this->formSpec['showButton'])) {
Carsten  Rose's avatar
Carsten Rose committed
156
157
            $formNewUrl = $this->deriveNewRecordUrlFromExistingSip($toolTipNew);

158
            $buttonNew = <<<BUTTON
Carsten  Rose's avatar
Carsten Rose committed
159
160
161
                <div class="btn-group" role="group">
                            <a href="$formNewUrl" id="form-new-button" class="btn btn-default navbar-btn" title="$toolTipNew"><span class="glyphicon glyphicon-plus"></span></a>
                </div>
162
163
164
165
166
167
BUTTON;
        }

//        <div class="btn-toolbar pull-right" role="toolbar">
        $html = <<<BUTTON
        <div class="btn-toolbar" role="toolbar">
Carsten  Rose's avatar
Carsten Rose committed
168
            $buttonDebug
169
170
171
172
173
174
175
176
177
178
179
            <div class="btn-group" role="group">
                <button id="save-button" type="button" class="btn btn-default navbar-btn" title="Save"><span class="glyphicon glyphicon-ok"></span></button>
                <button id="close-button" type="button" class="btn btn-default navbar-btn" title="Close"><span class="glyphicon glyphicon-remove"></span></button>
            </div>
            $buttonDelete
            $buttonNew
        </div>
BUTTON;
        return $html;
    }

Carsten  Rose's avatar
Carsten Rose committed
180
    /**
181
     * @param $pillArray
Carsten  Rose's avatar
Carsten Rose committed
182
     * @return string
183
     * @throws UserException
Carsten  Rose's avatar
Carsten Rose committed
184
     */
185
186
187
    private function buildPillNavigation($pillArray) {
        $pillButton = '';
        $pillDropdown = '';
188
        $htmlDropdown = '';
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

        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'] === '') {
                $this->store->setVar(SYSTEM_FORM_ELEMENT, $formElement['name'] . ' / ' . $formElement['id'], STORE_SYSTEM);
                $this->store->setVar(SYSTEM_FORM_ELEMENT_COLUMN, 'name, label', STORE_SYSTEM);
                throw new UserException("Field 'name' and/or 'label' are empty", ERROR_NAME_LABEL_EMPTY);
            }

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

210
211
212
213
214
215
216
            if ($ii <= $maxVisiblePill) {
                $pillButton .= '<li role="presentation" ' . $active . '>' . $a . '</li>';
            } else {
                $pillDropdown .= '<li>' . $a . '</li>';
            }
            $active = '';
        }
217

218
219
        // Pill Dropdown necessary?
        if ($ii > $maxVisiblePill) {
220
221
            $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;
222
            $htmlDropdown = Support::wrapTag('<li role="presentation" class="dropdown">', $htmlDropdown, false);
223
        }
224

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

228
        return $htmlDropdown;
229
230
231
232
233
234
235
236
237
238
239
240
    }

    /**
     * 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
241
242
243
244
245
246
247
    /**
     * @return string
     */
    private function getTabId() {
        return 'qfqTabs';
    }

248
249
250
251
252
    /**
     * Builds the complete HTML '<form ...>'-tag
     *
     * @return string
     */
253
    public function getFormTag() {
254
255
256
257
258
259
260
261

        $attribute = $this->getFormTagAtrributes();

        $attribute['class'] = 'form-horizontal';

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

262
263
264
265
266
    /**
     * @return string
     */
    public function tail() {
        $html = '';
267
        $html .= $this->buildNewSip();
Carsten  Rose's avatar
Carsten Rose committed
268
        $deleteUrl = '';
269

270
271
        $formId = $this->getFormId();

272
273
        // TODO: bootstrap. See BuildFormTable.tail()

274
        $html .= '</div> <!--class="tab-content" -->';  //  <div class="tab-content">
275
//        $html .= '<input type="submit" value="Submit">';
276

Carsten  Rose's avatar
Carsten Rose committed
277
278
279
        $formId = $this->getFormId();
        $tabId = $this->getTabId();

Carsten  Rose's avatar
Carsten Rose committed
280
281
282
        if (0 < ($recordId = $this->store->getVar(SIP_RECORD_ID, STORE_SIP))) {
            $deleteUrl = $this->createDeleteUrl($this->formSpec['tableName'], $recordId);
        }
283

284
        $html .= '</form>';  //  <form class="form-horizontal" ...
285
286
287
288
289
290
        $html .= <<<EOF
        <script type="text/javascript">
            $(function () {
                'use strict';
                QfqNS.Log.level = 0;

Carsten  Rose's avatar
Carsten Rose committed
291

Carsten  Rose's avatar
Carsten Rose committed
292
293
294
295
296
297
298
299
                var qfqPage = new QfqNS.QfqPage({
                    tabsId: '$tabId',
                    formId: '$formId',
                    submitTo: 'typo3conf/ext/qfq/qfq/api/save.php',
                    deleteUrl: '$deleteUrl'
                });

                var qfqRecordList = new QfqNS.QfqRecordList('typo3conf/ext/qfq/qfq/api/delete.php');
Carsten  Rose's avatar
Carsten Rose committed
300
            })
301
302
         </script>
EOF;
303
        $html .= '</div>';  //  <div class="container-fluid"> === main <div class=...> around everything
304

305
306
307
308
309
310
311
312
313
314
        return $html;
    }

    /**
     * @param array $formElement
     * @param $htmlFormElementId
     * @param $value
     * @return mixed
     */
    public function buildPill(array $formElement, $htmlFormElementId, $value) {
315
        $html = '';
316
317
318
319
        // save parent processed FE's
        $tmpStore = $this->feSpecNative;

        // child FE's
320
        $sql = SQL_FORM_ELEMENT_SPECIFIC_CONTAINER;
321
        $this->feSpecNative = $this->db->sql($sql, ROW_REGULAR, ['yes', $this->formSpec["id"], 'native,container', $formElement['id']]);
322
        HelperFormElement::explodeParameterInArrayElements($this->feSpecNative);
323
324
325
326
327
        $html = $this->elements($this->store->getVar(SIP_RECORD_ID, STORE_SIP), FORM_ELEMENTS_NATIVE_SUBRECORD);

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

328
329
330
        return $html;
    }

331
332
333
334
335
    /**
     * @param $formElement
     * @param $elementHtml
     * @return string
     */
336
337
338
339
340
341
342
343
344
345
346
347
    public function buildRowNative($formElement, $elementHtml) {
        $html = '';

        $html .= $this->wrapItem(WRAP_SETUP_LABEL, $formElement['label']);
        $html .= $this->wrapItem(WRAP_SETUP_INPUT, $elementHtml);
        $html .= $this->wrapItem(WRAP_SETUP_NOTE, $formElement['note'], true);

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

        return $html;
    }

348
349
350
351
352
    /**
     * @param $formElement
     * @param $elementHtml
     * @return string
     */
353
354
355
356
357
358
359
    public function buildRowPill($formElement, $elementHtml) {
        $html = '';

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

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

360
        $html = Support::wrapTag('<div role="tabpanel" class="tab-pane' . $active . '" id="' . $this->createAnker($formElement['id']) . '">', $html);
361
362
363
364
365
366

        $this->isFirstPill = false;

        return $html;
    }

367
368
369
370
    /**
     * @param $formElement
     * @param $elementHtml
     */
371
372
373
    public function buildRowFieldset($formElement, $elementHtml) {
    }

374
375
376
377
378
    /**
     * @param $formElement
     * @param $elementHtml
     * @return string
     */
379
380
381
382
383
    public function buildRowSubrecord($formElement, $elementHtml) {
        $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']));
384
385
386

        return $html;
    }
387
}