summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2020-09-29 16:44:12 +0200
committerYorhel <git@yorhel.nl>2020-09-29 16:44:14 +0200
commit17e6e0ca9a055c465484b32c8e05625fadf9b44a (patch)
tree1c778efe0c2449b15e1826eb576d0a3179b01c58
parent7e4b41874ff68c1f470240ad8e550d1e867f4ac4 (diff)
v2rw/Tag::List: Re-use tag listing to implement trait listing
Duplicating that code was really too much, the listings are way too similar.
-rw-r--r--data/style.css5
-rw-r--r--lib/VNDB/Handler/Traits.pm72
-rw-r--r--lib/VNWeb/Tags/List.pm45
3 files changed, 28 insertions, 94 deletions
diff --git a/data/style.css b/data/style.css
index f83076f9..74012057 100644
--- a/data/style.css
+++ b/data/style.css
@@ -906,11 +906,6 @@ div.uposts td.tc4 b { margin-left: 10px }
.browse.taglist tbody .tc3 a { margin-right: 10px }
-/***** Trait list (/i/list) *****/
-
-.browse.traitlist .tc1 { width: 120px; white-space: nowrap }
-
-
/***** Tag links *****/
.browse.taglinks .tc1 { width: 70px }
diff --git a/lib/VNDB/Handler/Traits.pm b/lib/VNDB/Handler/Traits.pm
index 3ed6b43c..ec7fc7c6 100644
--- a/lib/VNDB/Handler/Traits.pm
+++ b/lib/VNDB/Handler/Traits.pm
@@ -9,7 +9,6 @@ use VNDB::Func;
TUWF::register(
qr{i([1-9]\d*)}, \&traitpage,
- qr{i/list}, \&traitlist,
qr{i}, \&traitindex,
qr{xml/traits\.xml}, \&traitxml,
);
@@ -131,77 +130,6 @@ sub traitpage {
}
-sub traitlist {
- my $self = shift;
-
- my $f = $self->formValidate(
- { get => 's', required => 0, default => 'name', enum => ['added', 'name'] },
- { get => 'o', required => 0, default => 'a', enum => ['a', 'd'] },
- { get => 'p', required => 0, default => 1, template => 'page' },
- { get => 't', required => 0, default => -1, enum => [ -1..2 ] },
- { get => 'q', required => 0, default => '' },
- );
- return $self->resNotFound if $f->{_err};
-
- my($t, $np) = $self->dbTraitGet(
- sort => $f->{s}, reverse => $f->{o} eq 'd',
- page => $f->{p},
- results => 50,
- state => $f->{t},
- search => $f->{q}
- );
-
- $self->htmlHeader(title => 'Browse traits');
- div class => 'mainbox';
- h1 'Browse traits';
- form action => '/i/list', 'accept-charset' => 'UTF-8', method => 'get';
- input type => 'hidden', name => 't', value => $f->{t};
- $self->htmlSearchBox('i', $f->{q});
- end;
- p class => 'browseopts';
- a href => "/i/list?q=$f->{q};t=-1", $f->{t} == -1 ? (class => 'optselected') : (), 'All';
- a href => "/i/list?q=$f->{q};t=0", $f->{t} == 0 ? (class => 'optselected') : (), 'Awaiting moderation';
- a href => "/i/list?q=$f->{q};t=1", $f->{t} == 1 ? (class => 'optselected') : (), 'Deleted';
- a href => "/i/list?q=$f->{q};t=2", $f->{t} == 2 ? (class => 'optselected') : (), 'Accepted';
- end;
- if(!@$t) {
- p 'No results found';
- }
- end 'div';
- if(@$t) {
- $self->htmlBrowse(
- class => 'traitlist',
- options => $f,
- nextpage => $np,
- items => $t,
- pageurl => "/i/list?t=$f->{t};q=$f->{q};s=$f->{s};o=$f->{o}",
- sorturl => "/i/list?t=$f->{t};q=$f->{q}",
- header => [
- [ 'Created', 'added' ],
- [ 'Trait', 'name' ],
- ],
- row => sub {
- my($s, $n, $l) = @_;
- Tr;
- td class => 'tc1', fmtage $l->{added};
- td class => 'tc3';
- if($l->{group}) {
- b class => 'grayedout', $l->{groupname}.' / ';
- }
- a href => "/i$l->{id}", $l->{name};
- if($f->{t} == -1) {
- b class => 'grayedout', ' awaiting moderation' if $l->{state} == 0;
- b class => 'grayedout', ' deleted' if $l->{state} == 1;
- }
- end;
- end 'tr';
- }
- );
- }
- $self->htmlFooter;
-}
-
-
sub traitindex {
my $self = shift;
diff --git a/lib/VNWeb/Tags/List.pm b/lib/VNWeb/Tags/List.pm
index d19dc19a..77d1c6ec 100644
--- a/lib/VNWeb/Tags/List.pm
+++ b/lib/VNWeb/Tags/List.pm
@@ -1,10 +1,12 @@
+# XXX: Also used for the trait listing
+
package VNWeb::Tags::List;
use VNWeb::Prelude;
sub listing_ {
- my($opt, $list, $count) = @_;
+ my($type, $opt, $list, $count) = @_;
my sub url { '?'.query_encode %$opt, @_ }
@@ -13,14 +15,15 @@ sub listing_ {
table_ class => 'stripe', sub {
thead_ sub { tr_ sub {
td_ class => 'tc1', sub { txt_ 'Created'; sortable_ 'added', $opt, \&url };
- td_ class => 'tc2', sub { txt_ 'VNs'; sortable_ 'vns', $opt, \&url };
+ td_ class => 'tc2', sub { txt_ $type eq 'g' ? 'VNs' : 'Chars'; sortable_ 'items', $opt, \&url };
td_ class => 'tc3', sub { txt_ 'Name'; sortable_ 'name', $opt, \&url };
} };
tr_ sub {
td_ class => 'tc1', fmtage $_->{added};
td_ class => 'tc2', $_->{c_items}||'-';
td_ class => 'tc3', sub {
- a_ href => "/g$_->{id}", $_->{name};
+ b_ class => 'grayedout', "$_->{group} / " if $_->{group};
+ a_ href => "/$type$_->{id}", $_->{name};
join_ ',', sub { b_ class => 'grayedout', ' '.$_ },
$_->{state} == 0 ? 'awaiting moderation' : $_->{state} == 1 ? 'deleted' : (),
!$_->{applicable} ? 'not applicable' : (),
@@ -33,9 +36,10 @@ sub listing_ {
}
-TUWF::get qr{/g/list}, sub {
+TUWF::get qr{/(?<type>[gi])/list}, sub {
+ my $type = tuwf->capture('type');
my $opt = tuwf->validate(get =>
- s => { onerror => 'name', enum => ['added', 'name', 'vns'] },
+ s => { onerror => 'name', enum => ['added', 'name', 'vns', 'items'] },
o => { onerror => 'a', enum => ['a', 'd'] },
p => { upage => 1 },
t => { onerror => undef, enum => [ -1..2 ] },
@@ -43,7 +47,7 @@ TUWF::get qr{/g/list}, sub {
b => { undefbool => 1 },
q => { onerror => '' },
)->data;
-
+ $opt->{s} = 'items' if $opt->{s} eq 'vns';
$opt->{t} = undef if $opt->{t} && $opt->{t} == -1; # for legacy URLs
my $qs = $opt->{q} && '%'.sql_like($opt->{q}).'%';
@@ -51,21 +55,28 @@ TUWF::get qr{/g/list}, sub {
defined $opt->{t} ? sql 't.state =', \$opt->{t} : (),
defined $opt->{a} ? sql 't.applicable =', \$opt->{a} : (),
defined $opt->{b} ? sql 't.searchable =', \$opt->{b} : (),
- $opt->{q} ? sql 't.name ILIKE', \$qs, 'OR t.id IN(SELECT tag FROM tags_aliases WHERE alias ILIKE', \$qs, ')' : ();
+ $type eq 'g' ? (
+ $opt->{q} ? sql 't.name ILIKE', \$qs, 'OR t.id IN(SELECT tag FROM tags_aliases WHERE alias ILIKE', \$qs, ')' : ()
+ ) : (
+ $opt->{q} ? sql 't.name ILIKE', \$qs, 'OR t.alias ILIKE', \$qs : ()
+ );
- my $count = tuwf->dbVali('SELECT COUNT(*) FROM tags t WHERE', $where);
+ my $table = $type eq 'g' ? 'tags' : 'traits';
+ my $count = tuwf->dbVali("SELECT COUNT(*) FROM $table t WHERE", $where);
my $list = tuwf->dbPagei({ results => 50, page => $opt->{p} },'
- SELECT t.id, t.name, t.state, t.searchable, t.applicable, t.cat, t.c_items,', sql_totime('t.added'), 'as added
- FROM tags t
- WHERE ', $where, '
- ORDER BY', {qw|added id name name vns c_items|}->{$opt->{s}}, {qw|a ASC d DESC|}->{$opt->{o}}, ', id'
+ SELECT t.id, t.name, t.state, t.searchable, t.applicable, t.c_items,', sql_totime('t.added'), "as added
+ FROM $table t
+ WHERE ", $where, '
+ ORDER BY', {qw|added id name name items c_items|}->{$opt->{s}}, {qw|a ASC d DESC|}->{$opt->{o}}, ', id'
);
- framework_ title => 'Browse tags', index => 1, sub {
+ enrich_merge id => 'SELECT t.id, g.name AS "group" FROM traits t JOIN traits g ON g.id = t."group" WHERE t.id IN', $list if $type eq 'i';
+
+ framework_ title => "Browse $table", index => 1, sub {
div_ class => 'mainbox', sub {
- h1_ 'Browse tags';
- form_ action => '/g/list', method => 'get', sub {
- searchbox_ g => $opt->{q};
+ h1_ "Browse $table";
+ form_ action => "/$type/list", method => 'get', sub {
+ searchbox_ $type => $opt->{q};
};
my sub opt_ {
my($k,$v,$lbl) = @_;
@@ -88,7 +99,7 @@ TUWF::get qr{/g/list}, sub {
opt_ b => 1, 'Searchable';
};
};
- listing_ $opt, $list, $count if $count;
+ listing_ $type, $opt, $list, $count if $count;
};
};