FormAction.php 15.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 5/29/16
 * Time: 5:24 PM
 */

namespace qfq;

require_once(__DIR__ . '/../Constants.php');
require_once(__DIR__ . '/../Database.php');
require_once(__DIR__ . '/../store/Store.php');
require_once(__DIR__ . '/../Evaluate.php');

/**
 * Class formAction
 * @package qfq
 */
20
class FormAction {
21
22

//    private $feSpecNative = array(); // copy of all formElement.class='native' of the loaded form
23
24
25
26
27
    /**
     * @var Evaluate instantiated class
     */
    protected $evaluate = null;  // copy of the loaded form
    private $formSpec = array();
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    private $primaryTableName = '';
    /**
     * @var Database
     */
    private $db = null;
    /**
     * @var Store
     */
    private $store = null;

    /**
     * @param array $formSpec
     * @param Database $db
     * @param bool|false $phpUnit
     */
    public function __construct(array $formSpec, Database $db, $phpUnit = false) {
        $this->formSpec = $formSpec;
45
        $this->primaryTableName = Support::setIfNotSet($formSpec, F_TABLE_NAME);
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

        $this->db = $db;

        $this->store = Store::getInstance('', $phpUnit);

        $this->evaluate = new Evaluate($this->store, $this->db);

    }

    /**
     * @param integer $recordId
     * @param array $feSpecAction
     * @param string $feTypeList
     *         On FormLoad: FE_TYPE_BEFORE_LOAD, FE_TYPE_AFTER_LOAD
     *         Before Save: FE_TYPE_BEFORE_SAVE, FE_TYPE_BEFORE_INSERT, FE_TYPE_BEFORE_UPDATE, FE_TYPE_BEFORE_DELETE
     *         After Save: FE_TYPE_AFTER_SAVE, FE_TYPE_AFTER_INSERT, FE_TYPE_AFTER_UPDATE, FE_TYPE_AFTER_DELETE
     * @throws CodeException
     * @throws DbException
     * @throws UserFormException
     */
    public function elements($recordId, array $feSpecAction, $feTypeList) {

        // Iterate over all Action FormElements
        foreach ($feSpecAction as $fe) {

71
72
            $fe = $this->initActionFormElement($fe);

73
74
75
76
77
            if (false === Support::findInSet($fe[FE_TYPE], $feTypeList)) {
                continue;
            }

            if ($fe[FE_TYPE] !== FE_TYPE_BEFORE_LOAD && $fe[FE_TYPE] !== FE_TYPE_AFTER_LOAD) {
78
                // Always work on recent data: previous actions might have modified the data.
79
80
81
                $this->fillStoreRecord($this->primaryTableName, $recordId);
            }

82
83
84
85
            if (!$this->checkRequiredList($fe)) {
                continue;
            }

86
87
88
89
90
91
92
93
94
            // Preparation for Log, Debug
            $this->store->setVar(SYSTEM_FORM_ELEMENT, Logger::formatFormElementName($fe), STORE_SYSTEM);

            $this->validate($fe);

            $this->doSlave($fe, $recordId);
        }
    }

95
96
97
98
99
100
101
102
103
104
105
106
107
108
    /**
     * Set all necessary keys
     *
     * @param array $fe
     * @return array
     */
    private function initActionFormElement(array $fe) {
        $list = [FE_TYPE, FE_SLAVE_ID, FE_SQL_VALIDATE, FE_SQL_INSERT, FE_SQL_UPDATE, FE_SQL_DELETE, FE_EXPECT_RECORDS, FE_REQUIRED_LIST, FE_MESSAGE_FAIL];
        foreach ($list as $key) {
            Support::setIfNotSet($fe, $key);
        }
        return $fe;
    }

109
    /**
110
111
     * @param $table
     * @param $recordId
112
     * @throws CodeException
113
     * @throws DbException
114
115
     * @throws UserFormException
     */
116
    private function fillStoreRecord($table, $recordId) {
117

118
119
        if (!is_string($table) || $table === '') {
            throw new UserFormException("");
120
        }
121
122
123
        if ($recordId !== false && $recordId > 0) {
            $record = $this->db->sql("SELECT * FROM $table WHERE id = ?", ROW_EXPECT_1, [$recordId]);
            $this->store->setVarArray($record, STORE_RECORD, true);
124
        }
125
    }
126

127
128
129
130
131
132
133
134
135
    /**
     * Process all FormElements given in the `requiredList` identified be their name.
     * If none is empty in STORE_FORM return true, else false.
     * If none FormElement is specified, return true.
     *
     * @param array $fe
     * @return bool  true if none FE is specified or all specified are non empty.
     */
    private function checkRequiredList(array $fe) {
136

137
138
        if (!isset($fe[FE_REQUIRED_LIST]) || $fe[FE_REQUIRED_LIST] === '') {
            return true;
139
140
        }

141
142
143
144
145
146
147
148
149
        $arr = explode(',', $fe[FE_REQUIRED_LIST]);
        foreach ($arr as $key) {

            $key = trim($key);
            $val = $this->store->getVar($key, STORE_FORM);

            if ($val === false || $val === '' || $val === '0') {
                return false;
            }
150
151
        }

152
        return true;
153
154
155
156
157
158
    }

    /**
     * If there is a query defined in fe.parameter.FE_SQL_VALIDATE: fire them.
     * Count the selected records and compare them with fe.parameter.FE_EXPECT_RECORDS.
     * If match: everything is fine, do nothing.
159
     * Else throw UserFormException with error message of fe.parameter.FE_MESSAGE_FAIL
160
161
162
163
164
165
166
     *
     * @param array $fe
     * @throws UserFormException
     */
    private function validate(array $fe) {

        // Is there something to check?
167
        if ($fe[FE_SQL_VALIDATE] === '') {
168
169
170
            return;
        }

171
        $expect = $this->evaluate->parse($fe[FE_EXPECT_RECORDS]);
172

173
        if ($fe[FE_MESSAGE_FAIL] === '') {
174
175
176
177
178
179
180
181
182
            throw new UserFormException("Missing error message. Column: " . FE_MESSAGE_FAIL, ERROR_MISSING_MESSAGE_FAIL);
        }

        // Do the check
        $result = $this->evaluate->parse($fe[FE_SQL_VALIDATE]);
        if (!is_array($result)) {
            throw new UserFormException("Expected an array for '" . FE_SQL_VALIDATE . "', got a scalar. Please check for {{!...", ERROR_EXPECTED_ARRAY);
        }

183
184
185
186
187
188
        // If there is at least one record count given, who matches: return 'check succeeded'
        $countRecordsArr = explode(',', $expect);
        foreach ($countRecordsArr AS $count) {
            if (count($result) == $count) {
                return; // check succesfully passed
            }
189
190
        }

191
        $msg = $this->evaluate->parse($fe[FE_MESSAGE_FAIL]); // Replace possible dynamic parts
192
193
194
195
196

        // Throw user defineable error message
        throw new UserFormException($msg, ERROR_REPORT_FAILED_ACTION);
    }

197
198
199
200
201
202
203
204
205
206
207
    /**
     * Create the slave record. First try to evaluate a slaveId. Depending if the slaveId > 0 choose `sqlUpdate` or `sqlInsert`
     *
     * @param array $fe
     * @return int
     * @throws CodeException
     * @throws UserFormException
     */
    private function doSlave(array $fe, $recordId) {

        // Get the slaveId
208
        $slaveId = $this->evaluate->parse($fe[FE_SLAVE_ID]);
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224

        if ($slaveId === '' && $fe[FE_NAME] !== '') {
            // if the current action element has the same name as a real master record column: take that value as an id
            $slaveId = $this->store->getVar($fe[FE_NAME], STORE_RECORD);
        }

        if ($slaveId === '' || $slaveId === false) {
            $slaveId = 0;
        }

        // Store the slaveId: it's used and replaced in the update statement.
        $this->store->setVar(ACTION_KEYWORD_SLAVE_ID, $slaveId, STORE_VAR, true);

        // Fire slave query
        if ($slaveId == 0) {
            $slaveId = $this->evaluate->parse($fe[FE_SQL_INSERT]);
225
226
            // Store the slaveId: it's used and replaced in the update statement.
            $this->store->setVar(ACTION_KEYWORD_SLAVE_ID, $slaveId, STORE_VAR, true);
227
228
229
230
231
232
233
234
235
236
        } else {
            $this->evaluate->parse($fe[FE_SQL_UPDATE]);
        }

        // Check if there is a column with the same name as the 'action'-FormElement.
        if (false !== $this->store->getVar($fe[FE_NAME], STORE_RECORD)) {
            // After an insert or update, update the (new) slave id to the master record.
            $this->db->sql("UPDATE " . $this->primaryTableName . " SET " . $fe[FE_NAME] . " = $slaveId WHERE id = ? LIMIT 1", ROW_REGULAR, [$recordId]);
        }

237
238
239
        // If given fire a delete query
        $this->evaluate->parse($fe[FE_SQL_DELETE]);

240
241
        return $slaveId;
    }
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
}
//
///********************************************************
// * doAddNUpdate
// * RC: TRUE     ok
// * FALSE,$err bei Fehler
// *********************************************************/
//function doAddNUpdate($formId, $masterId, $tableName, &$err) {
//    global $FeId;
//
//    if ($this->DebugLevel > 3) t3lib_div::debug("doAddNUpdate");
//
//    // Selektiere alle 'addNupdate'-DS des aktuellen Formulars
//    $sql = "SELECT * FROM form_element WHERE form_id=" . $formId . " AND typ='addNupdate' AND active='Yes' ORDER BY ord";
//    if ($this->DebugLevel > 3) t3lib_div::debug($sql);
//    if (!($formDs = mysql(MATH_DB_NAME, $sql))) return ($this->buildMySQLErrMsg($err, $sql, __FILE__, __LINE__));
//
//    // Falls nix zu tun ist (kein DS vorhanden), gleich zurueck.
//    if (mysql_num_rows($formDs) == 0) return (TRUE);
//
//    // Durchlaufe alle 'addNupdate' DS
//    while ($formularDs = mysql_fetch_array($formDs, MYSQL_ASSOC)) {
//
//        $FeId = $formularDs['id']; // Just for logging
//
//        if ($this->DebugLevel > 3) t3lib_div::debug($formularDs);
//
//        // Lese den zuvor gespeicherten Master DS
//        // Der Master DS sollte nach jedem AddNUpdate neu gelesen werden, evtl. wurde etwas eingetragen
//
//        $masterDs = $this->doQuerySingle("SELECT * FROM " . $tableName . " WHERE id=" . $masterId, $err);
//        if ($err) return (FALSE);
//
//        // Check ob addNUpadte ueberhaupt ausgefuehrt werden soll: Ist 'param' gesetzt ?
//        //	 JA: dann auswerten ob die in 'param' aufgefuehrten Formularfelder gefuellt sind
//        //			 Wenn nein, naechsten addNUpdate DS bearbeiten
//        //	 NEIN: normal weiter machen
//        if ($formularDs["param"]) {
//            $arr = explode(",", $formularDs["param"]);
//            $t = TRUE;
//            foreach ($arr as $elem) {
//                if ($GLOBALS[HTTP_POST_VARS][FRM . $elem]) {
//                    $t = FALSE;
//                    break; //foreach
//                }
//            }
//            if ($t) {
//                if ($this->DebugLevel > 3) t3lib_div::debug("doAddNUpdate() nicht ausfuehren, da param gesetzt ist und angegebene Felder leer sind.");
//                continue; //while
//            }
//        }
//
//        # Falls in 'value' ein select Statement angegeben ist, dieses ausführen.
//        # Es sollte 0 oder 1 DS gefunden werden.
//        # Die Spalte 'id' muss vorhanden sein und diese die slaveID angeben.
//        if ($formularDs["value"]) {
//            $sql = $this->substituteAssoc($formularDs["value"], $masterDs);
//            $slaveDs = $this->doQuerySingle($sql, $err, EMPTY_IS_OK);
//            $slaveId = $slaveDs["id"];
//        } else {
//
//            // Name des aktuellen addNupdate	Formularelementes, kann gleichzeitig eine ID im masterDs auf eine slaveDS sein
//            // Ist so eine ID>0 muss ein Update durchgefuehrt werden, sonst ein insert.
//            // Bsp: Formular 'publikation_mit_upload_new' - zur aktuellen Publikation wird ein Notiz DS mit dem Dateinamen der Publikation angelegt.
//            if ($masterDs[$formularDs["name"]])
//                $slaveId = $masterDs[$formularDs["name"]];
//            else
//                $slaveId = $GLOBALS[HTTP_POST_VARS][FRM . $formularDs["name"]]; // einige spezielle doAddNUpdate benutzen temporaere Variablen (z.B. Formular publikation_mit_upload: my_pid
//        }
//
//        if ($slaveId > 0) {
//
//            //			if($GLOBALS[HTTP_POST_VARS][FRM."id".$post]>0) { // Es existiert ein zugehoeriger DS (=Slave)
//
//            // Check ob ein Update Statement existiert
//            if (!$formularDs["sql_update"])
//                continue;
//
//            // Ersetze Variablen in dem SQL update Statement.
//            $sql = $this->substituteAssoc($formularDs["sql_update"], $masterDs);
//
//            if ($this->DebugLevel > 0) echo("doAddNUpdate(update):" . $sql . "<BR>");
//
//            // Fuehre das Update auf den Slave DS aus.
//            if ($this->DebugLevel > 3) t3lib_div::debug($sql);
//            if (!($res = $this->doSQL(MATH_DB_NAME, $sql . " "))) return ($this->buildMySQLErrMsg($err, $sql . " ", __FILE__, __LINE__, $formularDs));
//
//        } else {    // Es existiert noch kein zugehoeriger DS (=Slave)
//
//            // Check ob ein Insert Statement existiert
//            if (!$formularDs["sqlq"])
//                continue;
//
//            // Ersetze Variablen in dem SQL insert Statement.
//            $sql = $this->substituteAssoc($formularDs["sqlq"], $masterDs);
//
//            // Fuehre das Update auf den Slave DS aus.
//            if ($this->DebugLevel > 1) t3lib_div::debug($sql);
//            if (!($res = $this->doSQL(MATH_DB_NAME, $sql))) {
//                $this->buildMySQLErrMsg($err, $sql, __FILE__, __LINE__);
//                return (FALSE);
//            }
//
//            // Bestimme den Tabellennamen der im Slave SQL Statement benutzt wird
//            $arr = explode(" ", $sql); //
//            if ("insert" == mb_strtolower(mb_substr(ltrim($sql), 0, 6))) {
//
//                if (mysql_affected_rows() > 0) {
//                    // Lade gerade geschriebenen Record
//                    $slaveTableName = mb_strtolower($arr[1]) == "into" ? $arr[2] : $arr[1]; // sql: "insert into <table> ..... der dritte Parameter ist der Tabellenname
//
//                    // Lese die Id des neu angelegten DS
//                    $slaveId = mysql_insert_id();
//
//                    // Bei einigen Tabellen gibt es keine Spalte 'id' - darum die gesuchte Spalte ueber die Definition von auto_increment  bestimmen. I.d.R. 'id'
//                    // Bsp: einfuegen von fe_usern in die T3 Tabelle 'fe_users'
//                    $sql = "show fields from $slaveTableName where Extra like 'auto_increment'";
//                    if (!($tmp = $this->doQuerySingle($sql, $err))) {
//                        $this->buildMySQLErrMsg($err, $sql, __FILE__, __LINE__);
//                        return (FALSE);
//                    }
//                    $colNameId = $tmp["Field"];
//
//                    // Lade den durch addNupdate erzeugten record
//                    $sql = "select *, $colNameId as id from $slaveTableName where $colNameId = $slaveId ";
//                    if (!($slaveDs = $this->doQuerySingle($sql, $err))) {
//                        $this->buildMySQLErrMsg($err, $sql, __FILE__, __LINE__);
//                        return (FALSE);
//                    }
//                }
//
//
//                // Ersetze Variablen in dem SQL do after Statement
//                $sql = $this->substituteAssoc($formularDs["sql_do_after"], $masterDs); // masterDs
//                if ($sql) {
//
//                    if ($this->DebugLevel > 0)
//                        echo("doAddNUpdate/sql_do_after(new):" . $sql . "<BR>");
//
//                    $sql = str_replace("~_", "~", $sql);
//                    $sql = $this->substituteAssoc($sql, $slaveDs);                                                    // slaveDs
//
//                    if ($this->DebugLevel > 0) t3lib_div::debug("doAddNUpdate/sql_do_after(insert):" . $sql);
//
//                    // Führe das Update auf den Slave DS aus.
//                    if ($this->DebugLevel > 1) t3lib_div::debug($sql);
//                    if (!($res = $this->doSQL(MATH_DB_NAME, $sql))) {
//                        $this->buildMySQLErrMsg($err, $sql, __FILE__, __LINE__);
//                        return (FALSE);
//                    }
//                }
//            } else {
//                if ($this->DebugLevel > 0) echo("doAddNUpdate/sql_do_after - in sql kein select gefunden:" . $sql . "<BR>");
//            }
//        }
//    } // while()
//
//    return (TRUE);
//} // doAddNUpdate