Example #1
0
 public function _billingCode($from, $to, $action)
 {
     switch ($action) {
         case static::ACT_CONVERT_TO_OBJECT:
             /* @var $from ProjectEntity */
             $to->billingCode = $from->getProperty(ProjectPropertyEntity::NAME_BILLING_CODE);
             break;
         case static::ACT_CONVERT_TO_ENTITY:
             /* @var $to ProjectEntity */
             break;
         case static::ACT_GET_FILTER_CRITERIA:
             $project = new ProjectEntity();
             $property = new ProjectPropertyEntity();
             return [AbstractEntity::STMT_FROM => $project->table() . " LEFT JOIN " . $property->table() . " ON {$property->columnProjectId} = {$project->columnProjectId}", AbstractEntity::STMT_WHERE => "{$property->columnName} = '" . ProjectPropertyEntity::NAME_BILLING_CODE . "' AND {$property->columnValue} = " . $property->qstr('value', $from->billingCode)];
     }
 }
Example #2
0
 /**
  * Also Removes Project properties generated for test
  *
  * {@inheritdoc}
  */
 public static function tearDownAfterClass()
 {
     //We have to remove CostCenter properties as they don't have foreign keys
     foreach (static::$testData as $rec) {
         if ($rec['class'] === ProjectEntity::class) {
             ProjectPropertyEntity::deleteByProjectId($rec['pk'][0]);
         }
     }
     parent::tearDownAfterClass();
 }
Example #3
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');
 }
Example #4
0
 /**
  * Loads all properties to entity
  */
 public function loadProperties()
 {
     $this->_properties = new ArrayCollection([]);
     foreach (ProjectPropertyEntity::findByProjectId($this->projectId) as $item) {
         $this->_properties[$item->name] = $item;
     }
 }
Example #5
0
 /**
  * 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;
 }
Example #6
0
 /**
  * Also Removes Project properties generated for test
  *
  * {@inheritdoc}
  * @see Scalr\Tests\Functional\Api\ApiTestCase::tearDownAfterClass()
  */
 public static function tearDownAfterClass()
 {
     ksort(static::$testData, SORT_REGULAR);
     foreach (static::$testData as $priority => $data) {
         foreach ($data as $class => $ids) {
             if ($class === 'Scalr\\Stats\\CostAnalytics\\Entity\\ProjectEntity') {
                 $ids = array_unique($ids, SORT_REGULAR);
                 foreach ($ids as $entry) {
                     /* @var $project ProjectEntity */
                     $project = $class::findPk(...$entry);
                     if (!empty($project)) {
                         try {
                             ProjectPropertyEntity::deleteByProjectId($project->projectId);
                             $project->delete();
                         } catch (\Exception $e) {
                             \Scalr::logException($e);
                         }
                     }
                 }
                 unset(static::$testData[$priority][$class]);
             }
         }
     }
     parent::tearDownAfterClass();
 }