TYPO3 CMS  TYPO3_8-7
PageLinkHandler.php
Go to the documentation of this file.
1 <?php
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 
29 
34 {
38  protected $expandPage = 0;
39 
45  protected $linkParts = [];
46 
56  public function canHandleLink(array $linkParts)
57  {
58  if (!$linkParts['url']) {
59  return false;
60  }
61 
62  $data = $linkParts['url'];
63  // Checking if the id-parameter is an alias.
64  if (isset($data['pagealias'])) {
65  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
66  ->getQueryBuilderForTable('pages');
67  $queryBuilder->getRestrictions()
68  ->removeAll()
69  ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
70  ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
71 
72  $pageUid = $queryBuilder->select('uid')
73  ->from('pages')
74  ->where(
75  $queryBuilder->expr()->eq(
76  'alias',
77  $queryBuilder->createNamedParameter($data['pagealias'], \PDO::PARAM_STR)
78  )
79  )
80  ->setMaxResults(1)
81  ->execute()
82  ->fetchColumn(0);
83 
84  if ($pageUid === false) {
85  return false;
86  }
87  $data['pageuid'] = (int)$pageUid;
88  }
89  // Check if the page still exists
90  if ((int)$data['pageuid'] > 0) {
91  $pageRow = BackendUtility::getRecordWSOL('pages', $data['pageuid']);
92  if (!$pageRow) {
93  return false;
94  }
95  } elseif ($data['pageuid'] !== 'current') {
96  return false;
97  }
98 
99  $this->linkParts = $linkParts;
100  return true;
101  }
102 
108  public function formatCurrentUrl()
109  {
110  $lang = $this->getLanguageService();
111  $titleLen = (int)$this->getBackendUser()->uc['titleLen'];
112 
113  $id = (int)$this->linkParts['url']['pageuid'];
114  $pageRow = BackendUtility::getRecordWSOL('pages', $id);
115 
116  return htmlspecialchars($lang->getLL('page'))
117  . ' \'' . htmlspecialchars(GeneralUtility::fixed_lgd_cs($pageRow['title'], $titleLen)) . '\''
118  . ' (ID: ' . $id . ($this->linkParts['url']['fragment'] ? ', #' . $this->linkParts['url']['fragment'] : '') . ')';
119  }
120 
128  public function render(ServerRequestInterface $request)
129  {
130  GeneralUtility::makeInstance(PageRenderer::class)->loadRequireJsModule('TYPO3/CMS/Recordlist/PageLinkHandler');
131 
132  $this->expandPage = isset($request->getQueryParams()['expandPage']) ? (int)$request->getQueryParams()['expandPage'] : 0;
133  $this->setTemporaryDbMounts();
134 
135  $backendUser = $this->getBackendUser();
136 
138  $pageTree = GeneralUtility::makeInstance(ElementBrowserPageTreeView::class);
139  $pageTree->setLinkParameterProvider($this);
140  $pageTree->ext_showNavTitle = (bool)$backendUser->getTSConfigVal('options.pageTree.showNavTitle');
141  $pageTree->ext_showPageId = (bool)$backendUser->getTSConfigVal('options.pageTree.showPageIdWithTitle');
142  $pageTree->ext_showPathAboveMounts = (bool)$backendUser->getTSConfigVal('options.pageTree.showPathAboveMounts');
143  $pageTree->addField('nav_title');
144 
145  $this->view->assign('temporaryTreeMountCancelLink', $this->getTemporaryTreeMountCancelNotice());
146  $this->view->assign('tree', $pageTree->getBrowsableTree());
147  $this->getRecordsOnExpandedPage($this->expandPage);
148  return $this->view->render('Page');
149  }
150 
156  protected function getRecordsOnExpandedPage($pageId)
157  {
158  // If there is an anchor value (content element reference) in the element reference, then force an ID to expand:
159  if (!$pageId && isset($this->linkParts['url']['fragment'])) {
160  // Set to the current link page id.
161  $pageId = $this->linkParts['url']['pageuid'];
162  }
163  // Draw the record list IF there is a page id to expand:
164  if ($pageId && MathUtility::canBeInterpretedAsInteger($pageId) && $this->getBackendUser()->isInWebMount($pageId)) {
165  $pageId = (int)$pageId;
166 
167  $activePageRecord = BackendUtility::getRecordWSOL('pages', $pageId);
168  $this->view->assign('expandActivePage', true);
169 
170  // Create header for listing, showing the page title/icon
171  $this->view->assign('activePage', $activePageRecord);
172  $this->view->assign('activePageTitle', BackendUtility::getRecordTitle('pages', $activePageRecord, true));
173  $this->view->assign('activePageIcon', $this->iconFactory->getIconForRecord('pages', $activePageRecord, Icon::SIZE_SMALL)->render());
174 
175  // Look up tt_content elements from the expanded page
176  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
177  ->getQueryBuilderForTable('tt_content');
178 
179  $queryBuilder->getRestrictions()
180  ->removeAll()
181  ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
182  ->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
183 
184  $contentElements = $queryBuilder
185  ->select('*')
186  ->from('tt_content')
187  ->where(
188  $queryBuilder->expr()->eq(
189  'pid',
190  $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT)
191  )
192  )
193  ->orderBy('colPos')
194  ->addOrderBy('sorting')
195  ->execute()
196  ->fetchAll();
197 
198  // Enrich list of records
199  foreach ($contentElements as &$contentElement) {
200  $contentElement['url'] = GeneralUtility::makeInstance(LinkService::class)->asString(['type' => LinkService::TYPE_PAGE, 'pageuid' => (int)$pageId, 'fragment' => $contentElement['uid']]);
201  $contentElement['isSelected'] = !empty($this->linkParts) && (int)$this->linkParts['url']['fragment'] === (int)$contentElement['uid'];
202  $contentElement['icon'] = $this->iconFactory->getIconForRecord('tt_content', $contentElement, Icon::SIZE_SMALL)->render();
203  $contentElement['title'] = BackendUtility::getRecordTitle('tt_content', $contentElement, true);
204  }
205  $this->view->assign('contentElements', $contentElements);
206  }
207  }
208 
215  {
216  if ((int)$this->getBackendUser()->getSessionData('pageTree_temporaryMountPoint') > 0) {
217  return GeneralUtility::linkThisScript(['setTempDBmount' => 0]);
218  }
219  return '';
220  }
221 
225  protected function setTemporaryDbMounts()
226  {
227  $backendUser = $this->getBackendUser();
228 
229  // Clear temporary DB mounts
230  $tmpMount = GeneralUtility::_GET('setTempDBmount');
231  if (isset($tmpMount)) {
232  $backendUser->setAndSaveSessionData('pageTree_temporaryMountPoint', (int)$tmpMount);
233  }
234  // Set temporary DB mounts
235  $alternativeWebmountPoint = (int)$backendUser->getSessionData('pageTree_temporaryMountPoint');
236  if ($alternativeWebmountPoint) {
237  $alternativeWebmountPoint = GeneralUtility::intExplode(',', $alternativeWebmountPoint);
238  $backendUser->setWebmounts($alternativeWebmountPoint);
239  } else {
240  // Setting alternative browsing mounts (ONLY local to browse_links.php this script so they stay "read-only")
241  $alternativeWebmountPoints = trim($backendUser->getTSConfigVal('options.pageTree.altElementBrowserMountPoints'));
242  $appendAlternativeWebmountPoints = $backendUser->getTSConfigVal('options.pageTree.altElementBrowserMountPoints.append');
243  if ($alternativeWebmountPoints) {
244  $alternativeWebmountPoints = GeneralUtility::intExplode(',', $alternativeWebmountPoints);
245  $this->getBackendUser()->setWebmounts($alternativeWebmountPoints, $appendAlternativeWebmountPoints);
246  }
247  }
248  }
249 
253  public function getBodyTagAttributes()
254  {
255  if (count($this->linkParts) === 0 || empty($this->linkParts['url']['pageuid'])) {
256  return [];
257  }
258  return [
259  'data-current-link' => GeneralUtility::makeInstance(LinkService::class)->asString([
260  'type' => LinkService::TYPE_PAGE,
261  'pageuid' => (int)$this->linkParts['url']['pageuid'],
262  'fragment' => $this->linkParts['url']['fragment']
263  ])
264  ];
265  }
266 
272  public function getUrlParameters(array $values)
273  {
274  $parameters = [
275  'expandPage' => isset($values['pid']) ? (int)$values['pid'] : $this->expandPage
276  ];
277  return array_merge($this->linkBrowser->getUrlParameters($values), $parameters);
278  }
279 
285  public function isCurrentlySelectedItem(array $values)
286  {
287  return !empty($this->linkParts) && (int)$this->linkParts['url']['pageuid'] === (int)$values['pid'];
288  }
289 
295  public function getScriptUrl()
296  {
297  return $this->linkBrowser->getScriptUrl();
298  }
299 
304  public function modifyLinkAttributes(array $fieldDefinitions)
305  {
306  $configuration = $this->linkBrowser->getConfiguration();
307  if (!empty($configuration['pageIdSelector.']['enabled'])) {
308  $this->linkAttributes[] = 'pageIdSelector';
309  $fieldDefinitions['pageIdSelector'] = '
310  <form class="form-horizontal"><div class="form-group form-group-sm">
311  <label class="col-xs-4 control-label">
312  ' . htmlspecialchars($this->getLanguageService()->getLL('page_id')) . '
313  </label>
314  <div class="col-xs-2">
315  <input type="number" size="6" name="luid" id="luid" class="form-control" />
316  </div>
317  <div class="col-xs-6">
318  <input class="btn btn-default t3js-pageLink" type="submit" value="' . htmlspecialchars($this->getLanguageService()->getLL('setLink')) . '" />
319  </div>
320  </div></form>';
321  }
322  return $fieldDefinitions;
323  }
324 }
static getRecordWSOL( $table, $uid, $fields=' *', $where='', $useDeleteClause=true, $unsetMovePointers=false)
static intExplode($delimiter, $string, $removeEmptyValues=false, $limit=0)
static linkThisScript(array $getParams=[])
static makeInstance($className,... $constructorArguments)
static getRecordTitle($table, $row, $prep=false, $forceResult=true)
static fixed_lgd_cs($string, $chars, $appendString='...')