BuildFormBootstrap.php 11.3 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
37
38
39

        // Set some defaults
        if(!isset($this->formSpec['class'])) {
            $this->formSpec['class'] = 'container';
        }
40
41
    }

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


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

51
52
        $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>';
53

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

58
59
60
61
62
63
64
65
66
        $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>';
67
68
69
70

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

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

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

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

    /**
     * @return string
     */
    public function head() {
93
94
95
96
97
98
99
        // Logged in BE User will see a FormEdit Link
        $sipParamString = OnArray::toString($this->store->getStore(STORE_SIP), ':', ', ', "'");
        $formEditUrl = $this->createFormEditUrl();
        $html = "<p><a href='$formEditUrl'>Edit</a> <small>[$sipParamString]</small></p>";

        $deleteUrl = $this->createDeleteUrl($this->formSpec['tableName'], $this->store->getVar(SIP_RECORD_ID, STORE_SIP));
        $html .=  "<p><a href='$deleteUrl'>Delete</a>";
100

101
        $html .= '<div ' . $this->getAttribute('class', $this->formSpec['class'], TRUE) . '>'; // main <div class=...> around everything, Whole FORM; class="container" or class="container-fluid"
102
103
104
105
106
107

        $html .= '<div class="row hidden-xs"><div class="col-md-12"><h2>' . $this->formSpec['title'] . '</h2></div></div>'; // Form Title

        $pill = $this->buildPillNavigation(OnArray::filter($this->feSpecNative, 'type', 'pill'));
        $button = $this->buildButtons();

108
        $html .= Support::wrapTag('<div class="row">', $pill . $button);
109

110
111
        $html .= $this->getFormTag();

112
        $html .= '<div class="tab-content">';
113
114
115
116

        return $html;
    }

Carsten  Rose's avatar
Carsten Rose committed
117
    /**
118
     * @param $pillArray
Carsten  Rose's avatar
Carsten Rose committed
119
     * @return string
120
     * @throws UserException
Carsten  Rose's avatar
Carsten Rose committed
121
     */
122
    private function buildPillNavigation($pillArray) {
123
        $htmlDropdown = '';
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
        $pillButton = '';
        $pillDropdown = '';

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

144
            $a = '<a href="#' . $this->createAnker($formElement['id']) . '" data-toggle="tab">' . $formElement['label'] . '</a>';
145

146
147
148
149
150
151
152
            if ($ii <= $maxVisiblePill) {
                $pillButton .= '<li role="presentation" ' . $active . '>' . $a . '</li>';
            } else {
                $pillDropdown .= '<li>' . $a . '</li>';
            }
            $active = '';
        }
153

154
155
        // Pill Dropdown necessary?
        if ($ii > $maxVisiblePill) {
156
157
158
            $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;
            $htmlDropdown = Support::wrapTag('<li role="presentation" class="dropdown">', $htmlDropdown,false);
159
        }
160

161
162
        $htmlDropdown = Support::wrapTag('<ul id="' . $this->getTabId() . '" class="nav nav-pills" role="tablist">', $pillButton . $htmlDropdown);
        $htmlDropdown = Support::wrapTag('<div class="col-md-9">', $htmlDropdown);
163

164
        return $htmlDropdown;
165
166
167
168
169
170
171
172
173
174
175
176
    }

    /**
     * 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
177
178
179
180
181
182
183
    /**
     * @return string
     */
    private function getTabId() {
        return 'qfqTabs';
    }

184
185
186
187
188
    /**
     * Simlute Submit Button: http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml
     *
     * @return string
     */
189
    private function buildButtons() {
190
191
192
193
194
195
196
197
198
199
200

        $tmp = <<<BUTTONS
        <div class="btn-group" role="group">
             <button id="form-edit-button" type="button" class="btn btn-default navbar-btn"><span class="glyphicon glyphicon-wrench"></span></button>
        </div>
BUTTONS;
        if($this->store->getVar('SHOW_DEBUG_INFO', STORE_SYSTEM) === 'yes'){
            $formEditButton = $tmp;
        }


201
        $html = <<<BUTTONS
202
        <div class="col-md-3 ">
203
            <div class="btn-toolbar pull-right" role="toolbar">
204
                $formEditButton
205
                <div class="btn-group" role="group">
Carsten  Rose's avatar
Carsten Rose committed
206
207
                    <button id="save-button" type="button" class="btn btn-default navbar-btn"><span class="glyphicon glyphicon-ok"></span></button>
                    <button id="close-button" type="button" class="btn btn-default navbar-btn"><span class="glyphicon glyphicon-remove"></span></button>
208
209
                </div>
                <div class="btn-group" role="group">
Carsten  Rose's avatar
Carsten Rose committed
210
                    <button id="delete-button" type="button" class="btn btn-default navbar-btn"><span class="glyphicon glyphicon-trash"></span></button>
211
212
                </div>
                <div class="btn-group" role="group">
Carsten  Rose's avatar
Carsten Rose committed
213
                    <button id="new-button" type="button" class="btn btn-default navbar-btn"><span class="glyphicon glyphicon-plus"></span></button>
214
215
216
217
                </div>
            </div>
        </div>
BUTTONS;
218
219
220
        return $html;
    }

221
222
223
224
225
    /**
     * Builds the complete HTML '<form ...>'-tag
     *
     * @return string
     */
226
    public function getFormTag() {
227
228
229
230
231
232
233
234

        $attribute = $this->getFormTagAtrributes();

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

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

235
236
237
238
239
    /**
     * @return string
     */
    public function tail() {
        $html = '';
240
        $html .= $this->buildNewSip();
241

242
243
        $formId = $this->getFormId();

244
245
        // TODO: bootstrap. See BuildFormTable.tail()

246
        $html .= '</div> <!--class="tab-content" -->';  //  <div class="tab-content">
247
//        $html .= '<input type="submit" value="Submit">';
248

Carsten  Rose's avatar
Carsten Rose committed
249
250
251
        $formId = $this->getFormId();
        $tabId = $this->getTabId();

252
        $html .= '</form>';  //  <form class="form-horizontal" ...
253
254
255
256
257
258
        $html .= <<<EOF
        <script type="text/javascript">
            $(function () {
                'use strict';
                QfqNS.Log.level = 0;

Carsten  Rose's avatar
Carsten Rose committed
259

Carsten  Rose's avatar
Carsten Rose committed
260
261
262
263
264
            var qfqPage = new QfqNS.QfqPage({
                tabsId: '$tabId',
                formId: '$formId',
                submitTo: 'typo3conf/ext/qfq/qfq/api/save.php',
                deleteUrl: 'typo3conf/ext/qfq/qfq/api/delete.php'
265
            });
Carsten  Rose's avatar
Carsten Rose committed
266
            })
267
268
         </script>
EOF;
269
        $html .= '</div>';  //  <div class="container-fluid"> === main <div class=...> around everything
270

271
272
273
274
275
276
277
278
279
280
        return $html;
    }

    /**
     * @param array $formElement
     * @param $htmlFormElementId
     * @param $value
     * @return mixed
     */
    public function buildPill(array $formElement, $htmlFormElementId, $value) {
281
        $html = '';
282
283
284
285
        // save parent processed FE's
        $tmpStore = $this->feSpecNative;

        // child FE's
286
        $sql = SQL_FORM_ELEMENT_SPECIFIC_CONTAINER;
287
        $this->feSpecNative = $this->db->sql($sql, ROW_REGULAR, ['yes', $this->formSpec["id"], 'native,container', $formElement['id']]);
288
        HelperFormElement::explodeParameterInArrayElements($this->feSpecNative);
289
290
291
292
293
        $html = $this->elements($this->store->getVar(SIP_RECORD_ID, STORE_SIP), FORM_ELEMENTS_NATIVE_SUBRECORD);

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

294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
        return $html;
    }


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

    public function buildRowPill($formElement, $elementHtml) {
        $html = '';

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

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

317
        $html = Support::wrapTag('<div role="tabpanel" class="tab-pane' . $active . '" id="' . $this->createAnker($formElement['id']) . '">', $html);
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332

        $this->isFirstPill = false;

        return $html;
    }

    public function buildRowFieldset($formElement, $elementHtml) {
    }


    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']));
333
334
335
336
337

        return $html;
    }


338
}