public function actionSave($type, $id = null, $isBeingCopied = false)
 {
     $postData = PostUtil::getData();
     $savedWorkflow = null;
     $workflow = null;
     $this->resolveSavedWorkflowAndWorkflowByPostData($postData, $savedWorkflow, $workflow, $type, $id, (bool) $isBeingCopied);
     $workflowToWizardFormAdapter = new WorkflowToWizardFormAdapter($workflow);
     $model = $workflowToWizardFormAdapter->makeFormByType();
     if (isset($postData['ajax']) && $postData['ajax'] === 'edit-form') {
         $this->actionValidate($postData, $model);
     }
     SavedWorkflowToWorkflowAdapter::resolveWorkflowToSavedWorkflow($workflow, $savedWorkflow);
     SavedWorkflowsUtil::resolveOrder($savedWorkflow, (bool) $isBeingCopied);
     if ($savedWorkflow->id > 0) {
         ControllerSecurityUtil::resolveCanCurrentUserAccessModule($savedWorkflow->moduleClassName);
     }
     if ($savedWorkflow->save()) {
         echo CJSON::encode(array('id' => $savedWorkflow->id, 'redirectToList' => false));
         Yii::app()->end(0, false);
     } else {
         throw new FailedToSaveModelException();
     }
 }
 /**
  * @depends testResolveProcessDateTimeByWorkflowAndModelWithPseudoNullDateTime
  */
 public function testResolveOrder()
 {
     $this->assertCount(0, SavedWorkflow::getAll());
     $savedWorkflow = new SavedWorkflow();
     $savedWorkflow->name = 'the name';
     $savedWorkflow->moduleClassName = 'AccountsModule';
     $savedWorkflow->serializedData = serialize(array('some data'));
     $savedWorkflow->triggerOn = Workflow::TRIGGER_ON_NEW;
     $savedWorkflow->type = Workflow::TYPE_ON_SAVE;
     $this->assertNull($savedWorkflow->order);
     SavedWorkflowsUtil::resolveOrder($savedWorkflow);
     $this->assertEquals(1, $savedWorkflow->order);
     $saved = $savedWorkflow->save();
     $this->assertTrue($saved);
     $savedWorkflowId1 = $savedWorkflow->id;
     $savedWorkflow = new SavedWorkflow();
     $savedWorkflow->name = 'the name 2';
     $savedWorkflow->moduleClassName = 'AccountsModule';
     $savedWorkflow->serializedData = serialize(array('some data 2'));
     $savedWorkflow->triggerOn = Workflow::TRIGGER_ON_NEW;
     $savedWorkflow->type = Workflow::TYPE_ON_SAVE;
     $this->assertNull($savedWorkflow->order);
     SavedWorkflowsUtil::resolveOrder($savedWorkflow);
     $this->assertEquals(2, $savedWorkflow->order);
     $saved = $savedWorkflow->save();
     $this->assertTrue($saved);
     $savedWorkflowId2 = $savedWorkflow->id;
     $savedWorkflow = new SavedWorkflow();
     $savedWorkflow->name = 'the name 3';
     $savedWorkflow->moduleClassName = 'AccountsModule';
     $savedWorkflow->serializedData = serialize(array('some data 2'));
     $savedWorkflow->triggerOn = Workflow::TRIGGER_ON_NEW;
     $savedWorkflow->type = Workflow::TYPE_ON_SAVE;
     $this->assertNull($savedWorkflow->order);
     SavedWorkflowsUtil::resolveOrder($savedWorkflow);
     $this->assertEquals(3, $savedWorkflow->order);
     $saved = $savedWorkflow->save();
     $this->assertTrue($saved);
     $savedWorkflowId3 = $savedWorkflow->id;
     $savedWorkflow = new SavedWorkflow();
     $savedWorkflow->name = 'the name 4';
     $savedWorkflow->moduleClassName = 'ContactsModule';
     $savedWorkflow->serializedData = serialize(array('some data'));
     $savedWorkflow->triggerOn = Workflow::TRIGGER_ON_NEW;
     $savedWorkflow->type = Workflow::TYPE_ON_SAVE;
     $this->assertNull($savedWorkflow->order);
     SavedWorkflowsUtil::resolveOrder($savedWorkflow);
     $this->assertEquals(1, $savedWorkflow->order);
     $saved = $savedWorkflow->save();
     $this->assertTrue($saved);
     $savedWorkflowId4 = $savedWorkflow->id;
     $savedWorkflow = SavedWorkflow::getById($savedWorkflowId2);
     $this->assertEquals(2, $savedWorkflow->order);
     SavedWorkflowsUtil::resolveOrder($savedWorkflow);
     $this->assertEquals(2, $savedWorkflow->order);
     //Change the moduleClassName to opportunities, it should show 1
     $savedWorkflow->moduleClassName = 'OpportunitiesModule';
     SavedWorkflowsUtil::resolveOrder($savedWorkflow);
     $this->assertEquals(1, $savedWorkflow->order);
     //Delete the workflow. When creating a new AccountsWorkflow, it should show order 4 since the max
     //is still 3.
     $deleted = $savedWorkflow->delete();
     $this->assertTrue($deleted);
     $savedWorkflow = new SavedWorkflow();
     $savedWorkflow->name = 'the name 5';
     $savedWorkflow->moduleClassName = 'AccountsModule';
     $savedWorkflow->serializedData = serialize(array('some data 2'));
     $savedWorkflow->triggerOn = Workflow::TRIGGER_ON_NEW;
     $savedWorkflow->type = Workflow::TYPE_ON_SAVE;
     $this->assertNull($savedWorkflow->order);
     SavedWorkflowsUtil::resolveOrder($savedWorkflow);
     $this->assertEquals(4, $savedWorkflow->order);
     $saved = $savedWorkflow->save();
     $this->assertTrue($saved);
     //Try to resolve order for the same
     SavedWorkflowsUtil::resolveOrder($savedWorkflow);
     $this->assertEquals(4, $savedWorkflow->order);
     //Resolve order when copying.  It should increase
     SavedWorkflowsUtil::resolveOrder($savedWorkflow, true);
     $this->assertEquals(5, $savedWorkflow->order);
 }