Ejemplo n.º 1
0
 /**
  * @param string $query
  * @param string $platform
  * @param string $cloudLocation
  * @param string $scope
  * @param string $osFamily
  * @param string $osId
  * @param string $id
  * @param string $hash
  * @param JsonData $sort
  * @param int $start
  * @param int $limit
  * @param JsonData $hideLocation
  * @param bool $hideNotActive
  * @throws Exception
  */
 public function xListAction($query = null, $platform = null, $cloudLocation = null, $scope = null, $osFamily = null, $osId = null, $id = null, $hash = null, JsonData $sort, $start = 0, $limit = 20, JsonData $hideLocation, $hideNotActive = false)
 {
     $this->restrictAccess('IMAGES');
     $osIds = $criteria = [];
     $accountId = $this->user->getAccountId() ?: NULL;
     $envId = $this->getEnvironmentId(true);
     if ($this->request->getScope() == ScopeInterface::SCOPE_SCALR) {
         $criteria[] = ['accountId' => NULL];
     } else {
         if ($this->request->getScope() == ScopeInterface::SCOPE_ACCOUNT) {
             $criteria[] = ['$or' => [['accountId' => $accountId], ['accountId' => NULL]]];
             $criteria[] = ['envId' => NULL];
         } else {
             $enabledPlatforms = $this->getEnvironment()->getEnabledPlatforms();
             $criteria[] = ['$or' => [['$and' => [['accountId' => NULL], ['platform' => empty($enabledPlatforms) ? NULL : ['$in' => $enabledPlatforms]]]], ['$and' => [['accountId' => $accountId], ['envId' => NULL]]], ['envId' => $envId]]];
         }
     }
     if ($hash) {
         $criteria[] = ['hash' => $hash];
     } else {
         if ($query) {
             $querySql = '%' . $query . '%';
             $criteria[] = ['name' => ['$like' => $querySql]];
         }
         if ($platform) {
             $criteria[] = ['platform' => $platform];
         }
         if ($cloudLocation) {
             $criteria[] = ['cloudLocation' => $cloudLocation];
         }
         if ($scope == ScopeInterface::SCOPE_SCALR) {
             $criteria[] = ['accountId' => NULL];
         } else {
             if ($scope == ScopeInterface::SCOPE_ACCOUNT) {
                 $criteria[] = ['accountId' => $accountId];
                 $criteria[] = ['envId' => NULL];
             } else {
                 if ($scope == ScopeInterface::SCOPE_ENVIRONMENT) {
                     $criteria[] = ['envId' => $envId];
                 }
             }
         }
         if ($osFamily) {
             $osIds = Os::findIdsBy($osFamily);
         }
         if ($osId) {
             $os = Os::find([['id' => $osId]]);
             $osIds = [];
             foreach ($os as $i) {
                 /* @var $i Os */
                 array_push($osIds, $i->id);
             }
         }
         if (!empty($osIds)) {
             $criteria[] = ['osId' => ['$in' => $osIds]];
         }
         if ($id) {
             $criteria[] = ['id' => ['$like' => $id . '%']];
         }
         if ($hideLocation) {
             foreach ($hideLocation as $platform => $locations) {
                 foreach ($locations as $loc) {
                     if ($loc) {
                         $criteria[] = ['$or' => [['platform' => ['$ne' => $platform]], ['cloudLocation' => ['$ne' => $loc]]]];
                     } else {
                         $criteria[] = ['platform' => ['$ne' => $platform]];
                     }
                 }
             }
         }
         if ($hideNotActive) {
             $criteria[] = ['status' => Image::STATUS_ACTIVE];
         }
     }
     $image = new Image();
     $os = new Os();
     $order = \Scalr\UI\Utils::convertOrder($sort, ['dtAdded' => false], ['id', 'platform', 'cloudLocation', 'name', 'osId', 'dtAdded', 'architecture', 'createdByEmail', 'source', 'type']);
     if (!empty($order)) {
         $sOrder = '';
         foreach ($order as $k => $v) {
             if ($k == 'osId') {
                 $sOrder .= ', IF(os.family IS NOT NULL, os.family, images.os_id)' . ($v ? '' : ' DESC') . ", CAST(os.version AS DECIMAL(10,2))" . ($v ? '' : ' DESC');
             } else {
                 $field = $image->getIterator()->getField($k);
                 if (!$field) {
                     throw new InvalidArgumentException(sprintf("Property %s does not exist in %s", $k, get_class($image)));
                 }
                 $sOrder .= ', ' . $field->getColumnName() . ($v ? '' : ' DESC');
             }
         }
         $sOrder = $sOrder != '' ? 'ORDER BY ' . substr($sOrder, 2) : '';
     }
     $result = $this->db->Execute("\n            SELECT " . (isset($limit) ? 'SQL_CALC_FOUND_ROWS ' : '') . $image->fields('images') . "\n            FROM " . $image->table('images') . "\n            LEFT JOIN " . $os->table('os') . " ON {$os->columnId} = {$image->columnOsId}\n            WHERE " . $image->_buildQuery($criteria, 'AND', 'images')['where'] . "\n            " . (!empty($sOrder) ? $sOrder : "") . "\n            " . (isset($limit) ? "LIMIT " . ($start ? intval($start) . ',' : '') . intval($limit) : "") . "\n        ");
     if (isset($limit)) {
         $totalNumber = $this->db->getOne('SELECT FOUND_ROWS()');
     } else {
         $totalNumber = $result->RowCount();
     }
     $data = [];
     while ($rec = $result->FetchRow()) {
         $image = new Image();
         $image->load($rec);
         $s = get_object_vars($image);
         $dtAdded = $image->getDtAdded();
         $s['dtAdded'] = $dtAdded ? Scalr_Util_DateTime::convertTz($dtAdded) : '';
         $s['dtLastUsed'] = $image->dtLastUsed ? Scalr_Util_DateTime::convertTz($image->dtLastUsed) : '';
         $s['used'] = $image->getUsed($accountId, $envId);
         $s['software'] = $image->getSoftwareAsString();
         $s['osFamily'] = $image->getOs()->family;
         $s['osGeneration'] = $image->getOs()->generation;
         $s['osVersion'] = $image->getOs()->version;
         $s['os'] = $image->getOs()->name;
         $s['scope'] = $image->getScope();
         $data[] = $s;
     }
     $this->response->data(['total' => $totalNumber, 'data' => $data]);
 }