CommaSeparatedValueProcessor.php 3.84 KB
Newer Older
1
<?php
2

3
4
5
6
7
8
9
10
11
12
13
14
15
/*
 * 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!
 */

16
17
namespace TYPO3\CMS\Frontend\DataProcessing;

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use TYPO3\CMS\Core\Utility\CsvUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;

/**
 * This data processor will take field data formatted as a string, where each line, separated by line feed,
 * represents a row. By default columns are separated by the delimiter character "comma ,",
 * and can be enclosed by the character 'quotation mark "', like the default in a regular CSV file.
 *
 * An example of such a field is "bodytext" in the CType "table".
 *
 * The table data is transformed to a multi dimensional array, taking the delimiter and enclosure into account,
 * before it is passed to the view.
 *
 * Example field data:
 *
 * This is row 1 column 1|This is row 1 column 2|This is row 1 column 3
 * This is row 2 column 1|This is row 2 column 2|This is row 2 column 3
 * This is row 3 column 1|This is row 3 column 2|This is row 3 column 3
 *
 * Example TypoScript configuration:
 *
40
 * 10 = TYPO3\CMS\Frontend\DataProcessing\CommaSeparatedValueProcessor
41
42
43
44
 * 10 {
 *   if.isTrue.field = bodytext
 *   fieldName = bodytext
 *   fieldDelimiter = |
45
 *   fieldEnclosure = '
46
47
48
49
50
51
52
53
54
55
56
 *   maximumColumns = 2
 *   as = table
 * }
 *
 * whereas "table" can be used as a variable {table} inside Fluid for iteration.
 *
 * Using maximumColumns limits the amount of columns in the multi dimensional array.
 * In the example, field data of the last column will be stripped off.
 *
 * Multi line cells are taken into account.
 */
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class CommaSeparatedValueProcessor implements DataProcessorInterface
{
    /**
     * Process CSV field data to split into a multi dimensional 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;
        }
73

74
        // The field name to process
75
        $fieldName = $cObj->stdWrapValue('fieldName', $processorConfiguration);
76
77
78
        if (empty($fieldName)) {
            return $processedData;
        }
79

80
        $originalValue = (string)$cObj->data[$fieldName];
81

82
83
        // Set the target variable
        $targetVariableName = $cObj->stdWrapValue('as', $processorConfiguration, $fieldName);
84

85
86
        // Set the maximum amount of columns
        $maximumColumns = $cObj->stdWrapValue('maximumColumns', $processorConfiguration, 0);
87

88
        // Set the field delimiter which is "," by default
89
        $fieldDelimiter = (string)$cObj->stdWrapValue('fieldDelimiter', $processorConfiguration, ',');
90

91
        // Set the field enclosure which is " by default
92
        $fieldEnclosure = (string)$cObj->stdWrapValue('fieldEnclosure', $processorConfiguration, '"');
93

94
95
96
97
98
99
        $processedData[$targetVariableName] = CsvUtility::csvToArray(
            $originalValue,
            $fieldDelimiter,
            $fieldEnclosure,
            (int)$maximumColumns
        );
100

101
102
        return $processedData;
    }
103
}