Fixed bug #15589: Move debug functions to own utility class
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_fullsearch.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2010 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 * Class used in module tools/dbint (advanced search) and which may hold code specific for that module
29 * However the class has a general principle in it which may be used in the web/export module.
30 *
31 * $Id$
32 *
33 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
34 * @coauthor Jo Hasenau <info@cybercraft.de>
35 */
36 /**
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 88: class t3lib_fullsearch
42 * 103: function form()
43 * 117: function makeStoreControl()
44 * 156: function initStoreArray()
45 * 176: function cleanStoreQueryConfigs($storeQueryConfigs,$storeArray)
46 * 193: function addToStoreQueryConfigs($storeQueryConfigs,$index)
47 * 209: function saveQueryInAction($uid)
48 * 256: function loadStoreQueryConfigs($storeQueryConfigs,$storeIndex,$writeArray)
49 * 272: function procesStoreControl()
50 * 344: function queryMaker()
51 * 414: function getQueryResultCode($mQ,$res,$table)
52 * 534: function csvValues($row, $delim=',', $quote='"', $conf=array(), $table='')
53 * 550: function tableWrap($str)
54 * 559: function search()
55 * 614: function resultRowDisplay($row,$conf,$table)
56 * 662: function getProcessedValueExtra($table, $fN, $fV, $conf, $splitString)
57 * 781: function getTreeList($id, $depth, $begin = 0, $perms_clause)
58 * 818: function makeValueList($fN, $fV, $conf, $table, $splitString)
59 * 1028: function resultRowTitles($row,$conf,$table)
60 * 1058: function csvRowTitles($row, $conf, $table)
61 *
62 * TOTAL FUNCTIONS: 19
63 * (This index is automatically created/updated by the extension "extdeveval")
64 *
65 */
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 /**
81 * Class used in module tools/dbint (advanced search) and which may hold code specific for that module
82 * However the class has a general principle in it which may be used in the web/export module.
83 *
84 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
85 * @package TYPO3
86 * @subpackage t3lib
87 */
88 class t3lib_fullsearch {
89 var $storeList = 'search_query_smallparts,search_result_labels,labels_noprefix,show_deleted,queryConfig,queryTable,queryFields,queryLimit,queryOrder,queryOrderDesc,queryOrder2,queryOrder2Desc,queryGroup,search_query_makeQuery';
90 var $downloadScript = 'index.php';
91 var $formW=48;
92 var $noDownloadB=0;
93
94 protected $formName = '';
95
96 /**
97 * constructor
98 */
99 public function __construct() {
100 $GLOBALS['LANG']->includeLLFile('EXT:lang/locallang_t3lib_fullsearch.xml');
101 }
102
103
104 /**
105 * [Describe function...]
106 *
107 * @return [type] ...
108 */
109 function form() {
110 $out='
111 Search Word:<BR>
112 <input type="text" name="SET[sword]" value="'.htmlspecialchars($GLOBALS['SOBE']->MOD_SETTINGS['sword']).'"'.$GLOBALS['TBE_TEMPLATE']->formWidth(20).'><input type="submit" name="submit" value="Search All Records">
113 ';
114
115 return $out;
116 }
117
118 /**
119 * [Describe function...]
120 *
121 * @return [type] ...
122 */
123 function makeStoreControl() {
124 // Load/Save
125 $storeArray = $this->initStoreArray();
126 $cur='';
127
128 // Store Array:
129 $opt=array();
130 foreach ($storeArray as $k => $v) {
131 $opt[]='<option value="'.$k.'"'.(!strcmp($cur,$v)?' selected':'').'>'.htmlspecialchars($v).'</option>';
132 }
133
134 // Actions:
135 if (t3lib_extMgm::isLoaded('sys_action') && $GLOBALS['BE_USER']->isAdmin()) {
136 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'sys_action', 'type=2', '', 'title');
137 if ($GLOBALS['TYPO3_DB']->sql_num_rows($res)) {
138 $opt[]='<option value="0">__Save to Action:__</option>';
139 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
140 $opt[]='<option value="-'.$row['uid'].'"'.(!strcmp($cur,'-'.$row['uid'])?' selected':'').'>'.htmlspecialchars($row['title'].' ['.$row['uid'].']').'</option>';
141 }
142 }
143 $GLOBALS['TYPO3_DB']->sql_free_result($res);
144 }
145
146 $TDparams=' nowrap="nowrap" class="bgColor4"';
147 $tmpCode='
148 <table border="0" cellpadding="3" cellspacing="1">
149 <tr'.$TDparams.'><td><select name="storeControl[STORE]" onChange="document.forms[0][\'storeControl[title]\'].value= this.options[this.selectedIndex].value!=0 ? this.options[this.selectedIndex].text : \'\';">'.implode(LF,$opt).'</select><input type="submit" name="storeControl[LOAD]" value="Load"></td></tr>
150 <tr'.$TDparams.'><td nowrap><input name="storeControl[title]" value="" type="text" max="80"'.$GLOBALS['SOBE']->doc->formWidth().'><input type="submit" name="storeControl[SAVE]" value="Save" onClick="if (document.forms[0][\'storeControl[STORE]\'].options[document.forms[0][\'storeControl[STORE]\'].selectedIndex].value<0) return confirm(\'Are you sure you want to overwrite the existing query in this action?\');"><input type="submit" name="storeControl[REMOVE]" value="Remove"></td></tr>
151 </table>
152 ';
153 return $tmpCode;
154 }
155
156 /**
157 * [Describe function...]
158 *
159 * @return [type] ...
160 */
161 function initStoreArray() {
162 $storeArray=array(
163 '0' => '[New]'
164 );
165
166 $savedStoreArray = unserialize($GLOBALS['SOBE']->MOD_SETTINGS['storeArray']);
167
168 if (is_array($savedStoreArray)) {
169 $storeArray = array_merge($storeArray,$savedStoreArray);
170 }
171 return $storeArray;
172 }
173
174 /**
175 * [Describe function...]
176 *
177 * @param [type] $storeQueryConfigs: ...
178 * @param [type] $storeArray: ...
179 * @return [type] ...
180 */
181 function cleanStoreQueryConfigs($storeQueryConfigs,$storeArray) {
182 if (is_array($storeQueryConfigs)) {
183 foreach ($storeQueryConfigs as $k => $v) {
184 if (!isset($storeArray[$k])) unset($storeQueryConfigs[$k]);
185 }
186 }
187 return $storeQueryConfigs;
188 }
189
190 /**
191 * [Describe function...]
192 *
193 * @param [type] $storeQueryConfigs: ...
194 * @param [type] $index: ...
195 * @return [type] ...
196 */
197 function addToStoreQueryConfigs($storeQueryConfigs,$index) {
198 $keyArr = explode(',',$this->storeList);
199 $storeQueryConfigs[$index]=array();
200 foreach ($keyArr as $k) {
201 $storeQueryConfigs[$index][$k]=$GLOBALS['SOBE']->MOD_SETTINGS[$k];
202 }
203 return $storeQueryConfigs;
204 }
205
206 /**
207 * [Describe function...]
208 *
209 * @param [type] $uid: ...
210 * @return [type] ...
211 */
212 function saveQueryInAction($uid) {
213 if (t3lib_extMgm::isLoaded('sys_action')) {
214 $keyArr = explode(',',$this->storeList);
215 $saveArr=array();
216 foreach ($keyArr as $k) {
217 $saveArr[$k]=$GLOBALS['SOBE']->MOD_SETTINGS[$k];
218 }
219
220 $qOK = 0;
221 // Show query
222 if ($saveArr['queryTable']) {
223 /* @var t3lib_queryGenerator */
224 $qGen = t3lib_div::makeInstance('t3lib_queryGenerator');
225 $qGen->init('queryConfig',$saveArr['queryTable']);
226 $qGen->makeSelectorTable($saveArr);
227
228 $qGen->enablePrefix=1;
229 $qString = $qGen->getQuery($qGen->queryConfig);
230 $qCount = $GLOBALS['TYPO3_DB']->SELECTquery('count(*)', $qGen->table, $qString.t3lib_BEfunc::deleteClause($qGen->table));
231 $qSelect = $qGen->getSelectQuery($qString);
232
233 $res = @$GLOBALS['TYPO3_DB']->sql_query($qCount);
234 if (!$GLOBALS['TYPO3_DB']->sql_error()) {
235 $GLOBALS['TYPO3_DB']->sql_free_result($res);
236 $dA = array();
237 $dA['t2_data'] = serialize(array(
238 'qC'=>$saveArr,
239 'qCount' => $qCount,
240 'qSelect' => $qSelect,
241 'qString' => $qString
242 ));
243 $GLOBALS['TYPO3_DB']->exec_UPDATEquery('sys_action', 'uid='.intval($uid), $dA);
244 $qOK=1;
245 }
246 }
247
248 return $qOK;
249 }
250 }
251
252 /**
253 * [Describe function...]
254 *
255 * @param [type] $storeQueryConfigs: ...
256 * @param [type] $storeIndex: ...
257 * @param [type] $writeArray: ...
258 * @return [type] ...
259 */
260 function loadStoreQueryConfigs($storeQueryConfigs,$storeIndex,$writeArray) {
261 if ($storeQueryConfigs[$storeIndex]) {
262 $keyArr = explode(',',$this->storeList);
263 foreach ($keyArr as $k) {
264 $writeArray[$k]=$storeQueryConfigs[$storeIndex][$k];
265 }
266 }
267 return $writeArray;
268 }
269
270 /**
271 * [Describe function...]
272 *
273 * @return [type] ...
274 */
275 function procesStoreControl() {
276 $storeArray = $this->initStoreArray();
277 $storeQueryConfigs = unserialize($GLOBALS['SOBE']->MOD_SETTINGS['storeQueryConfigs']);
278
279 $storeControl = t3lib_div::_GP('storeControl');
280 $storeIndex = intval($storeControl['STORE']);
281 $saveStoreArray=0;
282 $writeArray=array();
283 if (is_array($storeControl)) {
284 $msg = '';
285 if ($storeControl['LOAD']) {
286 if ($storeIndex>0) {
287 $writeArray=$this->loadStoreQueryConfigs($storeQueryConfigs,$storeIndex,$writeArray);
288 $saveStoreArray = 1;
289 $flashMessage = t3lib_div::makeInstance(
290 't3lib_FlashMessage',
291 sprintf($GLOBALS['LANG']->getLL('query_loaded'), htmlspecialchars($storeArray[$storeIndex]))
292 );
293 } elseif ($storeIndex<0 && t3lib_extMgm::isLoaded('sys_action')) {
294 $actionRecord=t3lib_BEfunc::getRecord('sys_action',abs($storeIndex));
295 if (is_array($actionRecord)) {
296 $dA = unserialize($actionRecord['t2_data']);
297 $dbSC=array();
298 if (is_array($dA['qC'])) {
299 $dbSC[0] = $dA['qC'];
300 }
301 $writeArray=$this->loadStoreQueryConfigs($dbSC,'0',$writeArray);
302 $saveStoreArray=1;
303
304 $flashMessage = t3lib_div::makeInstance(
305 't3lib_FlashMessage',
306 sprintf($GLOBALS['LANG']->getLL('query_from_action_loaded'), htmlspecialchars($actionRecord['title']))
307 );
308 }
309 }
310 } elseif ($storeControl['SAVE']) {
311 if ($storeIndex<0) {
312 $qOK = $this->saveQueryInAction(abs($storeIndex));
313 if ($qOK) {
314 $flashMessage = t3lib_div::makeInstance(
315 't3lib_FlashMessage',
316 $GLOBALS['LANG']->getLL('query_saved')
317 );
318 } else {
319 $flashMessage = t3lib_div::makeInstance(
320 't3lib_FlashMessage',
321 $GLOBALS['LANG']->getLL('query_notsaved'),
322 '',
323 t3lib_FlashMessage::ERROR
324 );
325 }
326 } else {
327 if (trim($storeControl['title'])) {
328 if ($storeIndex>0) {
329 $storeArray[$storeIndex]=$storeControl['title'];
330 } else {
331 $storeArray[]=$storeControl['title'];
332 end($storeArray);
333 $storeIndex=key($storeArray);
334 }
335 $storeQueryConfigs=$this->addToStoreQueryConfigs($storeQueryConfigs,$storeIndex);
336 $saveStoreArray=1;
337 $flashMessage = t3lib_div::makeInstance(
338 't3lib_FlashMessage',
339 $GLOBALS['LANG']->getLL('query_saved')
340 );
341 }
342 }
343 } elseif ($storeControl['REMOVE']) {
344 if ($storeIndex>0) {
345 $flashMessage = t3lib_div::makeInstance(
346 't3lib_FlashMessage',
347 sprintf($GLOBALS['LANG']->getLL('query_removed'), htmlspecialchars($storeArray[$storeControl['STORE']]))
348 );
349 unset($storeArray[$storeControl['STORE']]); // Removing
350 $saveStoreArray=1;
351 }
352 }
353 if ($flashMessage) {
354 $msg = $flashMessage->render();
355 }
356 }
357 if ($saveStoreArray) {
358 unset($storeArray[0]); // making sure, index 0 is not set!
359 $writeArray['storeArray']=serialize($storeArray);
360 $writeArray['storeQueryConfigs']=serialize($this->cleanStoreQueryConfigs($storeQueryConfigs,$storeArray));
361 $GLOBALS['SOBE']->MOD_SETTINGS = t3lib_BEfunc::getModuleData($GLOBALS['SOBE']->MOD_MENU, $writeArray, $GLOBALS['SOBE']->MCONF['name'], 'ses');
362 }
363 return $msg;
364 }
365
366 /**
367 * [Describe function...]
368 *
369 * @return [type] ...
370 */
371 function queryMaker() {
372 global $TCA;
373
374 if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3lib_fullsearch'])) {
375 $this->hookArray = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3lib_fullsearch'];
376 }
377 $msg=$this->procesStoreControl();
378
379 if (!$GLOBALS['BE_USER']->userTS['mod.']['dbint.']['disableStoreControl']) {
380 $output.= $GLOBALS['SOBE']->doc->section('Load/Save Query',$this->makeStoreControl(),0,1);
381 if ($msg) {
382 $output .= '<br />' . $msg;
383 }
384 $output.= $GLOBALS['SOBE']->doc->spacer(20);
385 }
386
387
388 // Query Maker:
389 $qGen = t3lib_div::makeInstance('t3lib_queryGenerator');
390 $qGen->init('queryConfig',$GLOBALS['SOBE']->MOD_SETTINGS['queryTable']);
391 if ($this->formName) {
392 $qGen->setFormName($this->formName);
393 }
394 $tmpCode=$qGen->makeSelectorTable($GLOBALS['SOBE']->MOD_SETTINGS);
395 $output.= $GLOBALS['SOBE']->doc->section('Make query',$tmpCode,0,1);
396
397 $mQ = $GLOBALS['SOBE']->MOD_SETTINGS['search_query_makeQuery'];
398
399 // Make form elements:
400 if ($qGen->table && is_array($TCA[$qGen->table])) {
401 if ($mQ) {
402 // Show query
403 $qGen->enablePrefix=1;
404 $qString = $qGen->getQuery($qGen->queryConfig);
405 // debug($qGen->queryConfig);
406
407 switch($mQ) {
408 case 'count':
409 $qExplain = $GLOBALS['TYPO3_DB']->SELECTquery('count(*)', $qGen->table, $qString.t3lib_BEfunc::deleteClause($qGen->table));
410 break;
411 default:
412 $qExplain = $qGen->getSelectQuery($qString);
413 if ($mQ=='explain') {
414 $qExplain='EXPLAIN '.$qExplain;
415 }
416 break;
417 }
418
419 if (!$GLOBALS['BE_USER']->userTS['mod.']['dbint.']['disableShowSQLQuery']) {
420 $output.= $GLOBALS['SOBE']->doc->section('SQL query',$this->tableWrap(htmlspecialchars($qExplain)),0,1);
421 }
422
423 $res = @$GLOBALS['TYPO3_DB']->sql_query($qExplain);
424 if ($GLOBALS['TYPO3_DB']->sql_error()) {
425 $out.='<BR><strong>Error:</strong><BR><font color="red"><strong>'.$GLOBALS['TYPO3_DB']->sql_error().'</strong></font>';
426 $output.= $GLOBALS['SOBE']->doc->section('SQL error',$out,0,1);
427 } else {
428 $cPR = $this->getQueryResultCode($mQ,$res,$qGen->table);
429 $GLOBALS['TYPO3_DB']->sql_free_result($res);
430 $output.=$GLOBALS['SOBE']->doc->section($cPR['header'],$cPR['content'],0,1);
431 }
432 }
433 }
434 return $output;
435 }
436
437 /**
438 * [Describe function...]
439 *
440 * @param [type] $mQ: ...
441 * @param [type] $res: ...
442 * @param [type] $table: ...
443 * @return [type] ...
444 */
445 function getQueryResultCode($mQ,$res,$table) {
446 global $TCA;
447 $output='';
448 $cPR=array();
449 switch($mQ) {
450 case 'count':
451 $row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
452 $cPR['header']='Count';
453 $cPR['content']='<BR><strong>'.$row[0]. '</strong> records selected.';
454 break;
455 case 'all':
456 $rowArr=array();
457 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
458 $rowArr[]=$this->resultRowDisplay($row,$TCA[$table],$table);
459 $lrow=$row;
460 }
461 if (is_array($this->hookArray['beforeResultTable'])) {
462 foreach ($this->hookArray['beforeResultTable'] as $_funcRef) {
463 $out.=t3lib_div::callUserFunction($_funcRef, $GLOBALS['SOBE']->MOD_SETTINGS, $this);
464 }
465 }
466 if (count($rowArr)) {
467 $out.='<table border="0" cellpadding="2" cellspacing="1" width="100%">'.$this->resultRowTitles($lrow, $TCA[$table], $table).implode(LF, $rowArr).'</table>';
468 }
469 if (!$out) $out='<em>No rows selected!</em>';
470 $cPR['header']='Result';
471 $cPR['content']=$out;
472 break;
473 case 'csv':
474 $rowArr=array();
475 $first=1;
476 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
477 if ($first) {
478 $rowArr[]=$this->csvValues(array_keys($row),',','');
479 $first=0;
480 }
481 $rowArr[]=$this->csvValues($row, ',', '"', $TCA[$table], $table);
482 }
483 if (count($rowArr)) {
484 $out.='<textarea name="whatever" rows="20" wrap="off"'.$GLOBALS['SOBE']->doc->formWidthText($this->formW,'','off').' class="fixed-font">'.t3lib_div::formatForTextarea(implode(LF,$rowArr)).'</textarea>';
485 if (!$this->noDownloadB) {
486 $out.='<BR><input type="submit" name="download_file" value="Click to download file" onClick="window.location.href=\''.$this->downloadScript.'\';">'; // document.forms[0].target=\'_blank\';
487 }
488 // Downloads file:
489 if (t3lib_div::_GP('download_file')) {
490 $filename='TYPO3_'.$table.'_export_'.date('dmy-Hi').'.csv';
491 $mimeType = 'application/octet-stream';
492 header('Content-Type: '.$mimeType);
493 header('Content-Disposition: attachment; filename='.$filename);
494 echo implode(CRLF,$rowArr);
495 exit;
496 }
497 }
498 if (!$out) $out='<em>No rows selected!</em>';
499 $cPR['header']='Result';
500 $cPR['content']=$out;
501 break;
502 case 'xml':
503 $xmlObj = t3lib_div::makeInstance('t3lib_xml', 'typo3_export');
504 $xmlObj->includeNonEmptyValues=1;
505 $xmlObj->renderHeader();
506 $first=1;
507 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
508 if ($first) {
509 $xmlObj->setRecFields($table,implode(',',array_keys($row)));
510 // debug($xmlObj->XML_recFields);
511 $first=0;
512 }
513 $valueArray = $row;
514 if ($GLOBALS['SOBE']->MOD_SETTINGS['search_result_labels']) {
515 foreach ($valueArray as $key => $val) {
516 $valueArray[$key] = $this->getProcessedValueExtra($table, $key, $val, $conf, ',');
517 }
518 }
519 $xmlObj->addRecord($table, $valueArray);
520 }
521 $xmlObj->renderFooter();
522 if ($GLOBALS['TYPO3_DB']->sql_num_rows($res)) {
523 $xmlData=$xmlObj->getResult();
524 $out.='<textarea name="whatever" rows="20" wrap="off"'.$GLOBALS['SOBE']->doc->formWidthText($this->formW,'','off').' class="fixed-font">'.t3lib_div::formatForTextarea($xmlData).'</textarea>';
525 if (!$this->noDownloadB) {
526 $out.='<BR><input type="submit" name="download_file" value="Click to download file" onClick="window.location.href=\''.$this->downloadScript.'\';">'; // document.forms[0].target=\'_blank\';
527 }
528 // Downloads file:
529 if (t3lib_div::_GP('download_file')) {
530 $filename='TYPO3_'.$table.'_export_'.date('dmy-Hi').'.xml';
531 $mimeType = 'application/octet-stream';
532 header('Content-Type: '.$mimeType);
533 header('Content-Disposition: attachment; filename='.$filename);
534 echo $xmlData;
535 exit;
536 }
537 }
538 if (!$out) $out='<em>No rows selected!</em>';
539 $cPR['header']='Result';
540 $cPR['content']=$out;
541 break;
542 case 'explain':
543 default:
544 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
545 $out .= '<br />' . t3lib_utility_Debug::viewArray($row);
546 }
547 $cPR['header']='Explain SQL query';
548 $cPR['content']=$out;
549 break;
550 }
551 return $cPR;
552 }
553
554 /**
555 * [Describe function...]
556 *
557 * @param [type] $row: ...
558 * @param [type] $delim: ...
559 * @param [type] $quote: ...
560 * @param [type] $conf: ...
561 * @param [type] $table: ...
562 * @return [type] ...
563 */
564 function csvValues($row, $delim=',', $quote='"', $conf=array(), $table='') {
565 $valueArray = $row;
566 if ($GLOBALS['SOBE']->MOD_SETTINGS['search_result_labels'] && $table) {
567 foreach ($valueArray as $key => $val) {
568 $valueArray[$key] = $this->getProcessedValueExtra($table, $key, $val, $conf, ';');
569 }
570 }
571 return t3lib_div::csvValues($valueArray, $delim, $quote);
572 }
573
574 /**
575 * [Describe function...]
576 *
577 * @param [type] $str: ...
578 * @return [type] ...
579 */
580 function tableWrap($str) {
581 return '<table border="0" cellpadding="10" cellspacing="0" class="bgColor4"><tr><td nowrap><pre>'.$str.'</pre></td></tr></table>';
582 }
583
584 /**
585 * [Describe function...]
586 *
587 * @return [type] ...
588 */
589 function search() {
590 global $TCA;
591 $SET = $GLOBALS['SOBE']->MOD_SETTINGS;
592 $swords = $SET['sword'];
593
594 $limit=200;
595 $showAlways=0;
596 if ($swords) {
597 foreach ($TCA as $table => $value) {
598 // Get fields list
599 t3lib_div::loadTCA($table);
600 $conf=$TCA[$table];
601
602 // avoid querying tables with no columns
603 if (empty($conf['columns'])) {
604 continue;
605 }
606
607 $list = array_keys($conf['columns']);
608 // Get query
609 $qp = $GLOBALS['TYPO3_DB']->searchQuery(array($swords), $list, $table);
610
611 // Count:
612 $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', $table, $qp . t3lib_BEfunc::deleteClause($table));
613 if($count || $showAlways) {
614 // Output header:
615 $out.='<strong>TABLE:</strong> '.$GLOBALS['LANG']->sL($conf['ctrl']['title']).'<BR>';
616 $out.='<strong>Results:</strong> '.$count.'<BR>';
617
618 // Show to limit
619 if ($count) {
620 $rowArr = array();
621 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,'.$conf['ctrl']['label'], $table, $qp.t3lib_BEfunc::deleteClause($table), '', '', $limit);
622 while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
623 $rowArr[]=$this->resultRowDisplay($row,$conf,$table);
624 $lrow=$row;
625 }
626 $GLOBALS['TYPO3_DB']->sql_free_result($res);
627 $out.='<table border="0" cellpadding="2" cellspacing="1">'.$this->resultRowTitles($lrow,$conf,$table).implode(LF,$rowArr).'</table>';
628 }
629 $out.='<HR>';
630 }
631 }
632 }
633 return $out;
634 }
635
636 /**
637 * [Describe function...]
638 *
639 * @param [type] $row: ...
640 * @param [type] $conf: ...
641 * @param [type] $table: ...
642 * @return [type] ...
643 */
644 function resultRowDisplay($row,$conf,$table) {
645 static $even = false;
646 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
647 $SET = $GLOBALS['SOBE']->MOD_SETTINGS;
648 $out='<tr class="bgColor' . ($even ? '6' : '4') . '">';
649 $even = !$even;
650 foreach ($row as $fN => $fV) {
651 if (t3lib_div::inList($SET['queryFields'], $fN) || (!$SET['queryFields'] && $fN!='pid' && $fN!='deleted')) {
652 if ($SET['search_result_labels']) {
653 $fVnew = $this->getProcessedValueExtra($table, $fN, $fV, $conf, '<br />');
654 } else {
655 $fVnew = htmlspecialchars($fV);
656 }
657 $out.='<td>' . $fVnew . '</td>';
658 }
659 }
660 $params = '&edit['.$table.']['.$row['uid'].']=edit';
661 $out.='<td nowrap>';
662 if (!$row['deleted']) {
663 $out .= '<a href="#" onClick="top.launchView(\''.$table.'\','.$row['uid'].',\''.$GLOBALS['BACK_PATH'].'\');return false;">' . t3lib_iconWorks::getSpriteIcon('status-dialog-information') . '</a>';
664 $out .= '<a href="#" onClick="'.t3lib_BEfunc::editOnClick($params, $GLOBALS['BACK_PATH'], t3lib_div::getIndpEnv('REQUEST_URI').t3lib_div::implodeArrayForUrl('SET', (array)t3lib_div::_POST('SET'))).'">' . t3lib_iconWorks::getSpriteIcon('actions-document-open') . '</a>';
665 } else {
666 $out.= '<a href="'.t3lib_div::linkThisUrl($GLOBALS['BACK_PATH'].'tce_db.php', array('cmd['.$table.']['.$row['uid'].'][undelete]' => '1', 'redirect' => t3lib_div::linkThisScript(array()))).'">';
667 $out.= t3lib_iconWorks::getSpriteIcon('actions-edit-restore', array('title' => 'undelete only')) . '</a>';
668 $out.= '<a href="'.t3lib_div::linkThisUrl($GLOBALS['BACK_PATH'].'tce_db.php', array('cmd['.$table.']['.$row['uid'].'][undelete]' => '1', 'redirect' => t3lib_div::linkThisUrl('alt_doc.php', array('edit['.$table.']['.$row['uid'].']' => 'edit', 'returnUrl' => t3lib_div::linkThisScript(array()))))).'">';
669 $out.= t3lib_iconWorks::getSpriteIcon('actions-edit-restore-edit', array('title' => 'undelete and edit')) . '</a>';
670 }
671 $_params = array($table=>$row);
672 if (is_array($this->hookArray['additionalButtons'])) {
673 foreach ($this->hookArray['additionalButtons'] as $_funcRef) {
674 $out.=t3lib_div::callUserFunction($_funcRef, $_params, $this);
675 }
676 }
677 $out.='</td>
678 </tr>
679 ';
680 return $out;
681 }
682
683 /**
684 * [Describe function...]
685 *
686 * @param [type] $table: ...
687 * @param [type] $fN: ...
688 * @param [type] $fV: ...
689 * @param [type] $conf: ...
690 * @param [type] $splitString: ...
691 * @return [type] ...
692 */
693 function getProcessedValueExtra($table, $fN, $fV, $conf, $splitString) {
694 global $TCA;
695 // Analysing the fields in the table.
696 if (is_array($TCA[$table])) {
697 t3lib_div::loadTCA($table);
698 $fC = $TCA[$table]['columns'][$fN];
699 $fields = $fC['config'];
700 $fields['exclude'] = $fC['exclude'];
701 if (is_array($fC) && $fC['label']) {
702 $fields['label'] = preg_replace('/:$/', '', trim($GLOBALS['LANG']->sL($fC['label'])));
703
704 switch ($fields['type']) {
705 case 'input':
706 if (preg_match('/int|year/i', $fields['eval'])) {
707 $fields['type'] = 'number';
708 } elseif (preg_match('/time/i', $fields['eval'])) {
709 $fields['type'] = 'time';
710 } elseif (preg_match('/date/i', $fields['eval'])) {
711 $fields['type'] = 'date';
712 } else {
713 $fields['type'] = 'text';
714 }
715 break;
716 case 'check':
717 if (!$fields['items']) {
718 $fields['type'] = 'boolean';
719 } else {
720 $fields['type'] = 'binary';
721 }
722 break;
723 case 'radio':
724 $fields['type'] = 'multiple';
725 break;
726 case 'select':
727 $fields['type'] = 'multiple';
728 if ($fields['foreign_table']) {
729 $fields['type'] = 'relation';
730 }
731 if ($fields['special']) {
732 $fields['type'] = 'text';
733 }
734 break;
735 case 'group':
736 $fields['type'] = 'files';
737 if ($fields['internal_type']=='db') {
738 $fields['type'] = 'relation';
739 }
740 break;
741 case 'user':
742 case 'flex':
743 case 'passthrough':
744 case 'none':
745 case 'text':
746 default:
747 $fields['type'] = 'text';
748 break;
749 }
750 } else {
751 $fields['label'] = '[FIELD: '.$fN.']';
752 switch ($fN) {
753 case 'pid':
754 $fields['type'] = 'relation';
755 $fields['allowed'] = 'pages';
756 break;
757 case 'cruser_id':
758 $fields['type'] = 'relation';
759 $fields['allowed'] = 'be_users';
760 break;
761 case 'tstamp':
762 case 'crdate':
763 $fields['type'] = 'time';
764 break;
765 default:
766 $fields['type'] = 'number';
767 break;
768 }
769 }
770 }
771
772 switch ($fields['type']) {
773 case 'date':
774 if ($fV != -1) {
775 $out = strftime('%e-%m-%Y', $fV);
776 }
777 break;
778 case 'time':
779 if ($fV != -1) {
780 if ($splitString=='<br />') {
781 $out = strftime('%H:%M'.$splitString.'%e-%m-%Y', $fV);
782 } else {
783 $out = strftime('%H:%M %e-%m-%Y', $fV);
784 }
785 }
786 break;
787 case 'multiple':
788 case 'binary':
789 case 'relation':
790 $out = $this->makeValueList($fN, $fV, $fields, $table, $splitString);
791 break;
792 case 'boolean':
793 $out = $fV ? 'True' : 'False';
794 break;
795 case 'files':
796 default:
797 $out = htmlspecialchars($fV);
798 break;
799 }
800 return $out;
801 }
802
803 /*
804 * [Describe function...]
805 *
806 * @param [type] $qString: ...
807 * @param [type] $depth: ...
808 * @param [type] $begin: ...
809 * @param [type] $perms_clause: ...
810 * @return [type] ...
811 */
812 function getTreeList($id, $depth, $begin = 0, $perms_clause) {
813 $depth = intval($depth);
814 $begin = intval($begin);
815 $id = intval($id);
816 if ($begin == 0) {
817 $theList = $id;
818 } else {
819 $theList = '';
820 }
821 if ($id && $depth > 0) {
822 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
823 'uid',
824 'pages',
825 'pid='.$id.' '.t3lib_BEfunc::deleteClause('pages').' AND '.$perms_clause
826 );
827 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
828 if ($begin <= 0) {
829 $theList .= ','.$row['uid'];
830 }
831 if ($depth > 1) {
832 $theList .= $this->getTreeList($row['uid'], $depth-1, $begin-1, $perms_clause);
833 }
834 }
835 $GLOBALS['TYPO3_DB']->sql_free_result($res);
836 }
837 return $theList;
838 }
839
840 /**
841 * [Describe function...]
842 *
843 * @param [type] $fN: ...
844 * @param [type] $fV: ...
845 * @param [type] $conf: ...
846 * @param [type] $table: ...
847 * @param [type] $splitString: ...
848 * @return [type] ...
849 */
850 function makeValueList($fN, $fV, $conf, $table, $splitString) {
851 $fieldSetup = $conf;
852 if ($fieldSetup['type'] == 'files') {
853 $d = dir(PATH_site . $fieldSetup['uploadfolder']);
854 while (false !== ($entry = $d->read())) {
855 if ($entry == '.' || $entry == '..') {
856 continue;
857 }
858 $fileArray[] = $entry;
859 }
860 $d->close();
861 natcasesort($fileArray);
862 while (list(, $fileName) = each($fileArray)) {
863 if (t3lib_div::inList($fV, $fileName) || $fV == $fileName) {
864 if (!$out) {
865 $out = htmlspecialchars($fileName);
866 } else {
867 $out .= $splitString.htmlspecialchars($fileName);
868 }
869 }
870 }
871 }
872 if ($fieldSetup['type'] == 'multiple') {
873 foreach ($fieldSetup['items'] as $key => $val) {
874 if (substr($val[0], 0, 4) == 'LLL:') {
875 $value = $GLOBALS['LANG']->sL($val[0]);
876 } else {
877 $value = $val[0];
878 }
879 if (t3lib_div::inList($fV, $val[1]) || $fV == $val[1]) {
880 if (!$out) {
881 $out = htmlspecialchars($value);
882 } else {
883 $out .= $splitString.htmlspecialchars($value);
884 }
885 }
886 }
887 }
888 if ($fieldSetup['type'] == 'binary') {
889 foreach($fieldSetup['items'] as $Key => $val) {
890 if (substr($val[0], 0, 4) == 'LLL:') {
891 $value = $GLOBALS['LANG']->sL($val[0]);
892 } else {
893 $value = $val[0];
894 }
895 if (!$out) {
896 $out = htmlspecialchars($value);
897 } else {
898 $out .= $splitString.htmlspecialchars($value);
899 }
900 }
901 }
902 if ($fieldSetup['type']=='relation') {
903 if ($fieldSetup['items']) {
904 foreach($fieldSetup['items'] as $key => $val) {
905 if (substr($val[0], 0, 4) == 'LLL:') {
906 $value = $GLOBALS['LANG']->sL($val[0]);
907 } else {
908 $value = $val[0];
909 }
910 if (t3lib_div::inList($fV, $value) || $fV == $value) {
911 if (!$out) {
912 $out = htmlspecialchars($value);
913 } else {
914 $out .= $splitString.htmlspecialchars($value);
915 }
916 }
917 }
918 }
919 global $TCA;
920 if (stristr($fieldSetup['allowed'], ',')) {
921 $from_table_Arr = explode(',', $fieldSetup['allowed']);
922 $useTablePrefix = 1;
923 if (!$fieldSetup['prepend_tname']) {
924 $checkres = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fN, $table, 'uid '.t3lib_BEfunc::deleteClause($table), $groupBy = '', $orderBy = '', $limit = '');
925 if ($checkres) {
926 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($checkres)) {
927 if (stristr($row[$fN], ',')) {
928 $checkContent = explode(',', $row[$fN]);
929 foreach($checkContent as $singleValue) {
930 if (!stristr($singleValue, '_')) {
931 $dontPrefixFirstTable = 1;
932 }
933 }
934 } else {
935 $singleValue = $row[$fN];
936 if (strlen($singleValue) && !stristr($singleValue, '_')) {
937 $dontPrefixFirstTable = 1;
938 }
939 }
940 }
941 $GLOBALS['TYPO3_DB']->sql_free_result($res);
942 }
943 }
944 } else {
945 $from_table_Arr[0] = $fieldSetup['allowed'];
946 }
947 if ($fieldSetup['prepend_tname']) {
948 $useTablePrefix = 1;
949 }
950 if ($fieldSetup['foreign_table']) {
951 $from_table_Arr[0] = $fieldSetup['foreign_table'];
952 }
953 $counter = 0;
954 foreach($from_table_Arr as $from_table) {
955 if (($useTablePrefix && !$dontPrefixFirstTable && $counter != 1) || $counter == 1) {
956 $tablePrefix = $from_table.'_';
957 }
958 $counter = 1;
959 if (is_array($TCA[$from_table])) {
960 t3lib_div::loadTCA($from_table);
961 $labelField = $TCA[$from_table]['ctrl']['label'];
962 $altLabelField = $TCA[$from_table]['ctrl']['label_alt'];
963 if ($TCA[$from_table]['columns'][$labelField]['config']['items']) {
964 reset($TCA[$from_table]['columns'][$labelField]['config']['items']);
965 while (list(, $labelArray) = each($TCA[$from_table]['columns'][$labelField]['config']['items'])) {
966 if (substr($labelArray[0], 0, 4) == 'LLL:') {
967 $labelFieldSelect[$labelArray[1]] = $GLOBALS['LANG']->sL($labelArray[0]);
968 } else {
969 $labelFieldSelect[$labelArray[1]] = $labelArray[0];
970 }
971 }
972 $useSelectLabels = 1;
973 }
974 if ($TCA[$from_table]['columns'][$altLabelField]['config']['items']) {
975 reset($TCA[$from_table]['columns'][$altLabelField]['config']['items']);
976 foreach($TCA[$from_table]['columns'][$altLabelField]['config']['items'] as $altLabelArray) {
977 if (substr($altLabelArray[0], 0, 4) == 'LLL:') {
978 $altLabelFieldSelect[$altLabelArray[1]] = $GLOBALS['LANG']->sL($altLabelArray[0]);
979 } else {
980 $altLabelFieldSelect[$altLabelArray[1]] = $altLabelArray[0];
981 }
982 }
983 $useAltSelectLabels = 1;
984 }
985 $altLabelFieldSelect = $altLabelField ? ','.$altLabelField : '';
986 $select_fields = 'uid,'.$labelField.$altLabelFieldSelect;
987 if (!$GLOBALS['BE_USER']->isAdmin() && $GLOBALS['TYPO3_CONF_VARS']['BE']['lockBeUserToDBmounts']) {
988 $webMounts = $GLOBALS['BE_USER']->returnWebmounts();
989 $perms_clause = $GLOBALS['BE_USER']->getPagePermsClause(1);
990 foreach($webMounts as $key => $val) {
991 if ($webMountPageTree) {
992 $webMountPageTreePrefix = ',';
993 }
994 $webMountPageTree .= $webMountPageTreePrefix.$this->getTreeList($val, 999, $begin = 0, $perms_clause);
995 }
996 if ($from_table == 'pages') {
997 $where_clause = 'uid IN ('.$webMountPageTree.') '.t3lib_BEfunc::deleteClause($from_table).' AND '.$perms_clause;
998 } else {
999 $where_clause = 'pid IN ('.$webMountPageTree.') '.t3lib_BEfunc::deleteClause($from_table);
1000 }
1001 } else {
1002 $where_clause = 'uid'.t3lib_BEfunc::deleteClause($from_table);
1003 }
1004 $orderBy = 'uid';
1005 if (!$this->tableArray[$from_table]) {
1006 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($select_fields, $from_table, $where_clause, $groupBy = '', $orderBy, $limit = '');
1007 $this->tableArray[$from_table] = array();
1008 }
1009 if ($res) {
1010 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
1011 $this->tableArray[$from_table][] = $row;
1012 }
1013 $GLOBALS['TYPO3_DB']->sql_free_result($res);
1014 }
1015 reset($this->tableArray[$from_table]);
1016 foreach ($this->tableArray[$from_table] as $key => $val) {
1017 $GLOBALS['SOBE']->MOD_SETTINGS['labels_noprefix'] = $GLOBALS['SOBE']->MOD_SETTINGS['labels_noprefix'] == 1 ? 'on' :
1018 $GLOBALS['SOBE']->MOD_SETTINGS['labels_noprefix'];
1019 $prefixString = $GLOBALS['SOBE']->MOD_SETTINGS['labels_noprefix'] == 'on' ? '' : ' ['.$tablePrefix.$val['uid'].'] ';
1020 if (t3lib_div::inList($fV, $tablePrefix.$val['uid']) || $fV == $tablePrefix.$val['uid']) {
1021 if ($useSelectLabels) {
1022 if (!$out) {
1023 $out = htmlspecialchars($prefixString.$labelFieldSelect[$val[$labelField]]);
1024 } else {
1025 $out .= $splitString.htmlspecialchars($prefixString.$labelFieldSelect[$val[$labelField]]);
1026 }
1027 } elseif ($val[$labelField]) {
1028 if (!$out) {
1029 $out = htmlspecialchars($prefixString.$val[$labelField]);
1030 } else {
1031 $out .= $splitString.htmlspecialchars($prefixString.$val[$labelField]);
1032 }
1033 } elseif ($useAltSelectLabels) {
1034 if (!$out) {
1035 $out = htmlspecialchars($prefixString.$altLabelFieldSelect[$val[$altLabelField]]);
1036 } else {
1037 $out .= $splitString.htmlspecialchars($prefixString.$altLabelFieldSelect[$val[$altLabelField]]);
1038 }
1039 } else {
1040 if (!$out) {
1041 $out = htmlspecialchars($prefixString.$val[$altLabelField]);
1042 } else {
1043 $out .= $splitString.htmlspecialchars($prefixString.$val[$altLabelField]);
1044 }
1045 }
1046 }
1047 }
1048 }
1049 }
1050 }
1051 return $out;
1052 }
1053
1054 /**
1055 * Render table header
1056 *
1057 * @param array row: Table columns
1058 * @param array conf: Table TCA
1059 * @param string table: Table name
1060 * @return string HTML of table header
1061 */
1062 function resultRowTitles($row, $conf, $table) {
1063 $SET = $GLOBALS['SOBE']->MOD_SETTINGS;
1064
1065 $tableHeader = array();
1066
1067 // Start header row
1068 $tableHeader[] = '<thead><tr class="bgColor5">';
1069
1070 // Iterate over given columns
1071 foreach ($row as $fieldName => $fieldValue) {
1072 if (t3lib_div::inList($SET['queryFields'], $fieldName) || (!$SET['queryFields'] && $fieldName != 'pid' && $fieldName != 'deleted')) {
1073 $THparams = (strlen($fieldValue) < 50) ? ' style="white-space:nowrap;"' : '';
1074
1075 if ($GLOBALS['SOBE']->MOD_SETTINGS['search_result_labels']) {
1076 $title = $GLOBALS['LANG']->sL($conf['columns'][$fieldName]['label'] ? $conf['columns'][$fieldName]['label'] : $fieldName, 1);
1077 } else {
1078 $title = $GLOBALS['LANG']->sL($fieldName, 1);
1079 }
1080
1081 $tableHeader[] = '<th' . $THparams . '>' . $title . '</th>';
1082 }
1083 }
1084
1085 // Add empty icon column
1086 $tableHeader[] = '<th style="white-space:nowrap;"></th>';
1087 // Close header row
1088 $tableHeader[] = '</tr></thead>';
1089
1090 return implode($tableHeader, LF);
1091 }
1092
1093 /**
1094 * [Describe function...]
1095 *
1096 * @param [type] $row: ...
1097 * @param [type] $conf: ...
1098 * @param [type] $table: ...
1099 * @return [type] ...
1100 */
1101 function csvRowTitles($row, $conf, $table) {
1102 $SET = $GLOBALS['SOBE']->MOD_SETTINGS;
1103 foreach ($row as $fN => $fV) {
1104 if (t3lib_div::inList($SET['queryFields'], $fN) || (!$SET['queryFields'] && $fN!='pid')) {
1105 if (!$out) {
1106 if ($GLOBALS['SOBE']->MOD_SETTINGS['search_result_labels']) {
1107 $out = $GLOBALS['LANG']->sL($conf['columns'][$fN]['label']?$conf['columns'][$fN]['label']:$fN, 1);
1108 } else {
1109 $out = $GLOBALS['LANG']->sL($fN, 1);
1110 }
1111 } else {
1112 if ($GLOBALS['SOBE']->MOD_SETTINGS['search_result_labels']) {
1113 $out.= ','.$GLOBALS['LANG']->sL($conf['columns'][$fN]['label']?$conf['columns'][$fN]['label']:$fN, 1);
1114 } else {
1115 $out.= ','.$GLOBALS['LANG']->sL($fN, 1);
1116 }
1117 }
1118 }
1119 }
1120 return $out;
1121 }
1122
1123 /**
1124 * Sets the current name of the input form.
1125 *
1126 * @param string $formName: The name of the form.
1127 * @return void
1128 */
1129 public function setFormName($formName) {
1130 $this->formName = trim($formName);
1131 }
1132 }
1133
1134 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_fullsearch.php']) {
1135 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_fullsearch.php']);
1136 }
1137 ?>