|
@@ -38,139 +38,142 @@ class Study extends Backend |
|
@@ -38,139 +38,142 @@ class Study extends Backend |
38
|
$this->view->assign("genderList", $this->model->getGenderList());
|
38
|
$this->view->assign("genderList", $this->model->getGenderList());
|
39
|
}
|
39
|
}
|
40
|
|
40
|
|
41
|
-
|
|
|
42
|
- /*
|
|
|
43
|
- * 导入
|
|
|
44
|
- */
|
|
|
45
|
public function import(){
|
41
|
public function import(){
|
46
|
- $file = $this->request->request('file');
|
|
|
47
|
- if (!$file) {
|
|
|
48
|
- $this->error(__('Parameter %s can not be empty', 'file'));
|
|
|
49
|
- }
|
|
|
50
|
- $filePath = ROOT_PATH . DS . 'public' . DS . $file;
|
|
|
51
|
- if (!is_file($filePath)) {
|
|
|
52
|
- $this->error(__('No results were found'));
|
|
|
53
|
- }
|
|
|
54
|
- //实例化reader
|
|
|
55
|
- $ext = pathinfo($filePath, PATHINFO_EXTENSION);
|
|
|
56
|
- if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
|
|
|
57
|
- $this->error(__('Unknown data format'));
|
|
|
58
|
- }
|
|
|
59
|
- if ($ext === 'csv') {
|
|
|
60
|
- $file = fopen($filePath, 'r');
|
|
|
61
|
- $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
|
|
|
62
|
- $fp = fopen($filePath, 'w');
|
|
|
63
|
- $n = 0;
|
|
|
64
|
- while ($line = fgets($file)) {
|
|
|
65
|
- $line = rtrim($line, "\n\r\0");
|
|
|
66
|
- $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
|
|
|
67
|
- if ($encoding !== 'utf-8') {
|
|
|
68
|
- $line = mb_convert_encoding($line, 'utf-8', $encoding);
|
|
|
69
|
- }
|
|
|
70
|
- if ($n == 0 || preg_match('/^".*"$/', $line)) {
|
|
|
71
|
- fwrite($fp, $line . "\n");
|
|
|
72
|
- } else {
|
|
|
73
|
- fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
|
|
|
74
|
- }
|
|
|
75
|
- $n++;
|
|
|
76
|
- }
|
|
|
77
|
- fclose($file) || fclose($fp);
|
|
|
78
|
-
|
|
|
79
|
- $reader = new Csv();
|
|
|
80
|
- } elseif ($ext === 'xls') {
|
|
|
81
|
- $reader = new Xls();
|
|
|
82
|
- } else {
|
|
|
83
|
- $reader = new Xlsx();
|
|
|
84
|
- }
|
|
|
85
|
-
|
|
|
86
|
- //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
|
|
|
87
|
- $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment';
|
|
|
88
|
-
|
|
|
89
|
- $table = $this->model->getQuery()->getTable();
|
|
|
90
|
- $database = \think\Config::get('database.database');
|
|
|
91
|
- $fieldArr = [];
|
|
|
92
|
- $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]);
|
|
|
93
|
- foreach ($list as $k => $v) {
|
|
|
94
|
- if ($importHeadType == 'comment') {
|
|
|
95
|
- $v['COLUMN_COMMENT'] = explode(':', $v['COLUMN_COMMENT'])[0]; //字段备注有:时截取
|
|
|
96
|
- $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
|
|
|
97
|
- } else {
|
|
|
98
|
- $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
|
|
|
99
|
- }
|
|
|
100
|
- }
|
|
|
101
|
-
|
|
|
102
|
- //加载文件
|
|
|
103
|
- $insert = [];
|
|
|
104
|
- try {
|
|
|
105
|
- if (!$PHPExcel = $reader->load($filePath)) {
|
|
|
106
|
- $this->error(__('Unknown data format'));
|
|
|
107
|
- }
|
|
|
108
|
- $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
|
|
|
109
|
- $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
|
|
|
110
|
- $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
|
|
|
111
|
- $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
|
|
|
112
|
- $fields = [];
|
|
|
113
|
- for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
|
|
|
114
|
- for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
|
|
|
115
|
- $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
|
|
|
116
|
- $fields[] = $val;
|
|
|
117
|
- }
|
|
|
118
|
- }
|
|
|
119
|
-
|
|
|
120
|
- for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
|
|
|
121
|
- $values = [];
|
|
|
122
|
- for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
|
|
|
123
|
- $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
|
|
|
124
|
- $values[] = is_null($val) ? '' : $val;
|
|
|
125
|
- }
|
|
|
126
|
- $row = [];
|
|
|
127
|
- $temp = array_combine($fields, $values);
|
|
|
128
|
- foreach ($temp as $k => $v) {
|
|
|
129
|
- if (isset($fieldArr[$k]) && $k !== '') {
|
|
|
130
|
- $row[$fieldArr[$k]] = $v;
|
|
|
131
|
- }
|
|
|
132
|
- }
|
|
|
133
|
- if ($row) {
|
|
|
134
|
- $insert[] = $row;
|
|
|
135
|
- }
|
|
|
136
|
- }
|
|
|
137
|
- } catch (Exception $exception) {
|
|
|
138
|
- $this->error($exception->getMessage());
|
|
|
139
|
- }
|
|
|
140
|
- if (!$insert) {
|
|
|
141
|
- $this->error(__('No rows were updated'));
|
|
|
142
|
- }
|
|
|
143
|
-
|
|
|
144
|
- try {
|
|
|
145
|
- //是否包含admin_id字段
|
|
|
146
|
- $has_admin_id = false;
|
|
|
147
|
- foreach ($fieldArr as $name => $key) {
|
|
|
148
|
- if ($key == 'admin_id') {
|
|
|
149
|
- $has_admin_id = true;
|
|
|
150
|
- break;
|
|
|
151
|
- }
|
|
|
152
|
- }
|
|
|
153
|
- if ($has_admin_id) {
|
|
|
154
|
- $auth = Auth::instance();
|
|
|
155
|
- foreach ($insert as &$val) {
|
|
|
156
|
- if (!isset($val['admin_id']) || empty($val['admin_id'])) {
|
|
|
157
|
- $val['admin_id'] = $auth->isLogin() ? $auth->id : 0;
|
|
|
158
|
- }
|
|
|
159
|
- }
|
|
|
160
|
- }
|
|
|
161
|
- $this->model->saveAll($insert);
|
|
|
162
|
- } catch (PDOException $exception) {
|
|
|
163
|
- $msg = $exception->getMessage();
|
|
|
164
|
- if (preg_match("/.+Integrity constraint violation: 1062 Duplicate entry '(.+)' for key '(.+)'/is", $msg, $matches)) {
|
|
|
165
|
- $msg = "导入失败,包含【{$matches[1]}】的记录已存在";
|
|
|
166
|
- };
|
|
|
167
|
- $this->error($msg);
|
|
|
168
|
- } catch (Exception $e) {
|
|
|
169
|
- $this->error($e->getMessage());
|
42
|
+ return parent::import();
|
170
|
}
|
43
|
}
|
171
|
|
44
|
|
172
|
- $this->success();
|
|
|
173
|
- }
|
45
|
+// /*
|
|
|
46
|
+// * 导入
|
|
|
47
|
+// */
|
|
|
48
|
+// public function import(){
|
|
|
49
|
+// $file = $this->request->request('file');
|
|
|
50
|
+// if (!$file) {
|
|
|
51
|
+// $this->error(__('Parameter %s can not be empty', 'file'));
|
|
|
52
|
+// }
|
|
|
53
|
+// $filePath = ROOT_PATH . DS . 'public' . DS . $file;
|
|
|
54
|
+// if (!is_file($filePath)) {
|
|
|
55
|
+// $this->error(__('No results were found'));
|
|
|
56
|
+// }
|
|
|
57
|
+// //实例化reader
|
|
|
58
|
+// $ext = pathinfo($filePath, PATHINFO_EXTENSION);
|
|
|
59
|
+// if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
|
|
|
60
|
+// $this->error(__('Unknown data format'));
|
|
|
61
|
+// }
|
|
|
62
|
+// if ($ext === 'csv') {
|
|
|
63
|
+// $file = fopen($filePath, 'r');
|
|
|
64
|
+// $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
|
|
|
65
|
+// $fp = fopen($filePath, 'w');
|
|
|
66
|
+// $n = 0;
|
|
|
67
|
+// while ($line = fgets($file)) {
|
|
|
68
|
+// $line = rtrim($line, "\n\r\0");
|
|
|
69
|
+// $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
|
|
|
70
|
+// if ($encoding !== 'utf-8') {
|
|
|
71
|
+// $line = mb_convert_encoding($line, 'utf-8', $encoding);
|
|
|
72
|
+// }
|
|
|
73
|
+// if ($n == 0 || preg_match('/^".*"$/', $line)) {
|
|
|
74
|
+// fwrite($fp, $line . "\n");
|
|
|
75
|
+// } else {
|
|
|
76
|
+// fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
|
|
|
77
|
+// }
|
|
|
78
|
+// $n++;
|
|
|
79
|
+// }
|
|
|
80
|
+// fclose($file) || fclose($fp);
|
|
|
81
|
+//
|
|
|
82
|
+// $reader = new Csv();
|
|
|
83
|
+// } elseif ($ext === 'xls') {
|
|
|
84
|
+// $reader = new Xls();
|
|
|
85
|
+// } else {
|
|
|
86
|
+// $reader = new Xlsx();
|
|
|
87
|
+// }
|
|
|
88
|
+//
|
|
|
89
|
+// //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
|
|
|
90
|
+// $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment';
|
|
|
91
|
+//
|
|
|
92
|
+// $table = $this->model->getQuery()->getTable();
|
|
|
93
|
+// $database = \think\Config::get('database.database');
|
|
|
94
|
+// $fieldArr = [];
|
|
|
95
|
+// $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]);
|
|
|
96
|
+// foreach ($list as $k => $v) {
|
|
|
97
|
+// if ($importHeadType == 'comment') {
|
|
|
98
|
+// $v['COLUMN_COMMENT'] = explode(':', $v['COLUMN_COMMENT'])[0]; //字段备注有:时截取
|
|
|
99
|
+// $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
|
|
|
100
|
+// } else {
|
|
|
101
|
+// $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
|
|
|
102
|
+// }
|
|
|
103
|
+// }
|
|
|
104
|
+//
|
|
|
105
|
+// //加载文件
|
|
|
106
|
+// $insert = [];
|
|
|
107
|
+// try {
|
|
|
108
|
+// if (!$PHPExcel = $reader->load($filePath)) {
|
|
|
109
|
+// $this->error(__('Unknown data format'));
|
|
|
110
|
+// }
|
|
|
111
|
+// $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
|
|
|
112
|
+// $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
|
|
|
113
|
+// $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
|
|
|
114
|
+// $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
|
|
|
115
|
+// $fields = [];
|
|
|
116
|
+// for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
|
|
|
117
|
+// for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
|
|
|
118
|
+// $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
|
|
|
119
|
+// $fields[] = $val;
|
|
|
120
|
+// }
|
|
|
121
|
+// }
|
|
|
122
|
+//
|
|
|
123
|
+// for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
|
|
|
124
|
+// $values = [];
|
|
|
125
|
+// for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
|
|
|
126
|
+// $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
|
|
|
127
|
+// $values[] = is_null($val) ? '' : $val;
|
|
|
128
|
+// }
|
|
|
129
|
+// $row = [];
|
|
|
130
|
+// $temp = array_combine($fields, $values);
|
|
|
131
|
+// foreach ($temp as $k => $v) {
|
|
|
132
|
+// if (isset($fieldArr[$k]) && $k !== '') {
|
|
|
133
|
+// $row[$fieldArr[$k]] = $v;
|
|
|
134
|
+// }
|
|
|
135
|
+// }
|
|
|
136
|
+// if ($row) {
|
|
|
137
|
+// $insert[] = $row;
|
|
|
138
|
+// }
|
|
|
139
|
+// }
|
|
|
140
|
+// } catch (Exception $exception) {
|
|
|
141
|
+// $this->error($exception->getMessage());
|
|
|
142
|
+// }
|
|
|
143
|
+// if (!$insert) {
|
|
|
144
|
+// $this->error(__('No rows were updated'));
|
|
|
145
|
+// }
|
|
|
146
|
+//
|
|
|
147
|
+// try {
|
|
|
148
|
+// //是否包含admin_id字段
|
|
|
149
|
+// $has_admin_id = false;
|
|
|
150
|
+// foreach ($fieldArr as $name => $key) {
|
|
|
151
|
+// if ($key == 'admin_id') {
|
|
|
152
|
+// $has_admin_id = true;
|
|
|
153
|
+// break;
|
|
|
154
|
+// }
|
|
|
155
|
+// }
|
|
|
156
|
+// if ($has_admin_id) {
|
|
|
157
|
+// $auth = Auth::instance();
|
|
|
158
|
+// foreach ($insert as &$val) {
|
|
|
159
|
+// if (!isset($val['admin_id']) || empty($val['admin_id'])) {
|
|
|
160
|
+// $val['admin_id'] = $auth->isLogin() ? $auth->id : 0;
|
|
|
161
|
+// }
|
|
|
162
|
+// }
|
|
|
163
|
+// }
|
|
|
164
|
+// $this->model->saveAll($insert);
|
|
|
165
|
+// } catch (PDOException $exception) {
|
|
|
166
|
+// $msg = $exception->getMessage();
|
|
|
167
|
+// if (preg_match("/.+Integrity constraint violation: 1062 Duplicate entry '(.+)' for key '(.+)'/is", $msg, $matches)) {
|
|
|
168
|
+// $msg = "导入失败,包含【{$matches[1]}】的记录已存在";
|
|
|
169
|
+// };
|
|
|
170
|
+// $this->error($msg);
|
|
|
171
|
+// } catch (Exception $e) {
|
|
|
172
|
+// $this->error($e->getMessage());
|
|
|
173
|
+// }
|
|
|
174
|
+//
|
|
|
175
|
+// $this->success();
|
|
|
176
|
+// }
|
174
|
|
177
|
|
175
|
/**
|
178
|
/**
|
176
|
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
179
|
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|