FillStoreForm.php 4.04 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
<?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');

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
47
     * @throws UserFormException
Carsten  Rose's avatar
Carsten Rose committed
48
49
50
51
52
53
54
     */
    private function loadFormElementsBasedOnSIP() {
        $form = $this->store->getVar(SIP_FORM, STORE_SIP);

        $feSpecNative = $this->db->sql(SQL_FORM_ELEMENT_SIMPLE_ALL_CONTAINER, ROW_REGULAR, [$form]);

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

        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
66
     * @throws UserFormException
Carsten  Rose's avatar
Carsten Rose committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
     */
    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);

        // 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;

            // Get related formElement.
            // construct the field name used in the form
            $clientFieldName = HelperFormElement::buildFormElementId($formElement['name'], $sipValues[SIP_RECORD_ID]);


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

            if (isset($clientValues[$clientFieldName])) {

                // SELECT with multiple values, or Multi CHECKBOX are delivered as array: implode them
                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]);

                    $clientValues[$clientFieldName] = implode(',', $clientValues[$clientFieldName]);
                }

114
115
//                $newValues[$formElement['name']] = $this->validateValue($formElement, $clientValues[$clientFieldName]);
                $newValues[$formElement['name']] = Sanitize::sanitize($clientValues[$clientFieldName], $formElement['checkType'], $formElement['checkPattern'], SANATIZE_EXCEPTION);
Carsten  Rose's avatar
Carsten Rose committed
116
117
118
119
120
121
122
123
            }

        }

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

}