Skip to content

Commit

Permalink
Refactor items per page code
Browse files Browse the repository at this point in the history
  • Loading branch information
Usbac committed Dec 30, 2024
1 parent 8c47498 commit 3c850ae
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/bootstrap/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'date_format' => 'dd MMM Y HH:mm',
'db' => $db,
'content' => 'public/content',
'per_page' => 1,
'mail' => fn($to, $subject, $message) => mail($to, $subject, $message),
'views' => 'app/views',
];
2 changes: 1 addition & 1 deletion app/bootstrap/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ function setting(?string $key = null): mixed
\Aurora\App\Setting::set($settings);
\Aurora\App\Media::setDirectory($kernel->config('content'));

(require('routes.php'))($kernel->router, $db, $view, $lang);
(require('routes.php'))($kernel, $db, $view, $lang);
};
14 changes: 7 additions & 7 deletions app/bootstrap/routes.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?php

use Aurora\Core\{DB, Helper, Kernel, Language, Route, View};
use Aurora\Core\{DB, Helper, Kernel, Language, View};

const ITEMS_PER_PAGE = 20;

return function (Route $router, DB $db, View $view, Language $lang) {
return function (\Aurora\Core\Kernel $kernel, DB $db, View $view, Language $lang) {
$user_mod = new \Aurora\App\Modules\User($db, $lang);
$tag_mod = new \Aurora\App\Modules\Tag($db, $lang);
$link_mod = new \Aurora\App\Modules\Link($db, $lang);
Expand All @@ -14,6 +12,7 @@
$blog_url = \Aurora\App\Setting::get('blog_url');
$theme_dir = 'themes/' . \Aurora\App\Setting::get('theme');
$rss = \Aurora\App\Setting::get('rss');
$router = $kernel->router;

$router->middleware('*', function() use ($db, $view, $lang, $theme_dir) {
if (Helper::isValidId($_SESSION['user']['id'] ?? false)) {
Expand Down Expand Up @@ -809,7 +808,7 @@
return json_encode([ 'success' => $success ]);
});

$router->get('json:admin/{mod}/page', function() use ($view, $page_mod, $post_mod, $user_mod, $tag_mod, $link_mod) {
$router->get('json:admin/{mod}/page', function() use ($kernel, $view, $page_mod, $post_mod, $user_mod, $tag_mod, $link_mod) {
$mod_str = $_GET['mod'] ?? '';
switch ($mod_str) {
case 'pages': $mod = $page_mod; break;
Expand All @@ -829,13 +828,14 @@
return;
}

$per_page = $kernel->config('per_page');
$where = $mod->getCondition($_GET);

return json_encode([
'next_page' => $mod->isNextPageAvailable($_GET['page'], ITEMS_PER_PAGE, $where),
'next_page' => $mod->isNextPageAvailable($_GET['page'], $per_page, $where),
'count' => $mod->count($where),
'html' => $view->get("admin/partials/lists/$mod_str.html", [
$mod_str => $mod->getPage($_GET['page'], ITEMS_PER_PAGE, $where, $_GET['order'] ?? $mod::DEFAULT_ORDER, ($_GET['sort'] ?? ($mod::DEFAULT_SORT ?? 'asc')) == 'asc'),
$mod_str => $mod->getPage($_GET['page'], $per_page, $where, $_GET['order'] ?? $mod::DEFAULT_ORDER, ($_GET['sort'] ?? ($mod::DEFAULT_SORT ?? 'asc')) == 'asc'),
]),
]);
});
Expand Down

0 comments on commit 3c850ae

Please sign in to comment.