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