Action.pm untested

filter: Statementstmt 0.0% 0 / 131 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 29 totaltotal 0.0% 0 / 206 CC 5 SCAR 10.5
CC
5
Cov
0%
_dest
6.0
_safety
6.0
_effect_from_asp
6.0
CRAP 2.9
1 ############################################################################
2 #
3 # Win32::ASP::Action - an abstract parent class for Actions
4 # in the Win32-ASP-DB system
5 #
6 # Author: Toby Everett
7 # Revision: 0.02
8 # Last Change:
9 ############################################################################
10 # Copyright 1999, 2000 Toby Everett. All rights reserved.
11 #
12 # This file is distributed under the Artistic License. See
13 # http://www.ActiveState.com/corporate/artistic_license.htm or
14 # the license that comes with your perl distribution.
15 #
16 # For comments, questions, bugs or general interest, feel free to
17 # contact Toby Everett at teverett@alascom.att.com
18 ############################################################################
19
20 use Class::SelfMethods;
21 use Error qw/:try/;
22 use Win32::ASP::Error;
23
24 package Win32::ASP::Action;
25 @ISA = ('Class::SelfMethods');
26
27 use strict;
28
29 sub new {
30 my $class = shift;
31
32 my $self = $class->SUPER::new(@_);
33
34 return($self->name, $self);
35 }
36
37 sub _label {
38 my $self = shift;
39 return $self->name;
40 }
41
42 sub _has_verify {
43 my $self = shift;
44 return 1;
45 }
46
47 sub _dest {
48 my $self = shift;
49 return $self->has_verify ? './action.asp' : './action2.asp';
50 }
51
52 sub _permit {
53 my $self = shift;
54 my($record) = @_;
55 return 1;
56 }
57
58 sub _safety {
59 my $self = shift;
60 my($record) = @_;
61
62 unless ($self->permit($record)) {
63 my $identifier = join(", ", map {"$_ $record->{orig}->{$_}"} $record->_PRIMARY_KEY);
64 throw Win32::ASP::Error::DBRecord::no_permission(action => $self->label, identifier => $identifier);
65 }
66 }
67
68 sub _effect {
69 my $self = shift;
70 my($record) = @_;
71 }
72
73 sub _effect_from_asp {
74 my $self = shift;
75 my($record, @params) = @_;
76
77 my $action = $main::Request->querystring('action')->item;
78 my(@primary_keys) = map {$main::Request->querystring($_)->item} $record->_PRIMARY_KEY;
79
80 $record->read(@primary_keys);
81 $self->safety($record);
82 if (exists $record->_FIELDS->{timestamp}) {
83 $record->set_timestamp($main::Request->QueryString('timestamp')->item);
84 } else {
85 $record->edit;
86 }
87 $self->effect($record, @params);
88 }
89
90 sub _disp_trigger {
91 my $self = shift;
92 my($record) = @_;
93
94 if ($self->permit($record)) {
95 my(%parameters) = map {($_, $record->{orig}->{$_})} $record->_PRIMARY_KEY;
96 exists $record->_FIELDS->{timestamp} and $parameters{timestamp} = $record->{orig}->{timestamp};
97 $parameters{action} = $self->name;
98 my $url = Win32::ASP::FormatURL($self->dest, %parameters);
99 (my $label = $self->label) =~ s/ /\&nbsp\;/g;
100 return "<A HREF=\"$url\">$label</A>";
101 }
102 }
103
104 sub _disp_verify {
105 my $self = shift;
106 my($record) = @_;
107
108 my $verify_msg = $self->verify_msg($record);
109 my(%primary_keys) = map {($_, $main::Request->QueryString($_)->Item)} $record->_PRIMARY_KEY;
110 my $yesurl = Win32::ASP::FormatURL('./action2.asp', &Win32::ASP::QueryStringList);
111 my $view_record = $self->view_record($record);
112 return <<ENDHTML;
113 $verify_msg<P>
114 <A HREF="$yesurl">Confirm</A><P>
115 $view_record
116 ENDHTML
117 }
118
119 sub _disp_success {
120 my $self = shift;
121 my($record) = @_;
122
123 my $success_msg = $self->success_msg($record);
124 my $view_record = $self->view_record($record);
125 return "$success_msg<P>\n$view_record";
126 }
127
128 sub _verify_msg {
129 my $self = shift;
130 my($record) = @_;
131
132 (my $label = $self->label) =~ s/ /\&nbsp\;/g;
133 my $identifier = $self->identifier($record);
134
135 return "Are you sure you want to $label $identifier?";
136 }
137
138 sub _success_msg {
139 my $self = shift;
140 my($record) = @_;
141
142 (my $label = $self->label) =~ s/ /\&nbsp\;/g;
143 my $identifier = $self->identifier($record);
144
145 return "The action $label was successfully completed on $identifier.";
146 }
147
148 sub _view_record {
149 my $self = shift;
150 my($record) = @_;
151
152 my(%primary_keys) = map {($_, $main::Request->QueryString($_)->Item)} $record->_PRIMARY_KEY;
153 my $url = Win32::ASP::FormatURL('./view.asp', %primary_keys);
154 my $identifier = $self->identifier($record);
155
156 return "Return to viewing <A HREF=\"$url\">$identifier</A>.";
157 }
158
159 sub _identifier {
160 my $self = shift;
161 my($record) = @_;
162
163 my(%primary_keys) = map {($_, $main::Request->QueryString($_)->item)} $record->_PRIMARY_KEY;
164 return join(', ', map {"$_ $primary_keys{$_}"} $record->_PRIMARY_KEY);
165 }
166
167
168
169
170
171 package Win32::ASP::Action::Insert;
172
173 @Win32::ASP::Action::Insert::ISA = ('Win32::ASP::Action');
174
175 sub _name {
176 my $self = shift;
177 return 'insert';
178 }
179
180 sub _label {
181 my $self = shift;
182 return 'Add New';
183 }
184
185 sub _permit {
186 my $self = shift;
187 my($record) = @_;
188
189 return $record->can_insert;
190 }
191
192 sub _dest {
193 my $self = shift;
194 return './insert.asp';
195 }
196
197 package Win32::ASP::Action::Edit;
198
199 @Win32::ASP::Action::Edit::ISA = ('Win32::ASP::Action');
200
201 sub _name {
202 my $self = shift;
203 return 'edit';
204 }
205
206 sub _label {
207 my $self = shift;
208 return 'Edit';
209 }
210
211 sub _permit {
212 my $self = shift;
213 my($record) = @_;
214
215 return $record->can_update;
216 }
217
218 sub _dest {
219 my $self = shift;
220 return './edit.asp';
221 }
222
223 package Win32::ASP::Action::Delete;
224
225 @Win32::ASP::Action::Delete::ISA = ('Win32::ASP::Action');
226
227 sub _name {
228 my $self = shift;
229 return 'delete';
230 }
231
232 sub _label {
233 my $self = shift;
234 return 'Delete';
235 }
236
237 sub _disp_success {
238 my $self = shift;
239 my($record) = @_;
240
241 return $self->success($record);
242 }
243
244 sub _success {
245 my $self = shift;
246 my($record) = @_;
247
248 my $identifier = $self->identifier($record);
249
250 return "$identifier was successfully deleted.";
251 }
252
253 sub _permit {
254 my $self = shift;
255 my($record) = @_;
256
257 return $record->can_delete;
258 }
259
260 sub _effect {
261 my $self = shift;
262 my($record) = @_;
263
264 $record->delete;
265 }
266
267 1;