Form.php
39.7 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
<?php
namespace fast;
use ArrayAccess;
/**
* 表单元素生成
* @class Form
* @package fast
* @method static string token() 生成Token
* @method static string label(string $name, string $value = null, array $options = []) label标签
* @method static string input($type, $name, string $value = null, array $options = []) 按类型生成文本框
* @method static string text(string $name, string $value = null, array $options = []) 普通文本框
* @method static string password(string $name, array $options = []) 密码文本框
* @method static string hidden(string $name, string $value = null, array $options = []) 隐藏文本框
* @method static string email(string $name, string $value = null, array $options = []) Email文本框
* @method static string url(string $name, string $value = null, array $options = []) URL文本框
* @method static string file(string $name, array $options = []) 文件上传组件
* @method static string textarea(string $name, string $value = null, array $options = []) 多行文本框
* @method static string editor(string $name, string $value = null, array $options = []) 富文本编辑器
* @method static string select(string $name, array $list = [], string $selected = null, array $options = []) 下拉列表组件
* @method static string selects(string $name, array $list = [], string $selected = null, array $options = []) 下拉列表组件(多选)
* @method static string selectpicker(string $name, array $list = [], string $selected = null, array $options = []) 下拉列表组件(友好)
* @method static string selectpickers(string $name, array $list = [], string $selected = null, array $options = []) 下拉列表组件(友好)(多选)
* @method static string selectpage(string $name, string $value, string $url, string $field = null, string $primaryKey = null, array $options = []) 动态下拉列表组件
* @method static string selectpages(string $name, string $value, string $url, string $field = null, string $primaryKey = null, array $options = []) 动态下拉列表组件(多选)
* @method static string citypicker(string $name, string $value, array $options = []) 城市选择组件
* @method static string switcher(string $name, string $value, array $options = []) 切换组件
* @method static string datepicker(string $name, string $value, array $options = []) 日期选择组件
* @method static string timepicker(string $name, string $value, array $options = []) 时间选择组件
* @method static string datetimepicker(string $name, string $value, array $options = []) 日期时间选择组件
* @method static string daterange(string $name, string $value, array $options = []) 日期区间组件
* @method static string timerange(string $name, string $value, array $options = []) 时间区间组件
* @method static string datetimerange(string $name, string $value, array $options = []) 日期时间区间组件
* @method static string fieldlist(string $name, string $value, string $title = null, string $template = null, array $options = []) 字段列表组件
* @method static string cxselect(string $url, array $names = [], array $values = [], array $options = []) 联动组件
* @method static string selectRange(string $name, string $begin, string $end, string $selected = null, array $options = []) 选择数字区间
* @method static string selectYear(string $name, string $begin, string $end, string $selected = null, array $options = []) 选择年
* @method static string selectMonth(string $name, string $selected = null, array $options = [], string $format = '%m') 选择月
* @method static string checkbox(string $name, string $value = '1', string $checked = null, array $options = []) 单个复选框
* @method static string checkboxs(string $name, array $list = [], string $checked = null, array $options = []) 一组复选框
* @method static string radio(string $name, string $value = null, string $checked = null, array $options = [])) 单个单选框
* @method static string radios(string $name, array $list = [], string $checked = null, array $options = [])) 一组单选框
* @method static string image(string $name = null, string $value, array $inputAttr = [], array $uploadAttr = [], array $chooseAttr = [], array $previewAttr = []) 上传图片组件
* @method static string images(string $name = null, string $value, array $inputAttr = [], array $uploadAttr = [], array $chooseAttr = [], array $previewAttr = []) 上传图片组件(多图))
* @method static string upload(string $name = null, string $value, array $inputAttr = [], array $uploadAttr = [], array $chooseAttr = [], array $previewAttr = []) 上传文件组件
* @method static string uploads(string $name = null, string $value, array $inputAttr = [], array $uploadAttr = [], array $chooseAttr = [], array $previewAttr = []) 上传文件组件(多文件))
* @method static string button(string $value = null, array $options = []) 表单button
*/
class Form
{
/**
* @param $name
* @param $arguments
* @return FormBuilder
*/
public static function __callStatic($name, $arguments)
{
return call_user_func_array([FormBuilder::instance(), $name], $arguments);
}
}
/**
*
* 表单元素生成
* @from https://github.com/illuminate/html
* @package fast
*/
class FormBuilder
{
/**
* Token
*
* @var string
*/
protected $csrfToken = array('name' => '__token__');
/**
* 已创建的标签名称
*
* @var array
*/
protected $labels = [];
/**
* 跳过的填充value值的类型
*
* @var array
*/
protected $skipValueTypes = array('file', 'password', 'checkbox', 'radio');
/**
* 转义HTML
* @var boolean
*/
protected $escapeHtml = true;
protected static $instance;
/**
* 获取单例
* @param array $options
* @return static
*/
public static function instance($options = [])
{
if (is_null(self::$instance)) {
self::$instance = new static($options);
}
return self::$instance;
}
/**
* 设置是否转义
* @param boolean $escape
*/
public function setEscapeHtml($escape)
{
$this->escapeHtml = $escape;
}
/**
* 获取转义编码后的值
* @param string $value
* @return string
*/
public function escape($value)
{
if (!$this->escapeHtml) {
return $value;
}
if (is_array($value)) {
$value = json_encode($value, JSON_UNESCAPED_UNICODE);
}
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false);
}
/**
* 生成Token
*
* @param string $name
* @param string $type
* @return string
*/
public function token($name = '__token__', $type = 'md5')
{
if (function_exists('token')) {
return token($name, $type);
}
return '';
}
/**
* 生成Label标签
*
* @param string $name
* @param string $value
* @param array $options
* @return string
*/
public function label($name, $value = null, $options = [])
{
$this->labels[] = $name;
$options = $this->attributes($options);
$value = $this->escape($this->formatLabel($name, $value));
return '<label for="' . $name . '"' . $options . '>' . $value . '</label>';
}
/**
* Format the label value.
*
* @param string $name
* @param string|null $value
* @return string
*/
protected function formatLabel($name, $value)
{
return $value ?: ucwords(str_replace('_', ' ', $name));
}
/**
* 生成文本框(按类型)
*
* @param string $type
* @param string $name
* @param string $value
* @param array $options
* @return string
*/
public function input($type, $name, $value = null, $options = [])
{
if (!isset($options['name'])) {
$options['name'] = $name;
}
$id = $this->getIdAttribute($name, $options);
if (!in_array($type, $this->skipValueTypes)) {
$value = $this->getValueAttribute($name, $value);
$options['class'] = isset($options['class']) ? $options['class'] . (stripos($options['class'], 'form-control') !== false ? '' : ' form-control') : 'form-control';
}
$merge = compact('type', 'value', 'id');
$options = array_merge($options, $merge);
return '<input' . $this->attributes($options) . '>';
}
/**
* 生成普通文本框
*
* @param string $name
* @param string $value
* @param array $options
* @return string
*/
public function text($name, $value = null, $options = [])
{
return $this->input('text', $name, $value, $options);
}
/**
* 生成密码文本框
*
* @param string $name
* @param array $options
* @return string
*/
public function password($name, $options = [])
{
return $this->input('password', $name, '', $options);
}
/**
* 生成隐藏文本框
*
* @param string $name
* @param string $value
* @param array $options
* @return string
*/
public function hidden($name, $value = null, $options = [])
{
return $this->input('hidden', $name, $value, $options);
}
/**
* 生成Email文本框
*
* @param string $name
* @param string $value
* @param array $options
* @return string
*/
public function email($name, $value = null, $options = [])
{
return $this->input('email', $name, $value, $options);
}
/**
* 生成URL文本框
*
* @param string $name
* @param string $value
* @param array $options
* @return string
*/
public function url($name, $value = null, $options = [])
{
return $this->input('url', $name, $value, $options);
}
/**
* 生成上传文件组件
*
* @param string $name
* @param array $options
* @return string
*/
public function file($name, $options = [])
{
return $this->input('file', $name, null, $options);
}
/**
* 生成多行文本框
*
* @param string $name
* @param string $value
* @param array $options
* @return string
*/
public function textarea($name, $value = null, $options = [])
{
if (!isset($options['name'])) {
$options['name'] = $name;
}
$options = $this->setTextAreaSize($options);
$options['id'] = $this->getIdAttribute($name, $options);
$value = (string)$this->getValueAttribute($name, $value);
unset($options['size']);
$options['class'] = isset($options['class']) ? $options['class'] . (stripos($options['class'], 'form-control') !== false ? '' : ' form-control') : 'form-control';
$options = $this->attributes($options);
return '<textarea' . $options . '>' . $this->escape($value) . '</textarea>';
}
/**
* 生成富文本编辑器
*
* @param string $name
* @param string $value
* @param array $options
* @return string
*/
public function editor($name, $value = null, $options = [])
{
$options['class'] = isset($options['class']) ? $options['class'] . ' editor' : 'editor';
return $this->textarea($name, $value, $options);
}
/**
* 设置默认的文本框行列数
*
* @param array $options
* @return array
*/
protected function setTextAreaSize($options)
{
if (isset($options['size'])) {
return $this->setQuickTextAreaSize($options);
}
$cols = array_get($options, 'cols', 50);
$rows = array_get($options, 'rows', 5);
return array_merge($options, compact('cols', 'rows'));
}
/**
* 根据size设置行数和列数
*
* @param array $options
* @return array
*/
protected function setQuickTextAreaSize($options)
{
$segments = explode('x', $options['size']);
return array_merge($options, array('cols' => $segments[0], 'rows' => $segments[1]));
}
/**
* 生成滑块
*
* @param string $name
* @param string $min
* @param string $max
* @param string $step
* @param string $value
* @param array $options
* @return string
*/
public function slider($name, $min, $max, $step, $value = null, $options = [])
{
$options = array_merge($options, ['data-slider-min' => $min, 'data-slider-max' => $max, 'data-slider-step' => $step, 'data-slider-value' => $value ? $value : '']);
$options['class'] = isset($options['class']) ? $options['class'] . (stripos($options['class'], 'form-control') !== false ? '' : ' slider form-control') : 'slider form-control';
return $this->input('text', $name, $value, $options);
}
/**
* 生成下拉列表框
*
* @param string $name
* @param array $list
* @param mixed $selected
* @param array $options
* @return string
*/
public function select($name, $list = [], $selected = null, $options = [])
{
$selected = $this->getValueAttribute($name, $selected);
$options['id'] = $this->getIdAttribute($name, $options);
if (!isset($options['name'])) {
$options['name'] = $name;
}
$html = [];
foreach ($list as $value => $display) {
$html[] = $this->getSelectOption($display, $value, $selected);
}
$options['class'] = isset($options['class']) ? $options['class'] . (stripos($options['class'], 'form-control') !== false ? '' : ' form-control') : 'form-control';
$options = $this->attributes($options);
$list = implode('', $html);
return "<select{$options}>{$list}</select>";
}
/**
* 下拉列表(多选)
*
* @param string $name
* @param array $list
* @param mixed $selected
* @param array $options
* @return string
*/
public function selects($name, $list = [], $selected = null, $options = [])
{
$options[] = 'multiple';
return $this->select($name, $list, $selected, $options);
}
/**
* 下拉列表(友好)
*
* @param string $name
* @param array $list
* @param mixed $selected
* @param array $options
* @return string
*/
public function selectpicker($name, $list = [], $selected = null, $options = [])
{
$options['class'] = isset($options['class']) ? $options['class'] . ' selectpicker' : 'selectpicker';
return $this->select($name, $list, $selected, $options);
}
/**
* 下拉列表(友好)(多选)
*
* @param string $name
* @param array $list
* @param mixed $selected
* @param array $options
* @return string
*/
public function selectpickers($name, $list = [], $selected = null, $options = [])
{
$options[] = 'multiple';
return $this->selectpicker($name, $list, $selected, $options);
}
/**
* 生成动态下拉列表
*
* @param string $name 名称
* @param mixed $value
* @param string $url 数据源地址
* @param string $field 显示的字段名称,默认为name
* @param string $primaryKey 主键,数据库中保存的值,默认为id
* @param array $options
* @return string
*/
public function selectpage($name, $value, $url, $field = null, $primaryKey = null, $options = [])
{
$options = array_merge($options, ['data-source' => $url, 'data-field' => $field ? $field : 'name', 'data-primary-key' => $primaryKey ? $primaryKey : 'id']);
$options['class'] = isset($options['class']) ? $options['class'] . ' selectpage' : 'selectpage';
return $this->text($name, $value, $options);
}
/**
* 生成动态下拉列表(复选)
*
* @param string $name 名称
* @param mixed $value
* @param string $url 数据源地址
* @param string $field 显示的字段名称,默认为name
* @param string $primaryKey 主键,数据库中保存的值,默认为id
* @param array $options
* @return string
*/
public function selectpages($name, $value, $url, $field = null, $primaryKey = null, $options = [])
{
$options['data-multiple'] = "true";
return $this->selectpage($name, $value, $url, $field, $primaryKey, $options);
}
/**
* 生成城市选择框
*
* @param string $name
* @param mixed $value
* @param array $options
* @return string
*/
public function citypicker($name, $value, $options = [])
{
$options['data-toggle'] = 'city-picker';
return "<div class='control-relative'>" . $this->text($name, $value, $options) . "</div>";
}
/**
* 生成switch组件
*
* @param string $name
* @param mixed $value
* @param array $options
* @return string
*/
public function switcher($name, $value, $options = [])
{
$domname = str_replace(['[', ']', '.'], '', $name);
$btn = $this->hidden($name, $value, ['id' => "c-{$domname}"]);
$yes = 1;
$no = 0;
if (isset($options['yes']) && isset($options['no'])) {
$yes = $options['yes'];
$no = $options['no'];
}
$selected = $no == $value ? "fa-flip-horizontal text-gray" : "";
$disabled = (isset($options['disabled']) && $options['disabled']) || in_array('disabled', $options) ? "disabled" : '';
$color = isset($options['color']) ? $options['color'] : 'success';
unset($options['yes'], $options['no'], $options['color'], $options['disabled']);
$attr = $this->attributes($options);
$html = <<<EOD
{$btn}
<a href="javascript:;" data-toggle="switcher" class="btn-switcher {$disabled}" data-input-id="c-{$domname}" data-yes="{$yes}" data-no="{$no}" {$attr}><i class="fa fa-toggle-on text-{$color} {$selected} fa-2x"></i></a>
EOD;
return $html;
}
/**
* 日期选择器
* @param string $name
* @param mixed $value
* @param array $options
* @return string
*/
public function datepicker($name, $value, $options = [])
{
$defaults = [
'data-date-format' => "YYYY-MM-DD",
];
$options = array_merge($defaults, $options);
$value = is_numeric($value) ? date("Y-m-d", $value) : $value;
return $this->datetimepicker($name, $value, $options);
}
/**
* 时间选择器
*
* @param string $name
* @param mixed $value
* @param array $options
* @return string
*/
public function timepicker($name, $value, $options = [])
{
$defaults = [
'data-date-format' => "HH:mm:ss",
];
$options = array_merge($defaults, $options);
$value = is_numeric($value) ? date("H:i:s", $value) : $value;
return $this->datetimepicker($name, $value, $options);
}
/**
* 日期时间选择器
*
* @param string $name
* @param mixed $value
* @param array $options
* @return string
*/
public function datetimepicker($name, $value, $options = [])
{
$defaults = [
'data-date-format' => "YYYY-MM-DD HH:mm:ss",
'data-use-current' => "true",
];
$value = is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
$options = array_merge($defaults, $options);
$options['class'] = isset($options['class']) ? $options['class'] . ' datetimepicker' : 'datetimepicker';
return $this->text($name, $value, $options);
}
/**
* 日期区间
*
* @param string $name
* @param string $value
* @param array $options
* @return string
*/
public function daterange($name, $value, $options = [])
{
$defaults = [
'data-locale' => [
'format' => 'YYYY-MM-DD'
]
];
$options = array_merge($defaults, $options);
return $this->datetimerange($name, $value, $options);
}
/**
* 时间区间
*
* @param string $name
* @param string $value
* @param array $options
* @return string
*/
public function timerange($name, $value, $options = [])
{
$defaults = [
'data-locale' => [
'format' => 'HH:mm:ss'
],
'data-ranges' => [],
'data-show-custom-range-label' => "false",
'data-time-picker' => "true",
];
$options = array_merge($defaults, $options);
return $this->datetimerange($name, $value, $options);
}
/**
* 日期时间区间
*
* @param string $name
* @param string $value
* @param array $options
* @return string
*/
public function datetimerange($name, $value, $options = [])
{
$defaults = [
'data-locale' => [
'format' => 'YYYY-MM-DD HH:mm:ss'
]
];
$options = array_merge($defaults, $options);
$options['class'] = isset($options['class']) ? $options['class'] . ' datetimerange' : 'datetimerange';
return $this->text($name, $value, $options);
}
/**
* 生成字段列表组件
*
* @param string $name
* @param mixed $value
* @param array $title
* @param string $template
* @param array $options
* @return string
*/
public function fieldlist($name, $value, $title = null, $template = null, $options = [])
{
$append = __('Append');
$template = $template ? 'data-template="' . $template . '"' : '';
$attributes = $this->attributes($options);
if (is_null($title)) {
$title = [__('Key'), __('Value')];
}
$ins = implode("\n", array_map(function ($value) {
return "<ins>{$value}</ins>";
}, $title));
$value = is_array($value) ? json_encode($value) : $value;
$html = <<<EOD
<dl class="fieldlist" data-name="{$name}" {$template} {$attributes}>
<dd>
{$ins}
</dd>
<dd><a href="javascript:;" class="btn btn-sm btn-success btn-append"><i class="fa fa-plus"></i> {$append}</a></dd>
<textarea name="{$name}" class="form-control hide" cols="30" rows="5">{$value}</textarea>
</dl>
EOD;
return $html;
}
/**
* 生成联动下拉列表
*
* @param string $url 联动获取数据源的URL地址
* @param array $names 联动字段名称
* @param array $values 联动字段默认选中的值
* @param array $options 扩展属性
* @return string
*/
public function cxselect($url, $names = [], $values = [], $options = [])
{
$classes = [];
$cxselect = [];
$attributes = $this->attributes($options);
foreach ($names as $index => $value) {
$level = $index + 1;
$class = "cxselect-{$level}";
$classes[] = $class;
$selectValue = isset($values[$value]) ? $values[$value] : (isset($values[$index]) ? $values[$index] : '');
$cxselect[] = <<<EOD
<select class="{$class} form-control" name="{$value}" data-value="{$selectValue}" data-url="{$url}?level={$level}&name={$value}" {$attributes}></select>
EOD;
}
$cxselect = implode("\n", $cxselect);
$selects = implode(',', $classes);
$html = <<<EOD
<div class="form-inline" data-toggle="cxselect" data-selects="{$selects}">
{$cxselect}
</div>
EOD;
return $html;
}
/**
* 创建一个下拉列表选择区间组件
*
* @param string $name
* @param string $begin
* @param string $end
* @param string $selected
* @param array $options
* @return string
*/
public function selectRange($name, $begin, $end, $selected = null, $options = [])
{
$range = array_combine($range = range($begin, $end), $range);
return $this->select($name, $range, $selected, $options);
}
/**
* 生成选择年组件
*
* @param string $name
* @param string $begin
* @param string $end
* @param string $selected
* @param array $options
* @return string
*/
public function selectYear($name, $begin, $end, $selected, $options)
{
return call_user_func_array(array($this, 'selectRange'), func_get_args());
}
/**
* 生成选择月组件
*
* @param string $name
* @param string $selected
* @param array $options
* @param string $format
* @return string
*/
public function selectMonth($name, $selected = null, $options = [], $format = '%m')
{
$months = [];
foreach (range(1, 12) as $month) {
$months[$month] = strftime($format, mktime(0, 0, 0, $month, 1));
}
return $this->select($name, $months, $selected, $options);
}
/**
* 根据传递的值生成option
*
* @param string $display
* @param string $value
* @param string $selected
* @return string
*/
public function getSelectOption($display, $value, $selected)
{
if (is_array($display)) {
return $this->optionGroup($display, $value, $selected);
}
return $this->option($display, $value, $selected);
}
/**
* 生成optionGroup
*
* @param array $list
* @param string $label
* @param string $selected
* @return string
*/
protected function optionGroup($list, $label, $selected)
{
$html = [];
foreach ($list as $value => $display) {
$html[] = $this->option($display, $value, $selected);
}
return '<optgroup label="' . $this->escape($label) . '">' . implode('', $html) . '</optgroup>';
}
/**
* 生成option选项
*
* @param string $display
* @param string $value
* @param string $selected
* @return string
*/
protected function option($display, $value, $selected)
{
$selected = $this->getSelectedValue($value, $selected);
$options = array('value' => $this->escape($value), 'selected' => $selected);
return '<option' . $this->attributes($options) . '>' . $this->escape($display) . '</option>';
}
/**
* 检测value是否选中
*
* @param string $value
* @param string $selected
* @return string
*/
protected function getSelectedValue($value, $selected)
{
if (is_array($selected)) {
return in_array($value, $selected) ? 'selected' : null;
}
return ((string)$value == (string)$selected) ? 'selected' : null;
}
/**
* 生成复选按钮
*
* @param string $name
* @param mixed $value
* @param bool $checked
* @param array $options
* @return string
*/
public function checkbox($name, $value = 1, $checked = null, $options = [])
{
if ($checked) {
$options['checked'] = 'checked';
}
return $this->input('checkbox', $name, $value, $options);
}
/**
* 生成一组筛选框
*
* @param string $name
* @param array $list
* @param mixed $checked
* @param array $options
* @return string
*/
public function checkboxs($name, $list, $checked, $options = [])
{
$html = [];
$checked = is_null($checked) ? [] : $checked;
$checked = is_array($checked) ? $checked : explode(',', $checked);
foreach ($list as $k => $v) {
$options['id'] = "{$name}-{$k}";
$html[] = sprintf(Form::label("{$name}-{$k}", "%s {$v}"), Form::checkbox("{$name}[{$k}]", $k, in_array($k, $checked), $options));
}
return '<div class="checkbox">' . implode(' ', $html) . '</div>';
}
/**
* 生成单选按钮
*
* @param string $name
* @param mixed $value
* @param bool $checked
* @param array $options
* @return string
*/
public function radio($name, $value = null, $checked = null, $options = [])
{
if (is_null($value)) {
$value = $name;
}
if ($checked) {
$options['checked'] = 'checked';
}
return $this->input('radio', $name, $value, $options);
}
/**
* 生成一组单选框
*
* @param string $name
* @param array $list
* @param mixed $checked
* @param array $options
* @return string
*/
public function radios($name, $list, $checked = null, $options = [])
{
$html = [];
$checked = is_null($checked) ? key($list) : $checked;
$checked = is_array($checked) ? $checked : explode(',', $checked);
foreach ($list as $k => $v) {
$options['id'] = "{$name}-{$k}";
$html[] = sprintf(Form::label("{$name}-{$k}", "%s {$v}"), Form::radio($name, $k, in_array($k, $checked), $options));
}
return '<div class="radio">' . implode(' ', $html) . '</div>';
}
/**
* 生成上传图片组件(单图)
*
* @param string $name
* @param string $value
* @param array $inputAttr
* @param array $uploadAttr
* @param array $chooseAttr
* @param array $previewAttr
* @return string
*/
public function image($name = null, $value = null, $inputAttr = [], $uploadAttr = [], $chooseAttr = [], $previewAttr = [])
{
$default = [
'data-mimetype' => 'image/gif,image/jpeg,image/png,image/jpg,image/bmp'
];
$uploadAttr = is_array($uploadAttr) ? array_merge($default, $uploadAttr) : $uploadAttr;
$chooseAttr = is_array($chooseAttr) ? array_merge($default, $chooseAttr) : $chooseAttr;
return $this->uploader($name, $value, $inputAttr, $uploadAttr, $chooseAttr, $previewAttr);
}
/**
* 生成上传图片组件(多图)
*
* @param string $name
* @param string $value
* @param array $inputAttr
* @param array $uploadAttr
* @param array $chooseAttr
* @param array $previewAttr
* @return string
*/
public function images($name = null, $value = null, $inputAttr = [], $uploadAttr = [], $chooseAttr = [], $previewAttr = [])
{
$default = [
'data-multiple' => 'true',
'data-mimetype' => 'image/gif,image/jpeg,image/png,image/jpg,image/bmp'
];
$uploadAttr = is_array($uploadAttr) ? array_merge($default, $uploadAttr) : $uploadAttr;
$chooseAttr = is_array($chooseAttr) ? array_merge($default, $chooseAttr) : $chooseAttr;
return $this->uploader($name, $value, $inputAttr, $uploadAttr, $chooseAttr, $previewAttr);
}
/**
* 生成上传文件组件(单文件)
*
* @param string $name
* @param string $value
* @param array $inputAttr
* @param array $uploadAttr
* @param array $chooseAttr
* @param array $previewAttr
* @return string
*/
public function upload($name = null, $value = null, $inputAttr = [], $uploadAttr = [], $chooseAttr = [], $previewAttr = [])
{
return $this->uploader($name, $value, $inputAttr, $uploadAttr, $chooseAttr, $previewAttr);
}
/**
* 生成上传文件组件(多文件)
*
* @param string $name
* @param string $value
* @param array $inputAttr
* @param array $uploadAttr
* @param array $chooseAttr
* @param array $previewAttr
* @return string
*/
public function uploads($name = null, $value = null, $inputAttr = [], $uploadAttr = [], $chooseAttr = [], $previewAttr = [])
{
$default = [
'data-multiple' => 'true',
];
$uploadAttr = is_array($uploadAttr) ? array_merge($default, $uploadAttr) : $uploadAttr;
$chooseAttr = is_array($chooseAttr) ? array_merge($default, $chooseAttr) : $chooseAttr;
return $this->uploader($name, $value, $inputAttr, $uploadAttr, $chooseAttr, $previewAttr);
}
protected function uploader($name = null, $value = null, $inputAttr = [], $uploadAttr = [], $chooseAttr = [], $previewAttr = [])
{
$domname = str_replace(['[', ']', '.'], '', $name);
$options = [
'id' => "plupload-{$domname}",
'class' => "btn btn-danger plupload",
'data-input-id' => "c-{$domname}",
];
$upload = $uploadAttr === false ? false : true;
$choose = $chooseAttr === false ? false : true;
$preview = $previewAttr === false ? false : true;
if ($preview) {
$options['data-preview-id'] = "p-{$domname}";
}
$uploadBtn = $upload ? $this->button('<i class="fa fa-upload"></i> ' . __('Upload'), array_merge($options, $uploadAttr)) : '';
$options = [
'id' => "fachoose-{$domname}",
'class' => "btn btn-danger fachoose",
'data-input-id' => "c-{$domname}",
];
if ($preview) {
$options['data-preview-id'] = "p-{$domname}";
}
$chooseBtn = $choose ? $this->button('<i class="fa fa-list"></i> ' . __('Choose'), array_merge($options, $chooseAttr)) : '';
$previewAttrHtml = $this->attributes($previewAttr);
$previewArea = $preview ? '<ul class="row list-inline plupload-preview" id="p-' . $domname . '" ' . $previewAttrHtml . '></ul>' : '';
$input = $this->text($name, $value, array_merge(['size' => 50, 'id' => "c-{$domname}"], $inputAttr));
$html = <<<EOD
<div class="input-group">
{$input}
<div class="input-group-addon no-border no-padding">
<span>{$uploadBtn}</span>
<span>{$chooseBtn}</span>
</div>
<span class="msg-box n-right" for="c-{$domname}"></span>
</div>
{$previewArea}
EOD;
return $html;
}
/**
* 生成一个按钮
*
* @param string $value
* @param array $options
* @return string
*/
public function button($value = null, $options = [])
{
if (!array_key_exists('type', $options)) {
$options['type'] = 'button';
}
return '<button' . $this->attributes($options) . '>' . $value . '</button>';
}
/**
* 获取ID属性值
*
* @param string $name
* @param array $attributes
* @return string
*/
public function getIdAttribute($name, $attributes)
{
if (array_key_exists('id', $attributes)) {
return $attributes['id'];
}
if (in_array($name, $this->labels)) {
return $name;
}
}
/**
* 获取Value属性值
*
* @param string $name
* @param string $value
* @return string
*/
public function getValueAttribute($name, $value = null)
{
if (is_null($name)) {
return $value;
}
if (!is_null($value)) {
return $value;
}
}
/**
* 数组转换成一个HTML属性字符串。
*
* @param array $attributes
* @return string
*/
public function attributes($attributes)
{
$html = [];
// 假设我们的keys 和 value 是相同的,
// 拿HTML“required”属性来说,假设是['required']数组,
// 会已 required="required" 拼接起来,而不是用数字keys去拼接
foreach ((array)$attributes as $key => $value) {
$element = $this->attributeElement($key, $value);
if (!is_null($element)) {
$html[] = $element;
}
}
return count($html) > 0 ? ' ' . implode(' ', $html) : '';
}
/**
* 拼接成一个属性。
*
* @param string $key
* @param string $value
* @return string
*/
protected function attributeElement($key, $value)
{
if (is_numeric($key)) {
$key = $value;
}
if (!is_null($value)) {
if (is_array($value) || stripos($value, '"') !== false) {
$value = is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value;
return $key . "='" . $value . "'";
} else {
return $key . '="' . $value . '"';
}
}
}
}
class Arr
{
/**
* Determine whether the given value is array accessible.
*
* @param mixed $value
* @return bool
*/
public static function accessible($value)
{
return is_array($value) || $value instanceof ArrayAccess;
}
/**
* Determine if the given key exists in the provided array.
*
* @param \ArrayAccess|array $array
* @param string|int $key
* @return bool
*/
public static function exists($array, $key)
{
if ($array instanceof ArrayAccess) {
return $array->offsetExists($key);
}
return array_key_exists($key, $array);
}
/**
* Get an item from an array using "dot" notation.
*
* @param \ArrayAccess|array $array
* @param string $key
* @param mixed $default
* @return mixed
*/
public static function get($array, $key, $default = null)
{
if (!static::accessible($array)) {
return $default;
}
if (is_null($key)) {
return $array;
}
if (static::exists($array, $key)) {
return $array[$key];
}
foreach (explode('.', $key) as $segment) {
if (static::accessible($array) && static::exists($array, $segment)) {
$array = $array[$segment];
} else {
return $default;
}
}
return $array;
}
/**
* Get all of the given array except for a specified array of items.
*
* @param array $array
* @param array|string $keys
* @return array
*/
public static function except($array, $keys)
{
static::forget($array, $keys);
return $array;
}
/**
* Remove one or many array items from a given array using "dot" notation.
*
* @param array $array
* @param array|string $keys
* @return void
*/
public static function forget(&$array, $keys)
{
$original = &$array;
$keys = (array)$keys;
if (count($keys) === 0) {
return;
}
foreach ($keys as $key) {
// if the exact key exists in the top-level, remove it
if (static::exists($array, $key)) {
unset($array[$key]);
continue;
}
$parts = explode('.', $key);
// clean up before each pass
$array = &$original;
while (count($parts) > 1) {
$part = array_shift($parts);
if (isset($array[$part]) && is_array($array[$part])) {
$array = &$array[$part];
} else {
continue 2;
}
}
unset($array[array_shift($parts)]);
}
}
}
if (!function_exists('array_get')) {
/**
* Get an item from an array using "dot" notation.
*
* @param \ArrayAccess|array $array
* @param string $key
* @param mixed $default
* @return mixed
*/
function array_get($array, $key, $default = null)
{
return Arr::get($array, $key, $default);
}
}
if (!function_exists('e')) {
/**
* Escape HTML special characters in a string.
*
*
* @return string
*/
function e($value)
{
if (is_array($value)) {
$value = json_encode($value, JSON_UNESCAPED_UNICODE);
}
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false);
}
}
if (!function_exists('array_except')) {
/**
* Get all of the given array except for a specified array of items.
*
* @param array $array
* @param array|string $keys
* @return array
*/
function array_except($array, $keys)
{
return Arr::except($array, $keys);
}
}