File: class.widgets.php

package info (click to toggle)
dotclear 2.6.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 8,420 kB
  • sloc: php: 54,270; sql: 1,290; sh: 213; xml: 173; makefile: 158
file content (289 lines) | stat: -rw-r--r-- 5,822 bytes parent folder | download
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
<?php
# -- BEGIN LICENSE BLOCK ---------------------------------------
#
# This file is part of Dotclear 2.
#
# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
# Licensed under the GPL version 2.0 license.
# See LICENSE file or
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# -- END LICENSE BLOCK -----------------------------------------
if (!defined('DC_RC_PATH')) { return; }

class dcWidgets
{
	private $__widgets = array();

	public static function load($s)
	{
		$o = @unserialize(base64_decode($s));

		if ($o instanceof self) {
			return $o;
		} else {
			return self::loadArray($o,$GLOBALS['__widgets']);
		}
	}

	public function store()
	{
		$serialized = array();
		foreach ($this->__widgets as $pos=>$w) {
			$serialized[]=($w->serialize($pos));
		}
		return base64_encode(serialize($serialized));
	}

	public function create($id,$name,$callback,$append_callback=null,$desc='')
	{
		$this->__widgets[$id] = new dcWidget($id,$name,$callback,$desc);
		$this->__widgets[$id]->append_callback = $append_callback;
	}

	public function append($widget)
	{
		if ($widget instanceof dcWidget) {
			if (is_callable($widget->append_callback)) {
				call_user_func($widget->append_callback,$widget);
			}
			$this->__widgets[] = $widget;
		}
	}

	public function isEmpty()
	{
		return count($this->__widgets) == 0;
	}

	public function elements($sorted=false)
	{
		if ($sorted) {
			uasort($this->__widgets,array('self','sort'));
		}
		return $this->__widgets;
	}

	public function __get($id)
	{
		if (!isset($this->__widgets[$id])) {
			return null;
		}
		return $this->__widgets[$id];
	}

	public function __wakeup()
	{
		foreach ($this->__widgets as $i => $w)
		{
			if (!($w instanceof dcWidget)) {
				unset($this->__widgets[$i]);
			}
		}
	}

	public static function loadArray($A,$widgets)
	{
		if (!($widgets instanceof self)) {
			return false;
		}

		uasort($A,array('self','arraySort'));

		$result = new self;
		foreach ($A as $v)
		{
			if ($widgets->{$v['id']} != null)
			{
				$w = clone $widgets->{$v['id']};

				# Settings
				unset($v['id']);
				unset($v['order']);
				foreach ($v as $sid => $s) {
					$w->{$sid} = $s;
				}

				$result->append($w);
			}
		}

		return $result;
	}

	private static function arraySort($a, $b)
	{
		if ($a['order'] == $b['order']) {
			return 0;
		}
		return $a['order'] > $b['order'] ? 1 : -1;
	}

	private static function sort($a,$b)
	{
		$c = $a->name();
		$d = $b->name();
		if ($c == $d) {
			return 0;
		}
		return ($c < $d) ? -1 : 1;
	}
}

class dcWidget
{
	private $id;
	private $name;
	private $desc;
	private $public_callback = null;
	public $append_callback = null;
	private $settings = array();

	public function serialize($order) {
		$values = array();
		foreach ($this->settings as $k=>$v)
			$values[$k]=$v['value'];
		$values['id']=$this->id;
		$values['order']=$order;
		return $values;
	}

	public function __construct($id,$name,$callback,$desc='')
	{
		$this->public_callback = $callback;
		$this->id = $id;
		$this->name = $name;
		$this->desc = $desc;
	}

	public function id()
	{
		return $this->id;
	}

	public function name()
	{
		return $this->name;
	}

	public function desc()
	{
		return $this->desc;
	}

	public function getCallback()
	{
		return $this->public_callback;
	}

	public function call($i=0)
	{
		if (is_callable($this->public_callback)) {
			return call_user_func($this->public_callback,$this,$i);
		}
		return '<p>Callback not found for widget '.$this->id.'</p>';
	}

	/* Widget settings
	--------------------------------------------------- */
	public function __get($n)
	{
		if (isset($this->settings[$n])) {
			return $this->settings[$n]['value'];
		}
		return null;
	}

	public function __set($n,$v)
	{
		if (isset($this->settings[$n])) {
			$this->settings[$n]['value'] = $v;
		}
	}

	public function setting($name,$title,$value,$type='text')
	{
		if ($type == 'combo') {
			$options = @func_get_arg(4);
			if (!is_array($options)) {
				return false;
			}
		}

		$this->settings[$name] = array(
			'title' => $title,
			'type' => $type,
			'value' => $value
		);

		if (isset($options)) {
			$this->settings[$name]['options'] = $options;
		}
	}

	public function settings()
	{
		return $this->settings;
	}

	public function formSettings($pr='',&$i=0)
	{
		$res = '';
		foreach ($this->settings as $id => $s)
		{
			$res .= $this->formSetting($id,$s,$pr,$i);
			$i++;
		}

		return $res;
	}

	public function formSetting($id,$s,$pr='',&$i=0)
	{
		$res = '';
		$wfid = "wf-".$i;
		$iname = $pr ? $pr.'['.$id.']' : $id;
		switch ($s['type'])
		{
			case 'text':
				$res .=
				'<p><label for="'.$wfid.'">'.$s['title'].'</label> '.
				form::field(array($iname,$wfid),20,255,html::escapeHTML($s['value']),'maximal').
				'</p>';
				break;
			case 'textarea':
				$res .=
				'<p><label for="'.$wfid.'">'.$s['title'].'</label> '.
				form::textarea(array($iname,$wfid),30,5,html::escapeHTML($s['value']),'maximal').
				'</p>';
				break;
			case 'check':
				$res .=
				'<p>'.form::hidden(array($iname),'0').
				'<label class="classic" for="'.$wfid.'">'.
				form::checkbox(array($iname,$wfid),'1',$s['value']).' '.$s['title'].
				'</label></p>';
				break;
			case 'radio':
				$res .= '<p>'.($s['title'] ? '<label class="classic">'.$s['title'].'</label><br/>' : '');
				if(!empty($s['options'])) {
					foreach ($s['options'] as $k => $v) {
						$res .= $k > 0 ? '<br/>' : '';
						$res .=
						'<label class="classic" for="'.$wfid.'-'.$k.'">'.
						form::radio(array($iname,$wfid.'-'.$k),$v[1],$s['value'] == $v[1]).' '.$v[0].
						'</label>';
					}
				}
				$res .= '</p>';
				break;
			case 'combo':
				$res .=
				'<p><label for="'.$wfid.'">'.$s['title'].'</label> '.
				form::combo(array($iname,$wfid),$s['options'],$s['value']).
				'</p>';
				break;
		}
		return $res;
	}
}