public function execute(&$params) { $workflowId = $this->parseOption('workflowId', $params); $stageNumber = $this->parseOption('stageNumber', $params); $model = $params['model']; $type = lcfirst(X2Model::getModuleName(get_class($model))); $modelId = $model->id; $workflowStatus = Workflow::getWorkflowStatus($workflowId, $modelId, $type); $message = ''; if (Workflow::validateAction('start', $workflowStatus, $stageNumber, '', $message)) { list($started, $workflowStatus) = Workflow::startStage($workflowId, $stageNumber, $model, $workflowStatus); assert($started); return array(true, Yii::t('studio', 'Stage "{stageName}" started for {recordName}', array('{stageName}' => $workflowStatus['stages'][$stageNumber]['name'], '{recordName}' => $model->getLink()))); } else { return array(false, $message); } }
/** * Moves a record up or down a workflow. Assumes that stageA is started but not completed. * Intermediate stages and stageB can be in any state. * @param int $workflowId * @param int $stageA Start stage (indexed by 1) * @param int $stageB End stage (indexed by 1) * @param object $model model associated with workflow * @param array $comments comment strings indexed by workflow stage number * Precondition: $stageA !== $stageB * @return array first element is success, the second is an optional message */ public static function moveFromStageAToStageB($workflowId, $stageA, $stageB, $model, $comments = array()) { if ($stageA === $stageB && YII_DEBUG) { throw new CException('Precondition violation: $stageA === $stageB'); } $modelId = $model->id; $type = lcfirst(X2Model::getModuleName(get_class($model))); $retVal = self::validateStageChange($workflowId, $stageA, $stageB, $modelId, $type, $comments); if (!$retVal[0]) { return $retVal; } // enact stage change if ($stageA < $stageB) { // complete first stage list($success, $status) = Workflow::completeStage($workflowId, $stageA, $model, isset($comments[$stageA]) ? $comments[$stageA] : '', false); for ($i = $stageA + 1; $i < $stageB; ++$i) { // start and complete intermediate stages list($success, $status) = Workflow::startStage($workflowId, $i, $model, $status); list($success, $status) = Workflow::completeStage($workflowId, $i, $model, isset($comments[$i]) ? $comments[$i] : '', false, $status); } list($success, $status) = Workflow::startStage($workflowId, $stageB, $model, $status); // uncomplete a completed final stage list($success, $status) = Workflow::revertStage($workflowId, $stageB, $model, false, $status); } else { // $stageA > $stageB // unstart first stage list($success, $status) = Workflow::revertStage($workflowId, $stageA, $model); for ($i = $stageA - 1; $i > $stageB; --$i) { // uncomplete and unstart intermediate stages list($success, $status) = Workflow::revertStage($workflowId, $i, $model, $status); list($success, $status) = Workflow::revertStage($workflowId, $i, $model, $status); } // uncomplete a completed final stage list($success, $status) = Workflow::revertStage($workflowId, $stageB, $model, false, $status); list($success, $status) = Workflow::startStage($workflowId, $stageB, $model, false, $status); } return array(true); }
public function testStartStage() { $workflow = $this->workflows('workflow2'); $model = $this->contacts('contact935'); list($success, $status) = Workflow::startStage($workflow->id, 5, $model); // couldn't start a stage which requires previous, uncompleted stage $this->assertFalse($success); // complete stage 4 and disable auto start so that stage 5 doesn't get started list($success, $status) = Workflow::completeStage($workflow->id, 4, $model, 'test comment', false); list($success, $status) = Workflow::startStage($workflow->id, 5, $model); // should have been able to start stage 5 now that 4 is completed $this->assertTrue($success); }
/** * Called via ajax to add a deal to the pipeline * @param int $workflowId the id of the workflow * @param int $stageNumber the number of the stage * @param int $modelId the id of the associated model * @param string $type the association type of the associated model */ public function actionAjaxAddADeal($workflowId, $stageNumber, $modelId = null, $type, $recordName = null) { $model = $this->validateParams($workflowId, $stageNumber, $modelId, $type, $recordName); $workflowStatus = Workflow::getWorkflowStatus($workflowId, $model->id, $type); $message = ''; if (Workflow::validateAction('start', $workflowStatus, $stageNumber, '', $message)) { list($started, $workflowStatus) = Workflow::startStage($workflowId, $stageNumber, $model, $workflowStatus); assert($started); } $value = Yii::app()->locale->numberFormatter->formatCurrency(Workflow::getProjectedValue($type, $model->getAttributes()), Yii::app()->params->currency); echo CJSON::encode(array('workflowStatus' => $workflowStatus, 'dealValue' => $value, 'flashes' => array('error' => !empty($message) ? array($message) : array(), 'success' => empty($message) ? array(Yii::t('workflow', 'Stage started')) : array()))); }
/** * Called via ajax to add a deal to the pipeline * @param int $workflowId the id of the workflow * @param int $stageNumber the number of the stage * @param int $modelId the id of the associated model * @param string $type the association type of the associated model */ public function actionAjaxAddADeal($workflowId, $stageNumber, $modelId = null, $type, $recordName = null) { $model = $this->validateParams($workflowId, $stageNumber, $modelId, $type, $recordName); $workflowStatus = Workflow::getWorkflowStatus($workflowId, $model->id, $type); $message = ''; if (Workflow::validateAction('start', $workflowStatus, $stageNumber, '', $message)) { list($started, $workflowStatus) = Workflow::startStage($workflowId, $stageNumber, $model, $workflowStatus); assert($started); } echo CJSON::encode(array('workflowStatus' => $workflowStatus, 'flashes' => array('error' => !empty($message) ? array($message) : array(), 'success' => empty($message) ? array(Yii::t('workflow', 'Stage started')) : array()))); }