Config.php
4.8 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
<?php
namespace app\school\controller\general;
use app\common\controller\ManystoreBase;
use app\common\library\Email;
use app\common\model\ManystoreConfigGroup;
use app\common\model\ManystoreConfig as ManystoreConfigModel;
use think\Cache;
use think\Exception;
use think\Validate;
/**
* 系统配置
*
* @icon fa fa-cogs
* @remark 可以在此增改系统的变量和分组,也可以自定义分组和变量,如果需要删除请从数据库中删除
*/
class Config extends ManystoreBase
{
/**
* @var \app\common\model\Config
*/
protected $model = null;
protected $config_value_model = null;
public function _initialize()
{
parent::_initialize();
$this->model = model('ManystoreConfig');
$this->config_value_model = model('ManystoreValue');
}
/**
* 查看
*/
public function index()
{
$siteList = [];
$manystoreConfigGroup = new ManystoreConfigGroup();
$groupList = $manystoreConfigGroup->getGroupData();
foreach ($groupList as $k => $v) {
$siteList[$k]['name'] = $k;
$siteList[$k]['title'] = $v;
$siteList[$k]['list'] = [];
}
$config_value_data_array = [];
$config_value_data = collection($this->config_value_model->where(array('school_id' => SCHOOL_ID))->select())->toArray();
foreach ($config_value_data as $value) {
$config_value_data_array[$value['config_id']] = $value;
}
foreach ($this->model->all() as $k => $v) {
if (!isset($siteList[$v['group']])) {
continue;
}
$value = $v->toArray();
$value['title'] = __($value['title']);
if (in_array($value['type'], ['select', 'selects', 'checkbox', 'radio'])) {
$value['value'] = explode(',', isset($config_value_data_array[$value['id']]['value']) ? $config_value_data_array[$value['id']]['value'] : $value['default']);
} else {
$value['value'] = isset($config_value_data_array[$value['id']]['value']) ? $config_value_data_array[$value['id']]['value'] : $value['default'];
}
$value['content'] = json_decode($value['content'], TRUE);
$siteList[$v['group']]['list'][] = $value;
}
$index = 0;
foreach ($siteList as $k => &$v) {
$v['active'] = !$index ? true : false;
$index++;
}
$this->view->assign('siteList', $siteList);
return $this->view->fetch();
}
/**
* 编辑
* @param null $ids
*/
public function edit($ids = NULL)
{
if ($this->request->isPost()) {
$row = $this->request->post("row/a");
if ($row) {
$configValueAll = [];
$config_value_data_array = [];
$config_value_data = collection($this->config_value_model->where(array('school_id' => SCHOOL_ID))->select())->toArray();
foreach ($config_value_data as $value) {
$config_value_data_array[$value['config_id']] = $value;
}
foreach ($this->model->all() as $v) {
if (isset($row[$v['name']])) {
$value = $row[$v['name']];
if (is_array($value) && isset($value['field'])) {
$value = json_encode(ManystoreConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE);
} else {
$value = is_array($value) ? implode(',', $value) : $value;
}
$v['value'] = $value;
$config = $v->toArray();
$config_value = array();
if (!empty($config_value_data_array[$v['id']])) {
$config_value['id'] = $config_value_data_array[$v['id']]['id'];
}
$config_value['school_id'] = SCHOOL_ID;
$config_value['store_id'] = STORE_ID;
$config_value['config_id'] = $config['id'];
$config_value['value'] = $value;
$configValueAll[] = $config_value;
}
}
$this->config_value_model->allowField(true)->saveAll($configValueAll);
try {
$this->refreshFile();
$this->success();
} catch (Exception $e) {
$this->error($e->getMessage());
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
}
/**
* 刷新配置文件
*/
protected function refreshFile()
{
Cache::rm('manystore_config:' . SCHOOL_ID);
}
}