TYPO3 CMS  TYPO3_8-7
PagePositionMap.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 
28 
34 {
35  // EXTERNAL, static:
39  public $moveOrCopy = 'move';
40 
45 
46  // How deep the position page tree will go.
50  public $depth = 2;
51 
52  // Can be set to the sys_language uid to select content elements for.
57 
58  // INTERNAL, dynamic:
59  // Request uri
63  public $R_URI = '';
64 
65  // Element id.
69  public $elUid = '';
70 
71  // tt_content element uid to move.
75  public $moveUid = '';
76 
77  // Caching arrays:
81  public $getModConfigCache = [];
82 
86  public $checkNewPageCache = [];
87 
88  // Label keys:
92  public $l_insertNewPageHere = 'insertNewPageHere';
93 
97  public $l_insertNewRecordHere = 'insertNewRecordHere';
98 
102  public $modConfigStr = 'mod.web_list.newPageWiz';
103 
109  protected $pageTreeClassName = ElementBrowserPageTreeView::class;
110 
114  protected $iconFactory;
115 
121  public function __construct($pageTreeClassName = null)
122  {
123  if ($pageTreeClassName !== null) {
124  $this->pageTreeClassName = $pageTreeClassName;
125  }
126  $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
127  }
128 
129  /*************************************
130  *
131  * Page position map:
132  *
133  **************************************/
143  public function positionTree($id, $pageinfo, $perms_clause, $R_URI)
144  {
145  // Make page tree object:
147  $pageTree = GeneralUtility::makeInstance($this->pageTreeClassName);
148  $pageTree->init(' AND ' . $perms_clause);
149  $pageTree->addField('pid');
150  // Initialize variables:
151  $this->R_URI = $R_URI;
152  $this->elUid = $id;
153  // Create page tree, in $this->depth levels.
154  $pageTree->getTree($pageinfo['pid'], $this->depth);
155  // Initialize variables:
156  $saveLatestUid = [];
157  $latestInvDepth = $this->depth;
158  // Traverse the tree:
159  $lines = [];
160  foreach ($pageTree->tree as $cc => $dat) {
161  if ($latestInvDepth > $dat['invertedDepth']) {
162  $margin = 'style="margin-left: ' . ($dat['invertedDepth'] * 16 + 9) . 'px;"';
163  $lines[] = '<ul class="list-tree" ' . $margin . '>';
164  }
165  // Make link + parameters.
166  $latestInvDepth = $dat['invertedDepth'];
167  $saveLatestUid[$latestInvDepth] = $dat;
168  if (isset($pageTree->tree[$cc - 1])) {
169  $prev_dat = $pageTree->tree[$cc - 1];
170  // If current page, subpage?
171  if ($prev_dat['row']['uid'] == $id) {
172  // 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...
173  if (!$this->dontPrintPageInsertIcons && $this->checkNewPageInPid($id) && !($prev_dat['invertedDepth'] > $pageTree->tree[$cc]['invertedDepth'])) {
174  end($lines);
175  $margin = 'style="margin-left: ' . (($dat['invertedDepth'] - 1) * 16 + 9) . 'px;"';
176  $lines[] = '<ul class="list-tree" ' . $margin . '><li><span class="text-nowrap"><a href="#" onclick="' . htmlspecialchars($this->onClickEvent($id, $id)) . '"><i class="t3-icon fa fa-long-arrow-left" title="' . $this->insertlabel() . '"></i></a></span></li></ul>';
177  }
178  }
179  // If going down
180  if ($prev_dat['invertedDepth'] > $pageTree->tree[$cc]['invertedDepth']) {
181  $prevPid = $pageTree->tree[$cc]['row']['pid'];
182  } elseif ($prev_dat['invertedDepth'] < $pageTree->tree[$cc]['invertedDepth']) {
183  // If going up
184  // First of all the previous level should have an icon:
185  if (!$this->dontPrintPageInsertIcons && $this->checkNewPageInPid($prev_dat['row']['pid'])) {
186  $prevPid = -$prev_dat['row']['uid'];
187  end($lines);
188  $lines[] = '<li><span class="text-nowrap"><a href="#" onclick="' . htmlspecialchars($this->onClickEvent($prevPid, $prev_dat['row']['pid'])) . '"><i class="t3-icon fa fa-long-arrow-left" title="' . $this->insertlabel() . '"></i></a></span></li>';
189  }
190  // Then set the current prevPid
191  $prevPid = -$prev_dat['row']['pid'];
192  if ($prevPid !== $dat['row']['pid']) {
193  $lines[] = '</ul>';
194  }
195  } else {
196  // In on the same level
197  $prevPid = -$prev_dat['row']['uid'];
198  }
199  } else {
200  // First in the tree
201  $prevPid = $dat['row']['pid'];
202  }
203  // print arrow on the same level
204  if (!$this->dontPrintPageInsertIcons && $this->checkNewPageInPid($dat['row']['pid'])) {
205  $lines[] = '<span class="text-nowrap"><a href="#" onclick="' . htmlspecialchars($this->onClickEvent($prevPid, $dat['row']['pid'])) . '"><i class="t3-icon fa fa-long-arrow-left" title="' . $this->insertlabel() . '"></i></a></span>';
206  }
207  // The line with the icon and title:
208  $toolTip = BackendUtility::getRecordToolTip($dat['row'], 'pages');
209  $icon = '<span ' . $toolTip . '>' . $this->iconFactory->getIconForRecord('pages', $dat['row'], Icon::SIZE_SMALL)->render() . '</span>';
210 
211  $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>';
212  }
213  // If the current page was the last in the tree:
214  $prev_dat = end($pageTree->tree);
215  if ($prev_dat['row']['uid'] == $id) {
216  if (!$this->dontPrintPageInsertIcons && $this->checkNewPageInPid($id)) {
217  $lines[] = '<ul class="list-tree" style="margin-left: 25px"><li><span class="text-nowrap"><a href="#" onclick="' . htmlspecialchars($this->onClickEvent($id, $id)) . '"><i class="t3-icon fa fa-long-arrow-left" title="' . $this->insertlabel() . '"></i></a></span></li></ul>';
218  }
219  }
220  for ($a = $latestInvDepth; $a <= $this->depth; $a++) {
221  $dat = $saveLatestUid[$a];
222  $prevPid = -$dat['row']['uid'];
223  if (!$this->dontPrintPageInsertIcons && $this->checkNewPageInPid($dat['row']['pid'])) {
224  if ($latestInvDepth < $dat['invertedDepth']) {
225  $lines[] = '</ul>';
226  }
227  $lines[] = '<span class="text-nowrap"><a href="#" onclick="' . htmlspecialchars($this->onClickEvent($prevPid, $dat['row']['pid'])) . '"><i class="t3-icon fa fa-long-arrow-left" title="' . $this->insertlabel() . '"></i></a></span>';
228  }
229  }
230 
231  $code = '<ul class="list-tree">';
232 
233  foreach ($lines as $line) {
234  if ((substr($line, 0, 3) === '<ul') || (substr($line, 0, 4) === '</ul')) {
235  $code .= $line;
236  } else {
237  $code .= '<li>' . $line . '</li>';
238  }
239  }
240 
241  $code .= '</ul>';
242  return $code;
243  }
244 
253  public function boldTitle($t_code, $dat, $id)
254  {
255  if ($dat['row']['uid'] == $id) {
256  $t_code = '<strong>' . $t_code . '</strong>';
257  }
258  return $t_code;
259  }
260 
271  public function onClickEvent($pid, $newPagePID)
272  {
273  $TSconfig = BackendUtility::getModTSconfig($newPagePID, 'mod.newPageWizard');
274  $TSconfig = $TSconfig['properties'];
275  if (isset($TSconfig['override']) && !empty($TSconfig['override'])) {
276  $url = BackendUtility::getModuleUrl(
277  $TSconfig['override'],
278  [
279  'positionPid' => $pid,
280  'newPageId' => $newPagePID,
281  'cmd' => 'crPage',
282  'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI')]
283  );
284  return 'window.location.href=' . GeneralUtility::quoteJSvalue($url) . ';';
285  }
286  $params = '&edit[pages][' . $pid . ']=new&returnNewPageId=1';
287  return BackendUtility::editOnClick($params, '', $this->R_URI);
288  }
289 
295  public function insertlabel()
296  {
297  return htmlspecialchars($this->getLanguageService()->getLL($this->l_insertNewPageHere));
298  }
299 
307  public function linkPageTitle($str, $rec)
308  {
309  return $str;
310  }
311 
319  public function checkNewPageInPid($pid)
320  {
321  if (!isset($this->checkNewPageCache[$pid])) {
322  $pidInfo = BackendUtility::getRecord('pages', $pid);
323  $this->checkNewPageCache[$pid] = $this->getBackendUser()->isAdmin() || $this->getBackendUser()->doesUserHaveAccess($pidInfo, 8);
324  }
325  return $this->checkNewPageCache[$pid];
326  }
327 
335  public function getModConfig($pid)
336  {
337  if (!isset($this->getModConfigCache[$pid])) {
338  // Acquiring TSconfig for this PID:
339  $this->getModConfigCache[$pid] = BackendUtility::getModTSconfig($pid, $this->modConfigStr);
340  }
341  return $this->getModConfigCache[$pid]['properties'];
342  }
343 
344  /*************************************
345  *
346  * Content element positioning:
347  *
348  **************************************/
359  public function printContentElementColumns($pid, $moveUid, $colPosList, $showHidden, $R_URI)
360  {
361  $this->R_URI = $R_URI;
362  $this->moveUid = $moveUid;
363  $colPosArray = GeneralUtility::trimExplode(',', $colPosList, true);
364  $lines = [];
365  foreach ($colPosArray as $kk => $vv) {
366  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tt_content');
367  $queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(BackendWorkspaceRestriction::class));
368  if ($showHidden) {
369  $queryBuilder->getRestrictions()
370  ->removeByType(HiddenRestriction::class)
371  ->removeByType(StartTimeRestriction::class)
372  ->removeByType(EndTimeRestriction::class);
373  }
374  $queryBuilder
375  ->select('*')
376  ->from('tt_content')
377  ->where(
378  $queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)),
379  $queryBuilder->expr()->eq('colPos', $queryBuilder->createNamedParameter($vv, \PDO::PARAM_INT))
380  )
381  ->orderBy('sorting');
382 
383  if ((string)$this->cur_sys_language !== '') {
384  $queryBuilder->andWhere(
385  $queryBuilder->expr()->eq(
386  'sys_language_uid',
387  $queryBuilder->createNamedParameter($this->cur_sys_language, \PDO::PARAM_INT)
388  )
389  );
390  }
391 
392  $res = $queryBuilder->execute();
393  $lines[$vv] = [];
394  $lines[$vv][] = $this->insertPositionIcon('', $vv, $kk, $moveUid, $pid);
395 
396  while ($row = $res->fetch()) {
397  BackendUtility::workspaceOL('tt_content', $row);
398  if (is_array($row)) {
399  $lines[$vv][] = $this->wrapRecordHeader($this->getRecordHeader($row), $row);
400  $lines[$vv][] = $this->insertPositionIcon($row, $vv, $kk, $moveUid, $pid);
401  }
402  }
403  }
404  return $this->printRecordMap($lines, $colPosArray, $pid);
405  }
406 
415  public function printRecordMap($lines, $colPosArray, $pid = 0)
416  {
417  $count = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange(count($colPosArray), 1);
418  $backendLayout = GeneralUtility::callUserFunction(\TYPO3\CMS\Backend\View\BackendLayoutView::class . '->getSelectedBackendLayout', $pid, $this);
419  if (isset($backendLayout['__config']['backend_layout.'])) {
420  $this->getLanguageService()->includeLLFile('EXT:backend/Resources/Private/Language/locallang_layout.xlf');
421  $table = '<div class="table-fit"><table class="table table-condensed table-bordered table-vertical-top">';
422  $colCount = (int)$backendLayout['__config']['backend_layout.']['colCount'];
423  $rowCount = (int)$backendLayout['__config']['backend_layout.']['rowCount'];
424  $table .= '<colgroup>';
425  for ($i = 0; $i < $colCount; $i++) {
426  $table .= '<col style="width:' . 100 / $colCount . '%"></col>';
427  }
428  $table .= '</colgroup>';
429  $table .= '<tbody>';
430  $tcaItems = GeneralUtility::callUserFunction(\TYPO3\CMS\Backend\View\BackendLayoutView::class . '->getColPosListItemsParsed', $pid, $this);
431  // Cycle through rows
432  for ($row = 1; $row <= $rowCount; $row++) {
433  $rowConfig = $backendLayout['__config']['backend_layout.']['rows.'][$row . '.'];
434  if (!isset($rowConfig)) {
435  continue;
436  }
437  $table .= '<tr>';
438  for ($col = 1; $col <= $colCount; $col++) {
439  $columnConfig = $rowConfig['columns.'][$col . '.'];
440  if (!isset($columnConfig)) {
441  continue;
442  }
443  // Which tt_content colPos should be displayed inside this cell
444  $columnKey = (int)$columnConfig['colPos'];
445  $head = '';
446  foreach ($tcaItems as $item) {
447  if ($item[1] == $columnKey) {
448  $head = htmlspecialchars($this->getLanguageService()->sL($item[0]));
449  }
450  }
451  // Render the grid cell
452  $table .= '<td'
453  . (isset($columnConfig['colspan']) ? ' colspan="' . $columnConfig['colspan'] . '"' : '')
454  . (isset($columnConfig['rowspan']) ? ' rowspan="' . $columnConfig['rowspan'] . '"' : '')
455  . ' class="col-nowrap col-min'
456  . (!isset($columnConfig['colPos']) ? ' warning' : '')
457  . (isset($columnConfig['colPos']) && !$head ? ' danger' : '') . '">';
458  // Render header
459  $table .= '<p>';
460  if (isset($columnConfig['colPos']) && $head) {
461  $table .= '<strong>' . $this->wrapColumnHeader($head, '', '') . '</strong>';
462  } elseif ($columnConfig['colPos']) {
463  $table .= '<em>' . $this->wrapColumnHeader($this->getLanguageService()->getLL('noAccess'), '', '') . '</em>';
464  } else {
465  $table .= '<em>' . $this->wrapColumnHeader(($this->getLanguageService()->sL($columnConfig['name']) ?: '') . ' (' . $this->getLanguageService()->getLL('notAssigned') . ')', '', '') . '</em>';
466  }
467  $table .= '</p>';
468  // Render lines
469  if (isset($columnConfig['colPos']) && $head && !empty($lines[$columnKey])) {
470  $table .= '<ul class="list-unstyled">';
471  foreach ($lines[$columnKey] as $line) {
472  $table .= '<li>' . $line . '</li>';
473  }
474  $table .= '</ul>';
475  }
476  $table .= '</td>';
477  }
478  $table .= '</tr>';
479  }
480  $table .= '</tbody>';
481  $table .= '</table></div>';
482  } else {
483  // Traverse the columns here:
484  $row = '';
485  foreach ($colPosArray as $kk => $vv) {
486  $row .= '<td class="col-nowrap col-min" width="' . round(100 / $count) . '%">';
487  $row .= '<p><strong>' . $this->wrapColumnHeader(htmlspecialchars($this->getLanguageService()->sL(BackendUtility::getLabelFromItemlist('tt_content', 'colPos', $vv))), $vv) . '</strong></p>';
488  if (!empty($lines[$vv])) {
489  $row .= '<ul class="list-unstyled">';
490  foreach ($lines[$vv] as $line) {
491  $row .= '<li>' . $line . '</li>';
492  }
493  $row .= '</ul>';
494  }
495  $row .= '</td>';
496  }
497  $table = '
498 
499  <!--
500  Map of records in columns:
501  -->
502  <div class="table-fit">
503  <table class="table table-condensed table-bordered table-vertical-top">
504  <tr>' . $row . '</tr>
505  </table>
506  </div>
507 
508  ';
509  }
510  return $table;
511  }
512 
521  public function wrapColumnHeader($str, $vv)
522  {
523  return $str;
524  }
525 
537  public function insertPositionIcon($row, $vv, $kk, $moveUid, $pid)
538  {
539  if (is_array($row) && !empty($row['uid'])) {
540  // Use record uid for the hash when inserting after this content element
541  $uid = $row['uid'];
542  } else {
543  // No uid means insert at first position in the column
544  $uid = '';
545  }
546  $cc = hexdec(substr(md5($uid . '-' . $vv . '-' . $kk), 0, 4));
547  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="' . htmlspecialchars($this->getLanguageService()->getLL($this->l_insertNewRecordHere)) . '"></i></a>';
548  }
549 
561  public function onClickInsertRecord($row, $vv, $moveUid, $pid, $sys_lang = 0)
562  {
563  $table = 'tt_content';
564  if (is_array($row)) {
565  $location = BackendUtility::getModuleUrl('tce_db') . '&cmd[' . $table . '][' . $moveUid . '][' . $this->moveOrCopy . ']=-' . $row['uid'] . '&prErr=1&uPT=1';
566  } else {
567  $location = BackendUtility::getModuleUrl('tce_db') . '&cmd[' . $table . '][' . $moveUid . '][' . $this->moveOrCopy . ']=' . $pid . '&data[' . $table . '][' . $moveUid . '][colPos]=' . $vv . '&prErr=1';
568  }
569  $location .= '&redirect=' . rawurlencode($this->R_URI);
570  // returns to prev. page
571  return 'window.location.href=' . GeneralUtility::quoteJSvalue($location) . ';return false;';
572  }
573 
581  public function wrapRecordHeader($str, $row)
582  {
583  return $str;
584  }
585 
592  public function getRecordHeader($row)
593  {
594  $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
595  $toolTip = BackendUtility::getRecordToolTip($row, 'tt_content');
596  $line = '<span ' . $toolTip . ' title="' . BackendUtility::getRecordIconAltText($row, 'tt_content') . '">' . $iconFactory->getIconForRecord('tt_content', $row, Icon::SIZE_SMALL)->render() . '</span>';
597  $line .= BackendUtility::getRecordTitle('tt_content', $row, true);
598  return $this->wrapRecordTitle($line, $row);
599  }
600 
608  public function wrapRecordTitle($str, $row)
609  {
610  return '<a href="' . htmlspecialchars(GeneralUtility::linkThisScript(['uid' => (int)$row['uid'], 'moveUid' => ''])) . '">' . $str . '</a>';
611  }
612 
618  protected function getBackendUser()
619  {
620  return $GLOBALS['BE_USER'];
621  }
622 
628  protected function getLanguageService()
629  {
630  return $GLOBALS['LANG'];
631  }
632 }
static editOnClick($params, $_='', $requestUri='')
static forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:31
static callUserFunction($funcName, &$params, &$ref, $_='', $errorMode=0)
insertPositionIcon($row, $vv, $kk, $moveUid, $pid)
static getRecordToolTip(array $row, $table='pages')
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
static workspaceOL($table, &$row, $wsid=-99, $unsetMovePointers=false)
static linkThisScript(array $getParams=[])
static makeInstance($className,... $constructorArguments)
onClickInsertRecord($row, $vv, $moveUid, $pid, $sys_lang=0)
printRecordMap($lines, $colPosArray, $pid=0)
static getRecordTitle($table, $row, $prep=false, $forceResult=true)
printContentElementColumns($pid, $moveUid, $colPosList, $showHidden, $R_URI)
static getRecordIconAltText($row, $table='pages')
static fixed_lgd_cs($string, $chars, $appendString='...')
static getRecord($table, $uid, $fields=' *', $where='', $useDeleteClause=true)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static getLabelFromItemlist($table, $col, $key)