Commit b6799ee3b9d496bf4936127441f285de1c84db12

Authored by conradverm
1 parent e78f5e61

KTS-2358

"php5 migration"
Updated. Removed & from &new. Added statics.

Committed By: Conrad Vermeulen
Reviewed By: Kevin Fourie

git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@7196 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/workflow/workflowutil.inc.php
... ... @@ -6,7 +6,7 @@
6 6 * License Version 1.1.2 ("License"); You may not use this file except in
7 7 * compliance with the License. You may obtain a copy of the License at
8 8 * http://www.knowledgetree.com/KPL
9   - *
  9 + *
10 10 * Software distributed under the License is distributed on an "AS IS"
11 11 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
12 12 * See the License for the specific language governing rights and
... ... @@ -17,9 +17,9 @@
17 17 * (ii) the KnowledgeTree copyright notice
18 18 * in the same form as they appear in the distribution. See the License for
19 19 * requirements.
20   - *
  20 + *
21 21 * The Original Code is: KnowledgeTree Open Source
22   - *
  22 + *
23 23 * The Initial Developer of the Original Code is The Jam Warehouse Software
24 24 * (Pty) Ltd, trading as KnowledgeTree.
25 25 * Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
... ... @@ -123,11 +123,11 @@ class KTWorkflowUtil {
123 123 /* WILL NOT RESET THE WORKFLOW if changing to the -same- workflow */
124 124 function changeWorkflowOnDocument($oWorkflow, $oDocument) {
125 125 $oDocument =& KTUtil::getObject('Document', $oDocument);
126   -
127   -
  126 +
  127 +
128 128 // fix for 1049: workflows reset on document move.
129 129 // this was the original purpose of "changeWorkflowOnDocument".
130   - if (is_null($oWorkflow)) {
  130 + if (is_null($oWorkflow)) {
131 131 if ($oDocument->getWorkflowId() == null) {
132 132 return true; // no definition.
133 133 }
... ... @@ -135,8 +135,8 @@ class KTWorkflowUtil {
135 135 if ($oDocument->getWorkflowId() == $oWorkflow->getId()) {
136 136 return true; // bail out, essentially.
137 137 }
138   - }
139   -
  138 + }
  139 +
140 140 return KTWorkflowUtil::startWorkflowOnDocument($oWorkflow, $oDocument);
141 141 }
142 142 // {{{ startWorkflowOnDocument
... ... @@ -169,7 +169,7 @@ class KTWorkflowUtil {
169 169 $oDocument->setWorkflowId($iWorkflowId);
170 170 $oDocument->setWorkflowStateId($iStartStateId);
171 171 $sTransactionComments = sprintf(_kt("Workflow \"%s\" started."), $oWorkflow->getHumanName());
172   -
  172 +
173 173 } else {
174 174 $oDocument->setWorkflowId(null);
175 175 $oDocument->setWorkflowStateId(null);
... ... @@ -180,20 +180,20 @@ class KTWorkflowUtil {
180 180 if (PEAR::isError($res)) { return $res; }
181 181  
182 182 // create the document transaction record
183   - $oDocumentTransaction = & new DocumentTransaction($oDocument, $sTransactionComments, 'ktcore.transactions.workflow_state_transition');
  183 + $oDocumentTransaction = new DocumentTransaction($oDocument, $sTransactionComments, 'ktcore.transactions.workflow_state_transition');
184 184 $oDocumentTransaction->create();
185 185  
186 186  
187 187 // FIXME does this function as expected?
188   -
  188 +
189 189 KTPermissionUtil::updatePermissionLookup($oDocument);
190   -
  190 +
191 191 if (isset($iStartStateId)) {
192 192 $oTargetState = KTWorkflowState::get($iStartStateId);
193   - KTWorkflowUtil::informUsersForState($oTargetState,
  193 + KTWorkflowUtil::informUsersForState($oTargetState,
194 194 KTWorkflowUtil::getInformedForState($oTargetState), $oDocument, $oUser, '');
195 195 }
196   -
  196 +
197 197 return $res;
198 198 }
199 199 // }}}
... ... @@ -287,7 +287,7 @@ class KTWorkflowUtil {
287 287 }
288 288  
289 289 if(!is_array($aActions)) return;
290   -
  290 +
291 291 $aOptions = array('noid' => true);
292 292 foreach ($aActions as $sAction) {
293 293 $res = DBUtil::autoInsert($sTable, array(
... ... @@ -316,7 +316,7 @@ class KTWorkflowUtil {
316 316 }
317 317  
318 318 if(!is_array($aActions)) return;
319   -
  319 +
320 320 $aOptions = array('noid' => true);
321 321 foreach ($aActions as $sAction) {
322 322 $res = DBUtil::autoInsert($sTable, array(
... ... @@ -328,7 +328,7 @@ class KTWorkflowUtil {
328 328 }
329 329 }
330 330 return;
331   - }
  331 + }
332 332  
333 333 // {{{ getEnabledActionsForState
334 334 /**
... ... @@ -405,7 +405,7 @@ class KTWorkflowUtil {
405 405 */
406 406 function getWorkflowForDocument ($oDocument, $aOptions = null) {
407 407 $ids = KTUtil::arrayGet($aOptions, 'ids', false);
408   -
  408 +
409 409 if (is_a($oDocument, 'KTDocumentCore')) {
410 410 $oDocument = $oDocument->getId();
411 411 }
... ... @@ -435,7 +435,7 @@ class KTWorkflowUtil {
435 435 * returning null if there is no workflow assigned.
436 436 */
437 437 function getWorkflowStateForDocument ($oDocument, $aOptions = null) {
438   -
  438 +
439 439 $ids = KTUtil::arrayGet($aOptions, 'ids', false);
440 440  
441 441 if (is_a($oDocument, 'KTDocumentCore')) {
... ... @@ -478,9 +478,9 @@ class KTWorkflowUtil {
478 478 $aTransitions = KTWorkflowUtil::getTransitionsFrom($oState);
479 479 $aEnabledTransitions = array();
480 480 foreach ($aTransitions as $oTransition) {
481   -
  481 +
482 482 // keeping this around to make coding the replacements easier.
483   -
  483 +
484 484 /*
485 485 $iPermissionId = $oTransition->getGuardPermissionId();
486 486 if ($iPermissionId) {
... ... @@ -500,11 +500,11 @@ class KTWorkflowUtil {
500 500 $iRoleId = $oTransition->getGuardRoleId();
501 501 if ($iRoleId) {
502 502 $oRoleAllocation = RoleAllocation::getAllocationsForFolderAndRole($oDocument->getFolderID(), $iRoleId);
503   -
  503 +
504 504 if ($oRoleAllocation == null) { // no role allocation, no fulfillment.
505 505 continue;
506 506 }
507   -
  507 +
508 508 if (!$oRoleAllocation->hasMember($oUser)) {
509 509 continue;
510 510 }
... ... @@ -517,7 +517,7 @@ class KTWorkflowUtil {
517 517 }
518 518 }
519 519 */
520   -
  520 +
521 521 $aGuardTriggers = KTWorkflowUtil::getGuardTriggersForTransition($oTransition);
522 522 if (PEAR::isError($aGuardTriggers)) {
523 523 return $aGuardTriggers; // error out?
... ... @@ -555,7 +555,7 @@ class KTWorkflowUtil {
555 555 return $oWorkflow;
556 556 }
557 557 $oSourceState =& KTWorkflowUtil::getWorkflowStateForDocument($oDocument);
558   -
  558 +
559 559 // walk the action triggers.
560 560 $aActionTriggers = KTWorkflowUtil::getActionTriggersForTransition($oTransition);
561 561 if (PEAR::isError($aActionTriggers)) {
... ... @@ -586,11 +586,11 @@ class KTWorkflowUtil {
586 586  
587 587 // create the document transaction record
588 588 $sTransactionComments = sprintf(_kt("Workflow state changed from %s to %s"), $sSourceState, $sTargetState);
589   -
  589 +
590 590 if ($sComments) {
591 591 $sTransactionComments .= _kt("; Reason given was: ") . $sComments;
592 592 }
593   - $oDocumentTransaction = & new DocumentTransaction($oDocument, $sTransactionComments, 'ktcore.transactions.workflow_state_transition');
  593 + $oDocumentTransaction = new DocumentTransaction($oDocument, $sTransactionComments, 'ktcore.transactions.workflow_state_transition');
594 594 $oDocumentTransaction->create();
595 595  
596 596 // walk the action triggers.
... ... @@ -611,13 +611,13 @@ class KTWorkflowUtil {
611 611 // {{{ informUsersForState
612 612 function informUsersForState($oState, $aInformed, $oDocument, $oUser, $sComments) {
613 613 // say no to duplicates.
614   -
  614 +
615 615 KTWorkflowNotification::clearNotificationsForDocument($oDocument);
616 616  
617 617 $aUsers = array();
618 618 $aGroups = array();
619 619 $aRoles = array();
620   -
  620 +
621 621 foreach (KTUtil::arrayGet($aInformed,'user',array()) as $iUserId) {
622 622 $oU = User::get($iUserId);
623 623 if (PEAR::isError($oU) || ($oU == false)) {
... ... @@ -626,7 +626,7 @@ class KTWorkflowUtil {
626 626 $aUsers[$oU->getId()] = $oU;
627 627 }
628 628 }
629   -
  629 +
630 630 foreach (KTUtil::arrayGet($aInformed,'group',array()) as $iGroupId) {
631 631 $oG = Group::get($iGroupId);
632 632 if (PEAR::isError($oG) || ($oG == false)) {
... ... @@ -635,7 +635,7 @@ class KTWorkflowUtil {
635 635 $aGroups[$oG->getId()] = $oG;
636 636 }
637 637 }
638   -
  638 +
639 639 foreach (KTUtil::arrayGet($aInformed,'role',array()) as $iRoleId) {
640 640 $oR = Role::get($iRoleId);
641 641 if (PEAR::isError($oR) || ($oR == false)) {
... ... @@ -644,9 +644,9 @@ class KTWorkflowUtil {
644 644 $aRoles[] = $oR;
645 645 }
646 646 }
647   -
648   -
649   -
  647 +
  648 +
  649 +
650 650 // FIXME extract this into a util - I see us using this again and again.
651 651 // start with roles ... roles _only_ ever contain groups.
652 652 foreach ($aRoles as $oRole) {
... ... @@ -662,11 +662,11 @@ class KTWorkflowUtil {
662 662 $oRoleAllocation = RoleAllocation::getAllocationsForFolderAndRole($oDocument->getFolderID(), $oRole->getId());
663 663 }
664 664 if (is_null($oRoleAllocation) || PEAR::isError($oRoleAllocation)) {
665   - continue;
  665 + continue;
666 666 }
667 667 $aRoleUsers = $oRoleAllocation->getUsers();
668 668 $aRoleGroups = $oRoleAllocation->getGroups();
669   -
  669 +
670 670 foreach ($aRoleUsers as $id => $oU) {
671 671 $aUsers[$id] = $oU;
672 672 }
... ... @@ -674,27 +674,27 @@ class KTWorkflowUtil {
674 674 $aGroups[$id] = $oGroup;
675 675 }
676 676 }
677   -
678   -
679   -
  677 +
  678 +
  679 +
680 680 // we now have a (potentially overlapping) set of groups, which may
681 681 // have subgroups.
682 682 //
683 683 // what we need to do _now_ is build a canonical set of groups, and then
684 684 // generate the singular user-base.
685   -
  685 +
686 686 $aGroupMembershipSet = GroupUtil::buildGroupArray();
687 687 $aAllIds = array_keys($aGroups);
688 688 foreach ($aGroups as $id => $oGroup) {
689 689 $aAllIds = kt_array_merge($aGroupMembershipSet[$id], $aAllIds);
690 690 }
691   -
  691 +
692 692 foreach ($aAllIds as $id) {
693 693 if (!array_key_exists($id, $aGroups)) {
694 694 $aGroups[$id] = Group::get($id);
695 695 }
696 696 }
697   -
  697 +
698 698 // now, merge this (again) into the user-set.
699 699 foreach ($aGroups as $oGroup) {
700 700 $aNewUsers = $oGroup->getMembers();
... ... @@ -705,9 +705,9 @@ class KTWorkflowUtil {
705 705 }
706 706 }
707 707 }
708   -
709   -
710   - // and done.
  708 +
  709 +
  710 + // and done.
711 711 foreach ($aUsers as $oU) {
712 712 if (!PEAR::isError($oU)) {
713 713 KTWorkflowNotification::newNotificationForDocument($oDocument, $oU, $oState, $oUser, $sComments);
... ... @@ -750,7 +750,7 @@ class KTWorkflowUtil {
750 750 return KTPermissionUtil::getAllowedForDescriptor($iDescriptorId);
751 751 }
752 752 // }}}
753   -
  753 +
754 754 // retrieves the triggers for a given transition in their WorkflowTrigger form.
755 755 function getTriggersForTransition($oTransition) {
756 756 $oKTWorkflowTriggerRegistry =& KTWorkflowTriggerRegistry::getSingleton();
... ... @@ -766,106 +766,106 @@ class KTWorkflowUtil {
766 766 }
767 767 return $aTriggers;
768 768 }
769   -
  769 +
770 770 function getGuardTriggersForTransition($oTransition) {
771 771 $aTriggers = KTWorkflowUtil::getTriggersForTransition($oTransition);
772   - if (PEAR::isError($aTriggers)) {
773   - return $aTriggers;
  772 + if (PEAR::isError($aTriggers)) {
  773 + return $aTriggers;
774 774 }
775 775 $aGuards = array();
776 776 foreach ($aTriggers as $oTrigger) {
777 777 $aInfo = $oTrigger->getInfo();
778   - if ($aInfo['guard']) {
  778 + if ($aInfo['guard']) {
779 779 $aGuards[] = $oTrigger;
780 780 }
781 781 }
782 782 return $aGuards;
783 783 }
784   -
  784 +
785 785 function getActionTriggersForTransition($oTransition) {
786 786 $aTriggers = KTWorkflowUtil::getTriggersForTransition($oTransition);
787   - if (PEAR::isError($aTriggers)) {
788   - return $aTriggers;
  787 + if (PEAR::isError($aTriggers)) {
  788 + return $aTriggers;
789 789 }
790 790 $aGuards = array();
791 791 foreach ($aTriggers as $oTrigger) {
792 792 $aInfo = $oTrigger->getInfo();
793   - if ($aInfo['action']) {
  793 + if ($aInfo['action']) {
794 794 $aGuards[] = $oTrigger;
795 795 }
796 796 }
797 797 return $aGuards;
798 798 }
799   -
  799 +
800 800  
801 801 function replaceState($oState, $oReplacement) {
802 802 $state_id = KTUtil::getId($oState);
803 803 $replacement_id = KTUtil::getId($oReplacement);
804   -
  804 +
805 805 // we need to convert:
806 806 // - documents
807 807 // - transitions
808 808 // before we do a delete.
809   - $doc = KTUtil::getTableName('document_metadata_version');
  809 + $doc = KTUtil::getTableName('document_metadata_version');
810 810 $aDocQuery = array(
811 811 "UPDATE $doc SET workflow_state_id = ? WHERE workflow_state_id = ?",
812 812 array($replacement_id, $state_id),
813   - );
  813 + );
814 814 $res = DBUtil::runQuery($aDocQuery);
815 815 if (PEAR::isError($res)) { return $res; }
816   -
  816 +
817 817 $wf = KTUtil::getTableName('workflow_transitions');
818 818 $aTransitionQuery = array(
819 819 "UPDATE $wf SET target_state_id = ? WHERE workflow_state_id = ?",
820 820 array($replacement_id, $state_id),
821   - );
822   - $res = DBUtil::runQuery($aTransitionQuery);
  821 + );
  822 + $res = DBUtil::runQuery($aTransitionQuery);
823 823 if (PEAR::isError($res)) { return $res; }
824   -
  824 +
825 825 $wf = KTUtil::getTableName('workflow_state_transitions');
826 826 $aTransitionQuery = array(
827 827 "DELETE FROM $wf WHERE state_id = ?",
828 828 array($state_id),
829   - );
830   - $res = DBUtil::runQuery($aTransitionQuery);
  829 + );
  830 + $res = DBUtil::runQuery($aTransitionQuery);
831 831 if (PEAR::isError($res)) { return $res; }
832   -
  832 +
833 833 Document::clearAllCaches();
834   - KTWorkflowTransitions::clearAllCaches();
  834 + KTWorkflowTransitions::clearAllCaches();
835 835 }
836   -}
  836 +}
837 837  
838 838 class KTWorkflowTriggerRegistry {
839 839 var $triggers;
840   -
  840 +
841 841 function KTWorkflowTriggerRegistry() {
842 842 $this->triggers = array();
843   - }
844   -
845   - // {{{ getSingleton
846   - function &getSingleton () {
847   - if (!KTUtil::arrayGet($GLOBALS['_KT_PLUGIN'], 'oKTWorkflowTriggerRegistry')) {
848   - $GLOBALS['_KT_PLUGIN']['oKTWorkflowTriggerRegistry'] = new KTWorkflowTriggerRegistry;
849   - }
850   - return $GLOBALS['_KT_PLUGIN']['oKTWorkflowTriggerRegistry'];
851 843 }
852   - // }}}
853   -
  844 +
  845 + static function &getSingleton () {
  846 + static $singleton=null;
  847 + if (is_null($singleton))
  848 + {
  849 + $singleton = new KTWorkflowTriggerRegistry();
  850 + }
  851 + return $singleton;
  852 + }
  853 +
854 854 function registerWorkflowTrigger($sNamespace, $sClassname, $sPath) {
855 855 $this->triggers[$sNamespace] = array('class' => $sClassname, 'path' => $sPath);
856 856 }
857   -
  857 +
858 858 function getWorkflowTrigger($sNamespace) {
859 859 $aInfo = KTUtil::arrayGet($this->triggers, $sNamespace, null);
860 860 if (is_null($aInfo)) {
861 861 return PEAR::raiseError(sprintf(_kt("Unable to find workflow trigger: %s"), $sNamespace));
862   - }
863   -
  862 + }
  863 +
864 864 require_once($aInfo['path']);
865 865  
866 866 return new $aInfo['class'];
867 867 }
868   -
  868 +
869 869 // get a keyed list of workflow triggers
870 870  
871 871 function listWorkflowTriggers() {
... ... @@ -877,5 +877,5 @@ class KTWorkflowTriggerRegistry {
877 877 // FIXME do we want to order this alphabetically?
878 878 return $triggerlist;
879 879 }
880   -}
  880 +}
881 881  
... ...