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