summaryrefslogtreecommitdiff
path: root/lib/VNDB/Handler/VNBrowse.pm
blob: 53c07db2d467b5266796df5a3c9617611c3f4444 (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

package VNDB::Handler::VNBrowse;

use strict;
use warnings;
use TUWF ':html', 'uri_escape';
use VNDB::Func;


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


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

  my $f = $self->formValidate(
    { get => 's', required => 0, default => 'tagscore', enum => [ qw|title rel pop tagscore rating| ] },
    { get => 'o', required => 0, enum => [ 'a','d' ] },
    { get => 'p', required => 0, default => 1, template => 'int' },
    { get => 'q', required => 0, default => '' },
    { get => 'sq', required => 0, default => '' },
    { get => 'fil',required => 0, default => '' },
  );
  return $self->resNotFound if $f->{_err};
  $f->{q} ||= $f->{sq};
  $f->{fil} = $self->authPref('filter_vn') if !grep $_ eq 'fil', $self->reqParam();
  my %compat = _fil_compat($self);

  return $self->resRedirect('/'.$1.$2.(!$3 ? '' : $1 eq 'd' ? '#'.$3 : '.'.$3), 'temp')
    if $f->{q} && $f->{q} =~ /^([gvrptud])([0-9]+)(?:\.([0-9]+))?$/;

  $f->{s} = 'title' if $f->{fil} !~ /tag_inc-/ && $f->{s} eq 'tagscore';
  $f->{o} = $f->{s} eq 'tagscore' ? 'd' : 'a' if !$f->{o};

  my($list, $np) = $self->filFetchDB(vn => $f->{fil}, \%compat, {
    what => 'rating',
    $char ne 'all' ? ( char => $char ) : (),
    $f->{q} ? ( search => $f->{q} ) : (),
    results => 50,
    page => $f->{p},
    sort => $f->{s}, reverse => $f->{o} eq 'd',
  });

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

  $self->htmlHeader(title => mt('_vnbrowse_title'), search => $f->{q});

  my $quri = uri_escape($f->{q});
  form action => '/v/all', 'accept-charset' => 'UTF-8', method => 'get';
  div class => 'mainbox';
   h1 mt '_vnbrowse_title';
   $self->htmlSearchBox('v', $f->{q});
   p class => 'browseopts';
    for ('all', 'a'..'z', 0) {
      a href => "/v/$_?q=$quri;fil=$f->{fil}", $_ eq $char ? (class => 'optselected') : (), $_ eq 'all' ? mt('_char_all') : $_ ? uc $_ : '#';
    }
   end;

   a id => 'filselect', href => '#v';
    lit '<i>&#9656;</i> '.mt('_rbrowse_filters').'<i></i>'; # TODO: it's not *r*browse
   end;
   input type => 'hidden', class => 'hidden', name => 'fil', id => 'fil', value => $f->{fil};
  end;
  end; # /form

  $self->htmlBrowseVN($list, $f, $np, "/v/$char?q=$quri;fil=$f->{fil}", $f->{fil} =~ /tag_inc-/);
  $self->htmlFooter(prefs => ['filter_vn']);
}


sub _fil_compat {
  my $self = shift;
  my %c;
  my $f = $self->formValidate(
    { get => 'ln', required => 0, multi => 1, enum => $self->{languages}, default => '' },
    { get => 'pl', required => 0, multi => 1, enum => $self->{platforms}, default => '' },
    { get => 'sp', required => 0, default => $self->reqCookie('tagspoil') =~ /^([0-2])$/ ? $1 : 0, enum => [0..2] },
  );
  return () if $f->{_err};
  $c{lang}     //= $f->{ln} if $f->{ln}[0];
  $c{plat}     //= $f->{pl} if $f->{pl}[0];
  $c{tagspoil} //= $f->{sp};
  return %c;
}


1;