[CLEANUP] Add phpDoc to properties in several extensions
[Packages/TYPO3.CMS.git] / typo3 / sysext / recordlist / Classes / RecordList / AbstractDatabaseRecordList.php
1 <?php
2 namespace TYPO3\CMS\Recordlist\RecordList;
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
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Child class for rendering of Web > List (not the final class.
22 * Shared between Web>List (db_list.php) and Web>Page (sysext/cms/layout/db_layout.php)
23 *
24 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
25 * @see localRecordList
26 */
27 class AbstractDatabaseRecordList extends \TYPO3\CMS\Backend\RecordList\AbstractRecordList {
28
29 /**
30 * Specify a list of tables which are the only ones allowed to be displayed.
31 *
32 * @var string
33 */
34 public $tableList = '';
35
36 /**
37 * Return URL
38 *
39 * @var string
40 */
41 public $returnUrl = '';
42
43 /**
44 * Thumbnails on records containing files (pictures)
45 *
46 * @var bool
47 */
48 public $thumbs = 0;
49
50 /**
51 * default Max items shown per table in "multi-table mode", may be overridden by tables.php
52 *
53 * @var int
54 */
55 public $itemsLimitPerTable = 20;
56
57 /**
58 * default Max items shown per table in "single-table mode", may be overridden by tables.php
59 *
60 * @var int
61 */
62 public $itemsLimitSingleTable = 100;
63
64 /**
65 * @var string
66 */
67 public $widthGif = '<img src="clear.gif" width="1" height="4" hspace="160" alt="" />';
68
69 /**
70 * Current script name
71 *
72 * @var string
73 */
74 public $script = 'index.php';
75
76 /**
77 * Indicates if all available fields for a user should be selected or not.
78 *
79 * @var int
80 */
81 public $allFields = 0;
82
83 /**
84 * Whether to show localization view or not
85 *
86 * @var bool
87 */
88 public $localizationView = FALSE;
89
90 /**
91 * If set, csvList is outputted.
92 *
93 * @var bool
94 */
95 public $csvOutput = FALSE;
96
97 /**
98 * Field, to sort list by
99 *
100 * @var string
101 */
102 public $sortField;
103
104 /**
105 * Field, indicating to sort in reverse order.
106 *
107 * @var bool
108 */
109 public $sortRev;
110
111 /**
112 * Containing which fields to display in extended mode
113 *
114 * @var array
115 */
116 public $displayFields;
117
118 /**
119 * String, can contain the field name from a table which must have duplicate values marked.
120 *
121 * @var string
122 */
123 public $duplicateField;
124
125 /**
126 * Page id
127 *
128 * @var int
129 */
130 public $id;
131
132 /**
133 * Tablename if single-table mode
134 *
135 * @var string
136 */
137 public $table = '';
138
139 /**
140 * If TRUE, records are listed only if a specific table is selected.
141 *
142 * @var bool
143 */
144 public $listOnlyInSingleTableMode = FALSE;
145
146 /**
147 * Pointer for browsing list
148 *
149 * @var int
150 */
151 public $firstElementNumber = 0;
152
153 /**
154 * Search string
155 *
156 * @var string
157 */
158 public $searchString = '';
159
160 /**
161 * Levels to search down.
162 *
163 * @var int
164 */
165 public $searchLevels = '';
166
167 /**
168 * Number of records to show
169 *
170 * @var int
171 */
172 public $showLimit = 0;
173
174 /**
175 * Query part for either a list of ids "pid IN (1,2,3)" or a single id "pid = 123" from
176 * which to select/search etc. (when search-levels are set high). See start()
177 *
178 * @var string
179 */
180 public $pidSelect = '';
181
182 /**
183 * Page select permissions
184 *
185 * @var string
186 */
187 public $perms_clause = '';
188
189 /**
190 * Some permissions...
191 *
192 * @var int
193 */
194 public $calcPerms = 0;
195
196 /**
197 * Mode for what happens when a user clicks the title of a record.
198 *
199 * @var string
200 */
201 public $clickTitleMode = '';
202
203 /**
204 * Shared module configuration, used by localization features
205 *
206 * @var array
207 */
208 public $modSharedTSconfig = array();
209
210 /**
211 * Loaded with page record with version overlay if any.
212 *
213 * @var array
214 */
215 public $pageRecord = array();
216
217 /**
218 * Tables which should not get listed
219 *
220 * @var string
221 */
222 public $hideTables = '';
223
224 /**
225 * Tables which should not list their translations
226 *
227 * @var string
228 */
229 public $hideTranslations = '';
230
231 /**
232 * TSconfig which overwrites TCA-Settings
233 *
234 * @var array
235 */
236 public $tableTSconfigOverTCA = array();
237
238 /**
239 * Array of collapsed / uncollapsed tables in multi table view
240 *
241 * @var array
242 */
243 public $tablesCollapsed = array();
244
245 /**
246 * JavaScript code accumulation
247 *
248 * @var string
249 */
250 public $JScode = '';
251
252 /**
253 * HTML output
254 *
255 * @var string
256 */
257 public $HTMLcode = '';
258
259 /**
260 * "LIMIT " in SQL...
261 *
262 * @var int
263 */
264 public $iLimit = 0;
265
266 /**
267 * Counting the elements no matter what...
268 *
269 * @var int
270 */
271 public $eCounter = 0;
272
273 /**
274 * Set to the total number of items for a table when selecting.
275 *
276 * @var string
277 */
278 public $totalItems = '';
279
280 /**
281 * Cache for record path
282 *
283 * @var array
284 */
285 public $recPath_cache = array();
286
287 /**
288 * Fields to display for the current table
289 *
290 * @var array
291 */
292 public $setFields = array();
293
294 /**
295 * Used for tracking next/prev uids
296 *
297 * @var array
298 */
299 public $currentTable = array();
300
301 /**
302 * Used for tracking duplicate values of fields
303 *
304 * @var array
305 */
306 public $duplicateStack = array();
307
308 /**
309 * @var array Module configuration
310 */
311 public $modTSconfig;
312
313 /**
314 * Override/add urlparameters in listUrl() method
315 * @var array
316 */
317 protected $overrideUrlParameters = array();
318
319 /**
320 * Initializes the list generation
321 *
322 * @param int $id Page id for which the list is rendered. Must be >= 0
323 * @param string $table Tablename - if extended mode where only one table is listed at a time.
324 * @param int $pointer Browsing pointer.
325 * @param string $search Search word, if any
326 * @param int $levels Number of levels to search down the page tree
327 * @param int $showLimit Limit of records to be listed.
328 * @return void
329 */
330 public function start($id, $table, $pointer, $search = '', $levels = '', $showLimit = 0) {
331 // Setting internal variables:
332 // sets the parent id
333 $this->id = (int)$id;
334 if ($GLOBALS['TCA'][$table]) {
335 // Setting single table mode, if table exists:
336 $this->table = $table;
337 }
338 $this->firstElementNumber = $pointer;
339 $this->searchString = trim($search);
340 $this->searchLevels = trim($levels);
341 $this->showLimit = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($showLimit, 0, 10000);
342 // Setting GPvars:
343 $this->csvOutput = GeneralUtility::_GP('csv') ? TRUE : FALSE;
344 $this->sortField = GeneralUtility::_GP('sortField');
345 $this->sortRev = GeneralUtility::_GP('sortRev');
346 $this->displayFields = GeneralUtility::_GP('displayFields');
347 $this->duplicateField = GeneralUtility::_GP('duplicateField');
348 if (GeneralUtility::_GP('justLocalized')) {
349 $this->localizationRedirect(GeneralUtility::_GP('justLocalized'));
350 }
351 // Init dynamic vars:
352 $this->counter = 0;
353 $this->JScode = '';
354 $this->HTMLcode = '';
355 // Limits
356 if (isset($this->modTSconfig['properties']['itemsLimitPerTable'])) {
357 $this->itemsLimitPerTable = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange((int)$this->modTSconfig['properties']['itemsLimitPerTable'], 1, 10000);
358 }
359 if (isset($this->modTSconfig['properties']['itemsLimitSingleTable'])) {
360 $this->itemsLimitSingleTable = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange((int)$this->modTSconfig['properties']['itemsLimitSingleTable'], 1, 10000);
361 }
362 // Set search levels:
363 $searchLevels = (int)$this->searchLevels;
364 $this->perms_clause = $GLOBALS['BE_USER']->getPagePermsClause(1);
365 // This will hide records from display - it has nothing todo with user rights!!
366 if ($pidList = $GLOBALS['BE_USER']->getTSConfigVal('options.hideRecords.pages')) {
367 if ($pidList = $GLOBALS['TYPO3_DB']->cleanIntList($pidList)) {
368 $this->perms_clause .= ' AND pages.uid NOT IN (' . $pidList . ')';
369 }
370 }
371 // Get configuration of collapsed tables from user uc and merge with sanitized GP vars
372 $this->tablesCollapsed = is_array($GLOBALS['BE_USER']->uc['moduleData']['list']) ? $GLOBALS['BE_USER']->uc['moduleData']['list'] : array();
373 $collapseOverride = GeneralUtility::_GP('collapse');
374 if (is_array($collapseOverride)) {
375 foreach ($collapseOverride as $collapseTable => $collapseValue) {
376 if (is_array($GLOBALS['TCA'][$collapseTable]) && ($collapseValue == 0 || $collapseValue == 1)) {
377 $this->tablesCollapsed[$collapseTable] = $collapseValue;
378 }
379 }
380 // Save modified user uc
381 $GLOBALS['BE_USER']->uc['moduleData']['list'] = $this->tablesCollapsed;
382 $GLOBALS['BE_USER']->writeUC($GLOBALS['BE_USER']->uc);
383 $returnUrl = GeneralUtility::sanitizeLocalUrl(GeneralUtility::_GP('returnUrl'));
384 if ($returnUrl !== '') {
385 \TYPO3\CMS\Core\Utility\HttpUtility::redirect($returnUrl);
386 }
387 }
388 if ($searchLevels > 0) {
389 $allowedMounts = $this->getSearchableWebmounts($this->id, $searchLevels, $this->perms_clause);
390 $pidList = implode(',', $GLOBALS['TYPO3_DB']->cleanIntArray($allowedMounts));
391 $this->pidSelect = 'pid IN (' . $pidList . ')';
392 } elseif ($searchLevels < 0) {
393 // Search everywhere
394 $this->pidSelect = '1=1';
395 } else {
396 $this->pidSelect = 'pid=' . (int)$id;
397 }
398 // Initialize languages:
399 if ($this->localizationView) {
400 $this->initializeLanguages();
401 }
402 }
403
404 /**
405 * Traverses the table(s) to be listed and renders the output code for each:
406 * The HTML is accumulated in $this->HTMLcode
407 * Finishes off with a stopper-gif
408 *
409 * @return void
410 */
411 public function generateList() {
412 // Set page record in header
413 $this->pageRecord = BackendUtility::getRecordWSOL('pages', $this->id);
414 // Traverse the TCA table array:
415 foreach ($GLOBALS['TCA'] as $tableName => $value) {
416 // Checking if the table should be rendered:
417 // Checks that we see only permitted/requested tables:
418 if ((!$this->table || $tableName == $this->table) && (!$this->tableList || GeneralUtility::inList($this->tableList, $tableName)) && $GLOBALS['BE_USER']->check('tables_select', $tableName)) {
419 // Don't show table if hidden by TCA ctrl section
420 $hideTable = $GLOBALS['TCA'][$tableName]['ctrl']['hideTable'] ? TRUE : FALSE;
421 // Don't show table if hidden by pageTSconfig mod.web_list.hideTables
422 if (in_array($tableName, GeneralUtility::trimExplode(',', $this->hideTables))) {
423 $hideTable = TRUE;
424 }
425 // Override previous selection if table is enabled or hidden by TSconfig TCA override mod.web_list.table
426 if (isset($this->tableTSconfigOverTCA[$tableName . '.']['hideTable'])) {
427 $hideTable = $this->tableTSconfigOverTCA[$tableName . '.']['hideTable'] ? TRUE : FALSE;
428 }
429 if ($hideTable) {
430 continue;
431 }
432 // check if we are in single- or multi-table mode
433 if ($this->table) {
434 $this->iLimit = isset($GLOBALS['TCA'][$tableName]['interface']['maxSingleDBListItems']) ? (int)$GLOBALS['TCA'][$tableName]['interface']['maxSingleDBListItems'] : $this->itemsLimitSingleTable;
435 } else {
436 // if there are no records in table continue current foreach
437 $firstRow = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
438 'uid',
439 $tableName,
440 $this->pidSelect . BackendUtility::deleteClause($tableName) . BackendUtility::versioningPlaceholderClause($tableName)
441 );
442 if ($firstRow === FALSE) {
443 continue;
444 }
445 $this->iLimit = isset($GLOBALS['TCA'][$tableName]['interface']['maxDBListItems']) ? (int)$GLOBALS['TCA'][$tableName]['interface']['maxDBListItems'] : $this->itemsLimitPerTable;
446 }
447 if ($this->showLimit) {
448 $this->iLimit = $this->showLimit;
449 }
450 // Setting fields to select:
451 if ($this->allFields) {
452 $fields = $this->makeFieldList($tableName);
453 $fields[] = 'tstamp';
454 $fields[] = 'crdate';
455 $fields[] = '_PATH_';
456 $fields[] = '_CONTROL_';
457 if (is_array($this->setFields[$tableName])) {
458 $fields = array_intersect($fields, $this->setFields[$tableName]);
459 } else {
460 $fields = array();
461 }
462 } else {
463 $fields = array();
464 }
465 // Find ID to use (might be different for "versioning_followPages" tables)
466 if ((int)$this->searchLevels === 0) {
467 $this->pidSelect = 'pid=' . (int)$this->id;
468 }
469 // Finally, render the list:
470 $this->HTMLcode .= $this->getTable($tableName, $this->id, implode(',', $fields));
471 }
472 }
473 }
474
475 /**
476 * Creates the search box
477 *
478 * @param bool $formFields If TRUE, the search box is wrapped in its own form-tags
479 * @return string HTML for the search box
480 */
481 public function getSearchBox($formFields = 1) {
482 // Setting form-elements, if applicable:
483 $formElements = array('', '');
484 if ($formFields) {
485 $formElements = array('<form action="' . htmlspecialchars($this->listURL('', -1, 'firstElementNumber')) . '" method="post">', '</form>');
486 }
487 // Make level selector:
488 $opt = array();
489 $parts = explode('|', $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.enterSearchLevels'));
490 foreach ($parts as $kv => $label) {
491 $opt[] = '<option value="' . $kv . '"' . ($kv == (int)$this->searchLevels ? ' selected="selected"' : '') . '>' . htmlspecialchars($label) . '</option>';
492 }
493 $lMenu = '<select name="search_levels" title="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.title.search_levels', TRUE) . '" id="search_levels">' . implode('', $opt) . '</select>';
494 // Table with the search box:
495 $content = '<div class="db_list-searchbox-form db_list-searchbox-toolbar" id="db_list-searchbox-toolbar" style="display: ' . ($this->searchString == '' ? 'none' : 'block') . ';">
496 ' . $formElements[0] . '
497 <div id="typo3-dblist-search">
498 <label for="search_field">' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.label.searchString', TRUE) . ': </label>
499 <input type="search" placeholder="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.enterSearchString', TRUE) . '" title="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.title.searchString', TRUE) . '" name="search_field" id="search_field" value="' . htmlspecialchars($this->searchString) . '"' . $GLOBALS['TBE_TEMPLATE']->formWidth(15) . ' />
500 <label for="search_levels">' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.label.search_levels', TRUE) . ': </label>
501 ' . $lMenu . '
502 <label for="showLimit">' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.label.limit', TRUE) . ': </label>
503 <input type="number" placeholder="10" title="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.title.limit', TRUE) . '" name="showLimit" id="showLimit" value="' . htmlspecialchars(($this->showLimit ? $this->showLimit : '')) . '"' . $GLOBALS['TBE_TEMPLATE']->formWidth(5) . ' />
504 <input type="submit" name="search" value="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.search', TRUE) . '" title="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.title.search', TRUE) . '" />
505 ' . $formElements[1] . '</div></div>';
506 return $content;
507 }
508
509 /******************************
510 *
511 * Various helper functions
512 *
513 ******************************/
514 /**
515 * Setting the field names to display in extended list.
516 * Sets the internal variable $this->setFields
517 *
518 * @return void
519 */
520 public function setDispFields() {
521 // Getting from session:
522 $dispFields = $GLOBALS['BE_USER']->getModuleData('list/displayFields');
523 // If fields has been inputted, then set those as the value and push it to session variable:
524 if (is_array($this->displayFields)) {
525 reset($this->displayFields);
526 $tKey = key($this->displayFields);
527 $dispFields[$tKey] = $this->displayFields[$tKey];
528 $GLOBALS['BE_USER']->pushModuleData('list/displayFields', $dispFields);
529 }
530 // Setting result:
531 $this->setFields = $dispFields;
532 }
533
534 /**
535 * Create thumbnail code for record/field
536 *
537 * @param array $row Record array
538 * @param string $table Table (record is from)
539 * @param string $field Field name for which thumbnail are to be rendered.
540 * @return string HTML for thumbnails, if any.
541 */
542 public function thumbCode($row, $table, $field) {
543 return BackendUtility::thumbCode($row, $table, $field, $this->backPath);
544 }
545
546 /**
547 * Returns the SQL-query array to select the records from a table $table with pid = $id
548 *
549 * @param string $table Table name
550 * @param int $id Page id (NOT USED! $this->pidSelect is used instead)
551 * @param string $addWhere Additional part for where clause
552 * @param string $fieldList Field list to select, * for all (for "SELECT [fieldlist] FROM ...")
553 * @return array Returns query array
554 */
555 public function makeQueryArray($table, $id, $addWhere = '', $fieldList = '*') {
556 $hookObjectsArr = array();
557 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/class.db_list.inc']['makeQueryArray'])) {
558 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['typo3/class.db_list.inc']['makeQueryArray'] as $classRef) {
559 $hookObjectsArr[] = GeneralUtility::getUserObj($classRef);
560 }
561 }
562 // Set ORDER BY:
563 $orderBy = $GLOBALS['TCA'][$table]['ctrl']['sortby'] ? 'ORDER BY ' . $GLOBALS['TCA'][$table]['ctrl']['sortby'] : $GLOBALS['TCA'][$table]['ctrl']['default_sortby'];
564 if ($this->sortField) {
565 if (in_array($this->sortField, $this->makeFieldList($table, 1))) {
566 $orderBy = 'ORDER BY ' . $this->sortField;
567 if ($this->sortRev) {
568 $orderBy .= ' DESC';
569 }
570 }
571 }
572 // Set LIMIT:
573 $limit = $this->iLimit ? ($this->firstElementNumber ? $this->firstElementNumber . ',' : '') . ($this->iLimit + 1) : '';
574 // Filtering on displayable pages (permissions):
575 $pC = $table == 'pages' && $this->perms_clause ? ' AND ' . $this->perms_clause : '';
576 // Adding search constraints:
577 $search = $this->makeSearchString($table, $id);
578 // Compiling query array:
579 $queryParts = array(
580 'SELECT' => $fieldList,
581 'FROM' => $table,
582 'WHERE' => $this->pidSelect . ' ' . $pC . BackendUtility::deleteClause($table) . BackendUtility::versioningPlaceholderClause($table) . ' ' . $addWhere . ' ' . $search,
583 'GROUPBY' => '',
584 'ORDERBY' => $GLOBALS['TYPO3_DB']->stripOrderBy($orderBy),
585 'LIMIT' => $limit
586 );
587 // Filter out records that are translated, if TSconfig mod.web_list.hideTranslations is set
588 if ((in_array($table, GeneralUtility::trimExplode(',', $this->hideTranslations)) || $this->hideTranslations === '*') && !empty($GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']) && $table !== 'pages_language_overlay') {
589 $queryParts['WHERE'] .= ' AND ' . $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'] . '=0 ';
590 }
591 // Apply hook as requested in http://forge.typo3.org/issues/16634
592 foreach ($hookObjectsArr as $hookObj) {
593 if (method_exists($hookObj, 'makeQueryArray_post')) {
594 $_params = array(
595 'orderBy' => $orderBy,
596 'limit' => $limit,
597 'pC' => $pC,
598 'search' => $search
599 );
600 $hookObj->makeQueryArray_post($queryParts, $this, $table, $id, $addWhere, $fieldList, $_params);
601 }
602 }
603 // Return query:
604 return $queryParts;
605 }
606
607 /**
608 * Based on input query array (query for selecting count(*) from a table) it will select the number of records and set the value in $this->totalItems
609 *
610 * @param array $queryParts Query array
611 * @return void
612 * @see makeQueryArray()
613 */
614 public function setTotalItems($queryParts) {
615 $this->totalItems = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', $queryParts['FROM'], $queryParts['WHERE']);
616 }
617
618 /**
619 * Creates part of query for searching after a word ($this->searchString)
620 * fields in input table.
621 *
622 * @param string $table Table, in which the fields are being searched.
623 * @param int $currentPid Page id for the possible search limit. -1 only if called from an old XCLASS.
624 * @return string Returns part of WHERE-clause for searching, if applicable.
625 */
626 public function makeSearchString($table, $currentPid = -1) {
627 $result = '';
628 $currentPid = (int)$currentPid;
629 $tablePidField = $table == 'pages' ? 'uid' : 'pid';
630 // Make query, only if table is valid and a search string is actually defined:
631 if ($this->searchString) {
632 $result = ' AND 0=1';
633 $searchableFields = $this->getSearchFields($table);
634 if (count($searchableFields) > 0) {
635 if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->searchString)) {
636 $whereParts = array(
637 'uid=' . $this->searchString
638 );
639 foreach ($searchableFields as $fieldName) {
640 if (isset($GLOBALS['TCA'][$table]['columns'][$fieldName])) {
641 $fieldConfig = &$GLOBALS['TCA'][$table]['columns'][$fieldName]['config'];
642 $condition = $fieldName . '=' . $this->searchString;
643 if ($fieldConfig['type'] == 'input' && $fieldConfig['eval'] && GeneralUtility::inList($fieldConfig['eval'], 'int')) {
644 if (is_array($fieldConfig['search']) && in_array('pidonly', $fieldConfig['search']) && $currentPid > 0) {
645 $condition = '(' . $condition . ' AND ' . $tablePidField . '=' . $currentPid . ')';
646 }
647 $whereParts[] = $condition;
648 } elseif (
649 $fieldConfig['type'] == 'text' ||
650 $fieldConfig['type'] == 'flex' ||
651 ($fieldConfig['type'] == 'input' && (!$fieldConfig['eval'] || !preg_match('/date|time|int/', $fieldConfig['eval'])))) {
652 $condition = $fieldName . ' LIKE \'%' . $this->searchString . '%\'';
653 $whereParts[] = $condition;
654 }
655 }
656 }
657 } else {
658 $whereParts = array();
659 $like = '\'%' . $GLOBALS['TYPO3_DB']->quoteStr($GLOBALS['TYPO3_DB']->escapeStrForLike($this->searchString, $table), $table) . '%\'';
660 foreach ($searchableFields as $fieldName) {
661 if (isset($GLOBALS['TCA'][$table]['columns'][$fieldName])) {
662 $fieldConfig = &$GLOBALS['TCA'][$table]['columns'][$fieldName]['config'];
663 $format = 'LCASE(%s) LIKE LCASE(%s)';
664 if (is_array($fieldConfig['search'])) {
665 if (in_array('case', $fieldConfig['search'])) {
666 $format = '%s LIKE %s';
667 }
668 if (in_array('pidonly', $fieldConfig['search']) && $currentPid > 0) {
669 $format = '(' . $format . ' AND ' . $tablePidField . '=' . $currentPid . ')';
670 }
671 if ($fieldConfig['search']['andWhere']) {
672 $format = '((' . $fieldConfig['search']['andWhere'] . ') AND (' . $format . '))';
673 }
674 }
675 if ($fieldConfig['type'] == 'text' || $fieldConfig['type'] == 'flex' || $fieldConfig['type'] == 'input' && (!$fieldConfig['eval'] || !preg_match('/date|time|int/', $fieldConfig['eval']))) {
676 $whereParts[] = sprintf($format, $fieldName, $like);
677 }
678 }
679 }
680 }
681 // If search-fields were defined (and there always are) we create the query:
682 if (count($whereParts)) {
683 $result = ' AND (' . implode(' OR ', $whereParts) . ')';
684 }
685 }
686 }
687 return $result;
688 }
689
690 /**
691 * Fetches a list of fields to use in the Backend search for the given table.
692 *
693 * @param string $tableName
694 * @return array
695 */
696 protected function getSearchFields($tableName) {
697 $fieldArray = array();
698 $fieldListWasSet = FALSE;
699 // Get fields from ctrl section of TCA first
700 if (isset($GLOBALS['TCA'][$tableName]['ctrl']['searchFields'])) {
701 $fieldArray = GeneralUtility::trimExplode(',', $GLOBALS['TCA'][$tableName]['ctrl']['searchFields'], TRUE);
702 $fieldListWasSet = TRUE;
703 }
704 // Call hook to add or change the list
705 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['mod_list']['getSearchFieldList'])) {
706 $hookParameters = array(
707 'tableHasSearchConfiguration' => $fieldListWasSet,
708 'tableName' => $tableName,
709 'searchFields' => &$fieldArray,
710 'searchString' => $this->searchString
711 );
712 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['mod_list']['getSearchFieldList'] as $hookFunction) {
713 GeneralUtility::callUserFunction($hookFunction, $hookParameters, $this);
714 }
715 }
716 return $fieldArray;
717 }
718
719 /**
720 * Returns the title (based on $code) of a table ($table) with the proper link around. For headers over tables.
721 * The link will cause the display of all extended mode or not for the table.
722 *
723 * @param string $table Table name
724 * @param string $code Table label
725 * @return string The linked table label
726 */
727 public function linkWrapTable($table, $code) {
728 if ($this->table != $table) {
729 return '<a href="' . htmlspecialchars($this->listURL('', $table, 'firstElementNumber')) . '">' . $code . '</a>';
730 } else {
731 return '<a href="' . htmlspecialchars($this->listURL('', '', 'sortField,sortRev,table,firstElementNumber')) . '">' . $code . '</a>';
732 }
733 }
734
735 /**
736 * Returns the title (based on $code) of a record (from table $table) with the proper link around (that is for 'pages'-records a link to the level of that record...)
737 *
738 * @param string $table Table name
739 * @param int $uid Item uid
740 * @param string $code Item title (not htmlspecialchars()'ed yet)
741 * @param array $row Item row
742 * @return string The item title. Ready for HTML output (is htmlspecialchars()'ed)
743 */
744 public function linkWrapItems($table, $uid, $code, $row) {
745 $origCode = $code;
746 // If the title is blank, make a "no title" label:
747 if ((string)$code === '') {
748 $code = '<i>[' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.no_title', 1) . ']</i> - ' . htmlspecialchars(GeneralUtility::fixed_lgd_cs(
749 BackendUtility::getRecordTitle($table, $row), $GLOBALS['BE_USER']->uc['titleLen']));
750 } else {
751 $code = htmlspecialchars(GeneralUtility::fixed_lgd_cs($code, $this->fixedL), ENT_QUOTES, 'UTF-8', FALSE);
752 if ($code != htmlspecialchars($origCode)) {
753 $code = '<span title="' . htmlspecialchars($origCode, ENT_QUOTES, 'UTF-8', FALSE) . '">' . $code . '</span>';
754 }
755 }
756 switch ((string)$this->clickTitleMode) {
757 case 'edit':
758 // If the listed table is 'pages' we have to request the permission settings for each page:
759 if ($table == 'pages') {
760 $localCalcPerms = $GLOBALS['BE_USER']->calcPerms(BackendUtility::getRecord('pages', $row['uid']));
761 $permsEdit = $localCalcPerms & 2;
762 } else {
763 $permsEdit = $this->calcPerms & 16;
764 }
765 // "Edit" link: ( Only if permissions to edit the page-record of the content of the parent page ($this->id)
766 if ($permsEdit) {
767 $params = '&edit[' . $table . '][' . $row['uid'] . ']=edit';
768 $code = '<a href="#" onclick="' . htmlspecialchars(
769 BackendUtility::editOnClick($params, $this->backPath, -1)) . '" title="' . $GLOBALS['LANG']->getLL('edit', TRUE) . '">' . $code . '</a>';
770 }
771 break;
772 case 'show':
773 // "Show" link (only pages and tt_content elements)
774 if ($table == 'pages' || $table == 'tt_content') {
775 $code = '<a href="#" onclick="' . htmlspecialchars(
776 BackendUtility::viewOnClick(($table == 'tt_content' ? $this->id . '#' . $row['uid'] : $row['uid']))) . '" title="' . $GLOBALS['LANG']->sL('LLL:EXT:lang/locallang_core.xlf:labels.showPage', TRUE) . '">' . $code . '</a>';
777 }
778 break;
779 case 'info':
780 // "Info": (All records)
781 $code = '<a href="#" onclick="' . htmlspecialchars(('top.launchView(\'' . $table . '\', \'' . $row['uid'] . '\'); return false;')) . '" title="' . $GLOBALS['LANG']->getLL('showInfo', TRUE) . '">' . $code . '</a>';
782 break;
783 default:
784 // Output the label now:
785 if ($table == 'pages') {
786 $code = '<a href="' . htmlspecialchars($this->listURL($uid, '', 'firstElementNumber')) . '" onclick="setHighlight(' . $uid . ')">' . $code . '</a>';
787 } else {
788 $code = $this->linkUrlMail($code, $origCode);
789 }
790 }
791 return $code;
792 }
793
794 /**
795 * Wrapping input code in link to URL or email if $testString is either.
796 *
797 * @param string $code code to wrap
798 * @param string $testString String which is tested for being a URL or email and which will be used for the link if so.
799 * @return string Link-Wrapped $code value, if $testString was URL or email.
800 */
801 public function linkUrlMail($code, $testString) {
802 // Check for URL:
803 $schema = parse_url($testString);
804 if ($schema['scheme'] && GeneralUtility::inList('http,https,ftp', $schema['scheme'])) {
805 return '<a href="' . htmlspecialchars($testString) . '" target="_blank">' . $code . '</a>';
806 }
807 // Check for email:
808 if (GeneralUtility::validEmail($testString)) {
809 return '<a href="mailto:' . htmlspecialchars($testString) . '" target="_blank">' . $code . '</a>';
810 }
811 // Return if nothing else...
812 return $code;
813 }
814
815 /**
816 * Creates the URL to this script, including all relevant GPvars
817 * Fixed GPvars are id, table, imagemode, returnUrl, search_field, search_levels and showLimit
818 * The GPvars "sortField" and "sortRev" are also included UNLESS they are found in the $exclList variable.
819 *
820 * @param string $altId Alternative id value. Enter blank string for the current id ($this->id)
821 * @param string $table Tablename to display. Enter "-1" for the current table.
822 * @param string $exclList Commalist of fields NOT to include ("sortField", "sortRev" or "firstElementNumber")
823 * @return string URL
824 */
825 public function listURL($altId = '', $table = -1, $exclList = '') {
826 $urlParameters = array();
827 if ((string)$altId !== '') {
828 $urlParameters['id'] = $altId;
829 } else {
830 $urlParameters['id'] = $this->id;
831 }
832 if ($table === -1) {
833 $urlParameters['table'] = $this->table;
834 } else {
835 $urlParameters['table'] = $table;
836 }
837 if ($this->thumbs) {
838 $urlParameters['imagemode'] = $this->thumbs;
839 }
840 if ($this->returnUrl) {
841 $urlParameters['returnUrl'] = $this->returnUrl;
842 }
843 if ($this->searchString) {
844 $urlParameters['search_field'] = $this->searchString;
845 }
846 if ($this->searchLevels) {
847 $urlParameters['search_levels'] = $this->searchLevels;
848 }
849 if ($this->showLimit) {
850 $urlParameters['showLimit'] = $this->showLimit;
851 }
852 if ((!$exclList || !GeneralUtility::inList($exclList, 'firstElementNumber')) && $this->firstElementNumber) {
853 $urlParameters['pointer'] = $this->firstElementNumber;
854 }
855 if ((!$exclList || !GeneralUtility::inList($exclList, 'sortField')) && $this->sortField) {
856 $urlParameters['sortField'] = $this->sortField;
857 }
858 if ((!$exclList || !GeneralUtility::inList($exclList, 'sortRev')) && $this->sortRev) {
859 $urlParameters['sortRev'] = $this->sortRev;
860 }
861
862 $urlParameters = array_merge_recursive($urlParameters, $this->overrideUrlParameters);
863
864 return BackendUtility::getModuleUrl(GeneralUtility::_GP('M'), $urlParameters);
865 }
866
867 /**
868 * Returns "requestUri" - which is basically listURL
869 *
870 * @return string Content of ->listURL()
871 */
872 public function requestUri() {
873 return $this->listURL();
874 }
875
876 /**
877 * Makes the list of fields to select for a table
878 *
879 * @param string $table Table name
880 * @param bool $dontCheckUser If set, users access to the field (non-exclude-fields) is NOT checked.
881 * @param bool $addDateFields If set, also adds crdate and tstamp fields (note: they will also be added if user is admin or dontCheckUser is set)
882 * @return array Array, where values are fieldnames to include in query
883 */
884 public function makeFieldList($table, $dontCheckUser = 0, $addDateFields = 0) {
885 // Init fieldlist array:
886 $fieldListArr = array();
887 // Check table:
888 if (is_array($GLOBALS['TCA'][$table]) && isset($GLOBALS['TCA'][$table]['columns']) && is_array($GLOBALS['TCA'][$table]['columns'])) {
889 if (isset($GLOBALS['TCA'][$table]['columns']) && is_array($GLOBALS['TCA'][$table]['columns'])) {
890 // Traverse configured columns and add them to field array, if available for user.
891 foreach ($GLOBALS['TCA'][$table]['columns'] as $fN => $fieldValue) {
892 if ($dontCheckUser || (!$fieldValue['exclude'] || $GLOBALS['BE_USER']->check('non_exclude_fields', $table . ':' . $fN)) && $fieldValue['config']['type'] != 'passthrough') {
893 $fieldListArr[] = $fN;
894 }
895 }
896
897 $fieldListArr[] = 'uid';
898 $fieldListArr[] = 'pid';
899
900 // Add date fields
901 if ($dontCheckUser || $GLOBALS['BE_USER']->isAdmin() || $addDateFields) {
902 if ($GLOBALS['TCA'][$table]['ctrl']['tstamp']) {
903 $fieldListArr[] = $GLOBALS['TCA'][$table]['ctrl']['tstamp'];
904 }
905 if ($GLOBALS['TCA'][$table]['ctrl']['crdate']) {
906 $fieldListArr[] = $GLOBALS['TCA'][$table]['ctrl']['crdate'];
907 }
908 }
909 // Add more special fields:
910 if ($dontCheckUser || $GLOBALS['BE_USER']->isAdmin()) {
911 if ($GLOBALS['TCA'][$table]['ctrl']['cruser_id']) {
912 $fieldListArr[] = $GLOBALS['TCA'][$table]['ctrl']['cruser_id'];
913 }
914 if ($GLOBALS['TCA'][$table]['ctrl']['sortby']) {
915 $fieldListArr[] = $GLOBALS['TCA'][$table]['ctrl']['sortby'];
916 }
917 if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('version') && $GLOBALS['TCA'][$table]['ctrl']['versioningWS']) {
918 $fieldListArr[] = 't3ver_id';
919 $fieldListArr[] = 't3ver_state';
920 $fieldListArr[] = 't3ver_wsid';
921 }
922 }
923 } else {
924 GeneralUtility::sysLog(sprintf('$TCA is broken for the table "%s": no required "columns" entry in $TCA.', $table), 'core', GeneralUtility::SYSLOG_SEVERITY_ERROR);
925 }
926 }
927 return $fieldListArr;
928 }
929
930 /**
931 * Get all allowed mount pages to be searched in.
932 *
933 * @param int $id Page id
934 * @param int $depth Depth to go down
935 * @param string $perms_clause select clause
936 * @return array
937 */
938 protected function getSearchableWebmounts($id, $depth, $perms_clause) {
939 /** @var \TYPO3\CMS\Backend\Tree\View\PageTreeView $tree */
940 $tree = GeneralUtility::makeInstance('TYPO3\\CMS\\Backend\\Tree\\View\\PageTreeView');
941 $tree->init('AND ' . $perms_clause);
942 $tree->makeHTML = 0;
943 $tree->fieldArray = array('uid', 'php_tree_stop');
944 $idList = array();
945
946 $allowedMounts = !$GLOBALS['BE_USER']->isAdmin() && $id === 0
947 ? $GLOBALS['BE_USER']->returnWebmounts()
948 : array($id);
949
950 foreach ($allowedMounts as $allowedMount) {
951 $idList[] = $allowedMount;
952 if ($depth) {
953 $tree->getTree($allowedMount, $depth, '');
954 }
955 $idList = array_merge($idList, $tree->ids);
956 }
957
958 return $idList;
959 }
960
961 /**
962 * Redirects to TCEforms (alt_doc) if a record is just localized.
963 *
964 * @param string $justLocalized String with table, orig uid and language separated by ":
965 * @return void
966 */
967 public function localizationRedirect($justLocalized) {
968 list($table, $orig_uid, $language) = explode(':', $justLocalized);
969 if ($GLOBALS['TCA'][$table] && $GLOBALS['TCA'][$table]['ctrl']['languageField'] && $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']) {
970 $localizedRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('uid', $table, $GLOBALS['TCA'][$table]['ctrl']['languageField'] . '=' . (int)$language . ' AND ' . $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'] . '=' . (int)$orig_uid . BackendUtility::deleteClause($table) . BackendUtility::versioningPlaceholderClause($table));
971 if (is_array($localizedRecord)) {
972 // Create parameters and finally run the classic page module for creating a new page translation
973 $url = substr($this->listURL(), strlen($this->backPath));
974 $params = '&edit[' . $table . '][' . $localizedRecord['uid'] . ']=edit';
975 $returnUrl = '&returnUrl=' . rawurlencode($url);
976 $location = $GLOBALS['BACK_PATH'] . 'alt_doc.php?' . $params . $returnUrl;
977 \TYPO3\CMS\Core\Utility\HttpUtility::redirect($location);
978 }
979 }
980 }
981
982 /**
983 * Set URL parameters to override or add in the listUrl() method.
984 *
985 * @param array $urlParameters
986 * @return void
987 */
988 public function setOverrideUrlParameters(array $urlParameters) {
989 $this->overrideUrlParameters = $urlParameters;
990 }
991
992 }