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