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 |
private template 'paging_top' => sub { |
108 |
my $self = shift; |
109 |
my $collection = shift; |
110 |
my $page = shift || 1; |
111 |
|
112 |
if ( $collection->pager->last_page > 1 ) { |
113 |
span { |
114 |
{ class is 'page-count' }; |
115 |
outs( |
116 |
_( "Found %1 units, showing %2-%3 on page %4/%5", |
117 |
$collection->pager->total_entries, |
118 |
$collection->pager->first, |
119 |
$collection->pager->last, |
120 |
$page, |
121 |
$collection->pager->last_page |
122 |
) |
123 |
); |
124 |
} |
125 |
} |
126 |
show( './paging_bottom', $collection, $page ); |
127 |
}; |
128 |
|
129 |
private template paging_bottom => sub { |
130 |
my $self = shift; |
131 |
my $collection = shift; |
132 |
my $page = shift || 0; |
133 |
div { |
134 |
{ class is 'paging' }; |
135 |
if ( $collection->pager->previous_page ) { |
136 |
span { |
137 |
hyperlink( |
138 |
label => _("Previous Page"), |
139 |
onclick => { |
140 |
args => { page => $collection->pager->previous_page } |
141 |
} |
142 |
); |
143 |
} |
144 |
} |
145 |
|
146 |
my ( $f_p, $t_p ) = ( '...', '...' ); |
147 |
|
148 |
my $from = $page - 15; |
149 |
if ( $from < 0 ) { |
150 |
$from = $collection->pager->first_page; |
151 |
$f_p = ''; |
152 |
} |
153 |
my $to = $page + 15; |
154 |
if ( $to > $collection->pager->last_page ) { |
155 |
$to = $collection->pager->last_page; |
156 |
$t_p = ''; |
157 |
} |
158 |
|
159 |
outs $f_p; |
160 |
|
161 |
foreach my $p ( $from .. $to ) { |
162 |
if ( $p == $page ) { |
163 |
b { $p } |
164 |
} else { |
165 |
span { |
166 |
hyperlink( |
167 |
label => $p, |
168 |
onclick => |
169 |
{ args => { page => $p } } |
170 |
); |
171 |
} |
172 |
} |
173 |
} |
174 |
|
175 |
outs $t_p; |
176 |
|
177 |
if ( $collection->pager->next_page ) { |
178 |
span { |
179 |
hyperlink( |
180 |
label => _("Next Page"), |
181 |
onclick => |
182 |
{ args => { page => $collection->pager->next_page } } |
183 |
); |
184 |
} |
185 |
} |
186 |
}; |
187 |
}; |
188 |
|
189 |
|
190 |
template 'view' => sub :CRUDView { |
191 |
my $self = shift; |
192 |
my $id = get('id'); |
193 |
my $record = $self->_get_record( $id ); |
194 |
|
195 |
my $update = $record->as_update_action( |
196 |
moniker => "update-" . Jifty->web->serial, |
197 |
); |
198 |
|
199 |
my $delete = $record->as_delete_action( |
200 |
moniker => "delete-" . Jifty->web->serial, |
201 |
); |
202 |
my $editing = $record->current_user_can('write'); |
203 |
warn "## current_user_can('write') = $editing\n"; |
204 |
$editing = 0 unless $self->current_user->editing; |
205 |
warn "## editing ",$editing ? 'ok' : 'DENIED'; |
206 |
|
207 |
div { { class is 'unit' } |
208 |
|
209 |
if ( $editing ) { |
210 |
outs_raw($delete->button( |
211 |
label => _('Delete unit'), |
212 |
class => 'float-crud-button button-delete', |
213 |
onclick => { |
214 |
submit => $delete, |
215 |
confirm => _('Really delete?'), |
216 |
# region => Jifty->web->current_region, |
217 |
replace_with => '/__jifty/empty', |
218 |
refresh_self => 1, |
219 |
args => { id => $id }, |
220 |
}, |
221 |
)); |
222 |
}; |
223 |
|
224 |
foreach my $f ( $self->display_columns ) { |
225 |
if ( $f eq 'material' ) { |
226 |
set( search_collection => $record->material ); |
227 |
render_region( |
228 |
name => 'unit-materials', |
229 |
path => '/materials/list', |
230 |
defaults => { |
231 |
page => 1, |
232 |
unit => $record->id, |
233 |
} |
234 |
); |
235 |
} else { |
236 |
my %opt; |
237 |
if ( ! $editing ) { |
238 |
$opt{render_mode} = 'read'; |
239 |
# skip fields without values |
240 |
$opt{render_as} = 'hidden' if ! defined( $update->record->$f ); |
241 |
} |
242 |
render_param( $update => $f, %opt ); |
243 |
} |
244 |
}; |
245 |
if ( $editing ) { |
246 |
div { |
247 |
{ class is 'submit_button' }; |
248 |
hyperlink( |
249 |
label => _('Update'), |
250 |
onclick => { |
251 |
submit => $update, |
252 |
refresh_self => 1, |
253 |
}, |
254 |
); |
255 |
} |
256 |
} |
257 |
|
258 |
}; # div class unit |
259 |
|
260 |
show('/unitpictures/fragment', $record); |
261 |
|
262 |
hr {}; |
263 |
}; |
264 |
|
265 |
template 'new_item' => sub { |
266 |
my $self = shift; |
267 |
return unless $self->current_user->editing; |
268 |
|
269 |
my ( $object_type, $id ) = ( $self->object_type, get('id') ); |
270 |
my $record_class = $self->record_class; |
271 |
my $create = $record_class->as_create_action; |
272 |
|
273 |
h1 { _("New unit") }; |
274 |
|
275 |
div { |
276 |
|
277 |
foreach my $f ( $self->display_columns ) { |
278 |
# last if $f eq 'material' && ! $id; |
279 |
render_param( $create => $f ) unless $f eq 'material'; |
280 |
#warn "## $f"; |
281 |
} |
282 |
|
283 |
div { |
284 |
{ class is 'submit_button' }; |
285 |
hyperlink( |
286 |
label => _("Add"), |
287 |
onclick => [ |
288 |
{ |
289 |
submit => $create, |
290 |
# args => { |
291 |
# }, |
292 |
}, |
293 |
{ refresh_self => 1 }, |
294 |
{ |
295 |
element => Jifty->web->current_region->parent->get_element( 'div.list' ), |
296 |
append => $self->fragment_for('view'), |
297 |
args => { |
298 |
object_type => $object_type, |
299 |
id => { result_of => $create, name => 'id' }, |
300 |
unit => get('unit'), |
301 |
}, |
302 |
}, |
303 |
], |
304 |
as_button => 1, |
305 |
); |
306 |
}; |
307 |
|
308 |
}; |
309 |
}; |
310 |
|
311 |
1; |