workflowsv2.php 42 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
<?php

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

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&fWorkflowId=' . $this->oWorkflow->getId());        
    
        $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;

    function check() {
        $res = parent::check();
        $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(),
            );            
        }
        
        return $res;
    }

    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_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(sprintf(_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 <a href=\"%s\">edit</a> the workflow's base properties."), KTUtil::addQueryString($_SERVER['PHP_SELF'], $this->meldPersistQuery("","editcore"))));
        }

        if ($this->oWorkflow->getStartStateId() == false) {
            $this->addErrorMessage(sprintf(_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 <a href=\"%s\">edit</a> the workflow's base properties."), KTUtil::addQueryString($_SERVER['PHP_SELF'], $this->meldPersistQuery("","editcore"))));
        }
        
        // 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();
        }
        

        
        $oTemplate->setData(array(
            'context' => $this,
            'workflow_name' => $this->oWorkflow->getName(),
            'state_name' => $state_name,
            'workflow' => $this->oWorkflow,
        ));
        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' => $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 do_basic() {
        $oTemplate = $this->oValidator->validateTemplate('ktcore/workflow/admin/basic_overview');            
        $this->aBreadcrumbs[] = array(
            'url' => KTUtil::addQueryStringSelf($this->meldPersistQuery("", "basic")),
            'name' => _kt("States and Transitions"),
        );
        $this->oPage->setBreadcrumbDetails(_kt("Overview"));
        
        $aStates = KTWorkflowState::getByWorkflow($this->oWorkflow);
        $aTransitions = KTWorkflowTransition::getByWorkflow($this->oWorkflow);        
        
        $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');

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

            $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."));
    }
    
    function form_addstates() {
        $oForm = new KTForm;
        $oForm->setOptions(array(
            'context' => $this,
            'label' => _kt("Add New States"),
            'submit_label' => _kt("Add States"),
            'action' => 'createstates',
            'cancel_action' => 'basic',
            'fail_action' => 'addstates',
        ));
        
        $oForm->setWidgets(array(
            array('ktcore.widgets.text',array(
                'label' => _kt('New States'),
                'description' => _kt('As documents progress through their lifecycle, they pass through a number of <strong>states</strong>.  These states describe a step in the process the document must follow.  Examples of states include "reviewed","submitted" or "pending".  Note that the first state you list is the one in which documents will start the workflow - this can be changed later on. Please enter a list of states, one per line.  State names must be unique, and this includes states already in this workflow.'),
                'required' => true,
                'name' => 'states',
                'rows' => 15,
            )),
        ));
        $oForm->setValidators(array(
            array('ktcore.validators.string', array(
                'test' => 'states',
                'output' => 'states',
                'max_length' => 9999,                
            )),
        ));
        
        return $oForm;
    }
    
    function do_addstates() {
        $oForm = $this->form_addstates();
        
        $oTemplate = $this->oValidator->validateTemplate('ktcore/workflow/admin/add_states');
        $oTemplate->setData(array(
            'context' => $this,
            'form' => $oForm
        ));
        return $oTemplate->render();
    }
    
    function do_createstates() {
        $oForm = $this->form_addstates();
        $res = $oForm->validate();
        $data = $res['results'];
        $errors = $res['errors'];
        $extra_errors = array();
        
        // we want to check for duplicates, empties, etc.
        
        $initial_states = (array) explode("\n", $data['states']);
        $failed = array();
        $old_states = array();
        $states = array();
        foreach ($initial_states as $sName) {
            $state_name = trim($sName);
            if (empty($state_name)) {
                continue;
            }
            
            if ($states[$state_name]) {
                $failed[] = $state_name;
                continue;
            }
            
            // check for pre-existing states.
            $exists = KTWorkflowState::nameExists($sName, $this->oWorkflow);
            if ($exists) {
                $old_states[] = $sName;
            }
            
            $states[$state_name] = $state_name;
        }
        if (empty($states)) {
            $extra_errors['states'][] = _kt('You must provide at least one state name.');
        }
        if (!empty($failed)) {
            $extra_errors['states'][] = sprintf(_kt("You cannot have duplicate state names: %s"), implode(', ', $failed)); 
        }
        if (!empty($old_states)) {
            $extra_errors['states'][] = sprintf(_kt("You cannot use state names that are in use: %s"), implode(', ', $old_states)); 
        }
        
        // handle any errors.
        if (!empty($errors) || !empty($extra_errors)) {
            $oForm->handleError(null, $extra_errors);
        }
        
        $this->startTransaction();
        // now act
        foreach ($states as $state_name) {
            $oState = KTWorkflowState::createFromArray(array(
                'workflowid' => $this->oWorkflow->getId(),
                'name' => $state_name,
                'humanname' => $state_name,
            ));
            if (PEAR::isError($oState)) {
                $oForm->handleError(sprintf(_kt("Unexpected failure creating state: %s"), $oState->getMessage()));
            }
        }
        
        $this->successRedirectTo('basic', _kt("New States Created."));
    }
    
    
    function form_addtransitions() {
        $oForm = new KTForm;
        $oForm->setOptions(array(
            'context' => $this,
            'label' => _kt("Add New Transitions"),
            'submit_label' => _kt("Add Transitions"),
            'action' => 'createtransitions',
            'cancel_action' => 'basic',
            'fail_action' => 'addtransitions',
        ));
        
        $oForm->setWidgets(array(
            array('ktcore.widgets.text',array(
                'label' => _kt('Transitions'),
                'description' => _kt('In order to move between states, users will cause "transitions" to occur.  These transitions represent processes followed, e.g. "review document", "distribute invoice" or "publish".  Please enter a list of transitions, one per line.  Transition names must be unique.  You\'ll assign transitions to states in the next step.'),
                'required' => false,
                'name' => 'transitions',
            )),           
        ));
        $oForm->setValidators(array(
            array('ktcore.validators.string', array(
                'test' => 'transitions',
                'output' => 'transitions',
                'max_length' => 9999,                
            )),
        ));
        
        return $oForm;
    }
    
    function do_addtransitions() {
        $oForm = $this->form_addtransitions();
        
        $oTemplate = $this->oValidator->validateTemplate('ktcore/workflow/admin/add_transitions');
        $oTemplate->setData(array(
            'context' => $this,
            'form' => $oForm
        ));
        return $oTemplate->render();
    }
    
    function do_createtransitions() {
        $oForm = $this->form_addtransitions();
        $res = $oForm->validate();
        $data = $res['results'];
        $errors = $res['errors'];
        $extra_errors = array();
        
        // we want to check for duplicates, empties, etc.
        
        $initial_transitions = (array) explode("\n", $data['transitions']);
        $failed = array();
        $old_transitions = array();
        $transitions = array();
        foreach ($initial_transitions as $sName) {
            $transition_name = trim($sName);
            if (empty($transition_name)) {
                continue;
            }
            
            if ($transitions[$transition_name]) {
                $failed[] = $transition_name;
                continue;
            }
            
            // check for pre-existing states.
            $exists = KTWorkflowTransition::nameExists($sName, $this->oWorkflow);
            if ($exists) {
                $old_transitions[] = $sName;
            }
            
            $transitions[$transition_name] = $transition_name;
        }
        if (empty($transitions)) {
            $extra_errors['transitions'][] = _kt('You must provide at least one transition name.');
        }
        if (!empty($failed)) {
            $extra_errors['transitions'][] = sprintf(_kt("You cannot have duplicate transition names: %s"), implode(', ', $failed)); 
        }
        if (!empty($old_states)) {
            $extra_errors['transitions'][] = sprintf(_kt("You cannot use transition names that are in use: %s"), implode(', ', $old_transitions)); 
        }
        
        // handle any errors.
        if (!empty($errors) || !empty($extra_errors)) {
            $oForm->handleError(null, $extra_errors);
        }
        
        $this->startTransaction();
        $transition_ids = array();
        $oState = KTWorkflowState::get($this->oWorkflow->getStartStateId());
        foreach ($transitions as $transition_name) {
            $oTransition = KTWorkflowTransition::createFromArray(array(
                "WorkflowId" => $this->oWorkflow->getId(),
                "Name" => $transition_name,
                "HumanName" => $transition_name,
                "TargetStateId" => $oState->getId(),
                "GuardPermissionId" => null,
                "GuardGroupId" => null,
                "GuardRoleId" => null,
                "GuardConditionId" => null,            
            ));
            if (PEAR::isError($oTransition)) {
                $oForm->handleError(sprintf(_kt("Unexpected failure creating transition: %s"), $oTransition->getMessage()));
            }
            $transition_ids[] = $oTransition->getId();
        }
        
        $transition_ids_query = array();
        foreach ($transition_ids as $id) {
            $transition_ids_query[] = sprintf('transition_ids[%s]=%s',$id,  $id);
        }
        $transition_ids_query = implode('&', $transition_ids_query);
        
        $this->successRedirectTo('transitionconnections', _kt("New States Created."), $transition_ids_query);
    }
    
    function form_editstate($oState) {
        $oForm = new KTForm;
        
        $oForm->setOptions(array(
            'context' => $this,
            'label' => _kt('Edit State'),
            'submit_label' => _kt('Update State'),
            'action' => 'savestate',
            'fail_action' => 'editstate',
            'cancel_action' => 'basic',
        ));
        
        $oForm->setWidgets(array(
            array('ktcore.widgets.string', array(
                'name' => 'name',
                'label' => _kt('State Name'),
                'description' => _kt('As documents progress through their lifecycle, they pass through a number of <strong>states</strong>.  These states describe a step in the process the document must follow.  Examples of states include "reviewed","submitted" or "pending".  Note that the first state you list is the one in which documents will start the workflow - this can be changed later on. Please enter a list of states, one per line.  State names must be unique, and this includes states already in this workflow.'),
                'required' => true,
                'value' => $oState->getName(),
            )),
        ));
        
        $oForm->setValidators(array(
            array('ktcore.validators.string', array(
                'test' => 'name',
                'output' => 'name',
            )),
        ));
        
        return $oForm;
    }
    
    function do_editstate() {
        // remember that we check for state,
        // and its null if none or an error was passed.
        if (is_null($this->oState)) {
            $this->errorRedirectTo('basic', _kt("No state specified."));
        }
        
        $oTemplate =& $this->oValidator->validateTemplate('ktcore/workflow/admin/edit_state');
        $this->oPage->setBreadcrumbDetails(_kt('Manage State Details'));
        
        $oForm = $this->form_editstate($this->oState);
        
        $oTemplate->setData(array(
            'context' => $this,
            'edit_form' => $oForm,        
        ));
        
        return $oTemplate->render();
    }
    
    function do_savestate() {
        $oForm = $this->form_editstate();
        $res = $oForm->validate();
        $data = $res['results'];
        $errors = $res['errors'];
        $extra_errors = array();
        
        // check if any *other* states have this name.
        if ($data['name'] == $this->oState->getName()) {
            $this->successRedirectTo('editstate',_kt("No change in name."));
        } 
        
        // otherwise we're looking for something different if there's a conflict.
        
        if (KTWorkflowState::nameExists($data['name'], $this->oWorkflow)) {
            $extra_errors['name'][] = _kt('There is already a state with that name in this workflow.');
        }
        
        if (!empty($errors) || !empty($extra_errors)) {
            $oForm->handleError(null, $extra_errors);
        }
        
        $this->startTransaction();
        
        $this->oState->setName($data['name']);
        $this->oState->setHumanName($data['name']);        
        $res = $this->oState->update();
        
        if (PEAR::isError($res)) {
            $oForm->handleError(sprintf(_kt("Unable to update state: %s"), $res->getMessage()));
        }
        
        $this->successRedirectTo('editstate', _kt("State updated."));
    }
    
    function form_edittransition($oTransition) {
        $oForm = new KTForm;
        
        $oForm->setOptions(array(
            'context' => $this,
            'label' => _kt('Edit Transition'),
            'submit_label' => _kt('Update Transition'),
            'action' => 'savetransition',
            'fail_action' => 'edittransition',
            'cancel_action' => 'basic',
        ));
        
        $oForm->setWidgets(array(
            array('ktcore.widgets.string', array(
                'name' => 'name',
                'label' => _kt('Transition Name'),
                'description' => _kt('In order to move between states, users will cause "transitions" to occur.  These transitions represent processes followed, e.g. "review document", "distribute invoice" or "publish".  Transition names must be unique within the workflow (e.g. within this workflow, you can only have one transition called "publish")'),
                'required' => true,
                'value' => $oTransition->getName(),
            )),
        ));
        
        $oForm->setValidators(array(
            array('ktcore.validators.string', array(
                'test' => 'name',
                'output' => 'name',
            )),
        ));
        
        return $oForm;
    }
    
    function do_edittransition() {
        // remember that we check for state,
        // and its null if none or an error was passed.
        if (is_null($this->oTransition)) {
            $this->errorRedirectTo('basic', _kt("No transition specified."));
        }
        
        $oTemplate =& $this->oValidator->validateTemplate('ktcore/workflow/admin/edit_transition');
        $this->oPage->setBreadcrumbDetails(_kt('Manage Transition'));
        
        $oForm = $this->form_edittransition($this->oTransition);
        
        $oTemplate->setData(array(
            'context' => $this,
            'edit_form' => $oForm,        
        ));
        
        return $oTemplate->render();
    }
    
    function do_savetransition() {
        $oForm = $this->form_edittransition();
        $res = $oForm->validate();
        $data = $res['results'];
        $errors = $res['errors'];
        $extra_errors = array();
        
        // check if any *other* states have this name.
        if ($data['name'] == $this->oTransition->getName()) {
            $this->successRedirectTo('edittransition',_kt("No change in name."));
        } 
        
        // otherwise we're looking for something different if there's a conflict.
        
        if (KTWorkflowTransitions::nameExists($data['name'], $this->oWorkflow)) {
            $extra_errors['name'][] = _kt('There is already a transition with that name in this workflow.');
        }
        
        if (!empty($errors) || !empty($extra_errors)) {
            $oForm->handleError(null, $extra_errors);
        }
        
        $this->startTransaction();
        
        $this->oTransition->setName($data['name']);
        $this->oTransition->setHumanName($data['name']);        
        $res = $this->oTransition->update();
        
        if (PEAR::isError($res)) {
            $oForm->handleError(sprintf(_kt("Unable to update transition: %s"), $res->getMessage()));
        }
        
        $this->successRedirectTo('transition', _kt("Transition updated."));
    }
    
    // ----------------- Security ---------------------
    function do_security() {
        $oTemplate = $this->oValidator->validateTemplate('ktcore/workflow/admin/security_overview');            
        $this->oPage->setBreadcrumbDetails(_kt("Security"));
        
        
        $oTemplate->setData(array(
            'context' => $this,
            'workflow_name' => $this->oWorkflow->getName(),
        ));
        return $oTemplate->render();    
    }
    
    
    // == PERMISSIONS
    function do_permissionsoverview() {
        $oTemplate = $this->oValidator->validateTemplate('ktcore/workflow/admin/permissions_overview');
        
        // we want to give a complete overview.
        // this involves a grid of:
        //          permission permissions
        //   state      x           -
        //   state      -           x
        
        $aStates = KTWorkflowState::getByWorkflow($this->oWorkflow);
        $aUsefulPermissions = KTPermission::getDocumentRelevantList();
        $aPermissionGrid = array();
        $aControllers = array();
        foreach ($aStates as $oState) {
            $perms = array();
            $aStatePermAssigns = KTWorkflowStatePermissionAssignment::getByState($oState);
            $aControllers[$oState->getId()] = (!empty($aStatePermAssigns));
            
            foreach ($aStatePermAssigns as $oPermAssign) {
                $perms[$oPermAssign->getPermissionId()] = $oPermAssign; // we only care about non-null in *this* map.
            }
            
            $aPermissionGrid[$oState->getId()] = $perms;
        }
        
        $oTemplate->setData(array(
            'context' => $this,
            'controllers' => $aControllers,
            'perm_grid' => $aPermissionGrid,
            'perms' => $aUsefulPermissions,
            'states' => $aStates,
        ));
        return $oTemplate->render();
    }
    
    function form_managepermissions() {
        $oForm = new KTForm;
        $oForm->setOptions(array(
            'label' => _kt("Controlled Permissions"),
            'submit_label' => _kt("Set controlled permissions"),
            'action' => 'setcontrolledpermissions',
            'fail_action' => 'managepermissions',
            'cancel_action' => 'permissionsoverview',
            'context' => $this,
        ));
        
        return $oForm;
    }
    
    // == PERMISSIONS
    function do_managepermissions() {
        $oTemplate = $this->oValidator->validateTemplate('ktcore/workflow/admin/managepermissions');
       
        $oForm = $this->form_managepermissions();       
       
        $aUsefulPermissions = KTPermission::getDocumentRelevantList();
        $aPermissionGrid = array();
        $aStatePermAssigns = KTWorkflowStatePermissionAssignment::getByState($this->oState);
           
        foreach ($aStatePermAssigns as $oPermAssign) {           
            $aPermissionGrid[$oPermAssign->getPermissionId()] = $oPermAssign;
        }
        
        $oTemplate->setData(array(
            'context' => $this,
            'perm_grid' => $aPermissionGrid,
            'perms' => $aUsefulPermissions,
            'form' => $oForm,
        ));
        return $oTemplate->render();
    }    

    function do_setcontrolledpermissions() {
        $active = (array) KTUtil::arrayGet($_REQUEST, 'fControlled');
        
        $aUsefulPerms = KTPermission::getDocumentRelevantList();
        $aStatePermAssigns = KTWorkflowStatePermissionAssignment::getByState($this->oState);
        $aStatePermAssigns = KTUtil::keyArray($aStatePermAssigns, 'getPermissionId');        
        $assigns = array();

        $this->startTransaction();        
        // delete those who don't know want
        // create those we don't have.
        
        foreach ($aStatePermAssigns as $perm_id => $assign) {
            if (!$active[$perm_id]) {
                $assign->delete();
            }
        }
        $emptydescriptor = KTPermissionUtil::getOrCreateDescriptor(array());
        if (PEAR::isError($emptydescriptor)) {
            $this->errorRedirectTo("managepermissions", sprintf(_kt("Failed to create assignment: %s"), $emptydescriptor->getMessage()));
        }
        foreach ($active as $perm_id => $discard) {
            if (!$aStatePermAssigns[$perm_id]) {
                $assign = KTWorkflowStatePermissionAssignment::createFromArray(array(
                    "iStateId" => $this->oState->getId(),
                    "iPermissionId" => $perm_id,
                    "iDescriptorId" => $emptydescriptor->getId(),
                ));
                if (PEAR::isError($assign)) {
                    $this->errorRedirectTo("managepermissions", sprintf(_kt("Failed to create assignment: %s"), $assign->getMessage()));
                }
            }
        }
        
        $this->successRedirectTo("managepermissions", _kt("Controlled permission updated."));
    }

    // == PERMISSIONS
    function do_allocatepermissions() {
        $oTemplate = $this->oValidator->validateTemplate('ktcore/workflow/admin/allocate_permissions');
       
        $oForm = $this->form_managepermissions();       
       
        $aUsefulPermissions = KTPermission::getDocumentRelevantList();
        $aPermissionGrid = array();
        $aStatePermAssigns = KTWorkflowStatePermissionAssignment::getByState($this->oState);
           
        foreach ($aStatePermAssigns as $oPermAssign) {           
            $aPermissionGrid[$oPermAssign->getPermissionId()] = $oPermAssign;
        }
        
        $aPermissionsToJSON = array();
        foreach($aUsefulPermissions as $oP) {
            $perm_id = $oP->getId();
            if ($aPermissionGrid[$perm_id]) {
                $aPermissionsToJSON[] = array('id'=>$oP->getId(), 'name'=>$oP->getHumanName());
            }
        }

        $oJSON = new Services_JSON;
        $sJSONPermissions = $oJSON->encode($aPermissionsToJSON);
        
        $oTemplate->setData(array(
            'context' => $this,
            'perm_grid' => $aPermissionGrid,
            'perms' => $aUsefulPermissions,
            'form' => $oForm,
            'jsonpermissions' => $sJSONPermissions,
            'args' => $this->meldPersistQuery("","setpermissionallocations",true),
        ));
        return $oTemplate->render();
    }    
    
    // JSON helper. from permissions.

    function &_getPermissionsMap() {
        $aStatePermAssigns = KTWorkflowStatePermissionAssignment::getByState($this->oState);
        $aPermissionsMap = array('role'=>array(), 'group'=>array());

        foreach ($aStatePermAssigns as $oPermAssign) {
            $oDescriptor = KTPermissionDescriptor::get($oPermAssign->getDescriptorId());
            $iPermissionId = $oPermAssign->getPermissionId();

	    // groups
            $aGroupIds = $oDescriptor->getGroups();
            foreach ($aGroupIds as $iId) {
                $aPermissionsMap['group'][$iId][$iPermissionId] = true;
            }

	    // roles
            $aRoleIds = $oDescriptor->getRoles();
            foreach ($aRoleIds as $iId) {
                $aPermissionsMap['role'][$iId][$iPermissionId] = true;
            }	    
        }
    	return $aPermissionsMap;
    }

    function json_getEntities($optFilter = null) {
        $sFilter = KTUtil::arrayGet($_REQUEST, 'filter', false);
        if($sFilter == false && $optFilter != null) {
            $sFilter = $optFilter;
        }
    
        $bSelected = KTUtil::arrayGet($_REQUEST, 'selected', false);
    
        $aEntityList = array('off'=>'-- Please filter --');
    
        // get permissions map
        $aPermissionsMap =& $this->_getPermissionsMap();
    
        if($bSelected || $sFilter && trim($sFilter)) {
            if(!$bSelected) {
                $aEntityList = array();
            }
    
            $aGroups = Group::getList(sprintf('name like "%%%s%%"', $sFilter));
            foreach($aGroups as $oGroup) {
                $aPerm = @array_keys($aPermissionsMap['group'][$oGroup->getId()]);
                if(!is_array($aPerm)) {
                    $aPerm = array();
                }

                if($bSelected) {
                    if(count($aPerm))
                        $aEntityList['g'.$oGroup->getId()] = array('type' => 'group',
                                   'display' => 'Group: ' . $oGroup->getName(),
                                   'name' => $oGroup->getName(),
                                   'permissions' => $aPerm,
                                   'id' => $oGroup->getId(),
                                   'selected' => true);
                } else {
                    $aEntityList['g'.$oGroup->getId()] = array('type' => 'group',
                               'display' => 'Group: ' . $oGroup->getName(),
                               'name' => $oGroup->getName(),
                               'permissions' => $aPerm,
                               'id' => $oGroup->getId());
                }                              
            }

            $aRoles = Role::getList(sprintf('name like "%%%s%%"', $sFilter));
            foreach($aRoles as $oRole) {
                $aPerm = @array_keys($aPermissionsMap['role'][$oRole->getId()]);
                if(!is_array($aPerm)) {
                    $aPerm = array();
                }
        
            if($bSelected) {
                if(count($aPerm)) 
                    $aEntityList['r'.$oRole->getId()] = array('type' => 'role',
                                      'display' => 'Role: ' . $oRole->getName(),
                                      'name' => $oRole->getName(),
                                      'permissions' => $aPerm,
                                      'id' => $oRole->getId(),
                                      'selected' => true);
            } else {
                $aEntityList['r'.$oRole->getId()] = array('type' => 'role',
                                      'display' => 'Role: ' . $oRole->getName(),
                                      'name' => $oRole->getName(),
                                      'permissions' => $aPerm,
                                      'id' => $oRole->getId());
            }
            }
        }     
        return $aEntityList;
    }
    

    function do_setpermissionallocations() {
        $aPermissionAllowed = (array) KTUtil::arrayGet($_REQUEST, 'foo'); // thanks BD.
        
        $this->startTransaction();
        
        $aStatePermAssigns = KTWorkflowStatePermissionAssignment::getByState($this->oState);
        
        // we now walk the alloc'd perms, and go.
        foreach ($aStatePermAssigns as $oPermAssign) {
            $aAllowed = (array) $aPermissionAllowed[$oPermAssign->getPermissionId()]; // is already role, group, etc.
            $oDescriptor = KTPermissionUtil::getOrCreateDescriptor($aAllowed);            
            if (PEAR::isError($oDescriptor)) { $this->errorRedirectTo('allocatepermissions', _kt('Failed to allocate as specified.')); } 
            
            $oPermAssign->setDescriptorId($oDescriptor->getId());
            $res = $oPermAssign->update();
            if (PEAR::isError($res)) { $this->errorRedirectTo('allocatepermissions', _kt('Failed to allocate as specified.')); }             
        }
        
        KTPermissionUtil::updatePermissionLookupForState($oState);
        
        $this->successRedirectTo('managepermissions', _kt('Permissions Allocated.'));
    }
    

    // ----------------- Effects ---------------------
    function do_effects() {
        $oTemplate = $this->oValidator->validateTemplate('ktcore/workflow/admin/effects_overview');            
        $this->oPage->setBreadcrumbDetails(_kt("Workflow Effects"));
        
        
        $oTemplate->setData(array(
            'context' => $this,
            'workflow_name' => $this->oWorkflow->getName(),
        ));
        return $oTemplate->render();    
    }    
}

?>