TYPO3 CMS  TYPO3_7-6
SuggestWizardDefaultReceiver.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 
22 
30 {
36  protected $table = '';
37 
44  protected $mmForeignTable = '';
45 
52  protected $selectClause = '';
53 
59  protected $orderByStatement = '';
60 
66  protected $addWhere = '';
67 
73  protected $config = [];
74 
80  protected $allowedPages = [];
81 
87  protected $maxItems = 10;
88 
92  protected $params = [];
93 
97  protected $iconFactory;
98 
105  public function __construct($table, $config)
106  {
107  $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
108  $this->table = $table;
109  $this->config = $config;
110  // get a list of all the pages that should be looked on
111  if (isset($config['pidList'])) {
112  $allowedPages = ($pageIds = GeneralUtility::trimExplode(',', $config['pidList']));
113  $depth = (int)$config['pidDepth'];
114  foreach ($pageIds as $pageId) {
115  if ($pageId > 0) {
117  }
118  }
119  $this->allowedPages = array_unique($allowedPages);
120  }
121  if (isset($config['maxItemsInResultList'])) {
122  $this->maxItems = $config['maxItemsInResultList'];
123  }
124  if ($this->table == 'pages') {
125  $this->addWhere = ' AND ' . $GLOBALS['BE_USER']->getPagePermsClause(1);
126  }
127  // if table is versionized, only get the records from the Live Workspace
128  // the overlay itself of WS-records is done below
129  if ($GLOBALS['TCA'][$this->table]['ctrl']['versioningWS'] == true) {
130  $this->addWhere .= ' AND t3ver_wsid = 0';
131  }
132  if (isset($config['addWhere'])) {
133  $this->addWhere .= ' ' . $config['addWhere'];
134  }
135  }
136 
149  public function queryTable(&$params, $recursionCounter = 0)
150  {
151  $rows = [];
152  $this->params = &$params;
153  $start = $recursionCounter * 50;
154  $this->prepareSelectStatement();
155  $this->prepareOrderByStatement();
156  $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $this->table, $this->selectClause, '', $this->orderByStatement, $start . ', 50');
157  $allRowsCount = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
158  if ($allRowsCount) {
159  while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
160  // check if we already have collected the maximum number of records
161  if (count($rows) > $this->maxItems) {
162  break;
163  }
164  $this->manipulateRecord($row);
165  $this->makeWorkspaceOverlay($row);
166  // check if the user has access to the record
167  if (!$this->checkRecordAccess($row, $row['uid'])) {
168  continue;
169  }
170  $spriteIcon = $this->iconFactory->getIconForRecord($this->table, $row, Icon::SIZE_SMALL)->render();
171  $uid = $row['t3ver_oid'] > 0 ? $row['t3ver_oid'] : $row['uid'];
172  $path = $this->getRecordPath($row, $uid);
173  if (strlen($path) > 30) {
174  $languageService = $this->getLanguageService();
175  $croppedPath = '<abbr title="' . htmlspecialchars($path) . '">' .
176  htmlspecialchars(
177  $languageService->csConvObj->crop($languageService->charSet, $path, 10)
178  . '...'
179  . $languageService->csConvObj->crop($languageService->charSet, $path, -20)
180  ) .
181  '</abbr>';
182  } else {
183  $croppedPath = htmlspecialchars($path);
184  }
185  $label = $this->getLabel($row);
186  $entry = [
187  'text' => '<span class="suggest-label">' . $label . '</span><span class="suggest-uid">[' . $uid . ']</span><br />
188  <span class="suggest-path">' . $croppedPath . '</span>',
189  'table' => $this->mmForeignTable ? $this->mmForeignTable : $this->table,
190  'label' => $label,
191  'path' => $path,
192  'uid' => $uid,
193  'style' => '',
194  'class' => isset($this->config['cssClass']) ? $this->config['cssClass'] : '',
195  'sprite' => $spriteIcon
196  ];
197  $rows[$this->table . '_' . $uid] = $this->renderRecord($row, $entry);
198  }
199  $GLOBALS['TYPO3_DB']->sql_free_result($res);
200  // if there are less records than we need, call this function again to get more records
201  if (count($rows) < $this->maxItems && $allRowsCount >= 50 && $recursionCounter < $this->maxItems) {
202  $tmp = self::queryTable($params, ++$recursionCounter);
203  $rows = array_merge($tmp, $rows);
204  }
205  }
206  return $rows;
207  }
208 
215  protected function prepareSelectStatement()
216  {
217  $searchWholePhrase = !isset($this->config['searchWholePhrase']) || $this->config['searchWholePhrase'];
218  $searchString = $this->params['value'];
219  $searchUid = (int)$searchString;
220  if ($searchString !== '') {
221  $searchString = $GLOBALS['TYPO3_DB']->quoteStr($searchString, $this->table);
222  $likeCondition = ' LIKE \'' . ($searchWholePhrase ? '%' : '') . $GLOBALS['TYPO3_DB']->escapeStrForLike($searchString, $this->table) . '%\'';
223  // Search in all fields given by label or label_alt
224  $selectFieldsList = $GLOBALS['TCA'][$this->table]['ctrl']['label'] . ',' . $GLOBALS['TCA'][$this->table]['ctrl']['label_alt'] . ',' . $this->config['additionalSearchFields'];
225  $selectFields = GeneralUtility::trimExplode(',', $selectFieldsList, true);
226  $selectFields = array_unique($selectFields);
227  $selectParts = [];
228  foreach ($selectFields as $field) {
229  $selectParts[] = $field . $likeCondition;
230  }
231  $this->selectClause = '(' . implode(' OR ', $selectParts) . ')';
232  if ($searchUid > 0 && $searchUid == $searchString) {
233  $this->selectClause = '(' . $this->selectClause . ' OR uid = ' . $searchUid . ')';
234  }
235  }
236  if (isset($GLOBALS['TCA'][$this->table]['ctrl']['delete'])) {
237  $this->selectClause .= ' AND ' . $GLOBALS['TCA'][$this->table]['ctrl']['delete'] . ' = 0';
238  }
239  if (!empty($this->allowedPages)) {
240  $pidList = $GLOBALS['TYPO3_DB']->cleanIntArray($this->allowedPages);
241  if (!empty($pidList)) {
242  $this->selectClause .= ' AND pid IN (' . implode(', ', $pidList) . ') ';
243  }
244  }
245  // add an additional search condition comment
246  if (isset($this->config['searchCondition']) && $this->config['searchCondition'] !== '') {
247  $this->selectClause .= ' AND ' . $this->config['searchCondition'];
248  }
249  // add the global clauses to the where-statement
250  $this->selectClause .= $this->addWhere;
251  }
252 
260  protected function getAllSubpagesOfPage($uid, $depth = 99)
261  {
262  $pageIds = [$uid];
263  $level = 0;
264  $pages = [$uid];
265  // fetch all
266  while ($depth - $level > 0 && !empty($pageIds)) {
267  ++$level;
268  $pidList = $GLOBALS['TYPO3_DB']->cleanIntArray($pageIds);
269  $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid', 'pages', 'pid IN (' . implode(', ', $pidList) . ')', '', '', '', 'uid');
270  if (!empty($rows)) {
271  $pageIds = array_keys($rows);
272  $pages = array_merge($pages, $pageIds);
273  } else {
274  break;
275  }
276  }
277  return $pages;
278  }
279 
286  protected function prepareOrderByStatement()
287  {
288  if ($GLOBALS['TCA'][$this->table]['ctrl']['label']) {
289  $this->orderByStatement = $GLOBALS['TCA'][$this->table]['ctrl']['label'];
290  }
291  }
292 
298  protected function manipulateRecord(&$row)
299  {
300  }
301 
309  protected function checkRecordAccess($row, $uid)
310  {
311  $retValue = true;
312  $table = $this->mmForeignTable ?: $this->table;
313  if ($table == 'pages') {
314  if (!BackendUtility::readPageAccess($uid, $GLOBALS['BE_USER']->getPagePermsClause(1))) {
315  $retValue = false;
316  }
317  } elseif (isset($GLOBALS['TCA'][$table]['ctrl']['is_static']) && (bool)$GLOBALS['TCA'][$table]['ctrl']['is_static']) {
318  $retValue = true;
319  } else {
320  if (!is_array(BackendUtility::readPageAccess($row['pid'], $GLOBALS['BE_USER']->getPagePermsClause(1)))) {
321  $retValue = false;
322  }
323  }
324  return $retValue;
325  }
326 
333  protected function makeWorkspaceOverlay(&$row)
334  {
335  // Check for workspace-versions
336  if ($GLOBALS['BE_USER']->workspace != 0 && $GLOBALS['TCA'][$this->table]['ctrl']['versioningWS'] == true) {
337  BackendUtility::workspaceOL($this->mmForeignTable ? $this->mmForeignTable : $this->table, $row);
338  }
339  }
340 
348  protected function getIcon($row)
349  {
351  return $this->iconFactory->getIconForRecord($this->mmForeignTable ?: $this->table, $row, Icon::SIZE_SMALL)->render();
352  }
353 
364  protected function getRecordPath(&$row, $uid)
365  {
366  $titleLimit = max($this->config['maxPathTitleLength'], 0);
367  if (($this->mmForeignTable ? $this->mmForeignTable : $this->table) == 'pages') {
368  $path = BackendUtility::getRecordPath($uid, '', $titleLimit);
369  // For pages we only want the first (n-1) parts of the path,
370  // because the n-th part is the page itself
371  $path = substr($path, 0, strrpos($path, '/', -2)) . '/';
372  } else {
373  $path = BackendUtility::getRecordPath($row['pid'], '', $titleLimit);
374  }
375  return $path;
376  }
377 
384  protected function getLabel($row)
385  {
386  return BackendUtility::getRecordTitle($this->mmForeignTable ? $this->mmForeignTable : $this->table, $row, true);
387  }
388 
398  protected function renderRecord($row, $entry)
399  {
400  // Call renderlet if available (normal pages etc. usually don't have one)
401  if ($this->config['renderFunc'] != '') {
402  $params = [
403  'table' => $this->table,
404  'uid' => $row['uid'],
405  'row' => $row,
406  'entry' => &$entry
407  ];
408  GeneralUtility::callUserFunction($this->config['renderFunc'], $params, $this, '');
409  }
410  return $entry;
411  }
412 
416  protected function getLanguageService()
417  {
418  return $GLOBALS['LANG'];
419  }
420 }
static readPageAccess($id, $perms_clause)
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
static workspaceOL($table, &$row, $wsid=-99, $unsetMovePointers=false)
static callUserFunction($funcName, &$params, &$ref, $checkPrefix='', $errorMode=0)
static getRecordTitle($table, $row, $prep=false, $forceResult=true)
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
$uid
Definition: server.php:38
static getRecordPath($uid, $clause, $titleLimit, $fullTitleLimit=0)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']