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