blib/lib/CGI/Application/Plugin/MessageStack.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 57 | 157 | 36.3 |
branch | 15 | 94 | 15.9 |
condition | 7 | 65 | 10.7 |
subroutine | 11 | 14 | 78.5 |
pod | 5 | 5 | 100.0 |
total | 95 | 335 | 28.3 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package CGI::Application::Plugin::MessageStack; | ||||||
2 | |||||||
3 | 11 | 11 | 696345 | use CGI::Application 4.01; | |||
11 | 273 | ||||||
11 | 339 | ||||||
4 | |||||||
5 | 11 | 11 | 168 | use 5.006; | |||
11 | 40 | ||||||
11 | 584 | ||||||
6 | 11 | 11 | 68 | use warnings; | |||
11 | 45 | ||||||
11 | 373 | ||||||
7 | 11 | 11 | 69 | use strict; | |||
11 | 32 | ||||||
11 | 528 | ||||||
8 | |||||||
9 | =head1 NAME | ||||||
10 | |||||||
11 | CGI::Application::Plugin::MessageStack - A message stack for your CGI::Application | ||||||
12 | |||||||
13 | =head1 VERSION | ||||||
14 | |||||||
15 | Version 0.34 | ||||||
16 | |||||||
17 | =cut | ||||||
18 | |||||||
19 | 11 | 11 | 60 | use vars qw( @ISA $VERSION @EXPORT %config ); | |||
11 | 17 | ||||||
11 | 25996 | ||||||
20 | |||||||
21 | @ISA = qw( Exporter AutoLoader ); | ||||||
22 | |||||||
23 | @EXPORT = qw( | ||||||
24 | push_message | ||||||
25 | pop_message | ||||||
26 | clear_messages | ||||||
27 | messages | ||||||
28 | capms_config | ||||||
29 | ); | ||||||
30 | |||||||
31 | sub import { | ||||||
32 | 11 | 11 | 187 | my $caller = scalar( caller ); | |||
33 | 11 | 6000 | $caller->add_callback( 'load_tmpl' => \&_pass_in_messages ); | ||||
34 | 11 | 2849 | goto &Exporter::import; | ||||
35 | } | ||||||
36 | |||||||
37 | $VERSION = '0.34'; | ||||||
38 | |||||||
39 | =head1 SYNOPSIS | ||||||
40 | |||||||
41 | This plugin gives you a few support methods that you can call within your cgiapp | ||||||
42 | to pass along messages between requests for a given user. | ||||||
43 | |||||||
44 | use CGI::Application::Plugin::Session; | ||||||
45 | use CGI::Application::Plugin::MessageStack; | ||||||
46 | |||||||
47 | sub mainpage { | ||||||
48 | my $self = shift; | ||||||
49 | my $template = $self->load_tmpl( 'mainpage.TMPL', 'die_on_bad_params' => 0 ); | ||||||
50 | # ... | ||||||
51 | $template->output; | ||||||
52 | } | ||||||
53 | |||||||
54 | sub process { | ||||||
55 | my $self = shift; | ||||||
56 | $self->push_message( | ||||||
57 | -scope => 'mainpage', | ||||||
58 | -message => 'Your listing has been updated', | ||||||
59 | -classification => 'INFO', | ||||||
60 | ); | ||||||
61 | $self->forward( 'mainpage' ); | ||||||
62 | } | ||||||
63 | |||||||
64 | sub cgiapp_init { | ||||||
65 | # setup your session object as usual... | ||||||
66 | } | ||||||
67 | |||||||
68 | Meanwhile, in your (HTML::Template) template code: | ||||||
69 | |||||||
70 | ... | ||||||
71 | |||||||
79 | ... | ||||||
80 | Howdy! |
||||||
81 | |||||||
82 | "> |
||||||
83 | |||||||
84 | |||||||
85 | |||||||
86 | ... | ||||||
87 | |||||||
88 | It's a good idea to turn off 'die_on_bad_params' in HTML::Template - in case this plugin tries to put in the parameters and they're not available in your template. | ||||||
89 | |||||||
90 | Here's a quick TT example: | ||||||
91 | |||||||
92 | |||||||
100 | ... | ||||||
101 | Howdy! |
||||||
102 | [% FOREACH CAP_Messages %] | ||||||
103 | [% message %] |
||||||
104 | [% END %] | ||||||
105 | ... | ||||||
106 | |||||||
107 | If you use TT, I recommend using CAP-TT and a more recent version (0.09), which supports cgiapp's load_tmpl hook and then this plugin will automatically supply TT with the relevant messages. Your runmode could be this simple: | ||||||
108 | |||||||
109 | sub start { | ||||||
110 | my $self = shift; | ||||||
111 | my $session = $self->session; | ||||||
112 | return $self->tt_process( 'output.tt' ); | ||||||
113 | } | ||||||
114 | |||||||
115 | I don't have the experience to weigh in on how you'd do this with other templates (HTDot, Petal), but basically, this plugin will put in a loop parameter called 'CAP_Messages'. Within each element of that loop, you'll have two tags, 'classification' and 'message'. | ||||||
116 | |||||||
117 | NOTE: I have broken backwards compatibility with this release (0.30) and the loop parameter's default name is now 'CAP_Messages'. If you used the old __CAP_Messages or want to use another name, feel free to use the capms_config to override the C<-loop_param_name>. | ||||||
118 | |||||||
119 | =head1 DESCRIPTION | ||||||
120 | |||||||
121 | This plugin by default needs a session object to tuck away the message(s). It's recommended that you use this in conjunction with CGI::Application::Plugin::Session. You can opt to not have the messages persist and thereby, not use CAP-Session by using the C<-dont_use_session> option in the C |
||||||
122 | |||||||
123 | This plugin hooks into cgiapp's load_tmpl method and if you've pushed any messages in the stack, will automatically add the message parameters. | ||||||
124 | |||||||
125 | In the functions, there are scope & classification keys and when they're used for either display or your API purposes (clearing, pop'ing, etc), the classification is an exclusive specification. Meaning, if you ask for messages with the 'ERROR' classification, it will only deal with messages that you've pushed in with the 'ERROR' classification. Any messages that have no classification aren't included. | ||||||
126 | |||||||
127 | The scope key is not exclusive, meaning that if you ask for messages with a 'mainpage' scope, it will deal with messages that you've pushed with that scope B |
||||||
128 | |||||||
129 | If you use both scope & classification, it blends both of those rules, first getting all matching messages with the same classification and then filtering out messages that are scoped and don't match the scope you're looking for. | ||||||
130 | |||||||
131 | This logic may change as I get feedback from more saavy developers. What we may end up doing is have a plugin configuration where you can dictate the logic that's used. | ||||||
132 | |||||||
133 | =head1 FUNCTIONS | ||||||
134 | |||||||
135 | =head2 push_message | ||||||
136 | |||||||
137 | $self->push_message( | ||||||
138 | -scope => 'mainpage', | ||||||
139 | -message => 'Your listing has been updated', | ||||||
140 | -classification => 'INFO', | ||||||
141 | ); | ||||||
142 | |||||||
143 | You provide a hash to the push_message() method with three possible keys: | ||||||
144 | |||||||
145 | =over | ||||||
146 | |||||||
147 | =item * message - a text message. You can put HTML in there - just make sure you don't use the ESCAPE=HTML in your HTML::Template code | ||||||
148 | |||||||
149 | =item * scope - which runmode(s) can this message appear? If you want to specify just one, use a scalar assignment. Otherwise, use an array reference with the list of runmodes. | ||||||
150 | |||||||
151 | =item * classification - a simple scalar name representing the classification of your message (i.e. 'ERROR', 'WARNING' ... ). This is very useful for CSS styles (see template example above). | ||||||
152 | |||||||
153 | =back | ||||||
154 | |||||||
155 | The scope & classification keys are optional. If you don't provide a scope, it will assume a global presence. | ||||||
156 | |||||||
157 | =cut | ||||||
158 | |||||||
159 | sub push_message { | ||||||
160 | 36 | 36 | 1 | 358630 | my $self = shift; | ||
161 | 36 | 85 | my $session = _check_for_session( $self ); | ||||
162 | 36 | 135 | my %message_hash = @_; | ||||
163 | 36 | 100 | 114 | if ( my $message_array = $session->param( '__CAP_MessageStack_Stack' ) ) { | |||
164 | 30 | 313 | push @$message_array, \%message_hash; | ||||
165 | 30 | 80 | $session->param( '__CAP_MessageStack_Stack' => $message_array ); | ||||
166 | } else { | ||||||
167 | 6 | 129 | $session->param( '__CAP_MessageStack_Stack' => [ \%message_hash ] ); | ||||
168 | } | ||||||
169 | } | ||||||
170 | |||||||
171 | =head2 messages | ||||||
172 | |||||||
173 | my @messages = $self->messages(); | ||||||
174 | my @messages = $self->messages( -scope => 'mainpage' ); | ||||||
175 | my @messages = $self->messages( -scope => 'mainpage', -classification => 'ERROR' ); | ||||||
176 | my @messages = $self->messages( -classification => 'ERROR' ); | ||||||
177 | |||||||
178 | If you want to take a gander at the message stack data structure, you can use this method. | ||||||
179 | |||||||
180 | Optionally, you can use a hash parameters to get a slice of the messages, using the same keys as specified in the push_message() method. | ||||||
181 | |||||||
182 | It will return an array reference of the matching messages or 'undef', if there's either no messages in the stack or no messages that match your specification(s). | ||||||
183 | |||||||
184 | =cut | ||||||
185 | |||||||
186 | sub messages { | ||||||
187 | 0 | 0 | 1 | 0 | my $self = shift; | ||
188 | 0 | 0 | my $session = _check_for_session( $self ); | ||||
189 | 0 | 0 | my %limiting_params = @_; | ||||
190 | 0 | 0 | 0 | my $message_array = $session->param( '__CAP_MessageStack_Stack' ) || []; | |||
191 | 0 | 0 | 0 | 0 | if ( $limiting_params{'-scope'} || $limiting_params{'-classification'} ) { | ||
192 | 0 | 0 | $message_array = _filter_messages( $message_array, \%limiting_params ) | ||||
193 | } else { | ||||||
194 | # if the dev config'd different message or classification names, i need to do | ||||||
195 | # 'em by hand here ... _filter_messages() would do that, but only if they | ||||||
196 | # wanted a slice. This is if they want everything. | ||||||
197 | 0 | 0 | 0 | if ( my $class_key = $config{'-classification_param_name'} ) { | |||
198 | map { | ||||||
199 | 0 | 0 | 0 | if ( $_->{'-classification'} ) { | |||
0 | 0 | ||||||
200 | 0 | 0 | $_->{$class_key} = $_->{'-classification'}; | ||||
201 | 0 | 0 | delete $_->{'-classification'}; | ||||
202 | } | ||||||
203 | } @$message_array; | ||||||
204 | } | ||||||
205 | 0 | 0 | 0 | if ( my $message_key = $config{'-message_param_name'} ) { | |||
206 | map { | ||||||
207 | 0 | 0 | 0 | if ( $_->{'-message'} ) { | |||
0 | 0 | ||||||
208 | 0 | 0 | $_->{$message_key} = $_->{'-message'}; | ||||
209 | 0 | 0 | delete $_->{'-message'}; | ||||
210 | } | ||||||
211 | } @$message_array; | ||||||
212 | } | ||||||
213 | } | ||||||
214 | |||||||
215 | 0 | 0 | return $message_array; | ||||
216 | } | ||||||
217 | |||||||
218 | =head2 pop_message | ||||||
219 | |||||||
220 | my $message = $self->pop_message(); | ||||||
221 | my $message = $self->pop_message( -scope => 'mainpage' ); | ||||||
222 | my $message = $self->pop_message( -scope => 'mainpage', -classification => 'WARNING' ); | ||||||
223 | my $message = $self->pop_message( -classification => 'ERROR' ); | ||||||
224 | |||||||
225 | Pops off the last message from the stack and returns it. Note that this just returns the -message part. | ||||||
226 | |||||||
227 | You can pop off an exact message, given a hash parameters, using the same keys as specified in the push_message() method. | ||||||
228 | |||||||
229 | Otherwise, it will pop off the message, given the current runmode and the last message added. | ||||||
230 | |||||||
231 | =cut | ||||||
232 | |||||||
233 | sub pop_message { | ||||||
234 | 0 | 0 | 1 | 0 | my $self = shift; | ||
235 | 0 | 0 | my $session = _check_for_session( $self ); | ||||
236 | 0 | 0 | my %limiting_params = @_; | ||||
237 | 0 | 0 | my $message; | ||||
238 | 0 | 0 | my $message_array = $session->param( '__CAP_MessageStack_Stack' ); | ||||
239 | |||||||
240 | 0 | 0 | 0 | if ( $config{'-dont_use_session'} ) { | |||
241 | 0 | 0 | $session->param( '__CAP_MessageStack_Stack' => undef ); | ||||
242 | } else { | ||||||
243 | 0 | 0 | $session->clear( [ '__CAP_MessageStack_Stack' ] ); | ||||
244 | } | ||||||
245 | |||||||
246 | 0 | 0 | 0 | 0 | if ( $limiting_params{'-scope'} || $limiting_params{'-classification'} ) { | ||
247 | 0 | 0 | my $index = scalar( @$message_array ) - 1; | ||||
248 | 0 | 0 | foreach my $message_hashref ( reverse @$message_array ) { | ||||
249 | # now we're looking for the first matching element ... if/when we find it, | ||||||
250 | # set the $message and splice out the element from the $message_array | ||||||
251 | 0 | 0 | my $match_found = 0; | ||||
252 | |||||||
253 | 0 | 0 | 0 | 0 | if ( $limiting_params{'-scope'} && $limiting_params{'-classification'} ) { | ||
0 | |||||||
0 | |||||||
254 | 0 | 0 | 0 | 0 | if ( ( ! $message_hashref->{'-scope'} || ( ( ref( $message_hashref->{'-scope'} ) && grep { $_ eq $limiting_params{'-scope'} } @{$message_hashref->{'-scope'}} ) || ( ! ref ( $message_hashref->{'-scope'} ) && $message_hashref->{'-scope'} eq $limiting_params{'-scope'} ) ) ) && ( $message_hashref->{'-classification'} && $message_hashref->{'-classification'} eq $limiting_params{'-classification'} ) ) { | ||
0 | |||||||
0 | |||||||
255 | 0 | 0 | $match_found = 1; | ||||
256 | 0 | 0 | $message = $message_hashref->{'-message'}; | ||||
257 | } | ||||||
258 | } elsif ( $limiting_params{'-scope'} ) { | ||||||
259 | 0 | 0 | 0 | if ( ! $message_hashref->{'-scope'} ) { | |||
260 | 0 | 0 | $match_found = 1; | ||||
261 | 0 | 0 | $message = $message_hashref->{'-message'}; | ||||
262 | } else { | ||||||
263 | 0 | 0 | 0 | if ( ref( $message_hashref->{'-scope'} ) ) { | |||
264 | 0 | 0 | 0 | if ( grep { $_ eq $limiting_params{'-scope'} } @{$message_hashref->{'-scope'}} ) { | |||
0 | 0 | ||||||
0 | 0 | ||||||
265 | 0 | 0 | $match_found = 1; | ||||
266 | 0 | 0 | $message = $message_hashref->{'-message'}; | ||||
267 | } | ||||||
268 | } else { | ||||||
269 | 0 | 0 | 0 | if ( $message_hashref->{'-scope'} eq $limiting_params{'-scope'} ) { | |||
270 | 0 | 0 | $match_found = 1; | ||||
271 | 0 | 0 | $message = $message_hashref->{'-message'}; | ||||
272 | } | ||||||
273 | } | ||||||
274 | } | ||||||
275 | } elsif ( $limiting_params{'-classification'} ) { | ||||||
276 | 0 | 0 | 0 | 0 | if ( $message_hashref->{'-classification'} && $message_hashref->{'-classification'} eq $limiting_params{'-classification'} ) { | ||
277 | 0 | 0 | $match_found = 1; | ||||
278 | 0 | 0 | $message = $message_hashref->{'-message'}; | ||||
279 | } | ||||||
280 | } | ||||||
281 | |||||||
282 | 0 | 0 | 0 | if ( $match_found ) { | |||
283 | 0 | 0 | splice( @$message_array, $index, 1 ); | ||||
284 | 0 | 0 | last; | ||||
285 | } | ||||||
286 | |||||||
287 | 0 | 0 | $index--; | ||||
288 | } | ||||||
289 | } else { | ||||||
290 | 0 | 0 | my $message_hashref = pop @$message_array; | ||||
291 | 0 | 0 | $message = $message_hashref->{'-message'}; | ||||
292 | } | ||||||
293 | |||||||
294 | 0 | 0 | $session->param( '__CAP_MessageStack_Stack' => $message_array ); | ||||
295 | |||||||
296 | 0 | 0 | $message; | ||||
297 | } | ||||||
298 | |||||||
299 | =head2 clear_messages | ||||||
300 | |||||||
301 | $self->clear_messages(); | ||||||
302 | $self->clear_messages( -scope => 'mainpage' ); | ||||||
303 | $self->clear_messages( -scope => 'mainpage', -classification => 'ERROR' ); | ||||||
304 | $self->clear_messages( -classification => 'ERROR' ); | ||||||
305 | |||||||
306 | Clears the message stack. | ||||||
307 | |||||||
308 | Optionally, you can clear particular slices of the message stack, given a hash parameters, using the same keys as specified in the push_message() method. | ||||||
309 | |||||||
310 | If you specify a scope, it will clear any messages that are either global or match that scope | ||||||
311 | |||||||
312 | If you specify a classification, it will clear any messages that have that classification (but not any messages that don't have any classification). | ||||||
313 | |||||||
314 | If you specify both, it will combine both that logic in an AND fashion. | ||||||
315 | |||||||
316 | =cut | ||||||
317 | |||||||
318 | sub clear_messages { | ||||||
319 | 0 | 0 | 1 | 0 | my $self = shift; | ||
320 | 0 | 0 | my $session = _check_for_session( $self ); | ||||
321 | 0 | 0 | my %limiting_params = @_; | ||||
322 | 0 | 0 | 0 | 0 | if ( $limiting_params{'-scope'} || $limiting_params{'-classification'} ) { | ||
323 | 0 | 0 | my $message_array = $session->param( '__CAP_MessageStack_Stack' ); | ||||
324 | # can't use filter, b/c we need to invert that result... | ||||||
325 | 0 | 0 | my $nonmatching_messages = []; | ||||
326 | 0 | 0 | 0 | 0 | if ( $limiting_params{'-classification'} && $limiting_params{'-scope'} ) { | ||
0 | |||||||
0 | |||||||
327 | 0 | 0 | foreach my $message_hashref ( @$message_array ) { | ||||
328 | 0 | 0 | 0 | 0 | next if ( $message_hashref->{'-classification'} && $message_hashref->{'-classification'} eq $limiting_params{'-classification'} ) && ( !$message_hashref->{'-scope'} || ( ( ref( $message_hashref->{'-scope'} ) && grep { $_ eq $limiting_params{'-scope'} } @{$message_hashref->{'-scope'}} ) || ( ! ref( $message_hashref->{'-scope'} ) && $message_hashref->{'-scope'} eq $limiting_params{'-scope'} ) ) ); | ||
0 | |||||||
0 | |||||||
329 | 0 | 0 | push @$nonmatching_messages, $message_hashref; | ||||
330 | } | ||||||
331 | } elsif ( $limiting_params{'-classification'} ) { | ||||||
332 | 0 | 0 | foreach my $message_hashref ( @$message_array ) { | ||||
333 | 0 | 0 | 0 | 0 | next if $message_hashref->{'-classification'} && $message_hashref->{'-classification'} eq $limiting_params{'-classification'}; | ||
334 | 0 | 0 | push @$nonmatching_messages, $message_hashref; | ||||
335 | } | ||||||
336 | } elsif ( $limiting_params{'-scope'} ) { | ||||||
337 | 0 | 0 | foreach my $message_hashref ( @$message_array ) { | ||||
338 | 0 | 0 | 0 | next if ! $message_hashref->{'-scope'}; # taking out global scopes | |||
339 | 0 | 0 | 0 | if ( ref( $message_hashref->{'-scope'} ) ) { | |||
340 | 0 | 0 | 0 | next if grep { $_ eq $limiting_params{'-scope'} } @{$message_hashref->{'-scope'}}; | |||
0 | 0 | ||||||
0 | 0 | ||||||
341 | } else { | ||||||
342 | 0 | 0 | 0 | next if $message_hashref->{'-scope'} eq $limiting_params{'-scope'}; # taking out matching scopes | |||
343 | } | ||||||
344 | 0 | 0 | push @$nonmatching_messages, $message_hashref; | ||||
345 | } | ||||||
346 | } | ||||||
347 | 0 | 0 | $session->param( '__CAP_MessageStack_Stack' => $nonmatching_messages ); | ||||
348 | } else { | ||||||
349 | 0 | 0 | 0 | if ( $config{'-dont_use_session'} ) { | |||
350 | 0 | 0 | $session->param( '__CAP_MessageStack_Stack' => undef ); | ||||
351 | } else { | ||||||
352 | 0 | 0 | $session->clear( [ '__CAP_MessageStack_Stack' ] ); | ||||
353 | } | ||||||
354 | } | ||||||
355 | } | ||||||
356 | |||||||
357 | =head2 capms_config | ||||||
358 | |||||||
359 | $self->capms_config( | ||||||
360 | -automatic_clearing => 1, | ||||||
361 | -dont_use_session => 1, | ||||||
362 | -loop_param_name => 'MyOwnLoopName', | ||||||
363 | -message_param_name => 'MyOwnMessageName', | ||||||
364 | -classification_param_name => 'MyOwnClassificationName', | ||||||
365 | ); | ||||||
366 | |||||||
367 | There is a configuration option that you, as the developer can specify: | ||||||
368 | |||||||
369 | =over | ||||||
370 | |||||||
371 | =item * -automatic_clearing: By default, this is turned off. If you override it with a true value, it will call clear_messages() automatically after the messages are automatically put into template. | ||||||
372 | |||||||
373 | =item * -dont_use_session: This will override this Plugin's dependence on CGI::Application::Plugin::Session and instead, temporarily store the message data such that it will be available to templates within the same web request, but no further. If you're running your cgiapp under a persistent state (mod_perl), we'll also make sure your messages are gone by the end of the request. | ||||||
374 | |||||||
375 | =item * -loop_param_name: This will override the default __CAP_Messages (or CAP_Messages for TT users) name for the loop of messages, which is only used for the C |
||||||
376 | |||||||
377 | |||||||
378 | ... | ||||||
379 | |||||||
380 | |||||||
381 | =item * -message_param_name: This will override the default '-message' in both the template code B |
||||||
382 | |||||||
383 | [ { 'MyOwnMessageName' => 'this is just a test' }, ... ] | ||||||
384 | |||||||
385 | instead of: | ||||||
386 | |||||||
387 | [ { '-message' => 'this is just a test' }, ... ] | ||||||
388 | |||||||
389 | Likewise, your templates will need to use your parameter name: | ||||||
390 | |||||||
391 | |||||||
392 | Here's the message: | ||||||
393 | |||||||
394 | |||||||
395 | =item * -classification_param_name: Just like the C<-message_param_name> parameter - this will override the default '-classification' key in both the template code B |
||||||
396 | |||||||
397 | [ { 'MyOwnClassificationName' => 'ERROR', 'MyOwnMessageName' => 'this is just a test' }, ... ] | ||||||
398 | |||||||
399 | instead of: | ||||||
400 | |||||||
401 | [ { '-classification' => 'ERROR', '-message' => 'this is just a test' }, ... ] | ||||||
402 | |||||||
403 | Likewise, your templates will need to use your parameter name: | ||||||
404 | |||||||
405 | |||||||
406 | "> |
||||||
407 | Here's the message: | ||||||
408 | |||||||
409 | |||||||
410 | |||||||
411 | =back | ||||||
412 | |||||||
413 | =cut | ||||||
414 | |||||||
415 | sub capms_config { | ||||||
416 | 4 | 4 | 1 | 48453 | my $self = shift; | ||
417 | 4 | 22 | %config = @_; | ||||
418 | } | ||||||
419 | |||||||
420 | sub _filter_messages { | ||||||
421 | 5 | 5 | 12 | my ( $messages, $limiting_params, $for_template ) = @_; | |||
422 | |||||||
423 | 5 | 11 | my $matching_messages = []; | ||||
424 | 5 | 50 | 42 | my $class_key = $config{'-classification_param_name'} || 'classification'; | |||
425 | 5 | 50 | 28 | my $message_key = $config{'-message_param_name'} || 'message'; | |||
426 | |||||||
427 | 5 | 50 | 33 | 51 | if ( $limiting_params->{'-classification'} && $limiting_params->{'-scope'} ) { | ||
50 | |||||||
50 | |||||||
428 | 0 | 0 | foreach my $message_hashref ( @$messages ) { | ||||
429 | 0 | 0 | 0 | 0 | next if !$message_hashref->{'-classification'} || $message_hashref->{'-classification'} ne $limiting_params->{'-classification'}; | ||
430 | 0 | 0 | 0 | if ( ref( $message_hashref->{'-scope'} ) ) { | |||
431 | 0 | 0 | 0 | next if ! grep { $_ eq $limiting_params->{'-scope'} } @{$message_hashref->{'-scope'}}; | |||
0 | 0 | ||||||
0 | 0 | ||||||
432 | } else { | ||||||
433 | 0 | 0 | 0 | 0 | next if $message_hashref->{'-scope'} && $message_hashref->{'-scope'} ne $limiting_params->{'-scope'}; | ||
434 | } | ||||||
435 | # i'm beginning to hate the dash now ... now i have to take 'em out | ||||||
436 | # so the template code doesn't need/use 'em... | ||||||
437 | 0 | 0 | 0 | if ( $for_template ) { | |||
438 | 0 | 0 | push @$matching_messages, { | ||||
439 | $class_key => $message_hashref->{'-classification'}, | ||||||
440 | $message_key => $message_hashref->{'-message'}, | ||||||
441 | }; | ||||||
442 | } else { | ||||||
443 | 0 | 0 | push @$matching_messages, $message_hashref; | ||||
444 | } | ||||||
445 | } | ||||||
446 | } elsif ( $limiting_params->{'-classification'} ) { | ||||||
447 | 0 | 0 | foreach my $message_hashref ( @$messages ) { | ||||
448 | 0 | 0 | 0 | 0 | next if !$message_hashref->{'-classification'} || $message_hashref->{'-classification'} ne $limiting_params->{'-classification'}; | ||
449 | 0 | 0 | 0 | if ( $for_template ) { | |||
450 | 0 | 0 | push @$matching_messages, { | ||||
451 | $class_key => $message_hashref->{'-classification'}, | ||||||
452 | $message_key => $message_hashref->{'-message'}, | ||||||
453 | }; | ||||||
454 | } else { | ||||||
455 | 0 | 0 | push @$matching_messages, $message_hashref; | ||||
456 | } | ||||||
457 | } | ||||||
458 | } elsif ( $limiting_params->{'-scope'} ) { | ||||||
459 | 5 | 15 | foreach my $message_hashref ( @$messages ) { | ||||
460 | 1 | 50 | 6 | if ( ref( $message_hashref->{'-scope'} ) ) { | |||
461 | 0 | 0 | 0 | next if ! grep { $_ eq $limiting_params->{'-scope'} } @{$message_hashref->{'-scope'}}; | |||
0 | 0 | ||||||
0 | 0 | ||||||
462 | } else { | ||||||
463 | 1 | 50 | 33 | 9 | next if $message_hashref->{'-scope'} && $message_hashref->{'-scope'} ne $limiting_params->{'-scope'}; | ||
464 | } | ||||||
465 | 1 | 50 | 3 | if ( $for_template ) { | |||
466 | 1 | 8 | push @$matching_messages, { | ||||
467 | $class_key => $message_hashref->{'-classification'}, | ||||||
468 | $message_key => $message_hashref->{'-message'}, | ||||||
469 | }; | ||||||
470 | } else { | ||||||
471 | 0 | 0 | push @$matching_messages, $message_hashref; | ||||
472 | } | ||||||
473 | } | ||||||
474 | } | ||||||
475 | |||||||
476 | 5 | 14 | return $matching_messages; | ||||
477 | } | ||||||
478 | |||||||
479 | sub _pass_in_messages { | ||||||
480 | 6 | 6 | 246895 | my ( $self, undef, $tmpl_params, undef ) = @_; | |||
481 | |||||||
482 | # get the proper messages and update $tmpl_params | ||||||
483 | 6 | 24 | my $session = _check_for_session( $self ); | ||||
484 | 5 | 42 | my $current_runmode = $self->get_current_runmode(); | ||||
485 | 5 | 43 | my $message_stack = $session->param( '__CAP_MessageStack_Stack' ); | ||||
486 | 5 | 97 | my $messages = _filter_messages( $message_stack, { -scope => $current_runmode }, 1 ); | ||||
487 | 5 | 50 | 36 | my $loop_name = $config{'-loop_param_name'} || 'CAP_Messages'; | |||
488 | |||||||
489 | 5 | 100 | 19 | $tmpl_params->{ $loop_name } = $messages if scalar( @$messages ); | |||
490 | 5 | 50 | 25 | $self->clear_messages( -scope => $current_runmode ) if ( $config{'-automatic_clearing'} ); | |||
491 | } | ||||||
492 | |||||||
493 | # This method will return an object, depending on if the developer wants to | ||||||
494 | # use a session (the default behavior) or just the cgiapp itself. | ||||||
495 | sub _check_for_session { | ||||||
496 | 42 | 42 | 61 | my $self = shift; | |||
497 | 42 | 60 | my $session_object = undef; | ||||
498 | |||||||
499 | 42 | 100 | 106 | if ( $config{'-dont_use_session'} ) { | |||
500 | 3 | 4 | $session_object = $self; | ||||
501 | } else { | ||||||
502 | # dynamic importing of CAP-Session | ||||||
503 | 39 | 53 | eval { | ||||
504 | 39 | 1282 | require CGI::Application::Plugin::Session; | ||||
505 | 39 | 7762 | CGI::Application::Plugin::Session->import(); | ||||
506 | 39 | 1763 | $session_object = $self->session; | ||||
507 | }; | ||||||
508 | 39 | 100 | 66 | 372 | if ( $@ || ! $session_object ) { | ||
509 | 1 | 6 | die "No session object! This module depends on CGI::Application::Plugin::Session! (or you need to use the -dont_use_session config parameter)" | ||||
510 | } | ||||||
511 | } | ||||||
512 | 41 | 83 | return $session_object; | ||||
513 | } | ||||||
514 | |||||||
515 | =head1 AUTHOR | ||||||
516 | |||||||
517 | Jason Purdy, C<< |
||||||
518 | |||||||
519 | =head1 SEE ALSO | ||||||
520 | |||||||
521 | L |
||||||
522 | |||||||
523 | =head1 BUGS | ||||||
524 | |||||||
525 | Please report any bugs or feature requests to | ||||||
526 | C |
||||||
527 | L |
||||||
528 | I will be notified, and then you'll automatically be notified of progress on | ||||||
529 | your bug as I make changes. | ||||||
530 | |||||||
531 | I suspect that this code could use some expert guidance. I hacked it together and I'd hate to think that it would be responsible for slowing templates down. Please feel free to submit patches, guiding comments, etc. | ||||||
532 | |||||||
533 | =head1 ACKNOWLEDGEMENTS | ||||||
534 | |||||||
535 | Thanks to the guys on the #cgiapp channel | ||||||
536 | |||||||
537 | =head1 COPYRIGHT & LICENSE | ||||||
538 | |||||||
539 | Copyright 2005 Jason Purdy, all rights reserved. | ||||||
540 | |||||||
541 | This program is free software; you can redistribute it and/or modify it | ||||||
542 | under the same terms as Perl itself. | ||||||
543 | |||||||
544 | =cut | ||||||
545 | |||||||
546 | 1; # End of CGI::Application::Plugin::MessageStack | ||||||
547 | |||||||
548 | __END__ |