Example #1
0
 public function runConfigureWorkflowTransition(TBGRequest $request)
 {
     $this->workflow = null;
     $this->transition = null;
     try {
         $this->workflow = TBGWorkflowsTable::getTable()->selectById((int) $request['workflow_id']);
         if ($request->hasParameter('transition_id')) {
             $mode = $request['mode'];
             $this->transition = TBGWorkflowTransitionsTable::getTable()->selectById((int) $request['transition_id']);
             if ($request->isPost()) {
                 if ($mode == 'edit') {
                     if (!$this->transition->isInitialTransition()) {
                         $this->transition->setName($request['transition_name']);
                         $this->transition->setDescription($request['transition_description']);
                         if ($request['template']) {
                             $this->transition->setTemplate($request['template']);
                         } else {
                             $this->transition->setTemplate(null);
                         }
                     }
                     try {
                         $step = TBGWorkflowStepsTable::getTable()->selectById((int) $request['outgoing_step_id']);
                         $this->transition->setOutgoingStep($step);
                     } catch (Exception $e) {
                     }
                     $this->transition->save();
                     $transition = $this->transition;
                     $redirect_transition = true;
                 } elseif ($mode == 'delete') {
                     $this->transition->deleteTransition($request['direction']);
                     return $this->renderJSON('ok');
                 } elseif ($mode == 'delete_action') {
                     $this->action = TBGWorkflowTransitionActionsTable::getTable()->selectById((int) $request['action_id']);
                     $this->action->delete();
                     return $this->renderJSON(array('message' => $this->getI18n()->__('The action has been deleted')));
                 } elseif ($mode == 'new_action') {
                     $action = new TBGWorkflowTransitionAction();
                     $action->setActionType($request['action_type']);
                     $action->setTransition($this->transition);
                     $action->setWorkflow($this->workflow);
                     $action->setTargetValue('');
                     $action->save();
                     return $this->renderJSON(array('content' => $this->getComponentHTML('configuration/workflowtransitionaction', array('action' => $action))));
                 } elseif ($mode == 'update_action') {
                     $this->action = TBGWorkflowTransitionActionsTable::getTable()->selectById((int) $request['action_id']);
                     $this->action->setTargetValue($request['target_value']);
                     $this->action->save();
                     $text = $request['target_value'];
                     switch ($this->action->getActionType()) {
                         case TBGWorkflowTransitionAction::ACTION_ASSIGN_ISSUE:
                             if ($this->action->hasTargetValue()) {
                                 $target_details = explode('_', $this->action->getTargetValue());
                                 $text = $target_details[0] == 'user' ? TBGUser::getB2DBTable()->selectById((int) $target_details[1])->getNameWithUsername() : TBGTeam::getB2DBTable()->selectById((int) $target_details[1])->getName();
                             } else {
                                 $text = $this->getI18n()->__('User specified during transition');
                             }
                             break;
                         case TBGWorkflowTransitionAction::ACTION_SET_RESOLUTION:
                             $text = $this->action->getTargetValue() ? TBGListTypesTable::getTable()->selectById((int) $this->action->getTargetValue())->getName() : $this->getI18n()->__('Resolution specified by user');
                             break;
                         case TBGWorkflowTransitionAction::ACTION_SET_REPRODUCABILITY:
                             $text = $this->action->getTargetValue() ? TBGListTypesTable::getTable()->selectById((int) $this->action->getTargetValue())->getName() : $this->getI18n()->__('Reproducability specified by user');
                             break;
                         case TBGWorkflowTransitionAction::ACTION_SET_STATUS:
                             $text = $this->action->getTargetValue() ? TBGListTypesTable::getTable()->selectById((int) $this->action->getTargetValue())->getName() : $this->getI18n()->__('Status specified by user');
                             break;
                         case TBGWorkflowTransitionAction::ACTION_SET_PRIORITY:
                             $text = $this->action->getTargetValue() ? TBGListTypesTable::getTable()->selectById((int) $this->action->getTargetValue())->getName() : $this->getI18n()->__('Priority specified by user');
                             break;
                         case TBGWorkflowTransitionAction::ACTION_SET_MILESTONE:
                             $text = $this->action->getTargetValue() ? TBGMilestonesTable::getTable()->selectById((int) $this->action->getTargetValue())->getName() : $this->getI18n()->__('Milestone specified by user');
                             break;
                     }
                     return $this->renderJSON(array('content' => $text));
                 } elseif ($mode == 'delete_validation_rule') {
                     $this->rule = TBGWorkflowTransitionValidationRulesTable::getTable()->selectById((int) $request['rule_id']);
                     $this->rule->delete();
                     return $this->renderJSON(array('message' => $this->getI18n()->__('The validation rule has been deleted')));
                 } elseif ($mode == 'new_validation_rule') {
                     $rule = new TBGWorkflowTransitionValidationRule();
                     if ($request['postorpre'] == 'post') {
                         $exists = (bool) $this->transition->hasPostValidationRule($request['rule']);
                         if (!$exists) {
                             $rule->setPost();
                         }
                     } elseif ($request['postorpre'] == 'pre') {
                         $exists = (bool) $this->transition->hasPreValidationRule($request['rule']);
                         if (!$exists) {
                             $rule->setPre();
                         }
                     }
                     if ($exists) {
                         $this->getResponse()->setHttpStatus(400);
                         return $this->renderJSON(array('message' => $this->getI18n()->__('This validation rule already exist')));
                     }
                     $rule->setRule($request['rule']);
                     $rule->setRuleValue('');
                     $rule->setTransition($this->transition);
                     $rule->setWorkflow($this->workflow);
                     $rule->save();
                     return $this->renderJSON(array('content' => $this->getTemplateHTML('configuration/workflowtransitionvalidationrule', array('rule' => $rule))));
                 } elseif ($mode == 'update_validation_rule') {
                     $this->rule = TBGWorkflowTransitionValidationRulesTable::getTable()->selectById((int) $request['rule_id']);
                     $text = null;
                     switch ($this->rule->getRule()) {
                         case TBGWorkflowTransitionValidationRule::RULE_MAX_ASSIGNED_ISSUES:
                             $this->rule->setRuleValue($request['rule_value']);
                             $text = $this->rule->getRuleValue() ? $this->rule->getRuleValue() : $this->getI18n()->__('Unlimited');
                             break;
                         case TBGWorkflowTransitionValidationRule::RULE_PRIORITY_VALID:
                         case TBGWorkflowTransitionValidationRule::RULE_REPRODUCABILITY_VALID:
                         case TBGWorkflowTransitionValidationRule::RULE_RESOLUTION_VALID:
                         case TBGWorkflowTransitionValidationRule::RULE_STATUS_VALID:
                         case TBGWorkflowTransitionValidationRule::RULE_TEAM_MEMBERSHIP_VALID:
                             $this->rule->setRuleValue(join(',', $request['rule_value']));
                             $text = $this->rule->getRuleValue() ? $this->rule->getRuleValueAsJoinedString() : $this->getI18n()->__('Any valid value');
                             break;
                     }
                     $this->rule->save();
                     return $this->renderJSON(array('content' => $text));
                 }
             }
         } elseif ($request->isPost() && $request->hasParameter('step_id')) {
             $step = TBGWorkflowStepsTable::getTable()->selectById((int) $request['step_id']);
             /*if ($step->isCore() || $workflow->isCore())
             		{
             			throw new InvalidArgumentException("The default workflow cannot be edited");
             		}*/
             if ($request['add_transition_type'] == 'existing' && $request->hasParameter('existing_transition_id')) {
                 $transition = TBGWorkflowTransitionsTable::getTable()->selectById((int) $request['existing_transition_id']);
                 $redirect_transition = false;
             } else {
                 if ($request['transition_name'] && $request['outgoing_step_id'] && $request->hasParameter('template')) {
                     if (($outgoing_step = TBGWorkflowStepsTable::getTable()->selectById((int) $request['outgoing_step_id'])) && $step instanceof TBGWorkflowStep) {
                         if (array_key_exists($request['template'], TBGWorkflowTransition::getTemplates())) {
                             $transition = new TBGWorkflowTransition();
                             $transition->setWorkflow($this->workflow);
                             $transition->setName($request['transition_name']);
                             $transition->setDescription($request['transition_description']);
                             $transition->setOutgoingStep($outgoing_step);
                             $transition->setTemplate($request['template']);
                             $transition->save();
                             $step->addOutgoingTransition($transition);
                             $redirect_transition = true;
                         } else {
                             throw new InvalidArgumentException($this->getI18n()->__('Please select a valid template'));
                         }
                     } else {
                         throw new InvalidArgumentException($this->getI18n()->__('Please select a valid outgoing step'));
                     }
                 } else {
                     throw new InvalidArgumentException($this->getI18n()->__('Please fill in all required fields'));
                 }
             }
             $step->addOutgoingTransition($transition);
         } else {
             throw new InvalidArgumentException('Invalid action');
         }
     } catch (InvalidArgumentException $e) {
         //throw $e;
         $this->error = $e->getMessage();
     } catch (Exception $e) {
         throw $e;
         $this->error = $this->getI18n()->__('This workflow / transition does not exist');
     }
     if (isset($redirect_transition) && $redirect_transition) {
         $this->forward(TBGContext::getRouting()->generate('configure_workflow_transition', array('workflow_id' => $this->workflow->getID(), 'transition_id' => $transition->getID())));
     } elseif (isset($redirect_transition)) {
         $this->forward(TBGContext::getRouting()->generate('configure_workflow_steps', array('workflow_id' => $this->workflow->getID())));
     }
 }
 public function perform(TBGIssue $issue, $request = null)
 {
     switch ($this->_action_type) {
         case self::ACTION_ASSIGN_ISSUE_SELF:
             $issue->setAssignee(TBGContext::getUser());
             break;
         case self::ACTION_SET_STATUS:
             if ($this->getTargetValue()) {
                 $issue->setStatus(TBGContext::factory()->TBGStatus((int) $this->getTargetValue()));
             } else {
                 $issue->setStatus($request['status_id']);
             }
             break;
         case self::ACTION_SET_MILESTONE:
             if ($this->getTargetValue()) {
                 $issue->setMilestone(TBGContext::factory()->TBGMilestone((int) $this->getTargetValue()));
             } else {
                 $issue->setMilestone($request['milestone_id']);
             }
             break;
         case self::ACTION_CLEAR_PRIORITY:
             $issue->setPriority(null);
             break;
         case self::ACTION_SET_PRIORITY:
             if ($this->getTargetValue()) {
                 $issue->setPriority(TBGContext::factory()->TBGPriority((int) $this->getTargetValue()));
             } else {
                 $issue->setPriority($request['priority_id']);
             }
             break;
         case self::ACTION_CLEAR_PERCENT:
             $issue->setPercentCompleted(0);
             break;
         case self::ACTION_SET_PERCENT:
             if ($this->getTargetValue()) {
                 $issue->setPercentCompleted((int) $this->getTargetValue());
             } else {
                 $issue->setPercentCompleted((int) $request['percent_complete_id']);
             }
             break;
         case self::ACTION_CLEAR_DUPLICATE:
             $issue->setDuplicateOf(null);
             break;
         case self::ACTION_SET_DUPLICATE:
             $issue->setDuplicateOf($request['duplicate_issue_id']);
             break;
         case self::ACTION_CLEAR_RESOLUTION:
             $issue->setResolution(null);
             break;
         case self::ACTION_SET_RESOLUTION:
             if ($this->getTargetValue()) {
                 $issue->setResolution(TBGContext::factory()->TBGResolution((int) $this->getTargetValue()));
             } else {
                 $issue->setResolution($request['resolution_id']);
             }
             break;
         case self::ACTION_CLEAR_REPRODUCABILITY:
             $issue->setReproducability(null);
             break;
         case self::ACTION_SET_REPRODUCABILITY:
             if ($this->getTargetValue()) {
                 $issue->setReproducability(TBGContext::factory()->TBGReproducability((int) $this->getTargetValue()));
             } else {
                 $issue->setReproducability($request['reproducability_id']);
             }
             break;
         case self::ACTION_CLEAR_ASSIGNEE:
             $issue->clearAssignee();
             break;
         case self::ACTION_ASSIGN_ISSUE:
             if ($this->getTargetValue()) {
                 $target_details = explode('_', $this->_target_value);
                 if ($target_details[0] == 'user') {
                     $assignee = TBGUser::getB2DBTable()->selectById((int) $target_details[1]);
                 } else {
                     $assignee = TBGTeam::getB2DBTable()->selectById((int) $target_details[1]);
                 }
                 $issue->setAssignee($assignee);
             } else {
                 $assignee = null;
                 switch ($request['assignee_type']) {
                     case 'user':
                         $assignee = TBGUser::getB2DBTable()->selectById((int) $request['assignee_id']);
                         break;
                     case 'team':
                         $assignee = TBGTeam::getB2DBTable()->selectById((int) $request['assignee_id']);
                         break;
                 }
                 if ((bool) $request->getParameter('assignee_teamup', false) && $assignee instanceof TBGUser && $assignee->getID() != TBGContext::getUser()->getID()) {
                     $team = new TBGTeam();
                     $team->setName($assignee->getBuddyname() . ' & ' . TBGContext::getUser()->getBuddyname());
                     $team->setOndemand(true);
                     $team->save();
                     $team->addMember($assignee);
                     $team->addMember(TBGContext::getUser());
                     $assignee = $team;
                 }
                 $issue->setAssignee($assignee);
             }
             break;
         case self::ACTION_USER_START_WORKING:
             $issue->clearUserWorkingOnIssue();
             if ($issue->getAssignee() instanceof TBGTeam && $issue->getAssignee()->isOndemand()) {
                 $members = $issue->getAssignee()->getMembers();
                 $issue->startWorkingOnIssue(array_shift($members));
             } elseif ($issue->getAssignee() instanceof TBGUser) {
                 $issue->startWorkingOnIssue($issue->getAssignee());
             }
             break;
         case self::ACTION_USER_STOP_WORKING:
             if ($request->getParameter('did', 'nothing') == 'nothing') {
                 $issue->clearUserWorkingOnIssue();
             } elseif ($request->getParameter('did', 'nothing') == 'this') {
                 $times = array();
                 if ($request['timespent_manual']) {
                     $times = TBGIssue::convertFancyStringToTime($request['timespent_manual']);
                 } elseif ($request['timespent_specified_type']) {
                     $times = array('points' => 0, 'hours' => 0, 'days' => 0, 'weeks' => 0, 'months' => 0);
                     $times[$request['timespent_specified_type']] = $request['timespent_specified_value'];
                 }
                 if (array_sum($times) > 0) {
                     $times['hours'] *= 100;
                     $spenttime = new TBGIssueSpentTime();
                     $spenttime->setIssue($issue);
                     $spenttime->setUser(TBGContext::getUser());
                     $spenttime->setSpentPoints($times['points']);
                     $spenttime->setSpentHours($times['hours']);
                     $spenttime->setSpentDays($times['days']);
                     $spenttime->setSpentWeeks($times['weeks']);
                     $spenttime->setSpentMonths($times['months']);
                     $spenttime->setActivityType($request['timespent_activitytype']);
                     $spenttime->setComment($request['timespent_comment']);
                     $spenttime->save();
                 }
                 $issue->clearUserWorkingOnIssue();
             } else {
                 $issue->stopWorkingOnIssue();
             }
             break;
     }
 }
							<?php 
                echo __('Set resolution to %resolution', array('%resolution' => '<span id="workflowtransitionaction_' . $action->getID() . '_value" style="font-weight: bold;">' . ($action->getTargetValue() ? TBGContext::factory()->TBGResolution((int) $action->getTargetValue())->getName() : __('Resolution provided by user')) . '</span>'));
                ?>
						<?php 
            } elseif ($action->getActionType() == TBGWorkflowTransitionAction::ACTION_SET_REPRODUCABILITY) {
                ?>
							<?php 
                echo __('Set reproducability to %reproducability', array('%reproducability' => '<span id="workflowtransitionaction_' . $action->getID() . '_value" style="font-weight: bold;">' . ($action->getTargetValue() ? TBGContext::factory()->TBGReproducability((int) $action->getTargetValue())->getName() : __('Reproducability provided by user')) . '</span>'));
                ?>
						<?php 
            } elseif ($action->getActionType() == TBGWorkflowTransitionAction::ACTION_ASSIGN_ISSUE) {
                ?>
							<?php 
                if ($action->hasTargetValue()) {
                    $target_details = explode('_', $action->getTargetValue());
                    echo __('Assign issue to %assignee', array('%assignee' => '<span id="workflowtransitionaction_' . $action->getID() . '_value" style="font-weight: bold;">' . ($target_details[0] == 'user' ? TBGUser::getB2DBTable()->selectById((int) $target_details[1])->getNameWithUsername() : TBGTeam::getB2DBTable()->selectById((int) $target_details[1])->getName()) . '</span>'));
                } else {
                    echo __('Assign issue to %assignee', array('%assignee' => '<span id="workflowtransitionaction_' . $action->getID() . '_value" style="font-weight: bold;">' . __('User or team specified during transition') . '</span>'));
                }
                ?>
						<?php 
            }
            ?>
					<?php 
        } elseif ($action->getTargetValue()) {
            ?>
						<span class="generic_error_message"><?php 
            echo __('Invalid transition configuration');
            ?>
</span>
					<?php