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 |
|
36 |
if ( $editing ) { |
37 |
form { |
38 |
outs_raw($delete->button( |
39 |
label => _('Delete'), |
40 |
class => 'float-crud-button', |
41 |
onclick => { |
42 |
submit => $delete, |
43 |
confirm => _('Really delete?'), |
44 |
# region => Jifty->web->current_region, |
45 |
replace_with => '/__jifty/empty', |
46 |
refresh_self => 1, |
47 |
args => { id => $id }, |
48 |
}, |
49 |
)); |
50 |
}; |
51 |
}; |
52 |
|
53 |
form { |
54 |
foreach my $f ( $self->display_columns ) { |
55 |
if ( $f eq 'material' ) { |
56 |
set( search_collection => $record->material ); |
57 |
render_region( |
58 |
name => 'unit-materials', |
59 |
path => '/materials/list', |
60 |
defaults => { |
61 |
page => 1, |
62 |
unit => $record->id, |
63 |
} |
64 |
); |
65 |
} else { |
66 |
my %opt; |
67 |
$opt{render_mode} = 'read' if ! $editing; |
68 |
render_param( $update => $f, %opt ); |
69 |
} |
70 |
}; |
71 |
if ( $editing ) { |
72 |
div { |
73 |
{ class is 'submit_button' }; |
74 |
hyperlink( |
75 |
label => _('Update'), |
76 |
onclick => { |
77 |
submit => $update, |
78 |
refresh_self => 1, |
79 |
}, |
80 |
) |
81 |
}; |
82 |
} |
83 |
}; |
84 |
hr {}; |
85 |
}; |
86 |
|
87 |
template 'new_item' => sub { |
88 |
my $self = shift; |
89 |
return unless $self->current_user->editing; |
90 |
|
91 |
my ( $object_type, $id ) = ( $self->object_type, get('id') ); |
92 |
my $record_class = $self->record_class; |
93 |
my $create = $record_class->as_create_action; |
94 |
|
95 |
h1 { _("New unit") }; |
96 |
|
97 |
form { |
98 |
|
99 |
foreach my $f ( $self->display_columns ) { |
100 |
last if $f eq 'material' && ! $id; |
101 |
render_param( $create => $f ); # unless $f eq 'material'; |
102 |
} |
103 |
|
104 |
div { |
105 |
{ class is 'submit_button' }; |
106 |
hyperlink( |
107 |
label => _("Add"), |
108 |
onclick => [ |
109 |
{ |
110 |
submit => $create, |
111 |
# args => { |
112 |
# }, |
113 |
}, |
114 |
{ refresh_self => 1 }, |
115 |
{ |
116 |
element => Jifty->web->current_region->parent->get_element( 'div.list' ), |
117 |
append => $self->fragment_for('view'), |
118 |
args => { |
119 |
object_type => $object_type, |
120 |
id => { result_of => $create, name => 'id' }, |
121 |
unit => get('unit'), |
122 |
}, |
123 |
}, |
124 |
], |
125 |
as_button => 1, |
126 |
); |
127 |
} |
128 |
|
129 |
}; |
130 |
}; |
131 |
|
132 |
1; |