[FEATURE] Add Data Processor for fetching DB records
[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\ContentObjectRenderer;
19 use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
20 use TYPO3\CMS\Frontend\ContentObject\DataProcessingTrait;
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 * order = 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 use DataProcessingTrait;
48
49 /**
50 * Fetches records from the database as an array
51 *
52 * @param ContentObjectRenderer $cObj The data of the content element or page
53 * @param array $contentObjectConfiguration The configuration of Content Object
54 * @param array $processorConfiguration The configuration of this processor
55 * @param array $processedData Key/value store of processed data (e.g. to be passed to a Fluid View)
56 *
57 * @return array the processed data as key/value store
58 */
59 public function process(ContentObjectRenderer $cObj, array $contentObjectConfiguration, array $processorConfiguration, array $processedData) {
60 if (isset($processorConfiguration['if.']) && !$cObj->checkIf($processorConfiguration['if.'])) {
61 return $processedData;
62 }
63
64 // the table to query, if none given, exit
65 $tableName = $cObj->stdWrapValue('table', $processorConfiguration);
66 if (empty($tableName)) {
67 return $processedData;
68 }
69 if (isset($processorConfiguration['table.'])) {
70 unset($processorConfiguration['table.']);
71 }
72 if (isset($processorConfiguration['table'])) {
73 unset($processorConfiguration['table']);
74 }
75
76 // The variable to be used within the result
77 $targetVariableName = $cObj->stdWrapValue('as', $processorConfiguration, 'records');
78
79 // Execute a SQL statement to fetch the records
80 $records = $cObj->getRecords($tableName, $processorConfiguration);
81 $processedRecordVariables = array();
82 foreach ($records as $key => $record) {
83 /** @var ContentObjectRenderer $recordContentObjectRenderer */
84 $recordContentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
85 $recordContentObjectRenderer->start($record, $tableName);
86 $processedRecordVariables[$key] = array('data' => $record);
87 $processedRecordVariables[$key] = $this->processData($recordContentObjectRenderer, $processorConfiguration, $processedRecordVariables[$key]);
88 }
89
90 $processedData[$targetVariableName] = $processedRecordVariables;
91
92 return $processedData;
93 }
94 }