From ee8f7b50f9a54920ebe0312065e7e96214283aeb Mon Sep 17 00:00:00 2001 From: daitingting Date: Tue, 11 Apr 2023 14:59:11 +0800 Subject: [PATCH] * Process filter of chart. * Process group of report. --- db/update18.3.sql | 3 +- module/upgrade/model.php | 87 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 80 insertions(+), 10 deletions(-) diff --git a/db/update18.3.sql b/db/update18.3.sql index c80e86121a..a3cdd7a21b 100644 --- a/db/update18.3.sql +++ b/db/update18.3.sql @@ -26,7 +26,7 @@ CREATE TABLE IF NOT EXISTS `zt_pivot` ( `objects` mediumtext NULL, `settings` mediumtext NOT NULL, `filters` mediumtext NOT NULL, - `step` tinyint(1) unsigned NOT NULL DEFAULT '', + `step` tinyint(1) unsigned NOT NULL, `stage` enum('draft','published') NOT NULL DEFAULT 'draft', `builtin` enum('0', '1') NOT NULL DEFAULT '0', `createdBy` varchar(30) NOT NULL, @@ -46,7 +46,6 @@ ALTER TABLE `zt_chart` MODIFY `group` varchar(255) NOT NULL; ALTER TABLE `zt_chart` ADD `stage` enum('draft','published') NOT NULL DEFAULT 'draft' AFTER `sql`; ALTER TABLE `zt_chart` ADD `langs` text NULL AFTER `fields`; ALTER TABLE `zt_chart` ADD `step` tinyint(1) unsigned NOT NULL DEFAULT '0' AFTER `filters`; -ALTER TABLE `zt_chart` DROP `dataset`; ALTER TABLE `zt_dataview` ADD `langs` text NULL AFTER `fields`; diff --git a/module/upgrade/model.php b/module/upgrade/model.php index 6971a54a83..3c27f7aa06 100644 --- a/module/upgrade/model.php +++ b/module/upgrade/model.php @@ -8692,7 +8692,6 @@ class upgradeModel extends model } $data->settings = json_encode(array($settings)); - $data->filters = json_encode($filters); if(isset($dataviewList[$chart->dataset])) { @@ -8706,6 +8705,41 @@ class upgradeModel extends model $data->fields = json_encode($fieldSettings); } + if(!empty($filters)) + { + $where = array(); + $operatorMap = array('in' => 'IN', 'notin' => 'NOT IN', 'notnull' => 'IS NOT NULL', 'null' => 'IS NULL'); + foreach($filters as $filter) + { + $operator = zget($operatorMap, $filter->operator); + $value = ''; + if(!in_array($filter->operator, array('null', 'notnull'))) + { + if(!is_array($filter->value)) + { + $value = "'" . $filter->value . "'"; + } + else + { + $values = array(); + foreach($filter->value as $v) $values[] = $type == 'number' ? $v : "'" . $v . "'"; + $value = '(' . implode(',', $values) . ')'; + } + } + + $where[] = $filter->field . ' ' . $operator . ' ' . $value; + } + + if(stripos($data->sql, 'where') === false) + { + $data->sql .= ' WHERE ' . implode(' AND ', $where); + } + else + { + $data->sql .= ' ' . implode(' AND ', $where); + } + } + $this->dao->update(TABLE_CHART)->data($data)->autoCheck()->where('id')->eq($chart->id)->exec(); } @@ -8763,6 +8797,7 @@ class upgradeModel extends model $pivotSettings = new stdclass(); $tableSettings = json_decode($table->settings); + $filters = array(); if($tableSettings) { $index = 1; @@ -8793,7 +8828,8 @@ class upgradeModel extends model $pivotSettings->columns = $columns; $pivot->settings = json_encode($pivotSettings); - $pivot->filters = json_encode($tableSettings->filter); + + if(!empty($tableSettings->filter)) $filters = $tableSettings->filter; } if(isset($dataviewList[$table->dataset])) @@ -8808,6 +8844,41 @@ class upgradeModel extends model $pivot->fields = json_encode($fieldSettings); } + if(!empty($filters)) + { + $where = array(); + $operatorMap = array('in' => 'IN', 'notin' => 'NOT IN', 'notnull' => 'IS NOT NULL', 'null' => 'IS NULL'); + foreach($filters as $filter) + { + $operator = zget($operatorMap, $filter->operator); + $value = ''; + if(!in_array($filter->operator, array('null', 'notnull'))) + { + if(!is_array($filter->value)) + { + $value = "'" . $filter->value . "'"; + } + else + { + $values = array(); + foreach($filter->value as $v) $values[] = $type == 'number' ? $v : "'" . $v . "'"; + $value = '(' . implode(',', $values) . ')'; + } + } + + $where[] = $filter->field . ' ' . $operator . ' ' . $value; + } + + if(stripos($pivot->sql, 'where') === false) + { + $pivot->sql .= ' WHERE ' . implode(' AND ', $where); + } + else + { + $pivot->sql .= ' ' . implode(' AND ', $where); + } + } + /* Process fieldSettings. */ $this->dao->insert(TABLE_PIVOT)->data($pivot)->autoCheck()->exec(); @@ -8830,8 +8901,7 @@ class upgradeModel extends model { $reports = $this->dao->select('*')->from(TABLE_REPORT)->fetchAll(); - $modulePairs = $this->dao->select('id, name')->from(TABLE_MODULE)->where('type')->eq('report')->fetchPairs(); - $groupNames = $this->dao->select('name, id')->from(TABLE_MODULE)->where('type')->eq('pivot')->fetchPairs(); + $groupCollectors = $this->dao->select('collector, id')->from(TABLE_MODULE)->where('type')->eq('pivot')->fetchPairs(); $this->loadModel('pivot'); @@ -8850,14 +8920,15 @@ class upgradeModel extends model $data->createdDate = $report->addedDate; /* Process group. */ - $modules = explode($report->module, ','); + $modules = explode(',', $report->module); $groups = array(); foreach($modules as $module) { - $moduleName = zget($modulePairs, $module, ''); - if(isset($groupNames[$moduleName])) + if(!$module) continue; + + if(isset($groupCollectors[$module])) { - $groups[] = $groupNames[$moduleName]; + $groups[] = $groupCollectors[$module]; } else {