TYPO3 CMS  TYPO3_6-2
SuggestDefaultReceiver.php
Go to the documentation of this file.
1 <?php
3 
20 
31 
37  protected $table = '';
38 
45  protected $mmForeignTable = '';
46 
53  protected $selectClause = '';
54 
60  protected $orderByStatement = '';
61 
67  protected $addWhere = '';
68 
74  protected $config = array();
75 
81  protected $allowedPages = array();
82 
88  protected $maxItems = 10;
89 
93  protected $params = array();
94 
102  public function __construct($table, $config) {
103  $this->table = $table;
104  $this->config = $config;
105  // get a list of all the pages that should be looked on
106  if (isset($config['pidList'])) {
107  $allowedPages = ($pageIds = GeneralUtility::trimExplode(',', $config['pidList']));
108  $depth = (int)$config['pidDepth'];
109  foreach ($pageIds as $pageId) {
110  if ($pageId > 0) {
112  }
113  }
114  $this->allowedPages = array_unique($allowedPages);
115  }
116  if (isset($config['maxItemsInResultList'])) {
117  $this->maxItems = $config['maxItemsInResultList'];
118  }
119  if ($this->table == 'pages') {
120  $this->addWhere = ' AND ' . $GLOBALS['BE_USER']->getPagePermsClause(1);
121  }
122  // if table is versionized, only get the records from the Live Workspace
123  // the overlay itself of WS-records is done below
124  if ($GLOBALS['TCA'][$this->table]['ctrl']['versioningWS'] == TRUE) {
125  $this->addWhere .= ' AND t3ver_wsid = 0';
126  }
127  if (isset($config['addWhere'])) {
128  $this->addWhere .= ' ' . $config['addWhere'];
129  }
130  }
131 
144  public function queryTable(&$params, $recursionCounter = 0) {
145  $rows = array();
146  $this->params = &$params;
147  $start = $recursionCounter * 50;
148  $this->prepareSelectStatement();
149  $this->prepareOrderByStatement();
150  $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', $this->table, $this->selectClause, '', $this->orderByStatement, $start . ', 50');
151  $allRowsCount = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
152  if ($allRowsCount) {
153  while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
154  // check if we already have collected the maximum number of records
155  if (count($rows) > $this->maxItems) {
156  break;
157  }
158  $this->manipulateRecord($row);
159  $this->makeWorkspaceOverlay($row);
160  // check if the user has access to the record
161  if (!$this->checkRecordAccess($row, $row['uid'])) {
162  continue;
163  }
165  $this->table, $row, array('style' => 'margin: 0 4px 0 -20px; padding: 0;')
166  );
167  $uid = $row['t3ver_oid'] > 0 ? $row['t3ver_oid'] : $row['uid'];
168  $path = $this->getRecordPath($row, $uid);
169  if (strlen($path) > 30) {
170  $croppedPath = '<abbr title="' . htmlspecialchars($path) . '">' . htmlspecialchars(($GLOBALS['LANG']->csConvObj->crop($GLOBALS['LANG']->charSet, $path, 10) . '...' . $GLOBALS['LANG']->csConvObj->crop($GLOBALS['LANG']->charSet, $path, -20))) . '</abbr>';
171  } else {
172  $croppedPath = htmlspecialchars($path);
173  }
174  $label = $this->getLabel($row);
175  $entry = array(
176  'text' => '<span class="suggest-label">' . $label . '</span><span class="suggest-uid">[' . $uid . ']</span><br />
177  <span class="suggest-path">' . $croppedPath . '</span>',
178  'table' => $this->mmForeignTable ? $this->mmForeignTable : $this->table,
179  'label' => $label,
180  'path' => $path,
181  'uid' => $uid,
182  'style' => '',
183  'class' => isset($this->config['cssClass']) ? $this->config['cssClass'] : '',
184  'sprite' => $spriteIcon
185  );
186  $rows[$this->table . '_' . $uid] = $this->renderRecord($row, $entry);
187  }
188  $GLOBALS['TYPO3_DB']->sql_free_result($res);
189  // if there are less records than we need, call this function again to get more records
190  if (count($rows) < $this->maxItems && $allRowsCount >= 50 && $recursionCounter < $this->maxItems) {
191  $tmp = self::queryTable($params, ++$recursionCounter);
192  $rows = array_merge($tmp, $rows);
193  }
194  }
195  return $rows;
196  }
197 
204  protected function prepareSelectStatement() {
205  $searchWholePhrase = $this->config['searchWholePhrase'];
206  $searchString = $this->params['value'];
207  $searchUid = (int)$searchString;
208  if (strlen($searchString)) {
209  $searchString = $GLOBALS['TYPO3_DB']->quoteStr($searchString, $this->table);
210  $likeCondition = ' LIKE \'' . ($searchWholePhrase ? '%' : '') . $GLOBALS['TYPO3_DB']->escapeStrForLike($searchString, $this->table) . '%\'';
211  // Search in all fields given by label or label_alt
212  $selectFieldsList = $GLOBALS['TCA'][$this->table]['ctrl']['label'] . ',' . $GLOBALS['TCA'][$this->table]['ctrl']['label_alt'] . ',' . $this->config['additionalSearchFields'];
213  $selectFields = GeneralUtility::trimExplode(',', $selectFieldsList, TRUE);
214  $selectFields = array_unique($selectFields);
215  $selectParts = array();
216  foreach ($selectFields as $field) {
217  $selectParts[] = $field . $likeCondition;
218  }
219  $this->selectClause = '(' . implode(' OR ', $selectParts) . ')';
220  if ($searchUid > 0 && $searchUid == $searchString) {
221  $this->selectClause = '(' . $this->selectClause . ' OR uid = ' . $searchUid . ')';
222  }
223  }
224  if (isset($GLOBALS['TCA'][$this->table]['ctrl']['delete'])) {
225  $this->selectClause .= ' AND ' . $GLOBALS['TCA'][$this->table]['ctrl']['delete'] . ' = 0';
226  }
227  if (count($this->allowedPages)) {
228  $pidList = $GLOBALS['TYPO3_DB']->cleanIntArray($this->allowedPages);
229  if (count($pidList)) {
230  $this->selectClause .= ' AND pid IN (' . implode(', ', $pidList) . ') ';
231  }
232  }
233  // add an additional search condition comment
234  if (isset($this->config['searchCondition']) && strlen($this->config['searchCondition']) > 0) {
235  $this->selectClause .= ' AND ' . $this->config['searchCondition'];
236  }
237  // add the global clauses to the where-statement
238  $this->selectClause .= $this->addWhere;
239  }
240 
248  protected function getAllSubpagesOfPage($uid, $depth = 99) {
249  $pageIds = array($uid);
250  $level = 0;
251  $pages = array($uid);
252  // fetch all
253  while ($depth - $level > 0 && !empty($pageIds)) {
254  ++$level;
255  $pidList = $GLOBALS['TYPO3_DB']->cleanIntArray($pageIds);
256  $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid', 'pages', 'pid IN (' . implode(', ', $pidList) . ')', '', '', '', 'uid');
257  if (count($rows) > 0) {
258  $pageIds = array_keys($rows);
259  $pages = array_merge($pages, $pageIds);
260  } else {
261  break;
262  }
263  }
264  return $pages;
265  }
266 
273  protected function prepareOrderByStatement() {
274  if ($GLOBALS['TCA'][$this->table]['ctrl']['label']) {
275  $this->orderByStatement = $GLOBALS['TCA'][$this->table]['ctrl']['label'];
276  }
277  }
278 
284  protected function manipulateRecord(&$row) {
285 
286  }
287 
295  protected function checkRecordAccess($row, $uid) {
296  $retValue = TRUE;
297  $table = $this->mmForeignTable ?: $this->table;
298  if ($table == 'pages') {
299  if (!BackendUtility::readPageAccess($uid, $GLOBALS['BE_USER']->getPagePermsClause(1))) {
300  $retValue = FALSE;
301  }
302  } elseif (isset($GLOBALS['TCA'][$table]['ctrl']['is_static']) && (bool) $GLOBALS['TCA'][$table]['ctrl']['is_static']) {
303  $retValue = TRUE;
304  } else {
305  if (!is_array(BackendUtility::readPageAccess($row['pid'], $GLOBALS['BE_USER']->getPagePermsClause(1)))) {
306  $retValue = FALSE;
307  }
308  }
309  return $retValue;
310  }
311 
318  protected function makeWorkspaceOverlay(&$row) {
319  // Check for workspace-versions
320  if ($GLOBALS['BE_USER']->workspace != 0 && $GLOBALS['TCA'][$this->table]['ctrl']['versioningWS'] == TRUE) {
321  BackendUtility::workspaceOL($this->mmForeignTable ? $this->mmForeignTable : $this->table, $row);
322  }
323  }
324 
331  protected function getIcon($row) {
332  $icon = IconUtility::getIcon($this->mmForeignTable ? $this->mmForeignTable : $this->table, $row);
333  return IconUtility::skinImg('', $icon, '', 1);
334  }
335 
346  protected function getRecordPath(&$row, $uid) {
347  $titleLimit = max($this->config['maxPathTitleLength'], 0);
348  if (($this->mmForeignTable ? $this->mmForeignTable : $this->table) == 'pages') {
349  $path = BackendUtility::getRecordPath($uid, '', $titleLimit);
350  // For pages we only want the first (n-1) parts of the path,
351  // because the n-th part is the page itself
352  $path = substr($path, 0, strrpos($path, '/', -2)) . '/';
353  } else {
354  $path = BackendUtility::getRecordPath($row['pid'], '', $titleLimit);
355  }
356  return $path;
357  }
358 
365  protected function getLabel($row) {
366  return BackendUtility::getRecordTitle($this->mmForeignTable ? $this->mmForeignTable : $this->table, $row, TRUE);
367  }
368 
378  protected function renderRecord($row, $entry) {
379  // Call renderlet if available (normal pages etc. usually don't have one)
380  if ($this->config['renderFunc'] != '') {
381  $params = array(
382  'table' => $this->table,
383  'uid' => $row['uid'],
384  'row' => $row,
385  'entry' => &$entry
386  );
387  GeneralUtility::callUserFunction($this->config['renderFunc'], $params, $this, '');
388  }
389  return $entry;
390  }
391 
392 }
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=TRUE, $includeEmptyValues=TRUE, $enableUnsetFeature=TRUE)
static skinImg($backPath, $src, $wHattribs='', $outputMode=0)
static readPageAccess($id, $perms_clause)
static workspaceOL($table, &$row, $wsid=-99, $unsetMovePointers=FALSE)
$uid
Definition: server.php:36
static trimExplode($delim, $string, $removeEmptyValues=FALSE, $limit=0)
static callUserFunction($funcName, &$params, &$ref, $checkPrefix='', $errorMode=0)
static getRecordTitle($table, $row, $prep=FALSE, $forceResult=TRUE)
static getSpriteIconForRecord($table, array $row, array $options=array())
static getIcon($table, $row=array(), $shaded=FALSE)
static getRecordPath($uid, $clause, $titleLimit, $fullTitleLimit=0)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]