Commit 266868fee986f78a726feef99330b965866e1028

Authored by Megan Watson
1 parent db925a1e

KTS-2391

"When changing doc type the old associated fieldsets should be removed"
Fixed. Incremented the metadata version and copied the metadata for the fieldsets that remain with the new doc type.

Committed by: Megan Watson
Reviewed by: Conrad Vermeulen



git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@8047 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing 1 changed file with 54 additions and 46 deletions
plugins/ktcore/document/edit.php
@@ -294,68 +294,76 @@ class KTDocumentEditAction extends KTDocumentAction { @@ -294,68 +294,76 @@ class KTDocumentEditAction extends KTDocumentAction {
294 } 294 }
295 295
296 $document_type = $data['type']; 296 $document_type = $data['type'];
  297 + $doctypeid = $document_type->getId();
297 298
  299 + // Get the current document type, fieldsets and metadata
  300 + $iOldDocTypeID = $this->oDocument->getDocumentTypeID();
  301 + $fieldsets = KTMetadataUtil::fieldsetsForDocument($this->oDocument, $iOldDocTypeID);
  302 + $mdlist = DocumentFieldLink::getByDocument($this->oDocument);
298 303
299 - $doctypeid = $document_type->getId();  
300 -  
301 -  
302 - DBUtil::startTransaction();  
303 - $this->oDocument->setDocumentTypeId($doctypeid);  
304 - $res = $this->oDocument->update(); 304 + $field_values = array();
  305 + foreach ($mdlist as $oFieldLink) {
  306 + $field_values[$oFieldLink->getDocumentFieldID()] = $oFieldLink->getValue();
  307 + }
305 308
  309 + DBUtil::startTransaction();
306 310
307 - if (PEAR::isError($res))  
308 - {  
309 - DBUtil::rollback();  
310 - return $res;  
311 - }  
312 - DBUtil::commit(); 311 + // Update the document with the new document type id
  312 + $this->oDocument->startNewMetadataVersion($this->oUser);
  313 + $this->oDocument->setDocumentTypeId($doctypeid);
  314 + $res = $this->oDocument->update();
313 315
314 - $fieldsets = KTMetadataUtil::fieldsetsForDocument($this->oDocument, $doctypeid); 316 + if (PEAR::isError($res))
  317 + {
  318 + DBUtil::rollback();
  319 + return $res;
  320 + }
315 321
316 - $fs_ids = array(); 322 + // Ensure all values for fieldsets common to both document types are retained
  323 + $fs_ids = array();
317 324
318 - $doctype_fieldsets = KTFieldSet::getForDocumentType($doctypeid);  
319 - foreach($doctype_fieldsets as $fieldset)  
320 - {  
321 - $fs_ids[] = $fieldset->getId();  
322 - }  
323 - $MDPack = array();  
324 - foreach ($fieldsets as $oFieldset)  
325 - {  
326 - if ($oFieldset->getIsGeneric() || in_array($oFieldset->getId(),$fs_ids))  
327 - {  
328 - //print $oFieldset->getName() . "<br>";  
329 - $fields = $oFieldset->getFields();  
330 - $values = (array) KTUtil::arrayGet($data, 'fieldset_' . $oFieldset->getId());  
331 - foreach ($fields as $oField)  
332 - {  
333 - $val = KTUtil::arrayGet($values, 'metadata_' . $oField->getId()); 325 + $doctype_fieldsets = KTFieldSet::getForDocumentType($doctypeid);
  326 + foreach($doctype_fieldsets as $fieldset)
  327 + {
  328 + $fs_ids[] = $fieldset->getId();
  329 + }
334 330
335 - // FIXME "null" has strange meanings here.  
336 - if (!is_null($val))  
337 - {  
338 - $MDPack[] = array(  
339 - $oField,  
340 - $val  
341 - );  
342 - }  
343 - }  
344 - } 331 + $MDPack = array();
  332 + foreach ($fieldsets as $oFieldset)
  333 + {
  334 + if ($oFieldset->getIsGeneric() || in_array($oFieldset->getId(), $fs_ids))
  335 + {
  336 + $fields = $oFieldset->getFields();
  337 +
  338 + foreach ($fields as $oField)
  339 + {
  340 + $val = isset($field_values[$oField->getId()]) ? $field_values[$oField->getId()] : '';
  341 +
  342 + if (!empty($val))
  343 + {
  344 + $MDPack[] = array($oField, $val);
  345 + }
  346 + }
  347 + }
  348 + }
345 349
346 - } 350 + $core_res = KTDocumentUtil::saveMetadata($this->oDocument, $MDPack, array('novalidate' => true));
347 351
348 - $core_res = KTDocumentUtil::saveMetadata($this->oDocument, $MDPack); 352 + if (PEAR::isError($core_res)) {
  353 + DBUtil::rollback();
  354 + return $core_res;
  355 + }
  356 + DBUtil::commit();
349 357
350 - $oKTTriggerRegistry = KTTriggerRegistry::getSingleton(); 358 + $oKTTriggerRegistry = KTTriggerRegistry::getSingleton();
351 $aTriggers = $oKTTriggerRegistry->getTriggers('edit', 'postValidate'); 359 $aTriggers = $oKTTriggerRegistry->getTriggers('edit', 'postValidate');
352 360
353 foreach ($aTriggers as $aTrigger) { 361 foreach ($aTriggers as $aTrigger) {
354 $sTrigger = $aTrigger[0]; 362 $sTrigger = $aTrigger[0];
355 $oTrigger = new $sTrigger; 363 $oTrigger = new $sTrigger;
356 $aInfo = array( 364 $aInfo = array(
357 - "document" => $this->oDocument,  
358 - "aOptions" => $MDPack, 365 + "document" => $this->oDocument,
  366 + "aOptions" => $MDPack,
359 ); 367 );
360 $oTrigger->setInfo($aInfo); 368 $oTrigger->setInfo($aInfo);
361 $ret = $oTrigger->postValidate(); 369 $ret = $oTrigger->postValidate();
@@ -366,4 +374,4 @@ class KTDocumentEditAction extends KTDocumentAction { @@ -366,4 +374,4 @@ class KTDocumentEditAction extends KTDocumentAction {
366 } 374 }
367 // }}} 375 // }}}
368 376
369 -?> 377 -?>
  378 +?>
370 \ No newline at end of file 379 \ No newline at end of file