[BUGFIX] Fix 1-2-3(-4) wizard
[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 // Query Maker:
345 $qGen = t3lib_div::makeInstance('t3lib_queryGenerator');
346 $qGen->init('queryConfig', $GLOBALS['SOBE']->MOD_SETTINGS['queryTable']);
347 if ($this->formName) {
348 $qGen->setFormName($this->formName);
349 }
350 $tmpCode = $qGen->makeSelectorTable($GLOBALS['SOBE']->MOD_SETTINGS);
351 $output .= $GLOBALS['SOBE']->doc->section('Make query', $tmpCode, 0, 1);
352
353 $mQ = $GLOBALS['SOBE']->MOD_SETTINGS['search_query_makeQuery'];
354
355 // Make form elements:
356 if ($qGen->table && is_array($GLOBALS['TCA'][$qGen->table])) {
357 if ($mQ) {
358 // Show query
359 $qGen->enablePrefix = 1;
360 $qString = $qGen->getQuery($qGen->queryConfig);
361
362 switch ($mQ) {
363 case 'count':
364 $qExplain = $GLOBALS['TYPO3_DB']->SELECTquery('count(*)', $qGen->table, $qString . t3lib_BEfunc::deleteClause($qGen->table));
365 break;
366 default:
367 $qExplain = $qGen->getSelectQuery($qString);
368 if ($mQ == 'explain') {
369 $qExplain = 'EXPLAIN ' . $qExplain;
370 }
371 break;
372 }
373
374 if (!$GLOBALS['BE_USER']->userTS['mod.']['dbint.']['disableShowSQLQuery']) {
375 $output .= $GLOBALS['SOBE']->doc->section('SQL query', $this->tableWrap(htmlspecialchars($qExplain)), 0, 1);
376 }
377
378 $res = @$GLOBALS['TYPO3_DB']->sql_query($qExplain);
379 if ($GLOBALS['TYPO3_DB']->sql_error()) {
380 $out = '<BR><strong>Error:</strong><BR><font color="red"><strong>' . $GLOBALS['TYPO3_DB']->sql_error() . '</strong></font>';
381 $output .= $GLOBALS['SOBE']->doc->section('SQL error', $out, 0, 1);
382 } else {
383 $cPR = $this->getQueryResultCode($mQ, $res, $qGen->table);
384 $GLOBALS['TYPO3_DB']->sql_free_result($res);
385 $output .= $GLOBALS['SOBE']->doc->section($cPR['header'], $cPR['content'], 0, 1);
386 }
387 }
388 }
389 return $output;
390 }
391
392 /**
393 * [Describe function...]
394 *
395 * @param string $mQ
396 * @param pointer $res
397 * @param string $table
398 * @return string
399 */
400 function getQueryResultCode($mQ, $res, $table) {
401 $out = '';
402 $cPR = array();
403 switch ($mQ) {
404 case 'count':
405 $row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
406 $cPR['header'] = 'Count';
407 $cPR['content'] = '<BR><strong>' . $row[0] . '</strong> records selected.';
408 break;
409 case 'all':
410 $rowArr = array();
411 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
412 $rowArr[] = $this->resultRowDisplay($row, $GLOBALS['TCA'][$table], $table);
413 $lrow = $row;
414 }
415 if (is_array($this->hookArray['beforeResultTable'])) {
416 foreach ($this->hookArray['beforeResultTable'] as $_funcRef) {
417 $out .= t3lib_div::callUserFunction($_funcRef, $GLOBALS['SOBE']->MOD_SETTINGS, $this);
418 }
419 }
420 if (count($rowArr)) {
421 $out .= '<table border="0" cellpadding="2" cellspacing="1" width="100%">' .
422 $this->resultRowTitles($lrow, $GLOBALS['TCA'][$table], $table) . implode(LF, $rowArr) . '</table>';
423 }
424 if (!$out) {
425 $out = '<em>No rows selected!</em>';
426 }
427 $cPR['header'] = 'Result';
428 $cPR['content'] = $out;
429 break;
430 case 'csv':
431 $rowArr = array();
432 $first = 1;
433 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
434 if ($first) {
435 $rowArr[] = $this->csvValues(array_keys($row), ',', '');
436 $first = 0;
437 }
438 $rowArr[] = $this->csvValues($row, ',', '"', $GLOBALS['TCA'][$table], $table);
439 }
440 if (count($rowArr)) {
441 $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>';
442 if (!$this->noDownloadB) {
443 $out .= '<BR><input type="submit" name="download_file" value="Click to download file" onClick="window.location.href=\'' . $this->downloadScript . '\';">'; // document.forms[0].target=\'_blank\';
444 }
445 // Downloads file:
446 if (t3lib_div::_GP('download_file')) {
447 $filename = 'TYPO3_' . $table . '_export_' . date('dmy-Hi') . '.csv';
448 $mimeType = 'application/octet-stream';
449 header('Content-Type: ' . $mimeType);
450 header('Content-Disposition: attachment; filename=' . $filename);
451 echo implode(CRLF, $rowArr);
452 exit;
453 }
454 }
455 if (!$out) {
456 $out = '<em>No rows selected!</em>';
457 }
458 $cPR['header'] = 'Result';
459 $cPR['content'] = $out;
460 break;
461 case 'xml':
462 $xmlObj = t3lib_div::makeInstance('t3lib_xml', 'typo3_export');
463 $xmlObj->includeNonEmptyValues = 1;
464 $xmlObj->renderHeader();
465 $first = 1;
466 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
467 if ($first) {
468 $xmlObj->setRecFields($table, implode(',', array_keys($row)));
469 $first = 0;
470 }
471 $valueArray = $row;
472 if ($GLOBALS['SOBE']->MOD_SETTINGS['search_result_labels']) {
473 foreach ($valueArray as $key => $val) {
474 $valueArray[$key] = $this->getProcessedValueExtra($table, $key, $val, array(), ',');
475 }
476 }
477 $xmlObj->addRecord($table, $valueArray);
478 }
479 $xmlObj->renderFooter();
480 if ($GLOBALS['TYPO3_DB']->sql_num_rows($res)) {
481 $xmlData = $xmlObj->getResult();
482 $out .= '<textarea name="whatever" rows="20" wrap="off"' . $GLOBALS['SOBE']->doc->formWidthText($this->formW, '', 'off') . ' class="fixed-font">' . t3lib_div::formatForTextarea($xmlData) . '</textarea>';
483 if (!$this->noDownloadB) {
484 $out .= '<BR><input type="submit" name="download_file" value="Click to download file" onClick="window.location.href=\'' . $this->downloadScript . '\';">'; // document.forms[0].target=\'_blank\';
485 }
486 // Downloads file:
487 if (t3lib_div::_GP('download_file')) {
488 $filename = 'TYPO3_' . $table . '_export_' . date('dmy-Hi') . '.xml';
489 $mimeType = 'application/octet-stream';
490 header('Content-Type: ' . $mimeType);
491 header('Content-Disposition: attachment; filename=' . $filename);
492 echo $xmlData;
493 exit;
494 }
495 }
496 if (!$out) {
497 $out = '<em>No rows selected!</em>';
498 }
499 $cPR['header'] = 'Result';
500 $cPR['content'] = $out;
501 break;
502 case 'explain':
503 default:
504 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
505 $out .= '<br />' . t3lib_utility_Debug::viewArray($row);
506 }
507 $cPR['header'] = 'Explain SQL query';
508 $cPR['content'] = $out;
509 break;
510 }
511 return $cPR;
512 }
513
514 /**
515 * [Describe function...]
516 *
517 * @param array $row
518 * @param string $delim
519 * @param string $quote
520 * @param array $conf
521 * @param string $table
522 * @return string A single line of CSV
523 */
524 function csvValues($row, $delim = ',', $quote = '"', $conf = array(), $table = '') {
525 $valueArray = $row;
526 if ($GLOBALS['SOBE']->MOD_SETTINGS['search_result_labels'] && $table) {
527 foreach ($valueArray as $key => $val) {
528 $valueArray[$key] = $this->getProcessedValueExtra($table, $key, $val, $conf, ';');
529 }
530 }
531 return t3lib_div::csvValues($valueArray, $delim, $quote);
532 }
533
534 /**
535 * [Describe function...]
536 *
537 * @param string $str
538 * @return string
539 */
540 function tableWrap($str) {
541 return '<table border="0" cellpadding="10" cellspacing="0" class="bgColor4"><tr><td nowrap><pre>' . $str . '</pre></td></tr></table>';
542 }
543
544 /**
545 * [Describe function...]
546 *
547 * @return string
548 */
549 function search() {
550 $SET = $GLOBALS['SOBE']->MOD_SETTINGS;
551 $swords = $SET['sword'];
552
553 $out = '';
554 $limit = 200;
555 $showAlways = 0;
556 if ($swords) {
557 foreach ($GLOBALS['TCA'] as $table => $value) {
558 // Get fields list
559 t3lib_div::loadTCA($table);
560 $conf = $GLOBALS['TCA'][$table];
561
562 // Avoid querying tables with no columns
563 if (empty($conf['columns'])) {
564 continue;
565 }
566
567 $list = array_keys($conf['columns']);
568 // Get query
569 $qp = $GLOBALS['TYPO3_DB']->searchQuery(array($swords), $list, $table);
570
571 // Count:
572 $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', $table, $qp . t3lib_BEfunc::deleteClause($table));
573 if ($count || $showAlways) {
574 // Output header:
575 $out .= '<strong>TABLE:</strong> ' . $GLOBALS['LANG']->sL($conf['ctrl']['title']) . '<BR>';
576 $out .= '<strong>Results:</strong> ' . $count . '<BR>';
577
578 // Show to limit
579 if ($count) {
580 $rowArr = array();
581 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,' . $conf['ctrl']['label'], $table, $qp . t3lib_BEfunc::deleteClause($table), '', '', $limit);
582 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
583 $rowArr[] = $this->resultRowDisplay($row, $conf, $table);
584 $lrow = $row;
585 }
586 $GLOBALS['TYPO3_DB']->sql_free_result($res);
587 $out .= '<table border="0" cellpadding="2" cellspacing="1">' . $this->resultRowTitles($lrow, $conf, $table) . implode(LF, $rowArr) . '</table>';
588 }
589 $out .= '<HR>';
590 }
591 }
592 }
593 return $out;
594 }
595
596 /**
597 * [Describe function...]
598 *
599 * @param array $row
600 * @param array $conf
601 * @param string $table
602 * @return string
603 */
604 function resultRowDisplay($row, $conf, $table) {
605 static $even = FALSE;
606 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
607 $SET = $GLOBALS['SOBE']->MOD_SETTINGS;
608 $out = '<tr class="bgColor' . ($even ? '6' : '4') . '">';
609 $even = !$even;
610 foreach ($row as $fN => $fV) {
611 if (t3lib_div::inList($SET['queryFields'], $fN) || (!$SET['queryFields'] && $fN != 'pid' && $fN != 'deleted')) {
612 if ($SET['search_result_labels']) {
613 $fVnew = $this->getProcessedValueExtra($table, $fN, $fV, $conf, '<br />');
614 } else {
615 $fVnew = htmlspecialchars($fV);
616 }
617 $out .= '<td>' . $fVnew . '</td>';
618 }
619 }
620 $params = '&edit[' . $table . '][' . $row['uid'] . ']=edit';
621 $out .= '<td nowrap>';
622 if (!$row['deleted']) {
623 $out .= '<a href="#" onClick="top.launchView(\'' . $table . '\',' . $row['uid'] . ',\'' . $GLOBALS['BACK_PATH'] . '\');return false;">' . t3lib_iconWorks::getSpriteIcon('status-dialog-information') . '</a>';
624 $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>';
625 } else {
626 $out .= '<a href="' . t3lib_div::linkThisUrl($GLOBALS['BACK_PATH'] . 'tce_db.php',
627 array(
628 'cmd[' . $table . '][' . $row['uid'] . '][undelete]' => '1',
629 'redirect' => t3lib_div::linkThisScript(array()))) . t3lib_BEfunc::getUrlToken('tceAction') . '">';
630 $out .= t3lib_iconWorks::getSpriteIcon('actions-edit-restore', array('title' => 'undelete only')) . '</a>';
631 $out .= '<a href="' . t3lib_div::linkThisUrl($GLOBALS['BACK_PATH'] . 'tce_db.php',
632 array(
633 'cmd[' . $table . '][' . $row['uid'] . '][undelete]' => '1',
634 'redirect' => t3lib_div::linkThisUrl('alt_doc.php',
635 array(
636 'edit[' . $table . '][' . $row['uid'] . ']' => 'edit',
637 'returnUrl' => t3lib_div::linkThisScript(array())
638 )
639 )
640 )
641 ) . t3lib_BEfunc::getUrlToken('tceAction') . '">';
642 $out .= t3lib_iconWorks::getSpriteIcon('actions-edit-restore-edit', array('title' => 'undelete and edit')) . '</a>';
643 }
644 $_params = array($table => $row);
645 if (is_array($this->hookArray['additionalButtons'])) {
646 foreach ($this->hookArray['additionalButtons'] as $_funcRef) {
647 $out .= t3lib_div::callUserFunction($_funcRef, $_params, $this);
648 }
649 }
650 $out .= '</td>
651 </tr>
652 ';
653 return $out;
654 }
655
656 /**
657 * [Describe function...]
658 *
659 * @param string $table
660 * @param string $fN
661 * @param string $fV
662 * @param array $conf Not used
663 * @param string $splitString
664 * @return string
665 */
666 function getProcessedValueExtra($table, $fN, $fV, $conf, $splitString) {
667 // Analysing the fields in the table.
668 if (is_array($GLOBALS['TCA'][$table])) {
669 t3lib_div::loadTCA($table);
670 $fC = $GLOBALS['TCA'][$table]['columns'][$fN];
671 $fields = $fC['config'];
672 $fields['exclude'] = $fC['exclude'];
673 if (is_array($fC) && $fC['label']) {
674 $fields['label'] = preg_replace('/:$/', '', trim($GLOBALS['LANG']->sL($fC['label'])));
675
676 switch ($fields['type']) {
677 case 'input':
678 if (preg_match('/int|year/i', $fields['eval'])) {
679 $fields['type'] = 'number';
680 } elseif (preg_match('/time/i', $fields['eval'])) {
681 $fields['type'] = 'time';
682 } elseif (preg_match('/date/i', $fields['eval'])) {
683 $fields['type'] = 'date';
684 } else {
685 $fields['type'] = 'text';
686 }
687 break;
688 case 'check':
689 if (!$fields['items']) {
690 $fields['type'] = 'boolean';
691 } else {
692 $fields['type'] = 'binary';
693 }
694 break;
695 case 'radio':
696 $fields['type'] = 'multiple';
697 break;
698 case 'select':
699 $fields['type'] = 'multiple';
700 if ($fields['foreign_table']) {
701 $fields['type'] = 'relation';
702 }
703 if ($fields['special']) {
704 $fields['type'] = 'text';
705 }
706 break;
707 case 'group':
708 $fields['type'] = 'files';
709 if ($fields['internal_type'] == 'db') {
710 $fields['type'] = 'relation';
711 }
712 break;
713 case 'user':
714 case 'flex':
715 case 'passthrough':
716 case 'none':
717 case 'text':
718 default:
719 $fields['type'] = 'text';
720 break;
721 }
722 } else {
723 $fields['label'] = '[FIELD: ' . $fN . ']';
724 switch ($fN) {
725 case 'pid':
726 $fields['type'] = 'relation';
727 $fields['allowed'] = 'pages';
728 break;
729 case 'cruser_id':
730 $fields['type'] = 'relation';
731 $fields['allowed'] = 'be_users';
732 break;
733 case 'tstamp':
734 case 'crdate':
735 $fields['type'] = 'time';
736 break;
737 default:
738 $fields['type'] = 'number';
739 break;
740 }
741 }
742 }
743
744 switch ($fields['type']) {
745 case 'date':
746 if ($fV != -1) {
747 $out = strftime('%e-%m-%Y', $fV);
748 }
749 break;
750 case 'time':
751 if ($fV != -1) {
752 if ($splitString == '<br />') {
753 $out = strftime('%H:%M' . $splitString . '%e-%m-%Y', $fV);
754 } else {
755 $out = strftime('%H:%M %e-%m-%Y', $fV);
756 }
757 }
758 break;
759 case 'multiple':
760 case 'binary':
761 case 'relation':
762 $out = $this->makeValueList($fN, $fV, $fields, $table, $splitString);
763 break;
764 case 'boolean':
765 $out = $fV ? 'True' : 'False';
766 break;
767 case 'files':
768 default:
769 $out = htmlspecialchars($fV);
770 break;
771 }
772 return $out;
773 }
774
775 /**
776 * [Describe function...]
777 *
778 * @param integer $id
779 * @param integer $depth
780 * @param integer $begin
781 * @param string $perms_clause
782 * @return string
783 */
784 function getTreeList($id, $depth, $begin = 0, $perms_clause) {
785 $depth = intval($depth);
786 $begin = intval($begin);
787 $id = intval($id);
788 if ($begin == 0) {
789 $theList = $id;
790 } else {
791 $theList = '';
792 }
793 if ($id && $depth > 0) {
794 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
795 'uid',
796 'pages',
797 'pid=' . $id . ' ' . t3lib_BEfunc::deleteClause('pages') . ' AND ' . $perms_clause
798 );
799 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
800 if ($begin <= 0) {
801 $theList .= ',' . $row['uid'];
802 }
803 if ($depth > 1) {
804 $theList .= $this->getTreeList($row['uid'], $depth - 1, $begin - 1, $perms_clause);
805 }
806 }
807 $GLOBALS['TYPO3_DB']->sql_free_result($res);
808 }
809 return $theList;
810 }
811
812 /**
813 * [Describe function...]
814 *
815 * @param string $fN
816 * @param string $fV
817 * @param array $conf
818 * @param string $table
819 * @param string $splitString
820 * @return string
821 */
822 function makeValueList($fN, $fV, $conf, $table, $splitString) {
823 $fieldSetup = $conf;
824 $out = '';
825 if ($fieldSetup['type'] == 'files') {
826 $d = dir(PATH_site . $fieldSetup['uploadfolder']);
827 while (FALSE !== ($entry = $d->read())) {
828 if ($entry == '.' || $entry == '..') {
829 continue;
830 }
831 $fileArray[] = $entry;
832 }
833 $d->close();
834 natcasesort($fileArray);
835 while (list(, $fileName) = each($fileArray)) {
836 if (t3lib_div::inList($fV, $fileName) || $fV == $fileName) {
837 if (!$out) {
838 $out = htmlspecialchars($fileName);
839 } else {
840 $out .= $splitString . htmlspecialchars($fileName);
841 }
842 }
843 }
844 }
845 if ($fieldSetup['type'] == 'multiple') {
846 foreach ($fieldSetup['items'] as $key => $val) {
847 if (substr($val[0], 0, 4) == 'LLL:') {
848 $value = $GLOBALS['LANG']->sL($val[0]);
849 } else {
850 $value = $val[0];
851 }
852 if (t3lib_div::inList($fV, $val[1]) || $fV == $val[1]) {
853 if (!$out) {
854 $out = htmlspecialchars($value);
855 } else {
856 $out .= $splitString . htmlspecialchars($value);
857 }
858 }
859 }
860 }
861 if ($fieldSetup['type'] == 'binary') {
862 foreach ($fieldSetup['items'] as $Key => $val) {
863 if (substr($val[0], 0, 4) == 'LLL:') {
864 $value = $GLOBALS['LANG']->sL($val[0]);
865 } else {
866 $value = $val[0];
867 }
868 if (!$out) {
869 $out = htmlspecialchars($value);
870 } else {
871 $out .= $splitString . htmlspecialchars($value);
872 }
873 }
874 }
875 if ($fieldSetup['type'] == 'relation') {
876 if ($fieldSetup['items']) {
877 foreach ($fieldSetup['items'] as $key => $val) {
878 if (substr($val[0], 0, 4) == 'LLL:') {
879 $value = $GLOBALS['LANG']->sL($val[0]);
880 } else {
881 $value = $val[0];
882 }
883 if (t3lib_div::inList($fV, $value) || $fV == $value) {
884 if (!$out) {
885 $out = htmlspecialchars($value);
886 } else {
887 $out .= $splitString . htmlspecialchars($value);
888 }
889 }
890 }
891 }
892 if (stristr($fieldSetup['allowed'], ',')) {
893 $from_table_Arr = explode(',', $fieldSetup['allowed']);
894 $useTablePrefix = 1;
895 if (!$fieldSetup['prepend_tname']) {
896 $checkres = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fN, $table, 'uid ' . t3lib_BEfunc::deleteClause($table), $groupBy = '', $orderBy = '', $limit = '');
897 if ($checkres) {
898 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($checkres)) {
899 if (stristr($row[$fN], ',')) {
900 $checkContent = explode(',', $row[$fN]);
901 foreach ($checkContent as $singleValue) {
902 if (!stristr($singleValue, '_')) {
903 $dontPrefixFirstTable = 1;
904 }
905 }
906 } else {
907 $singleValue = $row[$fN];
908 if (strlen($singleValue) && !stristr($singleValue, '_')) {
909 $dontPrefixFirstTable = 1;
910 }
911 }
912 }
913 $GLOBALS['TYPO3_DB']->sql_free_result($checkres);
914 }
915 }
916 } else {
917 $from_table_Arr[0] = $fieldSetup['allowed'];
918 }
919 if ($fieldSetup['prepend_tname']) {
920 $useTablePrefix = 1;
921 }
922 if ($fieldSetup['foreign_table']) {
923 $from_table_Arr[0] = $fieldSetup['foreign_table'];
924 }
925 $counter = 0;
926 foreach ($from_table_Arr as $from_table) {
927 if (($useTablePrefix && !$dontPrefixFirstTable && $counter != 1) || $counter == 1) {
928 $tablePrefix = $from_table . '_';
929 }
930 $counter = 1;
931 if (is_array($GLOBALS['TCA'][$from_table])) {
932 t3lib_div::loadTCA($from_table);
933 $labelField = $GLOBALS['TCA'][$from_table]['ctrl']['label'];
934 $altLabelField = $GLOBALS['TCA'][$from_table]['ctrl']['label_alt'];
935 if ($GLOBALS['TCA'][$from_table]['columns'][$labelField]['config']['items']) {
936 foreach ($GLOBALS['TCA'][$from_table]['columns'][$labelField]['config']['items'] as $labelArray) {
937 if (substr($labelArray[0], 0, 4) == 'LLL:') {
938 $labelFieldSelect[$labelArray[1]] = $GLOBALS['LANG']->sL($labelArray[0]);
939 } else {
940 $labelFieldSelect[$labelArray[1]] = $labelArray[0];
941 }
942 }
943 $useSelectLabels = 1;
944 }
945 if ($GLOBALS['TCA'][$from_table]['columns'][$altLabelField]['config']['items']) {
946 foreach ($GLOBALS['TCA'][$from_table]['columns'][$altLabelField]['config']['items'] as $altLabelArray) {
947 if (substr($altLabelArray[0], 0, 4) == 'LLL:') {
948 $altLabelFieldSelect[$altLabelArray[1]] = $GLOBALS['LANG']->sL($altLabelArray[0]);
949 } else {
950 $altLabelFieldSelect[$altLabelArray[1]] = $altLabelArray[0];
951 }
952 }
953 $useAltSelectLabels = 1;
954 }
955 $altLabelFieldSelect = $altLabelField ? ',' . $altLabelField : '';
956 $select_fields = 'uid,' . $labelField . $altLabelFieldSelect;
957 if (!$GLOBALS['BE_USER']->isAdmin() && $GLOBALS['TYPO3_CONF_VARS']['BE']['lockBeUserToDBmounts']) {
958 $webMounts = $GLOBALS['BE_USER']->returnWebmounts();
959 $perms_clause = $GLOBALS['BE_USER']->getPagePermsClause(1);
960 $webMountPageTree = '';
961 foreach ($webMounts as $key => $val) {
962 if ($webMountPageTree) {
963 $webMountPageTreePrefix = ',';
964 }
965 $webMountPageTree .= $webMountPageTreePrefix . $this->getTreeList($val, 999, $begin = 0, $perms_clause);
966 }
967 if ($from_table == 'pages') {
968 $where_clause = 'uid IN (' . $webMountPageTree . ') ' . t3lib_BEfunc::deleteClause($from_table) . ' AND ' . $perms_clause;
969 } else {
970 $where_clause = 'pid IN (' . $webMountPageTree . ') ' . t3lib_BEfunc::deleteClause($from_table);
971 }
972 } else {
973 $where_clause = 'uid' . t3lib_BEfunc::deleteClause($from_table);
974 }
975 $orderBy = 'uid';
976 if (!$this->tableArray[$from_table]) {
977 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($select_fields, $from_table, $where_clause, $groupBy = '', $orderBy, $limit = '');
978 $this->tableArray[$from_table] = array();
979 }
980 if ($res) {
981 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
982 $this->tableArray[$from_table][] = $row;
983 }
984 $GLOBALS['TYPO3_DB']->sql_free_result($res);
985 }
986 foreach ($this->tableArray[$from_table] as $key => $val) {
987 $GLOBALS['SOBE']->MOD_SETTINGS['labels_noprefix'] = $GLOBALS['SOBE']->MOD_SETTINGS['labels_noprefix'] == 1 ? 'on' :
988 $GLOBALS['SOBE']->MOD_SETTINGS['labels_noprefix'];
989 $prefixString = $GLOBALS['SOBE']->MOD_SETTINGS['labels_noprefix'] == 'on' ? '' : ' [' . $tablePrefix . $val['uid'] . '] ';
990 if (t3lib_div::inList($fV, $tablePrefix . $val['uid']) || $fV == $tablePrefix . $val['uid']) {
991 if ($useSelectLabels) {
992 if (!$out) {
993 $out = htmlspecialchars($prefixString . $labelFieldSelect[$val[$labelField]]);
994 } else {
995 $out .= $splitString . htmlspecialchars($prefixString . $labelFieldSelect[$val[$labelField]]);
996 }
997 } elseif ($val[$labelField]) {
998 if (!$out) {
999 $out = htmlspecialchars($prefixString . $val[$labelField]);
1000 } else {
1001 $out .= $splitString . htmlspecialchars($prefixString . $val[$labelField]);
1002 }
1003 } elseif ($useAltSelectLabels) {
1004 if (!$out) {
1005 $out = htmlspecialchars($prefixString . $altLabelFieldSelect[$val[$altLabelField]]);
1006 } else {
1007 $out .= $splitString . htmlspecialchars($prefixString . $altLabelFieldSelect[$val[$altLabelField]]);
1008 }
1009 } else {
1010 if (!$out) {
1011 $out = htmlspecialchars($prefixString . $val[$altLabelField]);
1012 } else {
1013 $out .= $splitString . htmlspecialchars($prefixString . $val[$altLabelField]);
1014 }
1015 }
1016 }
1017 }
1018 }
1019 }
1020 }
1021 return $out;
1022 }
1023
1024 /**
1025 * Render table header
1026 *
1027 * @param array $row Table columns
1028 * @param array $conf Table TCA
1029 * @param string $table Table name
1030 * @return string HTML of table header
1031 */
1032 function resultRowTitles($row, $conf, $table) {
1033 $SET = $GLOBALS['SOBE']->MOD_SETTINGS;
1034
1035 $tableHeader = array();
1036
1037 // Start header row
1038 $tableHeader[] = '<thead><tr class="bgColor5">';
1039
1040 // Iterate over given columns
1041 foreach ($row as $fieldName => $fieldValue) {
1042 if (t3lib_div::inList($SET['queryFields'], $fieldName) || (!$SET['queryFields'] && $fieldName != 'pid' && $fieldName != 'deleted')) {
1043 $THparams = (strlen($fieldValue) < 50) ? ' style="white-space:nowrap;"' : '';
1044
1045 if ($GLOBALS['SOBE']->MOD_SETTINGS['search_result_labels']) {
1046 $title = $GLOBALS['LANG']->sL($conf['columns'][$fieldName]['label'] ? $conf['columns'][$fieldName]['label'] : $fieldName, 1);
1047 } else {
1048 $title = $GLOBALS['LANG']->sL($fieldName, 1);
1049 }
1050
1051 $tableHeader[] = '<th' . $THparams . '>' . $title . '</th>';
1052 }
1053 }
1054
1055 // Add empty icon column
1056 $tableHeader[] = '<th style="white-space:nowrap;"></th>';
1057 // Close header row
1058 $tableHeader[] = '</tr></thead>';
1059
1060 return implode($tableHeader, LF);
1061 }
1062
1063 /**
1064 * [Describe function...]
1065 *
1066 * @param array $row
1067 * @param array $conf
1068 * @param mixed $table Not used
1069 * @return string
1070 */
1071 function csvRowTitles($row, $conf, $table) {
1072 $out = '';
1073 $SET = $GLOBALS['SOBE']->MOD_SETTINGS;
1074 foreach ($row as $fN => $fV) {
1075 if (t3lib_div::inList($SET['queryFields'], $fN) || (!$SET['queryFields'] && $fN != 'pid')) {
1076 if (!$out) {
1077 if ($GLOBALS['SOBE']->MOD_SETTINGS['search_result_labels']) {
1078 $out = $GLOBALS['LANG']->sL($conf['columns'][$fN]['label'] ? $conf['columns'][$fN]['label'] : $fN, 1);
1079 } else {
1080 $out = $GLOBALS['LANG']->sL($fN, 1);
1081 }
1082 } else {
1083 if ($GLOBALS['SOBE']->MOD_SETTINGS['search_result_labels']) {
1084 $out .= ',' . $GLOBALS['LANG']->sL($conf['columns'][$fN]['label'] ? $conf['columns'][$fN]['label'] : $fN, 1);
1085 } else {
1086 $out .= ',' . $GLOBALS['LANG']->sL($fN, 1);
1087 }
1088 }
1089 }
1090 }
1091 return $out;
1092 }
1093
1094 /**
1095 * Sets the current name of the input form.
1096 *
1097 * @param string $formName The name of the form.
1098 * @return void
1099 */
1100 public function setFormName($formName) {
1101 $this->formName = trim($formName);
1102 }
1103 }
1104
1105 ?>