1 |
package Arh::View::Units; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use base 'Jifty::View::Declare::CRUD'; |
7 |
use Jifty::View::Declare -base; |
8 |
|
9 |
use Data::Dump qw/dump/; |
10 |
|
11 |
sub object_type { 'Unit' }; |
12 |
|
13 |
sub per_page { |
14 |
my $per_page = get('per_page') || 3; |
15 |
warn "## Units per_page = $per_page\n"; |
16 |
return $per_page; |
17 |
}; |
18 |
|
19 |
sub display_columns { |
20 |
my $self = shift; |
21 |
return ( qw/ |
22 |
unit_id |
23 |
name |
24 |
subcategory |
25 |
category |
26 |
site |
27 |
gps |
28 |
placement |
29 |
context |
30 |
discovery |
31 |
function |
32 |
reusage |
33 |
ownership |
34 |
presevation_place |
35 |
inventory_nr |
36 |
dimensions |
37 |
condition |
38 |
toplogy |
39 |
technique |
40 |
description |
41 |
construction |
42 |
iconography |
43 |
decoration |
44 |
motifs |
45 |
inscription |
46 |
language |
47 |
font |
48 |
author |
49 |
chronology |
50 |
chronostyle |
51 |
explored |
52 |
conservations |
53 |
restorations |
54 |
legal |
55 |
links |
56 |
sources |
57 |
bibliography |
58 |
copyright |
59 |
material |
60 |
/ ); |
61 |
} |
62 |
|
63 |
#private template search_region => sub {}; |
64 |
|
65 |
=for later |
66 |
|
67 |
template 'index.html' => page { |
68 |
my $self = shift; |
69 |
title is $self->object_type; |
70 |
form { |
71 |
render_region( |
72 |
name => $self->object_type.'-list', |
73 |
path => $self->fragment_base_path.'/list' |
74 |
); |
75 |
}; |
76 |
}; |
77 |
|
78 |
template 'list' => sub { |
79 |
my $self = shift; |
80 |
|
81 |
my ( $page ) = get('page'); |
82 |
|
83 |
my $item_path = get('item_path') || $self->fragment_for("view"); |
84 |
my $collection = $self->_current_collection(); |
85 |
|
86 |
if ( $self->per_page == 1 ) { |
87 |
warn "## bulk import get to last page\n"; |
88 |
$page = $collection->pager->last_page; |
89 |
} |
90 |
|
91 |
div { |
92 |
{class is 'crud-'.$self->object_type}; |
93 |
|
94 |
show('./search_region'); |
95 |
show( './paging_top', $collection, $page ); |
96 |
show( './paging_bottom', $collection, $page ); |
97 |
show( './list_items', $collection, $item_path ); |
98 |
show( './paging_bottom', $collection, $page ); |
99 |
|
100 |
show( './new_item_region'); |
101 |
} |
102 |
|
103 |
}; |
104 |
|
105 |
=cut |
106 |
|
107 |
=head2 single |
108 |
|
109 |
display singe unit on page, using C<id> |
110 |
|
111 |
=cut |
112 |
|
113 |
template 'single' => page { |
114 |
my $self = shift; |
115 |
|
116 |
my $id = get('id') || die "no id?"; |
117 |
|
118 |
my $item = Arh::Model::Unit->new; |
119 |
$item->load( $id ); |
120 |
die _("Can't find item %1", $id) unless $item->id; |
121 |
|
122 |
title is $item->name; |
123 |
|
124 |
render_region( |
125 |
name => 'item-' . $item->id, |
126 |
path => $self->fragment_for('view'), |
127 |
defaults => { id => $item->id, object_type => $self->object_type } |
128 |
); |
129 |
|
130 |
}; |
131 |
|
132 |
private template 'paging_top' => sub { |
133 |
my $self = shift; |
134 |
my $collection = shift; |
135 |
my $page = shift || 1; |
136 |
|
137 |
if ( $collection->pager->last_page > 1 ) { |
138 |
span { |
139 |
{ class is 'page-count' }; |
140 |
outs( |
141 |
_( "Found %1 units, showing %2-%3 on page %4/%5", |
142 |
$collection->pager->total_entries, |
143 |
$collection->pager->first, |
144 |
$collection->pager->last, |
145 |
$page, |
146 |
$collection->pager->last_page |
147 |
) |
148 |
); |
149 |
} |
150 |
} |
151 |
show( './paging_bottom', $collection, $page ); |
152 |
}; |
153 |
|
154 |
private template paging_bottom => sub { |
155 |
my $self = shift; |
156 |
my $collection = shift; |
157 |
my $page = shift || 0; |
158 |
div { |
159 |
{ class is 'paging' }; |
160 |
if ( $collection->pager->previous_page ) { |
161 |
span { |
162 |
hyperlink( |
163 |
label => _("Previous Page"), |
164 |
onclick => { |
165 |
args => { page => $collection->pager->previous_page } |
166 |
} |
167 |
); |
168 |
} |
169 |
} |
170 |
|
171 |
my ( $f_p, $t_p ) = ( '...', '...' ); |
172 |
|
173 |
my $from = $page - 15; |
174 |
if ( $from < 0 ) { |
175 |
$from = $collection->pager->first_page; |
176 |
$f_p = ''; |
177 |
} |
178 |
my $to = $page + 15; |
179 |
if ( $to > $collection->pager->last_page ) { |
180 |
$to = $collection->pager->last_page; |
181 |
$t_p = ''; |
182 |
} |
183 |
|
184 |
outs $f_p; |
185 |
|
186 |
foreach my $p ( $from .. $to ) { |
187 |
if ( $p == $page ) { |
188 |
b { $p } |
189 |
} else { |
190 |
span { |
191 |
hyperlink( |
192 |
label => $p, |
193 |
onclick => |
194 |
{ args => { page => $p } } |
195 |
); |
196 |
} |
197 |
} |
198 |
} |
199 |
|
200 |
outs $t_p; |
201 |
|
202 |
if ( $collection->pager->next_page ) { |
203 |
span { |
204 |
hyperlink( |
205 |
label => _("Next Page"), |
206 |
onclick => |
207 |
{ args => { page => $collection->pager->next_page } } |
208 |
); |
209 |
} |
210 |
} |
211 |
}; |
212 |
}; |
213 |
|
214 |
|
215 |
template 'view' => sub :CRUDView { |
216 |
my $self = shift; |
217 |
my $id = get('id'); |
218 |
my $record = $self->_get_record( $id ); |
219 |
|
220 |
my $update = $record->as_update_action( |
221 |
moniker => "update-" . Jifty->web->serial, |
222 |
); |
223 |
|
224 |
my $delete = $record->as_delete_action( |
225 |
moniker => "delete-" . Jifty->web->serial, |
226 |
); |
227 |
my $editing = $record->current_user_can('write'); |
228 |
warn "## current_user_can('write') = $editing\n"; |
229 |
$editing = 0 unless $self->current_user->editing; |
230 |
warn "## editing ",$editing ? 'ok' : 'DENIED'; |
231 |
|
232 |
div { { class is 'unit' } |
233 |
|
234 |
if ( $editing ) { |
235 |
outs_raw($delete->button( |
236 |
label => _('Delete unit'), |
237 |
class => 'float-crud-button button-delete', |
238 |
onclick => { |
239 |
submit => $delete, |
240 |
confirm => _('Really delete?'), |
241 |
# region => Jifty->web->current_region, |
242 |
replace_with => '/__jifty/empty', |
243 |
refresh_self => 1, |
244 |
args => { id => $id }, |
245 |
}, |
246 |
)); |
247 |
}; |
248 |
|
249 |
foreach my $f ( $self->display_columns ) { |
250 |
if ( $f eq 'material' ) { |
251 |
set( search_collection => $record->material ); |
252 |
render_region( |
253 |
name => 'unit-materials', |
254 |
path => '/materials/list', |
255 |
defaults => { |
256 |
page => 1, |
257 |
unit => $record->id, |
258 |
} |
259 |
); |
260 |
} else { |
261 |
my %opt; |
262 |
if ( ! $editing ) { |
263 |
$opt{render_mode} = 'read'; |
264 |
# skip fields without values |
265 |
$opt{render_as} = 'hidden' if ! defined( $update->record->$f ); |
266 |
} |
267 |
render_param( $update => $f, %opt ); |
268 |
} |
269 |
}; |
270 |
if ( $editing ) { |
271 |
div { |
272 |
{ class is 'submit_button' }; |
273 |
hyperlink( |
274 |
label => _('Update'), |
275 |
onclick => { |
276 |
submit => $update, |
277 |
refresh_self => 1, |
278 |
}, |
279 |
); |
280 |
} |
281 |
} |
282 |
|
283 |
}; # div class unit |
284 |
|
285 |
show('/unitpictures/fragment', $record); |
286 |
|
287 |
hr {}; |
288 |
}; |
289 |
|
290 |
template 'new_item' => sub { |
291 |
my $self = shift; |
292 |
return unless $self->current_user->editing; |
293 |
|
294 |
my ( $object_type, $id ) = ( $self->object_type, get('id') ); |
295 |
my $record_class = $self->record_class; |
296 |
my $create = $record_class->as_create_action; |
297 |
|
298 |
h1 { _("New unit") }; |
299 |
|
300 |
div { |
301 |
|
302 |
foreach my $f ( $self->display_columns ) { |
303 |
# last if $f eq 'material' && ! $id; |
304 |
render_param( $create => $f ) unless $f eq 'material'; |
305 |
#warn "## $f"; |
306 |
} |
307 |
|
308 |
div { |
309 |
{ class is 'submit_button' }; |
310 |
hyperlink( |
311 |
label => _("Add"), |
312 |
onclick => [ |
313 |
{ |
314 |
submit => $create, |
315 |
# args => { |
316 |
# }, |
317 |
}, |
318 |
{ refresh_self => 1 }, |
319 |
{ |
320 |
element => Jifty->web->current_region->parent->get_element( 'div.list' ), |
321 |
append => $self->fragment_for('view'), |
322 |
args => { |
323 |
object_type => $object_type, |
324 |
id => { result_of => $create, name => 'id' }, |
325 |
unit => get('unit'), |
326 |
}, |
327 |
}, |
328 |
], |
329 |
as_button => 1, |
330 |
); |
331 |
}; |
332 |
|
333 |
}; |
334 |
}; |
335 |
|
336 |
1; |