/**
* xSaveAction
*
* @param string $ccId
* @param string $projectId
* @param string $name
* @param string $description
* @param string $billingCode
* @param string $leadEmail
* @param int $shared
* @param int $accountId optional
* @param bool $checkAccountAccessToCc optional
* @param bool $grantAccountAccessToCc optional
* @throws Scalr_Exception_InsufficientPermissions
*/
public function xSaveAction($ccId, $projectId, $name, $description, $billingCode, $leadEmail, $shared, $accountId = null, $checkAccountAccessToCc = true, $grantAccountAccessToCc = false)
{
$validator = new Validator();
$validator->validate($name, 'name', Validator::NOEMPTY);
if ($projectId) {
$project = $this->getContainer()->analytics->projects->get($projectId);
if (!$project) {
throw new Scalr_UI_Exception_NotFound();
}
} else {
$project = new ProjectEntity();
$project->createdById = $this->user->id;
$project->createdByEmail = $this->user->getEmail();
$cc = $this->getContainer()->analytics->ccs->get($ccId);
if (!$cc) {
$validator->addError('ccId', 'Cost center ID should be set');
}
$project->ccId = $ccId;
}
if ($shared == ProjectEntity::SHARED_WITHIN_ACCOUNT) {
$project->shared = ProjectEntity::SHARED_WITHIN_ACCOUNT;
$project->accountId = $accountId;
} elseif ($shared == ProjectEntity::SHARED_WITHIN_CC) {
$project->shared = ProjectEntity::SHARED_WITHIN_CC;
$project->accountId = null;
} else {
throw new Scalr_UI_Exception_NotFound();
}
if (!$validator->isValid($this->response)) {
return;
}
if ($project->shared == ProjectEntity::SHARED_WITHIN_ACCOUNT) {
if (!AccountCostCenterEntity::findOne([['accountId' => $project->accountId], ['ccId' => $ccId]])) {
if ($checkAccountAccessToCc) {
$this->response->data(['ccIsNotAllowedToAccount' => true]);
$this->response->failure();
return;
} elseif ($grantAccountAccessToCc) {
//give account access to cc
$accountCcEntity = new AccountCostCenterEntity($project->accountId, $ccId);
$accountCcEntity->save();
}
}
}
$project->name = $name;
$this->db->BeginTrans();
try {
$project->save();
//NOTE please take into account the presence of the usage->createHostedScalrAccountCostCenter() method
$project->saveProperty(ProjectPropertyEntity::NAME_BILLING_CODE, $billingCode);
$project->saveProperty(ProjectPropertyEntity::NAME_DESCRIPTION, $description);
$project->saveProperty(ProjectPropertyEntity::NAME_LEAD_EMAIL, $leadEmail);
$this->db->CommitTrans();
} catch (Exception $e) {
$this->db->RollbackTrans();
throw $e;
}
$this->response->data(['project' => $this->getProjectData($project)]);
$this->response->success('Project has been successfully saved');
}