From 5ea07fee8ccc78e570ef2d004e9c077e9870865d Mon Sep 17 00:00:00 2001 From: xieqiyu Date: Mon, 27 Nov 2023 16:01:21 +0800 Subject: [PATCH] * Code for task#106653, #106654. --- module/chart/control.php | 93 ++++++---------------------------------- module/chart/zen.php | 91 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 81 deletions(-) diff --git a/module/chart/control.php b/module/chart/control.php index 5cb27b57c1..d439cbb178 100644 --- a/module/chart/control.php +++ b/module/chart/control.php @@ -59,106 +59,39 @@ class chart extends control } /** + * 获取筛选器表单。 * Ajax get filter form html. * - * @param int $chartID * @access public * @return string */ - public function ajaxGetFilterForm($chartID) + public function ajaxGetFilterForm() { - $fieldList = $this->post->fieldList; - $filters = $this->post->filters; - $fieldSettings = $this->post->fieldSettings; - $langs = $this->post->langs; - $sql = $this->post->sql; - $clientLang = $this->app->getClientLang(); + $filters = $this->post->filters; - $fieldPairs = array(); - foreach($fieldList as $field => $fieldName) - { - $fieldObject = $fieldSettings[$field]['object']; - $relatedField = $fieldSettings[$field]['field']; - - $this->app->loadLang($fieldObject); - $fieldPairs[$field] = isset($this->lang->$fieldObject->$relatedField) ? $this->lang->$fieldObject->$relatedField : $field; - - if(!isset($langs[$field])) continue; - if(!empty($langs[$field][$clientLang])) $fieldPairs[$field] = $langs[$field][$clientLang]; - } + /* 获取关联字段下拉菜单数据。*/ + /* Get the options for the related field in filter. */ + $fields = $this->chartZen->getOptions4SelectField(); $htmls = array(); foreach($filters as $filter) { - $field = $filter['field']; - $type = $filter['type']; - $default = isset($filter['default']) ? $filter['default'] : ''; - - $options = array(); - if($type == 'select') - { - $fieldSetting = $fieldSettings[$field]; - $options = $this->chart->getSysOptions(zget($fieldSetting, 'type', ''), zget($fieldSetting, 'object', ''), zget($fieldSetting, 'field', ''), $sql); - } + $field = $filter['field']; + $type = $filter['type']; $filterHtml = array(); /* field html */ - $filterHtml['field'] = html::select('field', $fieldPairs, $field, "class='form-control picker-select' onchange='initFilterForm(this, this.value)'"); + $filterHtml['field'] = html::select('field', $fields, $field, "class='form-control picker-select' onchange='initFilterForm(this, this.value)'"); /* default html */ - $filterHtml['default'] = ''; - - if($type == 'input') $filterHtml['default'] .= html::input('default', $default, "class='form-control' onchange='changeDefault(this,this.value)'"); - if($type == 'date' or $type == 'datetime') - { - if(empty($default)) $default = array('begin' => '', 'end' => ''); - $class = $type == 'date' ? 'form-date' : 'form-datetime'; - $filterHtml['default'] .= '
'; - $filterHtml['default'] .= html::input('default[begin]', $default['begin'], "class='form-control $class default-begin' placeholder='{$this->lang->chart->unlimited}' onchange='changeDefault(this,this.value)'"); - $filterHtml['default'] .= "{$this->lang->chart->colon}"; - $filterHtml['default'] .= html::input('default[end]', $default['end'], "class='form-control $class default-end' placeholder='{$this->lang->chart->unlimited}' onchange='changeDefault(this,this.value)'"); - $filterHtml['default'] .= '
'; - } - if($type == 'select') $filterHtml['default'] = html::select('default[]', $options, $default, "class='form-control picker-select' onchange='changeDefault(this,this.value)' multiple"); - if($type == 'condition') - { - $operator = isset($filter['operator']) ? $filter['operator'] : ''; - $value = isset($filter['value']) ? $filter['value'] : ''; - $hidden = strpos($operator, 'NULL') !== false ? 'hidden' : ''; - $filterHtml['default'] = "
"; - $filterHtml['default'] .= html::select('operator', $this->config->bi->conditionList, $operator, "class='form-control picker-select' onchange='changeCondition(this,this.value)'"); - $filterHtml['default'] .= html::input('value', $value, "class='form-control $hidden' onchange='changeConditionValue(this,this.value)'"); - $filterHtml['default'] .= "
"; - } + $filterHtml['default'] = $this->chartZen->getHTML('default', $type, $filter); /* type html */ $filterHtml['type'] = html::select('type', $this->lang->chart->fieldTypeList, $type, "class='form-control picker-select' onchange='changeType(this, this.value)'"); /* filter item html */ - $filterHtml['item'] = ''; - if($type == 'input') $filterHtml['item'] .= html::input('default', $default, "class='form-control'"); - if($type == 'date' or $type == 'datetime') - { - if(empty($default)) $default = array('begin' => '', 'end' => ''); - $class = $type == 'date' ? 'form-date' : 'form-datetime'; - $filterHtml['item'] .= '
'; - $filterHtml['item'] .= ""; - $filterHtml['item'] .= '' . $this->lang->chart->colon . ''; - $filterHtml['item'] .= ""; - $filterHtml['item'] .= '
'; - } - if($type == 'select') $filterHtml['item'] .= html::select('default', $options, $default, "class='form-control picker-select' multiple"); - if($type == 'condition') - { - $operator = isset($filter['operator']) ? $filter['operator'] : ''; - $value = isset($filter['value']) ? $filter['value'] : ''; - $hidden = strpos($operator, 'NULL') !== false ? 'hidden' : ''; - $filterHtml['item'] = "
"; - $filterHtml['item'] .= html::select('operator', $this->config->bi->conditionList, $operator, "class='form-control picker-select' onchange='changeCondition(this,this.value, true)'"); - $filterHtml['item'] .= html::input('value', $value, "class='form-control $hidden'"); - $filterHtml['item'] .= "
"; - } + $filterHtml['item'] = $this->chartZen->getHTML('item', $type, $filter); $htmls[] = $filterHtml; } @@ -167,6 +100,7 @@ class chart extends control } /** + * 获取图表。 * Ajax get chart. * * @access public @@ -176,13 +110,10 @@ class chart extends control { $post = fixer::input('post')->skipSpecial('settings,filters,sql,langs')->get(); - $chartID = $post->id; $settings = current($post->settings); $type = $settings['type']; $filters = isset($_POST['searchFilters']) ? $_POST['searchFilters'] : (isset($_POST['filters']) ? $post->filters : array()); - $chart = $this->chart->getByID($chartID); - $filterFormat = $this->chart->getFilterFormat($filters); $sql = str_replace(';', '', "$post->sql"); diff --git a/module/chart/zen.php b/module/chart/zen.php index b1e414c392..0cee5c94dc 100644 --- a/module/chart/zen.php +++ b/module/chart/zen.php @@ -47,4 +47,95 @@ class chartZen extends chart return $chart; } + + /** + * 获取筛选器表单中关联字段下拉菜单数据。 + * Get the options for the related field in filter. + * + * @access protected + * @return array + */ + protected function getOptions4SelectField(): array + { + $options = array(); + $fieldList = $this->post->fieldList; + $fieldSettings = $this->post->fieldSettings; + $langs = $this->post->langs; + $clientLang = $this->app->getClientLang(); + + foreach($fieldList as $field => $fieldName) + { + $fieldObject = $fieldSettings[$field]['object']; + $relatedField = $fieldSettings[$field]['field']; + + $this->app->loadLang($fieldObject); + $options[$field] = isset($this->lang->$fieldObject->$relatedField) ? $this->lang->$fieldObject->$relatedField : $field; + + if(!isset($langs[$field])) continue; + if(!empty($langs[$field][$clientLang])) $options[$field] = $langs[$field][$clientLang]; + } + + return $options; + } + + /** + * 获取筛选器中默认值和查询的HTML。 + * Get the HTML for the default value and search in filter. + * + * @param string $key default|item + * @param string $type input|date|datetime|select|condition + * @param array $filter + * @access protected + * @return string + */ + protected function getHTML(string $key, string $type, array $filter): string + { + $html = ''; + $default = isset($filter['default']) ? $filter['default'] : ''; + + if($type == 'input') + { + $onChange = $key == 'default' ? "onchange='changeDefault(this,this.value)'" : ''; + $html = html::input('default', $default, "class='form-control' $onChange"); + } + + if($type == 'date' || $type == 'datetime') + { + if(empty($default)) $default = array('begin' => '', 'end' => ''); + $class = $type == 'date' ? 'form-date' : 'form-datetime'; + + $html = '
'; + $html .= html::input('default[begin]', $default['begin'], "class='form-control $class default-begin' autocomplete='off' placeholder='{$this->lang->chart->unlimited}' onchange='changeDefault(this, this.value)'"); + $html .= "{$this->lang->chart->colon}"; + $html .= html::input('default[end]', $default['end'], "class='form-control $class default-end' autocomplete='off' placeholder='{$this->lang->chart->unlimited}' onchange='changeDefault(this, this.value)'"); + $html .= '
'; + } + + if($type == 'select') + { + $options = array(); + $fieldSettings = $this->post->fieldSettings; + $fieldSetting = $fieldSettings[$field]; + $options = $this->chart->getSysOptions(zget($fieldSetting, 'type', ''), zget($fieldSetting, 'object', ''), zget($fieldSetting, 'field', ''), $this->post->sql); + $onChange = $key == 'default' ? "onchange='changeDefault(this, this.value)'" : ''; + + $html = html::select('default[]', $options, $default, "class='form-control picker-select' $onChange multiple"); + } + + if($type == 'condition') + { + $operator = isset($filter['operator']) ? $filter['operator'] : ''; + $value = isset($filter['value']) ? $filter['value'] : ''; + $hidden = strpos($operator, 'NULL') !== false ? 'hidden' : ''; + $onChange = $key == 'default' ? "onchange='changeConditionValue(this,this.value)'" : ''; + $showSearch = $key == 'item' ? true : false; + + $html = "
"; + $html .= html::select('operator', $this->config->bi->conditionList, $operator, "class='form-control picker-select' onchange='changeCondition(this, this.value, $showSearch)'"); + $html .= html::input('value', $value, "class='form-control $hidden' $onChange"); + $html .= "
"; + } + + return $html; + } }