[0] | 1 | <?php |
---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
| 3 | # |
---|
| 4 | # This file is part of Dotclear 2. |
---|
| 5 | # |
---|
[1179] | 6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
[0] | 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(); |
---|
[2566] | 17 | |
---|
[0] | 18 | public static function load($s) |
---|
| 19 | { |
---|
| 20 | $o = @unserialize(base64_decode($s)); |
---|
[2566] | 21 | |
---|
[0] | 22 | if ($o instanceof self) { |
---|
| 23 | return $o; |
---|
| 24 | } else { |
---|
| 25 | return self::loadArray($o,$GLOBALS['__widgets']); |
---|
| 26 | } |
---|
| 27 | } |
---|
[2566] | 28 | |
---|
[0] | 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 | } |
---|
[2566] | 37 | |
---|
[954] | 38 | public function create($id,$name,$callback,$append_callback=null,$desc='') |
---|
[0] | 39 | { |
---|
[954] | 40 | $this->__widgets[$id] = new dcWidget($id,$name,$callback,$desc); |
---|
[0] | 41 | $this->__widgets[$id]->append_callback = $append_callback; |
---|
| 42 | } |
---|
[2566] | 43 | |
---|
[0] | 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 | } |
---|
[2566] | 53 | |
---|
[0] | 54 | public function isEmpty() |
---|
| 55 | { |
---|
| 56 | return count($this->__widgets) == 0; |
---|
| 57 | } |
---|
[2566] | 58 | |
---|
[0] | 59 | public function elements($sorted=false) |
---|
| 60 | { |
---|
| 61 | if ($sorted) { |
---|
| 62 | uasort($this->__widgets,array('self','sort')); |
---|
| 63 | } |
---|
| 64 | return $this->__widgets; |
---|
| 65 | } |
---|
[2566] | 66 | |
---|
[0] | 67 | public function __get($id) |
---|
| 68 | { |
---|
| 69 | if (!isset($this->__widgets[$id])) { |
---|
| 70 | return null; |
---|
| 71 | } |
---|
| 72 | return $this->__widgets[$id]; |
---|
| 73 | } |
---|
[2566] | 74 | |
---|
[0] | 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 | } |
---|
[2566] | 84 | |
---|
[0] | 85 | public static function loadArray($A,$widgets) |
---|
| 86 | { |
---|
| 87 | if (!($widgets instanceof self)) { |
---|
| 88 | return false; |
---|
| 89 | } |
---|
[2566] | 90 | |
---|
[0] | 91 | uasort($A,array('self','arraySort')); |
---|
[2566] | 92 | |
---|
[0] | 93 | $result = new self; |
---|
| 94 | foreach ($A as $v) |
---|
| 95 | { |
---|
| 96 | if ($widgets->{$v['id']} != null) |
---|
| 97 | { |
---|
| 98 | $w = clone $widgets->{$v['id']}; |
---|
[2566] | 99 | |
---|
[0] | 100 | # Settings |
---|
| 101 | unset($v['id']); |
---|
| 102 | unset($v['order']); |
---|
| 103 | foreach ($v as $sid => $s) { |
---|
| 104 | $w->{$sid} = $s; |
---|
| 105 | } |
---|
[2566] | 106 | |
---|
[0] | 107 | $result->append($w); |
---|
| 108 | } |
---|
| 109 | } |
---|
[2566] | 110 | |
---|
[0] | 111 | return $result; |
---|
| 112 | } |
---|
[2566] | 113 | |
---|
[0] | 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 | } |
---|
[2566] | 121 | |
---|
[0] | 122 | private static function sort($a,$b) |
---|
| 123 | { |
---|
[2566] | 124 | $c = $a->name(); |
---|
| 125 | $d = $b->name(); |
---|
| 126 | if ($c == $d) { |
---|
| 127 | return 0; |
---|
| 128 | } |
---|
| 129 | return ($c < $d) ? -1 : 1; |
---|
[0] | 130 | } |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | class dcWidget |
---|
| 134 | { |
---|
| 135 | private $id; |
---|
| 136 | private $name; |
---|
[954] | 137 | private $desc; |
---|
[0] | 138 | private $public_callback = null; |
---|
| 139 | public $append_callback = null; |
---|
| 140 | private $settings = array(); |
---|
[2566] | 141 | |
---|
[0] | 142 | public function serialize($order) { |
---|
| 143 | $values = array(); |
---|
| 144 | foreach ($this->settings as $k=>$v) |
---|
| 145 | $values[$k]=$v['value']; |
---|
| 146 | $values['id']=$this->id; |
---|
| 147 | $values['order']=$order; |
---|
| 148 | return $values; |
---|
| 149 | } |
---|
[2566] | 150 | |
---|
[954] | 151 | public function __construct($id,$name,$callback,$desc='') |
---|
[0] | 152 | { |
---|
| 153 | $this->public_callback = $callback; |
---|
| 154 | $this->id = $id; |
---|
| 155 | $this->name = $name; |
---|
[954] | 156 | $this->desc = $desc; |
---|
[0] | 157 | } |
---|
[2566] | 158 | |
---|
[0] | 159 | public function id() |
---|
| 160 | { |
---|
| 161 | return $this->id; |
---|
| 162 | } |
---|
[2566] | 163 | |
---|
[0] | 164 | public function name() |
---|
| 165 | { |
---|
| 166 | return $this->name; |
---|
| 167 | } |
---|
[954] | 168 | |
---|
| 169 | public function desc() |
---|
| 170 | { |
---|
| 171 | return $this->desc; |
---|
| 172 | } |
---|
[2566] | 173 | |
---|
[0] | 174 | public function getCallback() |
---|
| 175 | { |
---|
| 176 | return $this->public_callback; |
---|
| 177 | } |
---|
[2566] | 178 | |
---|
[0] | 179 | public function call($i=0) |
---|
| 180 | { |
---|
| 181 | if (is_callable($this->public_callback)) { |
---|
| 182 | return call_user_func($this->public_callback,$this,$i); |
---|
| 183 | } |
---|
| 184 | return '<p>Callback not found for widget '.$this->id.'</p>'; |
---|
| 185 | } |
---|
[2566] | 186 | |
---|
[2662] | 187 | /* Widget rendering tool |
---|
| 188 | --------------------------------------------------- */ |
---|
[2667] | 189 | public function renderDiv($content_only,$class,$attr,$content) |
---|
| 190 | { |
---|
| 191 | if ($content_only) { |
---|
| 192 | return $content; |
---|
| 193 | } |
---|
| 194 | $ret = '<div class="widget'.($class ? ' '.html::escapeHTML($class) : '').'"'.($attr ? ' '.$attr : '').'>'."\n"; |
---|
| 195 | $ret .= $content."\n"; |
---|
| 196 | $ret .= '</div>'."\n"; |
---|
| 197 | |
---|
| 198 | return $ret; |
---|
| 199 | } |
---|
| 200 | |
---|
[2662] | 201 | public function renderTitle($title) |
---|
| 202 | { |
---|
| 203 | global $core; |
---|
| 204 | |
---|
| 205 | if (!$title) { |
---|
| 206 | return ''; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | $wtscheme = $core->themes->moduleInfo($core->blog->settings->system->theme,'widgettitleformat'); |
---|
| 210 | if (empty($wtscheme)) { |
---|
| 211 | $tplset = $core->themes->moduleInfo($core->blog->settings->system->theme,'tplset'); |
---|
| 212 | if (empty($tplset) || $tplset == DC_DEFAULT_TPLSET) { |
---|
| 213 | // Use H2 for mustek based themes |
---|
| 214 | $wtscheme = '<h2>%s</h2>'; |
---|
| 215 | } else { |
---|
| 216 | // Use H3 for currywurst based themes |
---|
| 217 | $wtscheme = '<h3>%s</h3>'; |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | $ret = sprintf($wtscheme,$title); |
---|
| 221 | |
---|
| 222 | return $ret; |
---|
| 223 | } |
---|
| 224 | |
---|
[2667] | 225 | public function renderSubtitle($title,$render=true) |
---|
| 226 | { |
---|
| 227 | global $core; |
---|
| 228 | |
---|
| 229 | if (!$title && $render) { |
---|
| 230 | return ''; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | $wtscheme = $core->themes->moduleInfo($core->blog->settings->system->theme,'widgetsubtitleformat'); |
---|
| 234 | if (empty($wtscheme)) { |
---|
| 235 | $tplset = $core->themes->moduleInfo($core->blog->settings->system->theme,'tplset'); |
---|
| 236 | if (empty($tplset) || $tplset == DC_DEFAULT_TPLSET) { |
---|
| 237 | // Use H2 for mustek based themes |
---|
| 238 | $wtscheme = '<h3>%s</h3>'; |
---|
| 239 | } else { |
---|
| 240 | // Use H3 for currywurst based themes |
---|
| 241 | $wtscheme = '<h4>%s</h4>'; |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | if (!$render) { |
---|
| 245 | return $wtscheme; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | $ret = sprintf($wtscheme,$title); |
---|
| 249 | return $ret; |
---|
| 250 | } |
---|
| 251 | |
---|
[0] | 252 | /* Widget settings |
---|
| 253 | --------------------------------------------------- */ |
---|
| 254 | public function __get($n) |
---|
| 255 | { |
---|
| 256 | if (isset($this->settings[$n])) { |
---|
[2412] | 257 | return $this->settings[$n]['value']; |
---|
[0] | 258 | } |
---|
[2412] | 259 | return null; |
---|
[0] | 260 | } |
---|
[2566] | 261 | |
---|
[0] | 262 | public function __set($n,$v) |
---|
| 263 | { |
---|
| 264 | if (isset($this->settings[$n])) { |
---|
| 265 | $this->settings[$n]['value'] = $v; |
---|
| 266 | } |
---|
| 267 | } |
---|
[2566] | 268 | |
---|
[0] | 269 | public function setting($name,$title,$value,$type='text') |
---|
| 270 | { |
---|
| 271 | if ($type == 'combo') { |
---|
| 272 | $options = @func_get_arg(4); |
---|
| 273 | if (!is_array($options)) { |
---|
| 274 | return false; |
---|
| 275 | } |
---|
| 276 | } |
---|
[2566] | 277 | |
---|
[0] | 278 | $this->settings[$name] = array( |
---|
| 279 | 'title' => $title, |
---|
| 280 | 'type' => $type, |
---|
| 281 | 'value' => $value |
---|
| 282 | ); |
---|
[2566] | 283 | |
---|
[0] | 284 | if (isset($options)) { |
---|
| 285 | $this->settings[$name]['options'] = $options; |
---|
| 286 | } |
---|
| 287 | } |
---|
[2566] | 288 | |
---|
[0] | 289 | public function settings() |
---|
| 290 | { |
---|
| 291 | return $this->settings; |
---|
| 292 | } |
---|
[2566] | 293 | |
---|
[117] | 294 | public function formSettings($pr='',&$i=0) |
---|
[0] | 295 | { |
---|
| 296 | $res = ''; |
---|
| 297 | foreach ($this->settings as $id => $s) |
---|
| 298 | { |
---|
[2442] | 299 | $res .= $this->formSetting($id,$s,$pr,$i); |
---|
[2395] | 300 | $i++; |
---|
| 301 | } |
---|
[2566] | 302 | |
---|
[2395] | 303 | return $res; |
---|
| 304 | } |
---|
[2566] | 305 | |
---|
[2395] | 306 | public function formSetting($id,$s,$pr='',&$i=0) |
---|
| 307 | { |
---|
| 308 | $res = ''; |
---|
| 309 | $wfid = "wf-".$i; |
---|
| 310 | $iname = $pr ? $pr.'['.$id.']' : $id; |
---|
| 311 | switch ($s['type']) |
---|
| 312 | { |
---|
| 313 | case 'text': |
---|
| 314 | $res .= |
---|
| 315 | '<p><label for="'.$wfid.'">'.$s['title'].'</label> '. |
---|
| 316 | form::field(array($iname,$wfid),20,255,html::escapeHTML($s['value']),'maximal'). |
---|
| 317 | '</p>'; |
---|
| 318 | break; |
---|
| 319 | case 'textarea': |
---|
| 320 | $res .= |
---|
| 321 | '<p><label for="'.$wfid.'">'.$s['title'].'</label> '. |
---|
| 322 | form::textarea(array($iname,$wfid),30,5,html::escapeHTML($s['value']),'maximal'). |
---|
| 323 | '</p>'; |
---|
| 324 | break; |
---|
| 325 | case 'check': |
---|
| 326 | $res .= |
---|
| 327 | '<p>'.form::hidden(array($iname),'0'). |
---|
| 328 | '<label class="classic" for="'.$wfid.'">'. |
---|
| 329 | form::checkbox(array($iname,$wfid),'1',$s['value']).' '.$s['title']. |
---|
| 330 | '</label></p>'; |
---|
| 331 | break; |
---|
| 332 | case 'radio': |
---|
| 333 | $res .= '<p>'.($s['title'] ? '<label class="classic">'.$s['title'].'</label><br/>' : ''); |
---|
| 334 | if(!empty($s['options'])) { |
---|
| 335 | foreach ($s['options'] as $k => $v) { |
---|
| 336 | $res .= $k > 0 ? '<br/>' : ''; |
---|
| 337 | $res .= |
---|
| 338 | '<label class="classic" for="'.$wfid.'-'.$k.'">'. |
---|
| 339 | form::radio(array($iname,$wfid.'-'.$k),$v[1],$s['value'] == $v[1]).' '.$v[0]. |
---|
| 340 | '</label>'; |
---|
| 341 | } |
---|
| 342 | } |
---|
| 343 | $res .= '</p>'; |
---|
| 344 | break; |
---|
| 345 | case 'combo': |
---|
| 346 | $res .= |
---|
| 347 | '<p><label for="'.$wfid.'">'.$s['title'].'</label> '. |
---|
| 348 | form::combo(array($iname,$wfid),$s['options'],$s['value']). |
---|
| 349 | '</p>'; |
---|
| 350 | break; |
---|
[0] | 351 | } |
---|
| 352 | return $res; |
---|
| 353 | } |
---|
| 354 | } |
---|