Save.php 46.3 KB
Newer Older
1
2
3
4
5
6
7
8
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/30/16
 * Time: 7:59 PM
 */

Marc Egger's avatar
Marc Egger committed
9
10
11
namespace IMATHUZH\Qfq\Core;

use IMATHUZH\Qfq\Core\Database\Database;
12
use IMATHUZH\Qfq\Core\Form\FormAction;
13
use IMATHUZH\Qfq\Core\Helper\HelperFile;
Marc Egger's avatar
Marc Egger committed
14
15
use IMATHUZH\Qfq\Core\Helper\HelperFormElement;
use IMATHUZH\Qfq\Core\Helper\Logger;
16
use IMATHUZH\Qfq\Core\Helper\OnArray;
Marc Egger's avatar
Marc Egger committed
17
18
use IMATHUZH\Qfq\Core\Helper\Sanitize;
use IMATHUZH\Qfq\Core\Helper\Support;
19
use IMATHUZH\Qfq\Core\Store\FillStoreForm;
Marc Egger's avatar
Marc Egger committed
20
use IMATHUZH\Qfq\Core\Store\Sip;
21
use IMATHUZH\Qfq\Core\Store\Store;
22

23
24
25
26
/**
 * Class Save
 * @package qfq
 */
27
28
29
30
31
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
32
    private $feSpecNativeRaw = array(); // copy of all formElement.class='native' of the loaded form
33
34
35
36
37
38

    /**
     * @var FormAction
     */
    private $formAction = null;

39
40
41
42
    /**
     * @var null|Store
     */
    private $store = null;
Carsten  Rose's avatar
Carsten Rose committed
43
44
45
46

    /**
     * @var Database
     */
47
48
49
50
    private $db = null;

    private $evaluate = null;

51
52
    private $qfqLogFilename = '';

53
54
55
56
    /**
     * @param array $formSpec
     * @param array $feSpecAction
     * @param array $feSpecNative
57
     * @param array $feSpecNativeRaw
Marc Egger's avatar
Marc Egger committed
58
59
60
61
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
62
     */
63
    public function __construct(array $formSpec, array $feSpecAction, array $feSpecNative, array $feSpecNativeRaw) {
64

65
66
67
        $this->formSpec = $formSpec;
        $this->feSpecAction = $feSpecAction;
        $this->feSpecNative = $feSpecNative;
68
        $this->feSpecNativeRaw = $feSpecNativeRaw;
69
        $this->store = Store::getInstance();
70
        $this->db = new Database($formSpec[F_DB_INDEX]);
71
        $this->evaluate = new Evaluate($this->store, $this->db);
72
        $this->formAction = new FormAction($formSpec, $this->db);
73

Marc Egger's avatar
Marc Egger committed
74
        $this->qfqLogFilename = $this->store->getVar(SYSTEM_SITE_PATH, STORE_SYSTEM) . '/' . $this->store->getVar(SYSTEM_QFQ_LOG, STORE_SYSTEM);
75

76
77
78
    }

    /**
Carsten  Rose's avatar
Carsten Rose committed
79
     * Starts save process. Returns recordId.
80
     *
81
     * @return int
Marc Egger's avatar
Marc Egger committed
82
83
84
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
85
     * @throws \UserReportException
86
87
     */
    public function process() {
88
        $rc = 0;
89

90
        if ($this->formSpec[F_MULTI_SQL] == '') {
91
92
            $recordId = $this->store->getVar(SIP_RECORD_ID, STORE_SIP . STORE_ZERO);
            $rc = $this->elements($recordId);
93
94
        } else {
            $rc = $this->saveMultiForm();
95
        }
96
97

        return $rc;
98
99
    }

100
101
102
103
104
105
106
107
108
109
110
    /**
     * @return int|string
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
     */
    private function saveMultiForm() {

        $parentRecords = $this->evaluate->parse($this->formSpec[F_MULTI_SQL], ROW_REGULAR);

111
        // No rows: This must be an error, cause MultiForms must have at least one record.
112
        if (empty($parentRecords)) {
113
114
115
116
            throw new \UserFormException(
                json_encode([ERROR_MESSAGE_TO_USER => $this->formSpec[F_MULTI_MSG_NO_RECORD],
                    ERROR_MESSAGE_TO_DEVELOPER => 'Query selects no records: ' . $this->formSpec[F_MULTI_SQL]]),
                ERROR_MISSING_EXPECT_RECORDS);
117
118
119
120
121
122
123
124
125
126
127
128
129
        }

        // Check for 'id' or '_id' as column name
        $idName = isset($parentRecords[0]['_' . F_MULTI_COL_ID]) ? '_' . F_MULTI_COL_ID : F_MULTI_COL_ID;

        // Check that an column 'id' is given
        if (!isset($parentRecords[0][$idName])) {
            throw new \UserFormException(
                json_encode([ERROR_MESSAGE_TO_USER => 'Missing column "_' . F_MULTI_COL_ID . '"', ERROR_MESSAGE_TO_DEVELOPER => $this->formSpec[F_MULTI_SQL]]),
                ERROR_INVALID_OR_MISSING_PARAMETER);
        }

        $fillStoreForm = new FillStoreForm();
130
        $storeVarBase = $this->store->getStore(STORE_VAR);
131
132

        foreach ($parentRecords as $row) {
133
134
135
136

            // Always start with a clean STORE_VAR
            $this->store->setStore($storeVarBase, STORE_VAR, true);

137
            $this->store->setStore($row, STORE_PARENT_RECORD, true);
138
            $this->store->setVar(F_MULTI_COL_ID, $row[$idName], STORE_PARENT_RECORD); // In case '_id' is used, both '_id' and 'id' should be accessible.
139

140
            $record = $this->db->sql('SELECT * FROM `' . $this->formSpec[F_TABLE_NAME] . '` WHERE id=' . $row[$idName], ROW_EXPECT_1);
141
142
            $this->store->setStore($record, STORE_RECORD, true);

143
144
145
146
147
148
149
150
151
152
            // Fake current recordId
            $this->store->setVar(SIP_RECORD_ID, $row[$idName], STORE_SIP);
            $fillStoreForm->process(FORM_SAVE);

            $rc = $this->elements($row[$idName]);
        }

        return $rc;
    }

153
    /**
Carsten  Rose's avatar
Carsten Rose committed
154
155
     * Create empty FormElements based on templateGroups, for those who not already exist.
     *
156
     * @param array $formValues
Carsten  Rose's avatar
Carsten Rose committed
157
     *
158
     * @return array
Marc Egger's avatar
Marc Egger committed
159
     * @throws \UserFormException
160
161
162
163
164
     */
    private function createEmptyTemplateGroupElements(array $formValues) {

        foreach ($this->feSpecNative as $formElement) {

165
166
167
168
169
170
171
172
            switch ($formElement[FE_TYPE]) {
//                case FE_TYPE_EXTRA:
                case FE_TYPE_NOTE:
                case FE_TYPE_SUBRECORD:
                    continue 2;
                default:
                    break;
            }
173
            $feName = $formElement[FE_NAME];
174
175
176

            // #7705. Skip FE, which are not already expanded. Detect them by '%' (== '%d')
            if (!isset($formValues[$feName]) && false === stripos($feName, '%d') && $this->isMemberOfTemplateGroup($formElement)) {
177
178
179
                $formValues[$feName] = $formElement[FE_VALUE];
            }
        }
Carsten  Rose's avatar
Carsten Rose committed
180

181
182
183
        return $formValues;
    }

184
    /**
185
186
187
188
189
     * Check if the current $formElement is member of a templateGroup.
     *
     * @param array $formElement
     * @param int $depth
     * @return bool
Marc Egger's avatar
Marc Egger committed
190
     * @throws \UserFormException
191
192
193
194
195
     */
    private function isMemberOfTemplateGroup(array $formElement, $depth = 0) {
        $depth++;

        if ($depth > 15) {
Marc Egger's avatar
Marc Egger committed
196
            throw new \UserFormException('FormElement nested too much (in each other - endless?): stop recursion', ERROR_FE_NESTED_TOO_MUCH);
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
        }

        if ($formElement[FE_TYPE] == FE_TYPE_TEMPLATE_GROUP) {
            return true;
        }

        if ($formElement[FE_ID_CONTAINER] == 0) {
            return false;
        }

        // Get the parent element
        $formElementArr = OnArray::filter($this->feSpecNativeRaw, FE_ID, $formElement[FE_ID_CONTAINER]);
        if (isset($formElementArr[0])) {
            return $this->isMemberOfTemplateGroup($formElementArr[0], $depth);
        }

        return false; // This should not be reached,
    }

    /**
     *
218
     * @param $feName
Carsten  Rose's avatar
Carsten Rose committed
219
     *
220
221
222
223
224
225
226
227
228
229
230
     * @return bool
     */
    private function isSetEmptyMeansNull($feName) {

        $fe = OnArray::filter($this->feSpecNative, FE_NAME, $feName);

        $flag = isset($fe[0][FE_EMPTY_MEANS_NULL]) && $fe[0][FE_EMPTY_MEANS_NULL] != '0';

        return $flag;
    }

231
    /**
Carsten  Rose's avatar
Carsten Rose committed
232
233
     * Build an array of all values which should be saved. Values must exist as a 'form value' as well as a regular
     * 'table column'.
Carsten  Rose's avatar
Carsten Rose committed
234
     *
235
     * @param $recordId
Carsten  Rose's avatar
Carsten Rose committed
236
     *
237
     * @return int    record id (in case of insert, it's different from $recordId)
Marc Egger's avatar
Marc Egger committed
238
239
     * @throws \CodeException
     * @throws \DbException
240
241
     * @throws \UserFormException
     * @throws \UserReportException
242
     */
243
    private function elements($recordId) {
244
245
        $columnCreated = false;
        $columnModified = false;
246
        $realColumnFound = false;
Carsten  Rose's avatar
Carsten Rose committed
247

248
249
250
        $newValues = array();

        $tableColumns = array_keys($this->store->getStore(STORE_TABLE_COLUMN_TYPES));
251
        $formValues = $this->store->getStore(STORE_FORM);
252
        $formValues = $this->createEmptyTemplateGroupElements($formValues);
253
254
255

        // Iterate over all table.columns. Built an assoc array $newValues.
        foreach ($tableColumns AS $column) {
256

257
            // Never save a predefined 'id': autoincrement values will be given by database..
258
            if ($column === COLUMN_ID) {
259
                continue;
260
            }
261

Carsten  Rose's avatar
Upload:    
Carsten Rose committed
262
263
            // Skip Upload Elements: those will be processed later.
            if ($this->isColumnUploadField($column)) {
264
                $realColumnFound = true;
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
265
266
267
                continue;
            }

268
269
270
271
272
273
274
275
            if ($column === COLUMN_CREATED) {
                $columnCreated = true;
            }

            if ($column === COLUMN_MODIFIED) {
                $columnModified = true;
            }

276
277
            // Is there a value? Do not forget SIP values. Those do not have necessarily a FormElement.
            if (!isset($formValues[$column])) {
278
                continue;
279
280
            }

281
            $this->store->setVar(SYSTEM_FORM_ELEMENT, "Column: $column", STORE_SYSTEM);
282

283
284
285
286
287
288
            // Check if an empty string has to be converted to null.
            if (isset($formValues[$column]) && $formValues[$column] == '' && $this->isSetEmptyMeansNull($column)) {
                $formValues[$column] = null;
            } else {
                Support::setIfNotSet($formValues, $column);
            }
289
            $newValues[$column] = $formValues[$column];
290
            $realColumnFound = true;
291

292
293
        }

294
295
        // Only save record if real columns exist.
        if ($realColumnFound) {
296

297
298
            if ($columnModified && !isset($newValues[COLUMN_MODIFIED])) {
                $newValues[COLUMN_MODIFIED] = date('YmdHis');
299
            }
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
300

301
302
303
304
305
306
307
            if ($recordId == 0) {
                if ($columnCreated && !isset($newValues[COLUMN_CREATED])) {
                    $newValues[COLUMN_CREATED] = date('YmdHis');
                }
                $recordId = $this->insertRecord($this->formSpec[F_TABLE_NAME], $newValues);

            } else {
308
309
                $this->updateRecord($this->formSpec[F_TABLE_NAME], $newValues, $recordId, $this->formSpec[F_PRIMARY_KEY]);
            }
310
311
        }

312
        $this->nativeDoSlave($recordId);
313

314
        return $recordId;
315
316
    }

317
318
    /**
     * @param $recordId
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
319
     *
320
321
322
323
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
324
     */
325
326
327
328
329
330
331
332
333
334
335
    private function nativeDoSlave($recordId) {

        foreach ($this->feSpecNative as $fe) {
            // Preparation for Log, Debug
            $this->store->setVar(SYSTEM_FORM_ELEMENT, Logger::formatFormElementName($fe), STORE_SYSTEM);
            $this->store->setVar(SYSTEM_FORM_ELEMENT_ID, $fe[FE_ID], STORE_SYSTEM);

            $this->formAction->doSqlBeforeSlaveAfter($fe, $recordId, false);
        }
    }

336
    /**
337
338
     * Checks if there is a formElement with name '$feName' of type 'upload'
     *
339
340
341
     * @param $feName
     * @return bool
     */
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
342
343
344
    private function isColumnUploadField($feName) {

        foreach ($this->feSpecNative AS $formElement) {
345
            if ($formElement[FE_NAME] === $feName && $formElement[FE_TYPE] == FE_TYPE_UPLOAD)
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
346
347
                return true;
        }
Carsten  Rose's avatar
Carsten Rose committed
348

Carsten  Rose's avatar
Upload:    
Carsten Rose committed
349
350
351
352
353
354
        return false;
    }

    /**
     * Insert new record in table $this->formSpec['tableName'].
     *
355
     * @param $tableName
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
356
     * @param array $values
Carsten  Rose's avatar
Carsten Rose committed
357
     *
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
358
     * @return int  last insert id
Marc Egger's avatar
Marc Egger committed
359
360
361
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
362
363
364
365
366
367
368
369
370
371
     */
    public function insertRecord($tableName, array $values) {

        if (count($values) === 0)
            return 0; // nothing to write, last insert id=0

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

372
        $sql = "INSERT INTO $tableName ( " . $columnList . " ) VALUES ( " . $paramList . ' )';
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
373
374
375
376
377
378
379
380

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

        return $rc;
    }

    /**
     * @param string $tableName
Carsten  Rose's avatar
Carsten Rose committed
381
382
     * @param array $values
     * @param int $recordId
383
     * @param string $primaryKey
Carsten  Rose's avatar
Carsten Rose committed
384
     *
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
385
     * @return bool|int     false if $values is empty, else affectedrows
Marc Egger's avatar
Marc Egger committed
386
387
388
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
389
     */
390
    public function updateRecord($tableName, array $values, $recordId, $primaryKey = F_PRIMARY_KEY_DEFAULT) {
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
391
392
393
394

        if (count($values) === 0)
            return 0; // nothing to write, 0 rows affected

395
        if ($recordId === 0) {
Marc Egger's avatar
Marc Egger committed
396
            throw new \CodeException('RecordId=0 - this is not possible for update.', ERROR_RECORDID_0_FORBIDDEN);
397
        }
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
398
399
400
401
402
403
404
405

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

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

            $sql .= '`' . $column . '` = ?, ';
        }

406
        $sql = substr($sql, 0, strlen($sql) - 2) . " WHERE $primaryKey = ?";
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
407
408
409
410
411
412
413
        $values[] = $recordId;

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

        return $rc;
    }

414
    /**
Carsten  Rose's avatar
Carsten Rose committed
415
416
     * Process all Upload Formelements for the given $recordId. After processing &$formValues will be updated with the
     * final filenames.
417
418
419
420
421
422
423
424
     *
     * Constellation: # FILE OLD   FILE NEW     FILESIZE
     *                1 none       none
     *                2 none       new
     *                3 exist      no change
     *                4 delete     none
     *                5 delete     new
     *
425
     * @param $recordId
Marc Egger's avatar
Marc Egger committed
426
427
428
429
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
430
431
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
432
     */
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
433
434
435
436
    public function processAllUploads($recordId) {

        $sip = new Sip(false);
        $newValues = array();
437
        $vars = array();
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
438
439

        $formValues = $this->store->getStore(STORE_FORM);
440
        $primaryRecord = $this->store->getStore(STORE_RECORD); // necessary to check if the current formElement exist as a column of the primary table.
441
442
443

        foreach ($this->feSpecNative AS $formElement) {
            // skip non upload formElements
444
            if ($formElement[FE_TYPE] != FE_TYPE_UPLOAD) {
445
446
447
                continue;
            }

448
449
450
451
            // Preparation for Log, Debug
            $this->store->setVar(SYSTEM_FORM_ELEMENT, Logger::formatFormElementName($formElement), STORE_SYSTEM);
            $this->store->setVar(SYSTEM_FORM_ELEMENT_ID, $formElement[FE_ID], STORE_SYSTEM);

452
            $formElement = HelperFormElement::initUploadFormElement($formElement);
453
            if (isset($formElement[FE_FILL_STORE_VAR])) {
454
                $formElement[FE_FILL_STORE_VAR] = $this->evaluate->parse($formElement[FE_FILL_STORE_VAR], ROW_EXPECT_0_1);
455
                $this->store->appendToStore($formElement[FE_FILL_STORE_VAR], STORE_VAR);
456
            }
457

458
            $column = $formElement[FE_NAME];
459
            $pathFileName = $this->doUpload($formElement, ($formValues[$column] ?? ''), $sip, $modeUpload);
460

461
462
            if ($modeUpload == UPLOAD_MODE_DELETEOLD && $pathFileName == '') {
                $pathFileNameTmp = '';  // see '4'
463
464
            } else {
                if (empty($pathFileName)) {
465
                    $pathFileNameTmp = $primaryRecord[$column] ?? ''; // see '3'. Attention: in case of Advanced Upload, $primaryRecord[$column] does not exist.
466
467
468
469
470
                } else {
                    $pathFileNameTmp = $pathFileName; // see '1,2,5'
                }
            }

471
            // Get latest file information
472
            if ($pathFileNameTmp == '') {
473
474
475
                // No new upload and no existing: take care to remove previous upload file statistics.
                $this->store->unsetVar(VAR_FILE_MIME_TYPE, STORE_VAR);
                $this->store->unsetVar(VAR_FILE_SIZE, STORE_VAR);
476
477
                $vars[VAR_FILE_SIZE] = 0;
                $vars[VAR_FILE_MIME_TYPE] = '';
478
479
480
481
482
            } else {
                $vars = HelperFile::getFileStat($pathFileNameTmp);
                $this->store->appendToStore($vars, STORE_VAR);
            }

483
484
485
            // If given: fire a sqlBefore query
            $this->evaluate->parse($formElement[FE_SQL_BEFORE]);

486
            // Upload Type: Simple or Advanced
487
488
489
            // If (isset($primaryRecord[$column])) { - see #5048 - isset does not deal correctly with NULL!
            if (array_key_exists($column, $primaryRecord)) {
                // 'Simple Upload': no special action needed, just process the current (maybe modified) value.
490
491
                if ($pathFileName !== false) {
                    $newValues[$column] = $pathFileName;
492
493
494
495
496
497
498
499

                    if (isset($primaryRecord[COLUMN_FILE_SIZE])) {
                        $newValues[COLUMN_FILE_SIZE] = $vars[VAR_FILE_SIZE];
                    }

                    if (isset($primaryRecord[COLUMN_MIME_TYPE])) {
                        $newValues[COLUMN_MIME_TYPE] = $vars[VAR_FILE_MIME_TYPE];
                    }
500
                }
501
502
            } elseif (isset($formElement[FE_IMPORT_TO_TABLE]) && !isset($formElement[FE_SLAVE_ID])) {
                // Excel import on nonexisting column -> no upload
503
504
            } else {
                // 'Advanced Upload'
505
                $this->doUploadSlave($formElement, $modeUpload);
506
            }
507
508
509
510

            // If given: fire a sqlAfter query
            $this->evaluate->parse($formElement[FE_SQL_AFTER]);

511
        }
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
512

513
        // Only used in 'Simple Upload'
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
514
        if (count($newValues) > 0) {
515
            $this->updateRecord($this->formSpec[F_TABLE_NAME], $newValues, $recordId, $this->formSpec[F_PRIMARY_KEY]);
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
516
        }
517
518
    }

519
    /**
520
521
     * Process all Upload FormElements for the given $recordId.
     * After processing, &$formValues will be updated with the final filename.
522
     *
Marc Egger's avatar
Marc Egger committed
523
524
     * @throws \CodeException
     * @throws \UserFormException
525
526
527
528
529
530
531
532
533
534
535
536
537
     */
    public function processAllImageCutFE() {

        foreach ($this->feSpecNative AS $formElement) {
            // skip non upload formElements
            if ($formElement[FE_TYPE] != FE_TYPE_IMAGE_CUT) {
                continue;
            }

            $this->extractImageDataReplaceFile($formElement);
        }
    }

538
539
540
541
542
543
544
545

    /**
     * Iterates over all FE and checks all 'required' (mode & modeSql) FE.
     * If a required FE is empty, throw an exception.
     * Take care to remove all FE with modeSql='hidden'.
     *
     * Typically, the browser does not allow a submit if a required field is empty.
     *
Marc Egger's avatar
Marc Egger committed
546
547
548
549
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
550
551
552
     */
    public function checkRequiredHidden() {

553
        $formModeGlobal = Support::getFormModeGlobal($this->formSpec[F_MODE_GLOBAL] ?? '');
554
        $reportRequiredFailed = true;
555

556
557
558
559
560
561
        switch ($formModeGlobal) {
            case F_MODE_REQUIRED_OFF:
            case F_MODE_REQUIRED_OFF_BUT_MARK:
                $reportRequiredFailed = false;
                break;
        }
562
563
564

        $clientValues = $this->store::getStore(STORE_FORM);

565
566
        $flagAllRequiredGiven = 1;

567
568
        foreach ($this->feSpecNative AS $key => $formElement) {

569
570
571
572
573
            // Do not check retype slave FE.
            if (isset($formElement[FE_RETYPE_SOURCE_NAME])) {
                continue;
            }

574
575
            $this->store->setVar(SYSTEM_FORM_ELEMENT, "Column: " . $formElement[FE_NAME], STORE_SYSTEM);

576
577
578
579
580
            // Normalize FE_MODE
            $mode = Support::handleEscapeSpaceComment($formElement[FE_MODE_SQL]);
            $mode = empty($mode) ? $formElement[FE_MODE] : $this->evaluate->parse($mode);
            $this->feSpecNative[$key][FE_MODE] = $formElement[FE_MODE] = $mode;
            $this->feSpecNative[$key][FE_MODE_SQL] = $formElement[FE_MODE_SQL] = '';
581

582
583
584
            if (isset($formElement[FE_ACCEPT_ZERO_AS_REQUIRED]) && $formElement[FE_ACCEPT_ZERO_AS_REQUIRED] != '0' &&
                isset($clientValues[$formElement[FE_NAME]]) && $clientValues[$formElement[FE_NAME]] == '0') {
                $mode = 'fake'; // The next if() should never be true.
585
586
            }

587
588
589
590
591
            if ($mode == FE_MODE_REQUIRED && empty($clientValues[$formElement[FE_NAME]])) {
                $flagAllRequiredGiven = 0;
                if ($reportRequiredFailed) {
                    throw new \UserFormException("Missing required value: " . $formElement[FE_LABEL], ERROR_REQUIRED_VALUE_EMPTY);
                }
592
593
594
595
596
597
598
            }

            if ($mode == FE_MODE_HIDDEN) {
                // Removing the value from the store, forces that the value won't be stored.
                $this->store::unsetVar($formElement[FE_NAME], STORE_FORM);
            }
        }
599
600
601

        // Save 'allRequiredGiven in STORE_VAR
        $this->store::setVar(VAR_ALL_REQUIRED_GIVEN, $flagAllRequiredGiven, STORE_VAR, true);
602
603
    }

604
605
606
    /**
     *
     * @param array $formElement
Marc Egger's avatar
Marc Egger committed
607
608
     * @throws \CodeException
     * @throws \UserFormException
609
610
611
612
613
614
     */
    private function extractImageDataReplaceFile(array $formElement) {

        // Take care the necessary target directories exist.
        $cwd = getcwd();
        $sitePath = $this->store->getVar(SYSTEM_SITE_PATH, STORE_SYSTEM);
615
        if ($cwd === false || $sitePath === false || !HelperFile::chdir($sitePath)) {
Marc Egger's avatar
Marc Egger committed
616
            throw new \UserFormException(
Marc Egger's avatar
Marc Egger committed
617
                json_encode([ERROR_MESSAGE_TO_USER => 'getcwd() failed or SITE_PATH undefined or chdir() failed', ERROR_MESSAGE_TO_DEVELOPER => "getcwd() failed or SITE_PATH undefined or chdir('$sitePath') failed."]),
618
                ERROR_IO_CHDIR);
619
620
        }

621
        // Get original pathFileName
622
623
624
        $field = HelperFormElement::AppendFormElementNameImageCut($formElement);
        $pathFileName = $this->store->getVar($field, STORE_SIP);
        if ($pathFileName == '' || !file_exists($pathFileName)) {
Marc Egger's avatar
Marc Egger committed
625
            throw new \UserFormException(
Marc Egger's avatar
Marc Egger committed
626
                json_encode([ERROR_MESSAGE_TO_USER => 'Empty file or file not found', ERROR_MESSAGE_TO_DEVELOPER => 'Empty file or file not found: ' . $pathFileName]),
627
                ERROR_IO_FILE_NOT_FOUND);
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
        }

        // 'data:image/png;base64,AAAFBfj42Pj4...';
        $data = $this->store->getVar($formElement[FE_NAME], STORE_FORM, SANITIZE_ALLOW_ALLBUT);
        // Replace data by pathFileName (that is stored in DB).
        $this->store->setVar($formElement[FE_NAME], $pathFileName, STORE_FORM, true);

        if ($data == '') {
            return; // Nothing to do
        }

        // Split base64 encoded image: 'data:image/png;base64,AAAFBfj42Pj4...'
        list($type, $imageData) = explode(';', $data, 2); // $type= 'data:image/png;', $imageData='base64,AAAFBfj42Pj4...'
        list(, $extension) = explode('/', $type); // $type='png'
        list(, $imageData) = explode(',', $imageData); // $imageData='AAAFBfj42Pj4...'

        // If undefined: set default. BTW: Defined and empty means "no original".
        if (!isset($formElement[FE_IMAGE_CUT_KEEP_ORIGINAL])) {
            $formElement[FE_IMAGE_CUT_KEEP_ORIGINAL] = FE_IMAGE_CUT_ORIGINAL_EXTENSION;
        }
        $extSave = $formElement[FE_IMAGE_CUT_KEEP_ORIGINAL];

        $pathParts = pathinfo($pathFileName);
        // Keep the original file?
        if ($extSave != '') {

            // In case the leading '.' is missing.
            if ($extSave[0] != ".") {
                $extSave = '.' . $extSave;
            }

            // Check if there is already an original - don't create an additional one.
            if (!file_exists($pathFileName . $extSave) &&
                !file_exists($pathParts['dirname'] . $pathParts['filename'] . $extSave)
            ) {
663
                HelperFile::rename($pathFileName, $pathFileName . $extSave);
664
665
666
667
668
669
670
671
            }
        }

        if ($extension != $pathParts['extension']) {
            $pathFileName .= "." . $extension;
        }

        if (false === file_put_contents($pathFileName, base64_decode($imageData))) {
Marc Egger's avatar
Marc Egger committed
672
            throw new \UserFormException(
Marc Egger's avatar
Marc Egger committed
673
                json_encode([ERROR_MESSAGE_TO_USER => 'Write new image failed', ERROR_MESSAGE_TO_DEVELOPER => "Write new image failed: $pathFileName"]),
674
                ERROR_IO_WRITE);
675
676
677
678
679
        }

        $this->store->setVar($formElement[FE_NAME], $pathFileName, STORE_FORM, true);
    }

680
    /**
681
682
683
     * Process upload for the given Formelement. If necessary, delete a previous uploaded file.
     * Calculate the final path/filename and move the file to the new location.
     *
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
684
685
     * Check also: doc/CODING.md
     *
Carsten  Rose's avatar
Carsten Rose committed
686
687
688
689
     * @param array $formElement FormElement 'upload'
     * @param string $sipUpload SIP
     * @param Sip $sip
     * @param string $modeUpload UPLOAD_MODE_UNCHANGED | UPLOAD_MODE_NEW | UPLOAD_MODE_DELETEOLD |
Carsten  Rose's avatar
Carsten Rose committed
690
691
     *                            UPLOAD_MODE_DELETEOLD_NEW
     *
692
     * @return false|string New pathFilename or false on error
Marc Egger's avatar
Marc Egger committed
693
694
695
696
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
697
698
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
699
700
     * @internal param $recordId
     */
701
702
703
    private function doUpload($formElement, $sipUpload, Sip $sip, &$modeUpload) {
        $flagDelete = false;
        $modeUpload = UPLOAD_MODE_UNCHANGED;
704

705
        // Status information about upload file
706
707
708
709
710
        $statusUpload = $this->store->getVar($sipUpload, STORE_EXTRA);
        if ($statusUpload === false) {
            return false;
        }

711
        if (isset($formElement[FE_IMPORT_TO_TABLE]) && isset($statusUpload[FILES_TMP_NAME])) {
712
            // Import
713
            $tmpFile = Support::extendFilename($statusUpload[FILES_TMP_NAME], UPLOAD_CACHED);
714
715
            $this->doImport($formElement, $tmpFile);
        }
716

717
        // Upload - Take care the necessary target directories exist.
718
719
        $cwd = getcwd();
        $sitePath = $this->store->getVar(SYSTEM_SITE_PATH, STORE_SYSTEM);
720
        if ($cwd === false || $sitePath === false || !HelperFile::chdir($sitePath)) {
Marc Egger's avatar
Marc Egger committed
721
            throw new \UserFormException(
Marc Egger's avatar
Marc Egger committed
722
                json_encode([ERROR_MESSAGE_TO_USER => 'getcwd() failed or SITE_PATH undefined or chdir() failed', ERROR_MESSAGE_TO_DEVELOPER => "getcwd() failed or SITE_PATH undefined or chdir('$sitePath') failed."]),
723
                ERROR_IO_CHDIR);
724
        }
725

726
727
728
729
730
        // Delete existing old file.
        if (isset($statusUpload[FILES_FLAG_DELETE]) && $statusUpload[FILES_FLAG_DELETE] == '1') {
            $arr = $sip->getVarsFromSip($sipUpload);
            $oldFile = $arr[EXISTING_PATH_FILE_NAME];
            if (file_exists($oldFile)) {
731
                //TODO: it might be possible to delete a file, which is referenced by another record - a check would be nice.
732
                HelperFile::unlink($oldFile, $this->qfqLogFilename);
733
734
735
736
737
738
739
740
741
742
743
            }
            $flagDelete = ($oldFile != '');
        }

        // Set $modeUpload
        if (isset($statusUpload[FILES_TMP_NAME]) && $statusUpload[FILES_TMP_NAME] != '') {
            $modeUpload = $flagDelete ? UPLOAD_MODE_DELETEOLD_NEW : UPLOAD_MODE_NEW;
        } else {
            $modeUpload = $flagDelete ? UPLOAD_MODE_DELETEOLD : UPLOAD_MODE_UNCHANGED;
        }

744
745
        Logger::logMessageWithPrefix(UPLOAD_LOG_PREFIX . ': modeUpload= ' . $modeUpload, $this->qfqLogFilename);

746
        // skip uploading the file, if this is an import without a specified file destination
747
748
        if (!isset($formElement[FE_IMPORT_TO_TABLE]) || isset($formElement[FE_FILE_DESTINATION])) {
            $pathFileName = $this->copyUploadFile($formElement, $statusUpload);
749
750
751
752

            $msg = UPLOAD_LOG_PREFIX . ': ';
            $msg .= ($pathFileName == '') ? 'Remove old upload / no new upload' : 'File "' . $statusUpload[FILES_TMP_NAME] . '" >> "' . $pathFileName . '"';
            Logger::logMessageWithPrefix($msg, $this->qfqLogFilename);
753
754
        }

755
        HelperFile::chdir($cwd);
756
757
758
759
760
761
762
763
764

        // Delete current used uniq SIP
        $this->store->setVar($sipUpload, array(), STORE_EXTRA);

        return $pathFileName;
    }

    /**
     * @param $formElement
765
     * @param $fileName
Marc Egger's avatar
Marc Egger committed
766
767
768
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
769
770
771
772
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     */
    private function doImport($formElement, $fileName) {
773
        $importNamedSheetsOnly = array();
774

775
        Support::setIfNotSet($formElement, FE_IMPORT_TYPE, FE_IMPORT_TYPE_AUTO);
776

777
778
779
780
        if (!empty($formElement[FE_IMPORT_NAMED_SHEETS_ONLY])) {
            $importNamedSheetsOnly = explode(',', $formElement[FE_IMPORT_NAMED_SHEETS_ONLY]);
        }

Carsten  Rose's avatar
Carsten Rose committed
781
        // Check for keywords which needs an explicit given document type.
782
783
784
785
786
        if ($formElement[FE_IMPORT_TYPE] === FE_IMPORT_TYPE_AUTO) {

            $list = [FE_IMPORT_LIST_SHEET_NAMES, FE_IMPORT_READ_DATA_ONLY, FE_IMPORT_LIST_SHEET_NAMES];
            foreach ($list as $token) {
                if (isset($formElement[$token])) {
Marc Egger's avatar
Marc Egger committed
787
                    throw new \UserFormException('If ' . $token .
788
789
790
791
792
                        ' is given, an explicit document type (like ' . FE_IMPORT_TYPE . '=xlsx) should be set.', ERROR_IMPORT_MISSING_EXPLICIT_TYPE);
                }
            }
        }

793
794
795
796
797
798
799
800
801
802
803
        switch ($formElement[FE_IMPORT_TYPE]) {
            case FE_IMPORT_TYPE_AUTO:
                $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($fileName);
                break;

            case FE_IMPORT_TYPE_XLS:
            case FE_IMPORT_TYPE_XLSX:
            case FE_IMPORT_TYPE_CSV:
            case FE_IMPORT_TYPE_ODS:
                $inputFileType = ucfirst($formElement[FE_IMPORT_TYPE]);
                $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
804
805
806
807
808
809
810
811
812
813
814
815
816

                // setReadDataOnly
                if (($formElement[FE_IMPORT_READ_DATA_ONLY] ?? '0') != '0') {
                    $reader->setReadDataOnly(true);
                }

                // setLoadSheetsOnly
                if (!empty ($importNamedSheetsOnly)) {
                    $reader->setLoadSheetsOnly($importNamedSheetsOnly);
                }

                if (($formElement[FE_IMPORT_LIST_SHEET_NAMES] ?? '0') != '0') {
                    $sheetNames = $reader->listWorksheetNames($fileName);
Marc Egger's avatar
Marc Egger committed
817
                    throw new \UserFormException("Worksheets: " . implode(', ', $sheetNames),
818
819
820
                        ERROR_IMPORT_LIST_SHEET_NAMES);
                }

821
822
823
824
                $spreadsheet = $reader->load($fileName);
                break;

            default:
Marc Egger's avatar
Marc Egger committed
825
                throw new \UserFormException("Unknown Excel import type: '" . $formElement[FE_IMPORT_TYPE] . "'.",
826
827
828
829
                    ERROR_UNKNOWN_EXCEL_IMPORT_TYPE);
        }

        $tableName = $formElement[FE_IMPORT_TO_TABLE];
830
        $regions = OnArray::trimArray(explode('|', $formElement[FE_IMPORT_REGION] ?? ''));
831
        $columnNames = OnArray::trimArray(explode(',', $formElement[FE_IMPORT_TO_COLUMNS] ?? ''));
832
833
834
835
        $importMode = $formElement[FE_IMPORT_MODE] ?? FE_IMPORT_MODE_APPEND;

        foreach ($regions as $region) {
            // region: tab, startColumn, startRow, endColumn, endRow
836
            $region = OnArray::trimArray(explode(',', $region));
837
838
839
840
841
842
843
844
845
846
847
848
849
850
            $tab = 1;
            if (!empty($region[0])) {
                $tab = $region[0];
            }

            try {
                if (is_numeric($tab)) {
                    $worksheet = $spreadsheet->getSheet($tab - 1); // 0-based
                } else {
                    $worksheet = $spreadsheet->getSheetByName($tab);
                    if ($worksheet === null) {
                        throw new \PhpOffice\PhpSpreadsheet\Exception(
                            "No sheet with the name '$tab' could be found."
                        );
851
852
                    }
                }
853
            } catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
Marc Egger's avatar
Marc Egger committed
854
                throw new \UserFormException($e->getMessage());
855
            }
856

857
858
859
860
861
862
863
864
865
            // Set up requested region
            $columnStart = '1';
            $columnEnd = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($worksheet->getHighestColumn());
            $rowStart = 1;
            $rowEnd = $worksheet->getHighestRow();
            if (!empty($region[1])) { // startColumn
                if (!is_numeric($region[1])) $region[1] = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($region[1]);
                if ($region[1] >= $columnStart && $region[1] <= $columnEnd) {
                    $columnStart = $region[1];
866
867
                }
            }
868
869
870
871
872
            if (!empty($region[3])) { // endColumn
                if (!is_numeric($region[3])) $region[3] = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($region[3]);
                if ($region[3] >= $columnStart && $region[3] <= $columnEnd) {
                    $columnEnd = $region[3];
                }
873
            }
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
            if (!empty($region[2]) && $region[2] >= $rowStart && $region[2] <= $rowEnd) {
                $rowStart = $region[2];
            }
            if (!empty($region[4]) && $region[4] >= $rowStart && $region[4] <= $rowEnd) {
                $rowEnd = $region[4];
            }
            // Read the specified region
            $rangeStr = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($columnStart) . $rowStart . ':' .
                \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($columnEnd) . $rowEnd;
            $worksheetData = $worksheet->rangeToArray($rangeStr, '', true, false);

            $columnDefinitionArr = [];
            $columnListArr = [];
            for ($column = $columnStart; $column <= $columnEnd; ++$column) {
                if (!empty($columnNames[$column - $columnStart])) {
                    $columnName = $columnNames[$column - $columnStart];
                } else {
                    $columnName = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($column);
892
                }
893
894
                $columnDefinitionArr[] = "`$columnName`   TEXT       NOT NULL  DEFAULT ''";
                $columnListArr[] = "$columnName";
895
896
            }

897
898
899
900
901
902
903
904
905
906
907
908
909
            // SQL time!
            $createTableSql = "CREATE TABLE IF NOT EXISTS `$tableName` (" .
                "`id`        INT(11)    NOT NULL  AUTO_INCREMENT," .
                implode(', ', $columnDefinitionArr) . ',' .
                "`modified`  TIMESTAMP  NOT NULL  DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP," .
                "`created`   DATETIME   NOT NULL  DEFAULT CURRENT_TIMESTAMP," .
                "PRIMARY KEY (`id`) )" .
                "ENGINE = InnoDB  DEFAULT CHARSET = utf8 AUTO_INCREMENT = 0;";
            $this->db->sql($createTableSql);

            if ($importMode === FE_IMPORT_MODE_REPLACE) {
                $this->db->sql("TRUNCATE $tableName");
                $importMode = FE_IMPORT_MODE_APPEND;
910
            }
911

912
913
914
915
916
917
918
            // Import the data
            foreach ($worksheetData AS $rowIndex => $row) {
                $columnList = implode(',', $columnListArr);
                $paramPlaceholders = str_repeat('?,', count($worksheetData[0]) - 1) . '?';
                $insertSql = "INSERT INTO `$tableName` ($columnList) VALUES ($paramPlaceholders)";
                $this->db->sql($insertSql, ROW_REGULAR, $row);
            }
919
        }
920
921
922
    }

    /**
Carsten  Rose's avatar
Upload:    
Carsten Rose committed
923
924
925
926
     * Copy uploaded file from temporary location to final location.
     *
     * Check also: doc/CODING.md
     *
927
928
929
     * @param array $formElement
     * @param array $statusUpload
     * @return array|mixed|null|string
Marc Egger's avatar
Marc Egger committed
930
931
932
933
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
934
     */
935
    private function copyUploadFile(array $formElement, array $statusUpload) {
936
937
        $pathFileName = '';

Carsten  Rose's avatar
Upload:    
Carsten Rose committed
938
        if (!isset($statusUpload[FILES_TMP_NAME]) || $statusUpload[FILES_TMP_NAME] === '') {
939
940
941
942
            // nothing to upload: e.g. user has deleted a previous uploaded file.
            return '';
        }

943
944
        $srcFile = Support::extendFilename($statusUpload[FILES_TMP_NAME], UPLOAD_CACHED);

945
        if (isset($formElement[FE_FILE_DESTINATION])) {
946

947
            // Provide variable 'filename'. Might be substituted in $formElement[FE_PATH_FILE_NAME].
948
            $origFilename = Sanitize::safeFilename($statusUpload[FILES_NAME]);
949
            $this->store->appendToStore(HelperFile::pathinfo($origFilename), STORE_VAR);
950

951
            $pathFileName = $this->evaluate->parse($formElement[FE_FILE_DESTINATION]);
952
            $pathFileName = Sanitize::safeFilename($pathFileName, false, true); // Dynamically calculated pathFileName might contain invalid characters.
953
954
955

            // Saved in store for later use during 'Advanced Upload'-post processing
            $this->store->setVar(VAR_FILE_DESTINATION, $pathFileName, STORE_VAR);
956
957
        }

958
        if ($pathFileName === '') {
Marc Egger's avatar
Marc Egger committed
959
            throw new \UserFormException("Upload failed, no target '" . FE_FILE_DESTINATION . "' specified.", ERROR_NO_TARGET_PATH_FILE_NAME);
960
961
        }

962
963
964
965
966
967
968
        // If given, get chmodDir. Needs to be prefixed with a 0 (=octal) - it should not be quoted! Symbolic mode is not allowed. E.g.: 0660, or 01777
        if (empty($formElement[FE_FILE_CHMOD_DIR])) {
            $chmodDir = false;
        } else {
            $chmodDir = octdec($formElement[FE_FILE_CHMOD_DIR]);
        }

969
        $overwrite = isset($formElement[FE_FILE_REPLACE_MODE]) && $formElement[FE_FILE_REPLACE_MODE] == FE_FILE_REPLACE_MODE_ALWAYS;
970
        Support::moveFile($srcFile, $pathFileName, $overwrite, $chmodDir);
971

972
        // get chmodFile
973
974
        if (empty($formElement[FE_FILE_CHMOD_FILE])) {
            $chmodFile = false;
975
        } else {
976
            $chmodFile = octdec($formElement[FE_FILE_CHMOD_FILE]);
977
978
        }

979
        $this->autoOrient($formElement, $pathFileName);
980
        HelperFile::chmod($pathFileName, $chmodFile);
981

982
        $this->splitUpload($formElement, $pathFileName, $chmodFile, $statusUpload);
983

984
985
        return $pathFileName;
    }
986

987
988
    /**
     * If fe['autoOrient'] is given and the MimeType corresponds to fe['autoOrientMimeType']: the given {{pathFileName:V}} will be converted.
989
     * ImageMagick 'convert' seems to do a better job than GraficsMagick (Orientation is stable even if multiple times applied).
990
991
992
     *
     * @param array $formElement
     * @param $pathFileName
Marc Egger's avatar
Marc Egger committed
993
994
995
996
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
997
     */
998
    private function autoOrient(array $formElement, $pathFileName) {
999
1000

        // 'autoOrient' wished?