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