diff --git a/module/chart/control.php b/module/chart/control.php index 3abf7dd36e..7916f71d82 100644 --- a/module/chart/control.php +++ b/module/chart/control.php @@ -91,17 +91,22 @@ class chart extends control $type = $filter['type']; $default = isset($filter['default']) ? $filter['default'] : ''; + $saveAs = isset($filter['saveAs']) ? $filter['saveAs'] : ''; + $saveAsClass = $type == 'select' ? '' : 'hidden'; // 只有是选择的时候,才展示显示为 + $options = array(); if($type == 'select') { $fieldSetting = $fieldSettings[$field]; - $options = $this->chart->getSysOptions(zget($fieldSetting, 'type', ''), zget($fieldSetting, 'object', ''), zget($fieldSetting, 'field', ''), $sql); + $options = $this->chart->getSysOptions(zget($fieldSetting, 'type', ''), zget($fieldSetting, 'object', ''), zget($fieldSetting, 'field', ''), $sql, zget($filter, 'saveAs', '')); } $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', $fieldPairs, $field, "class='form-control picker-select' onchange='initFilterForm(this, this.value)'"); + $filterHtml['saveAs'] = html::select('saveAs', array('' => '') + $fieldPairs, $saveAs, "class='form-control picker-select' onchange='changeSaveAs(this, this.value)'"); + $filterHtml['saveAsClass'] = $saveAsClass; /* default html */ $filterHtml['default'] = ''; diff --git a/module/chart/model.php b/module/chart/model.php index a7d7bdb5d9..a545b287cc 100644 --- a/module/chart/model.php +++ b/module/chart/model.php @@ -654,10 +654,14 @@ class chartModel extends model * Get sys options. * * @param string $type + * @param string $object + * @param string $field + * @param string $sql + * @param string $saveAs * @access public * @return array */ - public function getSysOptions($type, $object = '', $field = '', $sql = '') + public function getSysOptions($type, $object = '', $field = '', $sql = '', $saveAs = '') { $options = array('' => ''); switch($type) @@ -691,20 +695,24 @@ class chartModel extends model case 'object': if($field) { - $table = zget($this->config->objectTables, $object, ''); - if($table) $options = $this->dao->select("id, {$field}")->from($table)->fetchPairs(); + if($sql and $saveAs) + { + $options = $this->getOptionsFromSql($sql, $field, $saveAs); + } + else + { + $table = zget($this->config->objectTables, $object, ''); + if($table) $options = $this->dao->select("id, {$field}")->from($table)->fetchPairs(); + } } break; case 'string': case 'number': if($field and $sql) { - $cols = $this->dbh->query($sql)->fetchAll(); - foreach($cols as $col) - { - $data = $col->$field; - $options[$data] = $data; - } + $keyField = $field; + $valueField = $saveAs ? $saveAs : $field; + $options = $this->getOptionsFromSql($sql, $keyField, $valueField); } break; } @@ -712,6 +720,33 @@ class chartModel extends model return $options; } + /** + * Get pairs from column by keyField and valueField. + * + * @param string $sql + * @param string $keyField + * @param string $valueField + * @access public + * @return array + */ + public function getOptionsFromSql($sql, $keyField, $valueField) + { + $options = array(); + $cols = $this->dbh->query($sql)->fetchAll(); + $sample = current($cols); + + if(!isset($sample->$keyField) or !isset($sample->$valueField)) return $options; + + foreach($cols as $col) + { + $key = $col->$keyField; + $value = $col->$valueField; + $options[$key] = $value; + } + + return $options; + } + public function getFilterFormat($filters) { $filterFormat = array();