Scalr_UI_Controller_Images::xListAction PHP Method

xListAction() public method

public xListAction ( string $query = null, string $platform = null, string $cloudLocation = null, string $scope = null, string $osFamily = null, string $osId = null, string $id = null, string $hash = null, integer $isScalarized = null, JsonData $sort, integer $start, integer $limit = 20, boolean $hideNotActive = false, boolean $useHashAsFilter = false )
$query string
$platform string
$cloudLocation string
$scope string
$osFamily string
$osId string
$id string
$hash string
$isScalarized integer
$sort Scalr\UI\Request\JsonData
$start integer
$limit integer
$hideNotActive boolean
$useHashAsFilter boolean
    public function xListAction($query = null, $platform = null, $cloudLocation = null, $scope = null, $osFamily = null, $osId = null, $id = null, $hash = null, $isScalarized = null, JsonData $sort, $start = 0, $limit = 20, $hideNotActive = false, $useHashAsFilter = false)
    {
        $this->request->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 && !$useHashAsFilter) {
            $criteria[] = ['hash' => $hash];
        } else {
            if ($query) {
                $querySql = '%' . $query . '%';
                $criteria[] = ['name' => ['$like' => $querySql]];
            }
            if ($platform) {
                $criteria[] = ['platform' => $platform];
            }
            if ($cloudLocation) {
                $criteria[] = ['cloudLocation' => $cloudLocation];
            }
            if ($isScalarized) {
                $criteria[] = ['$or' => [['isScalarized' => 1], ['hasCloudInit' => 1]]];
            }
            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 ($hideNotActive) {
                $criteria[] = ['status' => Image::STATUS_ACTIVE];
            }
            if ($hash) {
                $criteria[] = ['hash' => $hash];
            }
        }
        $image = new Image();
        $os = new Os();
        $order = 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);
            $data[] = $this->convertEntityToArray($image);
        }
        $this->response->data(['total' => $totalNumber, 'data' => $data]);
    }