HelperFormElement.php 2.22 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
21
class HelperFormElement
{
Carsten  Rose's avatar
Carsten Rose committed
22

23
24
25
26
27
28
29
30
31
32
33
    /**
     * @param array $elements
     * @throws \qfq\UserFormException
     */
    public static function explodeParameterInArrayElements(array &$elements) {
        foreach ($elements AS $key => $element) {
            self::explodeParameter($element);
            $elements[$key] = $element;
        }
    }

Carsten  Rose's avatar
Carsten Rose committed
34
    /**
35
36
     * Take all rows from field $element['parameter'] and merge them with $element. '$element' grows in size.
     *
Carsten  Rose's avatar
Carsten Rose committed
37
     * Iterates over all FormElements in $elements.
38
     * If $element['parameter'] exist and is filled: explode the content and append them to the current array.
Carsten  Rose's avatar
Carsten Rose committed
39
40
     *
     * @param array $elements
41
     * @throws UserFormException
Carsten  Rose's avatar
Carsten Rose committed
42
     */
43
44
45
46
47
48
49
50
51
52
53
54
55
    public static function explodeParameter(array &$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);
56
                    throw new UserFormException("Found reserved keyname '$checkKey'", ERROR_RESERVED_KEY_NAME);
Carsten  Rose's avatar
Carsten Rose committed
57
58
                }
            }
59
60
61
62
63
64
65
66
67
68
69
70
            $element = array_merge($element, $arr);
        }
    }

    /**
     * @param $field
     * @param $id
     * @return string
     */
    public static function buildFormElementId($field, $id)
    {
        return ($field . ':' . $id);
71
72
    }

Carsten  Rose's avatar
Carsten Rose committed
73
}