[TASK] Move workspace preview functionality into versioning / workspace
[Packages/TYPO3.CMS.git] / typo3 / sysext / workspaces / Classes / Service / Workspaces.php
index e2fc593..c6d0faa 100644 (file)
  * @package Workspaces
  * @subpackage Service
  */
-class tx_Workspaces_Service_Workspaces {
+class tx_Workspaces_Service_Workspaces implements t3lib_Singleton {
+       /**
+        * @var array
+        */
+       protected $pageCache = array();
+
        const TABLE_WORKSPACE = 'sys_workspace';
        const SELECT_ALL_WORKSPACES = -98;
        const LIVE_WORKSPACE_ID = 0;
-       const DRAFT_WORKSPACE_ID = -1;
 
        /**
         * retrieves the available workspaces from the database and checks whether
@@ -49,9 +53,6 @@ class tx_Workspaces_Service_Workspaces {
                if ($GLOBALS['BE_USER']->checkWorkspace(array('uid' => (string) self::LIVE_WORKSPACE_ID))) {
                        $availableWorkspaces[self::LIVE_WORKSPACE_ID] = self::getWorkspaceTitle(self::LIVE_WORKSPACE_ID);
                }
-               if ($GLOBALS['BE_USER']->checkWorkspace(array('uid' => (string) self::DRAFT_WORKSPACE_ID))) {
-                       $availableWorkspaces[self::DRAFT_WORKSPACE_ID] = self::getWorkspaceTitle(self::DRAFT_WORKSPACE_ID);
-               }
 
                        // add custom workspaces (selecting all, filtering by BE_USER check):
                $customWorkspaces = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid, title, adminusers, members', 'sys_workspace', 'pid = 0' . t3lib_BEfunc::deleteClause('sys_workspace'), '', 'title');
@@ -66,6 +67,19 @@ class tx_Workspaces_Service_Workspaces {
                return $availableWorkspaces;
        }
 
+       /**
+        * Gets the current workspace ID.
+        *
+        * @return integer The current workspace ID
+        */
+       public function getCurrentWorkspace() {
+               $workspaceId = $GLOBALS['BE_USER']->workspace;
+               if ($GLOBALS['BE_USER']->isAdmin()) {
+                       $activeId = $GLOBALS['BE_USER']->getSessionData('tx_workspace_activeWorkspace');
+                       $workspaceId = $activeId !== NULL ? $activeId : $workspaceId;
+               }
+               return $workspaceId;
+       }
 
        /**
         * Find the title for the requested workspace.
@@ -79,9 +93,6 @@ class tx_Workspaces_Service_Workspaces {
                        case self::LIVE_WORKSPACE_ID:
                                $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_misc.xml:shortcut_onlineWS');
                                break;
-                       case self::DRAFT_WORKSPACE_ID:
-                               $title = $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_misc.xml:shortcut_offlineWS');
-                               break;
                        default:
                                $labelField = $GLOBALS['TCA']['sys_workspace']['ctrl']['label'];
                                $wsRecord = t3lib_beFunc::getRecord('sys_workspace', $wsId, 'uid,' . $labelField);
@@ -548,6 +559,81 @@ class tx_Workspaces_Service_Workspaces {
                }
                return $result;
        }
+
+       /**
+        * Generates a workspace preview link.
+        *
+        * @param integer $uid The ID of the record to be linked
+        * @return string the full domain including the protocol http:// or https://, but without the trailing '/'
+        */
+       public function generateWorkspacePreviewLink($uid) {
+               $previewObject = t3lib_div::makeInstance('Tx_Version_Preview');
+               $timeToLiveHours = $previewObject->getPreviewLinkLifetime();
+               $previewKeyword = $previewObject->compilePreviewKeyword('', $GLOBALS['BE_USER']->user['uid'], ($timeToLiveHours*3600), $this->getCurrentWorkspace());
+
+               $linkParams = array(
+                       'ADMCMD_prev' => $previewKeyword,
+                       'id' => $uid
+               );
+               return t3lib_BEfunc::getViewDomain($uid) . '/index.php?' . t3lib_div::implodeArrayForUrl('', $linkParams);
+       }
+
+       /**
+        * Generates a workspace splitted preview link.
+        *
+        * @param integer $uid The ID of the record to be linked
+        * @param boolean $addDomain Parameter to decide if domain should be added to the generated link, FALSE per default
+        * @return string the preview link without the trailing '/'
+        */
+       public function generateWorkspaceSplittedPreviewLink($uid, $addDomain = FALSE) {
+                       // In case a $pageUid is submitted we need to make sure it points to a live-page
+               if ($uid >  0) {
+                       $uid = $this->getLivePageUid($uid);
+               }
+
+               $objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
+               /** @var $uriBuilder Tx_Extbase_MVC_Web_Routing_UriBuilder */
+               $uriBuilder = $objectManager->create('Tx_Extbase_MVC_Web_Routing_UriBuilder');
+               /**
+                *  This seems to be very harsh to set this directly to "/typo3 but the viewOnClick also
+                *  has /index.php as fixed value here and dealing with the backPath is very error-prone
+                *
+                *  @todo make sure this would work in local extension installation too
+                */
+               $backPath = '/' . TYPO3_mainDir;
+               $redirect = $backPath . 'index.php?redirect_url=';
+                       // @todo why do we need these additional params? the URIBuilder should add the controller, but he doesn't :(
+               $additionalParams = '&tx_workspaces_web_workspacesworkspaces%5Bcontroller%5D=Preview&M=web_WorkspacesWorkspaces&id=';
+               $viewScript = $backPath . $uriBuilder->setArguments(array('tx_workspaces_web_workspacesworkspaces' => array('previewWS' => $GLOBALS['BE_USER']->workspace)))
+                                                                                               ->uriFor('index', array(), 'Tx_Workspaces_Controller_PreviewController', 'workspaces', 'web_workspacesworkspaces') . $additionalParams;
+
+               if ($addDomain === TRUE) {
+                       return t3lib_BEfunc::getViewDomain($uid) . $redirect . urlencode($viewScript) . $uid;
+               } else {
+                       return $viewScript;
+               }
+       }
+
+       /**
+        * Find the Live-Uid for a given page,
+        * the results are cached at run-time to avoid too many database-queries
+        *
+        * @throws InvalidArgumentException
+        * @param integer $uid
+        * @return integer
+        */
+       public function getLivePageUid($uid) {
+               if (!isset($this->pageCache[$uid])) {
+                       $pageRecord = t3lib_beFunc::getRecord('pages', $uid);
+                       if (is_array($pageRecord)) {
+                               $this->pageCache[$uid] = ($pageRecord['t3ver_oid'] ? $pageRecord['t3ver_oid'] : $uid);
+                       } else {
+                               throw new InvalidArgumentException('uid is supposed to point to an existing page - given value was:' . $uid, 1290628113);
+                       }
+               }
+
+               return $this->pageCache[$uid];
+       }
 }