/** * @Route("/", methods="GET") * @Request({"filter": "array", "page":"int"}) */ public function indexAction($filter = [], $page = 0) { $query = File::query()->select('f.*')->from('@download_file f'); $filter = array_merge(array_fill_keys(['status', 'category_id', 'search', 'order', 'limit'], ''), $filter); extract($filter, EXTR_SKIP); if ($search) { $query->where(function ($query) use($search) { $query->orWhere(['title LIKE :search'], ['search' => "%{$search}%"]); }); } if (is_numeric($status)) { $query->where(['status' => (int) $status]); } if (is_numeric($category_id)) { $query->select('x.catordering')->innerJoin('@download_files_categories x', 'x.file_id = f.id')->where(['x.category_id' => (int) $category_id]); } if (!preg_match('/^(date|downloads|title)\\s(asc|desc)$/i', $order, $order)) { $order = [1 => 'date', 2 => 'desc']; } $limit = (int) $limit ?: App::module('bixie/download')->config('files_per_page'); $count = $query->count(); $pages = ceil($count / $limit); $page = max(0, min($pages - 1, $page)); $files = array_values($query->related('categories')->offset($page * $limit)->limit($limit)->orderBy($order[1], $order[2])->get()); return compact('files', 'pages', 'count'); }