summaryrefslogtreecommitdiff
path: root/lib/VNWeb/Tags/Links.pm
blob: e329452086dc693163aa450759d8193a917b8677 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package VNWeb::Tags::Links;

use VNWeb::Prelude;
use VNWeb::Tags::Lib;


sub listing_ {
    my($opt, $lst, $np, $url) = @_;

    paginate_ $url, $opt->{p}, $np, 't';
    div_ class => 'mainbox browse taglinks', sub {
        table_ class => 'stripe', sub {
            thead_ sub { tr_ sub {
                    td_ class => 'tc1', sub { txt_ 'Date'; sortable_ 'date', $opt, $url; debug_ $lst; };
                    td_ class => 'tc2', 'User';
                    td_ class => 'tc3', 'Rating';
                    td_ class => 'tc4', sub { txt_ 'Tag';  sortable_ 'tag', $opt, $url };
                    td_ class => 'tc5', 'Spoiler';
                    td_ class => 'tc6', 'Visual novel';
                    td_ class => 'tc7', 'Note';
                }};
            tr_ sub {
                my $i = $_;
                td_ class => 'tc1', fmtdate $i->{date};
                td_ class => 'tc2', sub {
                    a_ href => $url->(u => $i->{uid}, p=>undef), class => 'setfil', '> ' if $i->{uid} && !defined $opt->{u};
                    user_ $i;
                };
                td_ class => 'tc3', sub { tagscore_ $i->{vote}, $i->{ignore} };
                td_ class => 'tc4', sub {
                    a_ href => $url->(t => $i->{tag}, p=>undef), class => 'setfil', '> ' if !defined $opt->{t};
                    a_ href => "/g$i->{tag}", $i->{name};
                };
                td_ class => 'tc5', sub {
                    my $s = !defined $i->{spoiler} ? '' : fmtspoil $i->{spoiler};
                    b_ class => 'grayedout', $s if $i->{ignore};
                    txt_ $s if !$i->{ignore};
                };
                td_ class => 'tc6', sub {
                    a_ href => $url->(v => $i->{vid}, p=>undef), class => 'setfil', '> ' if !defined $opt->{v};
                    a_ href => "/v$i->{vid}", shorten $i->{title}, 50;
                };
                td_ class => 'tc7', $i->{notes};
            } for @$lst;
        };
    };
    paginate_ $url, $opt->{p}, $np, 'b';
}


TUWF::get qr{/g/links}, sub {
    my $opt = tuwf->validate(get =>
        p => { page => 1 },
        o => { onerror => 'd', enum => ['a', 'd'] },
        s => { onerror => 'date', enum => [qw|date tag|] },
        v => { onerror => undef, id => 1 },
        u => { onerror => undef, id => 1 },
        t => { onerror => undef, id => 1 },
    )->data;

    my $where = sql_and
        defined $opt->{v} ? sql('tv.vid =', \$opt->{v}) : (),
        defined $opt->{u} ? sql('tv.uid =', \$opt->{u}) : (),
        defined $opt->{t} ? sql('tv.tag =', \$opt->{t}) : ();

    my $filt = defined $opt->{u} || defined $opt->{t} || defined $opt->{v};

    my $count = $filt && tuwf->dbVali('SELECT COUNT(*) FROM tags_vn tv WHERE', $where);
    my($lst, $np) = tuwf->dbPagei({ page => $opt->{p}, results => 50 }, '
        SELECT tv.vid, tv.uid, tv.tag, tv.vote, tv.spoiler,', sql_totime('tv.date'), 'as date, tv.ignore OR (u.id IS NOT NULL AND NOT u.perm_tag) AS ignore, tv.notes, v.title,', sql_user(), ', t.name
          FROM tags_vn tv
          JOIN vn v ON v.id = tv.vid
          LEFT JOIN users u ON u.id = tv.uid
          JOIN tags t ON t.id = tv.tag
         WHERE', $where, '
         ORDER BY', { date => 'tv.date', tag => 't.name' }->{$opt->{s}}, { a => 'ASC', d => 'DESC' }->{$opt->{o}}
    );
    $np = [ $count, 50 ] if $count;

    my sub url { '?'.query_encode %$opt, @_ }

    framework_ title => 'Tag link browser', sub {
        div_ class => 'mainbox', sub {
            h1_ 'Tag link browser';
            if($filt) {
                p_ 'Active filters:';
                ul_ sub {
                    li_ sub {
                        txt_ '['; a_ href => url(u=>undef, p=>undef), 'remove'; txt_ '] ';
                        txt_ 'User: ';
                        user_ tuwf->dbRowi('SELECT', sql_user(), 'FROM users u WHERE id=', \$opt->{u});
                    } if defined $opt->{u};
                    li_ sub {
                        txt_ '['; a_ href => url(t=>undef, p=>undef), 'remove'; txt_ '] ';
                        txt_ 'Tag:'; txt_ ' ';
                        a_ href => "/g$opt->{t}", tuwf->dbVali('SELECT name FROM tags WHERE id=', \$opt->{t})||'Unknown tag';
                    } if defined $opt->{t};
                    li_ sub {
                        txt_ '['; a_ href => url(v=>undef, p=>undef), 'remove'; txt_ '] ';
                        txt_ 'Visual novel'; txt_ ' ';
                        a_ href => "/v$opt->{v}", tuwf->dbVali('SELECT title FROM vn WHERE id=', \$opt->{v})||'Unknown VN';
                    } if defined $opt->{v};
                }
            }
            if($lst && @$lst) {
                br_;
                p_ 'Click the arrow before a user, tag or VN to add it as a filter.'
                    unless defined $opt->{u} && defined $opt->{t} && defined $opt->{v};
            } else {
                br_;
                p_ 'No tag votes matching the requested filters.';
            }
        };

        listing_ $opt, $lst, $np, \&url if $lst && @$lst;
    };
};

1;