1 | <?php |
---|
2 | |
---|
3 | class dcToolbar |
---|
4 | { |
---|
5 | protected $core; |
---|
6 | protected $toolbars; |
---|
7 | |
---|
8 | public function __construct($core) |
---|
9 | { |
---|
10 | $this->core = $core; |
---|
11 | $this->toolbars = array(); |
---|
12 | $this->i18n = array(); |
---|
13 | } |
---|
14 | |
---|
15 | public function addFormatter($formatter) |
---|
16 | { |
---|
17 | if (!is_string($formatter) || empty($formatter)) { |
---|
18 | return; |
---|
19 | } |
---|
20 | |
---|
21 | $toolbar = array( |
---|
22 | 'settings' => array(), |
---|
23 | 'plugins' => array(), |
---|
24 | 'buttons' => array( |
---|
25 | 1 => array(), |
---|
26 | 2 => array(), |
---|
27 | 3 => array(), |
---|
28 | 4 => array() |
---|
29 | ) |
---|
30 | ); |
---|
31 | |
---|
32 | $this->toolbars[$formatter] = array_key_exists($formatter,$this->toolbars) ? $this->toolbars[$formatter] : $toolbar; |
---|
33 | } |
---|
34 | |
---|
35 | public function addSettings($formatter,$settings) |
---|
36 | { |
---|
37 | |
---|
38 | if (!array_key_exists($formatter,$this->toolbars)) { |
---|
39 | return; |
---|
40 | } |
---|
41 | if (!is_array($settings)) { |
---|
42 | return; |
---|
43 | } |
---|
44 | |
---|
45 | $this->toolbars[$formatter]['settings'] = array_merge($this->toolbars[$formatter]['settings'],$settings); |
---|
46 | } |
---|
47 | |
---|
48 | public function addPlugins($formatter,$plugins) |
---|
49 | { |
---|
50 | if (!array_key_exists($formatter,$this->toolbars)) { |
---|
51 | return; |
---|
52 | } |
---|
53 | if (!is_array($plugins)) { |
---|
54 | return; |
---|
55 | } |
---|
56 | |
---|
57 | $this->toolbars[$formatter]['plugins'] = array_merge($this->toolbars[$formatter]['plugins'],$plugins); |
---|
58 | } |
---|
59 | |
---|
60 | public function addButtons($formatter,$buttons) |
---|
61 | { |
---|
62 | if (!array_key_exists($formatter,$this->toolbars)) { |
---|
63 | return; |
---|
64 | } |
---|
65 | if (!is_array($buttons)) { |
---|
66 | return; |
---|
67 | } |
---|
68 | |
---|
69 | foreach($buttons as $level => $ids) { |
---|
70 | if (!is_array($ids)) { |
---|
71 | continue; |
---|
72 | } |
---|
73 | $level = !preg_match('#^[1-4]$#',$level) ? 1 : $level; |
---|
74 | $this->toolbars[$formatter]['buttons'][$level] = array_merge($this->toolbars[$formatter]['buttons'][$level],$ids); |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | public function addI18n($component,$i18n) |
---|
79 | { |
---|
80 | if (!is_array($i18n)) { |
---|
81 | return; |
---|
82 | } |
---|
83 | if (!array_key_exists($component,$this->i18n)) { |
---|
84 | $this->i18n[$component] = array(); |
---|
85 | } |
---|
86 | |
---|
87 | $this->i18n[$component] = array_merge($this->i18n[$component],$i18n); |
---|
88 | } |
---|
89 | |
---|
90 | public function getJS() |
---|
91 | { |
---|
92 | $res = "dcToolBar = new dcToolBar();\n"; |
---|
93 | |
---|
94 | $this->core->callBehavior('adminToolbar',$this); |
---|
95 | |
---|
96 | foreach ($this->toolbars as $formatter => $options) { |
---|
97 | $s = $options['settings']; |
---|
98 | |
---|
99 | // Add plugins |
---|
100 | array_walk($options['plugins'],create_function('&$v,$k','$v=!$v ? "-".$k : $k;')); |
---|
101 | $s['plugins'] = implode(',',$options['plugins']); |
---|
102 | |
---|
103 | // Add buttons |
---|
104 | foreach ($options['buttons'] as $level => $buttons) { |
---|
105 | $s[sprintf('theme_advanced_buttons%d',$level)] = implode(',',$buttons); |
---|
106 | } |
---|
107 | |
---|
108 | // Add configuration |
---|
109 | array_walk($s,create_function('&$v,$k','$v=sprintf("%s: %s",$k,(!preg_match("#^(true|false|{.*})$#",$v)?"\'".$v."\'":$v));')); |
---|
110 | $res .= sprintf("dcToolBar.setConfig('%s',{%s});\n",$formatter,implode(",\n",$s)); |
---|
111 | } |
---|
112 | |
---|
113 | // Add translation |
---|
114 | $i18n = array(); |
---|
115 | |
---|
116 | foreach ($this->i18n as $component => $values) { |
---|
117 | array_walk($this->i18n[$component],create_function('&$v,$k','$v=sprintf("\'%s\': \'%s\'",$k,html::escapeJS($v));')); |
---|
118 | array_push($i18n,sprintf('%s: {%s}',$component,implode(',',$this->i18n[$component]))); |
---|
119 | } |
---|
120 | |
---|
121 | $res .= !empty($i18n) ? sprintf("dcToolBar.setI18n({%s: {%s}});\n",'en',implode(",\n",$i18n)) : ''; |
---|
122 | |
---|
123 | return $res; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | ?> |
---|