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