1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2010 Olivier Meunier & Association Dotclear |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
12 | if (!defined('DC_RC_PATH')) { return; } |
---|
13 | |
---|
14 | class dcWidgets |
---|
15 | { |
---|
16 | private $__widgets = array(); |
---|
17 | |
---|
18 | public static function load($s) |
---|
19 | { |
---|
20 | $o = @unserialize(base64_decode($s)); |
---|
21 | |
---|
22 | if ($o instanceof self) { |
---|
23 | return $o; |
---|
24 | } else { |
---|
25 | return self::loadArray($o,$GLOBALS['__widgets']); |
---|
26 | } |
---|
27 | } |
---|
28 | |
---|
29 | public function store() |
---|
30 | { |
---|
31 | $serialized = array(); |
---|
32 | foreach ($this->__widgets as $pos=>$w) { |
---|
33 | $serialized[]=($w->serialize($pos)); |
---|
34 | } |
---|
35 | return base64_encode(serialize($serialized)); |
---|
36 | } |
---|
37 | |
---|
38 | public function create($id,$name,$callback,$append_callback=null) |
---|
39 | { |
---|
40 | $this->__widgets[$id] = new dcWidget($id,$name,$callback); |
---|
41 | $this->__widgets[$id]->append_callback = $append_callback; |
---|
42 | } |
---|
43 | |
---|
44 | public function append($widget) |
---|
45 | { |
---|
46 | if ($widget instanceof dcWidget) { |
---|
47 | if (is_callable($widget->append_callback)) { |
---|
48 | call_user_func($widget->append_callback,$widget); |
---|
49 | } |
---|
50 | $this->__widgets[] = $widget; |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | public function isEmpty() |
---|
55 | { |
---|
56 | return count($this->__widgets) == 0; |
---|
57 | } |
---|
58 | |
---|
59 | public function elements($sorted=false) |
---|
60 | { |
---|
61 | if ($sorted) { |
---|
62 | uasort($this->__widgets,array('self','sort')); |
---|
63 | } |
---|
64 | return $this->__widgets; |
---|
65 | } |
---|
66 | |
---|
67 | public function __get($id) |
---|
68 | { |
---|
69 | if (!isset($this->__widgets[$id])) { |
---|
70 | return null; |
---|
71 | } |
---|
72 | return $this->__widgets[$id]; |
---|
73 | } |
---|
74 | |
---|
75 | public function __wakeup() |
---|
76 | { |
---|
77 | foreach ($this->__widgets as $i => $w) |
---|
78 | { |
---|
79 | if (!($w instanceof dcWidget)) { |
---|
80 | unset($this->__widgets[$i]); |
---|
81 | } |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | public static function loadArray($A,$widgets) |
---|
86 | { |
---|
87 | if (!($widgets instanceof self)) { |
---|
88 | return false; |
---|
89 | } |
---|
90 | |
---|
91 | uasort($A,array('self','arraySort')); |
---|
92 | |
---|
93 | $result = new self; |
---|
94 | foreach ($A as $v) |
---|
95 | { |
---|
96 | if ($widgets->{$v['id']} != null) |
---|
97 | { |
---|
98 | $w = clone $widgets->{$v['id']}; |
---|
99 | |
---|
100 | # Settings |
---|
101 | unset($v['id']); |
---|
102 | unset($v['order']); |
---|
103 | foreach ($v as $sid => $s) { |
---|
104 | $w->{$sid} = $s; |
---|
105 | } |
---|
106 | |
---|
107 | $result->append($w); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | return $result; |
---|
112 | } |
---|
113 | |
---|
114 | private static function arraySort($a, $b) |
---|
115 | { |
---|
116 | if ($a['order'] == $b['order']) { |
---|
117 | return 0; |
---|
118 | } |
---|
119 | return $a['order'] > $b['order'] ? 1 : -1; |
---|
120 | } |
---|
121 | |
---|
122 | private static function sort($a,$b) |
---|
123 | { |
---|
124 | $c = $a->name(); |
---|
125 | $d = $b->name(); |
---|
126 | if ($c == $d) { |
---|
127 | return 0; |
---|
128 | } |
---|
129 | return ($c < $d) ? -1 : 1; |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | class dcWidget |
---|
134 | { |
---|
135 | private $id; |
---|
136 | private $name; |
---|
137 | private $public_callback = null; |
---|
138 | public $append_callback = null; |
---|
139 | private $settings = array(); |
---|
140 | |
---|
141 | public function serialize($order) { |
---|
142 | $values = array(); |
---|
143 | foreach ($this->settings as $k=>$v) |
---|
144 | $values[$k]=$v['value']; |
---|
145 | $values['id']=$this->id; |
---|
146 | $values['order']=$order; |
---|
147 | return $values; |
---|
148 | } |
---|
149 | |
---|
150 | public function __construct($id,$name,$callback) |
---|
151 | { |
---|
152 | $this->public_callback = $callback; |
---|
153 | $this->id = $id; |
---|
154 | $this->name = $name; |
---|
155 | } |
---|
156 | |
---|
157 | public function id() |
---|
158 | { |
---|
159 | return $this->id; |
---|
160 | } |
---|
161 | |
---|
162 | public function name() |
---|
163 | { |
---|
164 | return $this->name; |
---|
165 | } |
---|
166 | |
---|
167 | public function getCallback() |
---|
168 | { |
---|
169 | return $this->public_callback; |
---|
170 | } |
---|
171 | |
---|
172 | public function call($i=0) |
---|
173 | { |
---|
174 | if (is_callable($this->public_callback)) { |
---|
175 | return call_user_func($this->public_callback,$this,$i); |
---|
176 | } |
---|
177 | return '<p>Callback not found for widget '.$this->id.'</p>'; |
---|
178 | } |
---|
179 | |
---|
180 | /* Widget settings |
---|
181 | --------------------------------------------------- */ |
---|
182 | public function __get($n) |
---|
183 | { |
---|
184 | if (isset($this->settings[$n])) { |
---|
185 | return $this->settings[$n]['value']; |
---|
186 | } |
---|
187 | return null; |
---|
188 | } |
---|
189 | |
---|
190 | public function __set($n,$v) |
---|
191 | { |
---|
192 | if (isset($this->settings[$n])) { |
---|
193 | $this->settings[$n]['value'] = $v; |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | public function setting($name,$title,$value,$type='text') |
---|
198 | { |
---|
199 | if ($type == 'combo') { |
---|
200 | $options = @func_get_arg(4); |
---|
201 | if (!is_array($options)) { |
---|
202 | return false; |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | $this->settings[$name] = array( |
---|
207 | 'title' => $title, |
---|
208 | 'type' => $type, |
---|
209 | 'value' => $value |
---|
210 | ); |
---|
211 | |
---|
212 | if (isset($options)) { |
---|
213 | $this->settings[$name]['options'] = $options; |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | public function settings() |
---|
218 | { |
---|
219 | return $this->settings; |
---|
220 | } |
---|
221 | |
---|
222 | public function formSettings($pr='',&$i=0) |
---|
223 | { |
---|
224 | $res = ''; |
---|
225 | foreach ($this->settings as $id => $s) |
---|
226 | { |
---|
227 | $wfid = "wf-".$i; |
---|
228 | $iname = $pr ? $pr.'['.$id.']' : $id; |
---|
229 | switch ($s['type']) |
---|
230 | { |
---|
231 | case 'text': |
---|
232 | $res .= |
---|
233 | '<p><label for="'.$wfid.'">'.$s['title'].' '. |
---|
234 | form::field(array($iname,$wfid),20,255,html::escapeHTML($s['value']),'maximal'). |
---|
235 | '</label></p>'; |
---|
236 | break; |
---|
237 | case 'textarea': |
---|
238 | $res .= |
---|
239 | '<p><label for="'.$wfid.'">'.$s['title'].' '. |
---|
240 | form::textarea(array($iname,$wfid),30,5,html::escapeHTML($s['value']),'maximal'). |
---|
241 | '</label></p>'; |
---|
242 | break; |
---|
243 | case 'check': |
---|
244 | $res .= |
---|
245 | '<p>'.form::hidden(array($iname),'0'). |
---|
246 | '<label class="classic" for="'.$wfid.'">'. |
---|
247 | form::checkbox(array($iname,$wfid),'1',$s['value']).' '.$s['title']. |
---|
248 | '</label></p>'; |
---|
249 | break; |
---|
250 | case 'combo': |
---|
251 | $res .= |
---|
252 | '<p><label for="'.$wfid.'">'.$s['title'].' '. |
---|
253 | form::combo(array($iname,$wfid),$s['options'],$s['value']). |
---|
254 | '</label></p>'; |
---|
255 | break; |
---|
256 | } |
---|
257 | $i++; |
---|
258 | } |
---|
259 | |
---|
260 | return $res; |
---|
261 | } |
---|
262 | } |
---|
263 | ?> |
---|