6 * Portions Copyright 1999-2001 (c) VA Linux Systems
7 * The rest Copyright 2004 (c) Guillaume Smet / Open Wide
13 require_once $gfwww.'search/include/renderers/SearchRenderer.class.php';
15 class HtmlSearchRenderer extends SearchRenderer {
18 * Headers of the HTML results table
20 * @var array $tableHeaders
22 var $tableHeaders = array();
27 * @param string $typeOfSearch type of the search (Software, Forum, People and so on)
28 * @param string $words words we are searching for
29 * @param int $offset offset
30 * @param boolean $isExact if we want to search for all the words or if only one matching the query is sufficient
31 * @param object $searchQuery SearchQuery instance
33 function HtmlSearchRenderer($typeOfSearch, $words, $isExact, $searchQuery) {
34 $this->SearchRenderer($typeOfSearch, $words, $isExact, $searchQuery);
38 * flush - flush the html output
41 $searchQuery =& $this->searchQuery;
42 if($searchQuery->isError()) {
44 echo '<div class="error">'.$searchQuery->getErrorMessage().'</div>';
47 $searchQuery->executeQuery();
48 if($searchQuery->getResult() && ($searchQuery->getRowsTotalCount() == 1 && $searchQuery->getOffset() == 0) && $this->implementsRedirectToResult()) {
49 $this->redirectToResult();
59 * writeHeader - write the header of the output
61 function writeHeader() {
65 * writeBody - write the body
67 function writeBody() {
68 echo $this->writeResults();
72 * writeFooter - write the footer
74 function writeFooter() {
75 $GLOBALS['HTML']->footer(array());
79 * getResults - get the html output which will display the search results
81 * @return string html output
83 function writeResults() {
84 $searchQuery =& $this->searchQuery;
85 $query =& $this->query;
87 $html = '<h1>'.sprintf(_('Search results for <em>%1$s</em>'), $query['words']).'</h1>';
88 if(!$searchQuery->getResult() || $searchQuery->getRowsCount() < 1) {
89 $html .= '<p><strong>'.sprintf(_('No matches found for <em>%1$s</em>'), $query['words']).'</strong></p>';
92 $html .= $GLOBALS['HTML']->listTableTop($this->tableHeaders);
93 $html .= $this->getRows();
94 $html .= $GLOBALS['HTML']->listTableBottom();
97 if($searchQuery->getRowsCount() > 0 && ($searchQuery->getRowsTotalCount() > $searchQuery->getRowsCount() || $searchQuery->getOffset() != 0 )) {
98 $html .= $this->getNavigationPanel();
105 * getNavigationPanel - get the html output for the navigation panel
107 * @return string html output
109 function getNavigationPanel() {
110 $searchQuery =& $this->searchQuery;
113 $html .= '<table class="tablecontent" width="100%" cellpadding="5" cellspacing="0">';
116 if ($searchQuery->getOffset() != 0) {
117 $html .= '<a href="'.$this->getPreviousResultsUrl().'" class="prev">'
118 . html_image('t2.png', '15', '15')
119 . ' '._('Previous Results').'</a>';
123 $html .= '</td><td align="right">';
124 if ($searchQuery->getRowsTotalCount() > $searchQuery->getRowsCount()) {
125 $html .= '<a href="'.$this->getNextResultsUrl().'" class="next">'
126 ._('Next Results').' '
127 . html_image('t.png', '15', '15') . '</a>';
131 $html .= '</td></tr>';
137 * getPreviousResultsUrl - get the url to go to see the previous results
139 * @return string url to previous results page
141 function getPreviousResultsUrl() {
142 $offset = $this->searchQuery->getOffset() - $this->searchQuery->getRowsPerPage();
143 $query =& $this->query;
145 $url = '/search/?type='.$query['typeOfSearch'].'&exact='.$query['isExact'].'&q='.urlencode($query['words']);
147 $url .= '&offset='.$offset;
153 * getNextResultsUrl - get the url to go to see the next results
155 * @return string url to next results page
157 function getNextResultsUrl() {
158 $query =& $this->query;
159 return '/search/?type='.$query['typeOfSearch'].'&exact='.$query['isExact'].'&q='.urlencode($query['words']).'&offset='.($this->searchQuery->getOffset() + $this->searchQuery->getRowsPerPage());
163 * highlightTargetWords - highlight the words we are looking for
165 * @param string $text text
166 * @return string text with keywords highlighted
168 function highlightTargetWords($text) {
172 $regexp = implode($this->searchQuery->getWords(), '|');
173 return preg_replace('/('.str_replace('/', '\/', $regexp).')/i','<span class="selected">\1</span>', $text);
177 * implementsRedirectToResult - check if the current object implements the redirect to result feature by having a redirectToResult method
179 * @return boolean true if our object implements search by id, false otherwise.
181 function implementsRedirectToResult() {
182 return method_exists($this, 'redirectToResult');
186 * getResultId - get the field value for the first row of a result handle
188 * @param string $fieldName field name
189 * @return string value of the field
191 function getResultId($fieldName) {
192 return db_result($this->searchQuery->getResult(), 0, $fieldName);