-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathBook.pm
79 lines (70 loc) · 2.76 KB
/
Book.pm
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
=head1 NAME
Tuba::Book : Controller class for books
=cut
package Tuba::Book;
use Mojo::Base qw/Tuba::Controller/;
use Tuba::DB::Objects qw/-nicknames/;
sub list {
my $c = shift;
$c->stash(extra_cols => [qw/title number_of_pages/]);
if ($c->param('in_library')) {
my $objects = Books->get_objects(query => [ in_library => 1 ],
($c->param('all')
? ()
: (page => $c->page, per_page => $c->per_page)),
sort_by => 'identifier'
);
my $count = Books->get_objects_count({ in_library => 1});
$c->stash(objects => $objects);
$c->set_pages($count);
}
$c->SUPER::list(@_);
}
sub show {
my $c = shift;
my $book = $c->_this_object or return $c->render_not_found_or_redirect;
$c->stash('object', $book);
$c->SUPER::show(@_);
}
sub update_rel_form {
my $c = shift;
$c->SUPER::update_rel_form(@_);
}
sub update {
my $c = shift;
$c->stash(tab => 'update_form');
my $book = $c->_this_object or return $c->reply->not_found;
if ($c->param('convert_into_report')) {
return $c->update_error("Not converting because this book has an ISBN, and reports don't have ISBNs.") if $book->isbn && length($book->isbn);
my $report = Report->new(
identifier => $book->identifier,
title => $book->title,
topic => $book->topic,
url => $book->url,
publication_year => $book->year
);
$report->save(audit_user => $c->audit_user, audit_note => $c->audit_note) or do {
return $c->update_error($report->error);
};
my $pub = $book->get_publication;
if ($pub) {
my $report_pub = $report->get_publication(autocreate => 1);
$report_pub->save(audit_user => $c->audit_user, audit_note => $c->audit_note);
my $refs = References->get_objects(query => [ child_publication_id => $pub->id ] );
for my $ref (@$refs) {
$ref->child_publication_id($report_pub->id);
my $attrs = $ref->attrs; # workaround : call inflate trigger explicitly
$ref->save(audit_user => $c->audit_user, audit_note => $c->audit_note) or return $c->update_error($ref->error);
}
my $maps = PublicationReferenceMaps->get_objects(query => [ publication_id => $report_pub->id ]);
for my $map (@$maps) {
$map->publication_id($report_pub->id);
$map->save(audit_user => $c->audit_user, audit_note => $c->audit_note) or return $c->update_error($map->error);
}
}
$book->delete(audit_user => $c->audit_user, audit_note => $c->audit_note);
return $c->redirect_to( $report->uri($c, { tab => 'show' } ) );
}
$c->SUPER::update(@_);
}
1;