BuildFormBootstrap.php 9.42 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\exceptions\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 {

Carsten  Rose's avatar
Carsten Rose committed
25
26
27
    /**
     *
     */
28
    public function fillWrap() {
29
30
        $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>';
31

32
33
        $this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_START] = '<div class="form-group">';
        $this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_END] = '</div>';
34

35
36
37
38
39
40
41
42
43
        $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>';
44
45
46
47

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

48
49
    }

Carsten  Rose's avatar
Carsten Rose committed
50
51
52
    /**
     * @return string
     */
53
54
55
56
    public function getProcessFilter() {
        return FORM_ELEMENTS_NATIVE_SUBRECORD;
    }

Carsten  Rose's avatar
Carsten Rose committed
57
58
59
    /**
     * @return string
     */
60
61
62
63
64
65
66
67
68
69
    public function doSubrecords() {
        return '';
    }

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

70
71
72
73
74
75
76
77
78
79
80
81
82
83
//        $html .= $this->wrapItem(WRAP_SETUP_TITLE, $this->formSpec['title'], true);
//        $html .= '<form action="?" method="post" target="_top" accept-charset="UTF-8">';


        $html .= '<div class="container-fluid">'; //Whole FORM
        $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();

        $html .= $this->wrapTag('<div class="row">', $pill . $button);

        $html .= '<form class="form-horizontal"  action="?" method="post" target="_top" accept-charset="UTF-8">';
        $html .= '<div class="tab-content">';
84
85
86
87

        return $html;
    }

Carsten  Rose's avatar
Carsten Rose committed
88
    /**
89
     * @param $pillArray
Carsten  Rose's avatar
Carsten Rose committed
90
     * @return string
91
     * @throws UserException
Carsten  Rose's avatar
Carsten Rose committed
92
     */
93
    private function buildPillNavigation($pillArray) {
94
        $html = '';
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        $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);
            }

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

117
118
119
120
121
122
123
            if ($ii <= $maxVisiblePill) {
                $pillButton .= '<li role="presentation" ' . $active . '>' . $a . '</li>';
            } else {
                $pillDropdown .= '<li>' . $a . '</li>';
            }
            $active = '';
        }
124

125
126
127
128
129
        // Pill Dropdown necessary?
        if ($ii > $maxVisiblePill) {
            $html = $this->wrapTag('<ul class="dropdown-menu">', $pillDropdown, true);
            $html .= '<a class="dropdown-toggle" data-toggle="dropdown" href="#" role="button">more <span class="caret"></span></a>';
        }
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
156
157
158
159
160
161
162
163
        $html = $this->wrapTag('<ul id="myTabs" class="nav nav-pills" role="tablist">', $pillButton . $html);
        $html = $this->wrapTag('<div class="col-md-10">', $html);

        return $html;
    }

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

    private function buildButtons() {
        $html = <<<BUTTONS
        <div class="col-md-2 ">
            <div class="btn-toolbar pull-right" role="toolbar">
                <div class="btn-group" role="group">
                    <button type="button" class="btn btn-default navbar-btn"><span class="glyphicon glyphicon-ok"></span></button>
                    <button type="button" class="btn btn-default navbar-btn"><span class="glyphicon glyphicon-remove"></span></button>
                </div>
                <div class="btn-group" role="group">
                    <button type="button" class="btn btn-default navbar-btn"><span class="glyphicon glyphicon-trash"></span></button>
                </div>
                <div class="btn-group" role="group">
                    <button type="button" class="btn btn-default navbar-btn"><span class="glyphicon glyphicon-plus"></span></button>
                </div>
            </div>
        </div>
BUTTONS;
164
165
166
167
168
169
170
171
172
        return $html;
    }

    /**
     * @return string
     */
    public function tail() {
        $html = '';

173
174
175
//        $sip = $this->store->getVar(CLIENT_SIP, STORE_CLIENT);
//        $sipName = CLIENT_SIP;
//        $html .= $this->buildHidden(array(), $sipName, $sip);
176
177
178

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

179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
        $html .= '</div> <!--class="tab-content" -->';  //  <div class="tab-content">
        $html .= '</form>';  //  <form class="form-horizontal" ...
        $html .= '</div>';  //  <div class="container-fluid">
        return $html;
    }

    /**
     * @param $htmlFormElementId
     * @param $formElement
     * @param $value
     * @return string
     */
    public function wrapElement($htmlFormElementId, $formElement, $value) {
        $html = '';

        // Construct Marshaller Name
        $buildElementFunctionName = 'build' . $this->buildElementFunctionName[$formElement['type']];

        if ($formElement['type'] === 'subrecord') {
            // subrecord in render='table' are outside the table
            $html .= $this->wrapItem(WRAP_SETUP_ELEMENT, $this->wrapItem(WRAP_SETUP_SUBRECORD, $formElement['label']));
            $html .= $this->wrapItem(WRAP_SETUP_ELEMENT, $this->wrapItem(WRAP_SETUP_SUBRECORD, $this->$buildElementFunctionName($formElement, $htmlFormElementId, $value)));
            $html .= $this->wrapItem(WRAP_SETUP_ELEMENT, $this->wrapItem(WRAP_SETUP_SUBRECORD, $formElement['note']));
        } else {
            if ($formElement['nestedInFieldSet'] === 'no') {
                $html .= $this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_START];

                $htmlElement = $this->$buildElementFunctionName($formElement, $htmlFormElementId, $value);
                if ($formElement['type'] == 'pill') {
                    $html = '<div role="tabpanel" class="tab-pane active" id="' . $this->createAnker($formElement['id']) . '">';

                    $html .= $htmlElement;
                    $html .= '</div>';
                } else {
                    $html .= $this->wrapItem(WRAP_SETUP_LABEL, $this->buildLabel($htmlFormElementId, $formElement['label']));
                    $html .= $this->wrapItem(WRAP_SETUP_INPUT, $htmlElement);
                }

                $html .= $this->wrapItem(WRAP_SETUP_NOTE, $formElement['note']);
                $html .= $this->wrap[WRAP_SETUP_ELEMENT][WRAP_SETUP_END];
            } else {
                $html .= $this->wrapItem(WRAP_SETUP_IN_FIELDSET, $this->buildLabel($htmlFormElementId, $formElement['label']));
                $html .= $this->wrapItem(WRAP_SETUP_IN_FIELDSET, $this->$buildElementFunctionName($formElement, $htmlFormElementId, $value));
                if ($formElement['note'] !== '')
                    $html .= $this->wrapItem(WRAP_SETUP_IN_FIELDSET, $formElement['note']);
            }
        }

227
228
229
        return $html;
    }

230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
    /**
     * @param array $formElement
     * @param $htmlFormElementId
     * @param $value
     * @return mixed
     */
    public function buildPill(array $formElement, $htmlFormElementId, $value) {
        // save parent processed FE's
        $tmpStore = $this->feSpecNative;


//        $html = '<div role="tabpanel" class="tab-pane active" id="' .  $this->createAnker($formElement['id']) .'">';

        // child FE's
        $sql = SQL_FORM_ELEMENT;
        $this->feSpecNative = $this->db->sql($sql, ROW_REGULAR, ['yes', $this->formSpec["id"], 'native,container', $formElement['id']]);
        HelperFormElement::explodeParameter($this->feSpecNative);
        $html = $this->elements($this->store->getVar(SIP_RECORD_ID, STORE_SIP), FORM_ELEMENTS_NATIVE_SUBRECORD);


//        $html .= '</div>';

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


        return $html;
    }


260
}