KTWidgets.php
14.4 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
require_once(KT_LIB_DIR . "/widgets/basewidget.inc.php");
require_once(KT_LIB_DIR . "/templating/templating.inc.php");
class KTCoreStringWidget extends KTWidget {
var $sNamespace = 'ktcore.widgets.string';
var $sTemplate = 'ktcore/forms/widgets/string';
}
class KTCoreFileWidget extends KTWidget {
var $sNamespace = 'ktcore.widgets.file';
var $sTemplate = 'ktcore/forms/widgets/file';
function wrapName($outer) {
$this->sName = sprintf("_kt_attempt_unique_%s", $this->sName);
// we don't have access via "wrap" when processing, so we can't actually
// wrap. just don't use a lot of names
}
function process($data){
$tname = sprintf("_kt_attempt_unique_%s", $this->sName);
return array($this->sBasename => $_FILES[$tname]);
}
function getValidators() {
if (!$this->bAutoValidate) {
return null;
}
$oVF =& KTValidatorFactory::getSingleton();
return $oVF->get('ktcore.validators.requiredfile', array(
'test' => sprintf("_kt_attempt_unique_%s", $this->sName),
'basename' => $this->sBasename,
));
}
}
class KTCoreTextWidget extends KTWidget {
var $sNamespace = 'ktcore.widgets.text';
var $sTemplate = 'ktcore/forms/widgets/text';
}
class KTCoreReasonWidget extends KTWidget {
var $sNamespace = 'ktcore.widgets.reason';
var $sTemplate = 'ktcore/forms/widgets/text';
function configure($aOptions) {
$res = parent::configure($aOptions);
if (PEAR::isError($res)) {
return $res;
}
// FIXME make required *either* per-action property
// FIXME or a global pref.
$global_required_default = true;
$this->bRequired = KTUtil::arrayGet($aOptions, 'required', $global_required_default, false);
$this->aOptions['cols'] = KTUtil::arrayGet($aOptions, 'cols', 60);
$this->aOptions['rows'] = KTUtil::arrayGet($aOptions, 'rows', 3);
}
}
class KTCoreBooleanWidget extends KTWidget {
var $sNamespace = 'ktcore.widgets.boolean';
var $sTemplate = 'ktcore/forms/widgets/boolean';
function setDefault($mValue) {
$this->value = ($mValue == true);
}
}
class KTCorePasswordWidget extends KTWidget {
var $sNamespace = 'ktcore.widgets.password';
var $sTemplate = 'ktcore/forms/widgets/password';
var $bConfirm = false;
var $sConfirmDescription;
function configure($aOptions) {
$res = parent::configure($aOptions);
if (PEAR::isError($res)) {
return $res;
}
$this->bConfirm = KTUtil::arrayGet($aOptions, 'confirm', false);
$this->sConfirmDescription = KTUtil::arrayGet($aOptions, 'confirm_description');
}
function process($raw_data) {
// since we're essentially a string, pass *that* out as the primary
// but we also might want to confirm, and if so we use a private name
$res = array();
if ($this->bConfirm) {
$res['_password_confirm_' . $this->sBasename] = array(
'base' => $raw_data[$this->sBasename]['base'],
'confirm' => $raw_data[$this->sBasename]['confirm'],
);
$res[$this->sBasename] = $raw_data[$this->sBasename]['base'];
} else {
$res[$this->sBasename] = $raw_data[$this->sName];
}
return $res;
}
function getValidators() {
if (!$this->bAutoValidate) {
return null;
}
$oVF =& KTValidatorFactory::getSingleton();
$val = array();
$val[] = parent::getValidators(); // required, etc.
$val[] = $oVF->get('ktcore.validators.password', array(
'test' => $this->sOrigname,
'basename' => $this->sBasename
));
return $val;
}
}
class KTCoreSelectionWidget extends KTWidget {
var $sNamespace = 'ktcore.widgets.selection';
var $bMulti = false; // multiselection
var $USE_SIMPLE = 10; // point at which to switch to a dropdown/multiselect
var $bUseSimple; // only use checkboxes, regardless of size
var $aVocab;
var $sEmptyMessage;
var $_valuesearch;
function configure($aOptions) {
$res = parent::configure($aOptions);
if (PEAR::isError($res)) {
return $res;
}
$this->bUseSimple = KTUtil::arrayGet($aOptions, 'simple_select', null, false);
$this->bMulti = KTUtil::arrayGet($aOptions, 'multi', false);
$this->aVocab = (array) KTUtil::arrayGet($aOptions, 'vocab');
$this->sEmptyMessage = KTUtil::arrayGet($aOptions, 'empty_message',
_kt('No options available for this field.'));
}
function getWidget() {
// very simple, general purpose passthrough. Chances are this is sufficient,
// just override the template being used.
$bHasErrors = false;
if (count($this->aErrors) != 0) { $bHasErrors = true; }
// at this last moment we pick the template to use
$total = count($this->aVocab);
if ($this->bUseSimple === true) {
$this->sTemplate = 'ktcore/forms/widgets/simple_selection';
} else if ($this->bUseSimple === false) {
$this->sTemplate = 'ktcore/forms/widgets/selection';
} else if (is_null($this->bUseSimple) && ($total <= $this->USE_SIMPLE)) {
$this->sTemplate = 'ktcore/forms/widgets/simple_selection';
} else {
$this->sTemplate = 'ktcore/forms/widgets/selection';
}
$oTemplating =& KTTemplating::getSingleton();
$oTemplate = $oTemplating->loadTemplate($this->sTemplate);
// performance optimisation for large selected sets.
if ($this->bMulti) {
$this->_valuesearch = array();
$value = (array) $this->value;
foreach ($value as $v) {
$this->_valuesearch[$v] = true;
}
}
$aTemplateData = array(
"context" => $this,
"name" => $this->sName,
"has_id" => ($this->sId !== null),
"id" => $this->sId,
"has_value" => ($this->value !== null),
"value" => $this->value,
"options" => $this->aOptions,
'vocab' => $this->aVocab,
);
return $oTemplate->render($aTemplateData);
}
function selected($lookup) {
if ($this->bMulti) {
return $this->_valuesearch[$lookup];
} else {
return ($this->value == $lookup);
}
}
function process($raw_data) {
return array($this->sBasename => $raw_data[$this->sBasename]);
}
}
// this happens so often, its worth creating a util function for it
class KTCoreEntitySelectionWidget extends KTCoreSelectionWidget {
var $sNamespace = 'ktcore.widgets.entityselection';
var $sIdMethod;
var $sLabelMethod;
function configure($aOptions) {
$res = parent::configure($aOptions);
if (PEAR::isError($res)) {
return $res;
}
// the selection widget's configure method has already setup almost
// all the vars we need. we have one utility here, where you pass
// in a list of existing entities that match the query, and we work
// from there.
$this->sIdMethod = KTUtil::arrayGet($aOptions, 'id_method', 'getId');
$this->sLabelMethod = KTUtil::arrayGet($aOptions, 'label_method');
if (empty($this->sLabelMethod)) {
return PEAR::raiseError(_kt('No label method specified.'));
}
$existing_entities = (array) KTUtil::arrayGet($aOptions, 'existing_entities');
// now we construct the "value" array from this set
// BUT ONLY IF WE DON'T HAVE A "VALUE" array.
if (empty($this->value)) {
$this->value = array();
foreach ($existing_entities as $oEntity) {
$this->value[] = call_user_func(array(&$oEntity, $this->sIdMethod));
}
}
// we next walk the "vocab" array, constructing a new one based on the
// functions passed in so far.
$new_vocab = array();
foreach ($this->aVocab as $oEntity) {
$id = call_user_func(array(&$oEntity, $this->sIdMethod));
$label = call_user_func(array(&$oEntity, $this->sLabelMethod));
$new_vocab[$id] = $label;
}
$this->aVocab = $new_vocab;
}
}
// wrap a set of fields into a core, basic one.
//
// this *also* subdivides the form data output namespace.
// to do this, it encapsulates a *large* amount of the KTWidget API
class KTCoreFieldsetWidget extends KTWidget {
var $sNamespace = 'ktcore.widgets.fieldset';
var $_widgets;
var $sDescription;
var $sLabel;
function configure($aOptions) {
// do NOT use parent.
$this->sLabel = KTUtil::arrayGet($aOptions, 'label');
$this->sDescription = KTUtil::arrayGet($aOptions, 'description');
$this->sName = KTUtil::arrayGet($aOptions, 'name');
$this->sBasename = $this->sName;
$aWidgets = (array) KTUtil::arrayGet($aOptions, 'widgets');
// very similar to the one in forms.inc.php
if (is_null($this->_oWF)) {
$this->_oWF =& KTWidgetFactory::getSingleton();
}
$this->_widgets = array();
// we don't want to expose the factory stuff to the user - its an
// arbitrary distinction to the user. Good point from NBM ;)
foreach ($aWidgets as $aInfo) {
if (is_null($aInfo)) {
continue;
} else if (is_object($aInfo)) {
// assume this is a fully configured object
$this->_widgets[] = $aInfo;
} else {
$namespaceOrObject = $aInfo[0];
$config = (array) $aInfo[1];
$this->_widgets[] = $this->_oWF->get($namespaceOrObject, $config);
}
}
}
function render() {
$oTemplating =& KTTemplating::getSingleton();
$oTemplate = $oTemplating->loadTemplate('ktcore/forms/widgets/fieldset');
$aTemplateData = array(
"context" => $this,
"label" => $this->sLabel,
"description" => $this->sDescription,
"widgets" => $this->renderWidgets(),
);
return $oTemplate->render($aTemplateData);
}
function renderWidgets() {
$rendered = array();
foreach ($this->_widgets as $v) {
if (PEAR::isError($v)) {
$rendered[] = sprintf(_kt('<div class="ktError"><p>Unable to show widget — %s</p></div>'), $v->getMessage());
} else {
$rendered[] = $v->render();
}
}
return implode(' ', $rendered);
}
function getDefault() {
// we need to do a little more admin here
// to obtain the default
// annoyingly
$d = array();
foreach ($this->_widgets as $w) {
if (PEAR::isError($w)) {
continue;
}
$d[$w->getBasename()] = $w->getDefault();
}
return $d;
}
function setDefault($aValue) {
$d = (array) $aValue;
foreach ($this->_widgets as $k => $w) {
$oWidget =& $this->_widgets[$k];
$oWidget->setDefault(KTUtil::arrayGet($d, $oWidget->getBasename(), $oWidget->getDefault()));
}
}
function wrapName($sOuter) {
$this->sName = sprintf('%s[%s]', $sOuter, $this->sBasename);
// now, chain to our children
foreach ($this->_widgets as $k => $v) {
$oWidget =& $this->_widgets[$k];
if (PEAR::isError($oWidget)) {
continue;
}
$oWidget->wrapName($this->sName);
}
}
function setErrors($aErrors = null) {
if (is_array($aErrors)) {
$this->aErrors = $aErrors;
}
foreach ($this->_widgets as $k => $w) {
$oWidget =& $this->_widgets[$k];
$oWidget->setErrors(KTUtil::arrayGet($aErrors, $oWidget->getBasename()));
}
}
function getValidators() {
// we use a fieldsetValidator here.
$extra_validators = array();
foreach ($this->_widgets as $oWidget) {
$res = $oWidget->getValidators();
if (!is_null($res)) {
if (is_array($res)) {
$extra_validators = kt_array_merge($extra_validators, $res);
} else {
$extra_validators[] = $res;
}
}
}
$oVF =& KTValidatorFactory::getSingleton();
return array($oVF->get('ktcore.validators.fieldset', array(
'test' => $this->sBasename,
'validators' => &$extra_validators,
)));
}
function process($raw_data) {
$d = (array) KTUtil::arrayGet($raw_data, $this->sBasename);
$o = array();
// we now need to recombine the process
foreach ($this->_widgets as $oWidget) {
$o =& kt_array_merge($o, $oWidget->process($d));
}
return array($this->sBasename => $o);
}
}
class KTCoreTransparentFieldsetWidget extends KTCoreFieldsetWidget {
var $sNamespace = 'ktcore.widgets.transparentfieldset';
function render() {
$oTemplating =& KTTemplating::getSingleton();
$oTemplate = $oTemplating->loadTemplate('ktcore/forms/widgets/transparent_fieldset');
$aTemplateData = array(
"widgets" => $this->renderWidgets(),
);
return $oTemplate->render($aTemplateData);
}
}
class KTExtraConditionalFieldsetWidget extends KTCoreFieldsetWidget {
var $sNamespace = 'ktextra.conditionalmetadata.fieldset';
function render() {
$oTemplating =& KTTemplating::getSingleton();
$oTemplate = $oTemplating->loadTemplate('ktcore/forms/widgets/conditionalfieldset');
$aTemplateData = array(
"context" => $this,
"label" => $this->sLabel,
"description" => $this->sDescription,
);
return $oTemplate->render($aTemplateData);
}
}
?>