[BUGFIX] Fix "orderBy" in DatabaseQueryProcessor example
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / DataProcessing / DatabaseQueryProcessor.php
1 <?php
2 namespace TYPO3\CMS\Frontend\DataProcessing;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor;
19 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
20 use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
21
22 /**
23 * Fetch records from the database, using the default .select syntax from TypoScript.
24 *
25 * This way, e.g. a FLUIDTEMPLATE cObject can iterate over the array of records.
26 *
27 * Example TypoScript configuration:
28 *
29 * 10 = TYPO3\CMS\Frontend\DataProcessing\DatabaseQueryProcessor
30 * 10 {
31 * table = tt_address
32 * pidInList = 123
33 * where = company="Acme" AND first_name="Ralph"
34 * orderBy = RAND()
35 * as = addresses
36 * dataProcessing {
37 * 10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
38 * 10 {
39 * references.fieldName = image
40 * }
41 * }
42 * }
43 *
44 * where "as" means the variable to be containing the result-set from the DB query.
45 */
46 class DatabaseQueryProcessor implements DataProcessorInterface
47 {
48 /**
49 * @var ContentDataProcessor
50 */
51 protected $contentDataProcessor;
52
53 /**
54 * Constructor
55 */
56 public function __construct()
57 {
58 $this->contentDataProcessor = GeneralUtility::makeInstance(ContentDataProcessor::class);
59 }
60
61 /**
62 * Fetches records from the database as an array
63 *
64 * @param ContentObjectRenderer $cObj The data of the content element or page
65 * @param array $contentObjectConfiguration The configuration of Content Object
66 * @param array $processorConfiguration The configuration of this processor
67 * @param array $processedData Key/value store of processed data (e.g. to be passed to a Fluid View)
68 *
69 * @return array the processed data as key/value store
70 */
71 public function process(ContentObjectRenderer $cObj, array $contentObjectConfiguration, array $processorConfiguration, array $processedData)
72 {
73 if (isset($processorConfiguration['if.']) && !$cObj->checkIf($processorConfiguration['if.'])) {
74 return $processedData;
75 }
76
77 // the table to query, if none given, exit
78 $tableName = $cObj->stdWrapValue('table', $processorConfiguration);
79 if (empty($tableName)) {
80 return $processedData;
81 }
82 if (isset($processorConfiguration['table.'])) {
83 unset($processorConfiguration['table.']);
84 }
85 if (isset($processorConfiguration['table'])) {
86 unset($processorConfiguration['table']);
87 }
88
89 // The variable to be used within the result
90 $targetVariableName = $cObj->stdWrapValue('as', $processorConfiguration, 'records');
91
92 // Execute a SQL statement to fetch the records
93 $records = $cObj->getRecords($tableName, $processorConfiguration);
94 $processedRecordVariables = [];
95 foreach ($records as $key => $record) {
96 /** @var ContentObjectRenderer $recordContentObjectRenderer */
97 $recordContentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
98 $recordContentObjectRenderer->start($record, $tableName);
99 $processedRecordVariables[$key] = ['data' => $record];
100 $processedRecordVariables[$key] = $this->contentDataProcessor->process($recordContentObjectRenderer, $processorConfiguration, $processedRecordVariables[$key]);
101 }
102
103 $processedData[$targetVariableName] = $processedRecordVariables;
104
105 return $processedData;
106 }
107 }