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