TYPO3 API  SVNRelease
class.tslib_content_searchresult.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  *  Copyright notice
00004  *
00005  *  (c) 2010-2011 Xavier Perseguers <typo3@perseguers.ch>
00006  *  (c) 2010-2011 Steffen Kamper <steffen@typo3.org>
00007  *  All rights reserved
00008  *
00009  *  This script is part of the TYPO3 project. The TYPO3 project is
00010  *  free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  The GNU General Public License can be found at
00016  *  http://www.gnu.org/copyleft/gpl.html.
00017  *  A copy is found in the textfile GPL.txt and important notices to the license
00018  *  from the author is found in LICENSE.txt distributed with these scripts.
00019  *
00020  *
00021  *  This script is distributed in the hope that it will be useful,
00022  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  *  GNU General Public License for more details.
00025  *
00026  *  This copyright notice MUST APPEAR in all copies of the script!
00027  ***************************************************************/
00028 
00029 /**
00030  * Contains SEARCHRESULT class object.
00031  *
00032  * $Id: class.tslib_content.php 7905 2010-06-13 14:42:33Z ohader $
00033  * @author Xavier Perseguers <typo3@perseguers.ch>
00034  * @author Steffen Kamper <steffen@typo3.org>
00035  */
00036 class tslib_content_SearchResult extends tslib_content_Abstract {
00037 
00038     /**
00039      * Rendering the cObject, SEARCHRESULT
00040      *
00041      * @param   array       Array of TypoScript properties
00042      * @return  string      Output
00043      */
00044     public function render($conf = array()) {
00045         if (t3lib_div::_GP('sword') && t3lib_div::_GP('scols')) {
00046             $search = t3lib_div::makeInstance('tslib_search');
00047             $search->register_and_explode_search_string(t3lib_div::_GP('sword'));
00048             $search->register_tables_and_columns(t3lib_div::_GP('scols'), $conf['allowedCols']);
00049                 // depth
00050             $depth = 100;
00051                 // the startId is found
00052             $theStartId = 0;
00053             if (t3lib_div::testInt(t3lib_div::_GP('stype'))) {
00054                 $temp_theStartId = t3lib_div::_GP('stype');
00055                 $rootLine = $GLOBALS['TSFE']->sys_page->getRootLine($temp_theStartId);
00056                     // The page MUST have a rootline with the Level0-page of the current site inside!!
00057                 foreach ($rootLine as $val) {
00058                     if ($val['uid'] == $GLOBALS['TSFE']->tmpl->rootLine[0]['uid']) {
00059                         $theStartId = $temp_theStartId;
00060                     }
00061                 }
00062             } else if (t3lib_div::_GP('stype')) {
00063                 if (substr(t3lib_div::_GP('stype'), 0, 1) == 'L') {
00064                     $pointer = intval(substr(t3lib_div::_GP('stype'), 1));
00065                     $theRootLine = $GLOBALS['TSFE']->tmpl->rootLine;
00066                         // location Data:
00067                     $locDat_arr = explode(':', t3lib_div::_POST('locationData'));
00068                     $pId = intval($locDat_arr[0]);
00069                     if ($pId) {
00070                         $altRootLine = $GLOBALS['TSFE']->sys_page->getRootLine($pId);
00071                         ksort($altRootLine);
00072                         if (count($altRootLine)) {
00073                                 // check if the rootline has the real Level0 in it!!
00074                             $hitRoot = 0;
00075                             $theNewRoot = array();
00076                             foreach ($altRootLine as $val) {
00077                                 if ($hitRoot || $val['uid'] == $GLOBALS['TSFE']->tmpl->rootLine[0]['uid']) {
00078                                     $hitRoot = 1;
00079                                     $theNewRoot[] = $val;
00080                                 }
00081                             }
00082                             if ($hitRoot) {
00083                                 $theRootLine = $theNewRoot; // Override the real rootline if any thing
00084                             }
00085                         }
00086                     }
00087                     $key = $this->cObj->getKey($pointer, $theRootLine);
00088                     $theStartId = $theRootLine[$key]['uid'];
00089                 }
00090             }
00091             if (!$theStartId) {
00092                     // If not set, we use current page
00093                 $theStartId = $GLOBALS['TSFE']->id;
00094             }
00095                 // generate page-tree
00096             $search->pageIdList .= $this->cObj->getTreeList(-1 * $theStartId, $depth);
00097 
00098             $endClause = 'pages.uid IN (' . $search->pageIdList . ')
00099                 AND pages.doktype in (' .
00100                     $GLOBALS['TYPO3_CONF_VARS']['FE']['content_doktypes'] . ($conf['addExtUrlsAndShortCuts'] ? ',3,4' : '') .
00101                 ')
00102                 AND pages.no_search=0' . $this->cObj->enableFields($search->fTable) . $this->cObj->enableFields('pages');
00103 
00104             if ($conf['languageField.'][$search->fTable]) {
00105                     // (using sys_language_uid which is the ACTUAL language of the page.
00106                     // sys_language_content is only for selecting DISPLAY content!)
00107                 $endClause .= ' AND ' . $search->fTable . '.' . $conf['languageField.'][$search->fTable] .
00108                     ' = ' .
00109                 intval($GLOBALS['TSFE']->sys_language_uid);
00110             }
00111 
00112                 // build query
00113             $search->build_search_query($endClause);
00114 
00115                 // count...
00116             if (t3lib_div::testInt(t3lib_div::_GP('scount'))) {
00117                 $search->res_count = t3lib_div::_GP('scount');
00118             } else {
00119                 $search->count_query();
00120             }
00121 
00122                 // range
00123             $spointer = intval(t3lib_div::_GP('spointer'));
00124             $range = isset($conf['range.'])
00125                 ? $this->cObj->stdWrap($conf['range'], $conf['range.'])
00126                 : $conf['range'];
00127             if ($range) {
00128                 $theRange = intval($range);
00129             } else {
00130                 $theRange = 20;
00131             }
00132 
00133                 // Order By:
00134             $noOrderBy = isset($conf['noOrderBy.'])
00135                 ? $this->cObj->stdWrap($conf['noOrderBy'], $conf['noOrderBy.'])
00136                 : $conf['noOrderBy'];
00137             if (!$noOrderBy) {
00138                 $search->queryParts['ORDERBY'] = 'pages.lastUpdated, pages.tstamp';
00139             }
00140 
00141             $search->queryParts['LIMIT'] = $spointer . ',' . $theRange;
00142 
00143                 // search...
00144             $search->execute_query();
00145             if ($GLOBALS['TYPO3_DB']->sql_num_rows($search->result)) {
00146                 $GLOBALS['TSFE']->register['SWORD_PARAMS'] = $search->get_searchwords();
00147 
00148                 $total = $search->res_count;
00149                 $rangeLow = t3lib_div::intInRange($spointer + 1, 1, $total);
00150                 $rangeHigh = t3lib_div::intInRange($spointer + $theRange, 1, $total);
00151                     // prev/next url:
00152 
00153                 $target = isset($conf['target.'])
00154                     ? $this->cObj->stdWrap($conf['target'], $conf['target.'])
00155                     : $conf['target'];
00156 
00157                 $LD = $GLOBALS['TSFE']->tmpl->linkData(
00158                     $GLOBALS['TSFE']->page,
00159                     $target,
00160                     1,
00161                     '',
00162                     '',
00163                     $this->cObj->getClosestMPvalueForPage($GLOBALS['TSFE']->page['uid'])
00164                 );
00165                 $targetPart = $LD['target'] ? ' target="' . htmlspecialchars($LD['target']) . '"' : '';
00166                 $urlParams = $this->cObj->URLqMark(
00167                     $LD['totalURL'],
00168                     '&sword=' . rawurlencode(t3lib_div::_GP('sword')) .
00169                     '&scols=' . rawurlencode(t3lib_div::_GP('scols')) .
00170                     '&stype=' . rawurlencode(t3lib_div::_GP('stype')) .
00171                     '&scount=' . $total
00172                 );
00173                     // substitution:
00174                 $result = $this->cObj->cObjGetSingle($conf['layout'], $conf['layout.'], 'layout');
00175                 $result = str_replace('###RANGELOW###', $rangeLow, $result);
00176                 $result = str_replace('###RANGEHIGH###', $rangeHigh, $result);
00177                 $result = str_replace('###TOTAL###', $total, $result);
00178 
00179                 if ($rangeHigh < $total) {
00180                     $next = $this->cObj->cObjGetSingle($conf['next'], $conf['next.'], 'next');
00181                     $next = '<a href="' . htmlspecialchars($urlParams .
00182                         '&spointer=' . ($spointer + $theRange)) . '"' .
00183                         $targetPart . $GLOBALS['TSFE']->ATagParams . '>' . $next . '</a>';
00184                 } else
00185                     $next = '';
00186                 $result = str_replace('###NEXT###', $next, $result);
00187 
00188                 if ($rangeLow > 1) {
00189                     $prev = $this->cObj->cObjGetSingle($conf['prev'], $conf['prev.'], 'prev');
00190                     $prev = '<a href="' . htmlspecialchars($urlParams .
00191                         '&spointer=' . ($spointer - $theRange)) . '"' .
00192                         $targetPart . $GLOBALS['TSFE']->ATagParams . '>' . $prev . '</a>';
00193                 } else
00194                     $prev = '';
00195                 $result = str_replace('###PREV###', $prev, $result);
00196 
00197                     // searching result
00198                 $theValue = $this->cObj->cObjGetSingle($conf['resultObj'], $conf['resultObj.'], 'resultObj');
00199                 $cObj = t3lib_div::makeInstance('tslib_cObj');
00200                 $cObj->setParent($this->cObj->data, $this->cObj->currentRecord);
00201                 $renderCode = '';
00202                 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($search->result)) {
00203                         // versionOL() here? This is search result displays, is that possible to preview anyway?
00204                         // Or are records selected here already future versions?
00205                     $cObj->start($row);
00206                     $renderCode .= $cObj->cObjGetSingle($conf['renderObj'], $conf['renderObj.'], 'renderObj');
00207                 }
00208                 $renderWrap = isset($conf['renderWrap.'])
00209                     ? $this->cObj->stdWrap($conf['renderWrap'], $conf['renderWrap.'])
00210                     : $conf['renderWrap'];
00211                 $theValue .= $this->cObj->wrap($renderCode, $renderWrap);
00212                 $theValue = str_replace('###RESULT###', $theValue, $result);
00213             } else {
00214                 $theValue = $this->cObj->cObjGetSingle($conf['noResultObj'], $conf['noResultObj.'], 'noResultObj');
00215             }
00216 
00217             $GLOBALS['TT']->setTSlogMessage('Search in fields:   ' . $search->listOfSearchFields);
00218 
00219                 // wrapping
00220             $content = $theValue;
00221 
00222             $wrap = isset($conf['wrap.'])
00223                 ? $this->cObj->stdWrap( $conf['wrap'], $conf['wrap.'])
00224                 :  $conf['wrap'];
00225             if ($wrap) {
00226                 $content = $this->cObj->wrap($content, $wrap);
00227             }
00228 
00229             if (isset($conf['stdWrap.'])) {
00230                 $content = $this->cObj->stdWrap($content, $conf['stdWrap.']);
00231             }
00232                 // returning
00233             $GLOBALS['TSFE']->set_no_cache();
00234 
00235             return $content;
00236         }
00237     }
00238 
00239 }
00240 
00241 
00242 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['tslib/content/class.tslib_content_searchresult.php'])) {
00243     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['tslib/content/class.tslib_content_searchresult.php']);
00244 }
00245 
00246 ?>