6 * Portions Copyright 1999-2001 (c) VA Linux Systems
7 * The rest Copyright 2004 (c) Guillaume Smet / Open Wide
14 require_once('www/search/include/renderers/SearchRenderer.class');
16 class HtmlSearchRenderer extends SearchRenderer {
19 * Localization BaseLanguage object
21 * @var object $Language
26 * Headers of the HTML results table
28 * @var array $tableHeaders
30 var $tableHeaders = array();
35 * @param string $typeOfSearch type of the search (Software, Forum, People and so on)
36 * @param string $words words we are searching for
37 * @param int $offset offset
38 * @param boolean $isExact if we want to search for all the words or if only one matching the query is sufficient
39 * @param object $searchQuery SearchQuery instance
41 function HtmlSearchRenderer($typeOfSearch, $words, $isExact, $searchQuery) {
43 $Language =& $GLOBALS['Language'];
45 $this->SearchRenderer($typeOfSearch, $words, $isExact, $searchQuery);
49 * flush - flush the html output
52 $searchQuery =& $this->searchQuery;
53 if($searchQuery->isError()) {
55 echo '<h2>'.$Language->getText('search', $searchQuery->getErrorMessage()).'</h2>';
58 $searchQuery->executeQuery();
59 if($searchQuery->getResult() && ($searchQuery->getRowsTotalCount() == 1 && $searchQuery->getOffset() == 0) && $this->implementsRedirectToResult()) {
60 $this->redirectToResult();
70 * writeHeader - write the header of the output
72 function writeHeader() {
73 echo '<div align="center">';
74 echo $GLOBALS['HTML']->searchBox();
79 * writeBody - write the body
81 function writeBody() {
82 echo $this->writeResults();
86 * writeFooter - write the footer
88 function writeFooter() {
89 $GLOBALS['HTML']->footer(array());
93 * getResults - get the html output which will display the search results
95 * @return string html output
97 function writeResults() {
98 $searchQuery =& $this->searchQuery;
99 $query =& $this->query;
103 if(!$searchQuery->getResult() || $searchQuery->getRowsCount() < 1) {
104 $html .= '<h2>'.sprintf(_('No matches found for <em>%1$s</em>'), htmlspecialchars($query['words'])).'</h2>';
107 $html .= '<h3>'.sprintf(_('Search results for <em>%1$s</em>'), htmlspecialchars($query['words'])).'</h3>';
109 $html .= $GLOBALS['HTML']->listTableTop($this->tableHeaders);
110 $html .= $this->getRows();
111 $html .= $GLOBALS['HTML']->listTableBottom();
114 if($searchQuery->getRowsCount() > 0 && ($searchQuery->getRowsTotalCount() > $searchQuery->getRowsCount() || $searchQuery->getOffset() != 0 )) {
115 $html .= $this->getNavigationPanel();
122 * getNavigationPanel - get the html output for the navigation panel
124 * @return string html output
126 function getNavigationPanel() {
127 $searchQuery =& $this->searchQuery;
131 $html .= '<table class="tablecontent" width="100%" cellpadding="5" cellspacing="0">';
134 if ($searchQuery->getOffset() != 0) {
135 $html .= '<a href="'.$this->getPreviousResultsUrl().'" class="prev">'
136 . html_image('t2.png', '15', '15', array('border'=>'0','align'=>'middle'))
137 . ' '._('Previous Results').'</a>';
141 $html .= '</td><td align="right">';
142 if ($searchQuery->getRowsTotalCount() > $searchQuery->getRowsCount()) {
143 $html .= '<a href="'.$this->getNextResultsUrl().'" class="next">'
144 ._('Next Results').' '
145 . html_image('t.png', '15', '15', array('border'=>'0','align'=>'middle')) . '</a>';
149 $html .= '</td></tr>';
155 * getPreviousResultsUrl - get the url to go to see the previous results
157 * @return string url to previous results page
159 function getPreviousResultsUrl() {
160 $offset = $this->searchQuery->getOffset() - $this->searchQuery->getRowsPerPage();
161 $query =& $this->query;
163 $url = '/search/?type='.$query['typeOfSearch'].'&exact='.$query['isExact'].'&q='.urlencode($query['words']);
165 $url .= '&offset='.$offset;
171 * getNextResultsUrl - get the url to go to see the next results
173 * @return string url to next results page
175 function getNextResultsUrl() {
176 $query =& $this->query;
177 return '/search/?type='.$query['typeOfSearch'].'&exact='.$query['isExact'].'&q='.urlencode($query['words']).'&offset='.($this->searchQuery->getOffset() + $this->searchQuery->getRowsPerPage());
181 * highlightTargetWords - highlight the words we are looking for
183 * @param string $text text
184 * @return string text with keywords highlighted
186 function highlightTargetWords($text) {
190 $regexp = implode($this->searchQuery->getWords(), '|');
191 return preg_replace('/('.str_replace('/', '\/', $regexp).')/i','<span class="selected">\1</span>', $text);
195 * implementsRedirectToResult - check if the current object implements the redirect to result feature by having a redirectToResult method
197 * @return boolean true if our object implements search by id, false otherwise.
199 function implementsRedirectToResult() {
200 return method_exists($this, 'redirectToResult');
204 * getResultId - get the field value for the first row of a result handle
206 * @param string $fieldName field name
207 * @return string value of the field
209 function getResultId($fieldName) {
210 return db_result($this->searchQuery->getResult(), 0, $fieldName);