Fields.php
14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<?php
namespace app\admin\controller\facrm;
use app\common\controller\Backend;
use app\common\model\Config;
use think\Db;
/**
* 自定义字段
*
* @icon fa fa-circle-o
*/
class Fields extends Backend
{
/**
* Fields模型对象
*/
protected $model = null;
protected $modelValidate = true;
protected $modelSceneValidate = true;
protected $noNeedRight = ['get_fields_html', 'rulelist', 'getTableFields','selectpage'];
protected $multiFields = 'isfilter,islist';
private $modelList = array();
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\facrm\Fields;
$this->view->assign("statusList", $this->model->getStatusList());
$this->view->assign('typeList', Config::getTypeList());
$this->view->assign('regexList', Config::getRegexList());
$this->modelList = ['customer' => __('客户'), 'customer_contacts' => __('联系人'),'contract'=>__("合同"),'contract_receivables'=>__("回款"),'business'=>__("商机"),'clues'=>__("线索")];
$this->request->filter(['strip_tags']);
}
/**
* 查看
*/
public function index()
{
$source = $this->request->param('source', 'customer');
if (!isset($this->modelList[$source])) {
$this->error(__("错误类型"));
}
$condition = ['source' => $source];
$prefix = \think\Config::get('database.prefix');
//设置过滤方法
$this->request->filter(['strip_tags']);
if ($this->request->isAjax()) {
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$total = $this->model
->where($condition)
->where($where)
->order($sort, $order)
->count();
$list = $this->model
->where($condition)
->where($where)
->order($sort, $order)
->limit($offset, $limit)
->select();
$fieldList = $this->getTableFields("{$prefix}facrm_{$source}",false);
$list = collection($list)->toArray();
$temp_arr = array_column($list, 'name');
foreach ($fieldList as $field) {
if (array_search($field['name'], $temp_arr) !== false) {
continue;
}
$item = [
'id' => $field['name'],
'state' => false,
'source' => '-',
'name' => $field['name'],
'title' => $field['title'],
'type' => $field['type'],
'issystem' => true,
'isfilter' => 0,
'islist' => 0,
'isorder' => 0,
'status' => 'normal',
'createtime' => 0,
'updatetime' => 0
];
$list[] = $item;
}
$result = array("total" => $total, "rows" => $list);
return json($result);
}
$this->assignconfig('params', "/source/{$source}");
$this->assignconfig('source', $source);
$this->view->assign('source', $source);
$this->view->assign('modelList', $this->modelList);
return $this->view->fetch();
}
/**
* 添加
*/
public function add()
{
$source = $this->request->param('source');
if (!isset($this->modelList[$source])) {
$this->error(__("错误类型"));
}
$this->view->assign('source', $source);
$this->renderTable();
return parent::add();
}
/**
* 编辑
*/
public function edit($ids = null)
{
$this->renderTable();
return parent::edit($ids);
}
/**
* 渲染表
*/
protected function renderTable()
{
$tableList = [];
$dbname = \think\Config::get('database.database');
$list = \think\Db::query("SELECT `TABLE_NAME`,`TABLE_COMMENT` FROM `information_schema`.`TABLES` where `TABLE_SCHEMA` = '{$dbname}';");
foreach ($list as $key => $row) {
$tableList[$row['TABLE_NAME']] = $row['TABLE_COMMENT'];
}
$this->view->assign("tableList", $tableList);
}
/**
* 批量操作
* @param string $ids
*/
public function multi($ids = "")
{
if (!$this->request->isPost()) {
$this->error(__("Invalid parameters"));
}
return parent::multi($ids);
}
/**
* 获取表字段信息
* @param string $table 表名
* @return array
* @internal
*/
public function getTableFields($table="",$return_ajax=true)
{
if (!$table){
$this->success(__("表名不能为空"));
}
$dbname = \config('database.database');
//从数据库中获取表字段信息
$sql = "SELECT * FROM `information_schema`.`columns` WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? ORDER BY ORDINAL_POSITION";
//加载主表的列
$columnList = Db::query($sql, [$dbname, $table]);
$fieldlist = [];
foreach ($columnList as $index => $item) {
$fieldlist[] = ['name' => $item['COLUMN_NAME'], 'title' => $item['COLUMN_COMMENT'], 'type' => $item['DATA_TYPE']];
}
if ($return_ajax){
$this->success("", null, ['fieldList' => $fieldlist]);
}
return $fieldlist;
}
/**
* 获取自定义字段
* @internal
*/
public function get_fields_html($source = "",$tpl = "fields")
{
$html="facrm/common/".$tpl;
switch ($source) {
case 'customer':
$this->model = new \app\admin\model\facrm\Customer();
break;
case 'customer_contacts':
$this->model =new \app\admin\model\facrm\customer\Contacts();
break;
case 'contract':
$html="facrm/common/fields_4_12";
$this->model = new \app\admin\model\facrm\Contract();
break;
case 'contract_receivables':
$html="facrm/common/fields_4_12";
$this->model = new \app\admin\model\facrm\contract\Receivables();
break;
case 'business':
$html="facrm/common/fields_4_12";
$this->model = new \app\admin\model\facrm\Business();
break;
case 'clues':
$this->model = new \app\admin\model\facrm\Clues();
break;
default:
$this->error();
}
$this->view->engine->layout(false);
$id = $this->request->param('id', 0, 'intval');
$values = [];
if ($id > 0) {
$values = $this->model->find($id);
}
$fields = \app\admin\model\facrm\Fields::getCustomFields($source, $values);
$this->view->assign('fields', $fields);
$this->view->assign('values', $values);
$this->success('', null, ['html' => $this->view->fetch($html)]);
}
/**
* 规则列表
* @internal
*/
public function rulelist()
{
//主键
$primarykey = $this->request->request("keyField");
//主键值
$keyValue = $this->request->request("keyValue", "");
$keyValueArr = array_filter(explode(',', $keyValue));
$regexList = Config::getRegexList();
$list = [];
foreach ($regexList as $k => $v) {
if ($keyValueArr) {
if (in_array($k, $keyValueArr)) {
$list[] = ['id' => $k, 'name' => $v];
}
} else {
$list[] = ['id' => $k, 'name' => $v];
}
}
return json(['list' => $list]);
}
/**
* 关联表联动
* @internal
*/
public function selectpage()
{
$id = $this->request->get("id/d", 0);
$fieldInfo = $this->model::get($id);
if (!$fieldInfo) {
$this->error("未找到指定字段");
}
$setting = $fieldInfo['setting'];
if (!$setting || !isset($setting['table'])) {
$this->error("字段配置不正确");
}
//搜索关键词,客户端输入以空格分开,这里接收为数组
$word = (array)$this->request->request("q_word/a");
//当前页
$page = $this->request->request("pageNumber");
//分页大小
$pagesize = $this->request->request("pageSize");
//搜索条件
$andor = $this->request->request("andOr", "and", "strtoupper");
//排序方式
$orderby = (array)$this->request->request("orderBy/a");
//显示的字段
//$field = $this->request->request("showField");
$field = $setting['field'];
//主键
//$primarykey = $this->request->request("keyField");
$primarykey = $setting['primarykey'];
//主键值
$primaryvalue = $this->request->request("keyValue");
//搜索字段
$searchfield = (array)$this->request->request("searchField/a");
$searchfield = [$field, $primarykey];
//自定义搜索条件
$custom = (array)$this->request->request("custom/a");
$custom = isset($setting['conditions']) ? (array)json_decode($setting['conditions'], true) : [];
$custom = array_filter($custom);
$admin_id = session('admin.id') ?: 0;
$user_id = $this->auth->id ?: 0;
//如果是管理员需要移除user_id筛选,否则会导致管理员无法筛选列表信息
$admin = $this->request->request("admin/d");
if ($admin_id && $admin) {
unset($custom['user_id']);
} else {
//如果不是管理员则需要判断是否开放相应的投稿字段
if (!in_array($fieldInfo['source'], array_keys($this->modelList))) {
$this->error("未开放栏目信息");
}
}
//是否返回树形结构
$istree = $this->request->request("isTree", 0);
$ishtml = $this->request->request("isHtml", 0);
if ($istree) {
$word = [];
$pagesize = 99999;
}
$order = [];
foreach ($orderby as $k => $v) {
$order[$v[0]] = $v[1];
}
$field = $field ? $field : 'name';
//如果有primaryvalue,说明当前是初始化传值
if ($primaryvalue !== null) {
$where = [$primarykey => ['in', $primaryvalue]];
$where = function ($query) use ($primaryvalue, $custom, $admin_id, $user_id) {
$query->where('id', 'in', $primaryvalue);
if ($custom && is_array($custom)) {
//替换暂位符
$search = ["{admin_id}", "{user_id}"];
$replace = [$admin_id, $user_id];
foreach ($custom as $k => $v) {
(is_array($v)&&json_decode($v))?$v=json_decode($v,true):'';
if (is_array($v) && 2 == count($v)) {
$query->where($k, trim($v[0]), str_replace($search, $replace, $v[1]));
} elseif (is_array($v) && 1 == count($v)){
$query->where($k, key($v), str_replace($search, $replace, reset($v)));
} else {
$query->where($k, '=', str_replace($search, $replace, $v));
}
}
}
};
$pagesize = 99999;
} else {
$where = function ($query) use ($word, $andor, $field, $searchfield, $custom, $admin_id, $user_id) {
$logic = $andor == 'AND' ? '&' : '|';
$searchfield = is_array($searchfield) ? implode($logic, $searchfield) : $searchfield;
$word = array_filter($word);
if ($word) {
foreach ($word as $k => $v) {
$query->where(str_replace(',', $logic, $searchfield), "like", "%{$v}%");
}
}
if ($custom && is_array($custom)) {
//替换暂位符
$search = ["{admin_id}", "{user_id}"];
$replace = [$admin_id, $user_id];
foreach ($custom as $k => $v) {
if (!is_array($v)){
json_decode($v)?$v=json_decode($v,true):'';
}
if (is_array($v) && 2 == count($v)) {
$query->where($k, trim($v[0]), str_replace($search, $replace, $v[1]));
} elseif (is_array($v) && 1 == count($v)){
$query->where($k, key($v), str_replace($search, $replace, reset($v)));
}else {
$query->where($k, '=', str_replace($search, $replace, $v));
}
}
}
};
}
$list = [];
$total = Db::table($setting['table'])->where($where)->count();
if ($total > 0) {
$datalist = Db::table($setting['table'])->where($where)
->order($order)
->page($page, $pagesize)
->field($primarykey . "," . $field . ($istree ? ",pid" : ""))
->select();
foreach ($datalist as $index => &$item) {
unset($item['password'], $item['salt']);
$list[] = [
$primarykey => isset($item[$primarykey]) ? $item[$primarykey] : '',
$field => isset($item[$field]) ? $item[$field] : '',
'pid' => isset($item['pid']) ? $item['pid'] : 0
];
}
if ($istree && !$primaryvalue) {
$tree = Tree::instance();
$tree->init($list, 'pid');
$list = $tree->getTreeList($tree->getTreeArray(0), $field);
if (!$ishtml) {
foreach ($list as &$item) {
$item = str_replace(' ', ' ', $item);
}
unset($item);
}
}
}
//这里一定要返回有list这个字段,total是可选的,如果total<=list的数量,则会隐藏分页按钮
return json(['list' => $list, 'total' => $total]);
}
}