Commit b23cac3297a5241d180d8216d39ba1acd62004d8

Authored by megan_w
1 parent d6027be1

KTC-189

"Deleting a fieldset does not remove all instances of itself and existing field and document type associations"
Fixed. On deleting the fieldset it checks for any document type associations and removes them.

KTS-2487
"Deleting an assigned fieldset causes a fatal error"
Fixed. Removing the document type associations fixes the error on viewing the document.

Committed by: Megan Watson
Reviewed by: Jonathan Byrne



git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@7599 c91229c3-7414-0410-bfa2-8a42b809f60b
plugins/ktcore/admin/documentFieldsv2.php
... ... @@ -6,32 +6,32 @@
6 6 * KnowledgeTree Open Source Edition
7 7 * Document Management Made Simple
8 8 * Copyright (C) 2004 - 2007 The Jam Warehouse Software (Pty) Limited
9   - *
  9 + *
10 10 * This program is free software; you can redistribute it and/or modify it under
11 11 * the terms of the GNU General Public License version 3 as published by the
12 12 * Free Software Foundation.
13   - *
  13 + *
14 14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 17 * details.
18   - *
  18 + *
19 19 * You should have received a copy of the GNU General Public License
20 20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21   - *
  21 + *
22 22 * You can contact The Jam Warehouse Software (Pty) Limited, Unit 1, Tramber Place,
23 23 * Blake Street, Observatory, 7925 South Africa. or email info@knowledgetree.com.
24   - *
  24 + *
25 25 * The interactive user interfaces in modified source and object code versions
26 26 * of this program must display Appropriate Legal Notices, as required under
27 27 * Section 5 of the GNU General Public License version 3.
28   - *
  28 + *
29 29 * In accordance with Section 7(b) of the GNU General Public License version 3,
30 30 * these Appropriate Legal Notices must retain the display of the "Powered by
31   - * KnowledgeTree" logo and retain the original copyright notice. If the display of the
  31 + * KnowledgeTree" logo and retain the original copyright notice. If the display of the
32 32 * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
33   - * must display the words "Powered by KnowledgeTree" and retain the original
34   - * copyright notice.
  33 + * must display the words "Powered by KnowledgeTree" and retain the original
  34 + * copyright notice.
35 35 * Contributor( s): ______________________________________
36 36 *
37 37 */
... ... @@ -49,7 +49,7 @@ class KTDocumentFieldDispatcher extends KTAdminDispatcher {
49 49 function predispatch() {
50 50 $this->aBreadcrumbs[] = array('url' => $_SERVER['PHP_SELF'], 'name' => _kt('Document Field Management'));
51 51 $this->persistParams(array('fFieldsetId'));
52   -
  52 +
53 53 $this->oFieldset = KTFieldset::get(KTUtil::arrayGet($_REQUEST, 'fFieldsetId'));
54 54 if (PEAR::isError($this->oFieldset)) {
55 55 $this->oFieldset = null;
... ... @@ -63,7 +63,7 @@ class KTDocumentFieldDispatcher extends KTAdminDispatcher {
63 63  
64 64 function do_main () {
65 65 $oTemplate =& $this->oValidator->validateTemplate('ktcore/metadata/admin/list');
66   -
  66 +
67 67 $oTemplate->setData(array(
68 68 'context' => $this,
69 69 'fieldsets' => KTFieldset::getList('disabled != true'),
... ... @@ -80,10 +80,10 @@ class KTDocumentFieldDispatcher extends KTAdminDispatcher {
80 80 'cancel_action' => 'main',
81 81 'fail_action' => 'newfieldset',
82 82 'action' => 'create',
83   - 'context' => $this,
  83 + 'context' => $this,
84 84 ));
85   -
86   -
  85 +
  86 +
87 87 // construct the widget set.
88 88 // we use a slight variation here, because "type" is only present in certain circumstances.
89 89 $widgets = array(
... ... @@ -98,16 +98,16 @@ class KTDocumentFieldDispatcher extends KTAdminDispatcher {
98 98 'name' => 'description',
99 99 'required' => true,
100 100 'description' => _kt("In order to ensure that the data that users enter is useful, it is essential that you provide a good example."),
101   - )),
  101 + )),
102 102 );
103 103 if ($this->bHaveConditional) {
104   -
  104 +
105 105 // FIXME get this from some external source.
106 106 $type_vocab = array(
107 107 'normal' => _kt("Normal"),
108   - 'conditional' => _kt("Conditional"),
  108 + 'conditional' => _kt("Conditional"),
109 109 );
110   -
  110 +
111 111 $widgets[] = array('ktcore.widgets.selection', array(
112 112 'label' => _kt("Fieldset Type"),
113 113 'use_simple' => false,
... ... @@ -119,20 +119,20 @@ class KTDocumentFieldDispatcher extends KTAdminDispatcher {
119 119 'vocab' => $type_vocab,
120 120 ));
121 121 }
122   -
  122 +
123 123 $widgets[] = array('ktcore.widgets.boolean',array(
124 124 'label' => _kt("Generic"),
125 125 'name' => 'generic',
126 126 'description' => _kt("A generic fieldset is one that is available for every document by default. These fieldsets will be available for users to edit and add for every document in the document management system."),
127 127 ));
128   -
  128 +
129 129 $oForm->setWidgets($widgets);
130   -
  130 +
131 131 // similarly, we construct validators here.
132 132 $validators = array(
133 133 array('ktcore.validators.string', array(
134 134 'test' => 'name',
135   - 'output' => 'name',
  135 + 'output' => 'name',
136 136 )),
137 137 array('ktcore.validators.string', array(
138 138 'test' => 'description',
... ... @@ -143,65 +143,65 @@ class KTDocumentFieldDispatcher extends KTAdminDispatcher {
143 143 'output' => 'generic',
144 144 )),
145 145 );
146   -
  146 +
147 147 if ($this->bHaveConditional) {
148 148 $validators[] = array('ktcore.validators.string', array(
149 149 'test' => 'fieldset_type',
150 150 'output' => 'fieldset_type',
151 151 ));
152 152 }
153   -
  153 +
154 154 $oForm->setValidators($validators);
155   -
  155 +
156 156 return $oForm;
157 157 }
158 158  
159 159 function do_newfieldset() {
160 160 $this->oPage->setBreadcrumbDetails(_kt("Create New Fieldset"));
161 161 $oForm = $this->form_create();
162   -
  162 +
163 163 return $oForm->render();
164 164 }
165   -
  165 +
166 166 function do_create() {
167 167 $oForm = $this->form_create();
168 168 $res = $oForm->validate();
169   -
  169 +
170 170 $data = $res['results'];
171 171 $errors = $res['errors'];
172 172 $extra_errors = array();
173   -
  173 +
174 174 if (!empty($data['name'])) {
175 175 $oFieldset = KTFieldset::getByName($data['name']);
176 176 if (!PEAR::isError($oFieldset)) {
177 177 // means we're looking at an existing name
178 178 $extra_errors['name'] = _kt("There is already a fieldset with that name.");
179   - }
  179 + }
180 180 }
181   -
  181 +
182 182 $is_conditional = false;
183 183 // FIXME this is inelegant. get it from somewhere else.
184 184 if ($this->bHaveConditional && ($data['fieldset_type'] == 'conditional')) {
185 185 $is_conditional = true;
186 186 }
187   -
188   -
  187 +
  188 +
189 189 if (!empty($errors) || !empty($extra_errors)) {
190 190 return $oForm->handleError(null, $extra_errors);
191 191 }
192   -
  192 +
193 193 // we also need a namespace.
194 194 $temp_name = $data['name'];
195 195 $namespace = KTUtil::nameToLocalNamespace('fieldsets', $temp_name);
196 196 $oOldFieldset = KTFieldset::getByNamespace($namespace);
197   -
  197 +
198 198 while (!PEAR::isError($oOldFieldset)) {
199 199 $temp_name .= '_';
200 200 $namespace = KTUtil::nameToLocalNamespace('fieldsets', $temp_name);
201   - $oOldFieldset = KTFieldset::getByNamespace($namespace);
  201 + $oOldFieldset = KTFieldset::getByNamespace($namespace);
202 202 }
203   -
204   - // we now know its a non-conflicting one.
  203 +
  204 + // we now know its a non-conflicting one.
205 205 // FIXME handle conditional fieldsets, which should be ... a different object.
206 206 $oFieldset = KTFieldset::createFromArray(array(
207 207 "name" => $data['name'],
... ... @@ -212,29 +212,29 @@ class KTDocumentFieldDispatcher extends KTAdminDispatcher {
212 212 "isGeneric" => $data['generic'],
213 213 "isComplete" => false,
214 214 "isComplex" => false,
215   - "isSystem" => false,
  215 + "isSystem" => false,
216 216 ));
217 217 if (PEAR::isError($oFieldset)) {
218 218 return $oForm->handleError(sprintf(_kt("Failed to create fieldset: %s"), $oFieldset->getMessage()));
219 219 }
220   -
  220 +
221 221 $this->successRedirectTo('edit',_kt("Fieldset created."), sprintf('fFieldsetId=%d', $oFieldset->getId()));
222   - }
  222 + }
223 223  
224 224 function getTypesForFieldset($oFieldset) {
225 225 if ($oFieldset->getIsGeneric()) {
226 226 return _kt('All types use this generic fieldset.');
227 227 }
228   -
  228 +
229 229 $types = $oFieldset->getAssociatedTypes();
230 230 if (PEAR::isError($types)) {
231 231 return _kt('Error retrieving list of types.');
232 232 }
233   - if (empty($types)) {
  233 + if (empty($types)) {
234 234 return _kt('None');
235 235 }
236 236 $aNames = array();
237   - foreach ($types as $oType) {
  237 + foreach ($types as $oType) {
238 238 if (!PEAR::isError($oType)) {
239 239 $aNames[] = $oType->getName();
240 240 }
... ... @@ -247,30 +247,30 @@ class KTDocumentFieldDispatcher extends KTAdminDispatcher {
247 247 // here we engage in some major evil.
248 248 // we check for the subevent var
249 249 // and subdispatch if appropriate.
250   - //
  250 + //
251 251 // saves a little code-duplication (actually, a lot of code-duplication)
252   -
  252 +
253 253 // FIXME this is essentially a stub for the fieldset-delegation code.
254 254 if ($this->oFieldset->getIsConditional()) {
255 255 require_once('fieldsets/conditional.inc.php');
256   - $oSubDispatcher = new ConditionalFieldsetManagementDispatcher;
  256 + $oSubDispatcher = new ConditionalFieldsetManagementDispatcher;
257 257 } else {
258 258 require_once('fieldsets/basic.inc.php');
259 259 $oSubDispatcher = new BasicFieldsetManagementDispatcher;
260 260 }
261   -
  261 +
262 262 $subevent_var = 'fieldset_action';
263 263 $subevent = KTUtil::arrayGet($_REQUEST, $subevent_var);
264 264 if (!empty($subevent)) {
265 265 // do nothing, since this will handle everything
266 266 $this_url = KTUtil::addQueryStringSelf($this->meldPersistQuery("","edit"));
267   - $oSubDispatcher->redispatch($subevent_var, null, $this, $this_url);
  267 + $oSubDispatcher->redispatch($subevent_var, null, $this, $this_url);
268 268 exit(0);
269 269 } else {
270 270 // what we want is the "additional info" section
271 271 $additional = $oSubDispatcher->describe_fieldset($this->oFieldset);
272 272 }
273   -
  273 +
274 274 $oTemplate =& $this->oValidator->validateTemplate('ktcore/metadata/admin/edit');
275 275 $oTemplate->setData(array(
276 276 'context' => $this,
... ... @@ -279,9 +279,19 @@ class KTDocumentFieldDispatcher extends KTAdminDispatcher {
279 279 ));
280 280 return $oTemplate->render();
281 281 }
282   -
  282 +
283 283 function do_delete() {
284 284 $this->startTransaction();
  285 +
  286 + // check if fieldset is associated with a document type - remove association
  287 + $types = $this->oFieldset->getAssociatedTypes();
  288 + $sFieldSetId = $this->oFieldset->getId();
  289 + if(!PEAR::isError($types) AND !empty($types)){
  290 + foreach($types as $oType){
  291 + $res = KTMetadataUtil::removeSetsFromDocumentType($oType, $sFieldSetId);
  292 + }
  293 + }
  294 +
285 295 $res = $this->oFieldset->delete('true');
286 296 $this->oValidator->notErrorFalse($res, array(
287 297 'redirect_to' => array('main', ''),
... ... @@ -289,7 +299,7 @@ class KTDocumentFieldDispatcher extends KTAdminDispatcher {
289 299 ));
290 300 $this->successRedirectToMain(_kt('Fieldset deleted'));
291 301 }
292   -
  302 +
293 303 function form_edit() {
294 304 $oForm = new KTForm;
295 305 $oForm->setOptions(array(
... ... @@ -299,10 +309,10 @@ class KTDocumentFieldDispatcher extends KTAdminDispatcher {
299 309 'cancel_action' => 'edit',
300 310 'fail_action' => 'editfieldset',
301 311 'action' => 'savefieldset',
302   - 'context' => $this,
  312 + 'context' => $this,
303 313 ));
304   -
305   -
  314 +
  315 +
306 316 // construct the widget set.
307 317 // we use a slight variation here, because "type" is only present in certain circumstances.
308 318 $widgets = array(
... ... @@ -318,24 +328,24 @@ class KTDocumentFieldDispatcher extends KTAdminDispatcher {
318 328 'name' => 'description',
319 329 'required' => true,
320 330 'description' => _kt("In order to ensure that the data that users enter is useful, it is essential that you provide a good example."),
321   - 'value' => sanitizeForHTML($this->oFieldset->getDescription()),
322   - )),
  331 + 'value' => sanitizeForHTML($this->oFieldset->getDescription()),
  332 + )),
323 333 );
324   -
  334 +
325 335 $widgets[] = array('ktcore.widgets.boolean',array(
326 336 'label' => _kt("Generic"),
327 337 'name' => 'generic',
328 338 'description' => _kt("A generic fieldset is one that is available for every document by default. These fieldsets will be available for users to edit and add for every document in the document management system."),
329   - 'value' => $this->oFieldset->getIsGeneric(),
  339 + 'value' => $this->oFieldset->getIsGeneric(),
330 340 ));
331   -
  341 +
332 342 $oForm->setWidgets($widgets);
333   -
  343 +
334 344 // similarly, we construct validators here.
335 345 $validators = array(
336 346 array('ktcore.validators.string', array(
337 347 'test' => 'name',
338   - 'output' => 'name',
  348 + 'output' => 'name',
339 349 )),
340 350 array('ktcore.validators.string', array(
341 351 'test' => 'description',
... ... @@ -346,22 +356,22 @@ class KTDocumentFieldDispatcher extends KTAdminDispatcher {
346 356 'output' => 'generic',
347 357 )),
348 358 );
349   -
  359 +
350 360 $oForm->setValidators($validators);
351   -
  361 +
352 362 return $oForm;
353 363 }
354   -
355   - function do_editfieldset() {
  364 +
  365 + function do_editfieldset() {
356 366 $oForm = $this->form_edit();
357 367 $this->oPage->setBreadcrumbDetails(_kt('edit fieldset'));
358 368 return $oForm->renderPage(_kt("Edit Fieldset"));
359 369 }
360   -
361   - function do_savefieldset() {
  370 +
  371 + function do_savefieldset() {
362 372 $oForm = $this->form_edit();
363 373 $res = $oForm->validate();
364   -
  374 +
365 375 $data = $res['results'];
366 376 $errors = $res['errors'];
367 377 $extra_errors = array();
... ... @@ -371,18 +381,18 @@ class KTDocumentFieldDispatcher extends KTAdminDispatcher {
371 381 $extra_errors['name'][] = _kt("A fieldset with that name already exists.");
372 382 }
373 383 }
374   -
  384 +
375 385 if (!empty($errors) || !empty($extra_errors)) {
376 386 return $oForm->handleError(null, $extra_errors);
377 387 }
378   -
  388 +
379 389 $this->startTransaction();
380   -
  390 +
381 391 $this->oFieldset->setName($data['name']);
382   - $this->oFieldset->setDescription($data['description']);
  392 + $this->oFieldset->setDescription($data['description']);
383 393 $bGeneric = $data['generic'];
384 394 if ($bGeneric != $this->oFieldset->getIsGeneric() && $bGeneric == true) {
385   - // delink it from all doctypes.
  395 + // delink it from all doctypes.
386 396 $aTypes = $this->oFieldset->getAssociatedTypes();
387 397 foreach ($aTypes as $oType) {
388 398 $res = KTMetadataUtil::removeSetsFromDocumentType($oType, $this->oFieldset->getId());
... ... @@ -392,17 +402,17 @@ class KTDocumentFieldDispatcher extends KTAdminDispatcher {
392 402 }
393 403 }
394 404 }
395   -
396   - $this->oFieldset->setIsGeneric($data['generic']);
397   -
  405 +
  406 + $this->oFieldset->setIsGeneric($data['generic']);
  407 +
398 408 $res = $this->oFieldset->update();
399 409 if (PEAR::isError($res)) {
400 410 $this->errorRedirectTo('edit', _kt('Could not save fieldset changes'));
401 411 exit(0);
402 412 }
403   -
  413 +
404 414 return $this->successRedirectTo('edit', _kt("Fieldset details updated."));
405   - }
  415 + }
406 416 }
407 417  
408 418 ?>
... ...