-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTable.php
executable file
·349 lines (306 loc) · 10.7 KB
/
Table.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
declare(strict_types=1);
/*
* This file is part of the Alight package.
*
* (c) June So <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alight\Admin;
use Alight\Admin;
use Alight\Request;
use Alight\Response;
use ArrayObject;
class Table
{
public static array $config = [];
private static int $buttonIndex = 0;
public const
EVENT_RENDER = 'render',
EVENT_REQUEST = 'request',
EVENT_RESPONSE = 'response';
public const
ACTION_FORM = 'form',
ACTION_PAGE = 'page',
ACTION_CONFIRM = 'confirm',
ACTION_SUBMIT = 'submit',
ACTION_POPUP = 'popup',
ACTION_REDIRECT = 'redirect',
ALIGN_LEFT = 'left',
ALIGN_CENTER = 'center',
ALIGN_RIGHT = 'right',
COLOR_DEFAULT = 'default',
COLOR_PRIMARY = 'primary',
COLOR_DANGER = 'danger',
COLOR_BLUE = 'blue',
COLOR_PURPLE = 'purple',
COLOR_CYAN = 'cyan',
COLOR_GREEN = 'green',
COLOR_MAGENTA = 'magenta',
COLOR_PINK = 'pink',
COLOR_RED = 'red',
COLOR_ORANGE = 'orange',
COLOR_YELLOW = 'yellow',
COLOR_VOLCANO = 'volcano',
COLOR_GEEKBLUE = 'geekblue',
COLOR_LIME = 'lime',
COLOR_GOLD = 'gold',
FIXED_LEFT = 'left',
FIXED_RIGHT = 'right',
SORT_DEFAULT = '',
SORT_ASCEND = 'ascend',
SORT_DESCEND = 'descend',
TYPE_PASSWORD = 'password',
TYPE_MONEY = 'money',
TYPE_TEXTAREA = 'textarea',
TYPE_DATE = 'date',
TYPE_DATE_TIME = 'dateTime',
TYPE_DATE_WEEK = 'dateWeek',
TYPE_DATE_MONTH = 'dateMonth',
TYPE_DATE_QUARTER = 'dateQuarter',
TYPE_DATE_YEAR = 'dateYear',
TYPE_DATE_RANGE = 'dateRange',
TYPE_DATE_TIME_RANGE = 'dateTimeRange',
TYPE_TIME = 'time',
TYPE_TIME_RANGE = 'timeRange',
TYPE_TEXT = 'text',
TYPE_SELECT = 'select',
TYPE_TREE_SELECT = 'treeSelect',
TYPE_CHECKBOX = 'checkbox',
TYPE_RATE = 'rate',
TYPE_RADIO = 'radio',
TYPE_RADIO_BUTTON = 'radioButton',
TYPE_PROGRESS = 'progress',
TYPE_PERCENT = 'percent',
TYPE_DIGIT = 'digit',
TYPE_DIGIT_RANGE = 'digitRange',
TYPE_SECOND = 'second',
TYPE_AVATAR = 'avatar',
TYPE_CODE = 'code',
TYPE_SWITCH = 'switch',
TYPE_FROM_NOW = 'fromNow',
TYPE_IMAGE = 'image',
TYPE_JSON_CODE = 'jsonCode',
TYPE_COLOR = 'color',
TYPE_CASCADER = 'cascader',
//extension
TYPE_QRCODE = 'qrcode',
VARIANT_OUTLINED = 'outlined',
VARIANT_DASHED = 'dashed',
VARIANT_SOLID = 'solid',
VARIANT_FILLED = 'filled',
VARIANT_TEXT = 'text',
VARIANT_LINK = 'link';
/**
* Create a button
*
* @param string $form
* @return TableButton
*/
public static function button(string $form): TableButton
{
++self::$buttonIndex;
return new TableButton(self::$buttonIndex, $form);
}
/**
* Create a column
*
* @param string $key
* @return TableColumn
*/
public static function column(string $key): TableColumn
{
return new TableColumn(__FUNCTION__, $key);
}
/**
* Create a expand column
*
* @param string $key
* @return TableExpand
*/
public static function expand(string $key): TableExpand
{
return new TableExpand(__FUNCTION__, $key);
}
/**
* Create a Summary
*
* @param string $key
* @return TableSummary
*/
public static function summary(string $key): TableSummary
{
return new TableSummary($key);
}
/**
* Create a statistic
*
* @param string $key
* @return TableStatistic
*/
public static function statistic(string $key): TableStatistic
{
return new TableStatistic($key);
}
/**
* Table page render
*
* @param string $table
* @param null|callable $callback function(string $event, array &$data){}
*/
public static function render(string $table, ?callable $callback = null)
{
$userId = Auth::getUserId();
$userInfo = Model::getUserInfo($userId);
if (!Request::isAjax()) {
$column = new ArrayObject();
if (isset(Table::$config['column'])) {
foreach (Table::$config['column'] as $k => $v) {
if (isset($v['role']) && $v['role'] && !in_array($userInfo['role_id'], $v['role'])) {
continue;
}
$column[$k] = $v;
}
}
$expand = new ArrayObject();
if (isset(Table::$config['expand'])) {
foreach (Table::$config['expand'] as $k => $v) {
if (isset($v['role']) && $v['role'] && !in_array($userInfo['role_id'], $v['role'])) {
continue;
}
if (isset($v['search'])) {
unset($v['search']);
}
$expand[$k] = $v;
}
}
$toolbar = [];
$batch = [];
if (isset(Table::$config['button'])) {
foreach (Table::$config['button'] as $v) {
if (isset($v['role']) && $v['role'] && !in_array($userInfo['role_id'], $v['role'])) {
continue;
}
if ($v['place'] === '_toolbar') {
$toolbar['button'][] = $v;
} elseif ($v['place'] === '_batch') {
$batch['button'][] = $v;
} elseif (substr($v['place'], 0, 7) === '_column') {
$_place = substr($v['place'], 8);
if (!$_place) {
$_place = $v['place'];
if (!isset($column[$_place])) {
$column[$_place] = [
'title' => ':action',
'database' => false,
'locale' => true,
];
}
}
$column[$_place]['button'][] = $v;
} elseif (substr($v['place'], 0, 7) === '_expand') {
$_place = substr($v['place'], 8);
if (!$_place) {
$_place = $v['place'];
if (!isset($expand[$_place])) {
$expand[$_place] = [
'title' => ':action',
'locale' => true,
];
}
}
$expand[$_place]['button'][] = $v;
}
}
}
$summary = [];
if (isset(Table::$config['summary'])) {
foreach (Table::$config['summary'] as $k => $v) {
if (isset($column[$k])) {
$summary[$k] = $v;
}
}
}
$renderData = [
'column' => $column,
'expand' => $expand,
'toolbar' => $toolbar,
'batch' => $batch,
'summary' => $summary,
'statistic' => Table::$config['statistic'] ?? [],
];
if (is_callable($callback)) {
$callback(self::EVENT_RENDER, $renderData);
}
Response::render(Admin::path() . '/src/Admin/View.phtml', ['title' => Request::request('_title', ''), 'script' => Admin::globalScript('Table', $renderData)]);
} else {
$page = Request::request('current', 1);
$limit = Request::request('pageSize', 20);
$limit = $limit < 100 ? $limit : 100;
$order = Request::request('_order', '') ?: 'id';
$sort = Request::request('_sort', '') ?: 'asc';
$sortLimit = ['ascend' => 'asc', 'descend' => 'desc'];
$sort = $sortLimit[$sort] ?? 'asc';
$column = [];
if (isset(Table::$config['column'])) {
foreach (Table::$config['column'] as $k => $v) {
if (isset($v['role']) && $v['role'] && !in_array($userInfo['role_id'], $v['role'])) {
continue;
}
if (!$v['database']) {
continue;
}
$column[$k] = $v;
}
}
$searchData = self::searchFilter($column, Request::request());
if (is_callable($callback)) {
$callback(self::EVENT_REQUEST, $searchData);
}
$resData = [
'count' => 0,
'list' => [],
];
if ($table) {
$resData['count'] = Model::tableCount($table, $searchData);
$resData['list'] = Model::tableSelect($table, $column, $searchData, $page, $limit, $order, $sort);
}
if (is_callable($callback)) {
$callback(self::EVENT_RESPONSE, $resData);
}
Response::api(0, null, $resData);
}
}
/**
* Search data filter
*
* @param array $column
* @param array $data
* @return array
*/
private static function searchFilter(array $column, array $data): array
{
$return = [];
if ($column) {
foreach ($column as $k => $v) {
if ($v['database'] && isset($v['searchType']) && isset($data[$k])) {
$_v = $data[$k];
if ($_v || is_numeric($_v)) {
if ($v['searchType'] === self::TYPE_TEXT) {
$return[$k . '[~]'] = $_v;
} elseif (in_array($v['searchType'], [self::TYPE_DATE_RANGE, self::TYPE_DATE_TIME_RANGE, self::TYPE_TIME_RANGE])) {
$return[$k . '[<>]'] = explode(',', (string) $_v);
} elseif ($v['searchType'] === self::TYPE_CHECKBOX || ($v['searchProps']['mode'] ?? '') === 'multiple') {
$return[$k] = explode(',', (string) $_v);
} else {
$return[$k] = $_v;
}
}
}
}
}
return $return;
}
}