fieldsetregistry.inc.php
8.45 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
<?php
/* fun with adaptors.
*
* Each fieldset type needs to handle
* - widget generation
* - validator generation
* - view rendering
* - configuration
* - fulltext generation
* - search widget/criteria
*
* Very little of this is actually handled in this version. In fact, at the
* moment the entire thing is faked. This is done to make add/edit a little
* more sane.
*
* In the long term, the "correct" approach here will be to make the actual
* KTFieldsetType a class, and to extend fieldset with a "config_array" and
* "fieldset_type_namespace" params.
*
* Comments and suggestions on this are welcome.
*/
require_once(KT_LIB_DIR . "/widgets/widgetfactory.inc.php");
require_once(KT_LIB_DIR . "/validation/validatorfactory.inc.php");
class KTFieldsetRegistry {
var $fieldsettypes;
var $oVF;
var $oWF;
function &getSingleton () {
if (!KTUtil::arrayGet($GLOBALS['_KT_PLUGIN'], 'oKTFieldsetRegistry')) {
$GLOBALS['_KT_PLUGIN']['oKTFieldsetRegistry'] = new KTFieldsetRegistry;
}
return $GLOBALS['_KT_PLUGIN']['oKTFieldsetRegistry'];
}
function registerType($sClassname, $sNS, $sFile) {
// stub
}
function formElementsForFieldset($fieldsetOrType, $sContainerName, $oDocument = null) {
// we want to create:
//
// - validators
// - widgets
//
// use $oDocument as a stub *if* its set.
}
function widgetsForFieldset($fieldsetOrType, $sContainerName, $oDocument = null) {
// this is likely to be called repeatedly.
if (is_null($this->oWF)) {
$this->oWF =& KTWidgetFactory::getSingleton();
}
// we're going to create one of two things, here:
// - conditional fieldset widget
// - a "Fieldset" widget
// FIXME delegate.
$oFieldset =& $fieldsetOrType;
if ($oFieldset->getIsConditional()) {
return PEAR::raiseError(_kt("Conditional Fieldsets are not yet implemented"));
} else {
$widgets = array();
$fields = $oFieldset->getFields();
foreach ($fields as $oField) {
$fname = 'metadata_' . $oField->getId();
$value = null;
// check if we had an old value
if (!is_null($oDocument)) {
$oFL = DocumentFieldLink::getByDocumentAndField($oDocument, $oField);
if (!is_null($oFL) && (!PEAR::isError($oFL))) {
$value = $oFL->getValue();
}
}
// we have to hack in support for the hardcoded types of fields
// handled by the "generic" widget.
//
// we try to make this a little more "sane"
$type = '';
if ($oField->getHasLookup()) {
if ($oField->getHasLookupTree()) {
$type = 'ktcore.fields.tree';
} else {
$type = 'ktcore.fields.lookup';
}
} else {
$type = 'ktcore.fields.string';
}
if ($type == 'ktcore.fields.string') {
$widgets[] = $this->oWF->get('ktcore.widgets.string', array(
'label' => $oField->getName(),
'required' => $oField->getIsMandatory(),
'name' => $fname,
'value' => $value,
'description' => $oField->getDescription(),
));
} else if ($type == 'ktcore.fields.lookup') {
$widgets[] = $this->oWF->get('ktcore.widgets.entityselection', array(
'label' => $oField->getName(),
'required' => $oField->getIsMandatory(),
'name' => $fname,
'value' => $value,
'description' => $oField->getDescription(),
'vocab' => MetaData::getEnabledByDocumentField($oField),
'id_method' => 'getName',
'label_method' => 'getName',
'unselected_label' => _kt("No selection."),
));
} else if ($type == 'ktcore.fields.tree') {
$widgets[] = $this->oWF->get('ktcore.widgets.treemetadata', array(
'label' => $oField->getName(),
'required' => $oField->getIsMandatory(),
'name' => $fname,
'value' => $value,
'description' => $oField->getDescription(),
'vocab' => MetaData::getEnabledByDocumentField($oField),
'field_id' => $oField->getId(),
));
}
}
return array($this->oWF->get('ktcore.widgets.fieldset',array(
'label' => $oFieldset->getName(),
'description' => $oFieldset->getDescription(),
'name' => $sContainerName,
'widgets' => $widgets,
)));
}
}
function validatorsForFieldset($fieldsetOrType, $sContainerName, $oDocument = null, $bIncludeAuto = false) {
// this is likely to be called repeatedly.
if (is_null($this->oVF)) {
$this->oVF =& KTValidatorFactory::getSingleton();
}
// FIXME delegate.
$oFieldset =& $fieldsetOrType;
if ($oFieldset->getIsConditional()) {
return PEAR::raiseError(_kt("Conditional Fieldsets are not yet implemented"));
} else {
$validators = array();
$fields = $oFieldset->getFields();
if ($bIncludeAuto) {
// we need to do *all* validation
// since we may be used outside a form.
//
// to prevent code duplication, we cheat and get the autovalidators
// this makes add/edit forms marginally less efficient, but we'll deal.
$widgets = $this->widgetsForFieldset($oFieldset, $sContainerName, $sDocument);
$validators = kt_array_merge($validators, $widgets[0]->getValidators());
}
foreach ($fields as $oField) {
$type = '';
if ($oField->getHasLookup()) {
if ($oField->getHasLookupTree()) {
$type = 'ktcore.fields.tree';
} else {
$type = 'ktcore.fields.lookup';
}
} else {
$type = 'ktcore.fields.string';
}
$fname = 'metadata_' . $oField->getId();
if ($type == 'ktcore.fields.string') {
$validators[] = $this->oVF->get('ktcore.validators.string',array(
'test' => $fname,
'output' => $fname,
));
} else if ($type == 'ktcore.fields.lookup') {
$validators[] = $this->oVF->get('ktcore.validators.membership',array(
'test' => $fname,
'output' => $fname,
'vocab' => MetaData::getEnabledValuesByDocumentField($oField),
'id_method' => 'getName',
));
} else if ($type == 'ktcore.fields.tree') {
// FIXME we really need to make tree entries richer
$validators[] = $this->oVF->get('ktcore.validators.membership',array(
'test' => $fname,
'output' => $fname,
'vocab' => MetaData::getEnabledValuesByDocumentField($oField),
'id_method' => 'getName',
));
} else {
$validators[] = PEAR::raiseError(sprintf(_kt("Unable to deal with field: id %d"), $oField->getId()));
}
}
return array($this->oVF->get('ktcore.validators.fieldset',array(
'test' => $sContainerName,
'output' => $sContainerName,
'validators' => $validators,
)));
}
}
}
?>