TYPO3 CMS  TYPO3_7-6
Collector.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 
21 {
25  protected $tableFields;
26 
30  protected $structure = [];
31 
35  protected $structurePaths = [];
36 
40  protected $records = [];
41 
45  public $cObj;
46 
47  public function addRecordData($content, array $configuration = null)
48  {
49  $recordIdentifier = $this->cObj->currentRecord;
50  list($tableName) = explode(':', $recordIdentifier);
51  $currentWatcherValue = $this->getCurrentWatcherValue();
52  $position = strpos($currentWatcherValue, '/' . $recordIdentifier);
53 
54  $recordData = $this->filterFields($tableName, $this->cObj->data);
55  $this->records[$recordIdentifier] = $recordData;
56 
57  if ($currentWatcherValue === $recordIdentifier) {
58  $this->structure[$recordIdentifier] = $recordData;
59  $this->structurePaths[$recordIdentifier] = [[]];
60  } elseif (!empty($position)) {
61  $levelIdentifier = substr($currentWatcherValue, 0, $position);
62  $this->addToStructure($levelIdentifier, $recordIdentifier, $recordData);
63  }
64  }
65 
66  public function addFileData($content, array $configuration = null)
67  {
68  $currentFile = $this->cObj->getCurrentFile();
69 
70  if ($currentFile instanceof \TYPO3\CMS\Core\Resource\File) {
71  $tableName = 'sys_file';
72  } elseif ($currentFile instanceof \TYPO3\CMS\Core\Resource\FileReference) {
73  $tableName = 'sys_file_reference';
74  } else {
75  return;
76  }
77 
78  $recordData = $this->filterFields($tableName, $currentFile->getProperties());
79  $recordIdentifier = $tableName . ':' . $currentFile->getUid();
80  $this->records[$recordIdentifier] = $recordData;
81 
82  $currentWatcherValue = $this->getCurrentWatcherValue();
83  $levelIdentifier = rtrim($currentWatcherValue, '/');
84  $this->addToStructure($levelIdentifier, $recordIdentifier, $recordData);
85  }
86 
92  protected function filterFields($tableName, array $recordData)
93  {
94  $recordData = array_intersect_key(
95  $recordData,
96  array_flip($this->getTableFields($tableName))
97  );
98  return $recordData;
99  }
100 
101  protected function addToStructure($levelIdentifier, $recordIdentifier, array $recordData)
102  {
103  $steps = explode('/', $levelIdentifier);
104  $structurePaths = [];
106 
107  foreach ($steps as $step) {
108  list($identifier, $fieldName) = explode('.', $step);
109  $structurePaths[] = $identifier;
110  $structurePaths[] = $fieldName;
111  if (!isset($structure[$identifier])) {
112  return;
113  }
114  $structure = &$structure[$identifier];
115  if (!isset($structure[$fieldName]) || !is_array($structure[$fieldName])) {
116  $structure[$fieldName] = [];
117  }
118  $structure = &$structure[$fieldName];
119  }
120 
121  $structure[$recordIdentifier] = $recordData;
122  $this->structurePaths[$recordIdentifier][] = $structurePaths;
123  }
124 
130  public function attachSection($content, array $configuration = null)
131  {
132  $section = [
133  'structure' => $this->structure,
134  'structurePaths' => $this->structurePaths,
135  'records' => $this->records,
136  ];
137 
138  $as = (!empty($configuration['as']) ? $configuration['as'] : null);
139  $this->getRenderer()->addSection($section, $as);
140  $this->reset();
141  }
142 
147  protected function getTableFields($tableName)
148  {
149  if (!isset($this->tableFields) && !empty($this->getFrontendController()->tmpl->setup['config.']['watcher.']['tableFields.'])) {
150  $this->tableFields = $this->getFrontendController()->tmpl->setup['config.']['watcher.']['tableFields.'];
151  foreach ($this->tableFields as &$fieldList) {
152  $fieldList = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $fieldList, true);
153  }
154  unset($fieldList);
155  }
156 
157  return !empty($this->tableFields[$tableName]) ? $this->tableFields[$tableName] : [];
158  }
159 
163  protected function getCurrentWatcherValue()
164  {
165  $watcherValue = null;
166  if (isset($this->getFrontendController()->register['watcher'])) {
167  $watcherValue = $this->getFrontendController()->register['watcher'];
168  }
169  return $watcherValue;
170  }
171 
175  protected function getRenderer()
176  {
177  return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
178  \TYPO3\CMS\Core\Tests\Functional\Framework\Frontend\Renderer::class
179  );
180  }
181 
185  protected function getFrontendController()
186  {
187  return $GLOBALS['TSFE'];
188  }
189 
195  protected function reset()
196  {
197  $this->structure = [];
198  $this->structurePaths = [];
199  $this->records = [];
200  }
201 }
attachSection($content, array $configuration=null)
Definition: Collector.php:130
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
addFileData($content, array $configuration=null)
Definition: Collector.php:66
addToStructure($levelIdentifier, $recordIdentifier, array $recordData)
Definition: Collector.php:101
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
addRecordData($content, array $configuration=null)
Definition: Collector.php:47