edit.php
6.82 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
<?php
/**
*
* The contents of this file are subject to the KnowledgeTree Public
* License Version 1.1 ("License"); You may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.ktdms.com/KPL
*
* Software distributed under the License is distributed on an "AS IS"
* basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is: KnowledgeTree Open Source
*
* The Initial Developer of the Original Code is The Jam Warehouse Software
* (Pty) Ltd, trading as KnowledgeTree.
* Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
* (C) 2006 The Jam Warehouse Software (Pty) Ltd;
* All Rights Reserved.
*
*/
require_once(KT_LIB_DIR . '/documentmanagement/Document.inc');
require_once(KT_LIB_DIR . '/documentmanagement/DocumentType.inc');
require_once(KT_LIB_DIR . '/foldermanagement/Folder.inc');
require_once(KT_LIB_DIR . '/documentmanagement/DocumentLink.inc');
require_once(KT_LIB_DIR . '/documentmanagement/documentutil.inc.php');
require_once(KT_LIB_DIR . '/documentmanagement/DocumentType.inc');
require_once(KT_LIB_DIR . '/metadata/fieldset.inc.php');
require_once(KT_LIB_DIR . '/documentmanagement/documentutil.inc.php');
require_once(KT_LIB_DIR . '/triggers/triggerregistry.inc.php');
require_once(KT_LIB_DIR . '/permissions/permission.inc.php');
require_once(KT_LIB_DIR . '/permissions/permissionutil.inc.php');
require_once(KT_LIB_DIR . "/widgets/forms.inc.php");
require_once(KT_LIB_DIR . "/metadata/fieldsetregistry.inc.php");
// {{{ KTDocumentEditAction
class KTDocumentEditAction extends KTDocumentAction {
var $sName = 'ktcore.actions.document.edit';
var $_sShowPermission = "ktcore.permissions.write";
var $_bMutator = true;
var $sIconClass = 'edit_metadata';
function getInfo() {
if ($this->oDocument->getIsCheckedOut()) {
return null;
}
return parent::getInfo();
}
function getDisplayName() {
return _kt('Edit Metadata');
}
function form_edit() {
$oForm = new KTForm;
$oForm->setOptions(array(
'label' => _kt('Edit Metadata'),
'submit_label' => _kt('Update Document'),
'action' => 'update',
'fail_action' => 'main',
'cancel_url' => KTBrowseUtil::getUrlForDocument($this->oDocument),
'context' => &$this,
'extraargs' => $this->meldPersistQuery("","",true),
));
$oFReg =& KTFieldsetRegistry::getSingleton();
$doctypeid = $this->oDocument->getDocumentTypeID();
$widgets = array(
array('ktcore.widgets.string', array(
'label' => _kt("Document Title"),
'description' => _kt("The document title is used as the main name of a document throughout KnowledgeTree."),
'name' => 'document_title',
'required' => true,
'value' => $this->oDocument->getName(),
)),
);
$validators = array(
array('ktcore.validators.string',array(
'test' => 'document_title',
'output' => 'document_title',
)),
);
$fieldsets = (array) KTMetadataUtil::fieldsetsForDocument($this->oDocument);
foreach ($fieldsets as $oFieldset) {
$widgets = kt_array_merge($widgets, $oFReg->widgetsForFieldset($oFieldset, 'fieldset_' . $oFieldset->getId(), $this->oDocument));
$validators = kt_array_merge($validators, $oFReg->validatorsForFieldset($oFieldset, 'fieldset_' . $oFieldset->getId(), $this->oDocument));
}
$oForm->setWidgets($widgets);
$oForm->setValidators($validators);
return $oForm;
}
function do_main() {
$this->addErrorMessage("Doctype changing regressed.");
$this->oPage->setBreadcrumbDetails("Edit Metadata");
$oTemplate = $this->oValidator->validateTemplate('ktcore/document/edit');
$oForm = $this->form_edit();
$oTemplate->setData(array(
'context' => $this,
'form' => $oForm,
'document' => $this->oDocument,
));
return $oTemplate->render();
}
function do_update() {
$oForm = $this->form_edit();
$res = $oForm->validate();
if (!empty($res['errors'])) {
return $oForm->handleError();
}
$data = $res['results'];
// we need to format these in MDPack format
// which is a little archaic:
//
// array(
// array($oField, $sValue),
// array($oField, $sValue),
// array($oField, $sValue),
// );
//
// we do this the "easy" way.
$fieldsets = KTMetadataUtil::fieldsetsForDocument($this->oDocument);
$MDPack = array();
foreach ($fieldsets as $oFieldset) {
$fields = $oFieldset->getFields();
$values = (array) KTUtil::arrayGet($data, 'fieldset_' . $oFieldset->getId());
foreach ($fields as $oField) {
$val = KTUtil::arrayGet($values, 'metadata_' . $oField->getId());
// FIXME "null" has strange meanings here.
if (!is_null($val)) {
$MDPack[] = array(
$oField,
$val
);
}
}
}
$this->startTransaction();
$this->oDocument->setName($data['document_title']);
$res = $this->oDocument->update();
if (PEAR::isError($res)) {
$oForm->handleError(sprintf(_kt("Unexpected failure to update document title: %s"), $res->getMessage()));
}
$core_res = KTDocumentUtil::saveMetadata($this->oDocument, $MDPack);
if (PEAR::isError($core_res)) {
$oForm->handleError(sprintf(_kt("Unexpected validation failure: %s."), $core_res->getMessage()));
}
// post-triggers.
$oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
$aTriggers = $oKTTriggerRegistry->getTriggers('edit', 'postValidate');
foreach ($aTriggers as $aTrigger) {
$sTrigger = $aTrigger[0];
$oTrigger = new $sTrigger;
$aInfo = array(
"document" => $this->oDocument,
);
$oTrigger->setInfo($aInfo);
$ret = $oTrigger->postValidate();
}
$this->commitTransaction();
redirect(KTBrowseUtil::getUrlForDocument($this->oDocument->getId()));
exit(0);
}
}
// }}}
?>