-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathmodule.cls.php
executable file
·328 lines (282 loc) · 10.3 KB
/
module.cls.php
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
<?php
/*
* This file is part of the phpems/phpems.
*
* (c) oiuv <[email protected]>
*
* 项目维护:oiuv(QQ:7300637) | 定制服务:火眼(QQ:278768688)
*
* This source file is subject to the MIT license that is bundled.
*/
class module
{
public $G;
private $cache = [];
public function __construct(&$G)
{
$this->G = $G;
}
public function _init()
{
$this->sql = $this->G->make('sql');
$this->pdosql = $this->G->make('pdosql');
$this->db = $this->G->make('pepdo');
$this->pg = $this->G->make('pg');
$this->ev = $this->G->make('ev');
}
//查询模型
public function searchModules($args)
{
$data = [false, 'module', $args];
$sql = $this->pdosql->makeSelect($data);
return $this->db->fetchAll($sql, 'moduleid');
}
//根据应用获取模型
public function getModulesByApp($app)
{
$data = [false, 'module', [['AND', 'moduleapp = :app', 'app', $app]], false, 'moduleid DESC', false];
$sql = $this->pdosql->makeSelect($data);
return $this->db->fetchAll($sql, 'moduleid');
}
public function getModulesList($args, $page, $number = PN, $order = 'moduleid desc')
{
$data = [
'select' => false,
'table' => 'module',
'query' => $args,
'orderby' => $order,
'serial' => 'modulelockfields',
];
$r = $this->db->listElements($page, $number, $data);
return $r;
}
//根据ID获取模型
public function getModuleById($moduleid)
{
if (!$this->cache['module'][$moduleid]) {
$data = [false, 'module', [['AND', 'moduleid = :moduleid', 'moduleid', $moduleid]]];
$sql = $this->pdosql->makeSelect($data);
$this->cache['module'][$moduleid] = $this->db->fetch($sql, 'modulelockfields');
}
return $this->cache['module'][$moduleid];
}
//插入模型
public function insertModule($args)
{
if (!$args['moduleapp']) {
$args['moduleapp'] = $this->G->app;
}
if ('autoform' == $args['moduleapp']) {
$args['moduletable'] = 'autoform_'.$args['modulecode'];
$this->_insertDefaultTable($args['moduletable'], $args['modulecode'].'id');
}
$data = ['module', $args];
$sql = $this->pdosql->makeInsert($data);
$this->db->exec($sql);
return $this->db->lastInsertId();
}
//插入独立表模型
public function insertTableModule($args)
{
if (!$args['moduleapp']) {
$args['moduleapp'] = $this->G->app;
}
$args['moduletable'] = $args['moduleapp'].'_'.$args['modulecode'];
$this->_insertDefaultTable($args['moduletable'], $args['modulecode'].'id');
$data = ['module', $args];
$sql = $this->pdosql->makeInsert($data);
$this->db->exec($sql);
return $this->db->lastInsertId();
}
//插入默认的表,$primary为主键字段名,必须为INT 11
private function _insertDefaultTable($table, $primary)
{
$fields = [['name' => $primary, 'type' => 'INT', 'length' => 11, 'auto' => true]];
$indexs = [['type' => 'PRIMARY KEY', 'field' => $primary]];
$sql = $this->pdosql->createTable($table, $fields, $indexs);
return $this->db->query($sql);
}
//根据用户获取模型所有字段
public function getMoudleFields($moduleid, $userinfo = 1, $uselock = false, $app = null)
{
if (!$app) {
$app = $this->G->app;
}
$module = $this->getModuleById($moduleid);
$data = [false, 'module_fields', [['AND', 'fieldmoduleid = :moduleid', 'moduleid', $moduleid], ['OR', ' (fieldpublic = 1 AND fieldappid = :app)', 'app', $app]], false, 'fieldsequence DESC,fieldid DESC'];
$sql = $this->pdosql->makeSelect($data);
$r = $this->db->fetchAll($sql);
if ($uselock) {
return $r;
}
foreach ($r as $key => $p) {
if ($module['modulelockfields'][$p['field']]) {
unset($r[$key]);
} else {
if (1 == $userinfo) {
if (false !== strpos($p['fieldforbidactors'], ',1,')) {
unset($r[$key]);
}
} else {
if (false !== strpos($p['fieldforbidactors'], ',-1,')) {
unset($r[$key]);
}
}
}
}
return $r;
}
public function getTableMoudleFields($moduleid, $userinfo = 1, $uselock = false, $app = null)
{
if (!$app) {
$app = $this->G->app;
}
$module = $this->getModuleById($moduleid);
$data = [false, 'module_fields', [['AND', 'fieldmoduleid = :moduleid', 'moduleid', $moduleid], ['AND', 'fieldappid = :app', 'app', $app]], false, 'fieldsequence DESC,fieldid DESC'];
$sql = $this->pdosql->makeSelect($data);
$r = $this->db->fetchAll($sql);
if ($uselock) {
return $r;
}
foreach ($r as $key => $p) {
if ($module['modulelockfields'][$p['field']]) {
unset($r[$key]);
} else {
if (1 == $userinfo) {
if (false !== strpos($p['fieldforbidactors'], ',1,')) {
unset($r[$key]);
}
} else {
if (false !== strpos($p['fieldforbidactors'], ',-1,')) {
unset($r[$key]);
}
}
}
}
return $r;
}
//获取模型直属字段
public function getPrivateMoudleFields($moduleid)
{
$data = [false, 'module_fields', [['AND', 'fieldmoduleid = :moduleid', 'moduleid', $moduleid], ['AND', 'fieldpublic = 0']], false, 'fieldsequence DESC,fieldid DESC'];
$sql = $this->pdosql->makeSelect($data);
return $this->db->fetchAll($sql);
}
//删除模型
public function delModule($moduleid)
{
$data = ['module', [['AND', 'moduleid = :moduleid', 'moduleid', $moduleid]]];
$sql = $this->pdosql->makeDelete($data);
return $this->db->exec($sql);
}
//删除模型字段
public function delField($fieldid)
{
$field = $this->getFieldById($fieldid);
$sql = $this->pdosql->delField($field['field'], $this->G->app);
$this->db->exec($sql);
$data = ['module_fields', [['AND', 'fieldid = :fieldid', 'fieldid', $fieldid]]];
$sql = $this->pdosql->makeDelete($data);
return $this->db->exec($sql);
}
//整理模型所需的参数,除去多余参数
public function tidyNeedFieldsPars($args, $moduleid, $userinfo = -1)
{
$tmp = [];
foreach ($this->G->make('config', $this->G->app)->fields as $p) {
if (isset($args[$p])) {
$tmp[$p] = $args[$p];
}
}
$r = $this->getMoudleFields($moduleid, $userinfo);
foreach ($r as $key => $data) {
if ('htmltime' == $data['fieldhtmltype']) {
$tmp[$data['field']] = strtotime($args[$data['field']]);
} else {
$tmp[$data['field']] = $args[$data['field']];
}
}
return $tmp;
}
//根据字段名称和模型ID查找字段
public function getFieldByNameAndModuleid($field, $moduleid = false)
{
if ($moduleid) {
$data = [false, 'module_fields', [['AND', 'fieldmoduleid = :moduleid', 'moduleid', $moduleid], ['AND', 'field = :field', 'field', $field]]];
} else {
$data = [false, 'module_fields', [['AND', 'field = :field', 'field', $field]]];
}
$sql = $this->pdosql->makeSelect($data);
return $this->db->fetch($sql);
}
//根据ID获取字段
public function getFieldById($fieldid)
{
if (!$this->cache['field'][$fieldid]) {
$data = [false, 'module_fields', [['AND', 'fieldid = :fieldid', 'fieldid', $fieldid]]];
$sql = $this->pdosql->makeSelect($data);
$this->cache['field'][$fieldid] = $this->db->fetch($sql);
}
return $this->cache['field'][$fieldid];
}
//插入模型字段
public function insertModuleField($args)
{
$args['fieldappid'] = $this->G->app;
$this->insertModuleFieldData($args);
$data = ['module_fields', $args];
$sql = $this->pdosql->makeInsert($data);
$this->db->exec($sql);
return $this->db->lastInsertId();
}
//编辑字段的HTML属性
public function modifyFieldHtmlType($fieldid, $args)
{
$data = ['module_fields', $args, [['AND', 'fieldid = :fieldid', 'fieldid', $fieldid]]];
$sql = $this->pdosql->makeUpdate($data);
return $this->db->exec($sql);
}
//编辑字段的数据库属性
public function modifyFieldDataType($fieldid, $args)
{
$this->modifyModuleField($args, $fieldid);
$data = ['module_fields', $args, [['AND', 'fieldid = :fieldid', 'fieldid', $fieldid]]];
$sql = $this->pdosql->makeUpdate($data);
return $this->db->exec($sql);
}
//插入模型字段
public function insertModuleFieldData($args)
{
$args['fieldcharset'] = 'utf8';
$r = $this->getModuleById($args['fieldmoduleid']);
if ($r['moduletable']) {
$table = $r['moduletable'];
} else {
$table = $this->G->app;
}
$sql = $this->sql->addField($args, $table);
return $this->db->query($sql);
}
//修改模型字段
public function modifyModuleField($fieldid, $args)
{
$args['fieldcharset'] = 'utf8';
$field = $this->getFieldById($fieldid);
$args['field'] = $field['field'];
$r = $this->getModuleById($field['fieldmoduleid']);
if ($r['moduletable']) {
$table = $r['moduletable'];
} else {
$table = $this->G->app;
}
$sql = $this->sql->modifyField($args, $table);
return $this->db->query($sql);
}
//修改模型
public function modifyModule($moduleid, $args)
{
$data = ['module', $args, [['AND', 'moduleid = :moduleid', 'moduleid', $moduleid]]];
$sql = $this->pdosql->makeUpdate($data);
return $this->db->exec($sql);
}
}