newworkflow.inc.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 410 行 · 第 1/2 页

PHP
410
字号
        }

        if (!empty($failed)) {
            $extra_errors['transitions'] = sprintf(_kt("You cannot have duplicate transition names: %s"), implode(', ', $failed));
        }
        $data['transitions'] = $transitions;

        // handle errors.
        if (!empty($res['errors']) || !empty($extra_errors)) {
            $oForm->handleError(null, $extra_errors);
        }

        // store the data for a while.

        $wiz_data = (array) $_SESSION['_wiz_data'];
        $wiz_data[$fWizardKey] = $data;
        $_SESSION['_wiz_data'] =& $wiz_data;

        if (empty($data['transitions'])) {
            return $this->finalise();   // finish and go.
        }

        $this->successRedirectTo("step2",_kt("Initial data stored."));
    }

    function do_step2() {
    	 $fWizardKey = KTUtil::arrayGet($_REQUEST, 'fWizardKey');
    	if (!empty($fWizardKey))
    	{
    		 $this->errorRedirectToMain(_kt("Could not create workflow.") );
    		 exit;
    	}
        $wiz_data = (array) $_SESSION['_wiz_data'][$fWizardKey];

        if (empty($wiz_data)) {
            $this->errorRedirectToMain(_kt("Unable to find previous value.  Please try again."));
        }

        $oTemplate = $this->oValidator->validateTemplate('ktcore/workflow/admin/new_wizard_step2');

        $transitions = (array) $wiz_data['transitions'];
        $args = $this->meldPersistQuery("", "process_step2", true);

        $oTemplate->setData(array(
            'context' => $this,
            'fWizardKey'=>$fWizardKey,
            'args' => $args,
            'transitions' => $wiz_data['transitions'],
            'states' => $wiz_data['states'],
        ));
        return $oTemplate->render();
    }

    function do_process_step2() {
        $fWizardKey = KTUtil::arrayGet($_REQUEST, 'fWizardKey');
        if (!empty($fWizardKey))
    	{
    		 $this->errorRedirectToMain(_kt("Could not create workflow.") );
    		 exit;
    	}
        $wiz_data = $_SESSION['_wiz_data'][$fWizardKey];
        if (empty($wiz_data)) {
            $this->errorRedirectToMain(_kt("Unable to locate stored data.  Please try again."));
        }

        // we can't use the form "stuff" here since we don't have a grid widget yet
        // and hopefully never will.

        $fToData = (array) KTUtil::arrayGet($_REQUEST, 'fTo');
        $fFromData = (array) KTUtil::arrayGet($_REQUEST, 'fFrom');

        // these are data[transition][state] = true

        $fTo = array();
        $initial_state = $wiz_data['initial_state'];
        foreach ($wiz_data['transitions'] as $transition) {
            $candidate = $fToData[$transition];
            if (empty($wiz_data['states'][$candidate])) {
                $candidate = $initial_state;
            }
            $fTo[$transition] = $candidate;
        }

        $fFrom = array();
        foreach ($wiz_data['transitions'] as $transition) {
            $d = (array) KTUtil::arrayGet($fFromData, $transition);
            $final = array();
            foreach ($d as $state => $discard) {
                if (!empty($wiz_data['states'][$state])) {
                    $final[] = $state;
                }
            }
            $fFrom[$transition] = $final;
        }

        $wiz_data['from'] = $fFrom;
        $wiz_data['to'] = $fTo;

        $_SESSION['_wiz_data'][$fWizardKey] = $wiz_data;

        return $this->finalise();
    }

    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(),
                // FIXME we need to deprecate these, eventually
                "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);
    }
}

?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?