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

Marc Egger's avatar
Marc Egger committed
9
namespace IMATHUZH\Qfq\Core\Helper;
Carsten  Rose's avatar
Carsten Rose committed
10

Marc Egger's avatar
Marc Egger committed
11
use IMATHUZH\Qfq\Core\Store\Store;
12

Carsten  Rose's avatar
Carsten Rose committed
13

14
15
16
17
/**
 * Class HelperFormElement
 * @package qfq
 */
18
class HelperFormElement {
Carsten  Rose's avatar
Carsten Rose committed
19

20
    /**
21
     * Expand column $keyName to row array as virtual columns.
22
     * E.g.: [ 'id' => '1', 'name' => 'John', 'parameter' => 'detail=xId:grId\nShowEmptyAtStart=1' ] becomes:
Carsten  Rose's avatar
Carsten Rose committed
23
24
25
     *  [ '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')
Carsten  Rose's avatar
Carsten Rose committed
28
     *
Marc Egger's avatar
Marc Egger committed
29
30
31
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
32
     */
33
    public static function explodeParameterInArrayElements(array &$elements, $keyName) {
Carsten  Rose's avatar
Carsten Rose committed
34

35
        foreach ($elements AS $key => $element) {
36
            self::explodeParameter($element, $keyName);
37
38
39
40
            $elements[$key] = $element;
        }
    }

41
42

    /**
43
44
45
     * Set default values for given FormElement.
     * Hint: to copy values from Form, copyAttributesToFormElements() is more appropriate.
     *
46
     * @param array $elements
Carsten  Rose's avatar
Carsten Rose committed
47
     *
48
     * @param array $formSpec
49
50
     * @return array
     */
51
52
    public static function formElementSetDefault(array $elements, array $formSpec) {

Carsten  Rose's avatar
Carsten Rose committed
53
54
        // Do not add FE_SLAVE_ID - it's necessary to detect if a value is given or not.
        $default = [FE_SQL_BEFORE => '', FE_SQL_INSERT => '', FE_SQL_UPDATE => '', FE_SQL_DELETE => '', FE_SQL_AFTER => ''];
55

56
57
        foreach ($elements AS $key => $element) {
            $elements[$key][FE_TG_INDEX] = 0;
58
            unset($elements[$key][FE_ADMIN_NOTE]);
Carsten  Rose's avatar
Carsten Rose committed
59
//            $elements[$key][FE_DATA_REFERENCE] = '';
60
61

            $elements[$key] = array_merge($default, $elements[$key]);
62
63
        }

64

65
66
67
        return $elements;
    }

Carsten  Rose's avatar
Carsten Rose committed
68
    /**
69
     * Take all rows from field $element[$keyName] and merge them with $element itself. '$element' grows in size.
Carsten  Rose's avatar
Carsten Rose committed
70
     *
71
72
     * @param array $element
     * @param string $keyName Typically F_PARAMETER or FE_PARAMETER (both: 'parameter')
Carsten  Rose's avatar
Carsten Rose committed
73
     *
74
     * @param bool $flagAllowOverwrite
Marc Egger's avatar
Marc Egger committed
75
76
77
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
Carsten  Rose's avatar
Carsten Rose committed
78
     */
79
    public static function explodeParameter(array &$element, $keyName, $flagAllowOverwrite = false) {
Carsten  Rose's avatar
Carsten Rose committed
80

81
        // Something to explode?
82
        if (isset($element[$keyName]) && $element[$keyName] !== '') {
83
            // Explode
84
            $arr = KeyValueStringParser::parse($element[$keyName], "=", "\n");
85
86
87
88
89

            if (!$flagAllowOverwrite) {
                // Check if some of the exploded keys conflict with existing keys
                $checkKeys = array_keys($arr);
                foreach ($checkKeys AS $checkKey) {
90
                    if (!empty($element[$checkKey])) {
91
92
93
                        $store = Store::getInstance();
                        $store->setVar(SYSTEM_FORM_ELEMENT, Logger::formatFormElementName($element), STORE_SYSTEM);
                        $store->setVar(SYSTEM_FORM_ELEMENT_COLUMN, $keyName, STORE_SYSTEM);
Marc Egger's avatar
Marc Egger committed
94
                        throw new \UserFormException("Found reserved keyname '$checkKey'", ERROR_RESERVED_KEY_NAME);
95
                    }
Carsten  Rose's avatar
Carsten Rose committed
96
97
                }
            }
98
            $element = array_merge($element, $arr);
99
100
101

            // For retype elements: copy the language specific value.
            if (isset($element[FE_RETYPE_SOURCE_NAME])) {
102

103
104
105
                if (!empty($element[FE_RETYPE_LABEL])) {
                    $element[FE_LABEL] = $element[FE_RETYPE_LABEL];
                }
106

107
108
109
110
111
                if (!empty($element[FE_RETYPE_NOTE])) {
                    $element[FE_NOTE] = $element[FE_RETYPE_NOTE];
                }
            }

112
            $element[$keyName] = ''; // to not expand it a second time
113
114
115
        }
    }

116
117
118
119
120
    /**
     * Take language specific definitions and overwrite the default values.
     *
     * @param array $formSpecFeSpecNative
     *
121
     * @param $parameterLanguageFieldName
122
     * @return array
Marc Egger's avatar
Marc Egger committed
123
124
125
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
126
127
128
129
130
131
132
133
134
     */
    public static function setLanguage(array $formSpecFeSpecNative, $parameterLanguageFieldName) {

        if (is_string($parameterLanguageFieldName) && isset($formSpecFeSpecNative[$parameterLanguageFieldName])) {
            self::explodeParameter($formSpecFeSpecNative, $parameterLanguageFieldName, true);
        }

        return $formSpecFeSpecNative;
    }
Carsten  Rose's avatar
Carsten Rose committed
135

136
    /**
137
     * Build the FE name: <field>-<record index)
Carsten  Rose's avatar
Carsten Rose committed
138
     *
Carsten  Rose's avatar
Carsten Rose committed
139
     * @param array $formElement
140
     * @param string $id
Carsten  Rose's avatar
Carsten Rose committed
141
     *
142
143
     * @return string
     */
144
145
146
    public static function buildFormElementName(array $formElement, $id) {
        $field = ($formElement[FE_NAME] == '') ? $formElement[FE_ID] : $formElement[FE_NAME];

147
        return "$field" . HTML_DELIMITER_NAME . "$id";
148
149
    }

150
151
152
153
154
155
156
157
158
159
160
161
    /**
     * Build the internal FE name for an imageCut element (used only in SIP): <field>-imageCut
     *
     * @param array $formElement
     * @return string
     */
    public static function AppendFormElementNameImageCut(array $formElement) {
        $field = ($formElement[FE_NAME] == '') ? $formElement[FE_ID] : $formElement[FE_NAME];

        return "$field" . HTML_DELIMITER_NAME . FE_TYPE_IMAGE_CUT;
    }

162
163
    /**
     * Build the FE id: <$formId>-<$formElementId>-<$formElementCopy>
Carsten  Rose's avatar
Carsten Rose committed
164
     * Attention: Radio's get's an additional index count as fourth parameter (not here).
165
166
167
168
169
     *
     * @param $formId
     * @param $formElementId
     * @param $recordId
     * @param $formElementCopy
Carsten  Rose's avatar
Carsten Rose committed
170
     *
171
172
173
     * @return string
     */
    public static function buildFormElementId($formId, $formElementId, $recordId, $formElementCopy) {
174
        return "$formId" . HTML_DELIMITER_ID . "$formElementId" . HTML_DELIMITER_ID . "$recordId" . HTML_DELIMITER_ID . "$formElementCopy";
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
202
203
    /**
     * In an array for $feSpecNative, set FE_HTML_ID for all fe.class=FE_CONTAINER Elements.
     *
     * @param array $feSpecNative
     * @param $formId
     * @param $recordId
     * @param int $formElementCopy
     * @return array
     */
    public static function setFeContainerFormElementId(array $feSpecNative, $formId, $recordId, $formElementCopy = 0) {

        foreach ($feSpecNative as $key => $fe) {

            switch ($fe[FE_CLASS]) {
                case FE_CLASS_CONTAINER:
                    $feSpecNative[$key][FE_HTML_ID] = self::buildFormElementId($formId, $fe[FE_ID], $recordId, $formElementCopy);
                    break;
                default:
                    break;
            }

        }

        return $feSpecNative;
    }

204
    /**
Carsten  Rose's avatar
Carsten Rose committed
205
206
     * Checkboxen, belonging to one element, grouped together by name: <fe>_<field>_<index>
     *
207
208
     * @param string $field
     * @param string $index
Carsten  Rose's avatar
Carsten Rose committed
209
     *
210
211
     * @return string
     */
212
213
    public static function prependFormElementNameCheckBoxMulti($field, $index) {
        return '_' . $index . '_' . $field;
214
215
    }

216
217
218
219
220
    /**
     * 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
Carsten  Rose's avatar
Carsten Rose committed
221
     *
222
223
224
225
226
227
228
     * @return array
     */
    public static function duplicateRetypeElements(array $elements) {
        $arr = array();

        foreach ($elements as $fe) {

229
            // adjust FE_RETYPE=1
230
231
232
233
234
235
236
237
            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') {

238
239
                // Reference to Source FormElement
                $fe[FE_RETYPE_SOURCE_NAME] = $fe[FE_NAME];
240
241
242
243
244
245
246
247
248
249
250
251
252
253

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

254
255
                $fe[FE_TG_INDEX] = 1; // Not sure if this is helpfull in case of dynamic update - but it will make the element uniqe.

256
257
258
259
260
261
262
263
                unset($fe[FE_RETYPE]);
                $arr[] = $fe;
            }
        }

        return $arr;
    }

264
265
266
267
268
    /**
     * Copy specific attributes defined on the form to all FormElements.
     *
     * @param array $formSpec
     * @param array $feSpecNative
Carsten  Rose's avatar
Carsten Rose committed
269
     *
270
271
272
     * @return mixed
     */
    public static function copyAttributesToFormElements(array $formSpec, array $feSpecNative) {
273
274

        // Iterate over all FormElement
275
        foreach ($feSpecNative as $key => $element) {
276
277
278

            $feSpecNative[$key][F_FE_DATA_PATTERN_ERROR_SYSTEM] = $formSpec[F_FE_DATA_PATTERN_ERROR_SYSTEM];

279
280
281
282
            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]);
283
284
285
            if ($feSpecNative[$key][F_FE_LABEL_ALIGN] == F_FE_LABEL_ALIGN_DEFAULT) {
                $feSpecNative[$key][F_FE_LABEL_ALIGN] = $formSpec[F_FE_LABEL_ALIGN];
            }
286
            Support::setIfNotSet($feSpecNative[$key], F_FE_REQUIRED_POSITION, $formSpec[F_FE_REQUIRED_POSITION]);
287
288
289
290
291
        }

        return $feSpecNative;
    }

292
293
294
295
296

    /**
     * Set all necessary keys - subsequent 'isset()' are not necessary anymore.
     *
     * @param array $fe
Carsten  Rose's avatar
Carsten Rose committed
297
     *
298
299
300
301
     * @return array
     */
    public static function initActionFormElement(array $fe) {

302
        $list = [FE_TYPE, FE_SQL_VALIDATE, FE_SLAVE_ID, FE_SQL_BEFORE, FE_SQL_INSERT, FE_SQL_UPDATE, FE_SQL_DELETE,
303
304
            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,
305
306
            FE_SENDMAIL_GR_ID, FE_SENDMAIL_X_ID, FE_SENDMAIL_X_ID2, FE_SENDMAIL_X_ID3, FE_SENDMAIL_BODY_MODE,
            FE_SENDMAIL_BODY_HTML_ENTITY, FE_SENDMAIL_SUBJECT_HTML_ENTITY];
307
308
309
310
311
312
313
314
315
316
317
318

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

        return $fe;
    }

    /**
     * Set all necessary keys - subsequent 'isset()' are not necessary anymore.
     *
     * @param array $fe
Carsten  Rose's avatar
Carsten Rose committed
319
     *
320
321
322
323
324
325
326
327
328
329
330
331
332
     * @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;
    }

333
334
335
336
337
338
339
340
341
342
343
344
345
346
    /**
     * Prepare code of 'lock', 'password', 'info' to extend a FormElement.
     * The 'info' will always be added, 'lock' and 'password' only on FE with mode=show|required
     * Depending on $showInside:
     *    * true: a button is shown inside the 'input' or 'select' element.
     *    * false: an icon is shown below the FormElement.
     *
     * 'Add' means:
     *    * $rcButton contains all HTML button code. The calling function is responsible to build the correct code.
     *    * $formElement[FE_INPUT_EXTRA_BUTTON_INFO] has been wrapped with HTML-tag and HTML-id.
     *    * $formElement[FE_MODE] - for 'lock' it will be faked to 'readonly'
     *    * $formElement[FE_TYPE] - for 'password' it will be faked to 'password'
     *
     * @param array $formElement
Carsten  Rose's avatar
Carsten Rose committed
347
     * @param bool $showInline
Carsten  Rose's avatar
Carsten Rose committed
348
     *
349
     * @return array
Marc Egger's avatar
Marc Egger committed
350
351
352
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
353
     */
354
    public static function prepareExtraButton(array $formElement, $showInline) {
355

356
        $store = Store::getInstance();
357

358
359
        $infoSymbolInside = $store->getVar(SYSTEM_EXTRA_BUTTON_INFO_INLINE, STORE_SYSTEM);
        $infoSymbolOutside = $store->getVar(SYSTEM_EXTRA_BUTTON_INFO_BELOW, STORE_SYSTEM);
360

361
362
363
364
        if (SYSTEM_EXTRA_BUTTON_INFO_POSITION_BELOW == $store->getVar(SYSTEM_EXTRA_BUTTON_INFO_POSITION, STORE_SYSTEM)) {
            $showInline = false;
        }

365
        $extraButton = '';
366
        $id = $formElement[FE_HTML_ID];
367
368
369
370
371

        if (false !== strpos($formElement[FE_NAME], FE_TEMPLATE_GROUP_NAME_PATTERN)) {
            $id .= '-' . FE_TEMPLATE_GROUP_NAME_PATTERN;
        }

372
        $formElement[FE_TMP_EXTRA_BUTTON_HTML] = '';
373
374
375

        // INFO: $showinline =- TRUE ('input' elemente)
        if (isset($formElement[FE_INPUT_EXTRA_BUTTON_INFO]) && $showInline) {
376
            $extraButton .= <<<EOF
377
378
379
            <button class="btn btn-info" onclick="$('#$id-extra-info').slideToggle('swing')">
                $infoSymbolInside
            </button>
380
381
382
383
384
385
386
387
388
389
EOF;

            $value = $formElement[FE_INPUT_EXTRA_BUTTON_INFO];
            $formElement[FE_INPUT_EXTRA_BUTTON_INFO] = <<<EOF
            <div class="alert alert-info" id="$id-extra-info" style="display: none;">
                <p>$value</p>
            </div>
EOF;
        }

390
391
392
393
394
//        <span class="glyphicon glyphicon-info-sign text-info" aria-hidden="true" onclick="$('#$id-extra-info').slideToggle('swing')"></span>

        $js = " onclick=\"$('#$id-extra-info').slideToggle('swing')\" ";
        $arr = explode(' ', $infoSymbolOutside, 2);
        $infoSymbolOutside = $arr[0] . $js . $arr[1];
395

396
        // INFO: $showinline == FALSE (e.g. 'textarea' elemente)
397
        if (isset($formElement[FE_INPUT_EXTRA_BUTTON_INFO]) && !$showInline) {
398
            $class = $formElement[FE_INPUT_EXTRA_BUTTON_INFO_CLASS];
399
            $extraButton .= <<<EOF
400
            <span class="$class">$infoSymbolOutside</span>
401
402
403
404
405
406
407
408
409
410
EOF;

            $value = $formElement[FE_INPUT_EXTRA_BUTTON_INFO];
            $formElement[FE_INPUT_EXTRA_BUTTON_INFO] = <<<EOF
            <div class="alert alert-info" id="$id-extra-info" style="display: none;">
                <p>$value</p>
            </div>
EOF;
        }

411
        $skip = (!($formElement[FE_MODE] == FE_MODE_SHOW || $formElement[FE_MODE] == FE_MODE_REQUIRED || $formElement[FE_MODE] == FE_MODE_SHOW_REQUIRED));
412
413

        // LOCK
414
        if (!$skip && HelperFormElement::booleParameter($formElement[FE_INPUT_EXTRA_BUTTON_LOCK] ?? '-')) {
415
416
            $formElement[FE_MODE] = FE_MODE_READONLY;

417
            $extraButton .= <<<EOF
418
419
420
421
            <button class="btn btn-info"
                    onclick="$('#$id').prop('readonly',!$('#$id').prop('readonly'))">
                <span class="glyphicon glyphicon-lock" aria-hidden="true"></span>
            </button>
422
423
424
425
EOF;
        }

        // PASSWORD
426
427
//        if (!$skip && isset($formElement[FE_INPUT_EXTRA_BUTTON_PASSWORD])) {
        if (!$skip && HelperFormElement::booleParameter($formElement[FE_INPUT_EXTRA_BUTTON_PASSWORD] ?? '-')) {
428
429
430

            $formElement[FE_TYPE] = 'password';

431
            $extraButton .= <<<EOF
432
433
434
435
            <button class="btn btn-info"
                    onclick="$('#$id').attr('type',$('#$id').attr('type')==='password' ? 'text': 'password')">
                <span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span>
            </button>
436
437
438
EOF;
        }

439
        $formElement[FE_TMP_EXTRA_BUTTON_HTML] = Support::wrapTag('<div class="input-group-btn" style="font-size: 1em;">', $extraButton, true);
440
441
        Support::setIfNotSet($formElement, FE_INPUT_EXTRA_BUTTON_INFO);

442
443
444
        return $formElement;
    }

445
    /**
446
     * Returns $maxLength if greater than 0, else FE_TEMPLATE_GROUP_DEFAULT_MAX_LENGTH
447
448
     *
     * @param $maxLength
Carsten  Rose's avatar
Carsten Rose committed
449
     *
450
451
452
453
454
     * @return int
     */
    public static function tgGetMaxLength($maxLength) {
        return (empty($maxLength)) ? FE_TEMPLATE_GROUP_DEFAULT_MAX_LENGTH : $maxLength;
    }
455
456
457
458
459
460

    /**
     * Converts a string of '00ff00' and returns a string of '{"red": 0, "green": 255, "blue": 0}'.
     *
     * @param array $formElement
     * @return string
Marc Egger's avatar
Marc Egger committed
461
     * @throws \UserFormException
462
463
464
465
466
467
468
469
     */
    public static function penColorToHex(array $formElement) {

        if (empty($formElement[FE_DEFAULT_PEN_COLOR])) {
            return '';
        }

        if (strlen($formElement[FE_DEFAULT_PEN_COLOR]) != LENGTH_HEX_COLOR) {
Marc Egger's avatar
Marc Egger committed
470
            throw new \UserFormException("Invalid Format for " . FE_DEFAULT_PEN_COLOR .
471
472
473
474
475
476
477
478
479
                ". Expect like '#ffdd00', got: '" . $formElement[FE_DEFAULT_PEN_COLOR] . "'", ERROR_INVALID_OR_MISSING_PARAMETER);
        }

        $rgb['red'] = hexdec($formElement[FE_DEFAULT_PEN_COLOR][0] . $formElement[FE_DEFAULT_PEN_COLOR][1]);
        $rgb['green'] = hexdec($formElement[FE_DEFAULT_PEN_COLOR][2] . $formElement[FE_DEFAULT_PEN_COLOR][3]);
        $rgb['blue'] = hexdec($formElement[FE_DEFAULT_PEN_COLOR][4] . $formElement[FE_DEFAULT_PEN_COLOR][5]);

        return json_encode($rgb);
    }
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516

    /**
     * Depending on value in $requiredPosition the array $classArr will contain the CSS class to align the required mark.
     * @param $requiredPosition
     * @return array
     * @throws \UserFormException
     */
    public static function getRequiredPositionClass($requiredPosition) {
        $classArr[FE_LABEL] = '';
        $classArr[FE_TYPE] = '';
        $classArr[FE_NOTE] = '';

        switch ($requiredPosition) {
            case F_FE_REQUIRED_POSITION_LABEL_LEFT:
                $classArr[FE_LABEL] = CSS_REQUIRED_LEFT;
                break;
            case F_FE_REQUIRED_POSITION_LABEL_RIGHT:
                $classArr[FE_LABEL] = CSS_REQUIRED_RIGHT;
                break;
            case F_FE_REQUIRED_POSITION_INPUT_LEFT:
                $classArr[FE_INPUT] = CSS_REQUIRED_LEFT;
                break;
            case F_FE_REQUIRED_POSITION_INPUT_RIGHT:
                $classArr[FE_INPUT] = CSS_REQUIRED_RIGHT;
                break;
            case F_FE_REQUIRED_POSITION_NOTE_LEFT:
                $classArr[FE_NOTE] = CSS_REQUIRED_LEFT;
                break;
            case F_FE_REQUIRED_POSITION_NOTE_RIGHT:
                $classArr[FE_NOTE] = CSS_REQUIRED_RIGHT;
                break;
            default:
                throw new \UserFormException('Unkown value for ' . F_FE_REQUIRED_POSITION . ': ' . $requiredPosition, ERROR_INVALID_VALUE);
        }

        return $classArr;
    }
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534

    /**
     * Function to check if a parameter is set (no value) or 0 or 1
     * Example:
     *   a) extraButtonLock
     *   b) extraButtonLock=0
     *   c) extraButtonLock=1
     *
     * a) and c) means 'on' and b) means off
     *
     * IMPORTANT: Call this function :   HelperFormElement::booleParameter( $fe[FE_INPUT_EXTRA_BUTTON_LOCK] ?? '-' )
     *
     * @param $data
     * @return bool
     */
    public static function booleParameter($data) {
        return $data == '' || $data == '1';
    }
535
536
537
538
539

    /**
     * Calculates (estimate) number of rows needed for a pre filled textarea element.
     * Explode $value into lines. Check each line for number of textarea width rows. Count all and return this number.
     * Apply min & max settings.
540
     * If no <height> is given, return always 1 - easy way to detect single or multile line input/textarea.
541
542
543
544
545
546
547
548
549
550
     *
     * @param array $colsRows
     * @param string $value
     * @return int
     */
    public static function textareaAutoHeight(array $colsRows, $value) {
        $cnt = 0;

        // Check
        if (count($colsRows) < 3) {
551
            return (empty($colsRows[1]) ? 1 : $colsRows[1]);
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
        }

        if (empty($colsRows[0])) {
            $colsRows[0] = 80; // just a default if no number is given.
        }

        // Explode into lines
        $lines = explode(PHP_EOL, $value);

        foreach ($lines as $line) {
            $cnt += floor(strlen($line) / $colsRows[0]) + 1;
        }

        // Respect min/max.
        if ($cnt < $colsRows[1]) {
            $cnt = $colsRows[1];
        }

        if ($colsRows[2] < $cnt) {
            $cnt = $colsRows[2];
        }

        return $cnt;
    }

Carsten  Rose's avatar
Carsten Rose committed
577
}