FillStoreForm.php 7.49 KB
Newer Older
Carsten  Rose's avatar
Carsten Rose committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 3/23/16
 * Time: 1:31 PM
 */

namespace qfq;

require_once(__DIR__ . '/Store.php');
require_once(__DIR__ . '/../Database.php');
require_once(__DIR__ . '/../Constants.php');
require_once(__DIR__ . '/../helper/HelperFormElement.php');
15
require_once(__DIR__ . '/../exceptions/UserFormException.php');
Carsten  Rose's avatar
Carsten Rose committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

class FillStoreForm {

    /**
     * @var Store
     */
    private $store = null;

    /**
     * @var Database
     */
    private $db = null;

    /**
     * @var array
     */
    private $feSpecNative = array();

    /**
     *
     */
    public function __construct() {
        $this->store = Store::getInstance();
        $this->db = new Database();
        $this->feSpecNative = $this->loadFormElementsBasedOnSIP();
    }

    /**
     * Loads a minimal definition of FormElement of the form specified in SIP.
     *
     * @throws CodeException
     * @throws DbException
48
     * @throws UserFormException
Carsten  Rose's avatar
Carsten Rose committed
49
50
     */
    private function loadFormElementsBasedOnSIP() {
51
        $formName = $this->store->getVar(SIP_FORM, STORE_SIP);
Carsten  Rose's avatar
Carsten Rose committed
52

53
54
55
56
        // Preparation for Log, Debug
        $this->store->setVar(SYSTEM_FORM, $formName, STORE_SYSTEM);

        $feSpecNative = $this->db->sql(SQL_FORM_ELEMENT_SIMPLE_ALL_CONTAINER, ROW_REGULAR, [$formName]);
Carsten  Rose's avatar
Carsten Rose committed
57
58

        if (count($feSpecNative) === 0) {
59
            throw new UserFormException('Form not found or multiple forms with the same name.', ERROR_FORM_NOT_FOUND);
Carsten  Rose's avatar
Carsten Rose committed
60
61
        }

62
63
        HelperFormElement::explodeParameterInArrayElements($feSpecNative);

Carsten  Rose's avatar
Carsten Rose committed
64
65
66
67
68
69
70
71
        return $feSpecNative;
    }

    /**
     * Copies all current form parameter from STORE_CLIENT to STORE_FORM. Checks the values against FormElement
     * definition and throws an exception if check fails. FormElements.type=hidden will be taken from STORE_SIP.
     *
     * @throws CodeException
72
     * @throws UserFormException
Carsten  Rose's avatar
Carsten Rose committed
73
74
75
76
77
78
79
80
81
82
     */
    public function process() {
        $html = '';
        $newValues = array();

        $clientValues = $this->store->getStore(STORE_CLIENT);

        // Retrieve SIP vars, e.g. for HIDDEN elements.
        $sipValues = $this->store->getStore(STORE_SIP);

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
        // Copy SIP Values; not necessarily defines as FormElements.
        foreach ($sipValues as $key => $value) {
            switch ($key) {
                case SIP_SIP:
                case SIP_RECORD_ID:
                case SIP_FORM:
                case SIP_TABLE:
                case SIP_URLPARAM:
                case 'id':
                    break;
                default:
                    $newValues[$key] = $value;
                    break;
            }
        }

Carsten  Rose's avatar
Carsten Rose committed
99
100
101
102
103
104
105
        // Iterate over all formelements. Sanatize values. Built an assoc array $newValues.
        foreach ($this->feSpecNative AS $formElement) {

            // Never get a predefined 'id'
            if ($formElement['name'] === 'id')
                continue;

106
107
108
            // Preparation for Log, Debug
            $this->store->setVar(SYSTEM_FORM_ELEMENT, Logger::formatFormElementName($formElement), STORE_SYSTEM);

109
110
111
112
            // evaluate current FormElement
//            $evaluate = new Evaluate($this->store, $this->db);
//            $formElement = $evaluate->parseArray($fe, $debugStack);

Carsten  Rose's avatar
Carsten Rose committed
113
114
115
116
            // Get related formElement.
            // construct the field name used in the form
            $clientFieldName = HelperFormElement::buildFormElementId($formElement['name'], $sipValues[SIP_RECORD_ID]);

117
118
            // Some Defaults
            $formElement = Support::setFeDefaults($formElement);
Carsten  Rose's avatar
Carsten Rose committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132

            // Preparation for Log, Debug
//            $this->store->setVar(SYSTEM_FORM_ELEMENT, $formElement['name'] . ' / ' . $formElement['id'], STORE_SYSTEM);

            if ($formElement['type'] == 'hidden') {
                // Hidden elements will be transferred by SIP
                if (!isset($sipValues[$formElement['name']])) {
                    throw new CodeException("Missing the hidden field '" . $formElement['name'] . "' in SIP.", ERROR_MISSING_HIDDEN_FIELD_IN_SIP);
                }

                $newValues[$formElement['name']] = $sipValues[$formElement['name']];
                continue;
            }

133
134
135
136
137
138
            if ($formElement['mode'] === FE_MODE_REQUIRED) {
                if (!isset($clientValues[$clientFieldName]) || ($clientValues[$clientFieldName] === '')) {
                    throw new UserFormException("Missing required value.", ERROR_REQUIRED_VALUE_EMPTY);
                }
            }

139
140
            switch ($formElement['mode']) {
                case FE_MODE_REQUIRED:
141
142
                case FE_MODE_SHOW:
                    if (isset($clientValues[$clientFieldName])) {
Carsten  Rose's avatar
Carsten Rose committed
143

144
                        // SELECT MULTI or CHECKBOX MULTI: delivered as array - implode them.
145
146
147
148
149
                        if (is_array($clientValues[$clientFieldName])) {
                            // E.g. Checkboxes needs a 'HIDDEN' HTML input to detect 'unset' of values. These 'HIDDEN' element
                            //  needs to be removed, if there is at least one checkbox is checked (=submitted)
                            if (count($clientValues[$clientFieldName]) > 1)
                                array_shift($clientValues[$clientFieldName]);
Carsten  Rose's avatar
Carsten Rose committed
150

151
152
                            $clientValues[$clientFieldName] = implode(',', $clientValues[$clientFieldName]);
                        }
Carsten  Rose's avatar
Carsten Rose committed
153

154
155
156
157
158
159
160
161
162
163
164
165
                        switch ($formElement['type']) {
                            case 'date':
                            case 'datetime':
                            case 'time':
                                if ($clientValues[$clientFieldName] !== '') // do not check empty values
                                    $newValues[$formElement['name']] = $this->doDateTime($formElement, $clientValues[$clientFieldName]);
                                break;
                            default:
                                $newValues[$formElement['name']] = Sanitize::sanitize($clientValues[$clientFieldName],
                                    $formElement['checkType'], $formElement['checkPattern'], SANATIZE_EXCEPTION);
                                break;
                        }
166
167
168
169
170
171
172
173
174
                    }
                    break;

                case FE_MODE_READONLY:
                case FE_MODE_LOCK:
                case FE_MODE_DISABLED:
                    continue;
                default:
                    throw new CodeException("Unknown mode: " . $formElement['mode'], ERROR_UNKNOWN_MODE);
Carsten  Rose's avatar
Carsten Rose committed
175
176
177
178
179
180
            }
        }

        $this->store->setVarArray($newValues, STORE_FORM, true);
    }

181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
    /**
     * Check  $value as date/datime/time value and convert it to FORMAT_DATE_INTERNATIONAL.
     *
     * @param array $formElement - if not set, set $formElement['dateFormat']
     * @param string $value - date/datetime/time value in format FORMAT_DATE_INTERNATIONAL or FORMAT_DATE_GERMAN
     * @return string - checked datetime string
     * @throws UserFormException
     */
    private function doDateTime(array &$formElement, $value) {

        $regexp = Support::dateTimeRegexp($formElement['type'], $formElement['dateFormat']);

        if (1 !== preg_match('/' . $regexp . '/', $value, $matches)) {
            $placeholder = Support::getDateTimePlaceholder($formElement);
            throw new UserFormException("DateTime format not recognized: $placeholder / $value ", ERROR_DATE_TIME_FORMAT_NOT_RECOGNISED);
        }

        $showTime = $formElement['type'] == 'date' ? '0' : '1';
        $value = Support::convertDateTime($value, FORMAT_DATE_INTERNATIONAL, '1', $showTime, $formElement[FE_SHOW_SECONDS]);

        return $value;
    }
Carsten  Rose's avatar
Carsten Rose committed
203
}