HelperFormElement.php 1.75 KB
Newer Older
Carsten  Rose's avatar
Carsten Rose committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
48
49
50
51
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/26/16
 * Time: 11:25 AM
 */

namespace qfq;

use qfq;
use qfq\Store;
use qfq\exceptions\UserException;

require_once(__DIR__ . '/../../qfq/store/Store.php');
require_once(__DIR__ . '/../../qfq/Constants.php');
require_once(__DIR__ . '/../../qfq/helper/KeyValueStringParser.php');
require_once(__DIR__ . '/../../qfq/exceptions/UserException.php');

class HelperFormElement {

    /**
     * Iterates over all FormElements in $elements.
     * If $element['parameter'] exist and is filled: explode the content and apend them to the current FormElement Array.
     *
     * @param array $elements
     * @throws UserException
     */
    public static function explodeParameter(array &$elements) {
        // Iterate all FormElements
        foreach ($elements AS $key => $element) {
            // Something to explode?
            if (isset($element['parameter']) && $element['parameter'] !== '') {
                // Explode
                $arr = KeyValueStringParser::parse($element['parameter'], "=", "\n");
                // 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();
                        $store->setVar(SYSTEM_FORM_ELEMENT, $element['name'] . ' / ' . $element['id'], STORE_SYSTEM);
                        $store->setVar(SYSTEM_FORM_ELEMENT_COLUMN, 'parameter', STORE_SYSTEM);
                        throw new UserException("Found reserved keyname '$checkKey'");
                    }
                }
                $elements[$key] = array_merge($element, $arr);
            }
        }
    }

}