[!!!][TASK] Deprecate IconUtility
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / ExtDirect / ExtDirectServer.php
1 <?php
2 namespace TYPO3\CMS\Workspaces\ExtDirect;
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\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Imaging\Icon;
19 use TYPO3\CMS\Core\Imaging\IconFactory;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * ExtDirect server
24 */
25 class ExtDirectServer extends AbstractHandler {
26
27 /**
28 * @var \TYPO3\CMS\Workspaces\Service\GridDataService
29 */
30 protected $gridDataService;
31
32 /**
33 * @var \TYPO3\CMS\Workspaces\Service\StagesService
34 */
35 protected $stagesService;
36
37 /**
38 * Checks integrity of elements before peforming actions on them.
39 *
40 * @param stdClass $parameters
41 * @return array
42 */
43 public function checkIntegrity(\stdClass $parameters) {
44 $integrity = $this->createIntegrityService($this->getAffectedElements($parameters));
45 $integrity->check();
46 $response = array(
47 'result' => $integrity->getStatusRepresentation()
48 );
49 return $response;
50 }
51
52 /**
53 * Get List of workspace changes
54 *
55 * @param \stdClass $parameter
56 * @return array $data
57 */
58 public function getWorkspaceInfos($parameter) {
59 // To avoid too much work we use -1 to indicate that every page is relevant
60 $pageId = $parameter->id > 0 ? $parameter->id : -1;
61 if (!isset($parameter->language) || !\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($parameter->language)) {
62 $parameter->language = NULL;
63 }
64 $versions = $this->getWorkspaceService()->selectVersionsInWorkspace($this->getCurrentWorkspace(), 0, -99, $pageId, $parameter->depth, 'tables_select', $parameter->language);
65 $data = $this->getGridDataService()->generateGridListFromVersions($versions, $parameter, $this->getCurrentWorkspace());
66 return $data;
67 }
68
69 /**
70 * Gets the editing history of a record.
71 *
72 * @param stdClass $parameters
73 * @return array
74 */
75 public function getHistory($parameters) {
76 /** @var $historyService \TYPO3\CMS\Workspaces\Service\HistoryService */
77 $historyService = GeneralUtility::makeInstance(\TYPO3\CMS\Workspaces\Service\HistoryService::class);
78 $history = $historyService->getHistory($parameters->table, $parameters->liveId);
79 return array(
80 'data' => $history,
81 'total' => count($history)
82 );
83 }
84
85 /**
86 * Get List of available workspace actions
87 *
88 * @param \stdClass $parameter
89 * @return array $data
90 */
91 public function getStageActions(\stdClass $parameter) {
92 $currentWorkspace = $this->getCurrentWorkspace();
93 $stages = array();
94 if ($currentWorkspace != \TYPO3\CMS\Workspaces\Service\WorkspaceService::SELECT_ALL_WORKSPACES) {
95 $stages = $this->getStagesService()->getStagesForWSUser();
96 }
97 $data = array(
98 'total' => count($stages),
99 'data' => $stages
100 );
101 return $data;
102 }
103
104 /**
105 * Fetch further information to current selected workspace record.
106 *
107 * @param \stdClass $parameter
108 * @return array $data
109 */
110 public function getRowDetails($parameter) {
111 $diffReturnArray = array();
112 $liveReturnArray = array();
113 /** @var $diffUtility \TYPO3\CMS\Core\Utility\DiffUtility */
114 $diffUtility = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Utility\DiffUtility::class);
115 /** @var $parseObj \TYPO3\CMS\Core\Html\RteHtmlParser */
116 $parseObj = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Html\RteHtmlParser::class);
117 $liveRecord = BackendUtility::getRecord($parameter->table, $parameter->t3ver_oid);
118 $versionRecord = BackendUtility::getRecord($parameter->table, $parameter->uid);
119 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
120 $icon_Live = $iconFactory->getIconForRecord($parameter->table, $liveRecord, Icon::SIZE_SMALL)->render();
121 $icon_Workspace = $iconFactory->getIconForRecord($parameter->table, $versionRecord, Icon::SIZE_SMALL)->render();
122 $stagePosition = $this->getStagesService()->getPositionOfCurrentStage($parameter->stage);
123 $fieldsOfRecords = array_keys($liveRecord);
124 if ($GLOBALS['TCA'][$parameter->table]) {
125 if ($GLOBALS['TCA'][$parameter->table]['interface']['showRecordFieldList']) {
126 $fieldsOfRecords = $GLOBALS['TCA'][$parameter->table]['interface']['showRecordFieldList'];
127 $fieldsOfRecords = GeneralUtility::trimExplode(',', $fieldsOfRecords, TRUE);
128 }
129 }
130 foreach ($fieldsOfRecords as $fieldName) {
131 // check for exclude fields
132 if ($GLOBALS['BE_USER']->isAdmin() || $GLOBALS['TCA'][$parameter->table]['columns'][$fieldName]['exclude'] == 0 || GeneralUtility::inList($GLOBALS['BE_USER']->groupData['non_exclude_fields'], $parameter->table . ':' . $fieldName)) {
133 // call diff class only if there is a difference
134 if ((string)$liveRecord[$fieldName] !== (string)$versionRecord[$fieldName]) {
135 // Select the human readable values before diff
136 $liveRecord[$fieldName] = BackendUtility::getProcessedValue(
137 $parameter->table,
138 $fieldName,
139 $liveRecord[$fieldName],
140 0,
141 1,
142 FALSE,
143 $liveRecord['uid']
144 );
145 $versionRecord[$fieldName] = BackendUtility::getProcessedValue(
146 $parameter->table,
147 $fieldName,
148 $versionRecord[$fieldName],
149 0,
150 1,
151 FALSE,
152 $versionRecord['uid']
153 );
154 // Get the field's label. If not available, use the field name
155 $fieldTitle = $GLOBALS['LANG']->sL(BackendUtility::getItemLabel($parameter->table, $fieldName));
156 if (empty($fieldTitle)) {
157 $fieldTitle = $fieldName;
158 }
159 if ($GLOBALS['TCA'][$parameter->table]['columns'][$fieldName]['config']['type'] == 'group' && $GLOBALS['TCA'][$parameter->table]['columns'][$fieldName]['config']['internal_type'] == 'file') {
160 $versionThumb = BackendUtility::thumbCode($versionRecord, $parameter->table, $fieldName, '');
161 $liveThumb = BackendUtility::thumbCode($liveRecord, $parameter->table, $fieldName, '');
162 $diffReturnArray[] = array(
163 'field' => $fieldName,
164 'label' => $fieldTitle,
165 'content' => $versionThumb
166 );
167 $liveReturnArray[] = array(
168 'field' => $fieldName,
169 'label' => $fieldTitle,
170 'content' => $liveThumb
171 );
172 } else {
173 $diffReturnArray[] = array(
174 'field' => $fieldName,
175 'label' => $fieldTitle,
176 'content' => $diffUtility->makeDiffDisplay($liveRecord[$fieldName], $versionRecord[$fieldName])
177 );
178 $liveReturnArray[] = array(
179 'field' => $fieldName,
180 'label' => $fieldTitle,
181 'content' => $parseObj->TS_images_rte($liveRecord[$fieldName])
182 );
183 }
184 }
185 }
186 }
187 // Hook for modifying the difference and live arrays
188 // (this may be used by custom or dynamically-defined fields)
189 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['workspaces']['modifyDifferenceArray'])) {
190 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['workspaces']['modifyDifferenceArray'] as $className) {
191 $hookObject =& GeneralUtility::getUserObj($className);
192 $hookObject->modifyDifferenceArray($parameter, $diffReturnArray, $liveReturnArray, $diffUtility);
193 }
194 }
195 $commentsForRecord = $this->getCommentsForRecord($parameter->uid, $parameter->table);
196 return array(
197 'total' => 1,
198 'data' => array(
199 array(
200 'diff' => $diffReturnArray,
201 'live_record' => $liveReturnArray,
202 'path_Live' => $parameter->path_Live,
203 'label_Stage' => $parameter->label_Stage,
204 'stage_position' => $stagePosition['position'],
205 'stage_count' => $stagePosition['count'],
206 'comments' => $commentsForRecord,
207 'icon_Live' => $icon_Live,
208 'icon_Workspace' => $icon_Workspace
209 )
210 )
211 );
212 }
213
214 /**
215 * Gets an array with all sys_log entries and their comments for the given record uid and table
216 *
217 * @param int $uid uid of changed element to search for in log
218 * @param string $table Name of the record's table
219 * @return array
220 */
221 public function getCommentsForRecord($uid, $table) {
222 $sysLogReturnArray = array();
223 $sysLogRows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
224 'log_data,tstamp,userid',
225 'sys_log',
226 'action=6 and details_nr=30 AND tablename=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($table, 'sys_log')
227 . ' AND recuid=' . (int)$uid,
228 '',
229 'tstamp DESC'
230 );
231 foreach ($sysLogRows as $sysLogRow) {
232 $sysLogEntry = array();
233 $data = unserialize($sysLogRow['log_data']);
234 $beUserRecord = BackendUtility::getRecord('be_users', $sysLogRow['userid']);
235 $sysLogEntry['stage_title'] = $this->getStagesService()->getStageTitle($data['stage']);
236 $sysLogEntry['user_uid'] = $sysLogRow['userid'];
237 $sysLogEntry['user_username'] = is_array($beUserRecord) ? $beUserRecord['username'] : '';
238 $sysLogEntry['tstamp'] = BackendUtility::datetime($sysLogRow['tstamp']);
239 $sysLogEntry['user_comment'] = $data['comment'];
240 $sysLogReturnArray[] = $sysLogEntry;
241 }
242 return $sysLogReturnArray;
243 }
244
245 /**
246 * Gets all available system languages.
247 *
248 * @return array
249 */
250 public function getSystemLanguages() {
251 $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
252 $systemLanguages = array(
253 array(
254 'uid' => 'all',
255 'title' => \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('language.allLanguages', 'workspaces'),
256 'icon' => $iconFactory->getIcon('empty-empty', Icon::SIZE_SMALL)->render()
257 )
258 );
259 foreach ($this->getGridDataService()->getSystemLanguages() as $id => $systemLanguage) {
260 if ($id < 0) {
261 continue;
262 }
263 $systemLanguages[] = array(
264 'uid' => $id,
265 'title' => htmlspecialchars($systemLanguage['title']),
266 'icon' => $iconFactory->getIcon($systemLanguage['flagIcon'], Icon::SIZE_SMALL)->render()
267 );
268 }
269 $result = array(
270 'total' => count($systemLanguages),
271 'data' => $systemLanguages
272 );
273 return $result;
274 }
275
276 /**
277 * Gets the Grid Data Service.
278 *
279 * @return \TYPO3\CMS\Workspaces\Service\GridDataService
280 */
281 protected function getGridDataService() {
282 if (!isset($this->gridDataService)) {
283 $this->gridDataService = GeneralUtility::makeInstance(\TYPO3\CMS\Workspaces\Service\GridDataService::class);
284 }
285 return $this->gridDataService;
286 }
287
288 /**
289 * Gets the Stages Service.
290 *
291 * @return \TYPO3\CMS\Workspaces\Service\StagesService
292 */
293 protected function getStagesService() {
294 if (!isset($this->stagesService)) {
295 $this->stagesService = GeneralUtility::makeInstance(\TYPO3\CMS\Workspaces\Service\StagesService::class);
296 }
297 return $this->stagesService;
298 }
299
300 }