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 |
sub object_type { 'Unit' }; |
10 |
|
11 |
sub display_columns { |
12 |
my $self = shift; |
13 |
return ( qw/name inv_id campaign material dimensions discovered_at description chronology location/ ); |
14 |
} |
15 |
|
16 |
#private template search_region => sub {}; |
17 |
|
18 |
template 'view' => sub :CRUDView { |
19 |
my $self = shift; |
20 |
my $id = get('id'); |
21 |
my $record = $self->_get_record( $id ); |
22 |
|
23 |
my $update = $record->as_update_action( |
24 |
moniker => "update-" . Jifty->web->serial, |
25 |
); |
26 |
|
27 |
my $delete = $record->as_delete_action( |
28 |
moniker => "delete-" . Jifty->web->serial, |
29 |
); |
30 |
my $editing = $record->current_user_can('write'); |
31 |
warn "## current_user_can('write') = $editing\n"; |
32 |
$editing = 0 unless $self->current_user->editing; |
33 |
warn "## editing ",$editing ? 'ok' : 'DENIED'; |
34 |
|
35 |
div { { class is 'unit' } |
36 |
|
37 |
if ( $editing ) { |
38 |
form { |
39 |
outs_raw($delete->button( |
40 |
label => _('Delete unit'), |
41 |
class => 'float-crud-button button-delete', |
42 |
onclick => { |
43 |
submit => $delete, |
44 |
confirm => _('Really delete?'), |
45 |
# region => Jifty->web->current_region, |
46 |
replace_with => '/__jifty/empty', |
47 |
refresh_self => 1, |
48 |
args => { id => $id }, |
49 |
}, |
50 |
)); |
51 |
}; |
52 |
}; |
53 |
|
54 |
|
55 |
form { |
56 |
foreach my $f ( $self->display_columns ) { |
57 |
if ( $f eq 'material' ) { |
58 |
set( search_collection => $record->material ); |
59 |
render_region( |
60 |
name => 'unit-materials', |
61 |
path => '/materials/list', |
62 |
defaults => { |
63 |
page => 1, |
64 |
unit => $record->id, |
65 |
} |
66 |
); |
67 |
} else { |
68 |
my %opt; |
69 |
$opt{render_mode} = 'read' if ! $editing; |
70 |
render_param( $update => $f, %opt ); |
71 |
} |
72 |
}; |
73 |
if ( $editing ) { |
74 |
div { |
75 |
{ class is 'submit_button' }; |
76 |
hyperlink( |
77 |
label => _('Update'), |
78 |
onclick => { |
79 |
submit => $update, |
80 |
refresh_self => 1, |
81 |
}, |
82 |
) |
83 |
}; |
84 |
} |
85 |
}; |
86 |
|
87 |
}; # div class unit |
88 |
|
89 |
show('/unitpictures/fragment', $record); |
90 |
|
91 |
hr {}; |
92 |
}; |
93 |
|
94 |
template 'new_item' => sub { |
95 |
my $self = shift; |
96 |
return unless $self->current_user->editing; |
97 |
|
98 |
my ( $object_type, $id ) = ( $self->object_type, get('id') ); |
99 |
my $record_class = $self->record_class; |
100 |
my $create = $record_class->as_create_action; |
101 |
|
102 |
h1 { _("New unit") }; |
103 |
|
104 |
form { |
105 |
|
106 |
foreach my $f ( $self->display_columns ) { |
107 |
last if $f eq 'material' && ! $id; |
108 |
render_param( $create => $f ); # unless $f eq 'material'; |
109 |
} |
110 |
|
111 |
div { |
112 |
{ class is 'submit_button' }; |
113 |
hyperlink( |
114 |
label => _("Add"), |
115 |
onclick => [ |
116 |
{ |
117 |
submit => $create, |
118 |
# args => { |
119 |
# }, |
120 |
}, |
121 |
{ refresh_self => 1 }, |
122 |
{ |
123 |
element => Jifty->web->current_region->parent->get_element( 'div.list' ), |
124 |
append => $self->fragment_for('view'), |
125 |
args => { |
126 |
object_type => $object_type, |
127 |
id => { result_of => $create, name => 'id' }, |
128 |
unit => get('unit'), |
129 |
}, |
130 |
}, |
131 |
], |
132 |
as_button => 1, |
133 |
); |
134 |
} |
135 |
|
136 |
}; |
137 |
}; |
138 |
|
139 |
1; |