Save.php 5.55 KB
Newer Older
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
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/30/16
 * Time: 7:59 PM
 */

namespace qfq;

require_once(__DIR__ . '/../qfq/store/Store.php');
require_once(__DIR__ . '/../qfq/Constants.php');
require_once(__DIR__ . '/../qfq/Evaluate.php');
//require_once(__DIR__ . '/../qfq/exceptions/UserException.php');
//require_once(__DIR__ . '/../qfq/exceptions/CodeException.php');
//require_once(__DIR__ . '/../qfq/exceptions/DbException.php');
//require_once(__DIR__ . '/../qfq/Evaluate.php');


class Save {

    private $formSpec = array();  // copy of the loaded form
    private $feSpecAction = array(); // copy of all formElement.class='action' of the loaded form
    private $feSpecNative = array(); // copy of all formElement.class='native' of the loaded form
    /**
     * @var null|Store
     */
    private $store = null;
    private $db = null;

    private $evaluate = null;

    /**
     * @param array $formSpec
     * @param array $feSpecAction
     * @param array $feSpecNative
     */
    public function __construct(array $formSpec, array $feSpecAction, array $feSpecNative) {
        $this->formSpec = $formSpec;
        $this->feSpecAction = $feSpecAction;
        $this->feSpecNative = $feSpecNative;
        $this->store = Store::getInstance();
        $this->db = new Database();
        $this->evaluate = new Evaluate($this->store, $this->db);
    }

    /**
48
49
     * Starts save process. On succcess, returns forwardmode/page.
     *
50
51
     * @throws CodeException
     * @throws DbException
52
     * @throws UserFormException
53
54
     */
    public function process() {
55
        $rc = 0;
56
57
58
59
60
61

        if ($this->formSpec['multiMode'] !== 'none') {

            $parentRecords = $this->db->sql($this->formSpec['multiSql']);
            foreach ($parentRecords as $row) {
                $this->store->setVarArray($row, STORE_PARENT_RECORD, true);
62
                $rc = $this->elements($row['_id']);
63
64
            }
        } else {
65
            $rc = $this->elements($this->store->getVar(SIP_RECORD_ID, STORE_SIP . STORE_ZERO));
66
        }
67
68

        return $rc;
69
70
71
72
    }

    /**
     * @param $recordId
73
     * @return int   record id (in case of insert, it's different from $recordId)
74
75
     * @throws CodeException
     * @throws DbException
76
     * @throws UserFormException
77
78
     */
    public function elements($recordId) {
Carsten  Rose's avatar
Carsten Rose committed
79

80
81
82
        $newValues = array();

        $tableColumns = array_keys($this->store->getStore(STORE_TABLE_COLUMN_TYPES));
83
        $formValues = $this->store->getStore(STORE_FORM);
84
85
86

        // Iterate over all table.columns. Built an assoc array $newValues.
        foreach ($tableColumns AS $column) {
87
            // Never save a predefined 'id': autoincrement values will be given by database..
88
89
90
91
92
93
94
95
            if ($column === 'id')
                continue;

            // Get related formElement.
            $formElement = $this->getFormElementByName($column);
            if ($formElement === false)
                continue;

96
            // Preparation for Log, Debug
97
98
            $this->store->setVar(SYSTEM_FORM_ELEMENT, $formElement['name'] . ' / ' . $formElement['id'], STORE_SYSTEM);

99
100
            if (isset($formValues[$column])) {
                $newValues[$column] = $formValues[$column];
101
102
103
            }
        }

104
105
106
107
108
109
110
111
        if ($recordId == 0) {
            $rc = $this->insertRecord($this->formSpec['tableName'], $newValues);
        } else {
            $this->updateRecord($this->formSpec['tableName'], $newValues, $recordId);
            $rc = $recordId;
        }

        return $rc;
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    }

    /**
     * Get the complete FormElement for $name
     *
     * @param $name
     * @return bool|array if found the FormElement, else false.
     */
    private function getFormElementByName($name) {
        foreach ($this->feSpecNative as $formElement) {
            if ($formElement['name'] === $name)
                return $formElement;
        }
        return false;
    }

    /**
     * Insert new record in table $this->formSpec['tableName'].
     *
     * @param array $values
132
     * @return int  last insert id
133
134
135
136
137
     * @throws DbException
     */
    public function insertRecord($tableName, array $values) {

        if (count($values) === 0)
138
            return 0; // nothing to write, last insert id=0
139
140
141
142
143
144
145

        $paramList = str_repeat('?, ', count($values));
        $paramList = substr($paramList, 0, strlen($paramList) - 2);
        $columnList = '`' . implode('`, `', array_keys($values)) . '`';

        $sql = 'INSERT INTO ' . $tableName . ' ( ' . $columnList . ' ) VALUES ( ' . $paramList . ' )';

146
        $rc = $this->db->sql($sql, ROW_REGULAR, array_values($values));
147

148
        return $rc;
149
150
151
    }

    /**
152
     * @param string $tableName
153
     * @param array $values
154
     * @param int $recordId
155
     * @return bool|int     false if $values is empty, else affectedrows
156
     * @throws CodeException
157
158
159
160
161
     * @throws DbException
     */
    public function updateRecord($tableName, array $values, $recordId) {

        if (count($values) === 0)
162
            return 0; // nothing to write, 0 rows affected
163
164
165
166

        if ($recordId === 0)
            throw new CodeException('RecordId=0 - this is not possible for update.', ERROR_RECORDID_0_FORBIDDEN);

167
168
//        $paramList = str_repeat('?, ', count($values));
//        $paramList = substr($paramList, 0, strlen($paramList) - 2);
169
170
171
172

        $sql = 'UPDATE `' . $tableName . '` SET ';

        foreach ($values as $column => $value) {
173

174
175
176
177
178
179
            $sql .= '`' . $column . '` = ?, ';
        }

        $sql = substr($sql, 0, strlen($sql) - 2) . ' WHERE id = ?';
        $values[] = $recordId;

180
181
182
        $rc = $this->db->sql($sql, ROW_REGULAR, array_values($values));

        return $rc;
183
184
185
    }

}