e348d0c1c97e57d489e4e91893ce5b7e21be857e
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / ExtDirect / ActionHandler.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 * @author Workspaces Team (http://forge.typo3.org/projects/show/typo3v4-workspaces)
30 * @package Workspaces
31 * @subpackage ExtDirect
32 */
33 class tx_Workspaces_ExtDirect_ActionHandler extends tx_Workspaces_ExtDirect_AbstractHandler {
34 /**
35 * @var Tx_Workspaces_Service_Stages
36 */
37 protected $stageService;
38
39 /**
40 * Creates this object.
41 */
42 public function __construct() {
43 $this->stageService = t3lib_div::makeInstance('Tx_Workspaces_Service_Stages');
44 }
45
46 /**
47 * Generates a workspace preview link.
48 *
49 * @param integer $uid The ID of the record to be linked
50 * @return string the full domain including the protocol http:// or https://, but without the trailing '/'
51 */
52 public function generateWorkspacePreviewLink($uid) {
53 $ttlHours = intval($GLOBALS['BE_USER']->getTSConfigVal('options.workspaces.previewLinkTTLHours'));
54 $ttlHours = ($ttlHours ? $ttlHours : 24*2) * 3600;
55 $linkParams = array(
56 'ADMCMD_prev' => t3lib_BEfunc::compilePreviewKeyword('', $GLOBALS['BE_USER']->user['uid'], $ttlHours, $this->getCurrentWorkspace()),
57 'id' => $uid
58 );
59 return t3lib_BEfunc::getViewDomain($uid) . '/index.php?' . t3lib_div::implodeArrayForUrl('', $linkParams);
60 }
61
62 /**
63 * Swaps a sisngle record.
64 *
65 * @param string $table
66 * @param integer $t3ver_oid
67 * @param integer $orig_uid
68 * @return void
69 *
70 * @todo What about reporting errors back to the ExtJS interface? /olly/
71 */
72 public function swapSingleRecord($table, $t3ver_oid, $orig_uid) {
73 $cmd[$table][$t3ver_oid]['version'] = array(
74 'action' => 'swap',
75 'swapWith' => $orig_uid,
76 'swapIntoWS' => 1
77 );
78
79 $tce = t3lib_div::makeInstance ('t3lib_TCEmain');
80 $tce->start(array(), $cmd);
81 $tce->process_cmdmap();
82 }
83
84 /**
85 * Deletes a single record.
86 *
87 * @param string $table
88 * @param integer $uid
89 * @return void
90 *
91 * @todo What about reporting errors back to the ExtJS interface? /olly/
92 */
93 public function deleteSingleRecord($table, $uid) {
94 $cmd[$table][$uid]['version'] = array(
95 'action' => 'clearWSID'
96 );
97
98 $tce = t3lib_div::makeInstance ('t3lib_TCEmain');
99 $tce->start(array(), $cmd);
100 $tce->process_cmdmap();
101 }
102
103 /**
104 * Generates a view link for a page.
105 *
106 * @param string $table
107 * @param string $uid
108 * @return void
109 */
110 public function viewSingleRecord($table, $uid) {
111 return tx_Workspaces_Service_Workspaces::viewSingleRecord($table, $uid);
112 }
113
114
115 /**
116 * Saves the selected columns to be shown to the preferences of the current backend user.
117 *
118 * @param object $model
119 * @return void
120 */
121 public function saveColumnModel($model) {
122 $data = array();
123 foreach ($model as $column) {
124 $data[$column->column] = array(
125 'position' => $column->position,
126 'hidden' => $column->hidden
127 );
128 }
129 $GLOBALS['BE_USER']->uc['moduleData']['Workspaces']['columns'] = $data;
130 $GLOBALS['BE_USER']->writeUC();
131 }
132
133 public function loadColumnModel() {
134 if(is_array($GLOBALS['BE_USER']->uc['moduleData']['Workspaces']['columns'])) {
135 return $GLOBALS['BE_USER']->uc['moduleData']['Workspaces']['columns'];
136 } else {
137 return array();
138 }
139 }
140
141 /**
142 * Gets the dialog window to be displayed before a record can be sent to the next stage.
143 *
144 * @param integer $uid
145 * @param string $table
146 * @param integer $t3ver_oid
147 * @return array
148 */
149 public function sendToNextStageWindow($uid, $table, $t3ver_oid) {
150 $elementRecord = t3lib_BEfunc::getRecord($table, $uid);
151
152 if(is_array($elementRecord)) {
153 $stageId = $elementRecord['t3ver_stage'];
154
155 if ($this->getStageService()->isValid($stageId)) {
156 $nextStage = $this->getStageService()->getNextStage($stageId);
157 $result = $this->getSentToStageWindow($nextStage['uid']);
158 $result['affects'] = array(
159 'table' => $table,
160 'nextStage' => $nextStage['uid'],
161 't3ver_oid' => $t3ver_oid,
162 'uid' => $uid,
163 );
164 } else {
165 $result = $this->getErrorResponse('error.stageId.invalid', 1291111644);
166 }
167 } else {
168 $result = $this->getErrorResponse('error.sendToNextStage.noRecordFound', 1287264776);
169 }
170
171 return $result;
172 }
173
174 /**
175 * Gets the dialog window to be displayed before a record can be sent to the previous stage.
176 *
177 * @param integer $uid
178 * @param string $table
179 * @return array
180 */
181 public function sendToPrevStageWindow($uid, $table) {
182 $elementRecord = t3lib_BEfunc::getRecord($table, $uid);
183
184 if(is_array($elementRecord)) {
185 $stageId = $elementRecord['t3ver_stage'];
186
187 if ($this->getStageService()->isValid($stageId)) {
188 if ($stageId !== Tx_Workspaces_Service_Stages::STAGE_EDIT_ID) {
189 $prevStage = $this->getStageService()->getPrevStage($stageId);
190
191 $result = $this->getSentToStageWindow($prevStage['uid']);
192 $result['affects'] = array(
193 'table' => $table,
194 'uid' => $uid,
195 'nextStage' => $prevStage['uid'],
196 );
197 } else {
198 // element is already in edit stage, there is no prev stage - return an error message
199 $result = $this->getErrorResponse('error.sendToPrevStage.noPreviousStage', 1287264746);
200 }
201 } else {
202 $result = $this->getErrorResponse('error.stageId.invalid', 1291111644);
203 }
204 } else {
205 $result = $this->getErrorResponse('error.sendToNextStage.noRecordFound', 1287264765);
206 }
207
208 return $result;
209 }
210
211 /**
212 * Gets the dialog window to be displayed before a record can be sent to a specific stage.
213 *
214 * @param integer $nextStageId
215 * @return array
216 */
217 public function sendToSpecificStageWindow($nextStageId) {
218 $result = $this->getSentToStageWindow($nextStageId);
219 $result['affects'] = array(
220 'nextStage' => $nextStageId,
221 );
222
223 return $result;
224 }
225
226 /**
227 * Gets a merged variant of recipient defined by uid and custom ones.
228 *
229 * @param array list of recipients
230 * @param string given user string of additional recipients
231 * @return array
232 */
233 public function getRecipientList(array $uidOfRecipients, $additionalRecipients) {
234 $finalRecipients = array();
235
236 $recipients = array();
237 foreach ($uidOfRecipients as $userUid) {
238 $beUserRecord = t3lib_befunc::getRecord('be_users',intval($userUid));
239 if(is_array($beUserRecord) && $beUserRecord['email'] != '') {
240 $recipients[] = $beUserRecord['email'];
241 }
242 }
243
244 if ($additionalRecipients != '') {
245 $additionalRecipients = t3lib_div::trimExplode("\n", $additionalRecipients, TRUE);
246 } else {
247 $additionalRecipients = array();
248 }
249
250 $allRecipients = array_unique(
251 array_merge($recipients, $additionalRecipients)
252 );
253
254 foreach ($allRecipients as $recipient) {
255 if (t3lib_div::validEmail($recipient)) {
256 $finalRecipients[] = $recipient;
257 }
258 }
259
260 return $finalRecipients;
261 }
262
263 /**
264 * Gets an object with this structure:
265 *
266 * affects: object
267 * table
268 * t3ver_oid
269 * nextStage
270 * uid
271 * receipients: array with uids
272 * additional: string
273 * comments: string
274 *
275 * @param stdObject $parameters
276 * @return array
277 */
278 public function sendToNextStageExecute(stdClass $parameters) {
279 $cmdArray = array();
280 $recipients = array();
281
282 $setStageId = $parameters->affects->nextStage;
283 $comments = $parameters->comments;
284 $table = $parameters->affects->table;
285 $uid = $parameters->affects->uid;
286 $t3ver_oid = $parameters->affects->t3ver_oid;
287 $recipients = $this->getRecipientList($parameters->receipients, $parameters->additional);
288
289 if ($setStageId == Tx_Workspaces_Service_Stages::STAGE_PUBLISH_EXECUTE_ID) {
290 $cmdArray[$table][$t3ver_oid]['version']['action'] = 'swap';
291 $cmdArray[$table][$t3ver_oid]['version']['swapWith'] = $uid;
292 $cmdArray[$table][$t3ver_oid]['version']['comment'] = $comments;
293 $cmdArray[$table][$t3ver_oid]['version']['notificationAlternativeRecipients'] = $recipients;
294 } else {
295 $cmdArray[$table][$uid]['version']['action'] = 'setStage';
296 $cmdArray[$table][$uid]['version']['stageId'] = $setStageId;
297 $cmdArray[$table][$uid]['version']['comment'] = $comments;
298 $cmdArray[$table][$uid]['version']['notificationAlternativeRecipients'] = $recipients;
299 }
300
301 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
302 $tce->start(array(), $cmdArray);
303 $tce->process_cmdmap();
304
305 $result = array(
306 'success' => TRUE,
307 );
308
309 return $result;
310 }
311
312 /**
313 * Gets an object with this structure:
314 *
315 * affects: object
316 * table
317 * t3ver_oid
318 * nextStage
319 * receipients: array with uids
320 * additional: string
321 * comments: string
322 *
323 * @param stdObject $parameters
324 * @return array
325 */
326 public function sendToPrevStageExecute(stdClass $parameters) {
327 $cmdArray = array();
328 $recipients = array();
329
330 $setStageId = $parameters->affects->nextStage;
331 $comments = $parameters->comments;
332 $table = $parameters->affects->table;
333 $uid = $parameters->affects->uid;
334 $recipients = $this->getRecipientList($parameters->receipients, $parameters->additional);
335
336 $cmdArray[$table][$uid]['version']['action'] = 'setStage';
337 $cmdArray[$table][$uid]['version']['stageId'] = $setStageId;
338 $cmdArray[$table][$uid]['version']['comment'] = $comments;
339 $cmdArray[$table][$uid]['version']['notificationAlternativeRecipients'] = $recipients;
340
341 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
342 $tce->start(array(), $cmdArray);
343 $tce->process_cmdmap();
344
345 $result = array(
346 'success' => TRUE,
347 );
348
349 return $result;
350 }
351
352 /**
353 * Gets an object with this structure:
354 *
355 * affects: object
356 * elements: array
357 * 0: object
358 * table
359 * t3ver_oid
360 * uid
361 * 1: object
362 * table
363 * t3ver_oid
364 * uid
365 * nextStage
366 * receipients: array with uids
367 * additional: string
368 * comments: string
369 *
370 * @param stdObject $parameters
371 * @return array
372 */
373 public function sendToSpecificStageExecute(stdClass $parameters) {
374 $cmdArray = array();
375
376 $setStageId = $parameters->affects->nextStage;
377 $comments = $parameters->comments;
378 $elements = $parameters->affects->elements;
379 $recipients = $this->getRecipientList($parameters->receipients, $parameters->additional);
380
381 foreach($elements as $key=>$element) {
382 if ($setStageId == Tx_Workspaces_Service_Stages::STAGE_PUBLISH_EXECUTE_ID) {
383 $cmdArray[$element->table][$element->t3ver_oid]['version']['action'] = 'swap';
384 $cmdArray[$element->table][$element->t3ver_oid]['version']['swapWith'] = $element->uid;
385 $cmdArray[$element->table][$element->t3ver_oid]['version']['comment'] = $comments;
386 $cmdArray[$element->table][$element->t3ver_oid]['version']['notificationAlternativeRecipients'] = $recipients;
387 } else {
388 $cmdArray[$element->table][$element->uid]['version']['action'] = 'setStage';
389 $cmdArray[$element->table][$element->uid]['version']['stageId'] = $setStageId;
390 $cmdArray[$element->table][$element->uid]['version']['comment'] = $comments;
391 $cmdArray[$element->table][$element->uid]['version']['notificationAlternativeRecipients'] = $recipients;
392 }
393 }
394
395 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
396 $tce->start(array(), $cmdArray);
397 $tce->process_cmdmap();
398
399 $result = array(
400 'success' => TRUE,
401 );
402
403 return $result;
404 }
405
406 /**
407 * Gets the dialog window to be displayed before a record can be sent to a stage.
408 *
409 * @param $nextStageId
410 * @return array
411 */
412 protected function getSentToStageWindow($nextStageId) {
413 $stageTitle = $this->getStageService()->getStageTitle($nextStageId);
414 $result = array(
415 'title' => $GLOBALS['LANG']->sL('LLL:EXT:workspaces/Resources/Private/Language/locallang.xml:actionSendToStage'),
416 'items' => array(
417 array(
418 'xtype' => 'panel',
419 'bodyStyle' => 'margin-bottom: 7px; border: none;',
420 'html' => $GLOBALS['LANG']->sL('LLL:EXT:workspaces/Resources/Private/Language/locallang.xml:window.sendToNextStageWindow.itemsWillBeSentTo') . $stageTitle,
421 ),
422 array(
423 'fieldLabel' => $GLOBALS['LANG']->sL('LLL:EXT:workspaces/Resources/Private/Language/locallang.xml:window.sendToNextStageWindow.sendMailTo'),
424 'xtype' => 'checkboxgroup',
425 'itemCls' => 'x-check-group-alt',
426 'columns' => 1,
427 'style' => 'max-height: 200px',
428 'autoScroll' => true,
429 'items' => array(
430 $this->getReceipientsOfStage($nextStageId)
431 )
432 ),
433 array(
434 'fieldLabel' => $GLOBALS['LANG']->sL('LLL:EXT:workspaces/Resources/Private/Language/locallang.xml:window.sendToNextStageWindow.additionalRecipients'),
435 'name' => 'additional',
436 'xtype' => 'textarea',
437 'width' => 250,
438 ),
439 array(
440 'fieldLabel' => $GLOBALS['LANG']->sL('LLL:EXT:workspaces/Resources/Private/Language/locallang.xml:window.sendToNextStageWindow.comments'),
441 'name' => 'comments',
442 'xtype' => 'textarea',
443 'width' => 250,
444 'value' => $this->getDefaultCommentOfStage($nextStageId),
445 ),
446 )
447 );
448
449 return $result;
450 }
451
452 /**
453 * Gets all assigned recipients of a particular stage.
454 *
455 * @param integer $stage
456 * @return array
457 */
458 protected function getReceipientsOfStage($stage) {
459 $result = array();
460
461 $recipients = $this->getStageService()->getResponsibleBeUser($stage);
462
463 foreach ($recipients as $id => $user) {
464 if (t3lib_div::validEmail($user['email'])) {
465 $name = $user['realName'] ? $user['realName'] : $user['username'];
466 $result[] = array(
467 'boxLabel' => sprintf('%s (%s)', $name, $user['email']),
468 'name' => 'receipients-' . $id,
469 'checked' => TRUE,
470 );
471 }
472 }
473
474 return $result;
475 }
476
477 /**
478 * Gets the default comment of a particular stage.
479 *
480 * @param integer $stage
481 * @return string
482 */
483 protected function getDefaultCommentOfStage($stage) {
484 $result = $this->getStageService()->getPropertyOfCurrentWorkspaceStage($stage, 'default_mailcomment');
485
486 return $result;
487 }
488
489 /**
490 * Gets an instance of the Stage service.
491 *
492 * @return Tx_Workspaces_Service_Stages
493 */
494 protected function getStageService() {
495 if (!isset($this->stageService)) {
496 $this->stageService = t3lib_div::makeInstance('Tx_Workspaces_Service_Stages');
497 }
498 return $this->stageService;
499 }
500 }
501
502
503 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/workspaces/Classes/ExtDirect/ActionHandler.php'])) {
504 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/workspaces/Classes/ExtDirect/ActionHandler.php']);
505 }
506 ?>