1 |
package Arh::View::UnitPictures; |
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 { 'UnitPicture' }; |
12 |
|
13 |
private template search_region => sub {}; |
14 |
private template 'no_items_found' => sub {}; |
15 |
|
16 |
sub display_columns { |
17 |
my $self = shift; |
18 |
return ('picture'); |
19 |
} |
20 |
|
21 |
sub edit_columns { |
22 |
my $self = shift; |
23 |
return ('picture'); |
24 |
} |
25 |
|
26 |
template 'list' => sub { |
27 |
my $self = shift; |
28 |
|
29 |
my ( $page, $unit ) = get(qw(page unit)); |
30 |
my $item_path = $self->fragment_for("view"); |
31 |
my $fragment_for_new_picture = $self->fragment_for('new_picture'); |
32 |
my $collection = $self->_current_collection(); |
33 |
# XXX show just pictures from current unit |
34 |
$collection->limit( column => 'unit', value => $unit ) if $unit; |
35 |
div { |
36 |
{ class is 'pictures' }; |
37 |
|
38 |
show('./search_region'); |
39 |
show( './paging_top', $collection, $page ); |
40 |
show( './list_items', $collection, $item_path ); |
41 |
show( './paging_bottom', $collection, $page ); |
42 |
|
43 |
render_region( |
44 |
name => 'new_picture', |
45 |
path => $fragment_for_new_picture, |
46 |
defaults => { |
47 |
object_type => $self->object_type, |
48 |
unit => $unit, |
49 |
}, |
50 |
); |
51 |
} |
52 |
|
53 |
}; |
54 |
|
55 |
template 'view' => sub :CRUDView { |
56 |
my $self = shift; |
57 |
my ( $id, $unit ) = get(qw(id unit)); |
58 |
my $record = $self->_get_record( $id ); |
59 |
|
60 |
my $delete = $record->as_delete_action( |
61 |
moniker => "delete-" . Jifty->web->serial, |
62 |
); |
63 |
|
64 |
warn "## id: $id unit $unit"; |
65 |
|
66 |
form { |
67 |
if ( $record->current_user_can('delete') && $self->current_user->editing ) { |
68 |
outs_raw( $delete->button( |
69 |
label => _("Delete picture"), |
70 |
class => "float-crud-button button-delete", |
71 |
onclick => { |
72 |
submit => $delete, |
73 |
confirm => _('Really delete?'), |
74 |
replace_with => '/__jifty/empty', |
75 |
refresh_self => 1, |
76 |
args => { id => $id }, |
77 |
}, |
78 |
)); |
79 |
}; |
80 |
img { attr { src => "/static/pics/" . $record->picture->filename } } |
81 |
tt{ $record->picture->filename } |
82 |
}; |
83 |
|
84 |
}; |
85 |
|
86 |
template 'new_picture' => sub { |
87 |
my $self = shift; |
88 |
return unless $self->current_user->editing; |
89 |
|
90 |
my ( $id, $unit ) = get(qw(id unit)); |
91 |
warn "## unit $id $unit"; |
92 |
|
93 |
# my $record_class = $self->record_class; |
94 |
# warn "## new_picture record_class: $record_class\n"; |
95 |
# my $create = $record_class->as_create_action( |
96 |
|
97 |
my $create = new_action( |
98 |
class => 'UploadPicture', |
99 |
moniker => "create-" . Jifty->web->serial, |
100 |
); |
101 |
|
102 |
my $attach_to = |
103 |
Jifty->web->current_region->parent ? |
104 |
Jifty->web->current_region->parent->get_element( 'div.list' ) : |
105 |
Jifty->web->current_region; |
106 |
|
107 |
warn "## attach_to = $attach_to"; |
108 |
|
109 |
form { |
110 |
render_action( $create => [ 'campaign', 'content', 'type' ] ); |
111 |
outs_raw( $create->hidden( unit => $unit ) ); |
112 |
hyperlink( |
113 |
label => _("Add picture"), |
114 |
class => "float-crud-button button-add", |
115 |
onclick => [ |
116 |
{ |
117 |
submit => $create, |
118 |
}, |
119 |
{ refresh_self => 1 }, |
120 |
{ |
121 |
element => $attach_to, |
122 |
append => $self->fragment_for('view'), |
123 |
args => { |
124 |
object_type => $self->object_type, |
125 |
id => { result_of => $create, name => 'id' }, |
126 |
unit => $unit, |
127 |
}, |
128 |
}, |
129 |
], |
130 |
as_button => 1, |
131 |
); |
132 |
}; |
133 |
}; |
134 |
|
135 |
template 'fragment' => sub { |
136 |
my ( $self, $unit ) = @_; |
137 |
|
138 |
#warn "## unit = ", dump( $unit ); |
139 |
|
140 |
my $unitpictures = Arh::Model::UnitPictureCollection->new; |
141 |
$unitpictures->limit( column => 'unit', value => $unit->id ); |
142 |
warn "## ", $unitpictures->count, " pictures for unit ",$unit->id; |
143 |
|
144 |
div { |
145 |
{ class is 'pictures-by-unit' }; |
146 |
|
147 |
if ( $unitpictures->count ) { |
148 |
em { _('Pictures for unit'), ' ', $unit->name }; |
149 |
} else { |
150 |
em { _('No pictures for unit'), ' ', $unit->name }; |
151 |
} |
152 |
|
153 |
set( search_collection => $unitpictures ); |
154 |
render_region( |
155 |
name => 'unit-pictures', |
156 |
path => '/unitpictures/list', |
157 |
defaults => { |
158 |
page => 1, |
159 |
unit => $unit->id, |
160 |
} |
161 |
); |
162 |
|
163 |
} |
164 |
}; |
165 |
|
166 |
1; |