line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::Controller::CRUD; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1946
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.21'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Catalyst::Controller::CRUD - CRUD (create/read/update/delete) Controller for Catalyst |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 SYNOPSIS |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
package MyApp::Controller::Foo; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
use base qw(Catalyst::Controller); |
17
|
|
|
|
|
|
|
use Catalyst::Controller::CRUD::CDBI; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub create : Local { |
20
|
|
|
|
|
|
|
my ($self, $c) = @_; |
21
|
|
|
|
|
|
|
Catalyst::Controller::CRUD::CDBI->create($c, $self); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
1; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 DESCRIPTION |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
This module provides CRUD (create/read/update/delete) action. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
create: insert new record |
31
|
|
|
|
|
|
|
read: retrieve record |
32
|
|
|
|
|
|
|
update: update already record |
33
|
|
|
|
|
|
|
delete: delete record |
34
|
|
|
|
|
|
|
list: retrieve all records |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head2 EXPORT |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
None by default. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head1 METHODS |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head2 create |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Create action. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
If there is $c->stash->{create}->{error}, then it does not insert new recoed. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
Triggers: |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
$self->call_trigger( 'create_check', $c, $hash ); |
51
|
|
|
|
|
|
|
$self->call_trigger( 'create_after', $c, $model ); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=cut |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub create { |
56
|
0
|
|
|
0
|
1
|
|
my ( $this, $c, $self ) = @_; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# insert new record |
59
|
0
|
0
|
0
|
|
|
|
if ( $c->req->param('btn_create') ) { |
|
|
0
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# create hash from request parameters |
61
|
0
|
|
|
|
|
|
my $hash; |
62
|
0
|
|
|
|
|
|
foreach (@{ $self->setting($c)->{columns} }) { |
|
0
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
my $param = $c->req->param($_); |
64
|
0
|
0
|
|
|
|
|
$hash->{$_} = $param if ( defined $param ); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# create check |
68
|
0
|
|
|
|
|
|
$self->call_trigger( 'create_check', $c, $hash ); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# insert new record |
71
|
0
|
0
|
0
|
|
|
|
if ( !$c->stash->{create}->{error} and scalar( keys %{$hash} ) ) { |
|
0
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
my $model = $c->model( $self->setting($c)->{model} )->create($hash); |
73
|
0
|
|
|
|
|
|
$self->call_trigger( 'create_after', $c, $model ); |
74
|
0
|
|
|
|
|
|
$c->res->redirect( $self->setting($c)->{default} ); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# prepare create form |
78
|
|
|
|
|
|
|
else { |
79
|
0
|
|
|
|
|
|
$c->stash->{$self->setting($c)->{name}} = $hash; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# copy record ex) /xxx/create/yyy |
84
|
|
|
|
|
|
|
elsif ( defined $c->req->args->[0] and $c->req->args->[0] =~ /^\d+$/ ) { |
85
|
0
|
|
|
|
|
|
$this->get_model( $c, $self, $c->req->args->[0] ); |
86
|
0
|
|
|
|
|
|
undef $c->stash->{$self->setting($c)->{name}}->{$self->setting($c)->{primary}}; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# template setting |
90
|
0
|
|
|
|
|
|
my $prefix = $self->setting($c)->{template}->{prefix}; |
91
|
0
|
0
|
|
|
|
|
my $suffix = $self->setting($c)->{template}->{suffix} ? $self->setting($c)->{template}->{suffix} : '.tt'; |
92
|
0
|
|
|
|
|
|
$c->stash->{template} = $prefix . 'create' . $suffix; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head2 read |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Read action. |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=cut |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub read { |
102
|
0
|
|
|
0
|
1
|
|
my ( $this, $c, $self ) = @_; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
# get model |
105
|
0
|
0
|
0
|
|
|
|
if ( defined $c->req->args->[0] and $c->req->args->[0] =~ /^\d+$/ ) { |
106
|
0
|
|
|
|
|
|
$this->get_model( $c, $self, $c->req->args->[0] ); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# template setting |
110
|
0
|
|
|
|
|
|
my $prefix = $self->setting($c)->{template}->{prefix}; |
111
|
0
|
0
|
|
|
|
|
my $suffix = $self->setting($c)->{template}->{suffix} ? $self->setting($c)->{template}->{suffix} : '.tt'; |
112
|
0
|
|
|
|
|
|
$c->stash->{template} = $prefix . 'read' . $suffix; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head2 update |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Update action. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
If there is $c->stash->{update}->{error}, then it does not update already recoed. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Triggers: |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
$self->call_trigger( 'update_check', $c, $hash ); |
124
|
|
|
|
|
|
|
$self->call_trigger( 'update_after', $c, $model ); |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=cut |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub update { |
129
|
0
|
|
|
0
|
1
|
|
my ( $this, $c, $self ) = @_; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# update already record |
132
|
0
|
0
|
0
|
|
|
|
if ( $c->req->param('btn_update') ) { |
|
|
0
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
my $model = $this->get_model( $c, $self, $c->req->param( $self->setting($c)->{primary} ) ); |
134
|
0
|
0
|
|
|
|
|
if (defined $model) { |
135
|
|
|
|
|
|
|
# create hash from request parameters |
136
|
0
|
|
|
|
|
|
my $primary = $self->setting($c)->{primary}; |
137
|
0
|
|
|
|
|
|
my $hash = {$primary => $model->$primary}; |
138
|
0
|
|
|
|
|
|
foreach (@{ $self->setting($c)->{columns} }) { |
|
0
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
my $value = $c->req->param($_); |
140
|
0
|
0
|
|
|
|
|
if (defined $value) { |
141
|
0
|
|
|
|
|
|
$hash->{$_} = $value; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
# update check |
146
|
0
|
|
|
|
|
|
$self->call_trigger( 'update_check', $c, $hash ); |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
# update already record |
149
|
0
|
0
|
0
|
|
|
|
if ( !$c->stash->{update}->{error} and scalar( keys %{$hash} ) ) { |
|
0
|
|
|
|
|
|
|
150
|
0
|
|
|
|
|
|
foreach (@{ $self->setting($c)->{columns} }) { |
|
0
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
|
my $value = $c->req->param($_); |
152
|
0
|
0
|
|
|
|
|
if (defined $value) { |
153
|
0
|
|
|
|
|
|
$model->$_( $value ); |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
} |
156
|
0
|
|
|
|
|
|
$model->update(); |
157
|
0
|
|
|
|
|
|
$self->call_trigger( 'update_after', $c, $model ); |
158
|
0
|
|
|
|
|
|
$c->res->redirect( $self->setting($c)->{default} ); |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
# prepare update form |
162
|
|
|
|
|
|
|
else { |
163
|
0
|
|
|
|
|
|
undef $c->stash->{$self->setting($c)->{name}}; |
164
|
0
|
|
|
|
|
|
$c->stash->{$self->setting($c)->{name}} = $hash; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
# prepare update form |
170
|
|
|
|
|
|
|
elsif ( defined $c->req->args->[0] and $c->req->args->[0] =~ /^\d+$/ ) { |
171
|
0
|
|
|
|
|
|
$this->get_model( $c, $self, $c->req->args->[0] ); |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
# update error |
175
|
|
|
|
|
|
|
else { |
176
|
0
|
|
|
|
|
|
$c->res->status(404); |
177
|
0
|
|
|
|
|
|
$c->res->body("404 Not Found\n"); |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
# template setting |
181
|
0
|
|
|
|
|
|
my $prefix = $self->setting($c)->{template}->{prefix}; |
182
|
0
|
0
|
|
|
|
|
my $suffix = $self->setting($c)->{template}->{suffix} ? $self->setting($c)->{template}->{suffix} : '.tt'; |
183
|
0
|
|
|
|
|
|
$c->stash->{template} = $prefix . 'update' . $suffix; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head2 delete |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Delete action. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
If there is $c->stash->{delete}->{error}, then it does not delete recoed. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Triggers: |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
$self->call_trigger( 'delete_check', $c, $model ); |
195
|
|
|
|
|
|
|
$self->call_trigger( 'delete_after', $c, $model ); |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=cut |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub delete { |
200
|
0
|
|
|
0
|
1
|
|
my ( $this, $c, $self ) = @_; |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
# delete record |
203
|
0
|
0
|
0
|
|
|
|
if ( defined $c->req->args->[0] and $c->req->args->[0] =~ /^\d+$/ ) { |
204
|
0
|
|
|
|
|
|
my $model = $this->get_model( $c, $self, $c->req->args->[0] ); |
205
|
0
|
0
|
|
|
|
|
if ( defined $model ) { |
206
|
0
|
|
|
|
|
|
$self->call_trigger( 'delete_check', $c, $model ); |
207
|
0
|
0
|
|
|
|
|
if ( !$c->stash->{delete}->{error} ) { |
208
|
0
|
0
|
|
|
|
|
if ($model->can('disable')) { |
209
|
0
|
|
|
|
|
|
$model->disable(1); |
210
|
0
|
|
|
|
|
|
$model->update(); |
211
|
|
|
|
|
|
|
} else { |
212
|
0
|
|
|
|
|
|
$model->delete(); |
213
|
|
|
|
|
|
|
} |
214
|
0
|
|
|
|
|
|
$self->call_trigger( 'delete_after', $c, $model ); |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
} |
217
|
0
|
|
|
|
|
|
$c->res->redirect( $self->setting($c)->{default} ); |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
# delete error |
221
|
|
|
|
|
|
|
else { |
222
|
0
|
|
|
|
|
|
$c->res->status(404); |
223
|
0
|
|
|
|
|
|
$c->res->body("404 Not Found\n"); |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head2 list |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
List action. |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=cut |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
sub list { |
234
|
0
|
|
|
0
|
1
|
|
my ( $this, $c, $self ) = @_; |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
# get models |
237
|
0
|
|
|
|
|
|
$c->stash->{ $self->setting($c)->{name} . 's' } = $this->get_models( $c, $self ); |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
# template setting |
240
|
0
|
|
|
|
|
|
my $prefix = $self->setting($c)->{template}->{prefix}; |
241
|
0
|
0
|
|
|
|
|
my $suffix = $self->setting($c)->{template}->{suffix} ? $self->setting($c)->{template}->{suffix} : '.tt'; |
242
|
0
|
|
|
|
|
|
$c->stash->{template} = $prefix . 'list' . $suffix; |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head1 INTERFACE METHODS |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=head2 get_model($this,$c,$self,$id) |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
This method returns model object having $id. |
250
|
|
|
|
|
|
|
This method must be implemented by sub class. |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=cut |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
sub get_model { |
255
|
0
|
|
|
0
|
1
|
|
die 'this method must be overriden in the subclass.'; |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=head2 get_models($this,$c,$self) |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
This method returns model objects. |
261
|
|
|
|
|
|
|
This method must be implemented by sub class. |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=cut |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
sub get_models { |
266
|
0
|
|
|
0
|
1
|
|
die 'this method must be overriden in the subclass.'; |
267
|
|
|
|
|
|
|
} |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=head1 SEE ALSO |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
Catalyst, Catalyst::Plugin::CRUD |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=head1 AUTHOR |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
Jun Shimizu, Ebayside@cpan.orgE |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
Copyright (C) 2006-2007 by Jun Shimizu |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
282
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.2 or, |
283
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
=cut |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
1; |