-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathFigure.pm
233 lines (198 loc) · 7.82 KB
/
Figure.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
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
=head1 NAME
Tuba::Figure : Controller class for figures.
=cut
package Tuba::Figure;
use Mojo::Base qw/Tuba::Controller/;
use Tuba::DB::Objects qw/-nicknames/;
use Tuba::Log qw/logger/;
sub list {
my $c = shift;
$c->param(thumbs => 1) unless defined($c->param('thumbs')) && length($c->param('thumbs'));
my $figures;
my $all = $c->param('all');
my @page = $all ? () : (page => $c->page, per_page => $c->per_page);
if (my $ch = $c->stash('chapter')) {
my $report_identifier = $c->stash('report_identifier');
$figures = Figures->get_objects(
query => [chapter_identifier => $ch->identifier, report_identifier => $report_identifier], with_objects => ['chapter'],
@page,
sort_by => "number, ordinal, t1.identifier",
);
$c->set_pages(Figures->get_objects_count(
query => [chapter_identifier => $ch->identifier, report_identifier => $report_identifier], with_objects => ['chapter'],
)) unless $all;
} elsif (my $report_identifier = $c->stash('report_identifier')) {
$figures = Figures->get_objects(
with_objects => ['chapter'], sort_by => "number, ordinal, t1.identifier",
query => [ report_identifier => $report_identifier ],
@page,
);
$c->set_pages(Figures->get_objects_count(
query => [ report_identifier => $report_identifier ])
) unless $all;
} else {
$figures = Figures->get_objects(
with_objects => ['report', 'chapter'], sort_by => "number, ordinal, t1.identifier",
@page,
);
$c->set_pages(Figures->get_objects_count()) unless $all;
}
$c->stash(objects => $figures);
$c->SUPER::list(@_);
}
# To avoid breaking the current API, Figure has it's own make_tree_for_list
# which strips out its private keys (_keys), letting all the other objects continue
# to include them :\
sub make_tree_for_list {
my $c = shift;
my $obj = shift;
my %t;
for my $method (@{ $obj->meta->column_accessor_method_names }) {
my $val = $obj->$method;
$t{$method} =
ref($val) && ref($val) eq 'DateTime::Duration' ?
human_duration($val) : $val;
}
my $uri = $obj->uri($c);
my $href = $uri->clone->to_abs;
$href .= '.'.$c->stash('format') if $c->stash('format');
$t{uri} = $uri;
$t{href} = $href;
for my $k (keys %t) {
delete $t{$k} if $k =~ /^_/;
}
return \%t;
}
sub set_title {
my $c = shift;
if (my $ch = $c->stash('chapter')) {
$c->stash(title => sprintf("Figures in chapter %s of %s",$ch->stringify(tiny => 1), $ch->report->title));
} elsif (my $report = $c->stash('report')) {
$c->stash(title => sprintf("Figures in %s",$c->stash('report')->title));
} else {
$c->stash(title => "All figures");
}
}
sub show_origination {
my $c = shift;
my $identifier = $c->stash('figure_identifier');
my $object = Figure->new(
identifier => $identifier,
report_identifier => $c->stash('report_identifier')
)->load(speculative => 1);
if (!$object && $identifier =~ /^[0-9]+[0-9a-zA-Z._-]*$/ && $c->stash('chapter') ) {
my $chapter = $c->stash('chapter');
$object = Figure->new(
report_identifier => $c->stash('report_identifier'),
chapter_identifier => $chapter->identifier,
ordinal => $identifier,
)->load(speculative => 1);
};
return $c->render_not_found_or_redirect unless $object;
my $origination = $object->get_origination();
$c->respond_to(
json => sub { my $c = shift;
$c->render(text => $origination, format => 'json' ); },
);
}
sub update_origination {
my $c = shift;
my $identifier = $c->stash('figure_identifier');
my $figure = $c->_this_object or return $c->reply->not_found;
my $json = $c->req->json;
if ( ! $json ) {
return $c->render(json => { success => 0, error => "invalid JSON" }, status => 422 );
}
my $json_string = $c->req->text;
$figure->set_origination($json_string);
$figure->save(audit_user => $c->audit_user, audit_note => $c->audit_note) or return $c->update_error($figure->error);
$c->render(json => { success => 1 } );
}
sub remove_origination {
my $c = shift;
my $identifier = $c->stash('figure_identifier');
my $figure = $c->_this_object or return $c->reply->not_found;
$figure->set_origination("{}");
$figure->save(audit_user => $c->audit_user, audit_note => $c->audit_note) or return $c->update_error($figure->error);
$c->render(json => { success => 1 } );
}
sub show {
my $c = shift;
my $identifier = $c->stash('figure_identifier');
my $object = Figure->new(
identifier => $identifier,
report_identifier => $c->stash('report_identifier')
)->load(speculative => 1, with => [qw/chapter images/]);
if (!$object && $identifier =~ /^[0-9]+[0-9a-zA-Z._-]*$/ && $c->stash('chapter') ) {
my $chapter = $c->stash('chapter');
$object = Figure->new(
report_identifier => $c->stash('report_identifier'),
chapter_identifier => $chapter->identifier,
ordinal => $identifier,
)->load(speculative => 1, with => [qw/chapter images/]);
return $c->redirect_to($object->uri_with_format($c)) if $object;
};
return $c->render_not_found_or_redirect unless $object;
if (!$c->stash('chapter_identifier') && $object->chapter_identifier) {
$c->stash(chapter_identifier => $object->chapter_identifier);
}
return $c->reply->not_found unless $c->verify_consistent_chapter($object);
$c->stash(object => $object);
$c->stash(meta => Figure->meta);
$c->SUPER::show(@_);
}
sub update_form {
my $c = shift;
my $object = $c->_this_object or return $c->reply->not_found;
$c->verify_consistent_chapter($object) or return $c->reply->not_found;
$c->SUPER::update_form(@_);
}
sub update_rel_form {
my $c = shift;
$c->stash(relationships => [ map Figure->meta->relationship($_), qw/images/ ]);
$c->stash(controls => {
images => sub {
my ($c,$obj) = @_;
+{
template => 'image',
params => { }
}
}
});
$c->SUPER::update_rel_form(@_);
}
sub update_rel {
my $c = shift;
my $object = $c->_this_object or return $c->reply->not_found;
$c->stash(tab => "update_rel_form");
my $json = $c->req->json;
$object->meta->error_mode('return');
if (my $new = $c->param('new_image')) {
my $img = $c->Tuba::Search::autocomplete_str_to_object($new);
$object->add_images($img);
$object->save(audit_user => $c->audit_user, audit_note => $c->audit_note) or return $c->update_error($object->error);
}
if (my $new = $json->{add_image_identifier}) {
my $img = Image->new(identifier => $new)->load(speculative => 1)
or return $c->update_error("Image $new not found");
$object->add_images($img);
$object->save(audit_user => $c->audit_user, audit_note => $c->audit_note) or return $c->update_error($object->error);
}
my $report_identifier = $c->stash('report_identifier');
my @delete_images;
@delete_images = $c->param('delete_image') if $c->param('delete_image');
if (my $nother = $json->{delete_image_identifier}) {
push @delete_images, $nother;
}
if (0 < scalar @delete_images) {
for my $id (@delete_images) {
ImageFigureMaps->delete_objects(
where => [{figure_identifier => $object->identifier,
report_identifier => $report_identifier}],
audit_user => $c->audit_user, audit_note => $c->audit_note);
$c->flash(message => 'Saved changes');
}
}
return $c->SUPER::update_rel(@_);
}
1;