summaryrefslogtreecommitdiff
path: root/lib/VNDB/Handler/ULists.pm
blob: f89e5e56297ea90741189c16cf6f7e531fe49536 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455

package VNDB::Handler::ULists;

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


YAWF::register(
  qr{v([1-9]\d*)/vote},  \&vnvote,
  qr{v([1-9]\d*)/wish},  \&vnwish,
  qr{v([1-9]\d*)/list},  \&vnlist_e,
  qr{r([1-9]\d*)/list},  \&rlist_e,
  qr{xml/rlist.xml},     \&rlist_e,
  qr{([uv])([1-9]\d*)/votes}, \&votelist,
  qr{u([1-9]\d*)/wish},  \&wishlist,
  qr{u([1-9]\d*)/list},  \&vnlist,
);


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

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

  return if !$self->authCheckCode;
  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;

  return if !$self->authCheckCode;
  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 vnlist_e {
  my($self, $id) = @_;

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

  return if !$self->authCheckCode;
  my $f = $self->formValidate(
    { name => 'e', enum => [ -1, @{$self->{rlst_vstat}} ] }
  );
  return 404 if $f->{_err};

  $self->dbVNListDel($uid, $id) if $f->{e} == -1;
  $self->dbVNListAdd($uid, $id, $f->{e}) if $f->{e} != -1;

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


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

  my $rid = $id;
  if(!$rid) {
    my $f = $self->formValidate(
      { name => 'id', required => 1, template => 'int' }
    );
    return 404 if $f->{_err};
    $rid = $f->{id};
  }

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

  return if !$self->authCheckCode;
  my $f = $self->formValidate(
    { name => 'e', required => 1, enum => [ -1, @{$self->{rlst_rstat}} ] }
  );
  return 404 if $f->{_err};

  $self->dbRListDel($uid, $rid) if $f->{e} == -1;
  $self->dbRListAdd($uid, $rid, $f->{e}) if $f->{e} >= 0;

  if($id) {
    (my $ref = $self->reqHeader('Referer')||"/r$id") =~ s/^\Q$self->{url}//;
    $self->resRedirect($ref, 'temp');
  } else {
    # doesn't really matter what we return, as long as it's XML
    $self->resHeader('Content-type' => 'text/xml');
    xml;
    tag 'done', '';
  }
}


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

  my $obj = $type eq 'v' ? $self->dbVNGet(id => $id)->[0] : $self->dbUserGet(uid => $id)->[0];
  return 404 if !$obj->{id};

  my $f = $self->formValidate(
    { name => 'p',  required => 0, default => 1, template => 'int' },
    { name => 'o',  required => 0, default => 'd', enum => ['a', 'd'] },
    { name => 's',  required => 0, default => 'date', enum => [qw|date title vote|] },
  );

  my($list, $np) = $self->dbVoteGet(
    $type.'id' => $id,
    what     => $type eq 'v' ? 'user' : 'vn',
    hide     => $type eq 'v',
    hide_ign => $type eq 'v',
    sort     => $f->{s} eq 'title' && $type eq 'v' ? 'username' : $f->{s},
    reverse  => $f->{o} eq 'd',
    results  => 50,
    page     => $f->{p}
  );
  return 404 if !@$list;

  my $title = mt $type eq 'v' ? '_votelist_title_vn' : '_votelist_title_user', $obj->{title} || $obj->{username};
  $self->htmlHeader(noindex => 1, title => $title);
  $self->htmlMainTabs($type => $obj, 'votes');
  div class => 'mainbox';
   h1 $title;
  end;

  $self->htmlBrowse(
    class    => 'votelist',
    items    => $list,
    options  => $f,
    nextpage => $np,
    pageurl  => "/$type$id/votes?o=$f->{o};s=$f->{s}",
    sorturl  => "/$type$id/votes",
    header   => [
      [ mt('_votelist_col_date'),  'date'  ],
      [ mt('_votelist_col_vote'),  'vote'  ],
      [ mt('_votelist_col_'.($type eq 'v'?'user':'vn')), 'title' ],
    ],
    row      => sub {
      my($s, $n, $l) = @_;
      Tr $n % 2 ? (class => 'odd') : ();
       td class => 'tc1', $self->{l10n}->date($l->{date});
       td class => 'tc2', $l->{vote};
       td class => 'tc3';
        a href => $type eq 'v' ? ("/u$l->{uid}", $l->{username}) : ("/v$l->{vid}", shorten $l->{title}, 100);
       end;
      end;
    },
  );

  $self->htmlFooter;
}


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} || $self->authCan('usermod'));

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

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

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

  my $title = $own ? mt('_wishlist_title_my') : mt('_wishlist_title_other', $u->{username});
  $self->htmlHeader(title => $title, noindex => 1);
  $self->htmlMainTabs('u', $u, 'wish');
  div class => 'mainbox';
   h1 $title;
   if(!@$list && $f->{f} == -1) {
      p mt '_wishlist_noresults';
     end;
     return $self->htmlFooter;
   }
   p class => 'browseopts';
    a $f->{f} == $_ ? (class => 'optselected') : (), href => "/u$uid/wish?f=$_",
        $_ == -1 ? mt '_wishlist_prio_all' : mt "_wish_$_"
      for (-1, @{$self->{wishlist_status}});
   end;
  end;

  if($own) {
    my $code = $self->authGetCode("/u$uid/wish");
    form action => "/u$uid/wish?formcode=$code;f=$f->{f};o=$f->{o};s=$f->{s};p=$f->{p}", method => 'post';
  }

  $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   => [
      [ mt('_wishlist_col_title') => 'title' ],
      [ mt('_wishlist_col_prio')  => 'wstat' ],
      [ mt('_wishlist_col_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', mt "_wish_$i->{wstat}";
       td class => 'tc3', $self->{l10n}->date($i->{added}, 'compact');
      end;
    },
    $own ? (footer => sub {
      Tr;
       td colspan => 3;
        Select name => 'batchedit', id => 'batchedit';
         option mt '_wishlist_select';
         optgroup label => mt '_wishlist_changeprio';
          option value => $_, mt "_wish_$_"
            for (@{$self->{wishlist_status}});
         end;
         option value => -1, mt '_wishlist_remove';
        end;
       end;
      end;
    }) : (),
  );
  end if $own;
  $self->htmlFooter;
}


sub vnlist {
  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} || $self->authCan('usermod'));

  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 => [ 'title', 'vote' ] },
    { name => 'c',  required => 0, default => 'all', enum => [ 'all', 'a'..'z', 0 ] },
    { name => 'v',  required => 0, default => 0, enum => [ -1..1  ] },
  );
  return 404 if $f->{_err};

  if($own && $self->reqMethod eq 'POST') {
    return if !$self->authCheckCode;
    my $frm = $self->formValidate(
      { name => 'vid', required => 0, default => 0, multi => 1, template => 'int' },
      { name => 'rid', required => 0, default => 0, multi => 1, template => 'int' },
      { name => 'vns', required => 1, enum => [ -2, -1, @{$self->{rlst_vstat}} ] },
      { name => 'rel', required => 1, enum => [ -2, -1, @{$self->{rlst_rstat}} ] },
    );
    my @vid = grep $_ > 0, @{$frm->{vid}};
    my @rid = grep $_ > 0, @{$frm->{rid}};
    if(!$frm->{_err} && @vid && $frm->{vns} > -2) {
      $self->dbVNListDel($uid, \@vid) if $frm->{vns} == -1;
      $self->dbVNListAdd($uid, \@vid, $frm->{vns}) if $frm->{vns} >= 0;
    }
    if(!$frm->{_err} && @rid && $frm->{rel} > -2) {
      $self->dbRListDel($uid, \@rid) if $frm->{rel} == -1;
      $self->dbRListAdd($uid, \@rid, $frm->{rel}) if $frm->{rel} >= 0;
    }
  }

  my($list, $np) = $self->dbVNListList(
    uid => $uid,
    results => 50,
    page => $f->{p},
    sort => $f->{s}, reverse => $f->{o} eq 'd',
    voted => $f->{v} == 0 ? undef : $f->{v} < 0 ? 0 : $f->{v},
    $f->{c} ne 'all' ? (char => $f->{c}) : (),
  );

  my $title = $own ? mt '_rlist_title_my' : mt '_rlist_title_other', $u->{username};
  $self->htmlHeader(title => $title, noindex => 1);
  $self->htmlMainTabs('u', $u, 'list');

  # url generator
  my $url = sub {
    my($n, $v) = @_;
    $n ||= '';
    local $_ = "/u$uid/list";
    $_ .= '?c='.($n eq 'c' ? $v : $f->{c});
    $_ .= ';v='.($n eq 'v' ? $v : $f->{v});
    if($n eq 'page') {
      $_ .= ';o='.($n eq 'o' ? $v : $f->{o});
      $_ .= ';s='.($n eq 's' ? $v : $f->{s});
    }
    return $_;
  };

  div class => 'mainbox';
   h1 $title;
   p class => 'browseopts';
    for ('all', 'a'..'z', 0) {
      a href => $url->(c => $_), $_ eq $f->{c} ? (class => 'optselected') : (), $_ eq 'all' ? mt('_char_all') : $_ ? uc $_ : '#';
    }
   end;
   p class => 'browseopts';
    a href => $url->(v =>  0),  0 == $f->{v} ? (class => 'optselected') : (), mt '_rlist_voted_all';
    a href => $url->(v =>  1),  1 == $f->{v} ? (class => 'optselected') : (), mt '_rlist_voted_only';
    a href => $url->(v => -1), -1 == $f->{v} ? (class => 'optselected') : (), mt '_rlist_voted_none';
   end;
  end;

  _vnlist_browse($self, $own, $list, $np, $f, $url, $uid);
  $self->htmlFooter;
}

sub _vnlist_browse {
  my($self, $own, $list, $np, $f, $url, $uid) = @_;

  form action => $url->().';formcode='.$self->authGetCode("/u$uid/list"), method => 'post'
    if $own;

  $self->htmlBrowse(
    class    => 'rlist',
    items    => $list,
    nextpage => $np,
    options  => $f,
    sorturl  => $url->(),
    pageurl  => $url->('page'),
    header   => [
      [ '' ],
      sub { td class => 'tc2', id => 'expandall'; lit '&#9656;'; end; },
      [ mt('_rlist_col_title') => 'title' ],
      [ '' ], [ '' ],
      [ mt('_rlist_col_status') ],
      [ mt('_rlist_col_releases').'*' ],
      [ mt('_rlist_col_vote')  => 'vote'  ],
    ],
    row      => sub {
      my($s, $n, $i) = @_;
      Tr $n % 2 == 0 ? (class => 'odd') : ();
       td class => 'tc1'; input type => 'checkbox', name => 'vid', value => $i->{vid} if $own; end;
       if(@{$i->{rels}}) {
         td class => 'tc2 collapse_but', id => "vid$i->{vid}"; lit '&#9656;'; end;
       } else {
         td class => 'tc2', '';
       }
       td class => 'tc3_5', colspan => 3;
        a href => "/v$i->{vid}", title => $i->{original}||$i->{title}, shorten $i->{title}, 70;
       end;
       td class => 'tc6', $i->{status} ? mt '_vnlst_stat_'.$i->{status} : '';
       td class => 'tc7';
        my $obtained = grep $_->{status}==2, @{$i->{rels}};
        my $total = scalar @{$i->{rels}};
        my $txt = sprintf '%d/%d', $obtained, $total;
        $txt = qq|<b class="done">$txt</b>| if $total && $obtained == $total;
        $txt = qq|<b class="todo">$txt</b>| if $obtained < $total;
        lit $txt;
       end;
       td class => 'tc8', $i->{vote} || '-';
      end;

      for (@{$i->{rels}}) {
        Tr class => "collapse relhid collapse_vid$i->{vid}".($n%2 ? '':' odd');
         td class => 'tc1', '';
         td class => 'tc2';
          input type => 'checkbox', name => 'rid', value => $_->{rid} if $own;
         end;
         td class => 'tc3', $self->{l10n}->datestr($_->{released});
         td class => 'tc4';
          cssicon "lang $_", mt "_lang_$_" for @{$_->{languages}};
          cssicon "rt$_->{type}", mt "_rtype_$_->{type}";
         end;
         td class => 'tc5';
          a href => "/r$_->{rid}", title => $_->{original}||$_->{title}, shorten $_->{title}, 50;
         end;
         td class => 'tc6', $_->{status} ? mt '_rlst_stat_'.$_->{status} : '';
         td class => 'tc7_8', colspan => 2, '';
        end;
      }
    },

    $own ? (footer => sub {
      Tr;
       td class => 'tc1'; input type => 'checkbox', name => 'vid', value => -1, class => 'checkall'; end;
       td class => 'tc2'; input type => 'checkbox', name => 'rid', value => -1, class => 'checkall'; end;
       td class => 'tc3_6', colspan => 4;
        Select id => 'vns', name => 'vns';
         option value => -2, mt '_rlist_withvn';
         optgroup label => mt '_rlist_changestat';
          option value => $_, mt "_vnlst_stat_$_"
            for (@{$self->{rlst_vstat}});
         end;
         option value => -1, mt '_rlist_del';
        end;
        Select id => 'rel', name => 'rel';
         option value => -2, mt '_rlist_withrel';
         optgroup label => mt '_rlist_changestat';
          option value => $_, mt "_rlst_stat_$_"
            for (@{$self->{rlst_rstat}});
         end;
         option value => -1, mt '_rlist_del';
        end;
        input type => 'submit', value => mt '_rlist_update';
       end;
       td class => 'tc7_8', colspan => 2, mt '_rlist_releasenote';
      end;
    }) : (),
  );

  end if $own;
}

1;