diff --git a/CHANGELOG b/CHANGELOG index ab0ddd2..d6f89d7 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ Revision history for Perl application Lstu 0.24-0 ????-??-?? + - ✨ Allow to sort results in admin stats page (#91) 0.23-1 2021-04-?? - Fix disabled URLs still showing in admin interface (#79) diff --git a/lib/Lstu/Controller/Stats.pm b/lib/Lstu/Controller/Stats.pm index 6ce4eac..28b8d1b 100644 --- a/lib/Lstu/Controller/Stats.pm +++ b/lib/Lstu/Controller/Stats.pm @@ -61,7 +61,22 @@ sub fullstats { } sub stats { - my $c = shift; + my $c = shift; + my $order = $c->param('order') // 'counter'; + my $dir = $c->param('dir') // '-desc'; + + my %orders = ( + 'short' => 1, + 'url' => 1, + 'counter' => 1, + 'created_by' => 1, + ); + my %dirs = (-desc => 1, -asc => 1); + + $order = 'counter' unless $orders{$order}; + $dir = '-desc' unless $dirs{$dir}; + + if ((!defined($c->config('ldap')) && !defined($c->config('htpasswd'))) || $c->is_user_authenticated) { my $db_session = Lstu::DB::Session->new( app => $c, @@ -77,7 +92,8 @@ sub stats { my @urls = Lstu::DB::URL->new( app => $c, - )->paginate($page, $c->config('page_offset')); + )->paginate($page, $c->config('page_offset'), $order, $dir); + $c->respond_to( json => sub { my $c = shift; diff --git a/lib/Lstu/DB/URL.pm b/lib/Lstu/DB/URL.pm index 23e3b12..a4c9646 100644 --- a/lib/Lstu/DB/URL.pm +++ b/lib/Lstu/DB/URL.pm @@ -300,8 +300,10 @@ sub paginate { my $c = shift; my $page = shift; my $page_offset = shift; + my $order = shift // 'counter'; + my $dir = shift // '-desc'; - return @{$c->app->dbi->db->query('SELECT * FROM lstu WHERE url IS NOT NULL AND disabled = 0 ORDER BY counter DESC LIMIT ? offset ?', $page_offset, $page * $page_offset)->hashes->to_array}; + return @{$c->app->dbi->db->select('lstu', undef, { url => { '!=', undef }, disabled => 0 }, { order_by => { $dir => $order }, limit => $page_offset, offset => $page * $page_offset })->hashes->to_array}; } =head2 get_a_lot @@ -353,7 +355,7 @@ eg: C sub total { my $c = shift; - return $c->app->dbi->db->query('SELECT count(short) AS count FROM lstu WHERE url IS NOT NULL')->hashes->first->{count}; + return $c->app->dbi->db->query('SELECT count(short) AS count FROM lstu WHERE url IS NOT NULL AND disabled = 0')->hashes->first->{count}; } =head2 delete_all diff --git a/lib/Lstu/DB/URL/SQLite.pm b/lib/Lstu/DB/URL/SQLite.pm index a5de101..6e8a39e 100644 --- a/lib/Lstu/DB/URL/SQLite.pm +++ b/lib/Lstu/DB/URL/SQLite.pm @@ -12,4 +12,15 @@ sub new { return $c; } +sub paginate { + my $c = shift; + my $page = shift; + my $page_offset = shift; + my $order = shift // 'counter'; + my $dir = shift // '-desc'; + $dir =~ s/^-//; + + return @{$c->app->dbi->db->query("SELECT * FROM lstu WHERE url IS NOT NULL AND disabled = 0 ORDER BY $order $dir LIMIT ? OFFSET ?", $page_offset, $page * $page_offset)->hashes->to_array}; +} + 1; diff --git a/themes/default/lib/Lstu/I18N/lstu.pot b/themes/default/lib/Lstu/I18N/lstu.pot index 3ef3a48..d963707 100644 --- a/themes/default/lib/Lstu/I18N/lstu.pot +++ b/themes/default/lib/Lstu/I18N/lstu.pot @@ -28,7 +28,7 @@ msgstr "" msgid "About" msgstr "" -#: themes/default/templates/stats.html.ep:13 themes/default/templates/stats.html.ep:14 +#: themes/default/templates/stats.html.ep:18 themes/default/templates/stats.html.ep:19 msgid "Admin password" msgstr "" @@ -44,11 +44,11 @@ msgstr "" msgid "Copy to clipboard" msgstr "" -#: themes/default/templates/stats.html.ep:46 +#: themes/default/templates/stats.html.ep:61 msgid "Counter" msgstr "" -#: themes/default/templates/stats.html.ep:47 +#: themes/default/templates/stats.html.ep:66 msgid "Created" msgstr "" @@ -56,11 +56,11 @@ msgstr "" msgid "Custom shortened text" msgstr "" -#: themes/default/templates/stats.html.ep:52 +#: themes/default/templates/stats.html.ep:73 msgid "Delete" msgstr "" -#: themes/default/templates/stats.html.ep:36 +#: themes/default/templates/stats.html.ep:41 msgid "Export your URLs" msgstr "" @@ -76,7 +76,7 @@ msgstr "" msgid "Get the details (visit counter, creation date, etc.) of a shortened URL." msgstr "" -#: themes/default/templates/index.html.ep:12 themes/default/templates/stats.html.ep:17 +#: themes/default/templates/index.html.ep:12 themes/default/templates/stats.html.ep:22 msgid "Go!" msgstr "" @@ -84,7 +84,7 @@ msgstr "" msgid "Hit Enter, then Ctrl+C to copy the short link" msgstr "" -#: themes/default/templates/stats.html.ep:89 +#: themes/default/templates/stats.html.ep:110 msgid "Home" msgstr "" @@ -109,7 +109,7 @@ msgstr "" msgid "If you are logged in as admin, you can provide a \"page\" parameter" msgstr "" -#: themes/default/templates/stats.html.ep:32 themes/default/templates/stats.html.ep:35 +#: themes/default/templates/stats.html.ep:37 themes/default/templates/stats.html.ep:40 msgid "Import URLs" msgstr "" @@ -137,11 +137,11 @@ msgstr "" msgid "Logout" msgstr "" -#: themes/default/templates/stats.html.ep:10 +#: themes/default/templates/stats.html.ep:15 msgid "Logout from admin stats" msgstr "" -#: themes/default/templates/stats.html.ep:84 +#: themes/default/templates/stats.html.ep:105 msgid "Next" msgstr "" @@ -162,11 +162,11 @@ msgstr "" msgid "Please, check your credentials: unable to authenticate." msgstr "" -#: themes/default/templates/stats.html.ep:82 +#: themes/default/templates/stats.html.ep:103 msgid "Previous" msgstr "" -#: themes/default/templates/stats.html.ep:50 +#: themes/default/templates/stats.html.ep:71 msgid "QRCode" msgstr "" @@ -182,7 +182,7 @@ msgstr "" msgid "Response:" msgstr "" -#: themes/default/templates/stats.html.ep:45 +#: themes/default/templates/stats.html.ep:56 msgid "Shortened URL" msgstr "" @@ -198,7 +198,7 @@ msgstr "" msgid "Sorry, unable to parse the provided file" msgstr "" -#: themes/default/templates/layouts/default.html.ep:60 themes/default/templates/layouts/default.html.ep:66 themes/default/templates/stats.html.ep:4 +#: themes/default/templates/layouts/default.html.ep:60 themes/default/templates/layouts/default.html.ep:66 themes/default/templates/stats.html.ep:9 msgid "Statistics" msgstr "" @@ -223,7 +223,7 @@ msgid "The URL you want to shorten comes from a domain (%1) that is blacklisted msgstr "" #. ($c->url_for('/') -#: lib/Lstu/Controller/Admin.pm:131 lib/Lstu/Controller/Stats.pm:184 lib/Lstu/Controller/URL.pm:242 +#: lib/Lstu/Controller/Admin.pm:131 lib/Lstu/Controller/Stats.pm:200 lib/Lstu/Controller/URL.pm:242 msgid "The shortened URL %1 doesn't exist." msgstr "" @@ -249,7 +249,7 @@ msgstr "" msgid "Too many bad passwords. You're banned." msgstr "" -#: themes/default/templates/api.html.ep:104 themes/default/templates/api.html.ep:95 themes/default/templates/stats.html.ep:44 +#: themes/default/templates/api.html.ep:104 themes/default/templates/api.html.ep:95 themes/default/templates/stats.html.ep:51 msgid "URL" msgstr "" diff --git a/themes/default/templates/stats.html.ep b/themes/default/templates/stats.html.ep index bc682e5..35bf6fb 100644 --- a/themes/default/templates/stats.html.ep +++ b/themes/default/templates/stats.html.ep @@ -1,6 +1,11 @@ % # vim:set sw=4 ts=4 sts=4 ft=html.epl expandtab: % title 'Lstu stats'; % use Mojo::Date; +% my $order = $self->param('order') // 'counter'; +% my $dir = $self->param('dir') // '-desc'; +% my $arrow = ($dir eq '-desc') ? '↓' : '↑'; +% my $adir = sprintf('&dir=%s', ($dir eq '-desc') ? '-asc' : '-desc'); +% $dir = sprintf('&dir=%s', $dir);

<%= l('Statistics') %>

% if (defined(config('adminpwd')) || defined(config('hashed_adminpwd'))) { % if (!flash('banned')) { @@ -41,10 +46,41 @@ - - - - +% if ((defined(config('adminpwd')) || defined(config('hashed_adminpwd'))) && defined(stash('admin'))) { + + + + +% } else { + + + + +% } % if (defined(config('adminpwd')) || defined(config('hashed_adminpwd'))) { % unless (defined(stash('admin'))) {
#<%= l('URL') %><%= l('Shortened URL') %><%= l('Counter') %><%= l('Created') %> + + <%= l('URL') %><%= $arrow if ($order eq 'url') %> + + + + <%= l('Shortened URL') %><%= $arrow if ($order eq 'short') %> + + + + <%= l('Counter') %><%= $arrow if ($order eq 'counter') %> + + + + <%= l('Created') %><%= $arrow if ($order eq 'created_by') %> + + + <%= l('URL') %> + + <%= l('Shortened URL') %> + + <%= l('Counter') %> + + <%= l('Created') %> + <%= l('QRCode') %>