5 * Copyright 1999-2001 (c) VA Linux Systems
6 * Copyright 2004 (c) Guillaume Smet / Open Wide
7 * Copyright (C) 2011 Alain Peyrat - Alcatel-Lucent
8 * http://fusionforge.org
10 * This file is part of FusionForge. FusionForge is free software;
11 * you can redistribute it and/or modify it under the terms of the
12 * GNU General Public License as published by the Free Software
13 * Foundation; either version 2 of the Licence, or (at your option)
16 * FusionForge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 require_once $gfwww.'search/include/renderers/SearchRenderer.class.php';
28 class HtmlSearchRenderer extends SearchRenderer {
31 * Headers of the HTML results table
33 * @var array $tableHeaders
35 var $tableHeaders = array();
40 * @param string $typeOfSearch type of the search (Software, Forum, People and so on)
41 * @param string $words words we are searching for
42 * @param int $offset offset
43 * @param boolean $isExact if we want to search for all the words or if only one matching the query is sufficient
44 * @param object $searchQuery SearchQuery instance
46 function HtmlSearchRenderer($typeOfSearch, $words, $isExact, $searchQuery) {
47 $this->SearchRenderer($typeOfSearch, $words, $isExact, $searchQuery);
51 * flush - flush the html output
54 $searchQuery =& $this->searchQuery;
55 if($searchQuery->isError()) {
57 echo '<p class="error">'.$searchQuery->getErrorMessage().'</p>';
60 $searchQuery->executeQuery();
61 if($searchQuery->getResult() && ($searchQuery->getRowsTotalCount() == 1 && $searchQuery->getOffset() == 0) && $this->implementsRedirectToResult()) {
62 $this->redirectToResult();
72 * writeHeader - write the header of the output
74 function writeHeader() {
78 * writeBody - write the body
80 function writeBody() {
81 echo $this->writeResults();
85 * writeFooter - write the footer
87 function writeFooter() {
88 $GLOBALS['HTML']->footer(array());
92 * getResults - get the html output which will display the search results
94 * @return string html output
96 function writeResults() {
97 $searchQuery =& $this->searchQuery;
98 $query =& $this->query;
100 $html = '<h2>'.sprintf(_('Search results for <em>%1$s</em>'), $query['words']).'</h2>';
101 if(!$searchQuery->getResult() || $searchQuery->getRowsCount() < 1) {
102 $html .= '<p><strong>'.sprintf(_('No matches found for <em>%1$s</em>'), $query['words']).'</strong></p>';
105 $html .= $GLOBALS['HTML']->listTableTop($this->tableHeaders);
106 $html .= $this->getRows();
107 $html .= $GLOBALS['HTML']->listTableBottom();
110 if($searchQuery->getRowsCount() > 0 && ($searchQuery->getRowsTotalCount() > $searchQuery->getRowsCount() || $searchQuery->getOffset() != 0 )) {
111 $html .= $this->getNavigationPanel();
118 * getNavigationPanel - get the html output for the navigation panel
120 * @return string html output
122 function getNavigationPanel() {
123 $searchQuery =& $this->searchQuery;
126 $html .= '<table class="tablecontent" width="100%" cellpadding="5" cellspacing="0">';
129 if ($searchQuery->getOffset() != 0) {
130 $html .= '<a href="'.$this->getPreviousResultsUrl().'" class="prev">'
131 . html_image('t2.png', '15', '15')
132 . ' '._('Previous Results').'</a>';
136 $html .= '</td><td align="right">';
137 if ($searchQuery->getRowsTotalCount() > $searchQuery->getRowsCount()) {
138 $html .= '<a href="'.$this->getNextResultsUrl().'" class="next">'
139 ._('Next Results').' '
140 . html_image('t.png', '15', '15') . '</a>';
144 $html .= '</td></tr>';
150 * getPreviousResultsUrl - get the url to go to see the previous results
152 * @return string url to previous results page
154 function getPreviousResultsUrl() {
155 $offset = $this->searchQuery->getOffset() - $this->searchQuery->getRowsPerPage();
156 $query =& $this->query;
158 $url = '/search/?type='.$query['typeOfSearch'].'&exact='.$query['isExact'].'&q='.urlencode($query['words']);
160 $url .= '&offset='.$offset;
166 * getNextResultsUrl - get the url to go to see the next results
168 * @return string url to next results page
170 function getNextResultsUrl() {
171 $query =& $this->query;
172 return '/search/?type='.$query['typeOfSearch'].'&exact='.$query['isExact'].'&q='.urlencode($query['words']).'&offset='.($this->searchQuery->getOffset() + $this->searchQuery->getRowsPerPage());
176 * highlightTargetWords - highlight the words we are looking for
178 * @param string $text text
179 * @return string text with keywords highlighted
181 function highlightTargetWords($text) {
185 $words = $this->searchQuery->getWords();
186 foreach ($this->searchQuery->getPhrases() as $p) {
187 foreach (explode(' ',$p) as $w) {
191 $regexp = implode('|',$words);
192 return preg_replace('/('.str_replace('/', '\/', $regexp).')/i','<span class="selected">\1</span>', $text);
196 * implementsRedirectToResult - check if the current object implements the redirect to result feature by having a redirectToResult method
198 * @return boolean true if our object implements search by id, false otherwise.
200 function implementsRedirectToResult() {
201 return method_exists($this, 'redirectToResult');
205 * getResultId - get the field value for the first row of a result handle
207 * @param string $fieldName field name
208 * @return string value of the field
210 function getResultId($fieldName) {
211 return db_result($this->searchQuery->getResult(), 0, $fieldName);