line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::Helper::Controller::Scaffold::HTML::Template; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
91117
|
use strict; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
76
|
|
4
|
2
|
|
|
2
|
|
6267
|
use Path::Class; |
|
2
|
|
|
|
|
124556
|
|
|
2
|
|
|
|
|
508
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Catalyst::Helper::Controller::Scaffold::HTML::Template - Helper for Scaffolding using HTML::Template. |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 SYNOPSIS |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Imagine you want to generate a scaffolding controller MyApp::C::SomeTable |
15
|
|
|
|
|
|
|
# for a CDBI table class MyApp::M::CDBI::SomeTable |
16
|
|
|
|
|
|
|
script/myapp_create.pl controller SomeTable Scaffold::HTML::Template CDBI::SomeTable |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 DESCRIPTION |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
This module is heavily based on Catalyst::Helper::Controller::Scaffold. |
21
|
|
|
|
|
|
|
It provides a framework to do the basic data operations (edit, view, list, |
22
|
|
|
|
|
|
|
delete, add). |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
Scaffolding is very simple with Catalyst, as most of the code will be automagically |
25
|
|
|
|
|
|
|
generated to handle the basic operations. |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Let's say you want to handle the data in SomeTable, all you have to do is : |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
script/myapp_create.pl controller SomeTable Scaffold::HTML::Template CDBI::SomeTable |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
this will create a controller for SomeTable using the model CDBI::SomeTable and |
32
|
|
|
|
|
|
|
all the required HTML::Template, namely : |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
lib/myapp/C/SomeTable.pm |
35
|
|
|
|
|
|
|
root/SomeTable/add.tmpl |
36
|
|
|
|
|
|
|
root/SomeTable/edit.tmpl |
37
|
|
|
|
|
|
|
root/SomeTable/list.tmpl |
38
|
|
|
|
|
|
|
root/SomeTable/view.tmpl |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
Now just add these lines to your CDBI class... |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
use Class::DBI::AsForm; |
43
|
|
|
|
|
|
|
use Class::DBI::FromForm; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
...and modify the one in your application class, to load the FormValidator plugin. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
use Catalyst qw/-Debug FormValidator/; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
You're done ! |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Just browse http://127.0.0.1:3000/sometable and enjoy the Catalyst's power : |
52
|
|
|
|
|
|
|
You can now add elements in SomeTable, view them, modify them, list them and delete them. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head1 METHODS |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=over 4 |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=item mk_compclass |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
mk_compclass now accept a hashref as its last argument. |
61
|
|
|
|
|
|
|
This hash can be used to overrides all $helper attributes |
62
|
|
|
|
|
|
|
before calling its mk_dir and render_file methods |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=cut |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub mk_compclass { |
67
|
0
|
|
|
0
|
1
|
|
my ( $self, $helper, $table_class, $ref_options ) = @_; |
68
|
0
|
0
|
|
|
|
|
if ($ref_options) { |
69
|
0
|
|
|
|
|
|
my %options = %$ref_options; |
70
|
0
|
|
|
|
|
|
for (keys %options) { $helper->{$_} = $options{$_} } |
|
0
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
} |
72
|
0
|
|
|
|
|
|
$helper->{table_class} = $helper->{app} . '::M::' . $table_class; |
73
|
0
|
|
|
|
|
|
my $file = $helper->{file}; |
74
|
0
|
|
|
|
|
|
my $dir = dir( $helper->{base}, 'root', $helper->{prefix} ); |
75
|
0
|
|
|
|
|
|
$helper->mk_dir($dir); |
76
|
0
|
|
|
|
|
|
$helper->render_file( 'compclass', $file ); |
77
|
0
|
|
|
|
|
|
$helper->render_file( 'add', file( $dir, 'add.tmpl' ) ); |
78
|
0
|
|
|
|
|
|
$helper->render_file( 'edit', file( $dir, 'edit.tmpl' ) ); |
79
|
0
|
|
|
|
|
|
$helper->render_file( 'list', file( $dir, 'list.tmpl' ) ); |
80
|
0
|
|
|
|
|
|
$helper->render_file( 'view', file( $dir, 'view.tmpl' ) ); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=back |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 AUTHOR |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Sebastian Riedel, Arnaud Assad |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head1 LICENSE |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
This library is free software . You can redistribute it and/or modify |
92
|
|
|
|
|
|
|
it under the same terms as perl itself. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=cut |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
1; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
__DATA__ |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
__compclass__ |
101
|
|
|
|
|
|
|
package [% class %]; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
use strict; |
104
|
|
|
|
|
|
|
use base 'Catalyst::Base'; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
__PACKAGE__->config( table_class => '[% table_class %]' ); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 NAME |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
[% class %] - Scaffolding Controller Component |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 SYNOPSIS |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
See L<[% app %]> |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 DESCRIPTION |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Scaffolding Controller Component. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 METHODS |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=over 4 |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=item add |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Sets a template. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=cut |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub add : Local { |
131
|
|
|
|
|
|
|
my ( $self, $c ) = @_; |
132
|
|
|
|
|
|
|
$c->stash->{columns} = [ |
133
|
|
|
|
|
|
|
map { |
134
|
|
|
|
|
|
|
{ column => $_, field => [% table_class %]->to_field($_)->as_XML } |
135
|
|
|
|
|
|
|
} [% table_class %]->columns |
136
|
|
|
|
|
|
|
]; |
137
|
|
|
|
|
|
|
$c->stash->{template} = ucfirst('[% uri %]') . '/add.tmpl'; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item default |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Forwards to list. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=cut |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub default : Private { |
147
|
|
|
|
|
|
|
my ( $self, $c ) = @_; |
148
|
|
|
|
|
|
|
$c->forward('list'); |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=item destroy |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Destroys a row and forwards to list. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=cut |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub destroy : Local { |
158
|
|
|
|
|
|
|
my ( $self, $c, $id ) = @_; |
159
|
|
|
|
|
|
|
[% table_class %]->retrieve($id)->delete; |
160
|
|
|
|
|
|
|
$c->forward('list'); |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item do_add |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Adds a new row to the table and forwards to list. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=cut |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub do_add : Local { |
170
|
|
|
|
|
|
|
my ( $self, $c ) = @_; |
171
|
|
|
|
|
|
|
$c->form( optional => [ [% table_class %]->columns ] ); |
172
|
|
|
|
|
|
|
[% table_class %]->create_from_form( $c->form ); |
173
|
|
|
|
|
|
|
$c->forward('list'); |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=item do_edit |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Edits a row and forwards to edit. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=cut |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub do_edit : Local { |
183
|
|
|
|
|
|
|
my ( $self, $c, $id ) = @_; |
184
|
|
|
|
|
|
|
$c->form( optional => [ [% table_class %]->columns ] ); |
185
|
|
|
|
|
|
|
[% table_class %]->retrieve($id)->update_from_form( $c->form ); |
186
|
|
|
|
|
|
|
$c->forward('edit'); |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=item edit |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Sets a template. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=cut |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sub edit : Local { |
196
|
|
|
|
|
|
|
my ( $self, $c, $id ) = @_; |
197
|
|
|
|
|
|
|
$c->stash->{id} = $id; |
198
|
|
|
|
|
|
|
$c->stash->{item} = [% table_class %]->retrieve($id); |
199
|
|
|
|
|
|
|
$c->stash->{columns} = [ |
200
|
|
|
|
|
|
|
map { |
201
|
|
|
|
|
|
|
{ column => $_, field => $c->stash->{item}->to_field($_)->as_XML } |
202
|
|
|
|
|
|
|
} grep { $_ !~ /^id$/i } [% table_class %]->columns |
203
|
|
|
|
|
|
|
]; |
204
|
|
|
|
|
|
|
$c->stash->{template} = ucfirst('[% uri %]') . '/edit.tmpl'; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=item list |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Sets a template. |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=cut |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
sub list : Local { |
214
|
|
|
|
|
|
|
my ( $self, $c ) = @_; |
215
|
|
|
|
|
|
|
my @columns = [% table_class %]->columns; |
216
|
|
|
|
|
|
|
$c->stash->{columns} = |
217
|
|
|
|
|
|
|
[ map { { column => $_ } } grep { $_ !~ /^id$/i } @columns ]; |
218
|
|
|
|
|
|
|
my @objects; |
219
|
|
|
|
|
|
|
for my $object ( [% table_class %]->retrieve_all ) { |
220
|
|
|
|
|
|
|
my @cols; |
221
|
|
|
|
|
|
|
for my $col (@columns) { |
222
|
|
|
|
|
|
|
push @cols, { column => $object->$col }; |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
push @objects, |
225
|
|
|
|
|
|
|
{ columns => [@cols], base => $c->req->base, id => $object->id }; |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
$c->stash->{objects} = [@objects]; |
228
|
|
|
|
|
|
|
$c->stash->{template} = ucfirst('[% uri %]') . '/list.tmpl'; |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=item view |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
Fetches a row and sets a template. |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=cut |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
sub view : Local { |
238
|
|
|
|
|
|
|
my ( $self, $c, $id ) = @_; |
239
|
|
|
|
|
|
|
$c->stash->{item} = [% table_class %]->retrieve($id); |
240
|
|
|
|
|
|
|
$c->stash->{columns} = |
241
|
|
|
|
|
|
|
[ map { { column => $_, value => $c->stash->{item}->$_ } } |
242
|
|
|
|
|
|
|
grep { $_ !~ /^id$/i } [% table_class %]->columns ]; |
243
|
|
|
|
|
|
|
$c->stash->{template} = ucfirst('[% uri %]') . '/view.tmpl'; |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
=back |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=head1 AUTHOR |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
[% author %] |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=head1 LICENSE |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
This library is free software . You can redistribute it and/or modify |
255
|
|
|
|
|
|
|
it under the same terms as perl itself. |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=cut |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
1; |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
__add__ |
262
|
|
|
|
|
|
|
<form action="<TMPL_VAR NAME="base">[% uri %]/do_add" method="post"> |
263
|
|
|
|
|
|
|
<TMPL_LOOP NAME="columns"> |
264
|
|
|
|
|
|
|
<TMPL_VAR NAME="column"><br /> |
265
|
|
|
|
|
|
|
<TMPL_VAR NAME="field"><br /> |
266
|
|
|
|
|
|
|
</TMPL_LOOP> |
267
|
|
|
|
|
|
|
<input type="submit" value="Add" /> |
268
|
|
|
|
|
|
|
</form> <br /> |
269
|
|
|
|
|
|
|
<a href="<TMPL_VAR NAME="base">[% uri %]/list">List</a> |
270
|
|
|
|
|
|
|
__edit__ |
271
|
|
|
|
|
|
|
<form action="<TMPL_VAR NAME="base">[% uri %]/do_edit/<TMPL_VAR NAME="id">" method="post"> |
272
|
|
|
|
|
|
|
<TMPL_LOOP NAME="columns"> |
273
|
|
|
|
|
|
|
<TMPL_VAR NAME="column"><br /> |
274
|
|
|
|
|
|
|
<TMPL_VAR NAME="field"><br /> |
275
|
|
|
|
|
|
|
</TMPL_LOOP> |
276
|
|
|
|
|
|
|
<input type="submit" value="Edit" /> |
277
|
|
|
|
|
|
|
</form> |
278
|
|
|
|
|
|
|
<br /> |
279
|
|
|
|
|
|
|
<a href="<TMPL_VAR NAME = "base">[% uri %]/list">List</a> |
280
|
|
|
|
|
|
|
__list__ |
281
|
|
|
|
|
|
|
<table> |
282
|
|
|
|
|
|
|
<tr> |
283
|
|
|
|
|
|
|
<TMPL_LOOP NAME="columns"> |
284
|
|
|
|
|
|
|
<th> <TMPL_VAR NAME="column"> </th> |
285
|
|
|
|
|
|
|
</TMPL_LOOP> |
286
|
|
|
|
|
|
|
</tr> |
287
|
|
|
|
|
|
|
<TMPL_LOOP NAME="objects"> |
288
|
|
|
|
|
|
|
<tr> |
289
|
|
|
|
|
|
|
<TMPL_LOOP NAME="columns"> |
290
|
|
|
|
|
|
|
<td> <TMPL_VAR NAME="column"> </td> |
291
|
|
|
|
|
|
|
</TMPL_LOOP> |
292
|
|
|
|
|
|
|
<td> |
293
|
|
|
|
|
|
|
<a href="<TMPL_VAR NAME="base">[% uri %]/view/<TMPL_VAR NAME="id">"> |
294
|
|
|
|
|
|
|
View |
295
|
|
|
|
|
|
|
</a> |
296
|
|
|
|
|
|
|
<a href="<TMPL_VAR NAME="base">[% uri %]/edit/<TMPL_VAR NAME="id">"> |
297
|
|
|
|
|
|
|
Edit |
298
|
|
|
|
|
|
|
</a> |
299
|
|
|
|
|
|
|
<a href="<TMPL_VAR NAME="base">[% uri %]/destroy/<TMPL_VAR NAME="id">"> |
300
|
|
|
|
|
|
|
Destroy |
301
|
|
|
|
|
|
|
</a> |
302
|
|
|
|
|
|
|
</td> |
303
|
|
|
|
|
|
|
</tr> |
304
|
|
|
|
|
|
|
</TMPL_LOOP> |
305
|
|
|
|
|
|
|
</table> |
306
|
|
|
|
|
|
|
<a href="<TMPL_VAR NAME="base">[% uri %]/add">Add</a> |
307
|
|
|
|
|
|
|
__view__ |
308
|
|
|
|
|
|
|
<TMPL_LOOP NAME="columns"> |
309
|
|
|
|
|
|
|
<b><TMPL_VAR NAME="column"></b><br /> |
310
|
|
|
|
|
|
|
<TMPL_VAR NAME="value"><br/><br /> |
311
|
|
|
|
|
|
|
</TMPL_LOOP> |
312
|
|
|
|
|
|
|
<a href="<TMPL_VAR NAME="base">[% uri %]/list">List</a> |