Commit 6741e8eee5d312d2b45fe4aa6ff8c5e575f509a1

Authored by megan_w
1 parent 78582561

KTS-3381

"Two workflows with the same name can be created"
Fixed. Removed extra spacing from workflow names.

Committed by: Megan Watson
Reviewed by: Conrad Vermeulen



git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/STABLE/branches/3.5.2c-Release-Branch@8490 c91229c3-7414-0410-bfa2-8a42b809f60b
plugins/ktcore/admin/workflow/newworkflow.inc.php
1 1 <?php
2 2 /**
3 3 * $Id$
4   - *
  4 + *
5 5 * KnowledgeTree Open Source Edition
6 6 * Document Management Made Simple
7 7 * Copyright (C) 2004 - 2008 The Jam Warehouse Software (Pty) Limited
8   - *
  8 + *
9 9 * This program is free software; you can redistribute it and/or modify it under
10 10 * the terms of the GNU General Public License version 3 as published by the
11 11 * Free Software Foundation.
12   - *
  12 + *
13 13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 16 * details.
17   - *
  17 + *
18 18 * You should have received a copy of the GNU General Public License
19 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20   - *
  20 + *
21 21 * You can contact The Jam Warehouse Software (Pty) Limited, Unit 1, Tramber Place,
22 22 * Blake Street, Observatory, 7925 South Africa. or email info@knowledgetree.com.
23   - *
  23 + *
24 24 * The interactive user interfaces in modified source and object code versions
25 25 * of this program must display Appropriate Legal Notices, as required under
26 26 * Section 5 of the GNU General Public License version 3.
27   - *
  27 + *
28 28 * In accordance with Section 7(b) of the GNU General Public License version 3,
29 29 * these Appropriate Legal Notices must retain the display of the "Powered by
30   - * KnowledgeTree" logo and retain the original copyright notice. If the display of the
  30 + * KnowledgeTree" logo and retain the original copyright notice. If the display of the
31 31 * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
32   - * must display the words "Powered by KnowledgeTree" and retain the original
33   - * copyright notice.
  32 + * must display the words "Powered by KnowledgeTree" and retain the original
  33 + * copyright notice.
34 34 * Contributor( s): ______________________________________
35 35 */
36 36  
37 37 // core
38 38 require_once(KT_LIB_DIR . "/dispatcher.inc.php");
39 39  
40   -// workflow
  40 +// workflow
41 41 require_once(KT_LIB_DIR . '/workflow/workflow.inc.php');
42 42 require_once(KT_LIB_DIR . '/workflow/workflowstate.inc.php');
43 43 require_once(KT_LIB_DIR . '/workflow/workflowtransition.inc.php');
... ... @@ -56,13 +56,13 @@ require_once(KT_LIB_DIR . &#39;/widgets/portlet.inc.php&#39;);
56 56 require_once(KT_LIB_DIR . '/widgets/forms.inc.php');
57 57  
58 58 class KTNewWorkflowWizard extends KTAdminDispatcher {
59   - function predispatch() {
  59 + function predispatch() {
60 60 $this->persistParams(array('fWizardKey'));
61 61 }
62 62  
63 63 function &form_step1() {
64 64 $oForm = new KTForm;
65   -
  65 +
66 66 $oForm->setOptions(array(
67 67 'action' => 'process_step1',
68 68 'cancel_url' => KTUtil::addQueryStringSelf(''), // NBM: is there a cleaner way to reference the parent?
... ... @@ -92,70 +92,71 @@ class KTNewWorkflowWizard extends KTAdminDispatcher {
92 92 '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.'),
93 93 'required' => false,
94 94 'name' => 'transitions',
95   - )),
  95 + )),
96 96 array('ktcore.widgets.hidden',array(
97 97 'required' => false,
98 98 'name' => 'fWizardKey',
99 99 'value' => KTUtil::randomString()
100   - )),
  100 + )),
101 101 ));
102   -
  102 +
103 103 $oForm->setValidators(array(
104 104 array('ktcore.validators.string', array(
105 105 'test' => 'workflow_name',
106   - 'output' => 'workflow_name',
  106 + 'output' => 'workflow_name',
107 107 )),
108 108 array('ktcore.validators.string', array(
109 109 'test' => 'fWizardKey',
110   - 'output' => 'fWizardKey',
  110 + 'output' => 'fWizardKey',
111 111 )),
112 112 array('ktcore.validators.string', array(
113 113 'test' => 'states',
114 114 'output' => 'states',
115   - 'max_length' => 9999,
  115 + 'max_length' => 9999,
116 116 )),
117 117 array('ktcore.validators.string', array(
118 118 'test' => 'transitions',
119   - 'output' => 'transitions',
120   - 'max_length' => 9999,
121   - )),
  119 + 'output' => 'transitions',
  120 + 'max_length' => 9999,
  121 + )),
122 122 ));
123   -
  123 +
124 124 return $oForm;
125 125 }
126 126  
127 127 function do_main() {
128 128 $oTemplate =& $this->oValidator->validateTemplate('ktcore/workflow/admin/new_wizard_step1');
129   -
  129 +
130 130 $oForm =& $this->form_step1();
131   -
  131 +
132 132 $oTemplate->setData(array(
133 133 'context' => $this,
134 134 'form' => $oForm,
135 135 ));
136 136 return $oTemplate->render();
137 137 }
138   -
  138 +
139 139 function do_process_step1() {
140   -
  140 +
141 141 $fWizardKey = KTUtil::arrayGet($_REQUEST, 'fWizardKey');
142 142 if (!empty($fWizardKey))
143 143 {
144 144 $this->errorRedirectToMain(_kt("Could not create workflow.") );
145 145 exit;
146 146 }
147   -
  147 +
148 148 $oForm =& $this->form_step1();
149 149 $res = $oForm->validate();
150 150 $data = $res['results'];
151 151 // perform additional validation.
152 152 $extra_errors = array();
153   -
  153 +
  154 + $data['workflow_name'] = str_replace(array(' ', ' '), array(' ', ' '), $data['workflow_name']);
154 155 $oWorkflow = KTWorkflow::getByName($data['workflow_name']);
155 156 if (!PEAR::isError($oWorkflow)) {
156 157 $extra_errors['workflow_name'][] = _kt("A workflow with that name already exists. Please choose a different name for this workflow.");
157 158 }
158   -
  159 +
159 160 $initial_states = (array) explode("\n", $data['states']); // must be there, we validated it.
160 161 $failed = array();
161 162 $states = array();
... ... @@ -166,7 +167,7 @@ class KTNewWorkflowWizard extends KTAdminDispatcher {
166 167 if (empty($state_name)) {
167 168 continue;
168 169 }
169   -
  170 +
170 171 if ($states[$state_name]) {
171 172 $failed[] = $state_name;
172 173 continue;
... ... @@ -181,11 +182,11 @@ class KTNewWorkflowWizard extends KTAdminDispatcher {
181 182 $extra_errors['states'][] = _kt('You must provide at least one state name.');
182 183 }
183 184 if (!empty($failed)) {
184   - $extra_errors['states'] = sprintf(_kt("You cannot have duplicate state names: %s"), implode(', ', $failed));
  185 + $extra_errors['states'] = sprintf(_kt("You cannot have duplicate state names: %s"), implode(', ', $failed));
185 186 }
186 187 $data['states'] = $states;
187 188 $data['initial_state'] = $initial_state;
188   -
  189 +
189 190 $initial_transitions = (array) explode("\n", $data['transitions']); // must be there, we validated it.
190 191 $failed = array();
191 192 $transitions = array();
... ... @@ -194,38 +195,38 @@ class KTNewWorkflowWizard extends KTAdminDispatcher {
194 195 if (empty($transition_name)) {
195 196 continue;
196 197 }
197   -
  198 +
198 199 if ($transitions[$transition_name]) {
199 200 $failed[] = $transition_name;
200 201 continue;
201 202 }
202   -
  203 +
203 204 $transitions[$transition_name] = $transition_name;
204 205 }
205 206  
206 207 if (!empty($failed)) {
207   - $extra_errors['transitions'] = sprintf(_kt("You cannot have duplicate transition names: %s"), implode(', ', $failed));
  208 + $extra_errors['transitions'] = sprintf(_kt("You cannot have duplicate transition names: %s"), implode(', ', $failed));
208 209 }
209 210 $data['transitions'] = $transitions;
210 211  
211 212 // handle errors.
212 213 if (!empty($res['errors']) || !empty($extra_errors)) {
213   - $oForm->handleError(null, $extra_errors);
214   - }
215   -
  214 + $oForm->handleError(null, $extra_errors);
  215 + }
  216 +
216 217 // store the data for a while.
217   -
  218 +
218 219 $wiz_data = (array) $_SESSION['_wiz_data'];
219 220 $wiz_data[$fWizardKey] = $data;
220 221 $_SESSION['_wiz_data'] =& $wiz_data;
221   -
  222 +
222 223 if (empty($data['transitions'])) {
223 224 return $this->finalise(); // finish and go.
224 225 }
225   -
  226 +
226 227 $this->successRedirectTo("step2",_kt("Initial data stored."));
227 228 }
228   -
  229 +
229 230 function do_step2() {
230 231 $fWizardKey = KTUtil::arrayGet($_REQUEST, 'fWizardKey');
231 232 if (!empty($fWizardKey))
... ... @@ -238,22 +239,22 @@ class KTNewWorkflowWizard extends KTAdminDispatcher {
238 239 if (empty($wiz_data)) {
239 240 $this->errorRedirectToMain(_kt("Unable to find previous value. Please try again."));
240 241 }
241   -
242   - $oTemplate = $this->oValidator->validateTemplate('ktcore/workflow/admin/new_wizard_step2');
243   -
  242 +
  243 + $oTemplate = $this->oValidator->validateTemplate('ktcore/workflow/admin/new_wizard_step2');
  244 +
244 245 $transitions = (array) $wiz_data['transitions'];
245 246 $args = $this->meldPersistQuery("", "process_step2", true);
246   -
  247 +
247 248 $oTemplate->setData(array(
248 249 'context' => $this,
249 250 'fWizardKey'=>$fWizardKey,
250 251 'args' => $args,
251 252 'transitions' => $wiz_data['transitions'],
252   - 'states' => $wiz_data['states'],
  253 + 'states' => $wiz_data['states'],
253 254 ));
254 255 return $oTemplate->render();
255 256 }
256   -
  257 +
257 258 function do_process_step2() {
258 259 $fWizardKey = KTUtil::arrayGet($_REQUEST, 'fWizardKey');
259 260 if (!empty($fWizardKey))
... ... @@ -265,15 +266,15 @@ class KTNewWorkflowWizard extends KTAdminDispatcher {
265 266 if (empty($wiz_data)) {
266 267 $this->errorRedirectToMain(_kt("Unable to locate stored data. Please try again."));
267 268 }
268   -
  269 +
269 270 // we can't use the form "stuff" here since we don't have a grid widget yet
270   - // and hopefully never will.
271   -
  271 + // and hopefully never will.
  272 +
272 273 $fToData = (array) KTUtil::arrayGet($_REQUEST, 'fTo');
273   - $fFromData = (array) KTUtil::arrayGet($_REQUEST, 'fFrom');
274   -
  274 + $fFromData = (array) KTUtil::arrayGet($_REQUEST, 'fFrom');
  275 +
275 276 // these are data[transition][state] = true
276   -
  277 +
277 278 $fTo = array();
278 279 $initial_state = $wiz_data['initial_state'];
279 280 foreach ($wiz_data['transitions'] as $transition) {
... ... @@ -283,7 +284,7 @@ class KTNewWorkflowWizard extends KTAdminDispatcher {
283 284 }
284 285 $fTo[$transition] = $candidate;
285 286 }
286   -
  287 +
287 288 $fFrom = array();
288 289 foreach ($wiz_data['transitions'] as $transition) {
289 290 $d = (array) KTUtil::arrayGet($fFromData, $transition);
... ... @@ -298,12 +299,12 @@ class KTNewWorkflowWizard extends KTAdminDispatcher {
298 299  
299 300 $wiz_data['from'] = $fFrom;
300 301 $wiz_data['to'] = $fTo;
301   -
  302 +
302 303 $_SESSION['_wiz_data'][$fWizardKey] = $wiz_data;
303   -
  304 +
304 305 return $this->finalise();
305 306 }
306   -
  307 +
307 308 function finalise() {
308 309 $fWizardKey = KTUtil::arrayGet($_REQUEST, 'fWizardKey');
309 310 if (!empty($fWizardKey))
... ... @@ -312,23 +313,23 @@ class KTNewWorkflowWizard extends KTAdminDispatcher {
312 313 exit;
313 314 }
314 315 $wiz_data = $_SESSION['_wiz_data'][$fWizardKey];
315   -
  316 +
316 317 // gather all our data. we're sure this is all good and healthy.
317   -
  318 +
318 319 $states = $wiz_data['states'];
319 320 $transitions = $wiz_data['transitions'];
320   - $from = $wiz_data['from'];
321   - $to = $wiz_data['to'];
322   - $initial_state = $wiz_data['initial_state'];
323   - $workflow_name = $wiz_data['workflow_name'];
324   -
325   - $this->startTransaction();
  321 + $from = $wiz_data['from'];
  322 + $to = $wiz_data['to'];
  323 + $initial_state = $wiz_data['initial_state'];
  324 + $workflow_name = $wiz_data['workflow_name'];
  325 +
  326 + $this->startTransaction();
326 327 // create the initial workflow
327 328 $oWorkflow = KTWorkflow::createFromArray(array(
328 329 'name' => $workflow_name,
329 330 'humanname' => $workflow_name,
330 331 'enabled' => true,
331   - ));
  332 + ));
332 333 if (PEAR::isError($oWorkflow)) {
333 334 $this->errorRedirectToMain(sprintf(_kt("Failed to create workflow: %s"), $oWorkflow->getMessage()));
334 335 }
... ... @@ -342,11 +343,11 @@ class KTNewWorkflowWizard extends KTAdminDispatcher {
342 343 'humanname' => $state_name,
343 344 ));
344 345 if (PEAR::isError($oState)) {
345   - $this->errorRedirectToMain(sprintf(_kt("Failed to create state: %s"), $oState->getMessage()));
  346 + $this->errorRedirectToMain(sprintf(_kt("Failed to create state: %s"), $oState->getMessage()));
346 347 }
347 348 $aStates[$state_name] = $oState;
348 349 }
349   -
  350 +
350 351 // update the initial state on workflow
351 352 $oInitialState = $aStates[$initial_state];
352 353 $oWorkflow->setStartStateId($oInitialState->getId());
... ... @@ -354,7 +355,7 @@ class KTNewWorkflowWizard extends KTAdminDispatcher {
354 355 if (PEAR::isError($res)) {
355 356 $this->errorRedirectToMain(sprintf(_kt("Failed to update workflow: %s"), $res->getMessage()));
356 357 }
357   -
  358 +
358 359 // next, we create and hook up the transitions.
359 360 $aTransitions = array();
360 361 foreach ($transitions as $transition) {
... ... @@ -369,12 +370,12 @@ class KTNewWorkflowWizard extends KTAdminDispatcher {
369 370 "GuardPermissionId" => null,
370 371 "GuardGroupId" => null,
371 372 "GuardRoleId" => null,
372   - "GuardConditionId" => null,
  373 + "GuardConditionId" => null,
373 374 ));
374 375 if (PEAR::isError($oTransition)) {
375   - $this->errorRedirectToMain(sprintf(_kt("Failed to create transition: %s"), $oTransition->getMessage()));
  376 + $this->errorRedirectToMain(sprintf(_kt("Failed to create transition: %s"), $oTransition->getMessage()));
376 377 }
377   -
  378 +
378 379 // hook up source states.
379 380 $state_ids = array();
380 381 $sources = (array) $from[$transition];
... ... @@ -384,18 +385,18 @@ class KTNewWorkflowWizard extends KTAdminDispatcher {
384 385 $oState = $aStates[$state_name];
385 386 $state_ids[] = $oState->getId();
386 387 }
387   -
  388 +
388 389 $res = KTWorkflowAdminUtil::saveTransitionSources($oTransition, $state_ids);
389 390 if (PEAR::isError($res)) {
390   - $this->errorRedirectToMain(sprintf(_kt("Failed to set transition origins: %s"), $res->getMessage()));
  391 + $this->errorRedirectToMain(sprintf(_kt("Failed to set transition origins: %s"), $res->getMessage()));
391 392 }
392 393 }
393 394  
394 395 $this->commitTransaction();
395   -
  396 +
396 397 // finally, we want to redirect the user to the parent dispatcher somehow.
397 398 // FIXME nbm: how do you recommend we do this?
398   -
  399 +
399 400 $base = $_SERVER['PHP_SELF'];
400 401 $qs = sprintf("action=view&fWorkflowId=%d",$oWorkflow->getId());
401 402 $url = KTUtil::addQueryString($base, $qs);
... ...