[BUGFIX] Versioning: Fetch language-field if table localizable
[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 if (t3lib_BEfunc::isTableLocalizable($table)) {
249 $fields .= ', A.' . $GLOBALS['TCA'][$table]['ctrl']['languageField'];
250 }
251 $from = $table . ' A,' . $table . ' B';
252
253 // Table A is the offline version and pid=-1 defines offline
254 $where = 'A.pid=-1 AND A.t3ver_state!=4';
255 if ($pageList) {
256 $pidField = ($table==='pages' ? 'uid' : 'pid');
257 $pidConstraint = strstr($pageList, ',') ? ' IN (' . $pageList . ')' : '=' . $pageList;
258 $where .= ' AND B.' . $pidField . $pidConstraint;
259 }
260
261 /**
262 * For "real" workspace numbers, select by that.
263 * If = -98, select all that are NOT online (zero).
264 * Anything else below -1 will not select on the wsid and therefore select all!
265 */
266 if ($wsid > self::SELECT_ALL_WORKSPACES) {
267 $where .= ' AND A.t3ver_wsid=' . $wsid;
268 } elseif ($wsid === self::SELECT_ALL_WORKSPACES) {
269 $where .= ' AND A.t3ver_wsid!=0';
270 }
271
272 /**
273 * lifecycle filter:
274 * 1 = select all drafts (never-published),
275 * 2 = select all published one or more times (archive/multiple)
276 */
277 if ($filter===1 || $filter===2) {
278 $where .= ' AND A.t3ver_count ' . ($filter === 1 ? '= 0' : '> 0');
279 }
280
281 if ($stage != -99) {
282 $where .= ' AND A.t3ver_stage=' . intval($stage);
283 }
284
285 // Table B (online) must have PID >= 0 to signify being online.
286 $where .= ' AND B.pid>=0';
287 // ... and finally the join between the two tables.
288 $where .= ' AND A.t3ver_oid=B.uid';
289 $where .= t3lib_BEfunc::deleteClause($table, 'A');
290 $where .= t3lib_BEfunc::deleteClause($table, 'B');
291
292 /**
293 * Select all records from this table in the database from the workspace
294 * This joins the online version with the offline version as tables A and B
295 * Order by UID, mostly to have a sorting in the backend overview module which doesn't "jump around" when swapping.
296 */
297 $res = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($fields, $from, $where, '', 'B.uid');
298 return is_array($res) ? $res : array();
299 }
300
301 /**
302 * Find all moved records at their new position.
303 *
304 * @param string $table
305 * @param string $pageList
306 * @param integer $wsid
307 * @return array
308 */
309 protected function getMoveToPlaceHolderFromPages($table, $pageList, $wsid, $filter, $stage) {
310
311 /**
312 * Aliases:
313 * A - moveTo placeholder
314 * B - online record
315 * C - moveFrom placeholder
316 */
317 $fields = 'A.pid AS wspid, B.uid AS t3ver_oid, C.uid AS uid, B.pid AS livepid';
318 $from = $table . ' A, ' . $table . ' B,' . $table . ' C';
319 $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';
320
321 if ($wsid > self::SELECT_ALL_WORKSPACES) {
322 $where .= ' AND A.t3ver_wsid=' . $wsid . ' AND C.t3ver_wsid=' . $wsid;
323 } elseif ($wsid === self::SELECT_ALL_WORKSPACES) {
324 $where .= ' AND A.t3ver_wsid!=0 AND C.t3ver_wsid!=0 ';
325 }
326
327 /**
328 * lifecycle filter:
329 * 1 = select all drafts (never-published),
330 * 2 = select all published one or more times (archive/multiple)
331 */
332 if ($filter===1 || $filter===2) {
333 $where .= ' AND C.t3ver_count ' . ($filter === 1 ? '= 0' : '> 0');
334 }
335
336 if ($stage != -99) {
337 $where .= ' AND C.t3ver_stage=' . intval($stage);
338 }
339
340 if ($pageList) {
341 $pidField = ($table==='pages' ? 'B.uid' : 'A.pid');
342 $pidConstraint = strstr($pageList, ',') ? ' IN (' . $pageList . ')' : '=' . $pageList;
343 $where .= ' AND ' . $pidField . $pidConstraint;
344 }
345
346 $where .= ' AND A.t3ver_move_id = B.uid AND B.uid = C.t3ver_oid';
347 $where .= t3lib_BEfunc::deleteClause($table, 'A');
348 $where .= t3lib_BEfunc::deleteClause($table, 'B');
349 $where .= t3lib_BEfunc::deleteClause($table, 'C');
350 $res = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($fields, $from, $where, '', 'A.uid');
351
352 return is_array($res) ? $res : array();
353 }
354
355
356 /**
357 * Find all page uids recursive starting from a specific page
358 *
359 * @param integer $pageId
360 * @param integer $wsid
361 * @param integer $recursionLevel
362 * @return string Comma sep. uid list
363 */
364 protected function getTreeUids($pageId, $wsid, $recursionLevel) {
365 /**
366 * Reusing existing functionality with the drawback that
367 * mount points are not covered yet
368 **/
369 $perms_clause = $GLOBALS['BE_USER']->getPagePermsClause(1);
370 $searchObj = t3lib_div::makeInstance('t3lib_fullsearch');
371 $pageList = FALSE;
372 if ($pageId > 0) {
373 $pageList = $searchObj->getTreeList($pageId, $recursionLevel, 0, $perms_clause);
374 } else {
375 $mountPoints = $GLOBALS['BE_USER']->uc['pageTree_temporaryMountPoint'];
376 if (!is_array($mountPoints) || empty($mountPoints)) {
377 $mountPoints = array_map('intval', $GLOBALS['BE_USER']->returnWebmounts());
378 $mountPoints = array_unique($mountPoints);
379 }
380 $newList = array();
381 foreach($mountPoints as $mountPoint) {
382 $newList[] = $searchObj->getTreeList($mountPoint, $recursionLevel, 0, $perms_clause);
383 }
384 $pageList = implode(',', $newList);
385 }
386
387 unset($searchObj);
388 if (intval($GLOBALS['TCA']['pages']['ctrl']['versioningWS']) === 2 && $pageList) {
389 // Remove the "subbranch" if a page was moved away
390 $movedAwayPages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
391 'uid, pid, t3ver_move_id',
392 'pages',
393 't3ver_move_id IN (' . $pageList . ') AND t3ver_wsid=' . intval($wsid) . t3lib_BEfunc::deleteClause('pages'),
394 '',
395 'uid',
396 '',
397 't3ver_move_id'
398 );
399 $pageIds = t3lib_div::intExplode(',', $pageList, TRUE);
400
401 // move all pages away
402 $newList = array_diff($pageIds, array_keys($movedAwayPages));
403
404 // keep current page in the list
405 $newList[] = $pageId;
406 // move back in if still connected to the "remaining" pages
407 do {
408 $changed = FALSE;
409 foreach ($movedAwayPages as $uid => $rec) {
410 if (in_array($rec['pid'], $newList) && !in_array($uid, $newList)) {
411 $newList[] = $uid;
412 $changed = TRUE;
413 }
414 }
415 } while ($changed);
416 $pageList = implode(',', $newList);
417
418 // In case moving pages is enabled we need to replace all move-to pointer with their origin
419 $pages = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
420 'uid, t3ver_move_id',
421 'pages',
422 'uid IN (' . $pageList . ')' . t3lib_BEfunc::deleteClause('pages'),
423 '',
424 'uid',
425 '',
426 'uid'
427 );
428
429 $newList = array();
430 $pageIds = t3lib_div::intExplode(',', $pageList, TRUE);
431 if (!in_array($pageId, $pageIds)) {
432 $pageIds[] = $pageId;
433 }
434 foreach ($pageIds as $pageId) {
435 if (intval($pages[$pageId]['t3ver_move_id']) > 0) {
436 $newList[] = intval($pages[$pageId]['t3ver_move_id']);
437 } else {
438 $newList[] = $pageId;
439 }
440 }
441 $pageList = implode(',', $newList);
442 }
443 return $pageList;
444 }
445
446 /**
447 * Remove all records which are not permitted for the user
448 *
449 * @param array $recs
450 * @param string $table
451 * @return array
452 */
453 protected function filterPermittedElements($recs, $table) {
454 $checkField = ($table == 'pages') ? 'uid' : 'wspid';
455 $permittedElements = array();
456 if (is_array($recs)) {
457 foreach ($recs as $rec) {
458 $page = t3lib_beFunc::getRecord('pages', $rec[$checkField], 'uid,pid,perms_userid,perms_user,perms_groupid,perms_group,perms_everybody');
459 if ($GLOBALS['BE_USER']->doesUserHaveAccess($page, 1) && $this->isLanguageAccessibleForCurrentUser($table, $rec)) {
460 $permittedElements[] = $rec;
461 }
462 }
463 }
464 return $permittedElements;
465 }
466
467 /**
468 * Check current be users language access on given record.
469 *
470 * @param string $table Name of the table
471 * @param array $record Record row to be checked
472 * @return boolean
473 */
474 protected function isLanguageAccessibleForCurrentUser($table, array $record) {
475 $languageUid = 0;
476
477 if (t3lib_BEfunc::isTableLocalizable($table)) {
478 $languageUid = $record[$GLOBALS['TCA'][$table]['ctrl']['languageField']];
479 } else {
480 return TRUE;
481 }
482
483 return $GLOBALS['BE_USER']->checkLanguageAccess($languageUid);
484 }
485
486 /**
487 * Trivial check to see if the user already migrated his workspaces
488 * to the new style (either manually or with the migrator scripts)
489 *
490 * @return bool
491 */
492 public static function isOldStyleWorkspaceUsed() {
493 $oldStyleWorkspaceIsUsed = FALSE;
494 $cacheKey = 'workspace-oldstyleworkspace-notused';
495 $cacheResult = $GLOBALS['BE_USER']->getSessionData($cacheKey);
496 if (!$cacheResult) {
497 $where = 'adminusers != "" AND adminusers NOT LIKE "%be_users%" AND adminusers NOT LIKE "%be_groups%" AND deleted=0';
498 $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('uid', 'sys_workspace', $where);
499 $oldStyleWorkspaceIsUsed = $count > 0;
500 $GLOBALS['BE_USER']->setAndSaveSessionData($cacheKey, !$oldStyleWorkspaceIsUsed);
501 } else {
502 $oldStyleWorkspaceIsUsed = !$cacheResult;
503 }
504 return $oldStyleWorkspaceIsUsed;
505 }
506
507 /**
508 * Determine whether a specific page is new and not yet available in the LIVE workspace
509 *
510 * @static
511 * @param $id Primary key of the page to check
512 * @param $language Language for which to check the page
513 * @return bool
514 */
515 public static function isNewPage($id, $language = 0) {
516 $isNewPage = FALSE;
517 // If the language is not default, check state of overlay
518 if ($language > 0) {
519 $whereClause = 'pid = ' . $id;
520 $whereClause .= ' AND ' .$GLOBALS['TCA']['pages_language_overlay']['ctrl']['languageField'] . ' = ' . $language;
521 $whereClause .= ' AND t3ver_wsid = ' . $GLOBALS['BE_USER']->workspace;
522 $whereClause .= t3lib_BEfunc::deleteClause('pages_language_overlay');
523 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('t3ver_state', 'pages_language_overlay', $whereClause);
524 if (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))) {
525 $isNewPage = (int) $row['t3ver_state'] === 1;
526 }
527
528 // Otherwise check state of page itself
529 } else {
530 $rec = t3lib_BEfunc::getRecord('pages', $id, 't3ver_state');
531 if (is_array($rec)) {
532 $isNewPage = (int) $rec['t3ver_state'] === 1;
533 }
534 }
535 return $isNewPage;
536 }
537
538 /**
539 * Generates a view link for a page.
540 *
541 * @static
542 * @param $table
543 * @param $uid
544 * @param $record
545 * @return string
546 */
547 public static function viewSingleRecord($table, $uid, $record=NULL) {
548 $viewUrl = '';
549 if ($table == 'pages') {
550 $viewUrl = t3lib_BEfunc::viewOnClick(t3lib_BEfunc::getLiveVersionIdOfRecord('pages', $uid));
551 } elseif ($table == 'pages_language_overlay' || $table == 'tt_content') {
552 $elementRecord = is_array($record) ? $record : t3lib_BEfunc::getLiveVersionOfRecord($table, $uid);
553 $viewUrl = t3lib_BEfunc::viewOnClick($elementRecord['pid']);
554 } else {
555 if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['workspaces']['viewSingleRecord'])) {
556 $_params = array('table' => $table, 'uid' => $uid, 'record' => $record);
557 $_funcRef = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['workspaces']['viewSingleRecord'];
558 $null = NULL;
559 $viewUrl = t3lib_div::callUserFunction($_funcRef, $_params, $null);
560 }
561 }
562 return $viewUrl;
563 }
564
565 /**
566 * Determine whether this page for the current
567 *
568 * @param $pageUid
569 * @param $workspaceUid
570 * @return void
571 */
572 public function canCreatePreviewLink($pageUid, $workspaceUid) {
573 $result = TRUE;
574 if ($pageUid > 0 && $workspaceUid > 0) {
575 $pageRecord = t3lib_BEfunc::getRecord('pages', $pageUid);
576 t3lib_BEfunc::workspaceOL('pages', $pageRecord, $workspaceUid);
577 if (!t3lib_div::inList($GLOBALS['TYPO3_CONF_VARS']['FE']['content_doktypes'], $pageRecord['doktype'])) {
578 $result = FALSE;
579 }
580 } else {
581 $result = FALSE;
582 }
583 return $result;
584 }
585
586 /**
587 * Generates a workspace preview link.
588 *
589 * @param integer $uid The ID of the record to be linked
590 * @return string the full domain including the protocol http:// or https://, but without the trailing '/'
591 */
592 public function generateWorkspacePreviewLink($uid) {
593 $previewObject = t3lib_div::makeInstance('Tx_Version_Preview');
594 $timeToLiveHours = $previewObject->getPreviewLinkLifetime();
595 $previewKeyword = $previewObject->compilePreviewKeyword('', $GLOBALS['BE_USER']->user['uid'], ($timeToLiveHours*3600), $this->getCurrentWorkspace());
596
597 $linkParams = array(
598 'ADMCMD_prev' => $previewKeyword,
599 'id' => $uid
600 );
601 return t3lib_BEfunc::getViewDomain($uid) . '/index.php?' . t3lib_div::implodeArrayForUrl('', $linkParams);
602 }
603
604 /**
605 * Generates a workspace splitted preview link.
606 *
607 * @param integer $uid The ID of the record to be linked
608 * @param boolean $addDomain Parameter to decide if domain should be added to the generated link, FALSE per default
609 * @return string the preview link without the trailing '/'
610 */
611 public function generateWorkspaceSplittedPreviewLink($uid, $addDomain = FALSE) {
612 // In case a $pageUid is submitted we need to make sure it points to a live-page
613 if ($uid > 0) {
614 $uid = $this->getLivePageUid($uid);
615 }
616
617 $objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
618 /** @var $uriBuilder Tx_Extbase_MVC_Web_Routing_UriBuilder */
619 $uriBuilder = $objectManager->create('Tx_Extbase_MVC_Web_Routing_UriBuilder');
620 /**
621 * This seems to be very harsh to set this directly to "/typo3 but the viewOnClick also
622 * has /index.php as fixed value here and dealing with the backPath is very error-prone
623 *
624 * @todo make sure this would work in local extension installation too
625 */
626 $backPath = '/' . TYPO3_mainDir;
627 $redirect = $backPath . 'index.php?redirect_url=';
628 // @todo why do we need these additional params? the URIBuilder should add the controller, but he doesn't :(
629 $additionalParams = '&tx_workspaces_web_workspacesworkspaces%5Bcontroller%5D=Preview&M=web_WorkspacesWorkspaces&id=';
630 $viewScript = $backPath . $uriBuilder->setArguments(array('tx_workspaces_web_workspacesworkspaces' => array('previewWS' => $GLOBALS['BE_USER']->workspace)))
631 ->uriFor('index', array(), 'Tx_Workspaces_Controller_PreviewController', 'workspaces', 'web_workspacesworkspaces') . $additionalParams;
632
633 if ($addDomain === TRUE) {
634 return t3lib_BEfunc::getViewDomain($uid) . $redirect . urlencode($viewScript) . $uid;
635 } else {
636 return $viewScript;
637 }
638 }
639
640 /**
641 * Find the Live-Uid for a given page,
642 * the results are cached at run-time to avoid too many database-queries
643 *
644 * @throws InvalidArgumentException
645 * @param integer $uid
646 * @return integer
647 */
648 public function getLivePageUid($uid) {
649 if (!isset($this->pageCache[$uid])) {
650 $pageRecord = t3lib_beFunc::getRecord('pages', $uid);
651 if (is_array($pageRecord)) {
652 $this->pageCache[$uid] = ($pageRecord['t3ver_oid'] ? $pageRecord['t3ver_oid'] : $uid);
653 } else {
654 throw new InvalidArgumentException('uid is supposed to point to an existing page - given value was:' . $uid, 1290628113);
655 }
656 }
657
658 return $this->pageCache[$uid];
659 }
660 }
661
662
663 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/workspaces/Classes/Service/Workspaces.php'])) {
664 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/workspaces/Classes/Service/Workspaces.php']);
665 }
666 ?>