99 lines
3.7 KiB
PHP
99 lines
3.7 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the TYPO3 CMS project.
|
|
*
|
|
* It is free software; you can redistribute it and/or modify it under
|
|
* the terms of the GNU General Public License, either version 2
|
|
* of the License, or any later version.
|
|
*
|
|
* For the full copyright and license information, please read the
|
|
* LICENSE.txt file that was distributed with this source code.
|
|
*
|
|
* The TYPO3 project - inspiring people to share!
|
|
*/
|
|
|
|
namespace TYPO3\CMS\Frontend\DataProcessing;
|
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|
use TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor;
|
|
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
|
|
use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
|
|
|
|
/**
|
|
* Fetch records from the database, using the default .select syntax from TypoScript.
|
|
*
|
|
* This way, e.g. a FLUIDTEMPLATE cObject can iterate over the array of records.
|
|
*
|
|
* Example TypoScript configuration:
|
|
*
|
|
* 10 = TYPO3\CMS\Frontend\DataProcessing\DatabaseQueryProcessor
|
|
* 10 {
|
|
* table = tt_address
|
|
* pidInList = 123
|
|
* where = company="Acme" AND first_name="Ralph"
|
|
* orderBy = sorting DESC
|
|
* as = addresses
|
|
* dataProcessing {
|
|
* 10 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
|
|
* 10 {
|
|
* references.fieldName = image
|
|
* }
|
|
* }
|
|
* }
|
|
*
|
|
* where "as" means the variable to be containing the result-set from the DB query.
|
|
*/
|
|
readonly class DatabaseQueryProcessor implements DataProcessorInterface
|
|
{
|
|
public function __construct(protected ContentDataProcessor $contentDataProcessor) {}
|
|
|
|
/**
|
|
* Fetches records from the database as an array
|
|
*
|
|
* @param ContentObjectRenderer $cObj The data of the content element or page
|
|
* @param array $contentObjectConfiguration The configuration of Content Object
|
|
* @param array $processorConfiguration The configuration of this processor
|
|
* @param array $processedData Key/value store of processed data (e.g. to be passed to a Fluid View)
|
|
*
|
|
* @return array the processed data as key/value store
|
|
*/
|
|
public function process(ContentObjectRenderer $cObj, array $contentObjectConfiguration, array $processorConfiguration, array $processedData)
|
|
{
|
|
if (isset($processorConfiguration['if.']) && !$cObj->checkIf($processorConfiguration['if.'])) {
|
|
return $processedData;
|
|
}
|
|
|
|
// the table to query, if none given, exit
|
|
$tableName = $cObj->stdWrapValue('table', $processorConfiguration);
|
|
if (empty($tableName)) {
|
|
return $processedData;
|
|
}
|
|
if (isset($processorConfiguration['table.'])) {
|
|
unset($processorConfiguration['table.']);
|
|
}
|
|
if (isset($processorConfiguration['table'])) {
|
|
unset($processorConfiguration['table']);
|
|
}
|
|
|
|
// The variable to be used within the result
|
|
$targetVariableName = $cObj->stdWrapValue('as', $processorConfiguration, 'records');
|
|
|
|
// Execute a SQL statement to fetch the records
|
|
$records = $cObj->getRecords($tableName, $processorConfiguration);
|
|
$request = $cObj->getRequest();
|
|
$processedRecordVariables = [];
|
|
foreach ($records as $key => $record) {
|
|
$recordContentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
|
|
$recordContentObjectRenderer->setRequest($request);
|
|
$recordContentObjectRenderer->start($record, $tableName);
|
|
$processedRecordVariables[$key] = ['data' => $record];
|
|
$processedRecordVariables[$key] = $this->contentDataProcessor->process($recordContentObjectRenderer, $processorConfiguration, $processedRecordVariables[$key]);
|
|
}
|
|
|
|
$processedData[$targetVariableName] = $processedRecordVariables;
|
|
|
|
return $processedData;
|
|
}
|
|
}
|