workflowsv2.php

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

PHP
1,647
字号
<?php
/**
 * $Id: workflowsv2.php 8510 2008-05-22 15:00:55Z megan_w $
 *
 * KnowledgeTree Community Edition
 * Document Management Made Simple
 * Copyright (C) 2008 KnowledgeTree Inc.
 * Portions copyright The Jam Warehouse Software (Pty) Limited
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 3 as published by the
 * Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You can contact KnowledgeTree Inc., PO Box 7775 #87847, San Francisco,
 * California 94120-7775, or email info@knowledgetree.com.
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * KnowledgeTree" logo and retain the original copyright notice. If the display of the
 * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
 * must display the words "Powered by KnowledgeTree" and retain the original
 * copyright notice.
 * Contributor( s): ______________________________________
 */

// core
require_once(KT_LIB_DIR . "/dispatcher.inc.php");

// workflow
require_once(KT_LIB_DIR . '/workflow/workflow.inc.php');
require_once(KT_LIB_DIR . '/workflow/workflowstate.inc.php');
require_once(KT_LIB_DIR . '/workflow/workflowtransition.inc.php');
require_once(KT_LIB_DIR . '/workflow/workflowstatepermissionsassignment.inc.php');
require_once(KT_LIB_DIR . '/workflow/workflowutil.inc.php');
require_once(KT_LIB_DIR . '/workflow/workflowadminutil.inc.php');

// other
require_once(KT_LIB_DIR . '/permissions/permission.inc.php');
require_once(KT_LIB_DIR . '/actions/documentaction.inc.php');
require_once(KT_LIB_DIR . '/widgets/portlet.inc.php');
require_once(KT_LIB_DIR . '/users/User.inc');
require_once(KT_LIB_DIR . '/groups/Group.inc');
require_once(KT_LIB_DIR . '/roles/Role.inc');
require_once(KT_LIB_DIR . '/widgets/portlet.inc.php');
require_once(KT_LIB_DIR . '/widgets/forms.inc.php');
//require_once(KT_DIR . "/thirdparty/pear/GraphViz.php");

class WorkflowNavigationPortlet extends KTPortlet {
    var $oWorkflow;
    var $sHelpPage = 'ktcore/admin/workflow.html';
    var $bActive = true;

    function WorkflowNavigationPortlet($sTitle, $oWorkflow = null) {
        $this->oWorkflow = $oWorkflow;
        parent::KTPortlet($sTitle);
    }

    function render() {
        if (is_null($this->oWorkflow)) { return _kt('No Workflow Selected.'); }

        $aAdminPages = array();
        $aAdminPages[] = array('name' => _kt('Overview'), 'query' => 'action=view&fWorkflowId=' . $this->oWorkflow->getId());
        $aAdminPages[] = array('name' => _kt('States and Transitions'), 'query' => 'action=basic&fWorkflowId=' . $this->oWorkflow->getId());
        $aAdminPages[] = array('name' => _kt('Security'), 'query' => 'action=security&fWorkflowId=' . $this->oWorkflow->getId());
        $aAdminPages[] = array('name' => _kt('Workflow Effects'), 'query' => 'action=effects&fWorkflowId=' . $this->oWorkflow->getId());
        $aAdminPages[] = array('name' => _kt('Select different workflow'), 'query' => 'action=main');

        $oTemplating =& KTTemplating::getSingleton();
        $oTemplate = $oTemplating->loadTemplate("ktcore/workflow/admin_portlet");
        $aTemplateData = array(
            "context" => $this,
            "aAdminPages" => $aAdminPages,
        );

        return $oTemplate->render($aTemplateData);
    }
}

class KTWorkflowAdminV2 extends KTAdminDispatcher {
    var $oWorkflow;
    var $oState;
    var $oTransition;
    var $HAVE_GRAPHVIZ;

    function predispatch() {
        $this->persistParams(array('fWorkflowId', 'fStateId', 'fTransitionId'));

        $iWorkflowId = KTUtil::arrayGet($_REQUEST, 'fWorkflowId');
        $iStateId = KTUtil::arrayGet($_REQUEST, 'fStateId');
        $iTransitionId = KTUtil::arrayGet($_REQUEST, 'fTransitionId');

        if (!is_null($iWorkflowId)) {
            $oWorkflow =& KTWorkflow::get($iWorkflowId);
            if (!PEAR::isError($oWorkflow)) {
                $this->oWorkflow =& $oWorkflow;
            }
        }

        if (!is_null($iStateId)) {
            $oState =& KTWorkflowState::get($iStateId);
            if (!PEAR::isError($oState)) {
                $this->oState =& $oState;
            }
        }

        if (!is_null($iTransitionId)) {
            $oTransition =& KTWorkflowTransition::get($iTransitionId);
            if (!PEAR::isError($oTransition)) {
                $this->oTransition =& $oTransition;
            }
        }

        $this->aBreadcrumbs[] = array(
            'url' => $_SERVER['PHP_SELF'],
            'name' => _kt('Workflows'),
        );

        if (!is_null($this->oWorkflow)) {
            $this->oPage->addPortlet(new WorkflowNavigationPortlet(_kt("Workflow Administration"), $this->oWorkflow));

            $this->aBreadcrumbs[] = array(
                'url' => KTUtil::addQueryStringSelf(sprintf('action=view&fWorkflowId=%d', $iWorkflowId)),
                'name' => $this->oWorkflow->getName(),
            );
        }

        $this->HAVE_GRAPHVIZ = false;
/*        $dotCommand = KTUtil::findCommand("ui/dot", 'dot');
        if (!empty($dotCommand)) {
            $this->HAVE_GRAPHVIZ = true;
            $this->dotCommand = $dotCommand;
        }
*/
    }

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

        $aWorkflows = KTWorkflow::getList();

        $oTemplate->setData(array(
            'context' => $this,
            'workflows' => $aWorkflows,
        ));
        return $oTemplate->render();
    }

    function do_branchConfirm() {
        $submit = KTUtil::arrayGet($_REQUEST, 'submit' , array());
        if (array_key_exists('copy',$submit)) {
            $selection = KTUtil::arrayGet($_REQUEST, 'workflowSelect' , array());
            if(empty($selection)){
            	$this->errorRedirectToMain(_kt('No workflow selected.'));
            }
            return $this->do_copy();
        }
        if (array_key_exists('confirmCopy',$submit)) {
            $workflowId = KTUtil::arrayGet($_REQUEST, 'workflowId' , array());
            if(empty($workflowId)){
            	$this->errorRedirectToMain(_kt('An unexpected error has occured.'));
            }
            return $this->do_confirmCopy();
        }
        $this->errorRedirectToMain(_kt('No action specified.'));
    }

    function do_copy() {
    	$this->aBreadcrumbs[] = array('url' =>  $_SERVER['PHP_SELF'], 'name' => _kt('Copy Workflow'));
        $selection = KTUtil::arrayGet($_REQUEST, 'workflowSelect' , array());
        $this->oPage->setTitle('Copy Workflow');

        // get selected workflow from database
        $oSelWorkflow = KTWorkflow::get($selection);

        $oTemplating =& KTTemplating::getSingleton();
        $oTemplate = $oTemplating->loadTemplate('ktcore/workflow/admin/copy');
        $oTemplate->setData(array(
            'context' => $this,
            'workFlowName' => $oSelWorkflow->getName(),
            'workFlowId' => $oSelWorkflow->getId(),

        ));
        return $oTemplate;
    }

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

        // 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);

⌨️ 快捷键说明

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