/** * Finds projects by key * It searches by name or billing number * * @param string $key optional Search key * @return ArrayCollection Returns collection of the ProjectEntity objects */ public function findByKey($key = null) { if (is_null($key) || $key === '') { return $this->all(); } $collection = new ArrayCollection(); $projectEntity = new ProjectEntity(); //Includes archived projects $projectPropertyEntity = new ProjectPropertyEntity(); //Cost center entity $ccEntity = new CostCentreEntity(); $rs = $this->db->Execute("\n SELECT " . $projectEntity->fields('p') . ", " . $ccEntity->fields('c', true) . "\n FROM " . $projectEntity->table('p') . "\n LEFT JOIN " . $ccEntity->table('c') . " ON c.`cc_id` = p.`cc_id`\n WHERE p.`name` LIKE ?\n OR EXISTS (\n SELECT 1 FROM " . $projectPropertyEntity->table('pp') . "\n WHERE `pp`.project_id = `p`.`project_id`\n AND `pp`.`name` = ? AND `pp`.`value` LIKE ?\n )\n ", ['%' . $key . '%', ProjectPropertyEntity::NAME_BILLING_CODE, '%' . $key . '%']); while ($rec = $rs->FetchRow()) { $item = new ProjectEntity(); $item->load($rec); if ($rec['c_cc_id']) { $cc = new CostCentreEntity(); $cc->load($rec, 'c'); $item->setCostCenter($cc); } $collection->append($item); } return $collection; }
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'); }
/** * Checks if user has permissions to project in environment or account scope * * @param string $projectId Identifier of the project * @param array $criteria ['envId' => '', 'clientid' => ''] * @return bool|mixed */ public function checkPermission($projectId, array $criteria) { $and = ''; foreach ($criteria as $name => $value) { $field = 'f.' . \Scalr::decamelize($name); $and .= " AND " . $field . "=" . $this->db->escape($value); } $projectEntity = new ProjectEntity(); $projectId = $projectEntity->type('projectId')->toDb($projectId); $where = " WHERE p.project_id = UNHEX('" . $projectId . "') AND EXISTS (\n SELECT * FROM farms f\n LEFT JOIN farm_settings fs ON f.id = fs.farmid\n WHERE fs.name = '" . Entity\FarmSetting::PROJECT_ID . "'\n AND REPLACE(fs.value, '-', '') = HEX(p.project_id)\n {$and})"; $sql = "SELECT " . $projectEntity->fields('p') . "\n FROM " . $projectEntity->table('p') . $where; return $this->db->GetOne($sql); }