[BUGFIX] CsvUtility method csvToArray does not handle enclosures right
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / DataProcessing / CommaSeparatedValueProcessor.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\CsvUtility;
18 use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
19 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
20 use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
21
22 /**
23 * This data processor will take field data formatted as a string, where each line, separated by line feed,
24 * represents a row. By default columns are separated by the delimiter character "comma ,",
25 * and can be enclosed by the character 'quotation mark "', like the default in a regular CSV file.
26 *
27 * An example of such a field is "bodytext" in the CType "table".
28 *
29 * The table data is transformed to a multi dimensional array, taking the delimiter and enclosure into account,
30 * before it is passed to the view.
31 *
32 * Example field data:
33 *
34 * This is row 1 column 1|This is row 1 column 2|This is row 1 column 3
35 * This is row 2 column 1|This is row 2 column 2|This is row 2 column 3
36 * This is row 3 column 1|This is row 3 column 2|This is row 3 column 3
37 *
38 * Example TypoScript configuration:
39 *
40 * 10 = \TYPO3\CMS\Frontend\ContentObject\DataProcessing\CommaSeparatedValueProcessor
41 * 10 {
42 * if.isTrue.field = bodytext
43 * fieldName = bodytext
44 * fieldDelimiter = |
45 * fieldEnclosure =
46 * maximumColumns = 2
47 * as = table
48 * }
49 *
50 * whereas "table" can be used as a variable {table} inside Fluid for iteration.
51 *
52 * Using maximumColumns limits the amount of columns in the multi dimensional array.
53 * In the example, field data of the last column will be stripped off.
54 *
55 * Multi line cells are taken into account.
56 */
57 class CommaSeparatedValueProcessor implements DataProcessorInterface {
58
59 /**
60 * Process CSV field data to split into a multi dimensional array
61 *
62 * @param ContentObjectRenderer $cObj The data of the content element or page
63 * @param array $contentObjectConfiguration The configuration of Content Object
64 * @param array $processorConfiguration The configuration of this processor
65 * @param array $processedData Key/value store of processed data (e.g. to be passed to a Fluid View)
66 * @return array the processed data as key/value store
67 */
68 public function process(ContentObjectRenderer $cObj, array $contentObjectConfiguration, array $processorConfiguration, array $processedData) {
69
70 if (isset($processorConfiguration['if.']) && !$cObj->checkIf($processorConfiguration['if.'])) {
71 return $processedData;
72 }
73
74 // The field name to process
75 $fieldName = $cObj->stdWrapValue('fieldName', $processorConfiguration);
76 if (empty($fieldName)) {
77 return $processedData;
78 }
79
80 $originalValue = $cObj->data[$fieldName];
81
82 // Set the target variable
83 $targetVariableName = $cObj->stdWrapValue('as', $processorConfiguration, $fieldName);
84
85 // Set the maximum amount of columns
86 $maximumColumns = $cObj->stdWrapValue('maximumColumns', $processorConfiguration, 0);
87
88 // Set the field delimiter which is "," by default
89 $fieldDelimiter = $cObj->stdWrapValue('fieldDelimiter', $processorConfiguration, ',');
90
91 // Set the field enclosure which is " by default
92 $fieldEnclosure = $cObj->stdWrapValue('fieldEnclosure', $processorConfiguration, '"');
93
94 $processedData[$targetVariableName] = CsvUtility::csvToArray(
95 $originalValue,
96 $fieldDelimiter,
97 $fieldEnclosure,
98 (int)$maximumColumns
99 );
100
101 return $processedData;
102 }
103 }