| 1 | <?php | 
|---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- | 
|---|
| 3 | # | 
|---|
| 4 | # This file is part of Dotclear 2. | 
|---|
| 5 | # | 
|---|
| 6 | # Copyright (c) 2003-2013 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,$desc='') | 
|---|
| 39 |      { | 
|---|
| 40 |           $this->__widgets[$id] = new dcWidget($id,$name,$callback,$desc); | 
|---|
| 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 $desc; | 
|---|
| 138 |      private $public_callback = null; | 
|---|
| 139 |      public $append_callback = null; | 
|---|
| 140 |      private $settings = array(); | 
|---|
| 141 |      private $advance_id; | 
|---|
| 142 |      private $advanced_settings = array(); | 
|---|
| 143 |       | 
|---|
| 144 |      public function serialize($order) { | 
|---|
| 145 |           $values = array(); | 
|---|
| 146 |           foreach ($this->settings as $k=>$v) | 
|---|
| 147 |                $values[$k]=$v['value']; | 
|---|
| 148 |           foreach ($this->advanced_settings as $k=>$v) | 
|---|
| 149 |                $values[$k]=$v['value']; | 
|---|
| 150 |           $values['id']=$this->id; | 
|---|
| 151 |           $values['order']=$order; | 
|---|
| 152 |           return $values; | 
|---|
| 153 |      } | 
|---|
| 154 |       | 
|---|
| 155 |      public function __construct($id,$name,$callback,$desc='') | 
|---|
| 156 |      { | 
|---|
| 157 |           $this->public_callback = $callback; | 
|---|
| 158 |           $this->id = $id; | 
|---|
| 159 |           $this->name = $name; | 
|---|
| 160 |           $this->desc = $desc; | 
|---|
| 161 |      } | 
|---|
| 162 |       | 
|---|
| 163 |      public function id() | 
|---|
| 164 |      { | 
|---|
| 165 |           return $this->id; | 
|---|
| 166 |      } | 
|---|
| 167 |       | 
|---|
| 168 |      public function name() | 
|---|
| 169 |      { | 
|---|
| 170 |           return $this->name; | 
|---|
| 171 |      } | 
|---|
| 172 |  | 
|---|
| 173 |      public function desc() | 
|---|
| 174 |      { | 
|---|
| 175 |           return $this->desc; | 
|---|
| 176 |      } | 
|---|
| 177 |       | 
|---|
| 178 |      public function getCallback() | 
|---|
| 179 |      { | 
|---|
| 180 |           return $this->public_callback; | 
|---|
| 181 |      } | 
|---|
| 182 |       | 
|---|
| 183 |      public function call($i=0) | 
|---|
| 184 |      { | 
|---|
| 185 |           if (is_callable($this->public_callback)) { | 
|---|
| 186 |                return call_user_func($this->public_callback,$this,$i); | 
|---|
| 187 |           } | 
|---|
| 188 |           return '<p>Callback not found for widget '.$this->id.'</p>'; | 
|---|
| 189 |      } | 
|---|
| 190 |       | 
|---|
| 191 |      /* Widget settings | 
|---|
| 192 |      --------------------------------------------------- */ | 
|---|
| 193 |      public function __get($n) | 
|---|
| 194 |      { | 
|---|
| 195 |           $setting = null; | 
|---|
| 196 |           if (isset($this->settings[$n])) { | 
|---|
| 197 |                $setting = $this->settings[$n]['value']; | 
|---|
| 198 |           } else if (isset($this->advanced_settings[$n])) { | 
|---|
| 199 |                $setting = $this->advanced_settings[$n]['value']; | 
|---|
| 200 |           } | 
|---|
| 201 |           return $setting; | 
|---|
| 202 |      } | 
|---|
| 203 |       | 
|---|
| 204 |      public function __set($n,$v) | 
|---|
| 205 |      { | 
|---|
| 206 |           if (isset($this->settings[$n])) { | 
|---|
| 207 |                $this->settings[$n]['value'] = $v; | 
|---|
| 208 |           } else if (isset($this->advanced_settings[$n])) { | 
|---|
| 209 |                $this->advanced_settings[$n]['value'] = $v; | 
|---|
| 210 |           } | 
|---|
| 211 |      } | 
|---|
| 212 |       | 
|---|
| 213 |      public function setting($name,$title,$value,$type='text') | 
|---|
| 214 |      { | 
|---|
| 215 |           if ($type == 'combo') { | 
|---|
| 216 |                $options = @func_get_arg(4); | 
|---|
| 217 |                if (!is_array($options)) { | 
|---|
| 218 |                     return false; | 
|---|
| 219 |                } | 
|---|
| 220 |           } | 
|---|
| 221 |            | 
|---|
| 222 |           $this->settings[$name] = array( | 
|---|
| 223 |                'title' => $title, | 
|---|
| 224 |                'type' => $type, | 
|---|
| 225 |                'value' => $value | 
|---|
| 226 |           ); | 
|---|
| 227 |            | 
|---|
| 228 |           if (isset($options)) { | 
|---|
| 229 |                $this->settings[$name]['options'] = $options; | 
|---|
| 230 |           } | 
|---|
| 231 |      } | 
|---|
| 232 |       | 
|---|
| 233 |      public function advanced_setting($name,$title,$value,$type='text') | 
|---|
| 234 |      { | 
|---|
| 235 |           if ($type == 'combo' || $type == 'radio') { | 
|---|
| 236 |                $options = @func_get_arg(4); | 
|---|
| 237 |                if (!is_array($options)) { | 
|---|
| 238 |                     return false; | 
|---|
| 239 |                } | 
|---|
| 240 |           } | 
|---|
| 241 |            | 
|---|
| 242 |           $this->advanced_settings[$name] = array( | 
|---|
| 243 |                'title' => $title, | 
|---|
| 244 |                'type' => $type, | 
|---|
| 245 |                'value' => $value | 
|---|
| 246 |           ); | 
|---|
| 247 |            | 
|---|
| 248 |           if (isset($options)) { | 
|---|
| 249 |                $this->advanced_settings[$name]['options'] = $options; | 
|---|
| 250 |           } | 
|---|
| 251 |      } | 
|---|
| 252 |       | 
|---|
| 253 |      public function advance_settings() | 
|---|
| 254 |      { | 
|---|
| 255 |           return $this->advanced_settings; | 
|---|
| 256 |      } | 
|---|
| 257 |       | 
|---|
| 258 |      public function settings() | 
|---|
| 259 |      { | 
|---|
| 260 |           return $this->settings; | 
|---|
| 261 |      } | 
|---|
| 262 |       | 
|---|
| 263 |      public function formSettings($pr='',&$i=0) | 
|---|
| 264 |      { | 
|---|
| 265 |           $res = ''; | 
|---|
| 266 |           foreach ($this->settings as $id => $s) | 
|---|
| 267 |           { | 
|---|
| 268 |                $res .= $this->formSetting($id,$s,$pr,$i=0); | 
|---|
| 269 |                $i++; | 
|---|
| 270 |           } | 
|---|
| 271 |            | 
|---|
| 272 |           if ( count($this->advanced_settings) > 0 ) | 
|---|
| 273 |           { | 
|---|
| 274 |                $res .= '<div class="widgetAdvancedSettings">'; | 
|---|
| 275 |                $res .= '<h5>'.__('Réglages avancés').'</h5>'; | 
|---|
| 276 |            | 
|---|
| 277 |                foreach ($this->advanced_settings as $id => $s) | 
|---|
| 278 |                { | 
|---|
| 279 |                     $res .= $this->formSetting($id,$s,$pr,$i); | 
|---|
| 280 |                     $i++; | 
|---|
| 281 |                } | 
|---|
| 282 |                 | 
|---|
| 283 |                $res .= '</div>'; | 
|---|
| 284 |           } | 
|---|
| 285 |            | 
|---|
| 286 |           return $res; | 
|---|
| 287 |      } | 
|---|
| 288 |       | 
|---|
| 289 |      public function formSetting($id,$s,$pr='',&$i=0) | 
|---|
| 290 |      { | 
|---|
| 291 |           $res = ''; | 
|---|
| 292 |           $wfid = "wf-".$i; | 
|---|
| 293 |           $iname = $pr ? $pr.'['.$id.']' : $id; | 
|---|
| 294 |           switch ($s['type']) | 
|---|
| 295 |           { | 
|---|
| 296 |                case 'text': | 
|---|
| 297 |                     $res .= | 
|---|
| 298 |                     '<p><label for="'.$wfid.'">'.$s['title'].'</label> '. | 
|---|
| 299 |                     form::field(array($iname,$wfid),20,255,html::escapeHTML($s['value']),'maximal'). | 
|---|
| 300 |                     '</p>'; | 
|---|
| 301 |                     break; | 
|---|
| 302 |                case 'textarea': | 
|---|
| 303 |                     $res .= | 
|---|
| 304 |                     '<p><label for="'.$wfid.'">'.$s['title'].'</label> '. | 
|---|
| 305 |                     form::textarea(array($iname,$wfid),30,5,html::escapeHTML($s['value']),'maximal'). | 
|---|
| 306 |                     '</p>'; | 
|---|
| 307 |                     break; | 
|---|
| 308 |                case 'check': | 
|---|
| 309 |                     $res .= | 
|---|
| 310 |                     '<p>'.form::hidden(array($iname),'0'). | 
|---|
| 311 |                     '<label class="classic" for="'.$wfid.'">'. | 
|---|
| 312 |                     form::checkbox(array($iname,$wfid),'1',$s['value']).' '.$s['title']. | 
|---|
| 313 |                     '</label></p>'; | 
|---|
| 314 |                     break; | 
|---|
| 315 |                case 'radio': | 
|---|
| 316 |                     $res .= '<p>'.($s['title'] ? '<label class="classic">'.$s['title'].'</label><br/>' : ''); | 
|---|
| 317 |                     if(!empty($s['options'])) { | 
|---|
| 318 |                          foreach ($s['options'] as $k => $v) { | 
|---|
| 319 |                               $res .= $k > 0 ? '<br/>' : ''; | 
|---|
| 320 |                               $res .= | 
|---|
| 321 |                               '<label class="classic" for="'.$wfid.'-'.$k.'">'. | 
|---|
| 322 |                               form::radio(array($iname,$wfid.'-'.$k),$v[1],$s['value'] == $v[1]).' '.$v[0]. | 
|---|
| 323 |                               '</label>'; | 
|---|
| 324 |                          } | 
|---|
| 325 |                     } | 
|---|
| 326 |                     $res .= '</p>'; | 
|---|
| 327 |                     break; | 
|---|
| 328 |                case 'combo': | 
|---|
| 329 |                     $res .= | 
|---|
| 330 |                     '<p><label for="'.$wfid.'">'.$s['title'].'</label> '. | 
|---|
| 331 |                     form::combo(array($iname,$wfid),$s['options'],$s['value']). | 
|---|
| 332 |                     '</p>'; | 
|---|
| 333 |                     break; | 
|---|
| 334 |           } | 
|---|
| 335 |            | 
|---|
| 336 |           return $res; | 
|---|
| 337 |      } | 
|---|
| 338 | } | 
|---|
| 339 | ?> | 
|---|