summaryrefslogtreecommitdiff
path: root/lib/VNDB/Handler/VNBrowse.pm
blob: 69ee30a5c8aa7ef1470129f484a87904061e8aa3 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

package VNDB::Handler::VNBrowse;

use strict;
use warnings;
use YAWF ':html';
use VNDB::Func;


YAWF::register(
  qr{v/([a-z0]|all)}  => \&list,
);


sub list {
  my($self, $char) = @_;

  my $f = $self->formValidate(
    { name => 's', required => 0, default => 'title', enum => [ qw|title rel| ] },
    { name => 'o', required => 0, default => 'a', enum => [ 'a','d' ] },
    { name => 'p', required => 0, default => 1, template => 'int' },
    { name => 'q', required => 0, default => '' },
    { name => 'sq', required => 0, default => '' },
  );
  return 404 if $f->{_err};
  $f->{q} ||= $f->{sq};

  # NOTE: this entire search thingy can also be done using a PgSQL fulltext search,
  #  which is faster and requires less code. It does require an extra database
  #  column, index and some triggers, though

  my(@cati, @cate, @plat, @lang);
  my $q = $f->{q};
  if($q) {
   # VNDBID
    return $self->resRedirect('/'.$1.$2.(!$3 ? '' : $1 eq 'd' ? '#'.$3 : '.'.$3), 'temp')
      if $q =~ /^([vrptud])([0-9]+)(?:\.([0-9]+))?$/;

    if(!($q =~ s/^title://)) {
     # categories
      my %catl = map {
        my $ic = $_;
        map { $ic.$_ => $self->{categories}{$ic}[1]{$_} } keys %{$self->{categories}{$ic}[1]}
      } keys %{$self->{categories}};

      $q =~ s/-(?:$catl{$_}|c:$_)//ig && push @cate, $_ for keys %catl;
      $q =~ s/(?:$catl{$_}|c:$_)//ig && push @cati, $_ for keys %catl;

     # platforms
      $_ ne 'oth' && $q =~ s/(?:$self->{platforms}{$_}|p:$_)//ig && push @plat, $_ for keys %{$self->{platforms}};

     # languages
      $q =~ s/($self->{languages}{$_}|l:$_)//ig && push @lang, $_ for keys %{$self->{languages}};
    }
  }
  $q =~ s/ +$//;
  $q =~ s/^ +//;

  my($list, $np) = $self->dbVNGet(
    $char ne 'all' ? ( char => $char ) : (),
    $q ? ( search => $q ) : (),
    results => 50,
    page => $f->{p},
    order => ($f->{s} eq 'rel' ? 'c_released' : 'title').($f->{o} eq 'a' ? ' ASC' : ' DESC'),
    @cati ? ( cati => \@cati ) : (),
    @cate ? ( cate => \@cate ) : (),
    @lang ? ( lang => \@lang ) : (),
    @plat ? ( platform => \@plat ) : (),
  );

  $self->resRedirect('/v'.$list->[0]{id}, 'temp')
    if $q && @$list == 1;

  $self->htmlHeader(title => 'Browse visual novels', search => $f->{q});
  _filters($self, $f, $char);
  $self->htmlBrowse(
    class    => 'vnbrowse',
    items    => $list,
    options  => $f,
    nextpage => $np,
    pageurl  => "/v/$char?o=$f->{o};s=$f->{s};q=$f->{q}",
    sorturl  => "/v/$char?q=$f->{q}",
    header   => [
      [ 'Title',    'title' ],
      [ '',         0       ],
      [ '',         0       ],
      [ 'Released', 'rel'   ],
    ],
    row     => sub {
      my($s, $n, $l) = @_;
      Tr $n % 2 ? (class => 'odd') : ();
       td class => 'tc1';
        a href => '/v'.$l->{id}, title => $l->{original}||$l->{title}, shorten $l->{title}, 100;
       end;
       td class => 'tc2';
        $_ ne 'oth' && cssicon $_, $self->{platforms}{$_}
          for (sort split /\//, $l->{c_platforms});
       end;
       td class => 'tc3';
        cssicon "lang $_", $self->{languages}{$_}
          for (reverse sort split /\//, $l->{c_languages});
       end;
       td class => 'tc4';
        lit monthstr $l->{c_released};
       end;
      end;
    },
  );
  $self->htmlFooter;
}


sub _filters {
  my($self, $f, $char) = @_;

  div class => 'mainbox';
   h1 'Browse visual novels';
   form class => 'search', action => '/v/all', 'accept-charset' => 'UTF-8', method => 'get';
    fieldset;
     input type => 'text', name => 'q', id => 'q', class => 'text', value => $f->{q};
     input type => 'submit', class => 'submit', value => 'Search!';
    end;
   end;
   p class => 'browseopts';
    for ('all', 'a'..'z', 0) {
      a href => "/v/$_", $_ eq $char ? (class => 'optselected') : (), $_ ? uc $_ : '#';
    }
   end;
   a id => 'advselect', href => '#';
    lit '<i>&#9656;</i> advanced search';
   end;
   div id => 'advoptions', class => 'hidden';

    h2;
     lit 'Categories <p>(boolean and, selecting more gives less results. The categories are explained on <a href="/d1">this page</a>)</p>';
    end;
    ul id => 'catselect';
     for my $c (qw| e g t p h l s |) {
       $c !~ /[thl]/ ? li : br;
        txt $self->{categories}{$c}[0];
        ul;
         li id => "cat_$c$_", $self->{categories}{$c}[1]{$_}
           for (sort keys %{$self->{categories}{$c}[1]});
        end;
       end if $c !~ /[gph]/;
     }

     h2;
      lit 'Languages <p>(boolean or, selecting more gives more results)</p>';
     end;
     for(sort @{$self->dbLanguages}) {
       span;
        input type => 'checkbox', id => "lang_$_";
        label for => "lang_$_";
         cssicon "lang $_", $self->{languages}{$_};
         txt $self->{languages}{$_};
        end;
       end;
     }

     h2;
      lit 'Platforms <p>(boolean or, selecting more gives more results)</p>';
     end;
     for(sort keys %{$self->{platforms}}) {
       next if $_ eq 'oth';
       span;
        input type => 'checkbox', id => "plat_$_";
        label for => "plat_$_";
         cssicon $_, $self->{platforms}{$_};
         txt $self->{platforms}{$_};
        end;
       end;
     }

     clearfloat;
    end;
   end;
  end;
}


1;