Commit 288e08adde68f552e661c75d888a0dc0d2d29023
1 parent
a91f2b9f
Move trigger for workflows.
git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@5755 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
2 changed files
with
204 additions
and
0 deletions
plugins/ktcore/KTCorePlugin.php
| ... | ... | @@ -120,6 +120,8 @@ class KTCorePlugin extends KTPlugin { |
| 120 | 120 | $this->registerWorkflowTrigger('ktcore.workflowtriggers.groupguard', 'GroupGuardTrigger', 'KTWorkflowTriggers.inc.php'); |
| 121 | 121 | $this->registerWorkflowTrigger('ktcore.workflowtriggers.conditionguard', 'ConditionGuardTrigger', 'KTWorkflowTriggers.inc.php'); |
| 122 | 122 | |
| 123 | + $this->registerWorkflowTrigger('ktcore.workflowtriggers.copyaction', 'CopyActionTrigger', 'KTWorkflowTriggers.inc.php'); | |
| 124 | + | |
| 123 | 125 | |
| 124 | 126 | $this->setupAdmin(); |
| 125 | 127 | } | ... | ... |
plugins/ktcore/KTWorkflowTriggers.inc.php
| ... | ... | @@ -417,4 +417,206 @@ class ConditionGuardTrigger extends KTWorkflowTrigger { |
| 417 | 417 | } |
| 418 | 418 | } |
| 419 | 419 | |
| 420 | + | |
| 421 | +class CopyActionTrigger extends KTWorkflowTrigger { | |
| 422 | + var $sNamespace = 'ktcore.workflowtriggers.copyaction'; | |
| 423 | + var $sFriendlyName; | |
| 424 | + var $sDescription; | |
| 425 | + var $oTriggerInstance; | |
| 426 | + var $aConfig = array(); | |
| 427 | + | |
| 428 | + // generic requirements - both can be true | |
| 429 | + var $bIsGuard = false; | |
| 430 | + var $bIsAction = true; | |
| 431 | + | |
| 432 | + function CopyActionTrigger() { | |
| 433 | + $this->sFriendlyName = _kt("Moves Document"); | |
| 434 | + $this->sDescription = _kt("Moves the document to another folder."); | |
| 435 | + } | |
| 436 | + | |
| 437 | + // perform more expensive checks -before- performTransition. | |
| 438 | + function precheckTransition($oDocument, $oUser) { | |
| 439 | + $iFolderId = KTUtil::arrayGet($this->aConfig, 'folder_id'); | |
| 440 | + $oFolder = Folder::get($iFolderId); | |
| 441 | + if (PEAR::isError($oFolder)) { | |
| 442 | + return PEAR::raiseError(_kt('The folder to which this document should be moved does not exist. Cancelling the transition - please contact a system administrator.')); | |
| 443 | + } | |
| 444 | + | |
| 445 | + return true; | |
| 446 | + } | |
| 447 | + | |
| 448 | + function performTransition($oDocument, $oUser) { | |
| 449 | + $iFolderId = KTUtil::arrayGet($this->aConfig, 'folder_id'); | |
| 450 | + $oFolder = Folder::get($iFolderId); | |
| 451 | + if (PEAR::isError($oFolder)) { | |
| 452 | + return PEAR::raiseError(_kt('The folder to which this document should be moved does not exist. Cancelling the transition - please contact a system administrator.')); | |
| 453 | + } | |
| 454 | + | |
| 455 | + // FIXME refactor into documentutil. | |
| 456 | + | |
| 457 | + $oOriginalFolder = Folder::get($oDocument->getFolderId()); | |
| 458 | + $iOriginalFolderPermissionObjectId = $oOriginalFolder->getPermissionObjectId(); | |
| 459 | + $iDocumentPermissionObjectId = $oDocument->getPermissionObjectId(); | |
| 460 | + | |
| 461 | + | |
| 462 | + if ($iDocumentPermissionObjectId === $iOriginalFolderPermissionObjectId) { | |
| 463 | + $oDocument->setPermissionObjectId($oFolder->getPermissionObjectId()); | |
| 464 | + } | |
| 465 | + | |
| 466 | + //put the document in the new folder | |
| 467 | + $oDocument->setFolderID($oFolder->getId()); | |
| 468 | + if (!$oDocument->update(true)) { | |
| 469 | + $this->errorRedirectTo("main", _kt("There was a problem updating the document's location in the database"), sprintf("fDocumentId=%d&fFolderId=%d", $this->oDocument->getId(), $this->oFolder->getId())); | |
| 470 | + } | |
| 471 | + | |
| 472 | + | |
| 473 | + //move the document on the file system | |
| 474 | + $oStorage =& KTStorageManagerUtil::getSingleton(); | |
| 475 | + if (!$oStorage->moveDocument($oDocument, $oFolder, $oOriginalFolder)) { | |
| 476 | + $oDocument->setFolderID($oFolder->getId()); | |
| 477 | + $res = $oDocument->update(true); | |
| 478 | + if (PEAR::isError($res)) { | |
| 479 | + return $res; | |
| 480 | + } | |
| 481 | + } | |
| 482 | + | |
| 483 | + $sMoveMessage = sprintf("Moved from %s/%s to %s/%s: Workflow trigger.", | |
| 484 | + $oOriginalFolder->getFullPath(), | |
| 485 | + $oOriginalFolder->getName(), | |
| 486 | + $oFolder->getFullPath(), | |
| 487 | + $oFolder->getName()); | |
| 488 | + | |
| 489 | + // create the document transaction record | |
| 490 | + | |
| 491 | + $oDocumentTransaction = & new DocumentTransaction($oDocument, $sMoveMessage, 'ktcore.transactions.move'); | |
| 492 | + $oDocumentTransaction->create(); | |
| 493 | + | |
| 494 | + | |
| 495 | + $oKTTriggerRegistry = KTTriggerRegistry::getSingleton(); | |
| 496 | + $aTriggers = $oKTTriggerRegistry->getTriggers('moveDocument', 'postValidate'); | |
| 497 | + foreach ($aTriggers as $aTrigger) { | |
| 498 | + $sTrigger = $aTrigger[0]; | |
| 499 | + $oTrigger = new $sTrigger; | |
| 500 | + $aInfo = array( | |
| 501 | + "document" => $oDocument, | |
| 502 | + "old_folder" => $oOriginalFolder, | |
| 503 | + "new_folder" => $oFolder, | |
| 504 | + ); | |
| 505 | + $oTrigger->setInfo($aInfo); | |
| 506 | + $ret = $oTrigger->postValidate(); | |
| 507 | + if (PEAR::isError($ret)) { | |
| 508 | + return $ret; | |
| 509 | + } | |
| 510 | + } | |
| 511 | + | |
| 512 | + return KTPermissionUtil::updatePermissionLookup($oDocument); | |
| 513 | + } | |
| 514 | + | |
| 515 | + function displayConfiguration($args) { | |
| 516 | + $oTemplating =& KTTemplating::getSingleton(); | |
| 517 | + $oTemplate = $oTemplating->loadTemplate("ktcore/workflowtriggers/moveaction"); | |
| 518 | + | |
| 519 | + require_once(KT_LIB_DIR . "/browse/DocumentCollection.inc.php"); | |
| 520 | + require_once(KT_LIB_DIR . "/browse/columnregistry.inc.php"); | |
| 521 | + | |
| 522 | + $collection = new AdvancedCollection; | |
| 523 | + $oColumnRegistry = KTColumnRegistry::getSingleton(); | |
| 524 | + $aColumns = array(); | |
| 525 | + $aColumns[] = $oColumnRegistry->getColumn('ktcore.columns.singleselection'); | |
| 526 | + $aColumns[] = $oColumnRegistry->getColumn('ktcore.columns.title'); | |
| 527 | + | |
| 528 | + $collection->addColumns($aColumns); | |
| 529 | + | |
| 530 | + $aOptions = $collection->getEnvironOptions(); // extract data from the environment | |
| 531 | + | |
| 532 | + | |
| 533 | + $qsFrag = array(); | |
| 534 | + foreach ($args as $k => $v) { | |
| 535 | + if ($k == 'action') { $v = 'editTrigger'; } // horrible hack - we really need iframe embedding. | |
| 536 | + $qsFrag[] = sprintf("%s=%s",urlencode($k), urlencode($v)); | |
| 537 | + } | |
| 538 | + $qs = implode('&',$qsFrag); | |
| 539 | + $aOptions['result_url'] = KTUtil::addQueryStringSelf($qs); | |
| 540 | + $aOptions['show_documents'] = false; | |
| 541 | + | |
| 542 | + $fFolderId = KTUtil::arrayGet($_REQUEST, 'fFolderId', KTUtil::arrayGet($this->aConfig, 'folder_id', 1)); | |
| 543 | + | |
| 544 | + $collection->setOptions($aOptions); | |
| 545 | + $collection->setQueryObject(new BrowseQuery($fFolderId, $this->oUser)); | |
| 546 | + $collection->setColumnOptions('ktcore.columns.singleselection', array( | |
| 547 | + 'rangename' => 'folder_id', | |
| 548 | + 'show_folders' => true, | |
| 549 | + 'show_documents' => false, | |
| 550 | + )); | |
| 551 | + | |
| 552 | + $collection->setColumnOptions('ktcore.columns.title', array( | |
| 553 | + 'direct_folder' => false, | |
| 554 | + 'folder_link' => $aOptions['result_url'], | |
| 555 | + )); | |
| 556 | + | |
| 557 | + $oFolder = Folder::get($fFolderId); | |
| 558 | + $aBreadcrumbs = array(); | |
| 559 | + $folder_path_names = $oFolder->getPathArray(); | |
| 560 | + $folder_path_ids = explode(',', $oFolder->getParentFolderIds()); | |
| 561 | + $folder_path_ids[] = $oFolder->getId(); | |
| 562 | + if ($folder_path_ids[0] == 0) { | |
| 563 | + array_shift($folder_path_ids); | |
| 564 | + array_shift($folder_path_names); | |
| 565 | + } | |
| 566 | + | |
| 567 | + foreach (range(0, count($folder_path_ids) - 1) as $index) { | |
| 568 | + $id = $folder_path_ids[$index]; | |
| 569 | + $qsFrag2 = $qsFrag; | |
| 570 | + $qsFrag2[] = sprintf('fFolderId=%d', $id); | |
| 571 | + $qs2 = implode('&',$qsFrag2); | |
| 572 | + $url = KTUtil::addQueryStringSelf($qs2); | |
| 573 | + $aBreadcrumbs[] = sprintf("<a href=\"%s\">%s</a>", $url, htmlentities($folder_path_names[$index], ENT_NOQUOTES, 'UTF-8')); | |
| 574 | + } | |
| 575 | + | |
| 576 | + $sBreadcrumbs = implode(' » ', $aBreadcrumbs); | |
| 577 | + | |
| 578 | + $aTemplateData = array( | |
| 579 | + "context" => $this, | |
| 580 | + 'breadcrumbs' => $sBreadcrumbs, | |
| 581 | + 'collection' => $collection, | |
| 582 | + 'args' => $args, | |
| 583 | + ); | |
| 584 | + return $oTemplate->render($aTemplateData); | |
| 585 | + } | |
| 586 | + | |
| 587 | + function saveConfiguration() { | |
| 588 | + $folder_id = KTUtil::arrayGet($_REQUEST, 'folder_id', null); | |
| 589 | + $oFolder = Folder::get($folder_id); | |
| 590 | + if (PEAR::isError($oFolder)) { | |
| 591 | + // silenty ignore | |
| 592 | + $folder_id = null; | |
| 593 | + } | |
| 594 | + | |
| 595 | + $config = array(); | |
| 596 | + $config['folder_id'] = $folder_id; | |
| 597 | + | |
| 598 | + $this->oTriggerInstance->setConfig($config); | |
| 599 | + $res = $this->oTriggerInstance->update(); | |
| 600 | + | |
| 601 | + return $res; | |
| 602 | + } | |
| 603 | + | |
| 604 | + function getConfigDescription() { | |
| 605 | + if (!$this->isLoaded()) { | |
| 606 | + return _kt('This trigger has no configuration.'); | |
| 607 | + } | |
| 608 | + // the actual permissions are stored in the array. | |
| 609 | + $perms = array(); | |
| 610 | + if (empty($this->aConfig) || is_null($this->aConfig['folder_id'])) { | |
| 611 | + return _kt('<strong>This transition cannot be performed: no folder has been selected.</strong>'); | |
| 612 | + } | |
| 613 | + $oFolder = Folder::get($this->aConfig['folder_id']); | |
| 614 | + if (PEAR::isError($oFolder)) { | |
| 615 | + return _kt('<strong>The folder required for this trigger has been deleted, so the transition cannot be performed.</strong>'); | |
| 616 | + } else { | |
| 617 | + return sprintf(_kt("The document will be moved to folder \"<a href=\"%s\">%s</a>\"."), KTBrowseUtil::getUrlForFolder($oFolder), htmlentities($oFolder->getName(), ENT_NOQUOTES, 'UTF-8')); | |
| 618 | + } | |
| 619 | + } | |
| 620 | +} | |
| 621 | + | |
| 420 | 622 | ?> |
| 421 | 623 | \ No newline at end of file | ... | ... |