documentFields.php
15.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
<?php
require_once('../../../../../config/dmsDefaults.php');
require_once(KT_LIB_DIR . '/dispatcher.inc.php');
require_once(KT_LIB_DIR . '/templating/templating.inc.php');
require_once(KT_LIB_DIR . '/documentmanagement/DocumentField.inc');
require_once(KT_LIB_DIR . '/metadata/fieldset.inc.php');
require_once(KT_LIB_DIR . '/metadata/metadatautil.inc.php');
$sectionName = "Administration";
require_once(KT_DIR . "/presentation/webpageTemplate.inc");
class KTDocumentFieldDispatcher extends KTStandardDispatcher {
var $bAutomaticTransaction = true;
// Breadcrumbs base - added to in methods
var $aBreadcrumbs = array(
array('action' => 'administration', 'name' => 'Administration'),
array('action' => 'docfield', 'name' => 'Document Field Management'),
);
// {{{ do_main
function do_main () {
$oTemplating =& KTTemplating::getSingleton();
$oTemplate =& $oTemplating->loadTemplate('ktcore/metadata/listFieldsets');
$oTemplate->setData(array(
'fieldsets' => KTFieldset::getList(),
));
return $oTemplate;
}
// }}}
// {{{ do_edit
function do_edit() {
$oTemplating =& KTTemplating::getSingleton();
$oTemplate =& $oTemplating->loadTemplate('ktcore/metadata/editFieldset');
$oFieldset =& KTFieldset::get($_REQUEST['fFieldsetId']);
$this->aBreadcrumbs[] = array(
'action' => 'docfield',
'query' => 'action=edit&fFieldsetId=' . $_REQUEST['fFieldsetId'],
'name' => 'Fieldset ' . $oFieldset->getName()
);
$oTemplate->setData(array(
'oFieldset' => $oFieldset,
));
return $oTemplate;
}
// }}}
// {{{ edit_object
function do_editobject() {
$oFieldset =& KTFieldset::get($_REQUEST['fFieldsetId']);
$oFieldset->setName($_REQUEST['name']);
$oFieldset->setNamespace($_REQUEST['namespace']);
$res = $oFieldset->update();
if (PEAR::isError($res) || ($res === false)) {
$this->errorRedirectTo('edit', 'Could not save fieldset changes', 'fFieldsetId=' . $oFieldset->getId());
exit(0);
}
$this->successRedirectTo('edit', 'Changes saved', 'fFieldsetId=' . $oFieldset->getId());
exit(0);
}
// }}}
// {{{ do_new
function do_new() {
if (KTUtil::arrayGet($_REQUEST, 'generic')) {
$generic = true;
} else {
$generic = false;
}
$res = KTFieldset::createFromArray(array(
'name' => $_REQUEST['name'],
'namespace' => $_REQUEST['namespace'],
'mandatory' => false,
'isconditional' => false,
'isgeneric' => $generic,
));
if (PEAR::isError($res) || ($res === false)) {
$this->errorRedirectToMain('Could not create fieldset');
exit(0);
}
$this->successRedirectTo('edit', 'Fieldset created', 'fFieldsetId=' . $res->getId());
exit(0);
}
// }}}
// {{{ do_newfield
function do_newfield() {
$is_lookup = false;
$is_tree = false;
if ($_REQUEST['type'] === "lookup") {
$is_lookup = true;
}
if ($_REQUEST['type'] === "tree") {
$is_lookup = true;
$is_tree = true;
}
$oFieldset = KTFieldset::get($_REQUEST['fFieldsetId']);
$oField =& DocumentField::createFromArray(array(
'name' => $_REQUEST['name'],
'datatype' => 'STRING',
'haslookup' => $is_lookup,
'haslookuptree' => $is_tree,
'parentfieldset' => $oFieldset->getId(),
));
if (PEAR::isError($res) || ($res === false)) {
$this->errorRedirectTo('edit', 'Could not create field', 'fFieldsetId=' . $oFieldset->getId());
exit(0);
}
if ($is_lookup) {
$this->successRedirectTo('editField', 'Field created', 'fFieldsetId=' . $oFieldset->getId() . '&fFieldId=' . $oField->getId());
} else {
$this->successRedirectTo('edit', 'Field created', 'fFieldsetId=' . $oFieldset->getId());
}
exit(0);
}
// }}}
// {{{ do_editField
function do_editField() {
$oTemplating =& KTTemplating::getSingleton();
$oTemplate =& $oTemplating->loadTemplate('ktcore/metadata/editField');
$oFieldset =& KTFieldset::get($_REQUEST['fFieldsetId']);
$oField =& DocumentField::get($_REQUEST['fFieldId']);
$this->aBreadcrumbs[] = array(
'action' => 'docfield',
'query' => 'action=edit&fFieldsetId=' . $_REQUEST['fFieldsetId'],
'name' => 'Fieldset ' . $oFieldset->getName()
);
$this->aBreadcrumbs[] = array(
'name' => 'Field ' . $oField->getName()
);
$oTemplate->setData(array(
'oFieldset' => $oFieldset,
'oField' => $oField,
));
return $oTemplate;
}
// }}}
// {{{ do_editFieldObject
function do_editFieldObject() {
$oTemplating =& KTTemplating::getSingleton();
$oTemplate =& $oTemplating->loadTemplate('ktcore/metadata/editField');
$oFieldset =& KTFieldset::get($_REQUEST['fFieldsetId']);
$oField =& DocumentField::get($_REQUEST['fFieldId']);
$oField->setName($_REQUEST['name']);
$res = $oField->update();
if (PEAR::isError($res) || ($res === false)) {
$this->errorRedirectTo('editField', 'Could not save field changes', 'fFieldsetId=' . $oFieldset->getId() . '&fFieldId=' . $oField->getId());
exit(0);
}
$this->successRedirectTo('editField', 'Changes saved', 'fFieldsetId=' . $oFieldset->getId() . '&fFieldId=' . $oField->getId());
exit(0);
}
// }}}
// {{{ do_addLookups
function do_addLookups() {
$oFieldset =& KTFieldset::get($_REQUEST['fFieldsetId']);
$oField =& DocumentField::get($_REQUEST['fFieldId']);
if (empty($_REQUEST['value'])) {
$this->errorRedirectTo('editField', 'Empty lookup not added', 'fFieldsetId=' . $oFieldset->getId() . '&fFieldId=' . $oField->getId());
}
$oMetaData =& MetaData::createFromArray(array(
'name' => $_REQUEST['value'],
'docfieldid' => $oField->getId(),
));
$this->successRedirectTo('editField', 'Lookup added', 'fFieldsetId=' . $oFieldset->getId() . '&fFieldId=' . $oField->getId());
exit(0);
}
// }}}
// {{{ do_removeLookups
function do_removeLookups() {
$oFieldset =& KTFieldset::get($_REQUEST['fFieldsetId']);
$oField =& DocumentField::get($_REQUEST['fFieldId']);
$aMetadata = KTUtil::arrayGet($_REQUEST, 'metadata');
if (empty($aMetadata)) {
$this->errorRedirectTo('editField', 'No lookups selected', 'fFieldsetId=' . $oFieldset->getId() . '&fFieldId=' . $oField->getId());
}
foreach ($_REQUEST['metadata'] as $iMetaDataId) {
$oMetaData =& MetaData::get($iMetaDataId);
$oMetaData->delete();
}
$this->successRedirectTo('editField', 'Lookups removed', 'fFieldsetId=' . $oFieldset->getId() . '&fFieldId=' . $oField->getId());
exit(0);
}
// }}}
// {{{ do_becomeconditional
function do_becomeconditional() {
$oFieldset =& KTFieldset::get($_REQUEST['fFieldsetId']);
$oFieldset->setIsConditional(true);
$oFieldset->setIsComplete(false);
$res = $oFieldset->update();
if (PEAR::isError($res) || ($res === false)) {
$this->errorRedirectTo('edit', 'Could not become conditional', 'fFieldsetId=' . $oFieldset->getId());
exit(0);
}
$this->successRedirectTo('edit', 'Became conditional', 'fFieldsetId=' . $oFieldset->getId());
exit(0);
}
// }}}
// {{{ do_removeconditional
function do_removeconditional() {
$oFieldset =& KTFieldset::get($_REQUEST['fFieldsetId']);
$oFieldset->setIsConditional(false);
$oFieldset->setIsComplete(true);
$res = $oFieldset->update();
if (PEAR::isError($res) || ($res === false)) {
$this->errorRedirectTo('edit', 'Could not stop being conditional', 'fFieldsetId=' . $oFieldset->getId());
exit(0);
}
$this->successRedirectTo('edit', 'Became no longer conditional', 'fFieldsetId=' . $oFieldset->getId());
exit(0);
}
// }}}
// {{{ do_removeFields
function do_removeFields() {
$oFieldset =& KTFieldset::get($_REQUEST['fFieldsetId']);
foreach ($_REQUEST['fields'] as $iFieldId) {
$oField =& DocumentField::get($iFieldId);
$oField->delete();
}
$this->successRedirectTo('edit', 'Fields removed', 'fFieldsetId=' . $oFieldset->getId());
exit(0);
}
// }}}
// {{{ do_manageConditional
function do_manageConditional () {
$oTemplating =& KTTemplating::getSingleton();
$oTemplate =& $oTemplating->loadTemplate('ktcore/metadata/conditional/manageConditional');
$oFieldset =& KTFieldset::get($_REQUEST['fFieldsetId']);
$iMasterFieldId = $oFieldset->getMasterFieldId();
if (!empty($iMasterFieldId)) {
$oMasterField =& DocumentField::get($iMasterFieldId);
if (PEAR::isError($oMasterField)) {
$oMasterField = null;
}
} else {
$oMasterField = null;
}
$sTable = KTUtil::getTableName('field_orders');
$aQuery = array(
"SELECT parent_field_id, child_field_id FROM $sTable WHERE fieldset_id = ?",
array($oFieldset->getId())
);
$aFieldOrders = DBUtil::getResultArray($aQuery);
$aFields = $oFieldset->getFields();
$aFreeFieldIds = array();
foreach ($aFields as $oField) {
$aFreeFieldIds[] = $oField->getId();
}
if ($oMasterField) {
$aParentFieldIds = array($oMasterField->getId());
foreach ($aFieldOrders as $aRow) {
$aParentFieldIds[] = $aRow['child_field_id'];
}
$aParentFields = array();
foreach (array_unique($aParentFieldIds) as $iId) {
$aParentFields[] =& DocumentField::get($iId);
}
$aFreeFields = array();
foreach ($aFreeFieldIds as $iId) {
if (in_array($iId, $aParentFieldIds)) {
continue;
}
$aFreeFields[] =& DocumentField::get($iId);
}
}
$res = KTMetadataUtil::checkConditionalFieldsetCompleteness($oFieldset);
if (PEAR::isError($res)) {
$sIncomplete = $res->getMessage();
} else {
$sIncomplete = null;
}
$this->aBreadcrumbs[] = array(
'action' => 'docfield',
'query' => 'action=edit&fFieldsetId=' . $_REQUEST['fFieldsetId'],
'name' => 'Fieldset ' . $oFieldset->getName()
);
$this->aBreadcrumbs[] = array(
'action' => 'docfield',
'query' => 'action=manageConditional&fFieldsetId=' . $_REQUEST['fFieldsetId'],
'name' => 'Manage conditional field',
);
$oTemplate->setData(array(
'oFieldset' => $oFieldset,
'free_fields' => $aFreeFields,
'parent_fields' => $aParentFields,
'aFieldOrders' => $aFieldOrders,
'oMasterField' => $oMasterField,
'sIncomplete' => $sIncomplete,
));
return $oTemplate;
}
// }}}
// {{{ do_orderFields
function do_orderFields() {
$oFieldset =& KTFieldset::get($_REQUEST['fFieldsetId']);
$aFreeFieldIds = $_REQUEST['fFreeFieldIds'];
if (empty($aFreeFieldIds)) {
$this->errorRedirectTo('manageConditional', 'No children fields selected', 'fFieldsetId=' . $oFieldset->getId());
}
$iParentFieldId = $_REQUEST['fParentFieldId'];
if (in_array($aParentFieldId, $aFreeFieldIds)) {
$this->errorRedirectTo('manageConditional', 'Field cannot be its own parent field', 'fFieldsetId=' . $oFieldset->getId());
}
foreach ($aFreeFieldIds as $iChildFieldId) {
$res = KTMetadataUtil::addFieldOrder($iParentFieldId, $iChildFieldId, $oFieldset);
$this->oValidator->notError($res, array(
'redirect_to' => array('manageConditional', 'fFieldsetId=' . $oFieldset->getId()),
'message' => 'Error adding Fields',
));
}
$this->successRedirectTo('manageConditional', 'Fields ordered', 'fFieldsetId=' . $oFieldset->getId());
exit(0);
}
// }}}
// {{{ do_setMasterField
function do_setMasterField() {
$oFieldset =& $this->oValidator->validateFieldset($_REQUEST['fFieldsetId']);
$oField =& $this->oValidator->validateField($_REQUEST['fFieldId']);
$res = KTMetadataUtil::removeFieldOrdering($oFieldset);
$oFieldset->setMasterFieldId($oField->getId());
$res = $oFieldset->update();
$this->oValidator->notError($res, array(
'redirect_to' => array('manageConditional', 'fFieldsetId=' . $oFieldset->getId()),
'message' => 'Error setting master field',
));
$this->successRedirectTo('manageConditional', 'Master field set', 'fFieldsetId=' . $oFieldset->getId());
exit(0);
}
// }}}
// {{{ do_checkComplete
/**
* Checks whether the fieldset is complete, and if it is, sets it to
* be complete in the database. Otherwise, set it to not be
* complete in the database (just in case), and set the error
* messages as to why it isn't.
*/
function do_checkComplete() {
$oFieldset =& $this->oValidator->validateFieldset($_REQUEST['fFieldsetId']);
$res = KTMetadataUtil::checkConditionalFieldsetCompleteness($oFieldset);
if ($res === true) {
$oFieldset->setIsComplete(true);
$oFieldset->update();
$this->successRedirectTo('manageConditional', 'Set to complete', 'fFieldsetId=' . $oFieldset->getId());
}
$oFieldset->setIsComplete(false);
$oFieldset->update();
// Success, as we want to save the incompleteness to the
// database...
$this->successRedirectTo('manageConditional', 'Could not to complete', 'fFieldsetId=' . $oFieldset->getId());
}
// }}}
// {{{ do_changeToSimple
function do_changeToSimple() {
$oFieldset =& $this->oValidator->validateFieldset($_REQUEST['fFieldsetId']);
$oFieldset->setIsComplex(false);
$res = $oFieldset->update();
$this->oValidator->notError($res, array(
'redirect_to' => array('manageConditional', 'fFieldsetId=' . $oFieldset->getId()),
'message' => 'Error changing to simple',
));
$this->successRedirectTo('manageConditional', 'Changed to simple', 'fFieldsetId=' . $oFieldset->getId());
}
// }}}
// {{{ do_changeToComplex
function do_changeToComplex() {
$oFieldset =& $this->oValidator->validateFieldset($_REQUEST['fFieldsetId']);
$oFieldset->setIsComplex(true);
$res = $oFieldset->update();
$this->oValidator->notError($res, array(
'redirect_to' => array('manageConditional', 'fFieldsetId=' . $oFieldset->getId()),
'message' => 'Error changing to complex',
));
$this->successRedirectTo('manageConditional', 'Changed to complex', 'fFieldsetId=' . $oFieldset->getId());
}
// }}}
}
$d =& new KTDocumentFieldDispatcher;
$d->dispatch();
?>