PageRenderTime 70ms CodeModel.GetById 43ms RepoModel.GetById 0ms app.codeStats 1ms

/gforge/www/search/include/renderers/HtmlSearchRenderer.class.php

https://github.com/neymanna/fusionforge
PHP | 205 lines | 107 code | 25 blank | 73 comment | 23 complexity | 8469685fa617b0310be81843a906ed7f MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * GForge Search Engine
  4. *
  5. * Portions Copyright 1999-2001 (c) VA Linux Systems
  6. * The rest Copyright 2004 (c) Guillaume Smet / Open Wide
  7. *
  8. * http://gforge.org
  9. *
  10. * @version $Id$
  11. */
  12. require_once $gfwww.'search/include/renderers/SearchRenderer.class.php';
  13. class HtmlSearchRenderer extends SearchRenderer {
  14. /**
  15. * Headers of the HTML results table
  16. *
  17. * @var array $tableHeaders
  18. */
  19. var $tableHeaders = array();
  20. /**
  21. * Constructor
  22. *
  23. * @param string $typeOfSearch type of the search (Software, Forum, People and so on)
  24. * @param string $words words we are searching for
  25. * @param int $offset offset
  26. * @param boolean $isExact if we want to search for all the words or if only one matching the query is sufficient
  27. * @param object $searchQuery SearchQuery instance
  28. */
  29. function HtmlSearchRenderer($typeOfSearch, $words, $isExact, $searchQuery) {
  30. $this->SearchRenderer($typeOfSearch, $words, $isExact, $searchQuery);
  31. }
  32. /**
  33. * flush - flush the html output
  34. */
  35. function flush() {
  36. $searchQuery =& $this->searchQuery;
  37. if($searchQuery->isError()) {
  38. $this->writeHeader();
  39. echo '<h2>'.$searchQuery->getErrorMessage().'</h2>';
  40. $this->writeFooter();
  41. } else {
  42. $searchQuery->executeQuery();
  43. if($searchQuery->getResult() && ($searchQuery->getRowsTotalCount() == 1 && $searchQuery->getOffset() == 0) && $this->implementsRedirectToResult()) {
  44. $this->redirectToResult();
  45. } else {
  46. $this->writeHeader();
  47. $this->writeBody();
  48. $this->writeFooter();
  49. }
  50. }
  51. }
  52. /**
  53. * writeHeader - write the header of the output
  54. */
  55. function writeHeader() {
  56. echo '<div align="center">';
  57. echo $GLOBALS['HTML']->searchBox();
  58. echo '</div>';
  59. }
  60. /**
  61. * writeBody - write the body
  62. */
  63. function writeBody() {
  64. echo $this->writeResults();
  65. }
  66. /**
  67. * writeFooter - write the footer
  68. */
  69. function writeFooter() {
  70. $GLOBALS['HTML']->footer(array());
  71. }
  72. /**
  73. * getResults - get the html output which will display the search results
  74. *
  75. * @return string html output
  76. */
  77. function writeResults() {
  78. $searchQuery =& $this->searchQuery;
  79. $query =& $this->query;
  80. $html = '';
  81. if(!$searchQuery->getResult() || $searchQuery->getRowsCount() < 1) {
  82. $html .= '<h2>'.sprintf(_('No matches found for <em>%1$s</em>'), htmlspecialchars($query['words'])).'</h2>';
  83. $html .= db_error();
  84. } else {
  85. $html .= '<h3>'.sprintf(_('Search results for <em>%1$s</em>'), htmlspecialchars($query['words'])).'</h3>';
  86. $html .= $GLOBALS['HTML']->listTableTop($this->tableHeaders);
  87. $html .= $this->getRows();
  88. $html .= $GLOBALS['HTML']->listTableBottom();
  89. }
  90. if($searchQuery->getRowsCount() > 0 && ($searchQuery->getRowsTotalCount() > $searchQuery->getRowsCount() || $searchQuery->getOffset() != 0 )) {
  91. $html .= $this->getNavigationPanel();
  92. }
  93. return $html;
  94. }
  95. /**
  96. * getNavigationPanel - get the html output for the navigation panel
  97. *
  98. * @return string html output
  99. */
  100. function getNavigationPanel() {
  101. $searchQuery =& $this->searchQuery;
  102. $html = '';
  103. $html .= '<br />';
  104. $html .= '<table class="tablecontent" width="100%" cellpadding="5" cellspacing="0">';
  105. $html .= '<tr>';
  106. $html .= '<td>';
  107. if ($searchQuery->getOffset() != 0) {
  108. $html .= '<a href="'.$this->getPreviousResultsUrl().'" class="prev">'
  109. . html_image('t2.png', '15', '15', array('border'=>'0','align'=>'middle'))
  110. . ' '._('Previous Results').'</a>';
  111. } else {
  112. $html .= '&nbsp;';
  113. }
  114. $html .= '</td><td align="right">';
  115. if ($searchQuery->getRowsTotalCount() > $searchQuery->getRowsCount()) {
  116. $html .= '<a href="'.$this->getNextResultsUrl().'" class="next">'
  117. ._('Next Results').' '
  118. . html_image('t.png', '15', '15', array('border'=>'0','align'=>'middle')) . '</a>';
  119. } else {
  120. $html .= '&nbsp;';
  121. }
  122. $html .= '</td></tr>';
  123. $html .= '</table>';
  124. return $html;
  125. }
  126. /**
  127. * getPreviousResultsUrl - get the url to go to see the previous results
  128. *
  129. * @return string url to previous results page
  130. */
  131. function getPreviousResultsUrl() {
  132. $offset = $this->searchQuery->getOffset() - $this->searchQuery->getRowsPerPage();
  133. $query =& $this->query;
  134. $url = '/search/?type='.$query['typeOfSearch'].'&amp;exact='.$query['isExact'].'&amp;q='.urlencode($query['words']);
  135. if($offset > 0) {
  136. $url .= '&amp;offset='.$offset;
  137. }
  138. return $url;
  139. }
  140. /**
  141. * getNextResultsUrl - get the url to go to see the next results
  142. *
  143. * @return string url to next results page
  144. */
  145. function getNextResultsUrl() {
  146. $query =& $this->query;
  147. return '/search/?type='.$query['typeOfSearch'].'&amp;exact='.$query['isExact'].'&amp;q='.urlencode($query['words']).'&amp;offset='.($this->searchQuery->getOffset() + $this->searchQuery->getRowsPerPage());
  148. }
  149. /**
  150. * highlightTargetWords - highlight the words we are looking for
  151. *
  152. * @param string $text text
  153. * @return string text with keywords highlighted
  154. */
  155. function highlightTargetWords($text) {
  156. if (empty($text)) {
  157. return '&nbsp;';
  158. }
  159. $regexp = implode($this->searchQuery->getWords(), '|');
  160. return preg_replace('/('.str_replace('/', '\/', $regexp).')/i','<span class="selected">\1</span>', $text);
  161. }
  162. /**
  163. * implementsRedirectToResult - check if the current object implements the redirect to result feature by having a redirectToResult method
  164. *
  165. * @return boolean true if our object implements search by id, false otherwise.
  166. */
  167. function implementsRedirectToResult() {
  168. return method_exists($this, 'redirectToResult');
  169. }
  170. /**
  171. * getResultId - get the field value for the first row of a result handle
  172. *
  173. * @param string $fieldName field name
  174. * @return string value of the field
  175. */
  176. function getResultId($fieldName) {
  177. return db_result($this->searchQuery->getResult(), 0, $fieldName);
  178. }
  179. }
  180. ?>