HelperFormElement.php 6.78 KB
Newer Older
Carsten  Rose's avatar
Carsten Rose committed
1
2
3
4
5
6
7
8
9
10
11
12
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/26/16
 * Time: 11:25 AM
 */

namespace qfq;

use qfq;
use qfq\Store;
13
use qfq\UserFormException;
Carsten  Rose's avatar
Carsten Rose committed
14
15
16
17

require_once(__DIR__ . '/../../qfq/store/Store.php');
require_once(__DIR__ . '/../../qfq/Constants.php');
require_once(__DIR__ . '/../../qfq/helper/KeyValueStringParser.php');
18
require_once(__DIR__ . '/../../qfq/exceptions/UserFormException.php');
Carsten  Rose's avatar
Carsten Rose committed
19

20
class HelperFormElement {
Carsten  Rose's avatar
Carsten Rose committed
21

22
    /**
23
24
25
     * Expand column $keyName to row array as virtual columns.
     * E.g.: [ 'id' => '1', 'name' => 'John', 'parameter' => 'detail=xId:grId\nShowEmptyAtStart=1' ] beocmes:
     *  [ 'id' => '1', 'name' => 'John', 'parameter' => 'detail=xId:grId\nShowEmptyAtStart=1', 'detail' => 'xId:grId', 'showEmptyAtStart'='1']
26
     * @param array $elements
27
     * @param string $keyName Typically F_PARAMETER or FE_PARAMETER (both: 'parameter')
28
29
     * @throws \qfq\UserFormException
     */
30
    public static function explodeParameterInArrayElements(array &$elements, $keyName) {
31
        foreach ($elements AS $key => $element) {
32
            self::explodeParameter($element, $keyName);
33
34
35
36
            $elements[$key] = $element;
        }
    }

Carsten  Rose's avatar
Carsten Rose committed
37
    /**
38
     * Take all rows from field $element[$keyName] and merge them with $element itself. '$element' grows in size.
Carsten  Rose's avatar
Carsten Rose committed
39
     *
40
41
42
43
     * @param array $element
     * @param string $keyName Typically F_PARAMETER or FE_PARAMETER (both: 'parameter')
     * @throws CodeException
     * @throws \qfq\UserFormException
Carsten  Rose's avatar
Carsten Rose committed
44
     */
45
    public static function explodeParameter(array &$element, $keyName) {
46
        // Something to explode?
47
        if (isset($element[$keyName]) && $element[$keyName] !== '') {
48
            // Explode
49
            $arr = KeyValueStringParser::parse($element[$keyName], "=", "\n");
50
51
52
53
54
            // Check if some of the exploded keys conflict with existing keys
            $checkKeys = array_keys($arr);
            foreach ($checkKeys AS $checkKey) {
                if (isset($element[$checkKey])) {
                    $store = Store::getInstance();
55
                    $store->setVar(SYSTEM_FORM_ELEMENT, Logger::formatFormElementName($element), STORE_SYSTEM);
56
                    $store->setVar(SYSTEM_FORM_ELEMENT_COLUMN, $keyName, STORE_SYSTEM);
57
                    throw new UserFormException("Found reserved keyname '$checkKey'", ERROR_RESERVED_KEY_NAME);
Carsten  Rose's avatar
Carsten Rose committed
58
59
                }
            }
60
61
62
63
64
            $element = array_merge($element, $arr);
        }
    }

    /**
65
     * Build the FE name: <field>-<record index)
Carsten  Rose's avatar
Carsten Rose committed
66
     *
67
68
     * @param string $field
     * @param string $id
69
70
     * @return string
     */
71
72
73
    public static function buildFormElementName(array $formElement, $id) {
        $field = ($formElement[FE_NAME] == '') ? $formElement[FE_ID] : $formElement[FE_NAME];

74
        return "$field" . HTML_DELIMITER_NAME . "$id";
75
76
77
78
79
80
81
82
83
84
85
86
    }

    /**
     * Build the FE id: <$formId>-<$formElementId>-<$formElementCopy>
     *
     * @param $formId
     * @param $formElementId
     * @param $recordId
     * @param $formElementCopy
     * @return string
     */
    public static function buildFormElementId($formId, $formElementId, $recordId, $formElementCopy) {
87
        return "$formId" . HTML_DELIMITER_ID . "$formElementId" . HTML_DELIMITER_ID . "$recordId" . HTML_DELIMITER_ID . "$formElementCopy";
88
89
    }

90
    /**
Carsten  Rose's avatar
Carsten Rose committed
91
92
     * Checkboxen, belonging to one element, grouped together by name: <fe>_<field>_<index>
     *
93
94
95
96
     * @param string $field
     * @param string $index
     * @return string
     */
97
98
    public static function prependFormElementNameCheckBoxMulti($field, $index) {
        return '_' . $index . '_' . $field;
99
100
    }

101
102
103
104
105
106
107
108
109
110
111
112
    /**
     * Check all FormElements if there are some with the attribute FE_RETYPE.
     * If yes: duplicate such elements and update FE_NAME, FE_LABEL, FE_NOTE.
     *
     * @param array $elements
     * @return array
     */
    public static function duplicateRetypeElements(array $elements) {
        $arr = array();

        foreach ($elements as $fe) {

113
            // adjust FE_RETYPE=1
114
115
116
117
118
119
120
121
            if (isset($fe[FE_RETYPE]) && ($fe[FE_RETYPE] == '' || $fe[FE_RETYPE] == '1')) {
                $fe[FE_RETYPE] = '1';
            }

            $arr[] = $fe;

            if (isset($fe[FE_RETYPE]) && $fe[FE_RETYPE] == '1') {

122
123
                // Reference to Source FormElement
                $fe[FE_RETYPE_SOURCE_NAME] = $fe[FE_NAME];
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

                // Create copy of FE, adjust name, label, note
                $fe[FE_NAME] .= RETYPE_FE_NAME_EXTENSION;

                if (isset($fe[FE_RETYPE_LABEL])) {
                    $fe[FE_LABEL] = $fe[FE_RETYPE_LABEL];
                    unset($fe[FE_RETYPE_LABEL]);
                }

                if (isset($fe[FE_RETYPE_NOTE])) {
                    $fe[FE_NOTE] = $fe[FE_RETYPE_NOTE];
                    unset($fe[FE_RETYPE_NOTE]);
                }

                unset($fe[FE_RETYPE]);
                $arr[] = $fe;
            }
        }

        return $arr;
    }

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
    /**
     * Copy specific attributes defined on the form to all FormElements.
     *
     * @param array $formSpec
     * @param array $feSpecNative
     * @return mixed
     */
    public static function copyAttributesToFormElements(array $formSpec, array $feSpecNative) {
        foreach ($feSpecNative as $key => $element) {
            Support::setIfNotSet($feSpecNative[$key], F_FE_DATA_PATTERN_ERROR, $formSpec[F_FE_DATA_PATTERN_ERROR]);
            Support::setIfNotSet($feSpecNative[$key], F_FE_DATA_REQUIRED_ERROR, $formSpec[F_FE_DATA_REQUIRED_ERROR]);
            Support::setIfNotSet($feSpecNative[$key], F_FE_DATA_MATCH_ERROR, $formSpec[F_FE_DATA_MATCH_ERROR]);
            Support::setIfNotSet($feSpecNative[$key], F_FE_DATA_ERROR, $formSpec[F_FE_DATA_ERROR]);
        }

        return $feSpecNative;
    }

164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

    /**
     * Set all necessary keys - subsequent 'isset()' are not necessary anymore.
     *
     * @param array $fe
     * @return array
     */
    public static function initActionFormElement(array $fe) {

        $list = [FE_TYPE, FE_SLAVE_ID, FE_SQL_VALIDATE, FE_SQL_BEFORE, FE_SQL_INSERT, FE_SQL_UPDATE, FE_SQL_DELETE,
            FE_SQL_AFTER, FE_EXPECT_RECORDS, FE_REQUIRED_LIST, FE_MESSAGE_FAIL, FE_SENDMAIL_TO, FE_SENDMAIL_CC,
            FE_SENDMAIL_BCC, FE_SENDMAIL_FROM, FE_SENDMAIL_SUBJECT, FE_SENDMAIL_REPLY_TO, FE_SENDMAIL_FLAG_AUTO_SUBMIT,
            FE_SENDMAIL_GR_ID, FE_SENDMAIL_X_ID];

        foreach ($list as $key) {
            Support::setIfNotSet($fe, $key);
        }

        return $fe;
    }

    /**
     * Set all necessary keys - subsequent 'isset()' are not necessary anymore.
     *
     * @param array $fe
     * @return array
     */
    public static function initUploadFormElement(array $fe) {

        $list = [FE_SQL_BEFORE, FE_SQL_INSERT, FE_SQL_UPDATE, FE_SQL_DELETE, FE_SQL_AFTER];

        foreach ($list as $key) {
            Support::setIfNotSet($fe, $key);
        }

        return $fe;
    }

Carsten  Rose's avatar
Carsten Rose committed
202
}