Report.php 57.8 KB
Newer Older
1
2
3
4
<?php
/***************************************************************
 *  Copyright notice
 *
Carsten  Rose's avatar
Carsten Rose committed
5
 *  (c) 2010 Glowbase GmbH
6
 *
Carsten  Rose's avatar
Carsten Rose committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
22
23
 ***************************************************************/

Marc Egger's avatar
Marc Egger committed
24
25
26
27
namespace IMATHUZH\Qfq\Core\Report;

use IMATHUZH\Qfq\Core\Database\Database;
use IMATHUZH\Qfq\Core\Evaluate;
28
use IMATHUZH\Qfq\Core\Form\FormAsFile;
Marc Egger's avatar
Marc Egger committed
29
30
31
32
33
34
35
use IMATHUZH\Qfq\Core\Helper\HelperFile;
use IMATHUZH\Qfq\Core\Helper\KeyValueStringParser;
use IMATHUZH\Qfq\Core\Helper\OnString;
use IMATHUZH\Qfq\Core\Helper\Sanitize;
use IMATHUZH\Qfq\Core\Helper\Support;
use IMATHUZH\Qfq\Core\Store\Sip;
use IMATHUZH\Qfq\Core\Store\Store;
36

37
38
const DEFAULT_QUESTION = 'question';
const DEFAULT_ICON = 'icon';
39
const DEFAULT_BOOTSTRAP_BUTTON = 'bootstrapButton';
40

41
42
43
44
/**
 * Class Report
 * @package qfq
 */
45
46
class Report {

Carsten  Rose's avatar
Carsten Rose committed
47
48
49
50
51
    /**
     * @var SIP
     */
    private $sip = null;

Carsten  Rose's avatar
Carsten Rose committed
52
53
54
55
56
    /**
     * @var Link
     */
    private $link = null;

57
58
59
60
61
    /**
     * @var Store
     */
    private $store = null;

62
63
64
65
66
67
68
    /**
     * @var string
     */
    private $dbAlias = '';

    // frArray[10.50.5.sql][select ...]
    private $frArray = array();
69

70
71
    // $indexArray[10][50][5]   one entry per 'sql' statement
    private $indexArray = array();
72

73
74
75
76
    // TODO to explain
//	private $resultArray = array();
    private $levelCount = 0;
    //private $counter = 0;
77

78
79
80
81
    /**
     * @var Variables
     */
    private $variables = null;
82

83
    /**
84
     * @var Database
85
86
     */
    private $db = null;
87

88
    private $dbIndexData = false;
89

90
91
92
93
94
    /**
     * @var Thumbnail
     */
    private $thumbnail = null;

95
96
97
98
99
    /**
     * @var Monitor
     */
    private $monitor = null;

100
101
102
103
    /**
     * @var array
     */
    private $pageDefaults = array();
104

105
    /**
Carsten  Rose's avatar
Carsten Rose committed
106
107
     * @var array - Emulate global variable: will be set much earlier in other functions. Will be shown in error
     *      messages.
108
     */
109
110
    private $fr_error = array('uid' => '', 'pid' => '', 'row' => '', 'debug_level' => '0', 'full_level' => '');

111
112
    private $phpUnit = false;

113
    private $showDebugInfoFlag = false;
114

115
    /**
116
     * Report constructor.
117
     *
Carsten  Rose's avatar
Carsten Rose committed
118
     * @param array $t3data
119
     * @param Evaluate $eval
Carsten  Rose's avatar
Carsten Rose committed
120
     * @param bool $phpUnit
Marc Egger's avatar
Marc Egger committed
121
122
123
124
     * @throws \CodeException
     * @throws \DbException
     * @throws \UserFormException
     * @throws \UserReportException
125
     */
126
    public function __construct(array $t3data, Evaluate $eval, $phpUnit = false) {
127

128
        #TODO: rewrite $phpUnit to: "if (!defined('PHPUNIT_QFQ')) {...}"
129
        $this->phpUnit = $phpUnit;
130

131
132
        Support::setIfNotSet($t3data, "uid", 0);

133
        $this->sip = new Sip($phpUnit);
134
135
        if ($phpUnit) {
            $this->sip->sipUniqId('badcaffee1234');
136
137
            //TODO Webserver Umgebung besser faken
            $_SERVER['REQUEST_URI'] = 'localhost';
138
139
        }

140
        $this->store = Store::getInstance();
141

142
        $this->showDebugInfoFlag = (Support::findInSet(SYSTEM_SHOW_DEBUG_INFO_YES, $this->store->getVar(SYSTEM_SHOW_DEBUG_INFO, STORE_SYSTEM)));
143

144
145
        $this->checkUpdateSqlLog();

146
147
        $this->pageDefaults[DEFAULT_QUESTION]["pagec"] = "Please confirm!:info";
        $this->pageDefaults[DEFAULT_QUESTION]["paged"] = "Do you really want to delete the record?:warning";
148

149
150
151
152
153
154
        $this->pageDefaults[DEFAULT_ICON]["paged"] = TOKEN_DELETE;
        $this->pageDefaults[DEFAULT_ICON]["pagee"] = TOKEN_EDIT;
        $this->pageDefaults[DEFAULT_ICON]["pageh"] = TOKEN_HELP;
        $this->pageDefaults[DEFAULT_ICON]["pagei"] = TOKEN_INFO;
        $this->pageDefaults[DEFAULT_ICON]["pagen"] = TOKEN_NEW;
        $this->pageDefaults[DEFAULT_ICON]["pages"] = TOKEN_SHOW;
155

156
157
158
159
160
161
        $this->pageDefaults[DEFAULT_BOOTSTRAP_BUTTON]["pagec"] = TOKEN_BOOTSTRAP_BUTTON;
        $this->pageDefaults[DEFAULT_BOOTSTRAP_BUTTON]["paged"] = TOKEN_BOOTSTRAP_BUTTON;
        $this->pageDefaults[DEFAULT_BOOTSTRAP_BUTTON]["pagee"] = TOKEN_BOOTSTRAP_BUTTON;
        $this->pageDefaults[DEFAULT_BOOTSTRAP_BUTTON]["pagen"] = TOKEN_BOOTSTRAP_BUTTON;
        $this->pageDefaults[DEFAULT_BOOTSTRAP_BUTTON]["pages"] = TOKEN_BOOTSTRAP_BUTTON;

162
        // Default should already set in QuickFormQuery() Constructor
Carsten  Rose's avatar
Carsten Rose committed
163
164
165
166
        $this->dbIndexData = $this->store->getVar(TOKEN_DB_INDEX, STORE_TYPO3);
        if ($this->dbIndexData === false) {
            $this->dbIndexData = DB_INDEX_DEFAULT;
        }
167

168
        $this->db = new Database($this->dbIndexData);
169
        $this->variables = new Variables($eval, $t3data["uid"]);
170

171
172
        $this->link = new Link($this->sip, $this->dbIndexData, $phpUnit);

173
        // Set static values, which won't change during this run.
174
        $this->fr_error["pid"] = isset($this->variables->resultArray['global.']['page_id']) ? $this->variables->resultArray['global.']['page_id'] : 0;
175
        $this->fr_error["uid"] = $t3data['uid'];
176
        $this->fr_error["debug_level"] = 0;
177

Carsten  Rose's avatar
Carsten Rose committed
178
        // Sanitize function for POST and GET Parameters.
179
        // Merged URL-Parameter (key1, id etc...) in resultArray.
180
        $this->variables->resultArray = array_merge($this->variables->resultArray, array("global." => $this->variables->collectGlobalVariables()));
181
182
183

    }

184
    /**
Carsten  Rose's avatar
Carsten Rose committed
185
186
     * If a variable 'sqlLog' is given in STORE_TYPO3 (=Bodytext) make them relative to SYSTEM_PATH_EXT and copy it to
     * STORE_SYSTEM
187
     *
Marc Egger's avatar
Marc Egger committed
188
189
     * @throws \CodeException
     * @throws \UserFormException
190
191
192
193
194
     */
    private function checkUpdateSqlLog() {

        $sqlLog = $this->store->getVar(TYPO3_SQL_LOG, STORE_TYPO3);
        if (false !== $sqlLog) {
195
            $sqlLog = HelperFile::joinPathFilename($this->store->getVar(SYSTEM_EXT_PATH, STORE_SYSTEM), $sqlLog);
196
197
198
199
200
201
202
203
204
            $this->store->setVar(SYSTEM_SQL_LOG, $sqlLog, STORE_SYSTEM);
        }

        $sqlLogMode = $this->store->getVar(TYPO3_SQL_LOG_MODE, STORE_TYPO3);
        if (false !== $sqlLogMode) {
            $this->store->setVar(SYSTEM_SQL_LOG_MODE, $sqlLogMode, STORE_SYSTEM);
        }
    }

205
206
207
    /**
     * Main function. Parses bodytext and iterates over all queries.
     *
208
     * @param $bodyText
209
     * @return string
Marc Egger's avatar
Marc Egger committed
210
211
212
     * @throws \CodeException
     * @throws \DbException
     * @throws \DownloadException
213
214
215
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
216
217
218
219
220
     * @throws \Twig\Error\LoaderError
     * @throws \Twig\Error\RuntimeError
     * @throws \Twig\Error\SyntaxError
     * @throws \UserFormException
     * @throws \UserReportException
221
     */
222
    public function process($bodyText) {
223

224
225
226
227
228
        //phpUnit Test: clean environment
        $this->frArray = array();
        $this->indexArray = array();
        $this->levelCount = 0;

229
        // Iteration over Bodytext
230
        $ttLineArray = explode("\n", $bodyText);
231

232
233
        foreach ($ttLineArray as $index => $line) {
            // Fill $frArray, $indexArray, $resultArray
234
            $this->parseLine($line);
235
        }
236

237
238
239
240
241
242
        // Sort array
        $this->sortIndexArray($this->indexArray, $this->generateSortArg());

        // Report
        $content = $this->triggerReport();

243
        return $content;
244
    }
245
246
247
248
249

    /**
     * Split line in level, command, content and fill 'frArray', 'levelCount', 'indexArray'
     * Example: 10.50.5.sql = select * from person
     *
250
     * @param string $ttLine : line to split in level, command, content
Carsten  Rose's avatar
Carsten Rose committed
251
     *
Marc Egger's avatar
Marc Egger committed
252
     * @throws \UserReportException
253
     */
254
    private function parseLine($ttLine) {
255
256
257
258

        // 10.50.5.sql = select ...
        $arr = explode("=", trim($ttLine), 2);

259
        // no elements or only one: do nothing
260
        if (count($arr) < 2) {
261
            return;
262
        }
263

264
265
266
267
        // 10.50.5.sql
        $key = strtolower(trim($arr[0]));

        // comment ?
268
        if (empty($key) || $key[0] === "#") {
269
            return;
270
        }
271

272
        // select ... - if needed, trim surrounding single ticks
273
        $value = trim($arr[1]);
274
        $value = OnString::trimQuote($value);
275
276
277
278
279
280
281

        // 10.50.5.sql
        $arrKey = explode('.', $key);

        // frCmd = "sql"
        $frCmd = $arrKey[count($arrKey) - 1];

282
        if (strpos('|' . strtolower(TOKEN_VALID_LIST) . '|', '|' . $frCmd . '|') === false) {
Marc Egger's avatar
Marc Egger committed
283
            throw new \UserReportException ("Unknown token: $frCmd in Line '$ttLine''", ERROR_UNKNOWN_TOKEN);
284
285
        }

286
287
288
289
290
291
292
293
294
295
296
        // remove last item (cmd)
        unset($arrKey[count($arrKey) - 1]);

        // save elements only if there is a level specified
        if (count($arrKey)) {
            // level = "10.50.5"
            $level = implode(".", $arrKey);

            // fill Array
            $this->setLine($level, $frCmd, $value);
        }
297
    }
298

299
300
301
302
303
    /**
     * @param $level
     * @param $frCmd
     * @param $value
     */
304
305
306
307
308
309
    private function setLine($level, $frCmd, $value) {

        // store complete line reformatted in frArray
        $this->frArray[$level . "." . $frCmd] = $value;

        // per sql command
310
        //pro sql cmd wird der Indexarray abgefüllt. Dieser wird später verwendet um auf den $frArray zuzugreifen
311
        //if(preg_match("/^sql/i", $frCmd) == 1){
312
313
//        if ($frCmd === TOKEN_FORM || $frCmd === TOKEN_SQL) {
        if ($frCmd === TOKEN_SQL) {
314
315
316
317
318
            // Remember max level
            $this->levelCount = max(substr_count($level, '.') + 1, $this->levelCount);
            // $indexArray[10][50][5]
            $this->indexArray[] = explode(".", $level);
        }
319
320
321

        // set defaults
        if ($frCmd === TOKEN_SQL) {
322
323
            $arr = explode('|', TOKEN_VALID_LIST);
            foreach ($arr as $key) {
324
                if (!isset($this->frArray[$level . "." . $key])) {
325
                    $this->frArray[$level . "." . $key] = '';
326
                }
327
328
            }
        }
329
    }
330
331
332
333

    /**
     * Sorts the associative array.
     *
Carsten  Rose's avatar
Carsten Rose committed
334
     * @param array $ary : The unsorted Level Array
335
     * @param string $clause : the sort argument 0 ASC, 1 ASC... according to the number of columns
336
     * @param bool|true $ascending
337
     */
338
    private function sortIndexArray(array &$ary, $clause, $ascending = true) {
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

        $clause = str_ireplace('order by', '', $clause);
        $clause = preg_replace('/\s+/', ' ', $clause);
        $keys = explode(',', $clause);
        $dirMap = array('desc' => 1, 'asc' => -1);
        $def = $ascending ? -1 : 1;

        $keyAry = array();
        $dirAry = array();
        foreach ($keys as $key) {
            $key = explode(' ', trim($key));
            $keyAry[] = trim($key[0]);
            if (isset($key[1])) {
                $dir = strtolower(trim($key[1]));
                $dirAry[] = $dirMap[$dir] ? $dirMap[$dir] : $def;
            } else {
                $dirAry[] = $def;
            }
        }
        $fnBody = '';
        for ($i = count($keyAry) - 1; $i >= 0; $i--) {
            $k = $keyAry[$i];
            $t = $dirAry[$i];
            $f = -1 * $t;
            $aStr = '$a[\'' . $k . '\']';
            $bStr = '$b[\'' . $k . '\']';

            if (strpos($k, '(') !== false) {
                $aStr = '$a->' . $k;
                $bStr = '$b->' . $k;
            }

            if ($fnBody == '') {
                $fnBody .= "if({$aStr} == {$bStr}) { return 0; }\n";
                $fnBody .= "return ({$aStr} < {$bStr}) ? {$t} : {$f};\n";
            } else {
                $fnBody = "if({$aStr} == {$bStr}) {\n" . $fnBody;
                $fnBody .= "}\n";
                $fnBody .= "return ({$aStr} < {$bStr}) ? {$t} : {$f};\n";
            }
        }

        if ($fnBody) {
            $sortFn = create_function('$a,$b', $fnBody);
383
384
385
386
387

            // TODO: at the moment, $sortFn() triggers some E_NOTICE warnings. We stop these here for a short time.
            $errorSet = error_reporting();
            error_reporting($errorSet & ~E_NOTICE);

388
            usort($ary, $sortFn);
389
390

            error_reporting($errorSet);
Carsten  Rose's avatar
Carsten Rose committed
391
            error_clear_last();
392
        }
393
    }
394

395
396
397
398
399
    /**
     * generateSortArg
     *
     * @return string
     */
400
401
402
403
404
405
406
    private function generateSortArg() {

        $sortArg = "";

        for ($i = 0; $i < $this->levelCount; $i++) {
            $sortArg = $sortArg . $i . " ASC, ";
        }
407

408
        $sortArg = substr($sortArg, 0, strlen($sortArg) - 2);
409

410
        return $sortArg;
411
    }
412
413

    /**
414
     * Executes the queries recursive. This Method is called for each sublevel.
415
416
417
418
419
420
     *
     * ROOTLEVEL
     * This method is called once from the main method.
     * For the first call the method executes the rootlevels
     *
     * SUBLEVEL
421
     * For each rootlevel the method calls it self with the level mode 0
422
423
424
     * If the next Level is a Sublevel it will be executed and $this->counter will be added by 1
     * The sublevel calls the method again for a following sublevel
     *
425
426
427
428
     * @param int $cur_level
     * @param array $super_level_array
     * @param int $counter
     * @return string
Marc Egger's avatar
Marc Egger committed
429
430
431
     * @throws \CodeException
     * @throws \DbException
     * @throws \DownloadException
432
433
434
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
435
436
437
     * @throws \Twig\Error\LoaderError
     * @throws \Twig\Error\RuntimeError
     * @throws \Twig\Error\SyntaxError
Carsten  Rose's avatar
Carsten Rose committed
438
439
440
     * @throws \Twig_Error_Loader
     * @throws \Twig_Error_Runtime
     * @throws \Twig_Error_Syntax
441
442
     * @throws \UserFormException
     * @throws \UserReportException
443
     */
444
445
    private function triggerReport($cur_level = 1, array $super_level_array = array(), $counter = 0) {
        $keys = array();
446
        $stat = array();
447
448
449
        $content = "";

        // CurrentLevel "10.10.50"
450
        if (isset($this->indexArray[$counter]) && is_array($this->indexArray[$counter])) {
451
            $fullLevel = implode(".", $this->indexArray[$counter]);
452
        } else {
453
            $fullLevel = '';
454
455
        }

456
        // Superlevel "10.10"
457
458
        if (!empty($super_level_array) && is_array($super_level_array)) {
            $full_super_level = implode(".", $super_level_array) . '.';
459
460
461
        } else {
            $full_super_level = '';
        }
462
463
464

        //condition1: indexArray
        //condition2: full_level == Superlevel (but at the length of the Superlevel)
465
466
        while ($counter < count($this->indexArray) && $full_super_level == substr($fullLevel, 0, strlen($full_super_level))) {
            $contentLevel = '';
Nicola Chiapolini's avatar
Nicola Chiapolini committed
467
            $contentSubqueries = '';
468
469
470

            //True: The cur_level is a subquery -> continue
            if ($cur_level != count($this->indexArray[$counter])) {
471
472
                ++$counter;
                if (isset($this->indexArray[$counter]) && is_array($this->indexArray[$counter])) {
473
                    $fullLevel = implode(".", $this->indexArray[$counter]);
474
                } else {
475
                    $fullLevel = '';
476
                }
477
478
479
480
                continue;
            }

            // Set debug, if one is specified else keep the parent one.
481
            $lineDebug = $this->getValueParentDefault(TOKEN_DEBUG, $full_super_level, $fullLevel, $cur_level, 0);
482
483

            // Prepare Error reporting
484
485
            $this->store->setVar(SYSTEM_SQL_RAW, $this->frArray[$fullLevel . "." . TOKEN_SQL], STORE_SYSTEM);
            $this->store->setVar(SYSTEM_REPORT_FULL_LEVEL, $fullLevel, STORE_SYSTEM);
486

487
            // Prepare SQL: replace variables. Actual 'line.total' or 'line.count' will recalculated: don't replace them now!
488
489
            unset($this->variables->resultArray[$fullLevel . ".line."][LINE_TOTAL]);
            unset($this->variables->resultArray[$fullLevel . ".line."][LINE_COUNT]);
490

491
            $sql = $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_SQL]);
492

493
            $this->store->setVar(SYSTEM_SQL_FINAL, $sql, STORE_SYSTEM);
494

495
496
497
498
499
            // import form files if changed
            if (FormAsFile::isFormQuery($sql)) {
                FormAsFile::updateAllForms();
            }

Nicola Chiapolini's avatar
Nicola Chiapolini committed
500
            //Execute SQL. All errors have been already catched.
501
            unset($result);
502
            $result = $this->db->sql($sql, ROW_KEYS, array(), '', $keys, $stat);
503
504

            // If an array is returned, $sql was a query, otherwise an 'insert', 'update', 'delete', ...
505
            // Query: total number of rows
506
507
            // insert, delete, update: number of affected rows
            $rowTotal = isset($stat[DB_NUM_ROWS]) ? $stat[DB_NUM_ROWS] : $stat[DB_AFFECTED_ROWS];
508

509
510
511
            $this->variables->resultArray[$fullLevel . ".line."][LINE_TOTAL] = $rowTotal;
            $this->variables->resultArray[$fullLevel . ".line."][LINE_COUNT] = is_array($result) ? 1 : 0;
            $this->variables->resultArray[$fullLevel . ".line."][LINE_INSERT_ID] = $stat[DB_INSERT_ID] ?? 0;
512

Nicola Chiapolini's avatar
Nicola Chiapolini committed
513
514
515
516
517

            /////////////////////////////////
            //    Render SHEAD and HEAD    //
            /////////////////////////////////

518
            $contentLevel .= $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_SHEAD]);
519
            // HEAD: If there is at least one record, do 'head'.
520
            if ($rowTotal > 0) {
521
                $contentLevel .= $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_HEAD]);
522
            }
523

Nicola Chiapolini's avatar
Nicola Chiapolini committed
524
525
526
527
            ///////////////////////
            //    Render rows    //
            ///////////////////////

528
            if (is_array($result)) {
529
530

                // Prepare row alteration
531
                $arrRbgd = explode("|", $this->frArray[$fullLevel . "." . TOKEN_RBGD], 2);
532
533
534
535
536
                if (count($arrRbgd) < 2) {
                    $arrRbgd[] = '';
                    $arrRbgd[] = '';
                }

Nicola Chiapolini's avatar
Nicola Chiapolini committed
537
                // Prepare skip wrapping of indexed columns
Carsten  Rose's avatar
Carsten Rose committed
538
539
540
541
                $fSkipWrap = array();
                if ('' != ($str = ($this->frArray[$fullLevel . "." . TOKEN_FSKIPWRAP]) ?? '')) {
                    $str = str_replace(' ', '', $str);
                    $fSkipWrap = explode(',', $str);
542
543
544
545
546
                    // Decrement all values to start counting with 0.
                    foreach ($fSkipWrap as $key => $value) {
                        $fSkipWrap[$key] = $value - 1;
                    }

Carsten  Rose's avatar
Carsten Rose committed
547
548
549
                    $fSkipWrap = array_flip($fSkipWrap);
                }

550
551
                $newKeys = $this->splitColumnNames($keys, $fSkipWrap);

552
                //---------------------------------
Carsten  Rose's avatar
Carsten Rose committed
553
                // Process each row of result set
554
                $columnValueSeparator = "";
555
                $rowIndex = 0;
Carsten  Rose's avatar
Carsten Rose committed
556

557
                foreach ($result as $row) {
Nicola Chiapolini's avatar
Nicola Chiapolini committed
558
                    // increment record number counter
559
                    $this->variables->resultArray[$fullLevel . ".line."][LINE_COUNT] = ++$rowIndex;
560
561
562

                    // replace {{<level>.line.count}} and {{<level>.line.total}} in __result__, if the variables specify their own full_level. This can't be replaced before firing the query.
                    for ($ii = 0; $ii < count($row); $ii++) {
563
564
                        $row[$ii] = str_replace("{{" . $fullLevel . ".line.count}}", $rowIndex, $row[$ii]);
                        $row[$ii] = str_replace("{{" . $fullLevel . ".line.total}}", $rowTotal, $row[$ii]);
565
                    }
566

567
                    // SEP set separator (empty on first run)
568
569
                    $contentLevel .= $columnValueSeparator;
                    $columnValueSeparator = $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_RSEP]);
570

Carsten  Rose's avatar
Carsten Rose committed
571
                    // RBEG
572
                    $rbeg = $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_RBEG]);
573

574
                    // RBGD: even/odd rows
575
                    $contentLevel .= str_replace(TOKEN_RBGD, $arrRbgd[$rowIndex % 2], $rbeg);
576

577
578
                    //-----------------------------
                    // COLUMNS: Collect all columns
579
                    $contentLevel .= $this->collectRow($row, $newKeys, $fullLevel, $rowIndex);
580

581
                    // REND
582
                    $contentLevel .= $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_REND]);
583

584
                    // Trigger subqueries of this level
Nicola Chiapolini's avatar
Nicola Chiapolini committed
585
586
587
                    $contentSubquery = $this->triggerReport($cur_level + 1, $this->indexArray[$counter], $counter + 1);
                    $contentSubqueries .= $contentSubquery;
                    $contentLevel .= $contentSubquery;
588

589
                    // RENR
590
                    $contentLevel .= $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_RENR]);
591
                }
592
593
            }

Nicola Chiapolini's avatar
Nicola Chiapolini committed
594
595
596
597
598

            /////////////////////////////////////////////////
            //    Render TAIL, ALT_HEAD, ALT_SQL, STAIL    //
            /////////////////////////////////////////////////

599
            if ($rowTotal > 0) {
600
                // tail
601
                $contentLevel .= $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_TAIL]);
602
            } else {
603
                // althead
604
                $contentLevel .= $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_ALT_HEAD]);
605
                // altsql
606
                $sql = $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_ALT_SQL]);
607
608
                if (!empty($sql)) {
                    $result = $this->db->sql($sql, ROW_KEYS, array(), '', $keys, $stat);
609

610
611
                    $newKeys = $this->splitColumnNames($keys);

612
613
614
615
616
                    $this->variables->resultArray[$fullLevel . ".line."][LINE_ALT_TOTAL] = $rowTotal;
                    $this->variables->resultArray[$fullLevel . ".line."][LINE_ALT_COUNT] = is_array($result) ? 1 : 0;
                    $this->variables->resultArray[$fullLevel . ".line."][LINE_ALT_INSERT_ID] = $stat[DB_INSERT_ID] ?? 0;

                    if (is_array($result)) {
617
618
                        foreach ($result as $row) {
                            $rowIndex = 0;
619
                            $contentLevel .= $this->collectRow($row, $newKeys, $fullLevel, $rowIndex);
620
                        }
621
622
                    }
                }
623
            }
624

625
626
            $contentLevel .= $this->variables->doVariables($this->frArray[$fullLevel . "." . TOKEN_STAIL]);

Nicola Chiapolini's avatar
Nicola Chiapolini committed
627
628
629
630
631
632
633
634
635
636
637
638
639
640

            ///////////////////////
            //    render TWIG    //
            ///////////////////////

            $twig_template = $this->frArray[$fullLevel . "." . TOKEN_TWIG];
            if ($twig_template !== '') {
                $contentLevel = $this->renderTwig($twig_template, $result, $keys);
            }

            ////////////////////////////////////////////////////////////
            //    Show / Hide / Save Level Content  (TOKEN_CONTENT)   //
            ////////////////////////////////////////////////////////////

641
642
643
            $token = $this->frArray[$fullLevel . "." . TOKEN_CONTENT];
            switch ($token) {

Nicola Chiapolini's avatar
Nicola Chiapolini committed
644
645
646
647
                case TOKEN_CONTENT_HIDE_LEVEL:
                    $this->variables->resultArray[$fullLevel . ".line."][TOKEN_CONTENT] = $contentLevel;
                    $contentLevel = $contentSubqueries;
                    break;
648
649
650
651
652
653
654
                case TOKEN_CONTENT_HIDE:
                    $this->variables->resultArray[$fullLevel . ".line."][TOKEN_CONTENT] = $contentLevel;
                    $contentLevel = '';
                    break;
                case TOKEN_CONTENT_STORE:
                    $this->variables->resultArray[$fullLevel . ".line."][TOKEN_CONTENT] = $contentLevel;
                    break;
655
                case TOKEN_CONTENT_SHOW:
656
657
658
659
                case '':
                    break;

                default:
Marc Egger's avatar
Marc Egger committed
660
                    throw new \UserReportException ("Unknown token: $token in Line '$fullLevel''", ERROR_UNKNOWN_TOKEN);
661
662
663
664
                    break;
            }

            $content .= $contentLevel;
665

Nicola Chiapolini's avatar
Nicola Chiapolini committed
666
667
668
669
670

            ///////////////////////////////
            //    Switch to next Level   //
            ///////////////////////////////

671
672
            ++$counter;
            if (isset($this->indexArray[$counter]) && is_array($this->indexArray[$counter])) {
673
                $fullLevel = implode(".", $this->indexArray[$counter]);
674
            } else {
675
                $fullLevel = '';
676
            }
677
678
        }
        return $content;
679
    }
680

681
682
    /**
     * Called with an array of column names.
Carsten  Rose's avatar
Carsten Rose committed
683
     * Each column name can be split in multiple string by '|': [s1[|s2[|s3]]]
684
685
686
687
688
689
690
691
692
693
     * Each s1|s2|s3 can be:  {title}, _{special colum name}, _hide, _noWrap, _={title},  _+{tag}, _<{tag1}><{tag2}>
     *
     * Return an Array: newKeys[idx][C_FULL|C_TITLE|C_NO_WRAP|C_HIDE]
     *
     * @param array $keys
     * @param array $fSkipWrap
     * @return array
     */
    private function splitColumnNames(array $keys, array $fSkipWrap = array()) {

Carsten  Rose's avatar
Carsten Rose committed
694
        // Split key names in title / specialColumnName / fSkipWrap / hide
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
        $ii = 0;
        $newKeys = array();

        foreach ($keys as $key) {
            $newKeys[$ii][C_FULL] = $key;

            $arr = explode(PARAM_DELIMITER, $key);
            foreach ($arr as $kk) {
                if (($kk[0] ?? '') == TOKEN_COLUMN_CTRL) {
                    switch ($kk) {
                        case TOKEN_COLUMN_CTRL . COLUMN_NO_WRAP:
                            $newKeys[$ii][C_NO_WRAP] = 1;
                            break;
                        case TOKEN_COLUMN_CTRL . COLUMN_HIDE:
                            $newKeys[$ii][C_HIDE] = 1;
                            break;
                        default:
                            $newKeys[$ii][C_SPECIAL] = $kk;
                            break;
                    }
                } else {
                    $newKeys[$ii][C_TITLE] = $kk;
                }
            }

            // Explicit given fSkipWrap
            if (isset($fSkipWrap[$ii])) {
                $newKeys[$ii][C_NO_WRAP] = $fSkipWrap[$ii];
            }

            // Fallback, if no dedicated title is given.
            if (!isset($newKeys[$ii][C_TITLE])) {
                // If no title is given, check if there is a specialColumnName (backward compatibility)
                $newKeys[$ii][C_TITLE] = isset($newKeys[$ii][C_SPECIAL]) ? $newKeys[$ii][C_SPECIAL] : $newKeys[$ii][C_FULL];
            }

            if (($newKeys[$ii][C_TITLE][0] ?? '') == TOKEN_COLUMN_CTRL) {
                $newKeys[$ii][C_TITLE] = substr($newKeys[$ii][C_TITLE], 1);
            }
            $ii++;
        }

        return $newKeys;
    }

Nicola Chiapolini's avatar
Nicola Chiapolini committed
740
741
742
    /**
     * Render given Twig template with content from $result
     *
743
744
745
746
     * @param $twig_template
     * @param $result
     * @param $keys
     * @return string
Nicola Chiapolini's avatar
Nicola Chiapolini committed
747
     * @throws \CodeException
748
749
750
     * @throws \Twig_Error_Loader
     * @throws \Twig_Error_Runtime
     * @throws \Twig_Error_Syntax
Nicola Chiapolini's avatar
Nicola Chiapolini committed
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
     * @throws \UserFormException
     * @throws \UserReportException
     */
    private function renderTwig($twig_template, $result, $keys) {
        if (count(array_unique($keys)) !== count($keys)) {
            throw new \UserReportException("Twig Error: There are multiple columns with the same name in the SQL query.", ERROR_TWIG_COLUMN_NOT_UNIQUE);
        }

        // Turn Result into Associative array
        $resultAssoc = array();
        foreach ($result as $i => $row) {
            foreach ($row as $j => $value) {
                $resultAssoc[$i][$keys[$j]] = $value;
            }
        }

        $tmpl_start = substr($twig_template, 0, 5);

        if ($tmpl_start == "file:") {
            $loader = new \Twig\Loader\FilesystemLoader([".", "typo3conf/ext/qfq/Resources/Public/twig_templates"]);
            $twig_template = substr($twig_template, 5);
        } else {
            $loader = new \Twig\Loader\ArrayLoader(array(
                "string_template" => trim($twig_template, '"\''))  # trim is needed for backward compatibility for MNF
            );
            $twig_template = "string_template";
        }

        $twig = new \Twig\Environment($loader, array());

        // Add QFQ Link Filter
        $filter = new \Twig\TwigFilter('qfqlink', function ($string) {
            return $this->link->renderLink($string);
        }, ['is_safe' => ['html']]);
        $twig->addFilter($filter);

Marc Egger's avatar
Marc Egger committed
787
788
789
        // Json decode Filter
        // E.g.: {% set obj = '["this is one", "this is two"]' | json_decode%}
        $filter = new \Twig\TwigFilter('json_decode', function ($string) {
790
            return json_decode($string, true);
Marc Egger's avatar
Marc Egger committed
791
792
793
        }, ['is_safe' => ['html']]);
        $twig->addFilter($filter);

Nicola Chiapolini's avatar
Nicola Chiapolini committed
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
        // render Twig
        $contentTwig = $twig->render($twig_template, array(
            'context' => $resultAssoc,  // backward compatibility for MNF
            'result' => $resultAssoc,
            'store' => array(
                'record' => $this->store->getStore(STORE_RECORD),
                'sip' => $this->store->getStore(STORE_SIP),
                'typo3' => $this->store->getStore(STORE_TYPO3),
                'user' => $this->store->getStore(STORE_USER),
                'system' => $this->store->getStore(STORE_SYSTEM),
            )
        ));
        return $contentTwig;
    }

809
810
811
812
813
814
815
    /**
     * Determine value:
     * 1) if one specified in line: take it
     * 2) if one specified in upper level: take it
     * 3) if none above take default
     * Set value on $full_level
     *
816
817
818
819
820
     * @param string $level_key - 'db' or 'debug'
     * @param string $full_super_level - f.e.: 10.10.
     * @param string $full_level - f.e.: 10.10.10.
     * @param string $cur_level - f.e.: 2
     * @param string $default - f.e.: 0
Carsten  Rose's avatar
Carsten Rose committed
821
     *
822
823
824
825
     * @return   string  The calculated value.
     */
    private function getValueParentDefault($level_key, $full_super_level, $full_level, $cur_level, $default) {

826
        if (!empty($this->frArray[$full_level . "." . $level_key])) {
827
828
829
830
831
            $value = $this->frArray[$full_level . "." . $level_key];
        } else {
            if ($cur_level == 1) {
                $value = $default;
            } else {
832
                $value = $this->variables->resultArray[$full_super_level . ".line."][$level_key] ?? '';
833
834
835
836
837
            }
        }
        $this->variables->resultArray[$full_level . ".line."][$level_key] = $value;

        return ($value);
838
    }
839
840
841
842

    /**
     * Steps through 'row' and collects all columns
     *
Carsten  Rose's avatar
Carsten Rose committed
843
844
     * @param array $row Recent row fetch from sql resultset.
     * @param array $keys List of all columnnames
845
846
     * @param string $full_level Recent position to work on.
     * @param string $rowIndex Index of recent row in resultset.
Carsten  Rose's avatar
Carsten Rose committed
847
     *
848
     * @return string               Collected content of all printable columns
Marc Egger's avatar
Marc Egger committed
849
850
851
     * @throws \CodeException
     * @throws \DbException
     * @throws \DownloadException
852
853
854
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
855
856
857
858
859
     * @throws \Twig\Error\LoaderError
     * @throws \Twig\Error\RuntimeError
     * @throws \Twig\Error\SyntaxError
     * @throws \UserFormException
     * @throws \UserReportException
860
     */
861
    private function collectRow(array $row, array $keys, $full_level, $rowIndex) {
862
        $content = "";
863
        $assoc = array();
864

865
        $fsep = '';
Carsten  Rose's avatar
Carsten Rose committed
866

867
868
        for ($ii = 0; $ii < count($keys); $ii++) {

869
            // Debugging
870
            $this->store->setVar(SYSTEM_REPORT_COLUMN_INDEX, $ii + 1, STORE_SYSTEM);
871
            $this->store->setVar(SYSTEM_REPORT_COLUMN_NAME, $keys[$ii][C_FULL], STORE_SYSTEM);
872
            $this->store->setVar(SYSTEM_REPORT_COLUMN_VALUE, $row[$ii], STORE_SYSTEM);
873

874
875
            $flagOutput = false;
            $renderedColumn = $this->renderColumn($ii, $keys[$ii], $row[$ii], $full_level, $rowIndex, $flagOutput);
876

877
            $keyAssoc = OnString::stripFirstCharIf(TOKEN_COLUMN_CTRL, $keys[$ii][C_TITLE]);
878
            $keyAssoc = OnString::stripFirstCharIf(COLUMN_STORE_USER, $keyAssoc);
879
880
            if ($keyAssoc != '') {
                $assoc[$keyAssoc] = $row[$ii];
881
                $assoc[REPORT_TOKEN_FINAL_VALUE . $keyAssoc] = $renderedColumn;
882
883
            }

884
            if ($flagOutput) {
885
                //prints
Carsten  Rose's avatar
Carsten Rose committed
886

887
                if (!isset($keys[$ii][C_NO_WRAP])) {
Carsten  Rose's avatar
Carsten Rose committed
888
889
890
891
                    $content .= $this->variables->doVariables($fsep);
                    $content .= $this->variables->doVariables($this->frArray[$full_level . "." . TOKEN_FBEG]);
                }

892
                $content .= $renderedColumn;
Carsten  Rose's avatar
Carsten Rose committed
893

894
                if (!isset($keys[$ii][C_NO_WRAP])) {
Carsten  Rose's avatar
Carsten Rose committed
895
896
897
                    $content .= $this->variables->doVariables($this->frArray[$full_level . "." . TOKEN_FEND]);
                }

898
                $fsep = $this->frArray[$full_level . "." . TOKEN_FSEP];
899
900
            }
        }
Carsten  Rose's avatar
Carsten Rose committed
901

902
        $this->store->appendToStore($assoc, STORE_RECORD);
903

904
        return ($content);
905
    }
906
907
908
909

    /**
     * Renders column depending of column name (if name is a reserved column name)
     *
910
     * @param string $columnIndex
911
     * @param array $columnCtrl
912
913
914
     * @param string $columnValue
     * @param string $full_level
     * @param string $rowIndex
915
     * @param $flagOutput
Carsten  Rose's avatar
Carsten Rose committed
916
     *
917
     * @return string rendered column
Marc Egger's avatar
Marc Egger committed
918
919
920
     * @throws \CodeException
     * @throws \DbException
     * @throws \DownloadException
921
922
923
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
924
925
926
927
928
     * @throws \Twig\Error\LoaderError
     * @throws \Twig\Error\RuntimeError
     * @throws \Twig\Error\SyntaxError
     * @throws \UserFormException
     * @throws \UserReportException
929
     */
930
    private function renderColumn($columnIndex, array $columnCtrl, $columnValue, $full_level, $rowIndex, &$flagOutput) {
931
        $content = "";
932
933
        $flagControl = false;
        $flagOutput = true;
934

935
        // Special column name:  '_...'? Empty column names are allowed: check with isset
936
        if (($columnCtrl[C_SPECIAL][0] ?? '') === TOKEN_COLUMN_CTRL) {
937
            $flagControl = true;
938
            $columnName = substr($columnCtrl[C_SPECIAL], 1);
939

940
941
            // Special column name and hide output
            if (isset($columnCtrl[C_HIDE])) {
942
943
                $flagOutput = false;
            }
944

945
946
947
            switch ($columnName) {
                case COLUMN_LINK:
                    $content .= $this->link->renderLink($columnValue);
948
949
                    break;

950
951
952
953
                case COLUMN_EXEC:
                    $rc = '';
                    $content .= Support::qfqExec($columnValue, $rc);
                    break;
954

955
956
957
958
959
960
961
962
963
964
965
966
967
                // Uppercase 'P'
                case COLUMN_PPAGE:
                case COLUMN_PPAGEC:
                case COLUMN_PPAGED:
                case COLUMN_PPAGEE:
                case COLUMN_PPAGEH:
                case COLUMN_PPAGEI:
                case COLUMN_PPAGEN:
                case COLUMN_PPAGES:
                    $lowerColumnName = strtolower($columnName);
                    $tokenizedValue = $this->doFixColPosPage($columnName, $columnValue);
                    $linkValue = $this->doPage($lowerColumnName, $tokenizedValue);
                    $content .= $this->link->renderLink($linkValue);
968
969
                    break;

970
971
972
973
974
975
976
977
978
979
980
981
                // Lowercase 'P'
                case COLUMN_PAGE:
                case COLUMN_PAGEC:
                case COLUMN_PAGED:
                case COLUMN_PAGEE:
                case COLUMN_PAGEH:
                case COLUMN_PAGEI:
                case COLUMN_PAGEN:
                case COLUMN_PAGES:
                    $linkValue = $this->doPage($columnName, $columnValue);
                    $content .= $this->link->renderLink($linkValue);
                    break;
982

983
984
985
                case COLUMN_YANK:
                    $linkValue = $this->doYank($columnName, $columnValue);
                    $content .= $this->link->renderLink($linkValue);
Carsten  Rose's avatar
Carsten Rose committed
986
987
                    break;

988
989
990
991
992
993
994
995
                case COLUMN_PPDF:
                case COLUMN_FFILE:
                case COLUMN_ZZIP:
                    $lowerColumnName = strtolower($columnName);
                    $tokenizedValue = $this->doFixColPosDownload($columnValue);
                    $linkValue = $this->doDownload($lowerColumnName, $tokenizedValue);
                    $content .= $this->link->renderLink($linkValue);
                    break;
996

997
998
999
1000
                case COLUMN_PDF:
                case COLUMN_FILE:
                case COLUMN_ZIP:
                case COLUMN_EXCEL:
For faster browsing, not all history is shown. View entire blame