Exemplo n.º 1
0
 function finalise()
 {
     $fWizardKey = KTUtil::arrayGet($_REQUEST, 'fWizardKey');
     if (!empty($fWizardKey)) {
         $this->errorRedirectToMain(_kt("Could not create workflow."));
         exit;
     }
     $wiz_data = $_SESSION['_wiz_data'][$fWizardKey];
     // gather all our data.  we're sure this is all good and healthy.
     $states = $wiz_data['states'];
     $transitions = $wiz_data['transitions'];
     $from = $wiz_data['from'];
     $to = $wiz_data['to'];
     $initial_state = $wiz_data['initial_state'];
     $workflow_name = $wiz_data['workflow_name'];
     $this->startTransaction();
     // create the initial workflow
     $oWorkflow = KTWorkflow::createFromArray(array('name' => $workflow_name, 'humanname' => $workflow_name, 'enabled' => true));
     if (PEAR::isError($oWorkflow)) {
         $this->errorRedirectToMain(sprintf(_kt("Failed to create workflow: %s"), $oWorkflow->getMessage()));
     }
     $iWorkflowId = $oWorkflow->getId();
     // create the states.
     $aStates = array();
     foreach ($states as $state_name) {
         $oState = KTWorkflowState::createFromArray(array('workflowid' => $iWorkflowId, 'name' => $state_name, 'humanname' => $state_name));
         if (PEAR::isError($oState)) {
             $this->errorRedirectToMain(sprintf(_kt("Failed to create state: %s"), $oState->getMessage()));
         }
         $aStates[$state_name] = $oState;
     }
     // update the initial state on workflow
     $oInitialState = $aStates[$initial_state];
     $oWorkflow->setStartStateId($oInitialState->getId());
     $res = $oWorkflow->update();
     if (PEAR::isError($res)) {
         $this->errorRedirectToMain(sprintf(_kt("Failed to update workflow: %s"), $res->getMessage()));
     }
     // next, we create and hook up the transitions.
     $aTransitions = array();
     foreach ($transitions as $transition) {
         $dest_name = $to[$transition];
         $oDestState = $aStates[$dest_name];
         $oTransition = KTWorkflowTransition::createFromArray(array("WorkflowId" => $iWorkflowId, "Name" => $transition, "HumanName" => $transition, "TargetStateId" => $oDestState->getId(), "GuardPermissionId" => null, "GuardGroupId" => null, "GuardRoleId" => null, "GuardConditionId" => null));
         if (PEAR::isError($oTransition)) {
             $this->errorRedirectToMain(sprintf(_kt("Failed to create transition: %s"), $oTransition->getMessage()));
         }
         // hook up source states.
         $state_ids = array();
         $sources = (array) $from[$transition];
         foreach ($sources as $state_name) {
             // must exist.
             $oState = $aStates[$state_name];
             $state_ids[] = $oState->getId();
         }
         $res = KTWorkflowAdminUtil::saveTransitionSources($oTransition, $state_ids);
         if (PEAR::isError($res)) {
             $this->errorRedirectToMain(sprintf(_kt("Failed to set transition origins: %s"), $res->getMessage()));
         }
     }
     $this->commitTransaction();
     // finally, we want to redirect the user to the parent dispatcher somehow.
     // FIXME nbm:  how do you recommend we do this?
     $base = $_SERVER['PHP_SELF'];
     $qs = sprintf("action=view&fWorkflowId=%d", $oWorkflow->getId());
     $url = KTUtil::addQueryString($base, $qs);
     $this->addInfoMessage(_kt("Your new workflow has been created.  You may want to configure security and notifications from the menu on the left."));
     redirect($url);
 }
Exemplo n.º 2
0
 function do_confirmCopy()
 {
     $oSelWorkflow = KTWorkflow::get(KTUtil::arrayGet($_REQUEST, 'workflowId', array()));
     $sWorkflowName = KTUtil::arrayGet($_REQUEST, 'workflowName', array());
     // Check that the workflow does not exist already
     $sWorkflowName = str_replace(array('   ', '  '), array(' ', ' '), $sWorkflowName);
     $oWorkflow = KTWorkflow::getByName($sWorkflowName);
     if (!PEAR::isError($oWorkflow)) {
         return $this->errorRedirectToMain(_kt("A workflow with that name already exists.  Please choose a different name for this workflow."));
     }
     // create the initial workflow
     $oNewWorkflow = KTWorkflow::createFromArray(array('name' => $sWorkflowName, 'humanname' => $sWorkflowName, 'enabled' => true));
     // get selected workflow states from database
     $oSelWorkflowStates = KTWorkflowState::getByWorkflow($oSelWorkflow);
     // array to store map of old and new states
     $aStatesMap = array();
     // create new states and build old-to-new map
     foreach ($oSelWorkflowStates as $oOldState) {
         $oNewState = KTWorkflowState::createFromArray(array('workflowid' => $oNewWorkflow->getId(), 'name' => $oOldState->getName(), 'humanname' => $oOldState->getName()));
         $aStatesMap[oldId][] = $oOldState->getId();
         $aStatesMap[newId][] = $oNewState->getId();
         if (PEAR::isError($oNewState)) {
             $oForm->errorRedirectToMain(sprintf(_kt("Unexpected failure cloning state: %s"), $oNewState->getMessage()));
         }
         // Get all state permission assignments for old workflow transitions
         // and copy for copied workflow state permission assignments
         $aPermissionAssignments = KTWorkflowStatePermissionAssignment::getByState($oOldState);
         if (count($aPermissionAssignments) > 0) {
             foreach ($aPermissionAssignments as $oPermAssign) {
                 for ($i = 0; $i < count($aStatesMap[oldId]); $i++) {
                     if ($aStatesMap[oldId][$i] == $oPermAssign->getStateId()) {
                         $iStateId = $aStatesMap[newId][$i];
                         $res = KTWorkflowStatePermissionAssignment::createFromArray(array('iStateId' => $iStateId, 'iPermissionId' => $oPermAssign->getPermissionId(), 'iDescriptorId' => $oPermAssign->getDescriptorId()));
                         if (PEAR::isError($res)) {
                             return $this->errorRedirectToMain(sprintf(_kt("Unable to copy state permission assignment: %s"), $res->getMessage()));
                         }
                     }
                 }
             }
         }
         // Copy all disabled actions for states
         $aDisabled = KTWorkflowUtil::getDisabledActionsForState($oOldState);
         $res = KTWorkflowUtil::setDisabledActionsForState($oNewState, $aDisabled);
         // Copy all enabled actions for states
         $aDisabled = KTWorkflowUtil::getEnabledActionsForState($oOldState);
         $res = KTWorkflowUtil::setEnabledActionsForState($oNewState, $aDisabled);
         if (PEAR::isError($res)) {
             return $this->errorRedirectToMain(sprintf(_kt("Unable to copy disabled state actions: %s"), $res->getMessage()));
         }
         $this->copyStateNotifications($oOldState, $oNewState);
     }
     // update workflow and set initial state
     for ($i = 0; $i < count($aStatesMap[oldId]); $i++) {
         if ($oSelWorkflow->getStartStateId() == $aStatesMap[oldId][$i]) {
             $oNewWorkflow->setStartStateId($aStatesMap[newId][$i]);
             $res = $oNewWorkflow->update();
             if (PEAR::isError($res)) {
                 $this->errorRedirectToMain(sprintf(_kt("Failed to update workflow: %s"), $res->getMessage()));
             }
         }
     }
     // set controlled workflow actions
     $aWFActions = KTWorkflowUtil::getControlledActionsForWorkflow($oSelWorkflow);
     $res = KTWorkflowUtil::setControlledActionsForWorkflow($oNewWorkflow, $aWFActions);
     if (PEAR::isError($res)) {
         $this->errorRedirectToMain(sprintf(_kt("Failed to copy workflow controlled actions: %s"), $res->getMessage()));
     }
     // get selected workflow transitions from database
     $oSelWorkflowTransitions = KTWorkflowTransition::getByWorkflow($oSelWorkflow);
     // array to store map of old and new transitions
     $aTransitionsMap = array();
     // copy transitions for workflow
     foreach ($oSelWorkflowTransitions as $oOldTransition) {
         for ($i = 0; $i < count($aStatesMap[oldId]); $i++) {
             if ($oOldTransition->getTargetStateId() == $aStatesMap[oldId][$i]) {
                 $iDestState = $aStatesMap[newId][$i];
             }
         }
         $oNewTransition = KTWorkflowTransition::createFromArray(array('workflowid' => $oNewWorkflow->getId(), 'Name' => $oOldTransition->getName(), 'HumanName' => $oOldTransition->getName(), 'TargetStateId' => $iDestState, 'GuardPermissionId' => null, 'GuardGroupId' => null, 'GuardRoleId' => null, 'GuardConditionId' => null));
         $aTransitionsMap[oldId][] = $oOldTransition->getId();
         $aTransitionsMap[newId][] = $oNewTransition->getId();
         if (PEAR::isError($oNewTransition)) {
             $this->errorRedirectToMain(sprintf(_kt("Failed to copy transition: %s"), $oTransition->getMessage()));
         }
         // map source transitions onto states
         $aOldTransitionSources = KTWorkflowAdminUtil::getSourceStates($oOldTransition);
         $aSourceStates = array();
         for ($j = 0; $j < count($aOldTransitionSources); $j++) {
             for ($i = 0; $i < count($aStatesMap[oldId]); $i++) {
                 if ($aStatesMap[oldId][$i] == $aOldTransitionSources[$j]->getId()) {
                     $aSourceStates[] = $aStatesMap[newId][$i];
                     continue;
                 }
             }
         }
         $res = KTWorkflowAdminUtil::saveTransitionSources($oNewTransition, $aSourceStates);
         if (PEAR::isError($res)) {
             $this->errorRedirectToMain(sprintf(_kt("Failed to set transition origins: %s"), $res->getMessage()));
         }
         // Get all triggers for old workflow transitions and
         // copy for copied workflow transitions
         $aTriggers = KTWorkflowTriggerInstance::getByTransition($oOldTransition);
         if (count($aTriggers) > 0) {
             foreach ($aTriggers as $oTrigger) {
                 for ($i = 0; $i < count($aTransitionsMap[oldId]); $i++) {
                     if ($aTransitionsMap[oldId][$i] == $oTrigger->getTransitionId()) {
                         $iTransitionId = $aTransitionsMap[newId][$i];
                         $res = KTWorkflowTriggerInstance::createFromArray(array('transitionid' => $iTransitionId, 'namespace' => $oTrigger->getNamespace(), 'config' => $oTrigger->getConfigArrayText()));
                         if (PEAR::isError($res)) {
                             return $this->errorRedirectToMain(sprintf(_kt("Unable to add trigger: %s"), $res->getMessage()));
                         }
                     }
                 }
             }
         }
     }
     return $this->successRedirectToMain(sprintf(_kt("%s successfully copied as %s"), $oSelWorkflow->getName(), $oNewWorkflow->getName()));
 }