* @package search * @version $Id: control.php 4129 2013-01-18 01:58:14Z wwccss $ * @link https://www.zentao.net */ class search extends control { public $search; /** * 构建搜索表单。 * Build search form. * * @param string $module * @param string $mode new 20版本后的新页面 | old 20版本前的旧页面 * @access public * @return void */ public function buildForm(string $module, string $mode = 'new') { $searchForm = $module . 'Form'; $searchParams = $module . 'searchParams'; $searchConfig = $this->search->processSearchParams($module); $fields = $searchConfig['fields'] ?? []; $params = $searchConfig['params'] ?? []; if(!$this->session->$searchForm) { $initFunc = $mode == 'new' ? 'initSession' : 'initOldSession'; $this->search->$initFunc($module, $fields, $params); } if(in_array($module, $this->config->search->searchObject) && $this->session->objectName) { $space = common::checkNotCN() ? ' ' : ''; $this->lang->search->common = $this->lang->search->common . $space . $this->session->objectName; } $this->view->module = $module; $this->view->fields = $fields; $this->view->fieldParams = $this->search->setDefaultParams($module, $fields, $params); $this->view->queries = $this->search->getQueryList($module); $this->view->actionURL = $this->session->$searchParams['actionURL']; $this->view->queryID = $this->session->$searchParams['queryID'] ?? 0; $this->view->style = $this->session->$searchParams['style'] ?? 'full'; $this->view->onMenuBar = $this->session->$searchParams['onMenuBar'] ?? 'no'; $this->view->formSession = $this->session->$searchForm; if($module == 'program') $this->view->options = $this->searchZen->setOptions($fields, $this->view->fieldParams, $this->view->queries); $this->display('search', $mode == 'new' ? 'buildForm' : 'buildOldForm'); } /** * 构建旧页面搜索表单。 * Build old search form. * * @param string $module * @access public * @return void */ public function buildOldForm(string $module) { $this->buildForm($module, 'old'); } /** * 构建搜索查询。 * Build search query. * * @param string $mode new 20版本后的新页面 | old 20版本前的旧页面 * @access public * @return void */ public function buildQuery(string $mode = 'new') { /* 将查询 sql 和 表单名字设置 session。*/ /* Set query sql and form name in session. */ $buildFunc = $mode == 'new' ? 'buildQuery' : 'buildOldQuery'; $this->search->$buildFunc(); $actionURL = $this->post->actionURL; $parsedURL = parse_url($actionURL); /* 查询链接中有 host 直接返回。*/ /* If action url has host, return. */ if(isset($parsedURL['host'])) return; /* 检查查询链接。*/ /* Check action url. */ if($this->config->requestType != 'GET') { $path = $parsedURL['path']; $path = str_replace($this->config->webRoot, '', $path); if(strpos($path, '.') !== false) $path = substr($path, 0, strpos($path, '.')); if(preg_match("/^\w+{$this->config->requestFix}\w+/", $path) == 0) return; } else { $query = $parsedURL['query']; if(preg_match("/^{$this->config->moduleVar}=\w+\&{$this->config->methodVar}=\w+/", $query) == 0) return; } if($mode == 'new') return print(json_encode(array('result' => 'success', 'load' => $actionURL))); echo js::locate($actionURL, 'parent'); } /** * Build query * * @access public * @return void */ public function buildOldQuery() { $this->buildQuery('old'); } /** * 保存搜索查询。 * Save search query. * * @param string $module * @param string $onMenuBar * @access public * @return void */ public function saveQuery(string $module, string $onMenuBar = 'no') { if($_POST) { $queryID = $this->search->saveQuery(); if(!$queryID) return $this->send(array('result' => 'fail', 'message' => dao::getError())); if($this->viewType == 'json') { echo 'success'; return; } if(!empty($_POST['onMenuBar'])) return $this->send(array('closeModal' => true, 'load' => true)); return $this->send(array('closeModal' => true, 'callback' => array('name' => 'zui.SearchForm.addQuery', 'params' => array(array('module' => $module, 'id' => $queryID, 'text' => $this->post->title))))); } $this->view->module = $module; $this->view->onMenuBar = $onMenuBar; $this->display(); } /** * Save old search query. * * @param string $module * @param string $onMenuBar * @access public * @return void */ public function saveOldQuery(string $module, string $onMenuBar = 'no') { if($_POST) { $queryID = $this->search->saveQuery(); if(!$queryID) return print(js::error(dao::getError())); $data = fixer::input('post')->get(); $shortcut = empty($data->onMenuBar) ? 0 : 1; return print(js::closeModal('parent.parent', '', "function(){parent.parent.loadQueries($queryID, $shortcut, '{$data->title}')}")); } $this->view->module = $module; $this->view->onMenuBar = $onMenuBar; $this->display(); } /** * 删除搜索查询。 * Delete current search query. * * @param int $queryID * @access public * @return void */ public function deleteQuery($queryID) { $this->search->deleteQuery($queryID); if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError())); return $this->send(array('result' => 'success', 'load' => true)); } /** * Ajax get search query. * * @param string $module * @param int $queryID * @access public * @return void */ public function ajaxGetQuery($module = '', $queryID = 0) { $query = $queryID ? $queryID : ''; $module = empty($module) ? $this->session->searchParams['module'] : $module; $queries = $this->search->getQueryList($module); $html = ''; foreach($queries as $query) { if(empty($query->id)) continue; $html .= '