PageRenderTime 65ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/class/bbs/SearchResult.class.php

https://github.com/hylinux/ltebbs
PHP | 321 lines | 187 code | 72 blank | 62 comment | 62 complexity | 41e4eab44f102dd41a8949858a25c0d0 MD5 | raw file
  1. <?php
  2. //vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3 foldcolumn=1 foldmethod=marker:
  3. /**
  4. * 项目: 5anet(BBS)
  5. * 文件: class/bbs/SearchResult.class.php
  6. *
  7. * 查看论坛搜索的结果
  8. *
  9. * PHP Version 5
  10. *
  11. * @package: class.bbs
  12. * @author: Mike.G Chinese Name: 黄叶 <hylinux@gmail.com>
  13. * @license: http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
  14. * @copyright: http://www.5anet.com
  15. * @version: $Id: SearchResult.class.php,v 1.2 2006-09-24 14:38:08 ghw Exp $
  16. * @date: $Date: 2006-09-24 14:38:08 $
  17. */
  18. include_once CLASS_PATH.'main/BaseAction.class.php';
  19. //包含需要用到的函数
  20. include_once FUNCTION_PATH.'utf8_substr.fun.php';
  21. include_once FUNCTION_PATH.'set_locale_time.fun.php';
  22. //包含需要用到的类
  23. include_once CLASS_PATH.'bbs/LayoutUtil.class.php';
  24. include_once CLASS_PATH.'user/UserUtil.class.php';
  25. //include the language file
  26. if ( file_exists(LANG_PATH.SYSTEM_LANG.'/SearchResult.lang.php') ) {
  27. include_once LANG_PATH.SYSTEM_LANG.'/SearchResult.lang.php';
  28. }
  29. class SearchResult extends BaseAction {
  30. /**
  31. * 数据库的连接
  32. */
  33. public $db;
  34. /**
  35. * 每一页显示帖子数
  36. */
  37. private $page_number = 25;
  38. /**
  39. * 构造函数
  40. * @param: NULL
  41. * @return: NULL
  42. * @access: public
  43. */
  44. public function __construct() {
  45. $this->db = $this->getCacheDB();
  46. }
  47. /**
  48. * 显示版面的情况
  49. * @param: NULL
  50. * @return: NULL
  51. * @access: public
  52. */
  53. public function run() {
  54. //取得用户的id
  55. $user_id = UserUtil::getUserId($this->db, $_SESSION['user']['name']);
  56. $smarty = $this->getSmarty();
  57. //取得站点的公告,并显示在页面上
  58. $is_have_post = false;
  59. $post_str = '';
  60. if ( PostUtil::haveNotExpirePost($this->getDB()) ) {
  61. $is_have_post = true;
  62. $post_array = PostUtil::getPost($this->getDB(), 3);
  63. foreach ( $post_array as $post_rows ) {
  64. $post_str .= '<a href=\'index.php?module=post&action=view&id='.
  65. $post_rows['id'].'\' title=\''.$post_rows['title'].'\'>'.
  66. utf8_substr($post_rows['title'], 0, 35).'</a>'.
  67. '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  68. }
  69. }
  70. $smarty->assign('have_system_post', $is_have_post);
  71. $smarty->assign('post_str', $post_str);
  72. //公告显示结束
  73. $q = $this->getParameterFromGET('q');
  74. $encode_q = urlencode($q);
  75. //取得查询字符串
  76. if ( !$q ) {
  77. $where_sql = '';
  78. //收集查询的变量
  79. //按关键字查询
  80. $word = $this->getParameter('word');
  81. //按用户名来查询
  82. $find_user = $this->getParameter('user');
  83. if ( !$find_user && !$word ) {
  84. $this->AlertAndBack(SR_NO_FIND_KEYWORD);
  85. return;
  86. }
  87. $word = ltrim($word);
  88. $word = rtrim($word);
  89. //按空格拆分关键字
  90. $word_array = preg_split("/[\s,]+/", $word);
  91. //看用户的搜索是按关键字,还是按帖子的内容
  92. $is_topic = $this->getParameter('wordtarget');
  93. if ( $is_topic != 1 && $is_topic != 2 ) {
  94. $is_topic = 1;
  95. }
  96. $i = 0;
  97. if ( count($word_array) > 1 ) {
  98. $where_sql .= " and ( ";
  99. foreach ( $word_array as $value ) {
  100. if ( $is_topic == 1 ) {
  101. $where_sql .= " title like '%".$value."%' ";
  102. if ( $i < count($word_array) - 1 ) {
  103. $where_sql .= " or ";
  104. }
  105. } else if ( $is_topic == 2 ) {
  106. $where_sql .= " MATCH(content) AGAINST('".$value."') ";
  107. if ( $i < count($word_array) - 1 ) {
  108. $where_sql .= " or ";
  109. }
  110. }
  111. $i = $i + 1;
  112. }
  113. $where_sql .= " ) ";
  114. } else if ( count($word_array)== 1 && $word ) {
  115. if ( $is_topic == 1 ) {
  116. $where_sql .= " and title like '%".$word."%' ";
  117. } else if ( $is_topic == 2 ) {
  118. $where_sql .= " and match(content) against('".$word."')";
  119. }
  120. }
  121. //是按用户名来搜索的
  122. //收集用户名
  123. $find_user = ltrim($find_user);
  124. $find_user = rtrim($find_user);
  125. $find_user_array = preg_split("/[\s,]+/", $find_user);
  126. $is_match = $this->getParameter('usermatch');
  127. $i = 0;
  128. if ( count($find_user_array) > 1 ) {
  129. $where_sql .= " and ( ";
  130. foreach ( $find_user_array as $value ) {
  131. if ( $is_match ) {
  132. $where_sql .= " author='".$value."' ";
  133. if ( $i < count($find_user_array) - 1 ) {
  134. $where_sql .= " or ";
  135. }
  136. } else {
  137. $where_sql .= " author like '%".$value."%' ";
  138. if ( $i < count($find_user_array) - 1 ) {
  139. $where_sql .= " or ";
  140. }
  141. }
  142. $i = $i + 1;
  143. }
  144. $where_sql .= " ) ";
  145. } else if ( count($find_user_array)==1 && $find_user ) {
  146. if ( $is_match ) {
  147. $where_sql .= " and author ='".$find_user."' ";
  148. } else {
  149. $where_sql .= " and author like '%".$find_user."%' ";
  150. }
  151. }
  152. //再收集用户是否选择了论坛进行搜索。
  153. $layout = $this->getParameterFromPOST('layout');
  154. if ( $layout && !is_array($layout) ) {
  155. $this->AlertAndBack(SR_SYSTEM_REQUEST_ERROR);
  156. return;
  157. }
  158. $i = 0;
  159. if ( count ( $layout ) > 1 ) {
  160. $where_sql .= " and ( ";
  161. foreach ( $layout as $value ) {
  162. $where_sql .= " layout_id='".$value."' ";
  163. if ( $i < count($layout) - 1 ) {
  164. $where_sql .= " or ";
  165. }
  166. $i = $i + 1;
  167. }
  168. $where_sql .= " ) ";
  169. } else if ( count($layout)==1 ) {
  170. $where_sql .= " and layout_id='".$layout."' ";
  171. }
  172. $q = ' where 1 '.$where_sql;
  173. $encode_q = base64_encode($q);
  174. $encode_q = urlencode($encode_q);
  175. } else {
  176. // $q = urldecode($q);
  177. $q = base64_decode($q);
  178. }
  179. //求总的total number
  180. $smarty->assign('encode_q', $encode_q);
  181. //生成所有的记录数
  182. $sql = 'select count(*) as num from bbs_subject '.$q;
  183. $res = $this->db->Execute($sql);
  184. $rows = $res->FetchRow();
  185. $total_number = $rows['num'];
  186. //求总公的页面
  187. $total_page = ceil($total_number / $this->page_number );
  188. //取得当前的页面
  189. $page = $this->getParameter('page');
  190. if ( !$page || $page < 0 ) {
  191. $page = 1;
  192. }
  193. if ( $page > $total_page && $total_page > 0 ) {
  194. $page = $total_page;
  195. }
  196. $begin_page = 1;
  197. $end_page = $total_page;
  198. if ( $page <= 10 && $total_page >=10 ) {
  199. $end_page = 10;
  200. } else if ( $page > 10 ) {
  201. if ( $page % 10 == 0 ) {
  202. //向前翻
  203. $end_page = $page;
  204. $begin_page = $end_page - 9;
  205. } else if ( $page % 10 == 1 ) {
  206. //向后翻
  207. //确定开始的页数
  208. $begin_page = $page;
  209. if ( $begin_page > $total_page ) {
  210. $begin_page = $page - 9;
  211. }
  212. if ( ( $begin_page + 9 ) > $total_page ) {
  213. $end_page = $total_page;
  214. } else {
  215. $end_page = $begin_page + 9;
  216. }
  217. } else {
  218. $num = $page % 10;
  219. $pre_num = floor($page / 10 );
  220. $begin_page = $pre_num * 10 + 1;
  221. $end_page = $begin_page + 9;
  222. }
  223. }
  224. if ( $end_page > $total_page ) {
  225. $end_page = $total_page;
  226. }
  227. $nav_page_array = array();
  228. for( $i = $begin_page; $i<=$end_page; $i++ ) {
  229. array_push($nav_page_array, $i);
  230. }
  231. //帖子导航栏
  232. $smarty->assign('nav_page', $nav_page_array);
  233. //当前的页面
  234. $smarty->assign('now_page', $page);
  235. //共有的页面
  236. $smarty->assign('total_page', $total_page);
  237. //显示搜索结果
  238. //求出偏移
  239. $offset_number = ( $page - 1 ) * $this->page_number;
  240. $subject_array = LayoutUtil::getCacheSubjectInfo($this->db, $this->page_number,
  241. $offset_number, $q);
  242. if ( $total_page > 0 ) {
  243. $smarty->assign('subject', $subject_array);
  244. $smarty->assign('have_subject', 1);
  245. }
  246. $smarty->display('bbssearchresult.tmpl');
  247. }
  248. }
  249. ?>