[TASK] Remove closing PHP tags
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / ContentObject / SearchResultContentObject.php
1 <?php
2 namespace TYPO3\CMS\Frontend\ContentObject;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2013 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 * Search class used for the content object SEARCHRESULT
31 * and searching in database tables, typ. "pages" and "tt_content"
32 * Used to generate search queries for TypoScript.
33 * The class is included from "class.tslib_pagegen.php" based on whether there has been detected content in the GPvar "sword"
34 */
35 class SearchResultContentObject extends \TYPO3\CMS\Frontend\ContentObject\AbstractContentObject {
36 /**
37 * @todo Define visibility
38 */
39 public $tables = array();
40
41 // Alternatively 'PRIMARY_KEY'; sorting by primary key
42 /**
43 * @todo Define visibility
44 */
45 public $group_by = 'PRIMARY_KEY';
46
47 // Standard SQL-operator between words
48 /**
49 * @todo Define visibility
50 */
51 public $default_operator = 'AND';
52
53 /**
54 * @todo Define visibility
55 */
56 public $operator_translate_table_caseinsensitive = TRUE;
57
58 // case-sensitiv. Defineres the words, which will be operators between words
59 /**
60 * @todo Define visibility
61 */
62 public $operator_translate_table = array(
63 array('+', 'AND'),
64 array('|', 'AND'),
65 array('-', 'AND NOT'),
66 // english
67 array('and', 'AND'),
68 array('or', 'OR'),
69 array('not', 'AND NOT')
70 );
71
72 // Internal
73 // Contains the search-words and operators
74 /**
75 * @todo Define visibility
76 */
77 public $sword_array;
78
79 // Contains the query parts after processing.
80 /**
81 * @todo Define visibility
82 */
83 public $queryParts;
84
85 // Addition to the whereclause. This could be used to limit search to a certain page or alike in the system.
86 /**
87 * @todo Define visibility
88 */
89 public $other_where_clauses;
90
91 // This is set with the foreign table that 'pages' are connected to.
92 /**
93 * @todo Define visibility
94 */
95 public $fTable;
96
97 // How many rows to offset from the beginning
98 /**
99 * @todo Define visibility
100 */
101 public $res_offset = 0;
102
103 // How many results to show (0 = no limit)
104 /**
105 * @todo Define visibility
106 */
107 public $res_shows = 20;
108
109 // Intern: How many results, there was last time (with the exact same searchstring.
110 /**
111 * @todo Define visibility
112 */
113 public $res_count;
114
115 // List of pageIds.
116 /**
117 * @todo Define visibility
118 */
119 public $pageIdList = '';
120
121 /**
122 * @todo Define visibility
123 */
124 public $listOfSearchFields = '';
125
126 /**
127 * Override default constructor to make it possible to instantiate this
128 * class for indexed_search
129 *
130 * @param \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $cObj
131 */
132 public function __construct(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $cObj = NULL) {
133 if (!is_null($cObj)) {
134 $this->cObj = $cObj;
135 $this->fileFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
136 }
137 }
138
139 /**
140 * Rendering the cObject, SEARCHRESULT
141 *
142 * @param array $conf Array of TypoScript properties
143 * @return string Output
144 */
145 public function render($conf = array()) {
146 if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('sword') && \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('scols')) {
147 $this->register_and_explode_search_string(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('sword'));
148 $this->register_tables_and_columns(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('scols'), $conf['allowedCols']);
149 // Depth
150 $depth = 100;
151 // The startId is found
152 $theStartId = 0;
153 if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('stype'))) {
154 $temp_theStartId = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('stype');
155 $rootLine = $GLOBALS['TSFE']->sys_page->getRootLine($temp_theStartId);
156 // The page MUST have a rootline with the Level0-page of the current site inside!!
157 foreach ($rootLine as $val) {
158 if ($val['uid'] == $GLOBALS['TSFE']->tmpl->rootLine[0]['uid']) {
159 $theStartId = $temp_theStartId;
160 }
161 }
162 } elseif (\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('stype')) {
163 if (substr(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('stype'), 0, 1) == 'L') {
164 $pointer = intval(substr(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('stype'), 1));
165 $theRootLine = $GLOBALS['TSFE']->tmpl->rootLine;
166 // location Data:
167 $locDat_arr = explode(':', \TYPO3\CMS\Core\Utility\GeneralUtility::_POST('locationData'));
168 $pId = intval($locDat_arr[0]);
169 if ($pId) {
170 $altRootLine = $GLOBALS['TSFE']->sys_page->getRootLine($pId);
171 ksort($altRootLine);
172 if (count($altRootLine)) {
173 // Check if the rootline has the real Level0 in it!!
174 $hitRoot = 0;
175 $theNewRoot = array();
176 foreach ($altRootLine as $val) {
177 if ($hitRoot || $val['uid'] == $GLOBALS['TSFE']->tmpl->rootLine[0]['uid']) {
178 $hitRoot = 1;
179 $theNewRoot[] = $val;
180 }
181 }
182 if ($hitRoot) {
183 // Override the real rootline if any thing
184 $theRootLine = $theNewRoot;
185 }
186 }
187 }
188 $key = $this->cObj->getKey($pointer, $theRootLine);
189 $theStartId = $theRootLine[$key]['uid'];
190 }
191 }
192 if (!$theStartId) {
193 // If not set, we use current page
194 $theStartId = $GLOBALS['TSFE']->id;
195 }
196 // Generate page-tree
197 $this->pageIdList .= $this->cObj->getTreeList(-1 * $theStartId, $depth);
198 $endClause = 'pages.uid IN (' . $this->pageIdList . ')
199 AND pages.doktype in (' . $GLOBALS['TYPO3_CONF_VARS']['FE']['content_doktypes'] . ($conf['addExtUrlsAndShortCuts'] ? ',3,4' : '') . ')
200 AND pages.no_search=0' . $this->cObj->enableFields($this->fTable) . $this->cObj->enableFields('pages');
201 if ($conf['languageField.'][$this->fTable]) {
202 // (using sys_language_uid which is the ACTUAL language of the page.
203 // sys_language_content is only for selecting DISPLAY content!)
204 $endClause .= ' AND ' . $this->fTable . '.' . $conf['languageField.'][$this->fTable] . ' = ' . intval($GLOBALS['TSFE']->sys_language_uid);
205 }
206 // Build query
207 $this->build_search_query($endClause);
208 // Count...
209 if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('scount'))) {
210 $this->res_count = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('scount');
211 } else {
212 $this->count_query();
213 }
214 // Range
215 $spointer = intval(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('spointer'));
216 $range = isset($conf['range.']) ? $this->cObj->stdWrap($conf['range'], $conf['range.']) : $conf['range'];
217 if ($range) {
218 $theRange = intval($range);
219 } else {
220 $theRange = 20;
221 }
222 // Order By:
223 $noOrderBy = isset($conf['noOrderBy.']) ? $this->cObj->stdWrap($conf['noOrderBy'], $conf['noOrderBy.']) : $conf['noOrderBy'];
224 if (!$noOrderBy) {
225 $this->queryParts['ORDERBY'] = 'pages.lastUpdated, pages.tstamp';
226 }
227 $this->queryParts['LIMIT'] = $spointer . ',' . $theRange;
228 // Search...
229 $this->execute_query();
230 if ($GLOBALS['TYPO3_DB']->sql_num_rows($this->result)) {
231 $GLOBALS['TSFE']->register['SWORD_PARAMS'] = $this->get_searchwords();
232 $total = $this->res_count;
233 $rangeLow = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($spointer + 1, 1, $total);
234 $rangeHigh = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($spointer + $theRange, 1, $total);
235 // prev/next url:
236 $target = isset($conf['target.']) ? $this->cObj->stdWrap($conf['target'], $conf['target.']) : $conf['target'];
237 $LD = $GLOBALS['TSFE']->tmpl->linkData($GLOBALS['TSFE']->page, $target, 1, '', '', $this->cObj->getClosestMPvalueForPage($GLOBALS['TSFE']->page['uid']));
238 $targetPart = $LD['target'] ? ' target="' . htmlspecialchars($LD['target']) . '"' : '';
239 $urlParams = $this->cObj->URLqMark($LD['totalURL'], '&sword=' . rawurlencode(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('sword')) . '&scols=' . rawurlencode(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('scols')) . '&stype=' . rawurlencode(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('stype')) . '&scount=' . $total);
240 // substitution:
241 $result = $this->cObj->cObjGetSingle($conf['layout'], $conf['layout.'], 'layout');
242 $result = str_replace('###RANGELOW###', $rangeLow, $result);
243 $result = str_replace('###RANGEHIGH###', $rangeHigh, $result);
244 $result = str_replace('###TOTAL###', $total, $result);
245 if ($rangeHigh < $total) {
246 $next = $this->cObj->cObjGetSingle($conf['next'], $conf['next.'], 'next');
247 $next = '<a href="' . htmlspecialchars(($urlParams . '&spointer=' . ($spointer + $theRange))) . '"' . $targetPart . $GLOBALS['TSFE']->ATagParams . '>' . $next . '</a>';
248 } else {
249 $next = '';
250 }
251 $result = str_replace('###NEXT###', $next, $result);
252 if ($rangeLow > 1) {
253 $prev = $this->cObj->cObjGetSingle($conf['prev'], $conf['prev.'], 'prev');
254 $prev = '<a href="' . htmlspecialchars(($urlParams . '&spointer=' . ($spointer - $theRange))) . '"' . $targetPart . $GLOBALS['TSFE']->ATagParams . '>' . $prev . '</a>';
255 } else {
256 $prev = '';
257 }
258 $result = str_replace('###PREV###', $prev, $result);
259 // Searching result
260 $theValue = $this->cObj->cObjGetSingle($conf['resultObj'], $conf['resultObj.'], 'resultObj');
261 $cObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
262 $cObj->setParent($this->cObj->data, $this->cObj->currentRecord);
263 $renderCode = '';
264 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($this->result)) {
265 // versionOL() here? This is search result displays, is that possible to preview anyway?
266 // Or are records selected here already future versions?
267 $cObj->start($row);
268 $renderCode .= $cObj->cObjGetSingle($conf['renderObj'], $conf['renderObj.'], 'renderObj');
269 }
270 $renderWrap = isset($conf['renderWrap.']) ? $this->cObj->stdWrap($conf['renderWrap'], $conf['renderWrap.']) : $conf['renderWrap'];
271 $theValue .= $this->cObj->wrap($renderCode, $renderWrap);
272 $theValue = str_replace('###RESULT###', $theValue, $result);
273 } else {
274 $theValue = $this->cObj->cObjGetSingle($conf['noResultObj'], $conf['noResultObj.'], 'noResultObj');
275 }
276 $GLOBALS['TT']->setTSlogMessage('Search in fields: ' . $this->listOfSearchFields);
277 // Wrapping
278 $content = $theValue;
279 $wrap = isset($conf['wrap.']) ? $this->cObj->stdWrap($conf['wrap'], $conf['wrap.']) : $conf['wrap'];
280 if ($wrap) {
281 $content = $this->cObj->wrap($content, $wrap);
282 }
283 if (isset($conf['stdWrap.'])) {
284 $content = $this->cObj->stdWrap($content, $conf['stdWrap.']);
285 }
286 // Returning, do not cache the result of the search
287 $GLOBALS['TSFE']->set_no_cache('Search result page');
288 return $content;
289 }
290 return '';
291 }
292
293 /**
294 * Creates the $this->tables-array.
295 * The 'pages'-table is ALWAYS included as the search is page-based. Apart from this there may be one and only one table, joined with the pages-table. This table is the first table mentioned in the requested-list. If any more tables are set here, they are ignored.
296 *
297 * @param string $requestedCols is a list (-) of columns that we want to search. This could be input from the search-form (see TypoScript documentation)
298 * @param string $allowedCols $allowedCols: is the list of columns, that MAY be searched. All allowed cols are set as result-fields. All requested cols MUST be in the allowed-fields list.
299 * @return void
300 * @todo Define visibility
301 */
302 public function register_tables_and_columns($requestedCols, $allowedCols) {
303 $rCols = $this->explodeCols($requestedCols);
304 $aCols = $this->explodeCols($allowedCols);
305 foreach ($rCols as $k => $v) {
306 $rCols[$k] = trim($v);
307 if (in_array($rCols[$k], $aCols)) {
308 $parts = explode('.', $rCols[$k]);
309 $this->tables[$parts[0]]['searchfields'][] = $parts[1];
310 }
311 }
312 $this->tables['pages']['primary_key'] = 'uid';
313 $this->tables['pages']['resultfields'][] = 'uid';
314 unset($this->tables['pages']['fkey']);
315 foreach ($aCols as $k => $v) {
316 $aCols[$k] = trim($v);
317 $parts = explode('.', $aCols[$k]);
318 $this->tables[$parts[0]]['resultfields'][] = $parts[1] . ' AS ' . str_replace('.', '_', $aCols[$k]);
319 $this->tables[$parts[0]]['fkey'] = 'pid';
320 }
321 $this->fTable = '';
322 foreach ($this->tables as $t => $v) {
323 if ($t != 'pages') {
324 if (!$this->fTable) {
325 $this->fTable = $t;
326 } else {
327 unset($this->tables[$t]);
328 }
329 }
330 }
331 }
332
333 /**
334 * Function that can convert the syntax for entering which tables/fields the search should be conducted in.
335 *
336 * @param string $in This is the code-line defining the tables/fields to search. Syntax: '[table1].[field1]-[field2]-[field3] : [table2].[field1]-[field2]'
337 * @return array An array where the values is "[table].[field]" strings to search
338 * @see register_tables_and_columns()
339 * @todo Define visibility
340 */
341 public function explodeCols($in) {
342 $theArray = explode(':', $in);
343 $out = array();
344 foreach ($theArray as $val) {
345 $val = trim($val);
346 $parts = explode('.', $val);
347 if ($parts[0] && $parts[1]) {
348 $subparts = explode('-', $parts[1]);
349 foreach ($subparts as $piece) {
350 $piece = trim($piece);
351 if ($piece) {
352 $out[] = $parts[0] . '.' . $piece;
353 }
354 }
355 }
356 }
357 return $out;
358 }
359
360 /**
361 * Takes a search-string (WITHOUT SLASHES or else it'll be a little sppooky , NOW REMEMBER to unslash!!)
362 * Sets up $this->sword_array op with operators.
363 * This function uses $this->operator_translate_table as well as $this->default_operator
364 *
365 * @param string $sword The input search-word string.
366 * @return void
367 * @todo Define visibility
368 */
369 public function register_and_explode_search_string($sword) {
370 $sword = trim($sword);
371 if ($sword) {
372 $components = $this->split($sword);
373 // the searchword is stored here during the loop
374 $s_sword = '';
375 if (is_array($components)) {
376 $i = 0;
377 $lastoper = '';
378 foreach ($components as $key => $val) {
379 $operator = $this->get_operator($val);
380 if ($operator) {
381 $lastoper = $operator;
382 } elseif (strlen($val) > 1) {
383 // A searchword MUST be at least two characters long!
384 $this->sword_array[$i]['sword'] = $val;
385 $this->sword_array[$i]['oper'] = $lastoper ? $lastoper : $this->default_operator;
386 $lastoper = '';
387 $i++;
388 }
389 }
390 }
391 }
392 }
393
394 /**
395 * Used to split a search-word line up into elements to search for. This function will detect boolean words like AND and OR, + and -, and even find sentences encapsulated in ""
396 * This function could be re-written to be more clean and effective - yet it's not that important.
397 *
398 * @param string $origSword The raw sword string from outside
399 * @param string $specchars Special chars which are used as operators (+- is default)
400 * @param string $delchars Special chars which are deleted if the append the searchword (+-., is default)
401 * @return mixed Returns an ARRAY if there were search words, othervise the return value may be unset.
402 * @todo Define visibility
403 */
404 public function split($origSword, $specchars = '+-', $delchars = '+.,-') {
405 $sword = $origSword;
406 $specs = '[' . preg_quote($specchars, '/') . ']';
407 // As long as $sword is TRUE (that means $sword MUST be reduced little by little until its empty inside the loop!)
408 while ($sword) {
409 // There was a double-quote and we will then look for the ending quote.
410 if (preg_match('/^"/', $sword)) {
411 // Removes first double-quote
412 $sword = preg_replace('/^"/', '', $sword);
413 // Removes everything till next double-quote
414 preg_match('/^[^"]*/', $sword, $reg);
415 // reg[0] is the value, should not be trimmed
416 $value[] = $reg[0];
417 $sword = preg_replace('/^' . preg_quote($reg[0], '/') . '/', '', $sword);
418 // Removes last double-quote
419 $sword = trim(preg_replace('/^"/', '', $sword));
420 } elseif (preg_match('/^' . $specs . '/', $sword, $reg)) {
421 $value[] = $reg[0];
422 // Removes = sign
423 $sword = trim(preg_replace('/^' . $specs . '/', '', $sword));
424 } elseif (preg_match('/[\\+\\-]/', $sword)) {
425 // Check if $sword contains + or -
426 // + and - shall only be interpreted as $specchars when there's whitespace before it
427 // otherwise it's included in the searchword (e.g. "know-how")
428 // explode $sword to single words
429 $a_sword = explode(' ', $sword);
430 // get first word
431 $word = array_shift($a_sword);
432 // Delete $delchars at end of string
433 $word = rtrim($word, $delchars);
434 // add searchword to values
435 $value[] = $word;
436 // re-build $sword
437 $sword = implode(' ', $a_sword);
438 } else {
439 // There are no double-quotes around the value. Looking for next (space) or special char.
440 preg_match('/^[^ ' . preg_quote($specchars, '/') . ']*/', $sword, $reg);
441 // Delete $delchars at end of string
442 $word = rtrim(trim($reg[0]), $delchars);
443 $value[] = $word;
444 $sword = trim(preg_replace('/^' . preg_quote($reg[0], '/') . '/', '', $sword));
445 }
446 }
447 return $value;
448 }
449
450 /**
451 * This creates the search-query.
452 * In TypoScript this is used for searching only records not hidden, start/endtimed and fe_grouped! (enable-fields, see tt_content)
453 * Sets $this->queryParts
454 *
455 * @param string $endClause Some extra conditions that the search must match.
456 * @return boolean Returns TRUE no matter what - sweet isn't it!
457 * @access private
458 * @see \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::SEARCHRESULT()
459 * @todo Define visibility
460 */
461 public function build_search_query($endClause) {
462 if (is_array($this->tables)) {
463 $tables = $this->tables;
464 $primary_table = '';
465 // Primary key table is found.
466 foreach ($tables as $key => $val) {
467 if ($tables[$key]['primary_key']) {
468 $primary_table = $key;
469 }
470 }
471 if ($primary_table) {
472 // Initialize query parts:
473 $this->queryParts = array(
474 'SELECT' => '',
475 'FROM' => '',
476 'WHERE' => '',
477 'GROUPBY' => '',
478 'ORDERBY' => '',
479 'LIMIT' => ''
480 );
481 // Find tables / field names to select:
482 $fieldArray = array();
483 $tableArray = array();
484 foreach ($tables as $key => $val) {
485 $tableArray[] = $key;
486 $resultfields = $tables[$key]['resultfields'];
487 if (is_array($resultfields)) {
488 foreach ($resultfields as $key2 => $val2) {
489 $fieldArray[] = $key . '.' . $val2;
490 }
491 }
492 }
493 $this->queryParts['SELECT'] = implode(',', $fieldArray);
494 $this->queryParts['FROM'] = implode(',', $tableArray);
495 // Set join WHERE parts:
496 $whereArray = array();
497 $primary_table_and_key = $primary_table . '.' . $tables[$primary_table]['primary_key'];
498 $primKeys = array();
499 foreach ($tables as $key => $val) {
500 $fkey = $tables[$key]['fkey'];
501 if ($fkey) {
502 $primKeys[] = $key . '.' . $fkey . '=' . $primary_table_and_key;
503 }
504 }
505 if (count($primKeys)) {
506 $whereArray[] = '(' . implode(' OR ', $primKeys) . ')';
507 }
508 // Additional where clause:
509 if (trim($endClause)) {
510 $whereArray[] = trim($endClause);
511 }
512 // Add search word where clause:
513 $query_part = $this->build_search_query_for_searchwords();
514 if (!$query_part) {
515 $query_part = '(0!=0)';
516 }
517 $whereArray[] = '(' . $query_part . ')';
518 // Implode where clauses:
519 $this->queryParts['WHERE'] = implode(' AND ', $whereArray);
520 // Group by settings:
521 if ($this->group_by) {
522 if ($this->group_by == 'PRIMARY_KEY') {
523 $this->queryParts['GROUPBY'] = $primary_table_and_key;
524 } else {
525 $this->queryParts['GROUPBY'] = $this->group_by;
526 }
527 }
528 }
529 }
530 }
531
532 /**
533 * Creates the part of the SQL-sentence, that searches for the search-words ($this->sword_array)
534 *
535 * @return string Part of where class limiting result to the those having the search word.
536 * @access private
537 * @todo Define visibility
538 */
539 public function build_search_query_for_searchwords() {
540 if (is_array($this->sword_array)) {
541 $main_query_part = array();
542 foreach ($this->sword_array as $key => $val) {
543 $s_sword = $this->sword_array[$key]['sword'];
544 // Get subQueryPart
545 $sub_query_part = array();
546 $this->listOfSearchFields = '';
547 foreach ($this->tables as $key3 => $val3) {
548 $searchfields = $this->tables[$key3]['searchfields'];
549 if (is_array($searchfields)) {
550 foreach ($searchfields as $key2 => $val2) {
551 $this->listOfSearchFields .= $key3 . '.' . $val2 . ',';
552 $sub_query_part[] = $key3 . '.' . $val2 . ' LIKE \'%' . $GLOBALS['TYPO3_DB']->quoteStr($s_sword, $key3) . '%\'';
553 }
554 }
555 }
556 if (count($sub_query_part)) {
557 $main_query_part[] = $this->sword_array[$key]['oper'];
558 $main_query_part[] = '(' . implode(' OR ', $sub_query_part) . ')';
559 }
560 }
561 if (count($main_query_part)) {
562 // Remove first part anyways.
563 unset($main_query_part[0]);
564 return implode(' ', $main_query_part);
565 }
566 }
567 }
568
569 /**
570 * This returns an SQL search-operator (eg. AND, OR, NOT) translated from the current localized set of operators (eg. in danish OG, ELLER, IKKE).
571 *
572 * @param string $operator The possible operator to find in the internal operator array.
573 * @return string If found, the SQL operator for the localized input operator.
574 * @access private
575 * @todo Define visibility
576 */
577 public function get_operator($operator) {
578 $operator = trim($operator);
579 $op_array = $this->operator_translate_table;
580 if ($this->operator_translate_table_caseinsensitive) {
581 // case-conversion is charset insensitive, but it doesn't spoil
582 // anything if input string AND operator table is already converted
583 $operator = strtolower($operator);
584 }
585 foreach ($op_array as $key => $val) {
586 $item = $op_array[$key][0];
587 if ($this->operator_translate_table_caseinsensitive) {
588 // See note above.
589 $item = strtolower($item);
590 }
591 if ($operator == $item) {
592 return $op_array[$key][1];
593 }
594 }
595 }
596
597 /**
598 * Counts the results and sets the result in $this->res_count
599 *
600 * @return boolean TRUE, if $this->query was found
601 * @todo Define visibility
602 */
603 public function count_query() {
604 if (is_array($this->queryParts)) {
605 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($this->queryParts['SELECT'], $this->queryParts['FROM'], $this->queryParts['WHERE'], $this->queryParts['GROUPBY']);
606 $this->res_count = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
607 return TRUE;
608 }
609 }
610
611 /**
612 * Executes the search, sets result pointer in $this->result
613 *
614 * @return boolean TRUE, if $this->query was set and query performed
615 * @todo Define visibility
616 */
617 public function execute_query() {
618 if (is_array($this->queryParts)) {
619 $this->result = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($this->queryParts);
620 return TRUE;
621 }
622 }
623
624 /**
625 * Returns URL-parameters with the current search words.
626 * Used when linking to result pages so that search words can be highlighted.
627 *
628 * @return string URL-parameters with the searchwords
629 * @todo Define visibility
630 */
631 public function get_searchwords() {
632 $SWORD_PARAMS = '';
633 if (is_array($this->sword_array)) {
634 foreach ($this->sword_array as $key => $val) {
635 $SWORD_PARAMS .= '&sword_list[]=' . rawurlencode($val['sword']);
636 }
637 }
638 return $SWORD_PARAMS;
639 }
640
641 /**
642 * Returns an array with the search words in
643 *
644 * @return array IF the internal sword_array contained search words it will return these, otherwise "void
645 * @todo Define visibility
646 */
647 public function get_searchwordsArray() {
648 if (is_array($this->sword_array)) {
649 foreach ($this->sword_array as $key => $val) {
650 $swords[] = $val['sword'];
651 }
652 }
653 return $swords;
654 }
655
656 }