diff --git a/module/pivot/control.php b/module/pivot/control.php index 4090ece2e2..07859e2bf5 100644 --- a/module/pivot/control.php +++ b/module/pivot/control.php @@ -263,16 +263,17 @@ class pivot extends control * @param string $type * @param string $object * @param string $field + * @param string $saveAs * @access public * @return string */ - public function ajaxGetSysOptions($type, $object = '', $field = '') + public function ajaxGetSysOptions($type, $object = '', $field = '', $saveAs = '') { $sql = isset($_POST['sql']) ? $_POST['sql'] : ''; $filters = isset($_POST['filters']) ? $_POST['filters'] : ''; $sql = $this->loadModel('chart')->parseSqlVars($sql, $filters); - $options = $this->pivot->getSysOptions($type, $object, $field, $sql); + $options = $this->pivot->getSysOptions($type, $object, $field, $sql, $saveAs); return print(html::select('default[]', array('' => '') + $options, '', "class='form-control form-select picker-select' multiple")); } diff --git a/module/pivot/js/preview.js b/module/pivot/js/preview.js index 21e1cab111..e9dc38b7f6 100644 --- a/module/pivot/js/preview.js +++ b/module/pivot/js/preview.js @@ -260,8 +260,9 @@ function initQueryControl(container, filter, object, field) function initResultControl(container, filter, object, field) { - var type = filter.type; - var value = filter.default; + var type = filter.type; + var value = filter.default; + var saveAs = 'saveAs' in filter ? filter.saveAs : ''; container.find('.default-block').addClass('hidden'); container.find('.default-block input[name^="default"]').val(''); @@ -307,7 +308,7 @@ function initResultControl(container, filter, object, field) if(value !== '') pickerOptions.defaultValue = value; var pivotParams = {sql: pivot.sql, filters: getQueryFilters(pivot)}; - $.post(createLink('pivot', 'ajaxGetSysOptions', 'type=' + options + '&object=' + object + '&field=' + field), pivotParams, function(resp) + $.post(createLink('pivot', 'ajaxGetSysOptions', 'type=' + options + '&object=' + object + '&field=' + field + '&saveAs=' + saveAs), pivotParams, function(resp) { control.html($(resp).html()); control.picker(pickerOptions); diff --git a/module/pivot/lang/de.php b/module/pivot/lang/de.php index 5083b02e48..48d4356d14 100644 --- a/module/pivot/lang/de.php +++ b/module/pivot/lang/de.php @@ -225,6 +225,7 @@ $lang->pivot->resultFilter = 'Result Filter'; $lang->pivot->queryFilter = 'Query Filter'; $lang->pivot->noName = 'Unnamed'; $lang->pivot->filterName = 'Name'; +$lang->pivot->showAs = 'Show as'; $lang->pivot->default = 'Default'; $lang->pivot->unlimited = 'Unlimited'; $lang->pivot->colon = 'To'; diff --git a/module/pivot/lang/en.php b/module/pivot/lang/en.php index cf85a8187e..018b85e55f 100644 --- a/module/pivot/lang/en.php +++ b/module/pivot/lang/en.php @@ -225,6 +225,7 @@ $lang->pivot->resultFilter = 'Result Filter'; $lang->pivot->queryFilter = 'Query Filter'; $lang->pivot->noName = 'Unnamed'; $lang->pivot->filterName = 'Name'; +$lang->pivot->showAs = 'Show as'; $lang->pivot->default = 'Default'; $lang->pivot->unlimited = 'Unlimited'; $lang->pivot->colon = 'To'; diff --git a/module/pivot/lang/fr.php b/module/pivot/lang/fr.php index 7ec1d431e3..0233a6e7e5 100644 --- a/module/pivot/lang/fr.php +++ b/module/pivot/lang/fr.php @@ -225,6 +225,7 @@ $lang->pivot->resultFilter = 'Result Filter'; $lang->pivot->queryFilter = 'Query Filter'; $lang->pivot->noName = 'Unnamed'; $lang->pivot->filterName = 'Name'; +$lang->pivot->showAs = 'Show as'; $lang->pivot->default = 'Default'; $lang->pivot->unlimited = 'Unlimited'; $lang->pivot->colon = 'To'; diff --git a/module/pivot/lang/vi.php b/module/pivot/lang/vi.php index 7514749bb1..1fdac495a2 100644 --- a/module/pivot/lang/vi.php +++ b/module/pivot/lang/vi.php @@ -213,6 +213,7 @@ $lang->pivot->resultFilter = 'Result Filter'; $lang->pivot->queryFilter = 'Query Filter'; $lang->pivot->noName = 'Unnamed'; $lang->pivot->filterName = 'Name'; +$lang->pivot->showAs = 'Show as'; $lang->pivot->default = 'Default'; $lang->pivot->unlimited = 'Unlimited'; $lang->pivot->colon = 'To'; diff --git a/module/pivot/lang/zh-cn.php b/module/pivot/lang/zh-cn.php index 820e6890fb..f5947a78d3 100644 --- a/module/pivot/lang/zh-cn.php +++ b/module/pivot/lang/zh-cn.php @@ -225,6 +225,7 @@ $lang->pivot->resultFilter = '结果筛选器'; $lang->pivot->queryFilter = '查询筛选器'; $lang->pivot->noName = '未命名'; $lang->pivot->filterName = '名称'; +$lang->pivot->showAs = '显示为'; $lang->pivot->default = '默认值'; $lang->pivot->unlimited = '不限'; $lang->pivot->colon = '至'; diff --git a/module/pivot/model.php b/module/pivot/model.php index ca8983d0c3..d8b0c0d34f 100644 --- a/module/pivot/model.php +++ b/module/pivot/model.php @@ -2482,7 +2482,7 @@ class pivotModel extends model * @access public * @return array */ - public function getSysOptions($type, $object = '', $field = '', $source = '') + public function getSysOptions($type, $object = '', $field = '', $source = '', $saveAs = '') { $options = array('' => ''); switch($type) @@ -2524,7 +2524,10 @@ class pivotModel extends model { $options = array(); foreach($source as $row) $options[$row->id] = $row->$field; - + } + elseif($saveAs) + { + $options = $this->getOptionsFromSql($source, $field, $saveAs); } else { @@ -2538,17 +2541,15 @@ class pivotModel extends model } break; case 'string': + case 'number': if($field) { $options = array(); if(is_string($source) and !empty($source)) { - $cols = $this->dbh->query($source)->fetchAll(); - foreach($cols as $col) - { - $data = $col->$field; - $options[$data] = $data; - } + $keyField = $field; + $valueField = $saveAs ? $saveAs : $field; + $options = $this->getOptionsFromSql($source, $keyField, $valueField); } else if(is_array($source)) { @@ -2560,6 +2561,33 @@ class pivotModel 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; + } + /** * Get tree of pivots and groups. *