Example #1
0
 public function xSaveAction()
 {
     if (!$this->user->isAdmin() && !$this->request->isAllowed(Acl::RESOURCE_ANALYTICS_PROJECTS)) {
         throw new Scalr_Exception_InsufficientPermissions();
     }
     $this->request->defineParams(array('name' => array('type' => 'string', 'validator' => array(Scalr_Validator::NOEMPTY => true)), 'billingCode' => array('type' => 'string', 'validator' => array(Scalr_Validator::NOEMPTY => true, Scalr_Validator::ALPHANUM => true)), 'leadEmail' => array('type' => 'string', 'validator' => array(Scalr_Validator::NOEMPTY => true, Scalr_Validator::EMAIL => true)), 'shared' => array('type' => 'int')));
     if ($this->user->isAdmin()) {
         if ($this->getParam('projectId')) {
             $project = $this->getContainer()->analytics->projects->get($this->getParam('projectId'));
             if (!$project) {
                 throw new Scalr_UI_Exception_NotFound();
             }
         } else {
             $project = new ProjectEntity();
         }
         $cc = $this->getContainer()->analytics->ccs->get($this->getParam('ccId'));
     } else {
         $this->request->restrictAccess(Acl::RESOURCE_ANALYTICS_PROJECTS);
         $project = new ProjectEntity();
         $cc = $this->getContainer()->analytics->ccs->get($this->getEnvironment()->getPlatformConfigValue(Scalr_Environment::SETTING_CC_ID));
         $project->shared = $this->getParam('shared');
         $project->envId = $this->getEnvironment()->id;
         $project->accountId = $this->user->getAccountId();
     }
     $this->request->validate();
     if (!$cc) {
         $this->request->addValidationErrors('ccId', 'Cost center ID should be set');
     }
     if (!$this->request->isValid()) {
         $this->response->data($this->request->getValidationErrors());
         $this->response->failure();
         return;
     }
     //Checks whether billing code specified in the request is already used in another Project
     $criteria = [['name' => ProjectPropertyEntity::NAME_BILLING_CODE], ['value' => $this->getParam('billingCode')]];
     if ($project->projectId !== null) {
         $criteria[] = ['projectId' => ['$ne' => $project->projectId]];
     } else {
         //This is a new record.
         //Email and identifier of the user who creates this record must be set.
         $project->createdById = $this->user->id;
         $project->createdByEmail = $this->user->getEmail();
     }
     $project->name = $this->getParam('name');
     $project->ccId = $cc->ccId;
     $pp = new ProjectPropertyEntity();
     $record = $this->db->GetRow("\n            SELECT " . $project->fields('p') . "\n            FROM " . $project->table('p') . "\n            JOIN " . $pp->table('pp') . " ON pp.project_id = p.project_id\n            WHERE " . $pp->_buildQuery($criteria, 'AND', 'pp')['where'] . "\n            LIMIT 1\n        ");
     if ($record) {
         $found = new ProjectEntity();
         $found->load($record);
     }
     if (!empty($found)) {
         throw new AnalyticsException(sprintf('Billing code "%s" is already used in the Project "%s"', strip_tags($this->getParam('billingCode')), $found->name));
     }
     $this->db->BeginTrans();
     try {
         $project->save();
         $project->saveProperty(ProjectPropertyEntity::NAME_BILLING_CODE, $this->getParam('billingCode'));
         $project->saveProperty(ProjectPropertyEntity::NAME_DESCRIPTION, $this->getParam('description'));
         $project->saveProperty(ProjectPropertyEntity::NAME_LEAD_EMAIL, $this->getParam('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');
 }