TYPO3 CMS  TYPO3_7-6
QueryView.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 
26 class QueryView
27 {
31  public $storeList = 'search_query_smallparts,search_result_labels,labels_noprefix,show_deleted,queryConfig,queryTable,queryFields,queryLimit,queryOrder,queryOrderDesc,queryOrder2,queryOrder2Desc,queryGroup,search_query_makeQuery';
32 
36  public $downloadScript = 'index.php';
37 
41  public $formW = 48;
42 
46  public $noDownloadB = 0;
47 
51  public $hookArray = [];
52 
56  protected $formName = '';
57 
61  protected $iconFactory;
62 
66  public function __construct()
67  {
68  $GLOBALS['LANG']->includeLLFile('EXT:lang/locallang_t3lib_fullsearch.xlf');
69  $this->iconFactory = GeneralUtility::makeInstance(IconFactory::class);
70  }
71 
77  public function form()
78  {
79  return '
80  <div class="form-group">
81  <input placeholder="Search Word" class="form-control" type="search" name="SET[sword]" value="' . htmlspecialchars($GLOBALS['SOBE']->MOD_SETTINGS['sword']) . '">
82  </div>
83  <div class="form-group">
84  <input class="btn btn-default" type="submit" name="submit" value="Search All Records">
85  </div>
86  ';
87  }
88 
94  public function makeStoreControl()
95  {
96  // Load/Save
97  $storeArray = $this->initStoreArray();
98 
99  $opt = [];
100  foreach ($storeArray as $k => $v) {
101  $opt[] = '<option value="' . $k . '">' . htmlspecialchars($v) . '</option>';
102  }
103  // Actions:
104  if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('sys_action') && $GLOBALS['BE_USER']->isAdmin()) {
105  $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'sys_action', 'type=2', '', 'title');
106  $opt[] = '<option value="0">__Save to Action:__</option>';
107  foreach ($rows as $row) {
108  $opt[] = '<option value="-' . $row['uid'] . '">' . htmlspecialchars(($row['title'] . ' [' . $row['uid'] . ']')) . '</option>';
109  }
110  }
111  return '<div class="load-queries">
112  <div class="form-inline">
113  <div class="form-group">
114  <select class="form-control" 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>
115  <input class="btn btn-default" type="submit" name="storeControl[LOAD]" value="Load">
116  </div>
117  </div>
118  <div class="form-inline">
119  <div class="form-group">
120  <input name="storeControl[title]" value="" type="text" max="80" class="form-control">
121  <input class="btn btn-default" 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?\');">
122  <input class="btn btn-default" type="submit" name="storeControl[REMOVE]" value="Remove">
123  </div>
124  </div>
125  </div>';
126  }
127 
133  public function initStoreArray()
134  {
135  $storeArray = [
136  '0' => '[New]'
137  ];
138  $savedStoreArray = unserialize($GLOBALS['SOBE']->MOD_SETTINGS['storeArray']);
139  if (is_array($savedStoreArray)) {
140  $storeArray = array_merge($storeArray, $savedStoreArray);
141  }
142  return $storeArray;
143  }
144 
152  public function cleanStoreQueryConfigs($storeQueryConfigs, $storeArray)
153  {
154  if (is_array($storeQueryConfigs)) {
155  foreach ($storeQueryConfigs as $k => $v) {
156  if (!isset($storeArray[$k])) {
157  unset($storeQueryConfigs[$k]);
158  }
159  }
160  }
161  return $storeQueryConfigs;
162  }
163 
171  public function addToStoreQueryConfigs($storeQueryConfigs, $index)
172  {
173  $keyArr = explode(',', $this->storeList);
174  $storeQueryConfigs[$index] = [];
175  foreach ($keyArr as $k) {
176  $storeQueryConfigs[$index][$k] = $GLOBALS['SOBE']->MOD_SETTINGS[$k];
177  }
178  return $storeQueryConfigs;
179  }
180 
187  public function saveQueryInAction($uid)
188  {
189  if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('sys_action')) {
190  $keyArr = explode(',', $this->storeList);
191  $saveArr = [];
192  foreach ($keyArr as $k) {
193  $saveArr[$k] = $GLOBALS['SOBE']->MOD_SETTINGS[$k];
194  }
195  $qOK = 0;
196  // Show query
197  if ($saveArr['queryTable']) {
199  $qGen = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\QueryGenerator::class);
200  $qGen->init('queryConfig', $saveArr['queryTable']);
201  $qGen->makeSelectorTable($saveArr);
202  $qGen->enablePrefix = 1;
203  $qString = $qGen->getQuery($qGen->queryConfig);
204  $qCount = $GLOBALS['TYPO3_DB']->SELECTquery('count(*)', $qGen->table, $qString . BackendUtility::deleteClause($qGen->table));
205  $qSelect = $qGen->getSelectQuery($qString);
206  $res = @$GLOBALS['TYPO3_DB']->sql_query($qCount);
207  if (!$GLOBALS['TYPO3_DB']->sql_error()) {
208  $GLOBALS['TYPO3_DB']->sql_free_result($res);
209  $dA = [];
210  $dA['t2_data'] = serialize([
211  'qC' => $saveArr,
212  'qCount' => $qCount,
213  'qSelect' => $qSelect,
214  'qString' => $qString
215  ]);
216  $GLOBALS['TYPO3_DB']->exec_UPDATEquery('sys_action', 'uid=' . (int)$uid, $dA);
217  $qOK = 1;
218  }
219  }
220  return $qOK;
221  }
222  }
223 
232  public function loadStoreQueryConfigs($storeQueryConfigs, $storeIndex, $writeArray)
233  {
234  if ($storeQueryConfigs[$storeIndex]) {
235  $keyArr = explode(',', $this->storeList);
236  foreach ($keyArr as $k) {
237  $writeArray[$k] = $storeQueryConfigs[$storeIndex][$k];
238  }
239  }
240  return $writeArray;
241  }
242 
248  public function procesStoreControl()
249  {
250  $storeArray = $this->initStoreArray();
251  $storeQueryConfigs = unserialize($GLOBALS['SOBE']->MOD_SETTINGS['storeQueryConfigs']);
252  $storeControl = GeneralUtility::_GP('storeControl');
253  $storeIndex = (int)$storeControl['STORE'];
254  $saveStoreArray = 0;
255  $writeArray = [];
256  if (is_array($storeControl)) {
257  $msg = '';
258  if ($storeControl['LOAD']) {
259  if ($storeIndex > 0) {
260  $writeArray = $this->loadStoreQueryConfigs($storeQueryConfigs, $storeIndex, $writeArray);
261  $saveStoreArray = 1;
262  $flashMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessage::class, sprintf($GLOBALS['LANG']->getLL('query_loaded'), htmlspecialchars($storeArray[$storeIndex])));
263  } elseif ($storeIndex < 0 && \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('sys_action')) {
264  $actionRecord = BackendUtility::getRecord('sys_action', abs($storeIndex));
265  if (is_array($actionRecord)) {
266  $dA = unserialize($actionRecord['t2_data']);
267  $dbSC = [];
268  if (is_array($dA['qC'])) {
269  $dbSC[0] = $dA['qC'];
270  }
271  $writeArray = $this->loadStoreQueryConfigs($dbSC, '0', $writeArray);
272  $saveStoreArray = 1;
273  $flashMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessage::class, sprintf($GLOBALS['LANG']->getLL('query_from_action_loaded'), htmlspecialchars($actionRecord['title'])));
274  }
275  }
276  } elseif ($storeControl['SAVE']) {
277  if ($storeIndex < 0) {
278  $qOK = $this->saveQueryInAction(abs($storeIndex));
279  if ($qOK) {
280  $flashMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessage::class, $GLOBALS['LANG']->getLL('query_saved'));
281  } else {
282  $flashMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessage::class, $GLOBALS['LANG']->getLL('query_notsaved'), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
283  }
284  } else {
285  if (trim($storeControl['title'])) {
286  if ($storeIndex > 0) {
287  $storeArray[$storeIndex] = $storeControl['title'];
288  } else {
289  $storeArray[] = $storeControl['title'];
290  end($storeArray);
291  $storeIndex = key($storeArray);
292  }
293  $storeQueryConfigs = $this->addToStoreQueryConfigs($storeQueryConfigs, $storeIndex);
294  $saveStoreArray = 1;
295  $flashMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessage::class, $GLOBALS['LANG']->getLL('query_saved'));
296  }
297  }
298  } elseif ($storeControl['REMOVE']) {
299  if ($storeIndex > 0) {
300  $flashMessage = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Messaging\FlashMessage::class, sprintf($GLOBALS['LANG']->getLL('query_removed'), htmlspecialchars($storeArray[$storeControl['STORE']])));
301  // Removing
302  unset($storeArray[$storeControl['STORE']]);
303  $saveStoreArray = 1;
304  }
305  }
306  if ($flashMessage) {
307  $msg = $flashMessage->render();
308  }
309  }
310  if ($saveStoreArray) {
311  // Making sure, index 0 is not set!
312  unset($storeArray[0]);
313  $writeArray['storeArray'] = serialize($storeArray);
314  $writeArray['storeQueryConfigs'] = serialize($this->cleanStoreQueryConfigs($storeQueryConfigs, $storeArray));
315  $GLOBALS['SOBE']->MOD_SETTINGS = BackendUtility::getModuleData($GLOBALS['SOBE']->MOD_MENU, $writeArray, $GLOBALS['SOBE']->MCONF['name'], 'ses');
316  }
317  return $msg;
318  }
319 
325  public function queryMaker()
326  {
327  $output = '';
328  if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3lib_fullsearch'])) {
329  $this->hookArray = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['t3lib_fullsearch'];
330  }
331  $msg = $this->procesStoreControl();
332  if (!$GLOBALS['BE_USER']->userTS['mod.']['dbint.']['disableStoreControl']) {
333  $output .= '<h2>Load/Save Query</h2><div>' . $this->makeStoreControl() . '</div>';
334  if ($msg) {
335  $output .= '<br />' . $msg;
336  }
337  $output .= '<div style="padding-top: 20px;"></div>';
338  }
339  // Query Maker:
340  $qGen = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\QueryGenerator::class);
341  $qGen->init('queryConfig', $GLOBALS['SOBE']->MOD_SETTINGS['queryTable']);
342  if ($this->formName) {
343  $qGen->setFormName($this->formName);
344  }
345  $tmpCode = $qGen->makeSelectorTable($GLOBALS['SOBE']->MOD_SETTINGS);
346  $output .= '<div id="query"></div>' . '<h2>Make query</h2><div>' . $tmpCode . '</div>';
347  $mQ = $GLOBALS['SOBE']->MOD_SETTINGS['search_query_makeQuery'];
348  // Make form elements:
349  if ($qGen->table && is_array($GLOBALS['TCA'][$qGen->table])) {
350  if ($mQ) {
351  // Show query
352  $qGen->enablePrefix = 1;
353  $qString = $qGen->getQuery($qGen->queryConfig);
354  switch ($mQ) {
355  case 'count':
356  $qExplain = $GLOBALS['TYPO3_DB']->SELECTquery('count(*)', $qGen->table, $qString . BackendUtility::deleteClause($qGen->table));
357  break;
358  default:
359  $qExplain = $qGen->getSelectQuery($qString);
360  if ($mQ == 'explain') {
361  $qExplain = 'EXPLAIN ' . $qExplain;
362  }
363  }
364  if (!$GLOBALS['BE_USER']->userTS['mod.']['dbint.']['disableShowSQLQuery']) {
365  $output .= '<h2>SQL query</h2><div>' . $this->tableWrap(htmlspecialchars($qExplain)) . '</div>';
366  }
367  $res = @$GLOBALS['TYPO3_DB']->sql_query($qExplain);
368  if ($GLOBALS['TYPO3_DB']->sql_error()) {
369  $out = '<BR><strong>Error:</strong><BR><font color="red"><strong>' . $GLOBALS['TYPO3_DB']->sql_error() . '</strong></font>';
370  $output .= '<h2>SQL error</h2><div>' . $out . '</div>';
371  } else {
372  $cPR = $this->getQueryResultCode($mQ, $res, $qGen->table);
373  $GLOBALS['TYPO3_DB']->sql_free_result($res);
374  $output .= '<h2>' . $cPR['header'] . '</h2><div>' . $cPR['content'] . '</div>';
375  }
376  }
377  }
378  return '<div class="query-builder">' . $output . '</div>';
379  }
380 
389  public function getQueryResultCode($mQ, $res, $table)
390  {
391  $out = '';
392  $cPR = [];
393  switch ($mQ) {
394  case 'count':
395  $row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
396  $cPR['header'] = 'Count';
397  $cPR['content'] = '<BR><strong>' . $row[0] . '</strong> records selected.';
398  break;
399  case 'all':
400  $rowArr = [];
401  while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
402  $rowArr[] = $this->resultRowDisplay($row, $GLOBALS['TCA'][$table], $table);
403  $lrow = $row;
404  }
405  if (is_array($this->hookArray['beforeResultTable'])) {
406  foreach ($this->hookArray['beforeResultTable'] as $_funcRef) {
407  $out .= GeneralUtility::callUserFunction($_funcRef, $GLOBALS['SOBE']->MOD_SETTINGS, $this);
408  }
409  }
410  if (!empty($rowArr)) {
411  $out .= '<table class="table table-striped table-hover">' . $this->resultRowTitles($lrow, $GLOBALS['TCA'][$table], $table) . implode(LF, $rowArr) . '</table>';
412  }
413  if (!$out) {
414  $out = '<div class="alert-info">No rows selected!</div>';
415  }
416  $cPR['header'] = 'Result';
417  $cPR['content'] = $out;
418  break;
419  case 'csv':
420  $rowArr = [];
421  $first = 1;
422  while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
423  if ($first) {
424  $rowArr[] = $this->csvValues(array_keys($row), ',', '');
425  $first = 0;
426  }
427  $rowArr[] = $this->csvValues($row, ',', '"', $GLOBALS['TCA'][$table], $table);
428  }
429  if (!empty($rowArr)) {
430  $out .= '<textarea name="whatever" rows="20" class="text-monospace" style="width:100%">'
431  . htmlspecialchars(implode(LF, $rowArr))
432  . '</textarea>';
433  if (!$this->noDownloadB) {
434  $out .= '<br><input class="btn btn-default" type="submit" name="download_file" value="Click to download file" onClick="window.location.href=\'' . $this->downloadScript . '\';">';
435  }
436  // Downloads file:
437  if (GeneralUtility::_GP('download_file')) {
438  $filename = 'TYPO3_' . $table . '_export_' . date('dmy-Hi') . '.csv';
439  $mimeType = 'application/octet-stream';
440  header('Content-Type: ' . $mimeType);
441  header('Content-Disposition: attachment; filename=' . $filename);
442  echo implode(CRLF, $rowArr);
443  die;
444  }
445  }
446  if (!$out) {
447  $out = '<em>No rows selected!</em>';
448  }
449  $cPR['header'] = 'Result';
450  $cPR['content'] = $out;
451  break;
452  case 'explain':
453 
454  default:
455  while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
456  $out .= '<br />' . \TYPO3\CMS\Core\Utility\DebugUtility::viewArray($row);
457  }
458  $cPR['header'] = 'Explain SQL query';
459  $cPR['content'] = $out;
460  }
461  return $cPR;
462  }
463 
474  public function csvValues($row, $delim = ',', $quote = '"', $conf = [], $table = '')
475  {
476  $valueArray = $row;
477  if ($GLOBALS['SOBE']->MOD_SETTINGS['search_result_labels'] && $table) {
478  foreach ($valueArray as $key => $val) {
479  $valueArray[$key] = $this->getProcessedValueExtra($table, $key, $val, $conf, ';');
480  }
481  }
482  return GeneralUtility::csvValues($valueArray, $delim, $quote);
483  }
484 
491  public function tableWrap($str)
492  {
493  return '<pre>' . $str . '</pre>';
494  }
495 
501  public function search()
502  {
503  $SET = $GLOBALS['SOBE']->MOD_SETTINGS;
504  $swords = $SET['sword'];
505  $out = '';
506  $limit = 200;
507  if ($swords) {
508  foreach ($GLOBALS['TCA'] as $table => $value) {
509  // Get fields list
510  $conf = $GLOBALS['TCA'][$table];
511  // Avoid querying tables with no columns
512  if (empty($conf['columns'])) {
513  continue;
514  }
515  $fieldsInDatabase = $GLOBALS['TYPO3_DB']->admin_get_fields($table);
516  $list = array_intersect(array_keys($conf['columns']), array_keys($fieldsInDatabase));
517  // Get query
518  $qp = $GLOBALS['TYPO3_DB']->searchQuery([$swords], $list, $table);
519  // Count:
520  $count = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows('*', $table, $qp . BackendUtility::deleteClause($table));
521  if ($count) {
522  $rowArr = [];
523  $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,' . $conf['ctrl']['label'], $table, $qp . BackendUtility::deleteClause($table), '', '', $limit);
524  while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
525  $rowArr[] = $this->resultRowDisplay($row, $conf, $table);
526  $lrow = $row;
527  }
528  $GLOBALS['TYPO3_DB']->sql_free_result($res);
529  $out .= '<div class="panel panel-default">
530  <div class="panel-heading">' . $GLOBALS['LANG']->sL($conf['ctrl']['title'], true) . ' (' . $count . ')</div>
531  <table class="table table-striped table-hover">' .
532  $this->resultRowTitles($lrow, $conf, $table) .
533  implode(LF, $rowArr) .
534  '</table>
535  </div>';
536  }
537  }
538  }
539  return $out;
540  }
541 
550  public function resultRowDisplay($row, $conf, $table)
551  {
552  $SET = $GLOBALS['SOBE']->MOD_SETTINGS;
553  $out = '<tr>';
554  foreach ($row as $fieldName => $fieldValue) {
555  if (GeneralUtility::inList($SET['queryFields'], $fieldName) || !$SET['queryFields'] && $fieldName != 'pid' && $fieldName != 'deleted') {
556  if ($SET['search_result_labels']) {
557  $fVnew = $this->getProcessedValueExtra($table, $fieldName, $fieldValue, $conf, '<br />');
558  } else {
559  $fVnew = htmlspecialchars($fieldValue);
560  }
561  $out .= '<td>' . $fVnew . '</td>';
562  }
563  }
564  $out .= '<td><div class="btn-group">';
565  if (!$row['deleted']) {
566  $url = BackendUtility::getModuleUrl('record_edit', [
567  'edit' => [
568  $table => [
569  $row['uid'] => 'edit'
570  ]
571  ],
572  'returnUrl' => GeneralUtility::getIndpEnv('REQUEST_URI') . GeneralUtility::implodeArrayForUrl('SET', (array)GeneralUtility::_POST('SET'))
573  ]);
574  $out .= '<a class="btn btn-default" href="#" onClick="top.launchView(\'' . $table . '\',' . $row['uid'] . ',\'' . $GLOBALS['BACK_PATH'] . '\');return false;">' . $this->iconFactory->getIcon('actions-document-info', Icon::SIZE_SMALL)->render() . '</a>';
575  $out .= '<a class="btn btn-default" href="' . htmlspecialchars($url) . '">' . $this->iconFactory->getIcon('actions-document-open', Icon::SIZE_SMALL)->render() . '</a>';
576  } else {
577  $out .= '<a class="btn btn-default" href="' . GeneralUtility::linkThisUrl(BackendUtility::getModuleUrl('tce_db'), [
578  ('cmd[' . $table . '][' . $row['uid'] . '][undelete]') => '1',
579  'redirect' => GeneralUtility::linkThisScript([])
580  ]) . '" title="' . $GLOBALS['LANG']->getLL('undelete_only', true) . '">';
581  $out .= $this->iconFactory->getIcon('actions-edit-restore', Icon::SIZE_SMALL)->render() . '</a>';
582  $formEngineParameters = [
583  'edit[' . $table . '][' . $row['uid'] . ']' => 'edit',
584  'returnUrl' => GeneralUtility::linkThisScript([])
585  ];
586  $redirectUrl = BackendUtility::getModuleUrl('record_edit', $formEngineParameters);
587  $out .= '<a class="btn btn-default" href="' . GeneralUtility::linkThisUrl(BackendUtility::getModuleUrl('tce_db'), [
588  ('cmd[' . $table . '][' . $row['uid'] . '][undelete]') => '1',
589  'redirect' => $redirectUrl
590  ]) . '" title="' . $GLOBALS['LANG']->getLL('undelete_and_edit', true) . '">';
591  $out .= $this->iconFactory->getIcon('actions-edit-restore-edit', Icon::SIZE_SMALL)->render() . '</a>';
592  }
593  $_params = [$table => $row];
594  if (is_array($this->hookArray['additionalButtons'])) {
595  foreach ($this->hookArray['additionalButtons'] as $_funcRef) {
596  $out .= GeneralUtility::callUserFunction($_funcRef, $_params, $this);
597  }
598  }
599  $out .= '</div></td>
600  </tr>
601  ';
602  return $out;
603  }
604 
615  public function getProcessedValueExtra($table, $fieldName, $fieldValue, $conf, $splitString)
616  {
617  $out = '';
618  // Analysing the fields in the table.
619  if (is_array($GLOBALS['TCA'][$table])) {
620  $fC = $GLOBALS['TCA'][$table]['columns'][$fieldName];
621  $fields = $fC['config'];
622  $fields['exclude'] = $fC['exclude'];
623  if (is_array($fC) && $fC['label']) {
624  $fields['label'] = preg_replace('/:$/', '', trim($GLOBALS['LANG']->sL($fC['label'])));
625  switch ($fields['type']) {
626  case 'input':
627  if (preg_match('/int|year/i', $fields['eval'])) {
628  $fields['type'] = 'number';
629  } elseif (preg_match('/time/i', $fields['eval'])) {
630  $fields['type'] = 'time';
631  } elseif (preg_match('/date/i', $fields['eval'])) {
632  $fields['type'] = 'date';
633  } else {
634  $fields['type'] = 'text';
635  }
636  break;
637  case 'check':
638  if (!$fields['items']) {
639  $fields['type'] = 'boolean';
640  } else {
641  $fields['type'] = 'binary';
642  }
643  break;
644  case 'radio':
645  $fields['type'] = 'multiple';
646  break;
647  case 'select':
648  $fields['type'] = 'multiple';
649  if ($fields['foreign_table']) {
650  $fields['type'] = 'relation';
651  }
652  if ($fields['special']) {
653  $fields['type'] = 'text';
654  }
655  break;
656  case 'group':
657  $fields['type'] = 'files';
658  if ($fields['internal_type'] == 'db') {
659  $fields['type'] = 'relation';
660  }
661  break;
662  case 'user':
663  case 'flex':
664  case 'passthrough':
665  case 'none':
666  case 'text':
667  default:
668  $fields['type'] = 'text';
669  }
670  } else {
671  $fields['label'] = '[FIELD: ' . $fieldName . ']';
672  switch ($fieldName) {
673  case 'pid':
674  $fields['type'] = 'relation';
675  $fields['allowed'] = 'pages';
676  break;
677  case 'cruser_id':
678  $fields['type'] = 'relation';
679  $fields['allowed'] = 'be_users';
680  break;
681  case 'tstamp':
682 
683  case 'crdate':
684  $fields['type'] = 'time';
685  break;
686  default:
687  $fields['type'] = 'number';
688  }
689  }
690  }
691  switch ($fields['type']) {
692  case 'date':
693  if ($fieldValue != -1) {
694  $out = strftime('%d-%m-%Y', $fieldValue);
695  }
696  break;
697  case 'time':
698  if ($fieldValue != -1) {
699  if ($splitString == '<br />') {
700  $out = strftime('%H:%M' . $splitString . '%d-%m-%Y', $fieldValue);
701  } else {
702  $out = strftime('%H:%M %d-%m-%Y', $fieldValue);
703  }
704  }
705  break;
706  case 'multiple':
707  case 'binary':
708  case 'relation':
709  $out = $this->makeValueList($fieldName, $fieldValue, $fields, $table, $splitString);
710  break;
711  case 'boolean':
712  $out = $fieldValue ? 'True' : 'False';
713  break;
714  case 'files':
715  default:
716  $out = htmlspecialchars($fieldValue);
717  }
718  return $out;
719  }
720 
730  public function getTreeList($id, $depth, $begin = 0, $perms_clause)
731  {
732  $depth = (int)$depth;
733  $begin = (int)$begin;
734  $id = (int)$id;
735  if ($id < 0) {
736  $id = abs($id);
737  }
738  if ($begin == 0) {
739  $theList = $id;
740  } else {
741  $theList = '';
742  }
743  if ($id && $depth > 0) {
744  $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid', 'pages', 'pid=' . $id . ' ' . BackendUtility::deleteClause('pages') . ' AND ' . $perms_clause);
745  while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
746  if ($begin <= 0) {
747  $theList .= ',' . $row['uid'];
748  }
749  if ($depth > 1) {
750  $theList .= $this->getTreeList($row['uid'], $depth - 1, $begin - 1, $perms_clause);
751  }
752  }
753  $GLOBALS['TYPO3_DB']->sql_free_result($res);
754  }
755  return $theList;
756  }
757 
768  public function makeValueList($fieldName, $fieldValue, $conf, $table, $splitString)
769  {
770  $fieldSetup = $conf;
771  $out = '';
772  if ($fieldSetup['type'] == 'files') {
773  $d = dir(PATH_site . $fieldSetup['uploadfolder']);
774  while (false !== ($entry = $d->read())) {
775  if ($entry == '.' || $entry == '..') {
776  continue;
777  }
778  $fileArray[] = $entry;
779  }
780  $d->close();
781  natcasesort($fileArray);
782  foreach ($fileArray as $fileName) {
783  if (GeneralUtility::inList($fieldValue, $fileName) || $fieldValue == $fileName) {
784  if (!$out) {
785  $out = htmlspecialchars($fileName);
786  } else {
787  $out .= $splitString . htmlspecialchars($fileName);
788  }
789  }
790  }
791  }
792  if ($fieldSetup['type'] == 'multiple') {
793  foreach ($fieldSetup['items'] as $key => $val) {
794  if (substr($val[0], 0, 4) == 'LLL:') {
795  $value = $GLOBALS['LANG']->sL($val[0]);
796  } else {
797  $value = $val[0];
798  }
799  if (GeneralUtility::inList($fieldValue, $val[1]) || $fieldValue == $val[1]) {
800  if (!$out) {
801  $out = htmlspecialchars($value);
802  } else {
803  $out .= $splitString . htmlspecialchars($value);
804  }
805  }
806  }
807  }
808  if ($fieldSetup['type'] == 'binary') {
809  foreach ($fieldSetup['items'] as $Key => $val) {
810  if (substr($val[0], 0, 4) == 'LLL:') {
811  $value = $GLOBALS['LANG']->sL($val[0]);
812  } else {
813  $value = $val[0];
814  }
815  if (!$out) {
816  $out = htmlspecialchars($value);
817  } else {
818  $out .= $splitString . htmlspecialchars($value);
819  }
820  }
821  }
822  if ($fieldSetup['type'] == 'relation') {
823  if ($fieldSetup['items']) {
824  foreach ($fieldSetup['items'] as $key => $val) {
825  if (substr($val[0], 0, 4) == 'LLL:') {
826  $value = $GLOBALS['LANG']->sL($val[0]);
827  } else {
828  $value = $val[0];
829  }
830  if (GeneralUtility::inList($fieldValue, $value) || $fieldValue == $value) {
831  if (!$out) {
832  $out = htmlspecialchars($value);
833  } else {
834  $out .= $splitString . htmlspecialchars($value);
835  }
836  }
837  }
838  }
839  if (stristr($fieldSetup['allowed'], ',')) {
840  $from_table_Arr = explode(',', $fieldSetup['allowed']);
841  $useTablePrefix = 1;
842  if (!$fieldSetup['prepend_tname']) {
843  $checkres = $GLOBALS['TYPO3_DB']->exec_SELECTquery($fieldName, $table, 'uid ' . BackendUtility::deleteClause($table), ($groupBy = ''), ($orderBy = ''), ($limit = ''));
844  if ($checkres) {
845  while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($checkres)) {
846  if (stristr($row[$fieldName], ',')) {
847  $checkContent = explode(',', $row[$fieldName]);
848  foreach ($checkContent as $singleValue) {
849  if (!stristr($singleValue, '_')) {
850  $dontPrefixFirstTable = 1;
851  }
852  }
853  } else {
854  $singleValue = $row[$fieldName];
855  if ($singleValue !== '' && !stristr($singleValue, '_')) {
856  $dontPrefixFirstTable = 1;
857  }
858  }
859  }
860  $GLOBALS['TYPO3_DB']->sql_free_result($checkres);
861  }
862  }
863  } else {
864  $from_table_Arr[0] = $fieldSetup['allowed'];
865  }
866  if ($fieldSetup['prepend_tname']) {
867  $useTablePrefix = 1;
868  }
869  if ($fieldSetup['foreign_table']) {
870  $from_table_Arr[0] = $fieldSetup['foreign_table'];
871  }
872  $counter = 0;
873  foreach ($from_table_Arr as $from_table) {
874  if ($useTablePrefix && !$dontPrefixFirstTable && $counter != 1 || $counter == 1) {
875  $tablePrefix = $from_table . '_';
876  }
877  $counter = 1;
878  if (is_array($GLOBALS['TCA'][$from_table])) {
879  $labelField = $GLOBALS['TCA'][$from_table]['ctrl']['label'];
880  $altLabelField = $GLOBALS['TCA'][$from_table]['ctrl']['label_alt'];
881  if ($GLOBALS['TCA'][$from_table]['columns'][$labelField]['config']['items']) {
882  foreach ($GLOBALS['TCA'][$from_table]['columns'][$labelField]['config']['items'] as $labelArray) {
883  if (substr($labelArray[0], 0, 4) == 'LLL:') {
884  $labelFieldSelect[$labelArray[1]] = $GLOBALS['LANG']->sL($labelArray[0]);
885  } else {
886  $labelFieldSelect[$labelArray[1]] = $labelArray[0];
887  }
888  }
889  $useSelectLabels = 1;
890  }
891  if ($GLOBALS['TCA'][$from_table]['columns'][$altLabelField]['config']['items']) {
892  foreach ($GLOBALS['TCA'][$from_table]['columns'][$altLabelField]['config']['items'] as $altLabelArray) {
893  if (substr($altLabelArray[0], 0, 4) == 'LLL:') {
894  $altLabelFieldSelect[$altLabelArray[1]] = $GLOBALS['LANG']->sL($altLabelArray[0]);
895  } else {
896  $altLabelFieldSelect[$altLabelArray[1]] = $altLabelArray[0];
897  }
898  }
899  $useAltSelectLabels = 1;
900  }
901  $altLabelFieldSelect = $altLabelField ? ',' . $altLabelField : '';
902  $select_fields = 'uid,' . $labelField . $altLabelFieldSelect;
903  if (!$GLOBALS['BE_USER']->isAdmin() && $GLOBALS['TYPO3_CONF_VARS']['BE']['lockBeUserToDBmounts']) {
904  $webMounts = $GLOBALS['BE_USER']->returnWebmounts();
905  $perms_clause = $GLOBALS['BE_USER']->getPagePermsClause(1);
906  $webMountPageTree = '';
907  foreach ($webMounts as $key => $val) {
908  if ($webMountPageTree) {
909  $webMountPageTreePrefix = ',';
910  }
911  $webMountPageTree .= $webMountPageTreePrefix . $this->getTreeList($val, 999, ($begin = 0), $perms_clause);
912  }
913  if ($from_table == 'pages') {
914  $where_clause = 'uid IN (' . $webMountPageTree . ') ' . BackendUtility::deleteClause($from_table) . ' AND ' . $perms_clause;
915  } else {
916  $where_clause = 'pid IN (' . $webMountPageTree . ') ' . BackendUtility::deleteClause($from_table);
917  }
918  } else {
919  $where_clause = 'uid' . BackendUtility::deleteClause($from_table);
920  }
921  $orderBy = 'uid';
922  if (!$this->tableArray[$from_table]) {
923  $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($select_fields, $from_table, $where_clause, ($groupBy = ''), $orderBy, ($limit = ''));
924  $this->tableArray[$from_table] = [];
925  }
926  if ($res) {
927  while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
928  $this->tableArray[$from_table][] = $row;
929  }
930  $GLOBALS['TYPO3_DB']->sql_free_result($res);
931  }
932  foreach ($this->tableArray[$from_table] as $key => $val) {
933  $GLOBALS['SOBE']->MOD_SETTINGS['labels_noprefix'] = $GLOBALS['SOBE']->MOD_SETTINGS['labels_noprefix'] == 1 ? 'on' : $GLOBALS['SOBE']->MOD_SETTINGS['labels_noprefix'];
934  $prefixString = $GLOBALS['SOBE']->MOD_SETTINGS['labels_noprefix'] == 'on' ? '' : ' [' . $tablePrefix . $val['uid'] . '] ';
935  if (GeneralUtility::inList($fieldValue, $tablePrefix . $val['uid']) || $fieldValue == $tablePrefix . $val['uid']) {
936  if ($useSelectLabels) {
937  if (!$out) {
938  $out = htmlspecialchars($prefixString . $labelFieldSelect[$val[$labelField]]);
939  } else {
940  $out .= $splitString . htmlspecialchars(($prefixString . $labelFieldSelect[$val[$labelField]]));
941  }
942  } elseif ($val[$labelField]) {
943  if (!$out) {
944  $out = htmlspecialchars($prefixString . $val[$labelField]);
945  } else {
946  $out .= $splitString . htmlspecialchars(($prefixString . $val[$labelField]));
947  }
948  } elseif ($useAltSelectLabels) {
949  if (!$out) {
950  $out = htmlspecialchars($prefixString . $altLabelFieldSelect[$val[$altLabelField]]);
951  } else {
952  $out .= $splitString . htmlspecialchars(($prefixString . $altLabelFieldSelect[$val[$altLabelField]]));
953  }
954  } else {
955  if (!$out) {
956  $out = htmlspecialchars($prefixString . $val[$altLabelField]);
957  } else {
958  $out .= $splitString . htmlspecialchars(($prefixString . $val[$altLabelField]));
959  }
960  }
961  }
962  }
963  }
964  }
965  }
966  return $out;
967  }
968 
977  public function resultRowTitles($row, $conf, $table)
978  {
979  $SET = $GLOBALS['SOBE']->MOD_SETTINGS;
980  $tableHeader = [];
981  // Start header row
982  $tableHeader[] = '<thead><tr>';
983  // Iterate over given columns
984  foreach ($row as $fieldName => $fieldValue) {
985  if (GeneralUtility::inList($SET['queryFields'], $fieldName) || !$SET['queryFields'] && $fieldName != 'pid' && $fieldName != 'deleted') {
986  if ($GLOBALS['SOBE']->MOD_SETTINGS['search_result_labels']) {
987  $title = $GLOBALS['LANG']->sL($conf['columns'][$fieldName]['label'] ? $conf['columns'][$fieldName]['label'] : $fieldName, true);
988  } else {
989  $title = $GLOBALS['LANG']->sL($fieldName, true);
990  }
991  $tableHeader[] = '<th>' . $title . '</th>';
992  }
993  }
994  // Add empty icon column
995  $tableHeader[] = '<th></th>';
996  // Close header row
997  $tableHeader[] = '</tr></thead>';
998  return implode(LF, $tableHeader);
999  }
1000 
1009  public function csvRowTitles($row, $conf, $table)
1010  {
1011  $out = '';
1012  $SET = $GLOBALS['SOBE']->MOD_SETTINGS;
1013  foreach ($row as $fieldName => $fieldValue) {
1014  if (GeneralUtility::inList($SET['queryFields'], $fieldName) || !$SET['queryFields'] && $fieldName != 'pid') {
1015  if (!$out) {
1016  if ($GLOBALS['SOBE']->MOD_SETTINGS['search_result_labels']) {
1017  $out = $GLOBALS['LANG']->sL($conf['columns'][$fieldName]['label'] ? $conf['columns'][$fieldName]['label'] : $fieldName, true);
1018  } else {
1019  $out = $GLOBALS['LANG']->sL($fieldName, true);
1020  }
1021  } else {
1022  if ($GLOBALS['SOBE']->MOD_SETTINGS['search_result_labels']) {
1023  $out .= ',' . $GLOBALS['LANG']->sL(($conf['columns'][$fieldName]['label'] ? $conf['columns'][$fieldName]['label'] : $fieldName), true);
1024  } else {
1025  $out .= ',' . $GLOBALS['LANG']->sL($fieldName, true);
1026  }
1027  }
1028  }
1029  }
1030  return $out;
1031  }
1032 
1039  public function setFormName($formName)
1040  {
1041  $this->formName = trim($formName);
1042  }
1043 }
getQueryResultCode($mQ, $res, $table)
Definition: QueryView.php:389
loadStoreQueryConfigs($storeQueryConfigs, $storeIndex, $writeArray)
Definition: QueryView.php:232
static callUserFunction($funcName, &$params, &$ref, $checkPrefix='', $errorMode=0)
resultRowTitles($row, $conf, $table)
Definition: QueryView.php:977
csvValues($row, $delim=',', $quote='"', $conf = [], $table = '')
Definition: QueryView.php:474
static getModuleData($MOD_MENU, $CHANGED_SETTINGS, $modName, $type='', $dontValidateList='', $setDefaultList='')
resultRowDisplay($row, $conf, $table)
Definition: QueryView.php:550
$uid
Definition: server.php:38
addToStoreQueryConfigs($storeQueryConfigs, $index)
Definition: QueryView.php:171
static getRecord($table, $uid, $fields=' *', $where='', $useDeleteClause=true)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
cleanStoreQueryConfigs($storeQueryConfigs, $storeArray)
Definition: QueryView.php:152
static deleteClause($table, $tableAlias='')