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