[BUGFIX] Make class name consistent
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Service / Workspaces.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 Service
32 */
33 class Tx_Workspaces_Service_Workspaces implements t3lib_Singleton {
34 /**
35 * @var array
36 */
37 protected $pageCache = array();
38
39 const TABLE_WORKSPACE = 'sys_workspace';
40 const SELECT_ALL_WORKSPACES = -98;
41 const LIVE_WORKSPACE_ID = 0;
42
43 /**
44 * retrieves the available workspaces from the database and checks whether
45 * they're available to the current BE user
46 *
47 * @return array array of worspaces available to the current user
48 */
49 public function getAvailableWorkspaces() {
50 $availableWorkspaces = array();
51
52 // add default workspaces
53 if ($GLOBALS['BE_USER']->checkWorkspace(array('uid' => (string) self::LIVE_WORKSPACE_ID))) {
54 $availableWorkspaces[self::LIVE_WORKSPACE_ID] = self::getWorkspaceTitle(self::LIVE_WORKSPACE_ID);
55 }
56
57 // add custom workspaces (selecting all, filtering by BE_USER check):
58 $customWorkspaces = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid, title, adminusers, members', 'sys_workspace', 'pid = 0' . t3lib_BEfunc::deleteClause('sys_workspace'), '', 'title');
59 if (count($customWorkspaces)) {
60 foreach ($customWorkspaces as $workspace) {
61 if ($GLOBALS['BE_USER']->checkWorkspace($workspace)) {
62 $availableWorkspaces[$workspace['uid']] = htmlspecialchars($workspace['title']);
63 }
64 }
65 }
66
67 return $availableWorkspaces;
68 }
69
70 /**
71 * Gets the current workspace ID.
72 *
73 * @return integer The current workspace ID
74 */
75 public function getCurrentWorkspace() {
76 $workspaceId = $GLOBALS['BE_USER']->workspace;
77 if ($GLOBALS['BE_USER']->isAdmin()) {
78 $activeId = $GLOBALS['BE_USER']->getSessionData('tx_workspace_activeWorkspace');
79 $workspaceId = $activeId !== NULL ? $activeId : $workspaceId;
80 }
81 return $workspaceId;
82 }
83
84 /**
85 * Find the title for the requested workspace.
86 *
87 * @param integer $wsId
88 * @return string
89 */
90 public static function getWorkspaceTitle($wsId) {
91 $title = FALSE;
92 switch ($wsId) {
93 case self::LIVE_WORKSPACE_ID:
94 $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_misc.xml:shortcut_onlineWS');
95 break;
96 default:
97 $labelField = $GLOBALS['TCA']['sys_workspace']['ctrl']['label'];
98 $wsRecord = t3lib_beFunc::getRecord('sys_workspace', $wsId, 'uid,' . $labelField);
99 if (is_array($wsRecord)) {
100 $title = $wsRecord[$labelField];
101 }
102 }
103
104 if ($title === FALSE) {
105 throw new InvalidArgumentException('No such workspace defined');
106 }
107
108 return $title;
109 }
110
111
112 /**
113 * Building tcemain CMD-array for swapping all versions in a workspace.
114 *
115 * @param integer Real workspace ID, cannot be ONLINE (zero).
116 * @param boolean If set, then the currently online versions are swapped into the workspace in exchange for the offline versions. Otherwise the workspace is emptied.
117 * @param integer $pageId: ...
118 * @return array Command array for tcemain
119 */
120 public function getCmdArrayForPublishWS($wsid, $doSwap, $pageId = 0) {
121
122 $wsid = intval($wsid);
123 $cmd = array();
124
125 if ($wsid >= -1 && $wsid!==0) {
126
127 // Define stage to select:
128 $stage = -99;
129 if ($wsid > 0) {
130 $workspaceRec = t3lib_BEfunc::getRecord('sys_workspace', $wsid);
131 if ($workspaceRec['publish_access'] & 1) {
132 $stage = Tx_Workspaces_Service_Stages::STAGE_PUBLISH_ID;
133 }
134 }
135
136 // Select all versions to swap:
137 $versions = $this->selectVersionsInWorkspace($wsid, 0, $stage, ($pageId ? $pageId : -1), 0, 'tables_modify');
138
139 // Traverse the selection to build CMD array:
140 foreach ($versions as $table => $records) {
141 foreach ($records as $rec) {
142 // Build the cmd Array:
143 $cmd[$table][$rec['t3ver_oid']]['version'] = array('action' => 'swap', 'swapWith' => $rec['uid'], 'swapIntoWS' => $doSwap ? 1 : 0);
144 }
145 }
146 }
147 return $cmd;
148 }
149
150
151 /**
152 * Building tcemain CMD-array for releasing all versions in a workspace.
153 *
154 * @param integer Real workspace ID, cannot be ONLINE (zero).
155 * @param boolean Run Flush (TRUE) or ClearWSID (FALSE) command
156 * @param integer $pageId: ...
157 * @return array Command array for tcemain
158 */
159 public function getCmdArrayForFlushWS($wsid, $flush = TRUE, $pageId = 0) {
160
161 $wsid = intval($wsid);
162 $cmd = array();
163
164 if ($wsid >= -1 && $wsid!==0) {
165 // Define stage to select:
166 $stage = -99;
167
168 // Select all versions to swap:
169 $versions = $this->selectVersionsInWorkspace($wsid, 0, $stage, ($pageId ? $pageId : -1), 0, 'tables_modify');
170
171 // Traverse the selection to build CMD array:
172 foreach ($versions as $table => $records) {
173 foreach ($records as $rec) {
174 // Build the cmd Array:
175 $cmd[$table][$rec['uid']]['version'] = array('action' => ($flush ? 'flush' : 'clearWSID'));
176 }
177 }
178 }
179 return $cmd;
180 }
181
182
183 /**
184 * Select all records from workspace pending for publishing
185 * Used from backend to display workspace overview
186 * User for auto-publishing for selecting versions for publication
187 *
188 * @param integer Workspace ID. If -99, will select ALL versions from ANY workspace. If -98 will select all but ONLINE. >=-1 will select from the actual workspace
189 * @param integer Lifecycle filter: 1 = select all drafts (never-published), 2 = select all published one or more times (archive/multiple), anything else selects all.
190 * @param integer Stage filter: -99 means no filtering, otherwise it will be used to select only elements with that stage. For publishing, that would be "10"
191 * @param integer Page id: Live page for which to find versions in workspace!
192 * @param integer Recursion Level - select versions recursive - parameter is only relevant if $pageId != -1
193 * @param string How to collect records for "listing" or "modify" these tables. Support the permissions of each type of record (@see t3lib_userAuthGroup::check).
194 * @return array Array of all records uids etc. First key is table name, second key incremental integer. Records are associative arrays with uid, t3ver_oid and t3ver_swapmode fields. The pid of the online record is found as "livepid" the pid of the offline record is found in "wspid"
195 */
196 public function selectVersionsInWorkspace($wsid, $filter = 0, $stage = -99, $pageId = -1, $recursionLevel = 0, $selectionType = 'tables_select') {
197
198 $wsid = intval($wsid);
199 $filter = intval($filter);
200 $output = array();
201
202 // Contains either nothing or a list with live-uids
203 if ($pageId != -1 && $recursionLevel > 0) {
204 $pageList = $this->getTreeUids($pageId, $wsid, $recursionLevel);
205 } elseif ($pageId != -1) {
206 $pageList = $pageId;
207 } else {
208 $pageList = '';
209 }
210
211 // Traversing all tables supporting versioning:
212 foreach ($GLOBALS['TCA'] as $table => $cfg) {
213
214 // we do not collect records from tables without permissions on them.
215 if (! $GLOBALS['BE_USER']->check($selectionType, $table)) {
216 continue;
217 }
218
219 if ($GLOBALS['TCA'][$table]['ctrl']['versioningWS']) {
220
221 $recs = $this->selectAllVersionsFromPages($table, $pageList, $wsid, $filter, $stage);
222 if (intval($GLOBALS['TCA'][$table]['ctrl']['versioningWS']) === 2) {
223 $moveRecs = $this->getMoveToPlaceHolderFromPages($table, $pageList, $wsid, $filter, $stage);
224 $recs = array_merge($recs, $moveRecs);
225 }
226 $recs = $this->filterPermittedElements($recs, $table);
227 if (count($recs)) {
228 $output[$table] = $recs;
229 }
230 }
231 }
232 return $output;
233 }
234
235 /**
236 * Find all versionized elements except moved records.
237 *
238 * @param string $table
239 * @param string $pageList
240 * @param integer $wsid
241 * @param integer $filter
242 * @param integer $stage
243 * @return array
244 */
245 protected function selectAllVersionsFromPages($table, $pageList, $wsid, $filter, $stage) {
246
247 $fields = 'A.uid, A.t3ver_oid, A.t3ver_stage, ' . ($table==='pages' ? ' A.t3ver_swapmode,' : '') . 'B.pid AS wspid, B.pid AS livepid';
248 $from = $table . ' A,' . $table . ' B';
249
250 // Table A is the offline version and pid=-1 defines offline
251 $where = 'A.pid=-1 AND A.t3ver_state!=4';
252 if ($pageList) {
253 $pidField = ($table==='pages' ? 'uid' : 'pid');
254 $pidConstraint = strstr($pageList, ',') ? ' IN (' . $pageList . ')' : '=' . $pageList;
255 $where .= ' AND B.' . $pidField . $pidConstraint;
256 }
257
258 /**
259 * For "real" workspace numbers, select by that.
260 * If = -98, select all that are NOT online (zero).
261 * Anything else below -1 will not select on the wsid and therefore select all!
262 */
263 if ($wsid > self::SELECT_ALL_WORKSPACES) {
264 $where .= ' AND A.t3ver_wsid=' . $wsid;
265 } elseif ($wsid === self::SELECT_ALL_WORKSPACES) {
266 $where .= ' AND A.t3ver_wsid!=0';
267 }
268
269 /**
270 * lifecycle filter:
271 * 1 = select all drafts (never-published),
272 * 2 = select all published one or more times (archive/multiple)
273 */
274 if ($filter===1 || $filter===2) {
275 $where .= ' AND A.t3ver_count ' . ($filter === 1 ? '= 0' : '> 0');
276 }
277
278 if ($stage != -99) {
279 $where .= ' AND A.t3ver_stage=' . intval($stage);
280 }
281
282 // Table B (online) must have PID >= 0 to signify being online.
283 $where .= ' AND B.pid>=0';
284 // ... and finally the join between the two tables.
285 $where .= ' AND A.t3ver_oid=B.uid';
286 $where .= t3lib_BEfunc::deleteClause($table, 'A');
287 $where .= t3lib_BEfunc::deleteClause($table, 'B');
288
289 /**
290 * Select all records from this table in the database from the workspace
291 * This joins the online version with the offline version as tables A and B
292 * Order by UID, mostly to have a sorting in the backend overview module which doesn't "jump around" when swapping.
293 */
294 $res = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($fields, $from, $where, '', 'B.uid');
295 return is_array($res) ? $res : array();
296 }
297
298 /**
299 * Find all moved records at their new position.
300 *
301 * @param string $table
302 * @param string $pageList
303 * @param integer $wsid
304 * @return array
305 */
306 protected function getMoveToPlaceHolderFromPages($table, $pageList, $wsid, $filter, $stage) {
307
308 /**
309 * Aliases:
310 * A - moveTo placeholder
311 * B - online record
312 * C - moveFrom placeholder
313 */
314 $fields = 'A.pid AS wspid, B.uid AS t3ver_oid, C.uid AS uid, B.pid AS livepid';
315 $from = $table . ' A, ' . $table . ' B,' . $table . ' C';
316 $where = 'A.t3ver_state=3 AND B.pid>0 AND B.t3ver_state=0 AND B.t3ver_wsid=0 AND C.pid=-1 AND C.t3ver_state=4';
317
318 if ($wsid > self::SELECT_ALL_WORKSPACES) {
319 $where .= ' AND A.t3ver_wsid=' . $wsid . ' AND C.t3ver_wsid=' . $wsid;
320 } elseif ($wsid === self::SELECT_ALL_WORKSPACES) {
321 $where .= ' AND A.t3ver_wsid!=0 AND C.t3ver_wsid!=0 ';
322 }
323
324 /**
325 * lifecycle filter:
326 * 1 = select all drafts (never-published),
327 * 2 = select all published one or more times (archive/multiple)
328 */
329 if ($filter===1 || $filter===2) {
330 $where .= ' AND C.t3ver_count ' . ($filter === 1 ? '= 0' : '> 0');
331 }
332
333 if ($stage != -99) {
334 $where .= ' AND C.t3ver_stage=' . intval($stage);
335 }
336
337 if ($pageList) {
338 $pidField = ($table==='pages' ? 'B.uid' : 'A.pid');
339 $pidConstraint = strstr($pageList, ',') ? ' IN (' . $pageList . ')' : '=' . $pageList;
340 $where .= ' AND ' . $pidField . $pidConstraint;
341 }
342
343 $where .= ' AND A.t3ver_move_id = B.uid AND B.uid = C.t3ver_oid';
344 $where .= t3lib_BEfunc::deleteClause($table, 'A');
345 $where .= t3lib_BEfunc::deleteClause($table, 'B');
346 $where .= t3lib_BEfunc::deleteClause($table, 'C');
347 $res = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($fields, $from, $where, '', 'A.uid');
348
349 return is_array($res) ? $res : array();
350 }
351
352
353 /**
354 * Find all page uids recursive starting from a specific page
355 *
356 * @param integer $pageId
357 * @param integer $wsid
358 * @param integer $recursionLevel
359 * @return string Comma sep. uid list
360 */
361 protected function getTreeUids($pageId, $wsid, $recursionLevel) {
362 /**
363 * Reusing existing functionality with the drawback that
364 * mount points are not covered yet
365 **/
366 $perms_clause = $GLOBALS['BE_USER']->getPagePermsClause(1);
367 $searchObj = t3lib_div::makeInstance('t3lib_fullsearch');
368 $pageList = FALSE;
369 if ($pageId > 0) {
370 $pageList = $searchObj->getTreeList($pageId, $recursionLevel, 0, $perms_clause);
371 } else {
372 $mountPoints = $GLOBALS['BE_USER']->uc['pageTree_temporaryMountPoint'];
373 if (!is_array($mountPoints) || empty($mountPoints)) {
374 $mountPoints = array_map('intval', $GLOBALS['BE_USER']->returnWebmounts());
375 $mountPoints = array_unique($mountPoints);
376 }
377 $newList = array();
378 foreach($mountPoints as $mountPoint) {
379 $newList[] = $searchObj->getTreeList($mountPoint, $recursionLevel, 0, $perms_clause);
380 }
381 $pageList = implode(',', $newList);
382 }
383
384 unset($searchObj);
385 if (intval($GLOBALS['TCA']['pages']['ctrl']['versioningWS']) === 2 && $pageList) {
386 // Remove the "subbranch" if a page was moved away
387 $movedAwayPages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
388 'uid, pid, t3ver_move_id',
389 'pages',
390 't3ver_move_id IN (' . $pageList . ') AND t3ver_wsid=' . intval($wsid) . t3lib_BEfunc::deleteClause('pages'),
391 '',
392 'uid',
393 '',
394 't3ver_move_id'
395 );
396 $pageIds = t3lib_div::intExplode(',', $pageList, TRUE);
397
398 // move all pages away
399 $newList = array_diff($pageIds, array_keys($movedAwayPages));
400
401 // keep current page in the list
402 $newList[] = $pageId;
403 // move back in if still connected to the "remaining" pages
404 do {
405 $changed = FALSE;
406 foreach ($movedAwayPages as $uid => $rec) {
407 if (in_array($rec['pid'], $newList) && !in_array($uid, $newList)) {
408 $newList[] = $uid;
409 $changed = TRUE;
410 }
411 }
412 } while ($changed);
413 $pageList = implode(',', $newList);
414
415 // In case moving pages is enabled we need to replace all move-to pointer with their origin
416 $pages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
417 'uid, t3ver_move_id',
418 'pages',
419 'uid IN (' . $pageList . ')' . t3lib_BEfunc::deleteClause('pages'),
420 '',
421 'uid',
422 '',
423 'uid'
424 );
425
426 $newList = array();
427 $pageIds = t3lib_div::intExplode(',', $pageList, TRUE);
428 if (!in_array($pageId, $pageIds)) {
429 $pageIds[] = $pageId;
430 }
431 foreach ($pageIds as $pageId) {
432 if (intval($pages[$pageId]['t3ver_move_id']) > 0) {
433 $newList[] = intval($pages[$pageId]['t3ver_move_id']);
434 } else {
435 $newList[] = $pageId;
436 }
437 }
438 $pageList = implode(',', $newList);
439 }
440 return $pageList;
441 }
442
443 /**
444 * Remove all records which are not permitted for the user
445 *
446 * @param array $recs
447 * @param string $table
448 * @return array
449 */
450 protected function filterPermittedElements($recs, $table) {
451 $checkField = ($table == 'pages') ? 'uid' : 'wspid';
452 $permittedElements = array();
453 if (is_array($recs)) {
454 foreach ($recs as $rec) {
455 $page = t3lib_beFunc::getRecord('pages', $rec[$checkField], 'uid,pid,perms_userid,perms_user,perms_groupid,perms_group,perms_everybody');
456 if ($GLOBALS['BE_USER']->doesUserHaveAccess($page, 1) && $this->isLanguageAccessibleForCurrentUser($table, $rec)) {
457 $permittedElements[] = $rec;
458 }
459 }
460 }
461 return $permittedElements;
462 }
463
464 /**
465 * Check current be users language access on given record.
466 *
467 * @param string $table Name of the table
468 * @param array $record Record row to be checked
469 * @return boolean
470 */
471 protected function isLanguageAccessibleForCurrentUser($table, array $record) {
472 $languageUid = 0;
473
474 if (t3lib_BEfunc::isTableLocalizable($table)) {
475 $languageUid = $record[$GLOBALS['TCA'][$table]['ctrl']['languageField']];
476 }
477
478 return $GLOBALS['BE_USER']->checkLanguageAccess($languageUid);
479 }
480
481 /**
482 * Trivial check to see if the user already migrated his workspaces
483 * to the new style (either manually or with the migrator scripts)
484 *
485 * @return bool
486 */
487 public static function isOldStyleWorkspaceUsed() {
488 $oldStyleWorkspaceIsUsed = FALSE;
489 $cacheKey = 'workspace-oldstyleworkspace-notused';
490 $cacheResult = $GLOBALS['BE_USER']->getSessionData($cacheKey);
491 if (!$cacheResult) {
492 $where = 'adminusers != "" AND adminusers NOT LIKE "%be_users%" AND adminusers NOT LIKE "%be_groups%" AND deleted=0';
493 $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('uid', 'sys_workspace', $where);
494 $oldStyleWorkspaceIsUsed = $count > 0;
495 $GLOBALS['BE_USER']->setAndSaveSessionData($cacheKey, !$oldStyleWorkspaceIsUsed);
496 } else {
497 $oldStyleWorkspaceIsUsed = !$cacheResult;
498 }
499 return $oldStyleWorkspaceIsUsed;
500 }
501
502 /**
503 * Determine whether a specific page is new and not yet available in the LIVE workspace
504 *
505 * @static
506 * @param $id Primary key of the page to check
507 * @param $language Language for which to check the page
508 * @return bool
509 */
510 public static function isNewPage($id, $language = 0) {
511 $isNewPage = FALSE;
512 // If the language is not default, check state of overlay
513 if ($language > 0) {
514 $whereClause = 'pid = ' . $id;
515 $whereClause .= ' AND ' .$GLOBALS['TCA']['pages_language_overlay']['ctrl']['languageField'] . ' = ' . $language;
516 $whereClause .= ' AND t3ver_wsid = ' . $GLOBALS['BE_USER']->workspace;
517 $whereClause .= t3lib_BEfunc::deleteClause('pages_language_overlay');
518 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('t3ver_state', 'pages_language_overlay', $whereClause);
519 if (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))) {
520 $isNewPage = (int) $row['t3ver_state'] === 1;
521 }
522
523 // Otherwise check state of page itself
524 } else {
525 $rec = t3lib_BEfunc::getRecord('pages', $id, 't3ver_state');
526 if (is_array($rec)) {
527 $isNewPage = (int) $rec['t3ver_state'] === 1;
528 }
529 }
530 return $isNewPage;
531 }
532
533 /**
534 * Generates a view link for a page.
535 *
536 * @static
537 * @param $table
538 * @param $uid
539 * @param $record
540 * @return string
541 */
542 public static function viewSingleRecord($table, $uid, $record=NULL) {
543 $viewUrl = '';
544 if ($table == 'pages') {
545 $viewUrl = t3lib_BEfunc::viewOnClick(t3lib_BEfunc::getLiveVersionIdOfRecord('pages', $uid));
546 } elseif ($table == 'pages_language_overlay' || $table == 'tt_content') {
547 $elementRecord = is_array($record) ? $record : t3lib_BEfunc::getLiveVersionOfRecord($table, $uid);
548 $viewUrl = t3lib_BEfunc::viewOnClick($elementRecord['pid']);
549 } else {
550 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['workspaces']['viewSingleRecord'])) {
551 $_params = array('table' => $table, 'uid' => $uid, 'record' => $record);
552 $_funcRef = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['workspaces']['viewSingleRecord'];
553 $viewUrl = t3lib_div::callUserFunction($_funcRef, $_params, NULL);
554 }
555 }
556 return $viewUrl;
557 }
558
559 /**
560 * Determine whether this page for the current
561 *
562 * @param $pageUid
563 * @param $workspaceUid
564 * @return void
565 */
566 public function canCreatePreviewLink($pageUid, $workspaceUid) {
567 $result = TRUE;
568 if ($pageUid > 0 && $workspaceUid > 0) {
569 $pageRecord = t3lib_BEfunc::getRecord('pages', $pageUid);
570 t3lib_BEfunc::workspaceOL('pages', $pageRecord, $workspaceUid);
571 if (!t3lib_div::inList($GLOBALS['TYPO3_CONF_VARS']['FE']['content_doktypes'], $pageRecord['doktype'])) {
572 $result = FALSE;
573 }
574 } else {
575 $result = FALSE;
576 }
577 return $result;
578 }
579
580 /**
581 * Generates a workspace preview link.
582 *
583 * @param integer $uid The ID of the record to be linked
584 * @return string the full domain including the protocol http:// or https://, but without the trailing '/'
585 */
586 public function generateWorkspacePreviewLink($uid) {
587 $previewObject = t3lib_div::makeInstance('Tx_Version_Preview');
588 $timeToLiveHours = $previewObject->getPreviewLinkLifetime();
589 $previewKeyword = $previewObject->compilePreviewKeyword('', $GLOBALS['BE_USER']->user['uid'], ($timeToLiveHours*3600), $this->getCurrentWorkspace());
590
591 $linkParams = array(
592 'ADMCMD_prev' => $previewKeyword,
593 'id' => $uid
594 );
595 return t3lib_BEfunc::getViewDomain($uid) . '/index.php?' . t3lib_div::implodeArrayForUrl('', $linkParams);
596 }
597
598 /**
599 * Generates a workspace splitted preview link.
600 *
601 * @param integer $uid The ID of the record to be linked
602 * @param boolean $addDomain Parameter to decide if domain should be added to the generated link, FALSE per default
603 * @return string the preview link without the trailing '/'
604 */
605 public function generateWorkspaceSplittedPreviewLink($uid, $addDomain = FALSE) {
606 // In case a $pageUid is submitted we need to make sure it points to a live-page
607 if ($uid > 0) {
608 $uid = $this->getLivePageUid($uid);
609 }
610
611 $objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
612 /** @var $uriBuilder Tx_Extbase_MVC_Web_Routing_UriBuilder */
613 $uriBuilder = $objectManager->create('Tx_Extbase_MVC_Web_Routing_UriBuilder');
614 /**
615 * This seems to be very harsh to set this directly to "/typo3 but the viewOnClick also
616 * has /index.php as fixed value here and dealing with the backPath is very error-prone
617 *
618 * @todo make sure this would work in local extension installation too
619 */
620 $backPath = '/' . TYPO3_mainDir;
621 $redirect = $backPath . 'index.php?redirect_url=';
622 // @todo why do we need these additional params? the URIBuilder should add the controller, but he doesn't :(
623 $additionalParams = '&tx_workspaces_web_workspacesworkspaces%5Bcontroller%5D=Preview&M=web_WorkspacesWorkspaces&id=';
624 $viewScript = $backPath . $uriBuilder->setArguments(array('tx_workspaces_web_workspacesworkspaces' => array('previewWS' => $GLOBALS['BE_USER']->workspace)))
625 ->uriFor('index', array(), 'Tx_Workspaces_Controller_PreviewController', 'workspaces', 'web_workspacesworkspaces') . $additionalParams;
626
627 if ($addDomain === TRUE) {
628 return t3lib_BEfunc::getViewDomain($uid) . $redirect . urlencode($viewScript) . $uid;
629 } else {
630 return $viewScript;
631 }
632 }
633
634 /**
635 * Find the Live-Uid for a given page,
636 * the results are cached at run-time to avoid too many database-queries
637 *
638 * @throws InvalidArgumentException
639 * @param integer $uid
640 * @return integer
641 */
642 public function getLivePageUid($uid) {
643 if (!isset($this->pageCache[$uid])) {
644 $pageRecord = t3lib_beFunc::getRecord('pages', $uid);
645 if (is_array($pageRecord)) {
646 $this->pageCache[$uid] = ($pageRecord['t3ver_oid'] ? $pageRecord['t3ver_oid'] : $uid);
647 } else {
648 throw new InvalidArgumentException('uid is supposed to point to an existing page - given value was:' . $uid, 1290628113);
649 }
650 }
651
652 return $this->pageCache[$uid];
653 }
654 }
655
656
657 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/workspaces/Classes/Service/Workspaces.php'])) {
658 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/workspaces/Classes/Service/Workspaces.php']);
659 }
660 ?>