diff --git a/plugins/ktcore/admin/workflow/newworkflow.inc.php b/plugins/ktcore/admin/workflow/newworkflow.inc.php index 4fc4946..e727f4a 100644 --- a/plugins/ktcore/admin/workflow/newworkflow.inc.php +++ b/plugins/ktcore/admin/workflow/newworkflow.inc.php @@ -1,43 +1,43 @@ . - * + * * You can contact The Jam Warehouse Software (Pty) Limited, Unit 1, Tramber Place, * Blake Street, Observatory, 7925 South Africa. 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 + * 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. + * 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 +// 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'); @@ -56,13 +56,13 @@ require_once(KT_LIB_DIR . '/widgets/portlet.inc.php'); require_once(KT_LIB_DIR . '/widgets/forms.inc.php'); class KTNewWorkflowWizard extends KTAdminDispatcher { - function predispatch() { + function predispatch() { $this->persistParams(array('fWizardKey')); } function &form_step1() { $oForm = new KTForm; - + $oForm->setOptions(array( 'action' => 'process_step1', 'cancel_url' => KTUtil::addQueryStringSelf(''), // NBM: is there a cleaner way to reference the parent? @@ -92,70 +92,71 @@ class KTNewWorkflowWizard extends KTAdminDispatcher { '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', - )), + )), array('ktcore.widgets.hidden',array( 'required' => false, 'name' => 'fWizardKey', 'value' => KTUtil::randomString() - )), + )), )); - + $oForm->setValidators(array( array('ktcore.validators.string', array( 'test' => 'workflow_name', - 'output' => 'workflow_name', + 'output' => 'workflow_name', )), array('ktcore.validators.string', array( 'test' => 'fWizardKey', - 'output' => 'fWizardKey', + 'output' => 'fWizardKey', )), array('ktcore.validators.string', array( 'test' => 'states', 'output' => 'states', - 'max_length' => 9999, + 'max_length' => 9999, )), array('ktcore.validators.string', array( 'test' => 'transitions', - 'output' => 'transitions', - 'max_length' => 9999, - )), + 'output' => 'transitions', + 'max_length' => 9999, + )), )); - + return $oForm; } function do_main() { $oTemplate =& $this->oValidator->validateTemplate('ktcore/workflow/admin/new_wizard_step1'); - + $oForm =& $this->form_step1(); - + $oTemplate->setData(array( 'context' => $this, 'form' => $oForm, )); return $oTemplate->render(); } - + function do_process_step1() { - + $fWizardKey = KTUtil::arrayGet($_REQUEST, 'fWizardKey'); if (!empty($fWizardKey)) { $this->errorRedirectToMain(_kt("Could not create workflow.") ); exit; } - + $oForm =& $this->form_step1(); $res = $oForm->validate(); $data = $res['results']; // perform additional validation. $extra_errors = array(); - + + $data['workflow_name'] = str_replace(array(' ', ' '), array(' ', ' '), $data['workflow_name']); $oWorkflow = KTWorkflow::getByName($data['workflow_name']); if (!PEAR::isError($oWorkflow)) { $extra_errors['workflow_name'][] = _kt("A workflow with that name already exists. Please choose a different name for this workflow."); } - + $initial_states = (array) explode("\n", $data['states']); // must be there, we validated it. $failed = array(); $states = array(); @@ -166,7 +167,7 @@ class KTNewWorkflowWizard extends KTAdminDispatcher { if (empty($state_name)) { continue; } - + if ($states[$state_name]) { $failed[] = $state_name; continue; @@ -181,11 +182,11 @@ class KTNewWorkflowWizard extends KTAdminDispatcher { $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)); + $extra_errors['states'] = sprintf(_kt("You cannot have duplicate state names: %s"), implode(', ', $failed)); } $data['states'] = $states; $data['initial_state'] = $initial_state; - + $initial_transitions = (array) explode("\n", $data['transitions']); // must be there, we validated it. $failed = array(); $transitions = array(); @@ -194,38 +195,38 @@ class KTNewWorkflowWizard extends KTAdminDispatcher { if (empty($transition_name)) { continue; } - + if ($transitions[$transition_name]) { $failed[] = $transition_name; continue; } - + $transitions[$transition_name] = $transition_name; } if (!empty($failed)) { - $extra_errors['transitions'] = sprintf(_kt("You cannot have duplicate transition names: %s"), implode(', ', $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); - } - + $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)) @@ -238,22 +239,22 @@ class KTNewWorkflowWizard extends KTAdminDispatcher { 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'); - + + $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'], + 'states' => $wiz_data['states'], )); return $oTemplate->render(); } - + function do_process_step2() { $fWizardKey = KTUtil::arrayGet($_REQUEST, 'fWizardKey'); if (!empty($fWizardKey)) @@ -265,15 +266,15 @@ class KTNewWorkflowWizard extends KTAdminDispatcher { 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. - + // and hopefully never will. + $fToData = (array) KTUtil::arrayGet($_REQUEST, 'fTo'); - $fFromData = (array) KTUtil::arrayGet($_REQUEST, 'fFrom'); - + $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) { @@ -283,7 +284,7 @@ class KTNewWorkflowWizard extends KTAdminDispatcher { } $fTo[$transition] = $candidate; } - + $fFrom = array(); foreach ($wiz_data['transitions'] as $transition) { $d = (array) KTUtil::arrayGet($fFromData, $transition); @@ -298,12 +299,12 @@ class KTNewWorkflowWizard extends KTAdminDispatcher { $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)) @@ -312,23 +313,23 @@ class KTNewWorkflowWizard extends KTAdminDispatcher { 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(); + $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())); } @@ -342,11 +343,11 @@ class KTNewWorkflowWizard extends KTAdminDispatcher { 'humanname' => $state_name, )); if (PEAR::isError($oState)) { - $this->errorRedirectToMain(sprintf(_kt("Failed to create state: %s"), $oState->getMessage())); + $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()); @@ -354,7 +355,7 @@ class KTNewWorkflowWizard extends KTAdminDispatcher { 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) { @@ -369,12 +370,12 @@ class KTNewWorkflowWizard extends KTAdminDispatcher { "GuardPermissionId" => null, "GuardGroupId" => null, "GuardRoleId" => null, - "GuardConditionId" => null, + "GuardConditionId" => null, )); if (PEAR::isError($oTransition)) { - $this->errorRedirectToMain(sprintf(_kt("Failed to create transition: %s"), $oTransition->getMessage())); + $this->errorRedirectToMain(sprintf(_kt("Failed to create transition: %s"), $oTransition->getMessage())); } - + // hook up source states. $state_ids = array(); $sources = (array) $from[$transition]; @@ -384,18 +385,18 @@ class KTNewWorkflowWizard extends KTAdminDispatcher { $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->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);