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

package VNDB::Handler::ULists;

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


YAWF::register(
  qr{v([1-9]\d*)/vote},  \&vnvote,
  qr{v([1-9]\d*)/wish},  \&vnwish,
  qr{u([1-9]\d*)/wish},  \&wishlist,
);


sub vnvote {
  my($self, $id) = @_;

  my $uid = $self->authInfo->{id};
  return $self->htmlDenied() if !$uid;

  my $f = $self->formValidate(
    { name => 'v', enum => [ -1, 1..10 ] }
  );
  return 404 if $f->{_err};

  $self->dbVoteDel($uid, $id) if $f->{v} == -1;
  $self->dbVoteAdd($id, $uid, $f->{v}) if $f->{v} > 0;

  $self->resRedirect('/v'.$id, 'temp');
}


sub vnwish {
  my($self, $id) = @_;

  my $uid = $self->authInfo->{id};
  return $self->htmlDenied() if !$uid;

  my $f = $self->formValidate(
    { name => 's', enum => [ -1..$#{$self->{wishlist_status}} ] }
  );
  return 404 if $f->{_err};

  $self->dbWishListDel($uid, $id) if $f->{s} == -1;
  $self->dbWishListAdd($id, $uid, $f->{s}) if $f->{s} != -1;

  $self->resRedirect('/v'.$id, 'temp');
}


sub wishlist {
  my($self, $uid) = @_;

  my $own = $self->authInfo->{id} && $self->authInfo->{id} == $uid;
  my $u = $self->dbUserGet(uid => $uid)->[0];
  return 404 if !$u || !$own && !$u->{show_list};

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

  if($own && $self->reqMethod eq 'POST') {
    my $frm = $self->formValidate(
      { name => 'sel', required => 0, default => 0, multi => 1, template => 'int' },
      { name => 'wishbatch', required => 1, enum => [ -1..$#{$self->{wishlist_status}} ] },
    );
    if(!$frm->{_err} && @{$frm->{sel}} && $frm->{sel}[0]) {
      $self->dbWishListDel($uid, $frm->{sel}) if $frm->{wishbatch} == -1;
      $self->dbWishListAdd($frm->{sel}, $uid, $frm->{wishbatch}) if $frm->{wishbatch} >= 0;
    }
  }

  my($list, $np) = $self->dbWishListGet(
    uid => $uid,
    order => $f->{s}.' '.($f->{o} eq 'a' ? 'ASC' : 'DESC'),
    $f->{f} != -1 ? (wstat => $f->{f}) : (),
    what => 'vn',
    results => 50,
    page => $f->{p},
  );

  my $title = $own ? 'My wishlist' : "\u$u->{username}'s wishlist";
  $self->htmlHeader(title => $title, noindex => 1);
  $self->htmlMainTabs('u', $u, 'wish');
  div class => 'mainbox';
   h1 $title;
   if(!@$list && $f->{f} == -1) {
      p 'Wishlist empty...';
     end;
     return $self->htmlFooter;
   }
   p class => 'browseopts';
    a $f->{f} == $_ ? (class => 'optselected') : (), href => "/u$uid/wish?f=$_",
        $_ == -1 ? 'All priorities' : ucfirst $self->{wishlist_status}[$_]
      for (-1..$#{$self->{wishlist_status}});
   end;
  end;

  form action => "/u$uid/wish?f=$f->{f};o=$f->{o};s=$f->{s};p=$f->{p}", method => 'post'
    if $own;

  $self->htmlBrowse(
    class    => 'wishlist',
    items    => $list,
    nextpage => $np,
    options  => $f,
    pageurl  => "/u$uid/wish?f=$f->{f};o=$f->{o};s=$f->{s}",
    sorturl  => "/u$uid/wish?f=$f->{f}",
    header   => [
      [ Title    => 'title' ],
      [ Priority => ''      ],
      [ Added    => 'added' ],
    ],
    row      => sub {
      my($s, $n, $i) = @_;
      Tr $n % 2 == 0 ? (class => 'odd') : ();
       td class => 'tc1';
        input type => 'checkbox', name => 'sel', value => $i->{vid}
          if $own;
        a href => "/v$i->{vid}", title => $i->{original}||$i->{title}, ' '.shorten $i->{title}, 70;
       end;
       td class => 'tc2', ucfirst $self->{wishlist_status}[$i->{wstat}];
       td class => 'tc3', date $i->{added}, 'compact';
      end;
    },
    footer   => sub {
      return if !$own;
      Tr;
       td colspan => 3;
        Select name => 'wishbatch', id => 'wishbatch';
         option '-- with selected --';
         optgroup label => 'Change priority';
          option value => $_, $self->{wishlist_status}[$_]
            for (0..$#{$self->{wishlist_status}});
         end;
         option value => -1, 'remove from wishlist';
        end;
       end;
      end;
    },
  );
  end if $own;
  $self->htmlFooter;
}


1;