-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpl_options-master.php
1138 lines (1063 loc) · 28.1 KB
/
tpl_options-master.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
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
<?php
/*
Plugin Name: 模板设置
Version: 2+2.1
Plugin URL: https://github.com/Baiqiang/em_tpl_options
Description: 为支持的模板设置参数。
ForEmlog: 5.2.0+
Author: 奇遇
Author URL: http://www.qiyuuu.com
*/
!defined('EMLOG_ROOT') && exit('access deined!');
/**
* 模板设置类
*/
class TplOptions {
//插件标识
const ID = 'tpl_options-master';
const NAME = '模板设置';
const VERSION = '2+2.1';
//数据表前缀
private $_prefix = 'tpl_options_';
//数据表
private $_tables = array(
'data',
);
//运行上传的文件类型
private $_imageTypes = array(
'gif',
'jpg',
'jpeg',
'png'
);
//实例
private static $_instance;
//是否初始化
private $_inited = false;
//模板参数
private $_templateOptions;
//从模板读取经过处理的原始设置项
private $_options;
//支持的参数类型
private $_types;
//数据为数组的类型
private $_arrayTypes = array();
//数据库连接实例
private $_db;
//插件模板目录
private $_view;
//插件前端资源路径
private $_assets;
//当前模板
private $_currentTemplate;
//页面
private $_pages;
/**
* 单例入口
* @return TplOptions
*/
public static function getInstance() {
if (self::$_instance === null) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* 私有构造函数,保证单例
*/
private function __construct() {
}
/**
* 初始化函数
* @return void
*/
public function init() {
if ($this->_inited === true) {
return;
}
$this->_inited = true;
//初始化各个数据表名
$tables = array();
foreach ($this->_tables as $name => $table) {
$tables[$table] = $this->getTableName($table);
}
$this->_tables = $tables;
//初始化模板设置类型
$this->_types = array(
'radio' => array(
'name' => '单选按钮',
'allowMulti' => false,
),
'checkbox' => array(
'name' => '复选按钮',
'allowMulti' => true,
),
'text' => array(
'name' => '文本',
'allowMulti' => true,
'allowRich' => true,
),
'image' => array(
'name' => '图片',
'allowMulti' => false,
),
'page' => array(
'name' => '页面',
'allowMulti' => true,
),
'sort' => array(
'name' => '分类',
'allowMulti' => true,
'allowDepend' => true,
),
'tag' => array(
'name' => '标签',
'allowMulti' => true,
),
);
$this->_arrayTypes = array(
'checkbox',
'tag',
);
//设置模板目录
$this->_view = dirname(__FILE__) . '/views/';
$this->_assets = BLOG_URL . 'content/plugins/' . self::ID . '/assets/';
//注册各个钩子
$scriptBaseName = strtolower(substr(basename($_SERVER['SCRIPT_NAME']), 0, -4));
addAction('data_prebakup', array(
$this,
'hookDataPreBackup'
));
if ($scriptBaseName == 'template') {
addAction('adm_main_top', array(
$this,
'hookAdminMainTopData'
));
addAction('adm_head', array(
$this,
'hookAdminHead'
));
}
}
/**
* 输出数据
* @return void
*/
public function hookAdminMainTopData() {
$templates = $this->getTemplates();
$data = array(
'templates' => $templates,
'prefix' => str_replace('_', '-', $this->_prefix),
'baseUrl' => $this->url(),
'uploadUrl' => $this->url(array(
"do" => "upload"
)),
);
echo sprintf('<script>var tplOptions = %s;</script>', json_encode($data));
}
/**
* 备份数据表
* @return void
*/
public function hookDataPreBackup() {
global $tables;
$prefixLen = strlen(DB_PREFIX);
foreach ($this->getTable() as $table) {
$tables[] = substr($table, $prefixLen);
}
}
/**
* 头部,如css文件
* @return void
*/
public function hookAdminHead() {
echo sprintf('<link rel="stylesheet" href="%s">', $this->_assets . 'main.css?ver=' . urlencode(self::VERSION));
echo '<script charset="utf-8" src="./editor/kindeditor.js"></script>';
echo '<script charset="utf-8" src="./editor/lang/zh_CN.js"></script>';
echo sprintf('<script src="%s"></script>', $this->_assets . 'main.js?ver=' . urlencode(self::VERSION));
}
/**
* 获取数据表
* @param mixed $table 表名缩写,可选,若不设置则返回所有表,否则返回对应表
* @return mixed 返回数组或字符串
*/
public function getTable($table = null) {
return $table === null ? $this->_tables : (isset($this->_tables[$table]) ? $this->_tables[$table] : '');
}
/**
* 获取数据表名
* @param string $table 表名缩写
* @return string 表全名
*/
private function getTableName($table) {
return DB_PREFIX . $this->_prefix . $table;
}
/**
* 获取模板参数数据,默认获取当前模板
* @param mixed $template 模板名称,可选
* @return array 模板参数
*/
public function getTemplateOptions($template = null) {
if ($template === null) {
$template = Option::get('nonce_templet');
}
if (isset($this->_templateOptions[$template])) {
return $this->_templateOptions[$template];
}
$_data = $this->queryAll('data', array(
'template' => $template,
));
$templateOptions = array();
$options = $this->getTemplateDefinedOptions($template);
if ($options === false) {
$options = array();
}
foreach ($_data as $row) {
extract($row);
$data = unserialize($data);
$templateOptions[$name] = $data;
}
$unsorted = isset($option['unsorted']) ? $option['unsorted'] : true;
$sorts = $this->getSorts($unsorted);
$pages = $this->getPages();
foreach ($options as $name => $option) {
if (!is_array($option) || !isset($option['name']) || !isset($option['type']) || !isset($this->_types[$option['type']])) {
unset($options[$name]);
continue;
}
if (!isset($templateOptions[$name])) {
$templateOptions[$name] = $this->getOptionDefaultValue($option, $template);
}
$depend = isset($option['depend']) ? $option['depend'] : '';
switch ($depend) {
case 'sort':
if (!is_array($templateOptions[$name])) {
$templateOptions[$name] = array();
}
foreach ($sorts as $sort) {
if (!isset($templateOptions[$name][$sort['sid']])) {
$templateOptions[$name][$sort['sid']] = $this->getOptionDefaultValue($option, $template);
}
}
break;
}
switch ($option['type']) {
case 'sort':
case 'page':
$varName = $option['type'] . 's';
$var = $$varName;
if (!$this->isMulti($option) && !isset($var[$templateOptions[$name]])) {
$templateOptions[$name] = $this->getOptionDefaultValue($option, $template);
}
break;
default:
break;
}
if ($option['type'] == 'image') {
$templateOptions[$name] = $this->buildImageUrl($templateOptions[$name]);
}
}
return $this->_templateOptions[$template] = $templateOptions;
}
/**
* 设置模板参数数据
* @param string $template 模板名称
* @param array $options 模板参数
* @return boolean
*/
public function setTemplateOptions($template, $options) {
if ($options === array()) {
return true;
}
$data = array();
foreach ($options as $name => $option) {
$data[] = array(
'template' => $template,
'name' => $name,
'depend' => $option['depend'],
'data' => serialize($option['data']),
);
}
return $this->insert('data', $data, true);
}
/**
* 获取所有分类
* @param boolean $unsorted 是否获取未分类
* @return array
*/
private function getSorts($unsorted = false) {
$sorts = Cache::getInstance()->readCache('sort');
if ($unsorted) {
array_unshift($sorts, array(
'sid' => - 1,
'sortname' => '未分类',
'lognum' => 0,
'children' => array(),
));
}
return $sorts;
}
/**
* 获取所有页面
* @return array
*/
private function getPages() {
if ($this->_pages !== null) {
return $this->_pages;
}
$data = $this->queryAll('blog', array(
'type' => 'page',
'hide' => 'n',
), 'gid, title');
$pages = array();
foreach ($data as $page) {
$pages[$page['gid']] = $this->encode($page['title']);
}
return $this->_pages = $pages;
}
/**
* 获取数据库连接
* @return MySql 数据库连接实例
*/
public function getDb() {
if ($this->_db !== null) {
return $this->_db;
}
if (class_exists('Database', false)) {
$this->_db = Mysqlii::getInstance();
} else {
$this->_db = MySql::getInstance();
}
return $this->_db;
}
/**
* 从表中查询出所有数据
* @param string $table 表名缩写
* @param mixed $condition 字符串或数组条件
* @return array 结果数据
*/
private function queryAll($table, $condition = '', $select = '*') {
$table = $this->getTable($table) ? $this->getTable($table) : DB_PREFIX . $table;
$subSql = $this->buildQuerySql($condition);
$sql = "SELECT $select FROM `$table`";
if ($subSql) {
$sql .= " WHERE $subSql";
}
$query = $this->getDb()->query($sql);
$data = array();
while ($row = $this->getDb()->fetch_array($query)) {
$data[] = $row;
}
return $data;
}
/**
* 将数据插入数据表
* @param string $table 表名缩写
* @param array $data 数据
* @return array 结果数据
*/
private function insert($table, $data, $replace = false) {
$table = $this->getTable($table);
$subSql = $this->buildInsertSql($data);
if ($replace) {
$sql = "REPLACE INTO `$table`";
} else {
$sql = "INSERT INTO `$table`";
}
$sql .= $subSql;
return $this->getDb()->query($sql) !== false;
}
/**
* 根据条件构造WHERE子句
* @param mixed $condition 字符串或数组条件
* @return string 根据条件构造的查询子句
*/
private function buildQuerySql($condition) {
if (is_string($condition)) {
return $condition;
}
$subSql = array();
foreach ($condition as $key => $value) {
if (is_string($value)) {
$value = $this->getDb()->escape_string($value);
$subSql[] = "(`$key`='$value')";
} elseif (is_array($value)) {
$subSql[] = "(`$key` IN (" . $this->implodeSqlArray($value) . '))';
}
}
return implode(' AND ', $subSql);
}
/**
* 根据数据构造INSERT/REPLACE INTO子句
* @param array $data 数据
* @return string 根据数据构造的子句
*/
private function buildInsertSql($data) {
$subSql = array();
if (array_key_exists(0, $data)) {
$keys = array_keys($data[0]);
} else {
$keys = array_keys($data);
$data = array(
$data
);
}
foreach ($data as $key => $value) {
$subSql[] = '(' . $this->implodeSqlArray($value) . ')';
}
$subSql = implode(',', $subSql);
$keys = '(`' . implode('`,`', $keys) . '`)';
$subSql = "$keys VALUES $subSql";
return $subSql;
}
/**
* 将数组展开为可以供SQL使用的字符串
* @param array $data 数据
* @return string 形如('value1', 'value2')的字符串
*/
private function implodeSqlArray($data) {
return implode(',', array_map(create_function('$val', 'return "\'" . addcslashes($val,"") . "\'";'), $data));
}
/**
* 插件设置函数
* @return void
*/
public function setting() {
$do = $this->arrayGet($_GET, 'do');
$template = $this->arrayGet($_GET, 'template');
$code = $this->arrayGet($_GET, 'code');
$msg = $this->arrayGet($_GET, 'msg');
if ($do != '') {
if ($do == 'upload' && isset($_FILES['image'])) {
$file = $_FILES['image'];
$target = $this->arrayGet($_POST, 'target');
$template = $this->arrayGet($_POST, 'template');
$result = $this->upload($template, $file, $target);
extract($result);
$src = '';
if ($path) {
$path = substr($path, 3);
$src = BLOG_URL . $path;
}
ob_clean();
include $this->view('upload');
exit;
} else {
emDirect('./template.php');
}
} elseif ($template !== null) {
if (!is_dir(TPLS_PATH . $template)) {
$this->jsonReturn(array(
'code' => 1,
'msg' => '该模板不存在',
));
}
$options = $this->getTemplateDefinedOptions($template);
if ($options === false) {
$this->jsonReturn(array(
'code' => 1,
'msg' => '该模板不支持本插件设置',
));
}
$this->_currentTemplate = $template;
$storedOptions = $this->getTemplateOptions($template);
foreach ($options as $name => & $option) {
if (!is_array($option) || !isset($option['name']) || !isset($option['type']) || !isset($this->_types[$option['type']])) {
unset($options[$name]);
continue;
}
$option['id'] = $name;
$option['value'] = $storedOptions[$name];
}
$this->_options = $options;
if (!empty($_POST)) {
$newOptions = array();
foreach ($_POST as $name => $data) {
if (!isset($options[$name])) {
continue;
}
$depend = isset($options[$name]['depend']) ? $options[$name]['depend'] : '';
$type = isset($options[$name]['type']) ? $options[$name]['type'] : '';
switch ($depend) {
case 'sort':
$sorts = $this->getSorts(true);
if (!is_array($data)) {
$data = array();
}
foreach ($sorts as $sort) {
$sid = $sort['sid'];
if (!isset($data[$sid]) || $this->shouldBeArray($options[$name], $data[$sid])) {
$data[$sid] = $this->getOptionDefaultValue($options[$name], $template);
}
}
break;
}
if ($this->shouldBeArray($options[$name], $data)) {
$data = array();
}
$newOptions[$name] = array(
'depend' => $depend,
'data' => $data,
);
}
$result = $this->setTemplateOptions($template, $newOptions);
$code = $result ? 0 : 1;
$data = array(
'template' => $template,
'code' => $result ? 0 : 1,
'msg' => '保存模板设置' . ($result ? '成功' : '失败'),
);
$this->jsonReturn($data);
}
ob_clean();
include $this->view('setting');
exit;
} else {
emDirect('./template.php');
}
}
/**
* 判断是否本该为数组但不是数组的
* @param array $option
* @param mixed $data
* @return boolean
*/
private function shouldBeArray($option, $data) {
if (is_array($data)) {
return false;
}
if (in_array($option['type'], $this->_arrayTypes)) {
return true;
}
if (in_array($option['type'], array(
'sort',
'page'
)) && $this->isMulti($option)) {
return true;
}
return false;
}
/**
* 判断是否为多选/多行文本
* @param array $option
* @return boolean
*/
private function isMulti($option) {
return isset($option['multi']) && $option['multi'];
}
/**
* 上传文件
* @param string $template 模板
* @param array $file 上传的文件
* @param string $target 目标
* @return array 上传结果信息
*/
private function upload($template, $file, $target) {
$result = array(
'code' => 0,
'msg' => '',
'name' => $file['name'],
'size' => $file['size'],
'path' => '',
);
if ($file['error'] == 1) {
$result['code'] = 100;
$result['msg'] = '文件大小超过系统限制';
return $result;
} elseif ($file['error'] > 1) {
$result['code'] = 101;
$result['msg'] = '上传文件失败';
return $result;
}
$extension = getFileSuffix($file['name']);
if (!in_array($extension, $this->_imageTypes)) {
$result['code'] = 102;
$result['msg'] = '错误的文件类型';
return $result;
}
if (method_exists('Option', 'getAttMaxSize')) {
$maxSize = Option::getAttMaxSize();
} else {
$maxSize = Option::UPLOADFILE_MAXSIZE;
}
if ($file['size'] > $maxSize) {
$result['code'] = 103;
$result['msg'] = '文件大小超出emlog的限制';
return $result;
}
$uploadPath = Option::UPLOADFILE_PATH . self::ID . "/$template/";
$fileName = rtrim(str_replace(array(
'[',
']'
), '.', $target), '.') . '.' . $extension;
$attachpath = $uploadPath . $fileName;
$result['path'] = $attachpath;
if (!is_dir($uploadPath)) {
@umask(0);
$ret = @mkdir($uploadPath, 0777, true);
if ($ret === false) {
$result['code'] = 104;
$result['msg'] = '创建文件上传目录失败';
return $result;
}
}
if (@is_uploaded_file($file['tmp_name'])) {
if (@!move_uploaded_file($file['tmp_name'], $attachpath)) {
@unlink($tmpFile);
$result['code'] = 105;
$result['msg'] = '上传失败。文件上传目录(content/uploadfile)不可写';
return $result;
}
@chmod($attachpath, 0777);
}
return $result;
}
/**
* 获取设置项的值
* @param array $option 模板设置项
* @param array $storedOptions 存储的模板设置项
* @param string $template
* @return mixed
*/
private function getOptionValue(&$option, $storedOptions, $template) {
if (isset($storedOptions[$option['id']])) {
return $storedOptions[$option['id']];
}
return $this->getOptionDefaultValue($option, $template);
}
/**
* 获取模板设置项的值
* @param array $option 模板设置项
* @param string $template
* @return mixed
*/
private function getOptionDefaultValue(&$option, $template) {
if (isset($option['default']) && !in_array($option['type'], array(
'page',
'sort',
'tag'
))) {
$default = $option['default'];
} else {
switch ($option['type']) {
case 'radio':
if (!isset($option['values']) || !is_array($option['values'])) {
$option['values'] = array(
0 => '否',
1 => '是',
);
}
$default = $this->arrayGet(array_keys($option['values']), 0);
break;
case 'checkbox':
if (!isset($option['values']) || !is_array($option['values'])) {
$option['values'] = array();
}
$default = $option['values'];
break;
case 'text':
case 'image':
if (!isset($option['values']) || !is_array($option['values'])) {
$option['values'] = array();
}
$default = reset($option['values']);
break;
case 'page':
if (!$this->isMulti($option)) {
$pages = $this->getPages();
$default = $this->arrayGet(array_keys($pages), 0);
break;
}
case 'sort':
if (!$this->isMulti($option)) {
$sorts = $this->getSorts();
$default = $this->arrayGet(array_keys($sorts), 0);
break;
}
case 'tag':
$default = array();
break;
default:
return null;
}
}
return $this->replacePath($default, $template);
}
/**
* 替换设置项里的url
* @param mixed $value
* @param string $template
* @return mixed
*/
private function replacePath($value, $template) {
$replace = array(
TEMPLATE_URL => TPLS_URL . $template . '/',
);
if (is_string($value)) {
return strtr($value, $replace);
} elseif (is_array($value)) {
foreach ($value as $key => $val) {
$value[$key] = $this->replacePath($val, $template);
}
return $value;
} else {
return $value;
}
}
/**
* 渲染设置页面的设置项
* @return void
*/
private function renderOptions() {
foreach ($this->_options as $option) {
$method = 'render' . ucfirst($option['type']);
$this->$method($option);
}
}
/**
* 渲染模板设置
* @return void
*/
private function renderByTpl($option, $tpl, $loopValues = true, $placeholder = true) {
echo '<div class="option">';
echo '<div class="option-name">', $this->encode($option['name']), '</div>';
$depend = isset($option['depend']) ? $option['depend'] : 'none';
echo sprintf('<div class="option-body depend-%s">', $depend);
if (!empty($option['description'])) {
echo '<div class="option-description">', $option['description'], '</div>';
}
switch ($depend) {
case 'sort':
$unsorted = isset($option['unsorted']) ? $option['unsorted'] : true;
$sorts = $this->getSorts($unsorted);
if (!is_array($option['value'])) {
$option['value'] = array();
}
echo '<div class="option-sort" data-option-name="', $option['name'], '">';
echo '<div class="option-sort-left">';
if (count($sorts) < 8) {
foreach ($sorts as $sort) {
echo '<div class="option-sort-name">';
echo $sort['sortname'];
echo '</div>';
}
} else {
echo '<select class="option-sort-select">';
foreach ($sorts as $sort) {
echo sprintf('<option value="%s">%s</option>', $sort['sortname'], $sort['sortname']);
}
echo '</select>';
}
echo '</div>';
echo '<div class="option-sort-right">';
foreach ($sorts as $sort) {
$sid = $sort['sid'];
echo '<div class="option-sort-option">';
if (!isset($option['value'][$sid])) {
$option['value'][$sid] = $this->getOptionDefaultValue($option, $this->_currentTemplate);
}
if ($loopValues) {
if ($placeholder) {
echo sprintf('<input type="hidden" name="%s" value="">', $option['id'] . "[{$sid}]");
}
foreach ($option['values'] as $value => $label) {
echo strtr($tpl, array(
'{name}' => $option['id'] . "[{$sid}]",
'{value}' => $this->encode($value),
'{label}' => $label,
'{checked}' => $this->getCheckedString($value, $option['value'][$sid]),
));
}
} else {
echo strtr($tpl, array(
'{name}' => $option['id'] . "[{$sid}]",
'{value}' => $this->encode($option['value'][$sid]),
'{label}' => '',
'{path}' => $this->getImagePath($option['value'][$sid]),
'{rich}' => $this->getRichString($option),
));
}
echo '</div>';
}
echo '</div>';
echo '<div class="clearfix"></div>';
echo '</div>';
break;
default:
if ($loopValues) {
if ($placeholder) {
echo sprintf('<input type="hidden" name="%s" value="">', $option['id']);
}
foreach ($option['values'] as $value => $label) {
echo strtr($tpl, array(
'{name}' => $option['id'],
'{value}' => $this->encode($value),
'{label}' => $label,
'{checked}' => $this->getCheckedString($value, $option['value']),
));
}
} else {
echo strtr($tpl, array(
'{name}' => $option['id'],
'{value}' => $this->encode($option['value']),
'{label}' => '',
'{path}' => $this->getImagePath($option['value']),
'{rich}' => $this->getRichString($option),
));
}
}
echo '</div></div>';
}
/**
* @param mixed $value
* @param mixed $optionvalue
* @return string
*/
private function getCheckedString($value, $optionValue) {
return is_array($optionValue) && in_array($value, $optionValue) || $value == $optionValue ? ' checked="checked"' : '';
}
/**
* @param array $option
* @return string
*/
private function getRichString($option) {
return isset($option['rich']) && isset($this->_types[$option['type']]['allowRich']) ? ' option-rich-text' : '';
}
/**
* @param string $url
* @return string
*/
private function getImagePath($url) {
return str_replace(BLOG_URL, '', $url);
}
/**
* @param array $option
* @return void
*/
private function renderRadio($option) {
$tpl = '<label><input type="radio" name="{name}" value="{value}"{checked}> {label}</label>';
$this->renderByTpl($option, $tpl);
}
/**
* @param array $option
* @return void
*/
private function renderCheckbox($option) {
$tpl = '<label><input type="checkbox" name="{name}[]" value="{value}"{checked}> {label}</label>';
$this->renderByTpl($option, $tpl);
}
/**
* @param array $option
* @return void
*/
private function renderText($option) {
if ($this->isMulti($option)) {
$tpl = '<textarea name="{name}" rows="8" class="form-control option-textarea{rich}">{value}</textarea>';
} else {
$tpl = '<input type="text" class="form-control" name="{name}" value="{value}"><br>';
}
$this->renderByTpl($option, $tpl, false);
}
/**
* @param array $option
* @return void
*/
private function renderImage($option) {
$tpl = '<span class="image-tip">友情提示:选择文件后将会立刻上传覆盖原图</span><a href="{value}" target="_blank" data-name="{name}"><img src="{value}"></a><br><input type="file" accept="image/*" data-target="{name}"><input type="hidden" name="{name}" value="{path}">';
$this->renderByTpl($option, $tpl, false);
}
/**
* @param array $option
* @return void
*/
private function renderPage($option) {
$pages = $this->getPages();
$option['values'] = $pages;
if ($this->isMulti($option)) {
$this->renderCheckbox($option);
} else {
$this->renderRadio($option);
}
}
/**
* @param array $option
* @return void
*/
private function renderSort($option) {
if (isset($option['depend']) && $option['depend'] == 'sort') {
unset($option['depend']);
}
$sorts = $this->getSorts();
$values = array();
foreach ($sorts as $sid => $sort) {
$values[$sid] = $sort['sortname'];
}
$option['values'] = $values;
if ($this->isMulti($option)) {
$this->renderCheckbox($option);
} else {
$this->renderRadio($option);
}
}
/**
* @param array $option
* @return void
*/
private function renderTag($option) {
$tags = Cache::getInstance()->readCache('tags');
$values = array();
foreach ($tags as $tag) {
$values[$tag['tagname']] = "${tag['tagname']} (${tag['usenum']})";
}
$option['values'] = $values;
$this->renderCheckbox($option);
}
/**
* 转义字符串,防止悲剧
* @param string $value
* @return string
*/
private function encode($value) {
return htmlspecialchars($value);
}
/**
* 获取支持的模板
* @return array
*/
private function getTemplates() {
$handle = @opendir(TPLS_PATH);
if ($handle === false) {
return array();
}
$templates = array();