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); }
function do_setconnections() { // we *must* ensure that transitions are not set to originate from their // destination. // // we can ignore it here, because its dealt with in workflowadminutil $to = (array) KTUtil::arrayGet($_REQUEST, 'fTo'); $from = (array) KTUtil::arrayGet($_REQUEST, 'fFrom'); // we do not trust any of this data. $states = KTWorkflowState::getByWorkflow($this->oWorkflow); $states = KTUtil::keyArray($states); $transitions = KTWorkflowTransition::getByWorkflow($this->oWorkflow); $this->startTransaction(); foreach ($transitions as $oTransition) { $dest_id = $to[$oTransition->getId()]; $oDestState = $states[$dest_id]; if (!is_null($oDestState)) { $oTransition->setTargetStateId($dest_id); $res = $oTransition->update(); if (PEAR::isError($res)) { $this->errorRedirectTo('basic', sprintf(_kt("Unexpected error updating transition: %s"), $res->getMessage())); } } // hook up source states. $source_state_ids = array(); $sources = (array) $from[$oTransition->getId()]; foreach ($sources as $state_id => $discard) { // test existence $oState = $states[$state_id]; if (!is_null($oState) && $dest_id != $state_id) { $source_state_ids[] = $oState->getId(); } } $aFromTransitionID = array_keys($_REQUEST['fFrom']); //run through all transitions to change foreach ($aFromTransitionID as $iCurrentId) { if ($oTransition->getId() == $iCurrentId) { $res = KTWorkflowAdminUtil::saveTransitionSources($oTransition, $source_state_ids); } } if (PEAR::isError($res)) { $this->errorRedirectTo('basic', sprintf(_kt("Failed to set transition origins: %s"), $res->getMessage())); } } $this->successRedirectTo('basic', _kt("Workflow process updated.")); }