workflowsv2.php

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

PHP
1,647
字号
	        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()));
    }

    function do_newWorkflow() {
        // subdispatch this to the NewWorkflowWizard.
        require_once(dirname(__FILE__) . '/workflow/newworkflow.inc.php');

        $oSubDispatcher =& new KTNewWorkflowWizard;
        $oSubDispatcher->redispatch('wizard', null, $this);
        exit(0);
    }

    // -------------------- Overview -----------------
    // basic view page.

    function do_view() {
        $oTemplate = $this->oValidator->validateTemplate('ktcore/workflow/admin/view');

        $this->oPage->setBreadcrumbDetails(_kt("Overview"));

        if (!$this->oWorkflow->getIsEnabled()) {
            $this->addInfoMessage(_kt("This workflow is currently marked as disabled.  No new documents can be assigned to this workflow until it is enabled.  To change this, please edit the workflow's base properties."));
        }

        if ($this->oWorkflow->getStartStateId() == false) {
            $this->addErrorMessage(_kt("No start state is specified for this workflow.  No new documents can be assigned to this workflow until one is assigned. To change this, please edit the workflow's base properties."));
        }

        // for the basic view
        $start_state_id = $this->oWorkflow->getStartStateId();
        $oState = KTWorkflowState::get($start_state_id);

        if (PEAR::isError($oState)) {
            $state_name = _kt('No starting state.');
        } else {
            $state_name = $oState->getName();
        }

        // we want to "outsource" some of the analysis

        if ($this->HAVE_GRAPHVIZ) {
            $graph_data = $this->get_graph($this->oWorkflow);
            if (!empty($graph_data['errors'])) {
                foreach ($graph_data['errors'] as $error) {
                    $this->addErrorMessage($error);
                }
            }

            if (!empty($graph_data['info'])) {
                foreach ($graph_data['info'] as $info) {
                    $this->addInfoMessage($info);
                }
            }
        }

        $oTemplate->setData(array(
            'context' => $this,
            'workflow_name' => $this->oWorkflow->getName(),
            'state_name' => $state_name,
            'workflow' => $this->oWorkflow,
            'have_graphviz' => $this->HAVE_GRAPHVIZ,
        ));
        return $oTemplate->render();
    }

    function form_coreedit() {
        $oForm = new KTForm;

        $oForm->setOptions(array(
            'context' => $this,
            'action' => 'setcore',
            'fail_action' => 'editcore',
            'cancel_action' => 'view',
            'label' => _kt('Edit Workflow Details'),
            'submit_label' => _kt('Update Workflow Details'),
        ));

        $oForm->setWidgets(array(
            array('ktcore.widgets.string',array(
                'label' => _kt("Workflow Name"),
                'description' => _kt("Each workflow must have a unique name."),
                'name' => 'workflow_name',
                'required' => true,
                'value' => sanitizeForHTML($this->oWorkflow->getName()),
            )),
            array('ktcore.widgets.entityselection', array(
                'label' => _kt("Starting State"),
                'description' => _kt('When a document has this workflow applied to it, which state should it initially have.'),
                'name' => 'start_state',
                'label_method' => 'getHumanName',
                'vocab' => KTWorkflowState::getByWorkflow($this->oWorkflow),
                'value' => $this->oWorkflow->getStartStateId(),
                'required' => true,
            )),
            array('ktcore.widgets.boolean', array(
                'label' => _kt('Enabled'),
                'description' => _kt('If a workflow is disabled, no new documents may be placed in it.  Documents which were previously in the workflow continue to be able to change state, however.'),
                'name' => 'enabled',
                'value' => $this->oWorkflow->getIsEnabled(),
            )),
        ));

        $oForm->setValidators(array(
            array('ktcore.validators.string', array(
                'test' => 'workflow_name',
                'output' => 'workflow_name',
            )),
            array('ktcore.validators.entity', array(
                'test' => 'start_state',
                'class' => 'KTWorkflowState',
                'output' => 'start_state',
            )),
            array('ktcore.validators.boolean', array(
                'test' => 'enabled',
                'output' => 'enabled',
            ))
        ));

        return $oForm;
    }

    function do_editcore() {

        $oTemplate = $this->oValidator->validateTemplate('ktcore/workflow/admin/edit_core');
        $this->oPage->setBreadcrumbDetails(_kt("Edit Details"));

        $oForm = $this->form_coreedit();

        $oTemplate->setData(array(
            'context' => $this,
            'workflow_name' => $this->oWorkflow->getName(),
            'edit_form' => $oForm,
        ));
        return $oTemplate->render();
    }

    function do_setcore() {
        $oForm = $this->form_coreedit();
        $res = $oForm->validate();
        $data = $res['results'];
        $errors = $res['errors'];
        if (!empty($errors)) {
            $oForm->handleError();
        }

        $this->startTransaction();
        $this->oWorkflow->setName($data['workflow_name']);
        $this->oWorkflow->setHumanName($data['workflow_name']);
        $this->oWorkflow->setStartStateId($data['start_state']->getId());
        $this->oWorkflow->setIsEnabled($data['enabled']);
        $res = $this->oWorkflow->update();
        if (PEAR::isError($res)) {
            $oForm->handleError(sprintf(_kt("Failed to update workflow: %s"), $res->getMessage()));
        }

        $this->successRedirectTo("view",_kt("Workflow updated."));
    }

    // ----------------- Basic - States & Transition ---------------------
    function breadcrumbs_basic() {
        $this->aBreadcrumbs[] = array(
            'url' => KTUtil::addQueryStringSelf($this->meldPersistQuery("", "basic")),
            'name' => _kt("States and Transitions"),
        );
    }

    function do_basic() {
        $oTemplate = $this->oValidator->validateTemplate('ktcore/workflow/admin/basic_overview');
        $this->breadcrumbs_basic();
        $this->oPage->setBreadcrumbDetails(_kt("Overview"));

        $aStates = KTWorkflowState::getByWorkflow($this->oWorkflow);
        $aTransitions = KTWorkflowTransition::getByWorkflow($this->oWorkflow);


        if ($this->HAVE_GRAPHVIZ) {
            $graph_data = $this->get_graph($this->oWorkflow);
            if (!empty($graph_data['errors'])) {
                foreach ($graph_data['errors'] as $error) {
                    $this->addErrorMessage($error);
                }
            }

            if (!empty($graph_data['info'])) {
                foreach ($graph_data['info'] as $info) {
                    $this->addInfoMessage($info);
                }
            }
        }

        $oTemplate->setData(array(
            'context' => $this,
            'workflow_name' => $this->oWorkflow->getName(),
            'states' => $aStates,
            'transitions' => $aTransitions,
        ));
        return $oTemplate->render();
    }

    function form_transitionconnections() {
        $oForm = new KTForm;
        $oForm->setOptions(array(
            'label' => _kt('Configure Workflow Process'),
            'description' => _kt('The process a document follows is controlled by the way that the transitions between states are setup.  A document starts the workflow in the initial state, and then follows transitions between states.  Which users can perform these transitions can be configured in the "Security" section.'),
            'submit_label' => _kt('Update Process'),
            'cancel_action' => 'basic',
            'action' => 'setconnections',
            'fail_action' => 'transitionconnections', // consistency - this is not really used.
            'context' => $this,
        ));

        return $oForm;
    }

    function do_transitionconnections() {
        // we don't use a traditional form here, since the grid is too complex
        // and its essentially one-shot.
        //
        //
        $oForm = $this->form_transitionconnections();
        $oTemplate = $this->oValidator->validateTemplate('ktcore/workflow/admin/configure_process');

        $this->breadcrumbs_basic();
        $this->oPage->setBreadcrumbDetails(_kt("Edit Transition Connections"));

        // we want to re-use this for *subsets*.
        $transition_ids = KTUtil::arrayGet($_REQUEST, 'transition_ids');
        $bRestrict = is_array($transition_ids);

        $transitions = KTWorkflowTransition::getByWorkflow($this->oWorkflow);
        $availability = array();
        foreach ($transitions as $oTransition) {
            if ($bRestrict) {
                if ($transition_ids[$oTransition->getId()]) {
                    $final_transitions[] = $oTransition;
                } else {
                    continue;
                }
            }

            $sources = KTWorkflowAdminUtil::getSourceStates($oTransition, array('ids' => true));
            $aSources = array();
            foreach ($sources as $source) { $aSources[$source] = $source; }
            $availability[$oTransition->getId()] = $aSources;
        }


        if ($bRestrict) {
            $transitions = $final_transitions;
        }


        if ($this->HAVE_GRAPHVIZ) {
            $graph_data = $this->get_graph($this->oWorkflow);
            if (!empty($graph_data['errors'])) {
                foreach ($graph_data['errors'] as $error) {
                    $this->addErrorMessage($error);
                }
            }

            if (!empty($graph_data['info'])) {
                foreach ($graph_data['info'] as $info) {
                    $this->addInfoMessage($info);
                }
            }
        }

        $oTemplate->setData(array(
            'context' => $this,
            'form' => $oForm,
            'states' => KTWorkflowState::getByWorkflow($this->oWorkflow),
            'transitions' => $transitions,
            'availability' => $availability,
        ));

        return $oTemplate->render();
    }

    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()));
                }

⌨️ 快捷键说明

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