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