TYPO3  7.6
PagePositionMap.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Backend\Tree\View;
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 
25 
31 {
32  // EXTERNAL, static:
36  public $moveOrCopy = 'move';
37 
42 
43  // How deep the position page tree will go.
47  public $depth = 2;
48 
49  // Can be set to the sys_language uid to select content elements for.
54 
55  // INTERNAL, dynamic:
56  // Request uri
60  public $R_URI = '';
61 
62  // Element id.
66  public $elUid = '';
67 
68  // tt_content element uid to move.
72  public $moveUid = '';
73 
74  // Caching arrays:
78  public $getModConfigCache = array();
79 
83  public $checkNewPageCache = array();
84 
85  // Label keys:
89  public $l_insertNewPageHere = 'insertNewPageHere';
90 
94  public $l_insertNewRecordHere = 'insertNewRecordHere';
95 
99  public $modConfigStr = 'mod.web_list.newPageWiz';
100 
106  protected $pageTreeClassName = ElementBrowserPageTreeView::class;
107 
111  protected $iconFactory;
112 
118  public function __construct($pageTreeClassName = null)
119  {
120  if ($pageTreeClassName !== null) {
121  $this->pageTreeClassName = $pageTreeClassName;
122  }
123  $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
124  }
125 
126  /*************************************
127  *
128  * Page position map:
129  *
130  **************************************/
140  public function positionTree($id, $pageinfo, $perms_clause, $R_URI)
141  {
142  // Make page tree object:
144  $pageTree = GeneralUtility::makeInstance($this->pageTreeClassName);
145  $pageTree->init(' AND ' . $perms_clause);
146  $pageTree->addField('pid');
147  // Initialize variables:
148  $this->R_URI = $R_URI;
149  $this->elUid = $id;
150  // Create page tree, in $this->depth levels.
151  $pageTree->getTree($pageinfo['pid'], $this->depth);
152  // Initialize variables:
153  $saveLatestUid = array();
154  $latestInvDepth = $this->depth;
155  // Traverse the tree:
156  $lines = array();
157  foreach ($pageTree->tree as $cc => $dat) {
158  // Make link + parameters.
159  $latestInvDepth = $dat['invertedDepth'];
160  $saveLatestUid[$latestInvDepth] = $dat;
161  if (isset($pageTree->tree[$cc - 1])) {
162  $prev_dat = $pageTree->tree[$cc - 1];
163  // If current page, subpage?
164  if ($prev_dat['row']['uid'] == $id) {
165  // 1) It must be allowed to create a new page and 2) If there are subpages there is no need to render a subpage icon here - it'll be done over the subpages...
166  if (!$this->dontPrintPageInsertIcons && $this->checkNewPageInPid($id) && !($prev_dat['invertedDepth'] > $pageTree->tree[$cc]['invertedDepth'])) {
167  end($lines);
168  $lines[key($lines)] .= '<ul><li><span class="text-nowrap"><a href="#" onclick="' . htmlspecialchars($this->onClickEvent($id, $id, 1)) . '"><i class="t3-icon fa fa-long-arrow-left" title="' . $this->insertlabel() . '"></i></a></span></li></ul>';
169  }
170  }
171  // If going down
172  if ($prev_dat['invertedDepth'] > $pageTree->tree[$cc]['invertedDepth']) {
173  $prevPid = $pageTree->tree[$cc]['row']['pid'];
174  } elseif ($prev_dat['invertedDepth'] < $pageTree->tree[$cc]['invertedDepth']) {
175  // If going up
176  // First of all the previous level should have an icon:
177  if (!$this->dontPrintPageInsertIcons && $this->checkNewPageInPid($prev_dat['row']['pid'])) {
178  $prevPid = -$prev_dat['row']['uid'];
179  end($lines);
180  $lines[key($lines)] .= '<ul><li><span class="text-nowrap"><a href="#" onclick="' . htmlspecialchars($this->onClickEvent($prevPid, $prev_dat['row']['pid'], 2)) . '"><i class="t3-icon fa fa-long-arrow-left" title="' . $this->insertlabel() . '"></i></a></span></li></ul>';
181  }
182  // Then set the current prevPid
183  $prevPid = -$prev_dat['row']['pid'];
184  } else {
185  // In on the same level
186  $prevPid = -$prev_dat['row']['uid'];
187  }
188  } else {
189  // First in the tree
190  $prevPid = $dat['row']['pid'];
191  }
192  // print arrow on the same level
193  if (!$this->dontPrintPageInsertIcons && $this->checkNewPageInPid($dat['row']['pid'])) {
194  $lines[] = '<span class="text-nowrap"><a href="#" onclick="' . htmlspecialchars($this->onClickEvent($prevPid, $dat['row']['pid'], 3)) . '"><i class="t3-icon fa fa-long-arrow-left" title="' . $this->insertlabel() . '"></i></a></span>';
195  }
196  // The line with the icon and title:
197  $toolTip = BackendUtility::getRecordToolTip($dat['row'], 'pages');
198  $icon = '<span ' . $toolTip . '>' . $this->iconFactory->getIconForRecord('pages', $dat['row'], Icon::SIZE_SMALL)->render() . '</span>';
199 
200  $lines[] = '<span class="text-nowrap">' . $icon . $this->linkPageTitle($this->boldTitle(htmlspecialchars(GeneralUtility::fixed_lgd_cs($dat['row']['title'], $this->getBackendUser()->uc['titleLen'])), $dat, $id), $dat['row']) . '</span>';
201  }
202  // If the current page was the last in the tree:
203  $prev_dat = end($pageTree->tree);
204  if ($prev_dat['row']['uid'] == $id) {
205  if (!$this->dontPrintPageInsertIcons && $this->checkNewPageInPid($id)) {
206  $lines[] = '<span class="text-nowrap"><a href="#" onclick="' . htmlspecialchars($this->onClickEvent($id, $id, 4)) . '"><i class="t3-icon fa fa-long-arrow-left" title="' . $this->insertlabel() . '"></i></a></span>';
207  }
208  }
209  for ($a = $latestInvDepth; $a <= $this->depth; $a++) {
210  $dat = $saveLatestUid[$a];
211  $prevPid = -$dat['row']['uid'];
212  if (!$this->dontPrintPageInsertIcons && $this->checkNewPageInPid($dat['row']['pid'])) {
213  $lines[] = '<span class="text-nowrap"><a href="#" onclick="' . htmlspecialchars($this->onClickEvent($prevPid, $dat['row']['pid'], 5)) . '"><i class="t3-icon fa fa-long-arrow-left" title="' . $this->insertlabel() . '"></i></a></span>';
214  }
215  }
216 
217  $code = '<ul class="list-tree">';
218 
219  foreach ($lines as $line) {
220  $code .= '<li>' . $line . '</li>';
221  }
222 
223  $code .= '</ul>';
224  return $code;
225  }
226 
235  public function boldTitle($t_code, $dat, $id)
236  {
237  if ($dat['row']['uid'] == $id) {
238  $t_code = '<strong>' . $t_code . '</strong>';
239  }
240  return $t_code;
241  }
242 
253  public function onClickEvent($pid, $newPagePID)
254  {
255  $TSconfigProp = $this->getModConfig($newPagePID);
256  if ($TSconfigProp['overrideWithExtension']) {
257  if (ExtensionManagementUtility::isLoaded($TSconfigProp['overrideWithExtension'])) {
258  $onclick = 'window.location.href=' . GeneralUtility::quoteJSvalue(ExtensionManagementUtility::extRelPath($TSconfigProp['overrideWithExtension']) . 'mod1/index.php?cmd=crPage&positionPid=' . $pid) . ';';
259  return $onclick;
260  }
261  }
262  $params = '&edit[pages][' . $pid . ']=new&returnNewPageId=1';
263  return BackendUtility::editOnClick($params, '', $this->R_URI);
264  }
265 
271  public function insertlabel()
272  {
273  return $this->getLanguageService()->getLL($this->l_insertNewPageHere, 1);
274  }
275 
283  public function linkPageTitle($str, $rec)
284  {
285  return $str;
286  }
287 
295  public function checkNewPageInPid($pid)
296  {
297  if (!isset($this->checkNewPageCache[$pid])) {
298  $pidInfo = BackendUtility::getRecord('pages', $pid);
299  $this->checkNewPageCache[$pid] = $this->getBackendUser()->isAdmin() || $this->getBackendUser()->doesUserHaveAccess($pidInfo, 8);
300  }
301  return $this->checkNewPageCache[$pid];
302  }
303 
311  public function getModConfig($pid)
312  {
313  if (!isset($this->getModConfigCache[$pid])) {
314  // Acquiring TSconfig for this PID:
315  $this->getModConfigCache[$pid] = BackendUtility::getModTSconfig($pid, $this->modConfigStr);
316  }
317  return $this->getModConfigCache[$pid]['properties'];
318  }
319 
320  /*************************************
321  *
322  * Content element positioning:
323  *
324  **************************************/
335  public function printContentElementColumns($pid, $moveUid, $colPosList, $showHidden, $R_URI)
336  {
337  $this->R_URI = $R_URI;
338  $this->moveUid = $moveUid;
339  $colPosArray = GeneralUtility::trimExplode(',', $colPosList, true);
340  $lines = array();
341  foreach ($colPosArray as $kk => $vv) {
342  $res = $this->getDatabase()->exec_SELECTquery('*', 'tt_content', 'pid=' . (int)$pid . ($showHidden ? '' : BackendUtility::BEenableFields('tt_content')) . ' AND colPos=' . (int)$vv . ((string)$this->cur_sys_language !== '' ? ' AND sys_language_uid=' . (int)$this->cur_sys_language : '') . BackendUtility::deleteClause('tt_content') . BackendUtility::versioningPlaceholderClause('tt_content'), '', 'sorting');
343  $lines[$vv] = array();
344  $lines[$vv][] = $this->insertPositionIcon('', $vv, $kk, $moveUid, $pid);
345  while ($row = $this->getDatabase()->sql_fetch_assoc($res)) {
346  BackendUtility::workspaceOL('tt_content', $row);
347  if (is_array($row)) {
348  $lines[$vv][] = $this->wrapRecordHeader($this->getRecordHeader($row), $row);
349  $lines[$vv][] = $this->insertPositionIcon($row, $vv, $kk, $moveUid, $pid);
350  }
351  }
352  $this->getDatabase()->sql_free_result($res);
353  }
354  return $this->printRecordMap($lines, $colPosArray, $pid);
355  }
356 
365  public function printRecordMap($lines, $colPosArray, $pid = 0)
366  {
367  $count = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange(count($colPosArray), 1);
368  $backendLayout = GeneralUtility::callUserFunction(\TYPO3\CMS\Backend\View\BackendLayoutView::class . '->getSelectedBackendLayout', $pid, $this);
369  if (isset($backendLayout['__config']['backend_layout.'])) {
370  $this->getLanguageService()->includeLLFile('EXT:backend/Resources/Private/Language/locallang_layout.xlf');
371  $table = '<div class="table-fit"><table class="table table-condensed table-bordered table-vertical-top">';
372  $colCount = (int)$backendLayout['__config']['backend_layout.']['colCount'];
373  $rowCount = (int)$backendLayout['__config']['backend_layout.']['rowCount'];
374  $table .= '<colgroup>';
375  for ($i = 0; $i < $colCount; $i++) {
376  $table .= '<col style="width:' . 100 / $colCount . '%"></col>';
377  }
378  $table .= '</colgroup>';
379  $table .= '<tbody>';
380  $tcaItems = GeneralUtility::callUserFunction(\TYPO3\CMS\Backend\View\BackendLayoutView::class . '->getColPosListItemsParsed', $pid, $this);
381  // Cycle through rows
382  for ($row = 1; $row <= $rowCount; $row++) {
383  $rowConfig = $backendLayout['__config']['backend_layout.']['rows.'][$row . '.'];
384  if (!isset($rowConfig)) {
385  continue;
386  }
387  $table .= '<tr>';
388  for ($col = 1; $col <= $colCount; $col++) {
389  $columnConfig = $rowConfig['columns.'][$col . '.'];
390  if (!isset($columnConfig)) {
391  continue;
392  }
393  // Which tt_content colPos should be displayed inside this cell
394  $columnKey = (int)$columnConfig['colPos'];
395  $head = '';
396  foreach ($tcaItems as $item) {
397  if ($item[1] == $columnKey) {
398  $head = $this->getLanguageService()->sL($item[0], true);
399  }
400  }
401  // Render the grid cell
402  $table .= '<td'
403  . (isset($columnConfig['colspan']) ? ' colspan="' . $columnConfig['colspan'] . '"' : '')
404  . (isset($columnConfig['rowspan']) ? ' rowspan="' . $columnConfig['rowspan'] . '"' : '')
405  . ' class="col-nowrap col-min'
406  . (!isset($columnConfig['colPos']) ? ' warning' : '')
407  . (isset($columnConfig['colPos']) && !$head ? ' danger' : '') . '">';
408  // Render header
409  $table .= '<p>';
410  if (isset($columnConfig['colPos']) && $head) {
411  $table .= '<strong>' . $this->wrapColumnHeader($head, '', '') . '</strong>';
412  } elseif ($columnConfig['colPos']) {
413  $table .= '<em>' . $this->wrapColumnHeader($this->getLanguageService()->getLL('noAccess'), '', '') . '</em>';
414  } else {
415  $table .= '<em>' . $this->wrapColumnHeader(($columnConfig['name']?: '') . ' (' . $this->getLanguageService()->getLL('notAssigned') . ')', '', '') . '</em>';
416  }
417  $table .= '</p>';
418  // Render lines
419  if (isset($columnConfig['colPos']) && $head && !empty($lines[$columnKey])) {
420  $table .= '<ul class="list-unstyled">';
421  foreach ($lines[$columnKey] as $line) {
422  $table .= '<li>' . $line . '</li>';
423  }
424  $table .= '</ul>';
425  }
426  $table .= '</td>';
427  }
428  $table .= '</tr>';
429  }
430  $table .= '</tbody>';
431  $table .= '</table></div>';
432  } else {
433  // Traverse the columns here:
434  $row = '';
435  foreach ($colPosArray as $kk => $vv) {
436  $row .= '<td class="col-nowrap col-min" width="' . round(100 / $count) . '%">';
437  $row .= '<p><strong>' . $this->wrapColumnHeader($this->getLanguageService()->sL(BackendUtility::getLabelFromItemlist('tt_content', 'colPos', $vv), true), $vv) . '</strong></p>';
438  if (!empty($lines[$vv])) {
439  $row .= '<ul class="list-unstyled">';
440  foreach ($lines[$vv] as $line) {
441  $row .= '<li>' . $line . '</li>';
442  }
443  $row .= '</ul>';
444  }
445  $row .= '</td>';
446  }
447  $table = '
448 
449  <!--
450  Map of records in columns:
451  -->
452  <div class="table-fit">
453  <table class="table table-condensed table-bordered table-vertical-top">
454  <tr>' . $row . '</tr>
455  </table>
456  </div>
457 
458  ';
459  }
460  return $table;
461  }
462 
471  public function wrapColumnHeader($str, $vv)
472  {
473  return $str;
474  }
475 
487  public function insertPositionIcon($row, $vv, $kk, $moveUid, $pid)
488  {
489  if (is_array($row) && !empty($row['uid'])) {
490  // Use record uid for the hash when inserting after this content element
491  $uid = $row['uid'];
492  } else {
493  // No uid means insert at first position in the column
494  $uid = '';
495  }
496  $cc = hexdec(substr(md5($uid . '-' . $vv . '-' . $kk), 0, 4));
497  return '<a href="#" onclick="' . htmlspecialchars($this->onClickInsertRecord($row, $vv, $moveUid, $pid, $this->cur_sys_language)) . '">' . '<i class="t3-icon fa fa-long-arrow-left" name="mImgEnd' . $cc . '" title="' . $this->getLanguageService()->getLL($this->l_insertNewRecordHere, 1) . '"></i></a>';
498  }
499 
511  public function onClickInsertRecord($row, $vv, $moveUid, $pid, $sys_lang = 0)
512  {
513  $table = 'tt_content';
514  if (is_array($row)) {
515  $location = BackendUtility::getModuleUrl('tce_db') . '&cmd[' . $table . '][' . $moveUid . '][' . $this->moveOrCopy . ']=-' . $row['uid'] . '&prErr=1&uPT=1&vC=' . $this->getBackendUser()->veriCode();
516  } else {
517  $location = BackendUtility::getModuleUrl('tce_db') . '&cmd[' . $table . '][' . $moveUid . '][' . $this->moveOrCopy . ']=' . $pid . '&data[' . $table . '][' . $moveUid . '][colPos]=' . $vv . '&prErr=1&vC=' . $this->getBackendUser()->veriCode();
518  }
519  $location .= '&redirect=' . rawurlencode($this->R_URI);
520  // returns to prev. page
521  return 'window.location.href=' . GeneralUtility::quoteJSvalue($location) . ';return false;';
522  }
523 
531  public function wrapRecordHeader($str, $row)
532  {
533  return $str;
534  }
535 
542  public function getRecordHeader($row)
543  {
544  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
545  $toolTip = BackendUtility::getRecordToolTip($row, 'tt_content');
546  $line = '<span ' . $toolTip . ' title="' . BackendUtility::getRecordIconAltText($row, 'tt_content') . '">' . $iconFactory->getIconForRecord('tt_content', $row, Icon::SIZE_SMALL)->render() . '</span>';
547  $line .= BackendUtility::getRecordTitle('tt_content', $row, true);
548  return $this->wrapRecordTitle($line, $row);
549  }
550 
558  public function wrapRecordTitle($str, $row)
559  {
560  return '<a href="' . htmlspecialchars(GeneralUtility::linkThisScript(array('uid' => (int)$row['uid'], 'moveUid' => ''))) . '">' . $str . '</a>';
561  }
562 
568  protected function getBackendUser()
569  {
570  return $GLOBALS['BE_USER'];
571  }
572 
578  protected function getLanguageService()
579  {
580  return $GLOBALS['LANG'];
581  }
582 
586  protected function getDatabase()
587  {
588  return $GLOBALS['TYPO3_DB'];
589  }
590 }