Commit 3579c7f12a495f33d429db1ba4911f51a6b0d129
1 parent
d02a51e6
KTS-1266
"duplicate workflows. " Fixed. Reviewed By: Conrad git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@6565 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
3 changed files
with
227 additions
and
1 deletions
plugins/ktcore/admin/workflowsv2.php
| @@ -122,6 +122,198 @@ class KTWorkflowAdminV2 extends KTAdminDispatcher { | @@ -122,6 +122,198 @@ class KTWorkflowAdminV2 extends KTAdminDispatcher { | ||
| 122 | return $oTemplate->render(); | 122 | return $oTemplate->render(); |
| 123 | } | 123 | } |
| 124 | 124 | ||
| 125 | + function do_branchConfirm() { | ||
| 126 | + $submit = KTUtil::arrayGet($_REQUEST, 'submit' , array()); | ||
| 127 | + if (array_key_exists('copy',$submit)) { | ||
| 128 | + $selection = KTUtil::arrayGet($_REQUEST, 'workflowSelect' , array()); | ||
| 129 | + if(empty($selection)){ | ||
| 130 | + $this->errorRedirectToMain(_kt('No workflow selected.')); | ||
| 131 | + } | ||
| 132 | + return $this->do_copy(); | ||
| 133 | + } | ||
| 134 | + if (array_key_exists('confirmCopy',$submit)) { | ||
| 135 | + $workflowId = KTUtil::arrayGet($_REQUEST, 'workflowId' , array()); | ||
| 136 | + if(empty($workflowId)){ | ||
| 137 | + $this->errorRedirectToMain(_kt('An unexpected error has occured.')); | ||
| 138 | + } | ||
| 139 | + return $this->do_confirmCopy(); | ||
| 140 | + } | ||
| 141 | + $this->errorRedirectToMain(_kt('No action specified.')); | ||
| 142 | + } | ||
| 143 | + | ||
| 144 | + function do_copy() { | ||
| 145 | + $this->aBreadcrumbs[] = array('url' => $_SERVER['PHP_SELF'], 'name' => _kt('Copy Workflow')); | ||
| 146 | + $selection = KTUtil::arrayGet($_REQUEST, 'workflowSelect' , array()); | ||
| 147 | + $this->oPage->setTitle('Copy Workflow'); | ||
| 148 | + | ||
| 149 | + // get selected workflow from database | ||
| 150 | + $oSelWorkflow = KTWorkflow::get($selection); | ||
| 151 | + | ||
| 152 | + $oTemplating =& KTTemplating::getSingleton(); | ||
| 153 | + $oTemplate = $oTemplating->loadTemplate('ktcore/workflow/admin/copy'); | ||
| 154 | + $oTemplate->setData(array( | ||
| 155 | + 'context' => $this, | ||
| 156 | + 'workFlowName' => $oSelWorkflow->getName(), | ||
| 157 | + 'workFlowId' => $oSelWorkflow->getId(), | ||
| 158 | + | ||
| 159 | + )); | ||
| 160 | + return $oTemplate; | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + function do_confirmCopy(){ | ||
| 164 | + $oSelWorkflow = KTWorkflow::get(KTUtil::arrayGet($_REQUEST, 'workflowId' , array())); | ||
| 165 | + $sWorkflowName = KTUtil::arrayGet($_REQUEST, 'workflowName' , array()); | ||
| 166 | + // create the initial workflow | ||
| 167 | + $oNewWorkflow = KTWorkflow::createFromArray(array( | ||
| 168 | + 'name' => $sWorkflowName, | ||
| 169 | + 'humanname' => $sWorkflowName, | ||
| 170 | + 'enabled' => true, | ||
| 171 | + )); | ||
| 172 | + | ||
| 173 | + // get selected workflow states from database | ||
| 174 | + $oSelWorkflowStates = KTWorkflowState::getByWorkflow($oSelWorkflow); | ||
| 175 | + | ||
| 176 | + // array to store map of old and new states | ||
| 177 | + $aStatesMap = array(); | ||
| 178 | + | ||
| 179 | + // create new states and build old-to-new map | ||
| 180 | + foreach ($oSelWorkflowStates as $oOldState) { | ||
| 181 | + $oNewState = KTWorkflowState::createFromArray(array( | ||
| 182 | + 'workflowid' => $oNewWorkflow->getId(), | ||
| 183 | + 'name' => $oOldState->getName(), | ||
| 184 | + 'humanname' => $oOldState->getName(), | ||
| 185 | + )); | ||
| 186 | + $aStatesMap[oldId][] = $oOldState->getId(); | ||
| 187 | + $aStatesMap[newId][] = $oNewState->getId(); | ||
| 188 | + if (PEAR::isError($oNewState)) { | ||
| 189 | + $oForm->errorRedirectToMain(sprintf(_kt("Unexpected failure cloning state: %s"), $oNewState->getMessage())); | ||
| 190 | + } | ||
| 191 | + | ||
| 192 | + // Get all state permission assignments for old workflow transitions and copy for copied workflow state permission assignments | ||
| 193 | + $aPermissionAssignments = KTWorkflowStatePermissionAssignment::getByState($oOldState); | ||
| 194 | + if(count($aPermissionAssignments) > 0){ | ||
| 195 | + foreach ($aPermissionAssignments as $oPermAssign) { | ||
| 196 | + for($i=0;$i<count($aStatesMap[oldId]);$i++){ | ||
| 197 | + if($aStatesMap[oldId][$i] == $oPermAssign->getStateId()){ | ||
| 198 | + $iStateId = $aStatesMap[newId][$i]; | ||
| 199 | + | ||
| 200 | + $res = KTWorkflowStatePermissionAssignment::createFromArray(array( | ||
| 201 | + 'iStateId' => $iStateId, | ||
| 202 | + 'iPermissionId' => $oPermAssign->getPermissionId(), | ||
| 203 | + 'iDescriptorId' => $oPermAssign->getDescriptorId(), | ||
| 204 | + )); | ||
| 205 | + | ||
| 206 | + if (PEAR::isError($res)) { | ||
| 207 | + return $this->errorRedirectToMain(sprintf(_kt("Unable to copy state permission assignment: %s"), $res->getMessage())); | ||
| 208 | + } | ||
| 209 | + } | ||
| 210 | + } | ||
| 211 | + } | ||
| 212 | + } | ||
| 213 | + | ||
| 214 | + // Copy all disabled actions for states | ||
| 215 | + $aDisabled = KTWorkflowUtil::getDisabledActionsForState($oOldState); | ||
| 216 | + $res = KTWorkflowUtil::setDisabledActionsForState($oNewState, $aDisabled); | ||
| 217 | + | ||
| 218 | + // Copy all enabled actions for states | ||
| 219 | + $aDisabled = KTWorkflowUtil::getEnabledActionsForState($oOldState); | ||
| 220 | + $res = KTWorkflowUtil::setEnabledActionsForState($oNewState, $aDisabled); | ||
| 221 | + | ||
| 222 | + if (PEAR::isError($res)) { | ||
| 223 | + return $this->errorRedirectToMain(sprintf(_kt("Unable to copy disabled state actions: %s"), $res->getMessage())); | ||
| 224 | + } | ||
| 225 | + } | ||
| 226 | + | ||
| 227 | + // update workflow and set initial state | ||
| 228 | + for($i=0;$i<count($aStatesMap[oldId]);$i++){ | ||
| 229 | + if($oSelWorkflow->getStartStateId() == $aStatesMap[oldId][$i]){ | ||
| 230 | + $oNewWorkflow->setStartStateId($aStatesMap[newId][$i]); | ||
| 231 | + $res = $oNewWorkflow->update(); | ||
| 232 | + if (PEAR::isError($res)) { | ||
| 233 | + $this->errorRedirectToMain(sprintf(_kt("Failed to update workflow: %s"), $res->getMessage())); | ||
| 234 | + } | ||
| 235 | + } | ||
| 236 | + } | ||
| 237 | + | ||
| 238 | + // set controlled workflow actions | ||
| 239 | + $aWFActions = KTWorkflowUtil::getControlledActionsForWorkflow($oSelWorkflow); | ||
| 240 | + $res = KTWorkflowUtil::setControlledActionsForWorkflow($oNewWorkflow, $aWFActions); | ||
| 241 | + if (PEAR::isError($res)) { | ||
| 242 | + $this->errorRedirectToMain(sprintf(_kt("Failed to copy workflow controlled actions: %s"), $res->getMessage())); | ||
| 243 | + } | ||
| 244 | + | ||
| 245 | + // get selected workflow transitions from database | ||
| 246 | + $oSelWorkflowTransitions = KTWorkflowTransition::getByWorkflow($oSelWorkflow); | ||
| 247 | + | ||
| 248 | + // array to store map of old and new transitions | ||
| 249 | + $aTransitionsMap = array(); | ||
| 250 | + | ||
| 251 | + // copy transitions for workflow | ||
| 252 | + foreach ($oSelWorkflowTransitions as $oOldTransition) { | ||
| 253 | + for($i=0;$i<count($aStatesMap[oldId]);$i++){ | ||
| 254 | + if($oOldTransition->getTargetStateId() == $aStatesMap[oldId][$i]){ | ||
| 255 | + $iDestState = $aStatesMap[newId][$i]; | ||
| 256 | + } | ||
| 257 | + } | ||
| 258 | + $oNewTransition = KTWorkflowTransition::createFromArray(array( | ||
| 259 | + 'workflowid' => $oNewWorkflow->getId(), | ||
| 260 | + 'Name' => $oOldTransition->getName(), | ||
| 261 | + 'HumanName' => $oOldTransition->getName(), | ||
| 262 | + 'TargetStateId' => $iDestState, | ||
| 263 | + 'GuardPermissionId' => null, | ||
| 264 | + 'GuardGroupId' => null, | ||
| 265 | + 'GuardRoleId' => null, | ||
| 266 | + 'GuardConditionId' => null, | ||
| 267 | + )); | ||
| 268 | + | ||
| 269 | + $aTransitionsMap[oldId][] = $oOldTransition->getId(); | ||
| 270 | + $aTransitionsMap[newId][] = $oNewTransition->getId(); | ||
| 271 | + | ||
| 272 | + if (PEAR::isError($oNewTransition)) { | ||
| 273 | + $this->errorRedirectToMain(sprintf(_kt("Failed to copy transition: %s"), $oTransition->getMessage())); | ||
| 274 | + } | ||
| 275 | + | ||
| 276 | + // map source transitions onto states | ||
| 277 | + $aOldTransitionSources = KTWorkflowAdminUtil::getSourceStates($oOldTransition); | ||
| 278 | + $aSourceStates = array(); | ||
| 279 | + for($j=0;$j<count($aOldTransitionSources);$j++){ | ||
| 280 | + for($i=0;$i<count($aStatesMap[oldId]);$i++){ | ||
| 281 | + if($aStatesMap[oldId][$i] == $aOldTransitionSources[$j]->getId()){ | ||
| 282 | + $aSourceStates[] = $aStatesMap[newId][$i]; | ||
| 283 | + continue; | ||
| 284 | + } | ||
| 285 | + } | ||
| 286 | + } | ||
| 287 | + $res = KTWorkflowAdminUtil::saveTransitionSources($oNewTransition, $aSourceStates); | ||
| 288 | + if (PEAR::isError($res)) { | ||
| 289 | + $this->errorRedirectToMain(sprintf(_kt("Failed to set transition origins: %s"), $res->getMessage())); | ||
| 290 | + } | ||
| 291 | + | ||
| 292 | + // Get all triggers for old workflow transitions and copy for copied workflow transitions | ||
| 293 | + $aTriggers = KTWorkflowTriggerInstance::getByTransition($oOldTransition); | ||
| 294 | + if(count($aTriggers) > 0){ | ||
| 295 | + foreach ($aTriggers as $oTrigger) { | ||
| 296 | + for($i=0;$i<count($aTransitionsMap[oldId]);$i++){ | ||
| 297 | + if($aTransitionsMap[oldId][$i] == $oTrigger->getTransitionId()){ | ||
| 298 | + $iTransitionId = $aTransitionsMap[newId][$i]; | ||
| 299 | + | ||
| 300 | + $res = KTWorkflowTriggerInstance::createFromArray(array( | ||
| 301 | + 'transitionid' => $iTransitionId, | ||
| 302 | + 'namespace' => $oTrigger->getNamespace(), | ||
| 303 | + 'config' => $oTrigger->getConfigArrayText(), | ||
| 304 | + )); | ||
| 305 | + | ||
| 306 | + if (PEAR::isError($res)) { | ||
| 307 | + return $this->errorRedirectToMain(sprintf(_kt("Unable to add trigger: %s"), $res->getMessage())); | ||
| 308 | + } | ||
| 309 | + } | ||
| 310 | + } | ||
| 311 | + } | ||
| 312 | + } | ||
| 313 | + } | ||
| 314 | + return $this->successRedirectToMain(sprintf(_kt("%s successfully copied as %s"), $oSelWorkflow->getName(), $oNewWorkflow->getName())); | ||
| 315 | + } | ||
| 316 | + | ||
| 125 | function do_newWorkflow() { | 317 | function do_newWorkflow() { |
| 126 | // subdispatch this to the NewWorkflowWizard. | 318 | // subdispatch this to the NewWorkflowWizard. |
| 127 | require_once(dirname(__FILE__) . '/workflow/newworkflow.inc.php'); | 319 | require_once(dirname(__FILE__) . '/workflow/newworkflow.inc.php'); |
templates/ktcore/workflow/admin/copy.smarty
0 → 100644
| 1 | +<h2>{i18n}Copy Workflow{/i18n}</h2> | ||
| 2 | +<p class="descriptiveText"> | ||
| 3 | +{i18n}You are about to make a copy of <strong>{$workFlowName}</strong>. This will make and exact copy of the selected workflow, | ||
| 4 | + including all associated permissions, actions and triggers. Enter the desired name for the resulting workflow | ||
| 5 | + copy to continue.{/i18n} | ||
| 6 | +</p> | ||
| 7 | + | ||
| 8 | +<h3>{i18n}New workflow information{/i18n}</h3> | ||
| 9 | +<form action="{$smarty.server.PHP_SELF}" method="POST"> | ||
| 10 | + <input type="hidden" name="action" value="branchConfirm" /> | ||
| 11 | + <input type="hidden" name="workflowId" value="{$workFlowId}" /> | ||
| 12 | + <table> | ||
| 13 | + <tr> | ||
| 14 | + <td>New workflow name: </td> | ||
| 15 | + <td><input type="text" name="workflowName"></td> | ||
| 16 | + </tr> | ||
| 17 | + <tr> | ||
| 18 | + <td colspan="2"><hr></td> | ||
| 19 | + </tr> | ||
| 20 | + </table> | ||
| 21 | + | ||
| 22 | + <input type="submit" name="submit[confirmCopy]" value="{i18n}Copy{/i18n}" /> | ||
| 23 | +</form> | ||
| 0 | \ No newline at end of file | 24 | \ No newline at end of file |
templates/ktcore/workflow/admin/list.smarty
| @@ -9,10 +9,14 @@ | @@ -9,10 +9,14 @@ | ||
| 9 | <h3>{i18n}Existing workflows{/i18n}</h3> | 9 | <h3>{i18n}Existing workflows{/i18n}</h3> |
| 10 | <p class="descriptiveText">{i18n}Select a workflow to modify. To enable a disabled workflow, edit it and set a proper starting state.{/i18n}</p> | 10 | <p class="descriptiveText">{i18n}Select a workflow to modify. To enable a disabled workflow, edit it and set a proper starting state.{/i18n}</p> |
| 11 | 11 | ||
| 12 | +<form action="{$smarty.server.PHP_SELF}" method="POST"> | ||
| 13 | + | ||
| 14 | +<input type="hidden" name="action" value="branchConfirm" /> | ||
| 12 | <table class="kt_collection narrow" cellspacing="0"> | 15 | <table class="kt_collection narrow" cellspacing="0"> |
| 13 | 16 | ||
| 14 | <thead> | 17 | <thead> |
| 15 | <tr> | 18 | <tr> |
| 19 | + <th></th> | ||
| 16 | <th>{i18n}Name{/i18n}</th> | 20 | <th>{i18n}Name{/i18n}</th> |
| 17 | <th>{i18n}Status{/i18n}</th> | 21 | <th>{i18n}Status{/i18n}</th> |
| 18 | <th>{i18n}Edit{/i18n}</th> | 22 | <th>{i18n}Edit{/i18n}</th> |
| @@ -21,14 +25,21 @@ | @@ -21,14 +25,21 @@ | ||
| 21 | 25 | ||
| 22 | <tbody> | 26 | <tbody> |
| 23 | {foreach from=$workflows item=oWorkflow} | 27 | {foreach from=$workflows item=oWorkflow} |
| 24 | - | ||
| 25 | <tr> | 28 | <tr> |
| 29 | + <td><input type="radio" name="workflowSelect" value="{$oWorkflow->getId()}" /></td> | ||
| 26 | <td>{$oWorkflow->getName()}</td> | 30 | <td>{$oWorkflow->getName()}</td> |
| 27 | <td class="centered">{if $oWorkflow->getIsFunctional()} <span class="ktAction ktAllowed">{i18n}Enabled{/i18n}</span> {else} <span class="ktAction ktDenied">{i18n}Disabled{/i18n}</span> {/if}</td> | 31 | <td class="centered">{if $oWorkflow->getIsFunctional()} <span class="ktAction ktAllowed">{i18n}Enabled{/i18n}</span> {else} <span class="ktAction ktDenied">{i18n}Disabled{/i18n}</span> {/if}</td> |
| 28 | <td><a class="ktAction ktEdit" href="{addQS}action=view&fWorkflowId={$oWorkflow->getId()}{/addQS}">{i18n}Edit{/i18n}</a></td> | 32 | <td><a class="ktAction ktEdit" href="{addQS}action=view&fWorkflowId={$oWorkflow->getId()}{/addQS}">{i18n}Edit{/i18n}</a></td> |
| 29 | </tr> | 33 | </tr> |
| 30 | {/foreach} | 34 | {/foreach} |
| 35 | + <tr> | ||
| 36 | + <td colspan="4"><hr/></td> | ||
| 37 | + </tr> | ||
| 38 | + <tr> | ||
| 39 | + <td><input type="submit" name="submit[copy]" value="{i18n}Copy{/i18n}" /></td> | ||
| 40 | + </tr> | ||
| 31 | </tbody> | 41 | </tbody> |
| 32 | </table> | 42 | </table> |
| 43 | +</form> | ||
| 33 | 44 | ||
| 34 | {/if} | 45 | {/if} |