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