control.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. use function zin\wg;
  3. /**
  4. * The control file of search module of ZenTaoPMS.
  5. *
  6. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  7. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  8. * @author Chunsheng Wang <chunsheng@cnezsoft.com>
  9. * @package search
  10. * @version $Id: control.php 4129 2013-01-18 01:58:14Z wwccss $
  11. * @link https://www.zentao.net
  12. */
  13. class search extends control
  14. {
  15. public $search;
  16. /**
  17. * 构建搜索表单。
  18. * Build search form.
  19. *
  20. * @param string $module
  21. * @param string $mode new 20版本后的新页面 | old 20版本前的旧页面
  22. * @access public
  23. * @return void
  24. */
  25. public function buildForm($module, $mode = 'new')
  26. {
  27. $searchForm = $module . 'Form';
  28. $searchParams = $module . 'searchParams';
  29. $searchConfig = $this->search->processSearchParams($module);
  30. $fields = $searchConfig['fields'] ?? [];
  31. $params = $searchConfig['params'] ?? [];
  32. if(!$this->session->$searchForm)
  33. {
  34. $initFunc = $mode == 'new' ? 'initSession' : 'initOldSession';
  35. $this->search->$initFunc($module, $fields, $params);
  36. }
  37. if(in_array($module, $this->config->search->searchObject) && $this->session->objectName)
  38. {
  39. $space = common::checkNotCN() ? ' ' : '';
  40. $this->lang->search->common = $this->lang->search->common . $space . $this->session->objectName;
  41. }
  42. $this->view->module = $module;
  43. $this->view->fields = $fields;
  44. $this->view->fieldParams = $this->search->setDefaultParams($module, $fields, $params);
  45. $this->view->queries = $this->search->getQueryList($module);
  46. $this->view->actionURL = $this->session->$searchParams['actionURL'];
  47. $this->view->queryID = $this->session->$searchParams['queryID'] ?? 0;
  48. $this->view->style = $this->session->$searchParams['style'] ?? 'full';
  49. $this->view->onMenuBar = $this->session->$searchParams['onMenuBar'] ?? 'no';
  50. $this->view->formSession = $this->session->$searchForm;
  51. if($module == 'program') $this->view->options = $this->searchZen->setOptions($fields, $this->view->fieldParams, $this->view->queries);
  52. $this->display('search', $mode == 'new' ? 'buildForm' : 'buildOldForm');
  53. }
  54. /**
  55. * 构建旧页面搜索表单。
  56. * Build old search form.
  57. *
  58. * @param string $module
  59. * @access public
  60. * @return void
  61. */
  62. public function buildOldForm($module)
  63. {
  64. $this->buildForm($module, 'old');
  65. }
  66. /**
  67. * 构建搜索查询。
  68. * Build search query.
  69. *
  70. * @param string $mode new 20版本后的新页面 | old 20版本前的旧页面
  71. * @access public
  72. * @return void
  73. */
  74. public function buildQuery($mode = 'new')
  75. {
  76. /* 将查询 sql 和 表单名字设置 session。*/
  77. /* Set query sql and form name in session. */
  78. $buildFunc = $mode == 'new' ? 'buildQuery' : 'buildOldQuery';
  79. $this->search->$buildFunc();
  80. $actionURL = $this->post->actionURL;
  81. $parsedURL = parse_url($actionURL);
  82. /* 查询链接中有 host 直接返回。*/
  83. /* If action url has host, return. */
  84. if(isset($parsedURL['host'])) return;
  85. /* 检查查询链接。*/
  86. /* Check action url. */
  87. if($this->config->requestType != 'GET')
  88. {
  89. $path = $parsedURL['path'];
  90. $path = str_replace($this->config->webRoot, '', $path);
  91. if(strpos($path, '.') !== false) $path = substr($path, 0, strpos($path, '.'));
  92. if(preg_match("/^\w+{$this->config->requestFix}\w+/", $path) == 0) return;
  93. }
  94. else
  95. {
  96. $query = $parsedURL['query'];
  97. if(preg_match("/^{$this->config->moduleVar}=\w+\&{$this->config->methodVar}=\w+/", $query) == 0) return;
  98. }
  99. if($mode == 'new') return print(json_encode(array('result' => 'success', 'load' => $actionURL)));
  100. echo js::locate($actionURL, 'parent');
  101. }
  102. /**
  103. * Build query
  104. *
  105. * @access public
  106. * @return void
  107. */
  108. public function buildOldQuery()
  109. {
  110. $this->buildQuery('old');
  111. }
  112. /**
  113. * 保存搜索查询。
  114. * Save search query.
  115. *
  116. * @param string $module
  117. * @param string $onMenuBar
  118. * @access public
  119. * @return void
  120. */
  121. public function saveQuery($module, $onMenuBar = 'no')
  122. {
  123. if($_POST)
  124. {
  125. $queryID = $this->search->saveQuery();
  126. if(!$queryID) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  127. if($this->viewType == 'json')
  128. {
  129. echo 'success';
  130. return;
  131. }
  132. if(!empty($_POST['onMenuBar'])) return $this->send(array('closeModal' => true, 'load' => true));
  133. return $this->send(array('closeModal' => true, 'callback' => array('name' => 'zui.SearchForm.addQuery', 'params' => array(array('module' => $module, 'id' => $queryID, 'text' => $this->post->title)))));
  134. }
  135. $this->view->module = $module;
  136. $this->view->onMenuBar = $onMenuBar;
  137. $this->display();
  138. }
  139. /**
  140. * Save old search query.
  141. *
  142. * @param string $module
  143. * @param string $onMenuBar
  144. * @access public
  145. * @return void
  146. */
  147. public function saveOldQuery($module, $onMenuBar = 'no')
  148. {
  149. if($_POST)
  150. {
  151. $queryID = $this->search->saveQuery();
  152. if(!$queryID) return print(js::error(dao::getError()));
  153. $data = fixer::input('post')->get();
  154. $shortcut = empty($data->onMenuBar) ? 0 : 1;
  155. return print(js::closeModal('parent.parent', '', "function(){parent.parent.loadQueries($queryID, $shortcut, '{$data->title}')}"));
  156. }
  157. $this->view->module = $module;
  158. $this->view->onMenuBar = $onMenuBar;
  159. $this->display();
  160. }
  161. /**
  162. * 删除搜索查询。
  163. * Delete current search query.
  164. *
  165. * @param int $queryID
  166. * @access public
  167. * @return void
  168. */
  169. public function deleteQuery($queryID)
  170. {
  171. $this->search->deleteQuery($queryID);
  172. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  173. return $this->send(array('result' => 'success', 'load' => true));
  174. }
  175. /**
  176. * Ajax get search query.
  177. *
  178. * @param string $module
  179. * @param int $queryID
  180. * @access public
  181. * @return void
  182. */
  183. public function ajaxGetQuery($module = '', $queryID = 0)
  184. {
  185. $query = $queryID ? $queryID : '';
  186. $module = empty($module) ? $this->session->searchParams['module'] : $module;
  187. $queries = $this->search->getQueryList($module);
  188. $html = '';
  189. foreach($queries as $query)
  190. {
  191. if(empty($query->id)) continue;
  192. $html .= '<li>' . html::a("javascript:executeQuery({$query->id})", $query->title . ((common::hasPriv('search', 'deleteQuery') and $this->app->user->account == $query->account) ? '<i class="icon icon-close"></i>' : ''), '', "class='label user-query' data-query-id='$query->id' title='{$query->title}'") . '</li>';
  193. }
  194. echo $html;
  195. }
  196. /**
  197. * 删除搜索查询菜单。
  198. * Ajax remove from menu.
  199. *
  200. * @param int $queryID
  201. * @access public
  202. * @return void
  203. */
  204. public function ajaxRemoveMenu($queryID)
  205. {
  206. $this->dao->update(TABLE_USERQUERY)->set('shortcut')->eq('0')->where('id')->eq($queryID)->exec();
  207. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  208. return $this->send(array('result' => 'success', 'load' => true));
  209. }
  210. /**
  211. * 建立索引。
  212. * Build All index.
  213. *
  214. * @param string $mode show|build
  215. * @param string $type
  216. * @param int $lastID
  217. * @access public
  218. * @return void
  219. */
  220. public function buildIndex($mode = 'show', $type = '', $lastID = 0)
  221. {
  222. if($mode == 'build')
  223. {
  224. if($this->config->edition != 'open') $this->loadModel('workflow')->appendSearchConfig();
  225. $result = $this->search->buildAllIndex($type, $lastID);
  226. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  227. if(!empty($result['finished'])) return $this->send(array('result' => 'finished', 'message' => $this->lang->search->buildSuccessfully));
  228. $type = zget($this->lang->search->modules, ($result['type'] == 'testcase' ? 'case' : $result['type']), $result['type']);
  229. return $this->send(array('result' => 'unfinished', 'message' => sprintf($this->lang->search->buildResult, $type, $type, $result['count']), 'type' => $type, 'count' => $result['count'], 'next' => inlink('buildIndex', "mode=build&type={$result['type']}&lastID={$result['lastID']}")));
  230. }
  231. $this->view->title = $this->lang->search->buildIndex;
  232. $this->display();
  233. }
  234. /**
  235. * 全局搜索结果页面。
  236. * Global search results home page.
  237. *
  238. * @param int $recTotal
  239. * @param int $pageID
  240. * @access public
  241. * @return void
  242. */
  243. public function index($recTotal = 0, $pageID = 1)
  244. {
  245. $this->lang->admin->menu->search = "{$this->lang->search->common}|search|index";
  246. /* 获取搜索的关键词。*/
  247. /* Get the words. */
  248. if(empty($words)) $words = $this->get->words;
  249. if(empty($words)) $words = $this->post->words;
  250. if(empty($words) && ($recTotal != 0 || $pageID != 1)) $words = $this->session->searchIngWord;
  251. $words = strip_tags(strtolower($words));
  252. /* 获取搜索类型。*/
  253. /* Get the type. */
  254. if(empty($type)) $type = $this->get->type;
  255. if(empty($type)) $type = $this->post->type;
  256. if(empty($type) && ($recTotal != 0 || $pageID != 1)) $type = $this->session->searchIngType;
  257. if(is_array($type)) $type = array_filter(array_unique($type));
  258. $type = (empty($type) || (is_array($type) && in_array('all', $type))) ? 'all' : $type;
  259. /* 追加工作流搜索配置。*/
  260. if($this->config->edition != 'open') $this->loadModel('workflow')->appendSearchConfig();
  261. /* 开始搜索时记录当时的时间。*/
  262. $begin = time();
  263. $this->app->loadClass('pager', $static = true);
  264. $pager = new pager(0, $this->config->search->recPerPage, $pageID);
  265. $results = $this->search->getList($words, $type, $pager);
  266. $uri = inlink('index', "recTotal=$pager->recTotal&pageID=$pager->pageID");
  267. $uri .= strpos($uri, '?') === false ? '?' : '&';
  268. $uri .= 'words=' . $words;
  269. $this->searchZen->setSessionForIndex($uri, $words, $type);
  270. $this->view->title = $this->lang->search->index;
  271. $this->view->results = $results;
  272. $this->view->consumed = time() - $begin;
  273. $this->view->type = $type;
  274. $this->view->typeList = $this->searchZen->getTypeList();
  275. $this->view->pager = $pager;
  276. $this->view->words = $words;
  277. $this->view->referer = $this->session->referer;
  278. $this->display();
  279. }
  280. }