workflows.php
21.6 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
<?php
require_once(KT_LIB_DIR . '/dispatcher.inc.php');
require_once(KT_LIB_DIR . '/validation/dispatchervalidation.inc.php');
require_once(KT_LIB_DIR . '/templating/templating.inc.php');
require_once(KT_LIB_DIR . '/widgets/fieldWidgets.php');
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 . "/templating/kt3template.inc.php");
require_once(KT_LIB_DIR . '/permissions/permission.inc.php');
require_once(KT_LIB_DIR . '/groups/Group.inc');
require_once(KT_LIB_DIR . '/roles/Role.inc');
require_once(KT_LIB_DIR . '/search/savedsearch.inc.php');
require_once(KT_LIB_DIR . '/actions/documentaction.inc.php');
class KTWorkflowDispatcher extends KTAdminDispatcher {
var $bAutomaticTransaction = true;
// {{{ WORKFLOW HANDLING
// {{{ do_main
function do_main () {
$this->aBreadcrumbs[] = array(
'url' => $_SERVER['PHP_SELF'],
'name' => _('Workflows'),
);
$add_fields = array();
$add_fields[] = new KTStringWidget(_('Name'),_('A human-readable name for the workflow.'), 'fName', null, $this->oPage, true);
$oTemplating =& KTTemplating::getSingleton();
$oTemplate =& $oTemplating->loadTemplate('ktcore/workflow/listWorkflows');
$oTemplate->setData(array(
'context' => $this,
'aWorkflow' => KTWorkflow::getList(),
'add_fields' => $add_fields,
));
return $oTemplate;
}
// }}}
// {{{ do_editWorkflow
function do_editWorkflow() {
$oTemplate =& $this->oValidator->validateTemplate('ktcore/workflow/editWorkflow');
$oWorkflow =& $this->oValidator->validateWorkflow($_REQUEST['fWorkflowId']);
$aStates = KTWorkflowState::getByWorkflow($oWorkflow);
$aPermissions = KTPermission::getList();
$edit_fields = array();
$edit_fields[] = new KTStringWidget(_('Name'), _('A human-readable name for the workflow.'), 'fName', $oWorkflow->getName(), $this->oPage, true);
$aOptions = array();
$vocab = array();
$vocab[0] = 'None';
foreach($aStates as $state) {
$vocab[$state->getId()] = $state->getName();
}
$aOptions['vocab'] = $vocab;
$edit_fields[] = new KTLookupWidget(_('Starting State'), _('When a document has this workflow applied to it, to which state should it initially be set'), 'fStartStateId', $oWorkflow->getStartStateId(), $this->oPage, false, null, null, $aOptions);
$add_state_fields = array();
$add_state_fields[] = new KTStringWidget(_('Name'), _('A human-readable name for the state.'), 'fName', null, $this->oPage, true);
$add_transition_fields = array();
$add_transition_fields[] = new KTStringWidget(_('Name'), _('A human-readable name for the state.'), 'fName', null, $this->oPage, true);
$aOptions = array();
$vocab = array();
foreach($aStates as $state) {
$vocab[$state->getId()] = $state->getName();
}
$aOptions['vocab'] = $vocab;
$add_transition_fields[] = new KTLookupWidget(_('Destination State'), _('Once this transition is complete, which state should the document be in?'), 'fTargetStateId', $oWorkflow->getStartStateId(), $this->oPage, true, null, null, $aOptions);
$aOptions = array();
$vocab = array();
foreach($aPermissions as $permission) {
$vocab[$permission->getId()] = $permission->getHumanName();
}
$aOptions['vocab'] = $vocab;
$add_transition_fields[] = new KTLookupWidget(_('Guard Permission.'), _('Which permission must the user have in order to follow this transition?'), 'fPermissionId', $oWorkflow->getStartStateId(), $this->oPage, true, null, null, $aOptions);
$this->aBreadcrumbs[] = array(
'url' => $_SERVER['PHP_SELF'],
'name' => _('Workflows'),
);
$this->aBreadcrumbs[] = array(
'url' => $_SERVER['PHP_SELF'],
'query' => 'action=editWorkflow&fWorkflowId=' . $oWorkflow->getId(),
'name' => $oWorkflow->getName(),
);
$oTemplate->setData(array(
'oWorkflow' => $oWorkflow,
'aStates' => $aStates,
'aTransitions' => KTWorkflowTransition::getByWorkflow($oWorkflow),
'aPermissions' => $aPermissions,
'aActions' => KTDocumentActionUtil::getAllDocumentActions(),
'aActionsSelected' => KTWorkflowUtil::getControlledActionsForWorkflow($oWorkflow),
// fields
'edit_fields' => $edit_fields,
'add_state_fields' => $add_state_fields,
'add_transition_fields' => $add_transition_fields,
));
return $oTemplate;
}
// }}}
// {{{ do_saveWorkflow
function do_saveWorkflow() {
$oWorkflow =& $this->oValidator->validateWorkflow($_REQUEST['fWorkflowId']);
$aOptions = array(
'redirect_to' => array('editWorkflow', 'fWorkflowId=' . $oWorkflow->getId()),
);
$oWorkflow->setName($_REQUEST['fName']);
$oWorkflow->setHumanName($_REQUEST['fName']);
if (!empty($_REQUEST['fStartStateId'])) {
$oWorkflow->setStartStateId($_REQUEST['fStartStateId']);
} else {
$oWorkflow->setStartStateId(null);
}
$res = $oWorkflow->update();
$this->oValidator->notErrorFalse($res, array(
'redirect_to' => array('editWorkflow', 'fWorkflowId=' . $oWorkflow->getId()),
'message' => _('Error saving workflow'),
));
$this->successRedirectTo('editWorkflow', _('Changes saved'), 'fWorkflowId=' . $oWorkflow->getId());
exit(0);
}
// }}}
// {{{ do_newWorkflow
function do_newWorkflow() {
$aErrorOptions = array(
'redirect_to' => array('main'),
'message' => 'No name given',
);
$sName = KTUtil::arrayGet($_REQUEST, 'fName');
$sName = $this->oValidator->validateString($sName,
$aErrorOptions);
$res = KTWorkflow::createFromArray(array(
'name' => $sName,
'humanname' => $sName,
));
$this->oValidator->notError($res, array(
'redirect_to' => array('main'),
'message' => _('Could not create workflow'),
));
$this->successRedirectTo('editWorkflow', _('Workflow created'), 'fWorkflowId=' . $res->getId());
exit(0);
}
// }}}
// {{{ do_setWorkflowActions
function do_setWorkflowActions() {
$oWorkflow =& $this->oValidator->validateWorkflow($_REQUEST['fWorkflowId']);
$res = KTWorkflowUtil::setControlledActionsForWorkflow($oWorkflow, $_REQUEST['fActions']);
$this->oValidator->notErrorFalse($res, array(
'redirect_to' => array('editWorkflow', 'fWorkflowId=' . $oWorkflow->getId()),
'message' => _('Error saving workflow controlled actions'),
));
$this->successRedirectTo('editWorkflow', _('Changes saved'), 'fWorkflowId=' . $oWorkflow->getId());
exit(0);
}
// }}}
// }}}
// {{{ STATE HANDLING
//
// {{{ do_newState
function do_newState() {
$oWorkflow =& $this->oValidator->validateWorkflow($_REQUEST['fWorkflowId']);
$oState = KTWorkflowState::createFromArray(array(
'workflowid' => $oWorkflow->getId(),
'name' => $_REQUEST['fName'],
'humanname' => $_REQUEST['fName'],
));
$this->oValidator->notError($oState, array(
'redirect_to' => array('editWorkflow', 'fWorkflowId=' . $oWorkflow->getId()),
'message' => _('Could not create workflow state'),
));
$this->successRedirectTo('editState', _('Workflow state created'), 'fWorkflowId=' . $oWorkflow->getId() . '&fStateId=' . $oState->getId());
exit(0);
}
// }}}
// {{{ do_editState
function do_editState() {
$oTemplate =& $this->oValidator->validateTemplate('ktcore/workflow/editState');
$oWorkflow =& $this->oValidator->validateWorkflow($_REQUEST['fWorkflowId']);
$oState =& $this->oValidator->validateWorkflowState($_REQUEST['fStateId']);
$aTransitionsTo =& KTWorkflowTransition::getByTargetState($oState);
$aTransitionIdsTo = array();
foreach ($aTransitionsTo as $oTransition) {
$aTransitionIdsTo[] = $oTransition->getId();
}
$aAllTransitions =& KTWorkflowTransition::getByWorkflow($oWorkflow);
$aTransitions = array();
foreach ($aAllTransitions as $oTransition) {
if (!in_array($oTransition->getId(), $aTransitionIdsTo)) {
$aTransitions[] = $oTransition;
}
}
$aTransitionsSelected = KTWorkflowUtil::getTransitionsFrom($oState, array('ids' => true));
$this->aBreadcrumbs[] = array(
'url' => $_SERVER['PHP_SELF'],
'name' => _('Workflows'),
);
$this->aBreadcrumbs[] = array(
'url' => $_SERVER['PHP_SELF'],
'query' => 'action=editWorkflow&fWorkflowId=' . $oWorkflow->getId(),
'name' => $oWorkflow->getName(),
);
$this->oPage->setBreadcrumbDetails(_('state') . ': ' . $oState->getName());
$aInformed = KTWorkflowUtil::getInformedForState($oState);
$oTemplate->setData(array(
'oWorkflow' => $oWorkflow,
'oState' => $oState,
'oNotifyRole' => $oRole,
'aTransitionsTo' => $aTransitionsTo,
'aTransitions' => $aTransitions,
'aTransitionsSelected' => $aTransitionsSelected,
'aActions' => KTDocumentActionUtil::getDocumentActionsByNames(KTWorkflowUtil::getControlledActionsForWorkflow($oWorkflow)),
'aActionsSelected' => KTWorkflowUtil::getEnabledActionsForState($oState),
'aGroups' => Group::getList(),
'aRoles' => Role::getList(),
'aUsers' => User::getList(),
'aInformed' => $aInformed,
));
return $oTemplate;
}
// }}}
// {{{ do_saveState
function do_saveState() {
$oWorkflow =& $this->oValidator->validateWorkflow($_REQUEST['fWorkflowId']);
$oState =& $this->oValidator->validateWorkflowState($_REQUEST['fStateId']);
$oState->setName($_REQUEST['fName']);
$oState->setHumanName($_REQUEST['fName']);
$res = $oState->update();
$this->oValidator->notErrorFalse($res, array(
'redirect_to' => array('editState', 'fWorkflowId=' . $oWorkflow->getId() . '&fStateId=' . $oState->getId()),
'message' => _('Error saving state'),
));
$this->successRedirectTo('editState', _('Changes saved'), 'fWorkflowId=' . $oWorkflow->getId() . '&fStateId=' . $oState->getId());
exit(0);
}
// }}}
// {{{ do_saveTransitions
function do_saveTransitions() {
$oWorkflow =& $this->oValidator->validateWorkflow($_REQUEST['fWorkflowId']);
$oState =& $this->oValidator->validateWorkflowState($_REQUEST['fStateId']);
$res = KTWorkflowUtil::saveTransitionsFrom($oState, $_REQUEST['fTransitionIds']);
$this->oValidator->notErrorFalse($res, array(
'redirect_to' => array('editState', 'fWorkflowId=' . $oWorkflow->getId() . '&fStateId=' . $oState->getId()),
'message' => _('Error saving transitions'),
));
$this->successRedirectTo('editState', _('Changes saved'), 'fWorkflowId=' . $oWorkflow->getId() . '&fStateId=' . $oState->getId());
exit(0);
}
// }}}
// {{{ do_setStateActions
function do_setStateActions() {
$oWorkflow =& $this->oValidator->validateWorkflow($_REQUEST['fWorkflowId']);
$oState =& $this->oValidator->validateWorkflowState($_REQUEST['fStateId']);
$res = KTWorkflowUtil::setEnabledActionsForState($oState, $_REQUEST['fActions']);
$this->oValidator->notErrorFalse($res, array(
'redirect_to' => array('editState', 'fWorkflowId=' . $oWorkflow->getId(), '&fStateId=' . $oState->getId()),
'message' => _('Error saving state enabled actions'),
));
$this->successRedirectTo('editState', _('Actions set'), 'fWorkflowId=' . $oWorkflow->getId() . '&fStateId=' . $oState->getId());
exit(0);
}
// }}}
// {{{ do_saveInform
function do_saveInform() {
$oWorkflow =& $this->oValidator->validateWorkflow($_REQUEST['fWorkflowId']);
$oState =& $this->oValidator->validateWorkflowState($_REQUEST['fStateId']);
$sTargetAction = 'editState';
$sTargetParams = 'fWorkflowId=' . $oWorkflow->getId() . '&fStateId=' . $oState->getId();
$aRoleIds = KTUtil::arrayGet($_REQUEST, 'fRoleIds');
if (empty($aRoleIds)) {
$aRoleIds = array();
}
if (!is_array($aRoleIds)) {
$this->errorRedirectTo($sTargetAction, _('Invalid roles specified'), $sTargetParams);
}
$aGroupIds = KTUtil::arrayGet($_REQUEST, 'fGroupIds');
if (empty($aGroupIds)) {
$aGroupIds = array();
}
if (!is_array($aGroupIds)) {
$this->errorRedirectTo($sTargetAction, _('Invalid groups specified'), $sTargetParams);
}
$aUserIds = KTUtil::arrayGet($_REQUEST, 'fUserIds');
if (empty($aUserIds)) {
$aUserIds = array();
}
if (!is_array($aUserIds)) {
$this->errorRedirectTo($sTargetAction, _('Invalid users specified'), $sTargetParams);
}
$aAllowed = array(
'role' => $aRoleIds,
'group' => $aGroupIds,
'user' => $aUserIds,
);
KTWorkflowUtil::setInformedForState($oState, $aAllowed);
$this->successRedirectTo($sTargetAction, _('Changes saved'), $sTargetParams);
}
// }}}
// }}}
// {{{ TRANSITION HANDLING
//
// {{{ do_newTransition
function do_newTransition() {
$oWorkflow =& $this->oValidator->validateWorkflow($_REQUEST['fWorkflowId']);
$oState =& $this->oValidator->validateWorkflowState($_REQUEST['fTargetStateId']);
$iPermissionId = KTUtil::arrayGet($_REQUEST, 'fPermissionId');
$iGroupId = KTUtil::arrayGet($_REQUEST, 'fGroupId');
$iRoleId = KTUtil::arrayGet($_REQUEST, 'fRoleId');
if ($iPermissionId) {
$this->oValidator->validatePermission($_REQUEST['fPermissionId']);
}
if ($iGroupId) {
$this->oValidator->validateGroup($_REQUEST['fGroupId']);
}
if ($iRoleId) {
$this->oValidator->validateRole($_REQUEST['fRoleId']);
}
$res = KTWorkflowTransition::createFromArray(array(
'workflowid' => $oWorkflow->getId(),
'name' => $_REQUEST['fName'],
'humanname' => $_REQUEST['fName'],
'targetstateid' => $oState->getId(),
'guardpermissionid' => $iPermissionId,
'guardgroupid' => $iGroupId,
'guardroleid' => $iRoleId,
));
$this->oValidator->notError($res, array(
'redirect_to' => array('editWorkflow', 'fWorkflowId=' . $oWorkflow->getId()),
'message' => _('Could not create workflow transition'),
));
$this->successRedirectTo('editWorkflow', _('Workflow transition created'), 'fWorkflowId=' . $oWorkflow->getId());
exit(0);
}
// }}}
// {{{ do_editTransition
function do_editTransition() {
$oTemplate =& $this->oValidator->validateTemplate('ktcore/workflow/editTransition');
$oWorkflow =& $this->oValidator->validateWorkflow($_REQUEST['fWorkflowId']);
$oTransition =& $this->oValidator->validateWorkflowTransition($_REQUEST['fTransitionId']);
$aStates = KTWorkflowState::getByWorkflow($oWorkflow);
$aPermissions = KTPermission::getList();
$aGroups = Group::getList();
$aRoles = Role::getList();
$aConditions = KTSavedSearch::getConditions();
$edit_fields = array();
$edit_fields[] = new KTStringWidget(_('Name'), _('A human-readable name for the state.'), 'fName', $oTransition->getName(), $this->oPage, true);
$aOptions = array();
$vocab = array();
foreach($aStates as $state) {
$vocab[$state->getId()] = $state->getName();
}
$aOptions['vocab'] = $vocab;
$edit_fields[] = new KTLookupWidget(_('Destination State'), _('Once this transition is complete, which state should the document be in?'), 'fTargetStateId', $oTransition->getTargetStateId(), $this->oPage, true, null, null, $aOptions);
$aOptions = array();
$vocab = array();
$vocab[0] = 'None';
foreach($aPermissions as $permission) {
$vocab[$permission->getId()] = $permission->getHumanName();
}
$aOptions['vocab'] = $vocab;
$edit_fields[] = new KTLookupWidget(_('Guard Permission.'), _('Which permission must the user have in order to follow this transition?'), 'fPermissionId', $oTransition->getGuardPermissionId(), $this->oPage, true, null, null, $aOptions);
$aOptions = array();
$vocab = array();
$vocab[0] = 'None';
foreach($aGroups as $group) {
$vocab[$group->getId()] = $group->getName();
}
$aOptions['vocab'] = $vocab;
$edit_fields[] = new KTLookupWidget(_('Guard Group.'), _('Which group must the user belong to in order to follow this transition?'), 'fGroupId', $oTransition->getGuardGroupId(), $this->oPage, false, null, null, $aOptions);
$aOptions = array();
$vocab = array();
$vocab[0] = 'None';
foreach($aRoles as $role) {
$vocab[$role->getId()] = $role->getName();
}
$aOptions['vocab'] = $vocab;
$edit_fields[] = new KTLookupWidget(_('Guard Role.'), _('Which role must the user have in order to follow this transition?'), 'fRoleId', $oTransition->getGuardRoleId(), $this->oPage, false, null, null, $aOptions);
if (!empty($aConditions)) {
$aOptions = array();
$vocab = array();
$vocab[0] = 'None';
foreach($aConditions as $condition) {
$vocab[$condition->getId()] = $condition->getName();
}
$aOptions['vocab'] = $vocab;
$edit_fields[] = new KTLookupWidget(_('Guard Condition.'), _('Which condition (stored search) must be satisfied before the transition can take place?'), 'fConditionId', $oTransition->getGuardConditionId(), $this->oPage, false, null, null, $aOptions);
}
$this->aBreadcrumbs[] = array(
'url' => $_SERVER['PHP_SELF'],
'name' => _('Workflows'),
);
$this->aBreadcrumbs[] = array(
'url' => $_SERVER['PHP_SELF'],
'query' => 'action=editWorkflow&fWorkflowId=' . $oWorkflow->getId(),
'name' => $oWorkflow->getName(),
);
$this->aBreadcrumbs[] = array(
'url' => $_SERVER['PHP_SELF'],
'query' => 'action=editTransition&fWorkflowId=' . $oWorkflow->getId() . '&fTransitionId=' . $oTransition->getId(),
'name' => $oTransition->getName(),
);
$oTemplate->setData(array(
'oWorkflow' => $oWorkflow,
'oTransition' => $oTransition,
'aStates' => $aStates,
'aPermissions' => $aPermissions,
'aGroups' => $aGroups,
'aRoles' => $aRoles,
'aConditions' => $aConditions,
// fields
'edit_fields' => $edit_fields,
));
return $oTemplate;
}
// }}}
// {{{ do_saveTransition
function do_saveTransition() {
$aRequest = $this->oValidator->validateDict($_REQUEST, array(
'fWorkflowId' => array('type' => 'workflow'),
'fTransitionId' => array('type' => 'workflowtransition'),
));
$oWorkflow =& $this->oValidator->validateWorkflow($_REQUEST['fWorkflowId']);
$oTransition =& $this->oValidator->validateWorkflowTransition($_REQUEST['fTransitionId']);
$oState =& $this->oValidator->validateWorkflowState($_REQUEST['fTargetStateId']);
$iPermissionId = KTUtil::arrayGet($_REQUEST, 'fPermissionId', null);
$iGroupId = KTUtil::arrayGet($_REQUEST, 'fGroupId', null);
$iRoleId = KTUtil::arrayGet($_REQUEST, 'fRoleId', null);
$iConditionId = KTUtil::arrayGet($_REQUEST, 'fConditionId', null);
if ($iPermissionId) {
$this->oValidator->validatePermission($_REQUEST['fPermissionId']);
}
if ($iGroupId) {
$this->oValidator->validateGroup($_REQUEST['fGroupId']);
}
if ($iRoleId) {
$this->oValidator->validateRole($_REQUEST['fRoleId']);
}
if ($iConditionId) {
$this->oValidator->validateCondition($_REQUEST['fConditionId']);
}
$oTransition->updateFromArray(array(
'workflowid' => $oWorkflow->getId(),
'name' => $_REQUEST['fName'],
'humanname' => $_REQUEST['fName'],
'targetstateid' => $oState->getId(),
'guardpermissionid' => $iPermissionId,
'guardgroupid' => $iGroupId,
'guardroleid' => $iRoleId,
'guardconditionid' => $iConditionId,
));
$res = $oTransition->update();
$this->oValidator->notErrorFalse($res, array(
'redirect_to' => array('editTransition', 'fWorkflowId=' . $oWorkflow->getId() . '&fTransitionId=' . $oTransition->getId()),
'message' => _('Error saving transition'),
));
$this->successRedirectTo('editTransition', _('Changes saved'), 'fWorkflowId=' . $oWorkflow->getId() . '&fTransitionId=' . $oTransition->getId());
exit(0);
}
// }}}
// }}}
}
?>