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