TYPO3 CMS  TYPO3_7-6
DataSet.php
Go to the documentation of this file.
1 <?php
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 
18 
22 class DataSet
23 {
27  protected $data;
28 
34  public static function read($fileName, $applyDefaultValues = false)
35  {
36  $data = self::parseData(self::readData($fileName));
37 
38  if ($applyDefaultValues) {
39  $data = self::applyDefaultValues($data);
40  }
41 
43  \TYPO3\CMS\Core\Tests\Functional\DataHandling\Framework\DataSet::class,
44  $data
45  );
46  }
47 
53  protected static function readData($fileName)
54  {
55  if (!file_exists($fileName)) {
56  throw new \RuntimeException('File "' . $fileName . '" does not exist');
57  }
58 
59  $rawData = [];
60  $fileHandle = fopen($fileName, 'r');
61  while (($values = fgetcsv($fileHandle, 0)) !== false) {
62  $rawData[] = $values;
63  }
64  fclose($fileHandle);
65  return $rawData;
66  }
67 
78  protected static function parseData(array $rawData)
79  {
80  $data = [];
81  $tableName = null;
82  $fieldCount = null;
83  $idIndex = null;
84  foreach ($rawData as $values) {
85  if (!empty($values[0])) {
86  // Skip comment lines, starting with "#"
87  if ($values[0]{0} === '#') {
88  continue;
89  }
90  $tableName = $values[0];
91  $fieldCount = null;
92  $idIndex = null;
93  if (!isset($data[$tableName])) {
94  $data[$tableName] = [];
95  }
96  } elseif (implode('', $values) === '') {
97  $tableName = null;
98  $fieldCount = null;
99  $idIndex = null;
100  } elseif ($tableName !== null && !empty($values[1])) {
101  array_shift($values);
102  if (!isset($data[$tableName]['fields'])) {
103  $data[$tableName]['fields'] = [];
104  foreach ($values as $value) {
105  if (empty($value)) {
106  continue;
107  }
108  $data[$tableName]['fields'][] = $value;
109  $fieldCount = count($data[$tableName]['fields']);
110  }
111  if (in_array('uid', $values)) {
112  $idIndex = array_search('uid', $values);
113  $data[$tableName]['idIndex'] = $idIndex;
114  }
115  } else {
116  if (!isset($data[$tableName]['elements'])) {
117  $data[$tableName]['elements'] = [];
118  }
119  $values = array_slice($values, 0, $fieldCount);
120  foreach ($values as &$value) {
121  if ($value === '\\NULL') {
122  $value = null;
123  }
124  }
125  unset($value);
126  $element = array_combine($data[$tableName]['fields'], $values);
127  if ($idIndex !== null) {
128  $data[$tableName]['elements'][$values[$idIndex]] = $element;
129  } else {
130  $data[$tableName]['elements'][] = $element;
131  }
132  }
133  }
134  }
135  return $data;
136  }
137 
145  protected static function applyDefaultValues(array $data)
146  {
147  foreach ($data as $tableName => $sections) {
148  if (empty($GLOBALS['TCA'][$tableName]['columns'])) {
149  continue;
150  }
151 
152  $fields = $sections['fields'];
153 
154  foreach ($GLOBALS['TCA'][$tableName]['columns'] as $tcaFieldName => $tcaFieldConfiguration) {
155  // Skip if field was already imported
156  if (in_array($tcaFieldName, $fields)) {
157  continue;
158  }
159  // Skip if field is an enable-column (it's expected that those fields have proper DBMS defaults)
160  if (!empty($GLOBALS['TCA'][$tableName]['ctrl']['enablecolumns']) && in_array($tcaFieldName, $GLOBALS['TCA'][$tableName]['ctrl']['enablecolumns'])) {
161  continue;
162  }
163  // Skip if no default value is defined in the accordant TCA definition (NULL values might occur as well)
164  if (empty($tcaFieldConfiguration['config']) || !array_key_exists('default', $tcaFieldConfiguration['config'])) {
165  continue;
166  }
167 
168  $data[$tableName]['fields'][] = $tcaFieldName;
169  foreach ($data[$tableName]['elements'] as &$element) {
170  $element[$tcaFieldName] = $tcaFieldConfiguration['config']['default'];
171  }
172  }
173  }
174  return $data;
175  }
176 
180  public function __construct(array $data)
181  {
182  $this->data = $data;
183  }
184 
188  public function getTableNames()
189  {
190  return array_keys($this->data);
191  }
192 
197  public function getFields($tableName)
198  {
199  $fields = null;
200  if (isset($this->data[$tableName]['fields'])) {
201  $fields = $this->data[$tableName]['fields'];
202  }
203  return $fields;
204  }
205 
210  public function getIdIndex($tableName)
211  {
212  $idIndex = null;
213  if (isset($this->data[$tableName]['idIndex'])) {
214  $idIndex = $this->data[$tableName]['idIndex'];
215  }
216  return $idIndex;
217  }
218 
223  public function getElements($tableName)
224  {
225  $elements = null;
226  if (isset($this->data[$tableName]['elements'])) {
227  $elements = $this->data[$tableName]['elements'];
228  }
229  return $elements;
230  }
231 
235  public function persist($fileName)
236  {
237  $fileHandle = fopen($fileName, 'w');
238 
239  foreach ($this->data as $tableName => $tableData) {
240  if (empty($tableData['fields']) || empty($tableData['elements'])) {
241  continue;
242  }
243 
244  $fields = $tableData['fields'];
245  array_unshift($fields, '');
246 
247  fputcsv($fileHandle, [$tableName]);
248  fputcsv($fileHandle, $fields);
249 
250  foreach ($tableData['elements'] as $element) {
251  array_unshift($element, '');
252  fputcsv($fileHandle, $element);
253  }
254  }
255 
256  fclose($fileHandle);
257  }
258 }
static read($fileName, $applyDefaultValues=false)
Definition: DataSet.php:34
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']