line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::Plugin::BootstrapAlert; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
13902
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
19
|
|
6
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
467
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Catalyst::Plugin::BootstrapAlert - Replacement for Catalyst::Plugin::StatusMessage inline with Bootstrap alert names (success, info, warning, and danger). |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 VERSION |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Version 0.50 |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=cut |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '0.50'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 SYNOPSIS |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
Replacement for Catalyst::Plugin::StatusMessage using Bootstrap alert names (success, info, warning, and danger), |
25
|
|
|
|
|
|
|
whilst also keeping C and C as aliases. |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Storing an array-ref of msgs, or even an array-ref of hash-refs is fine, in TT you'd want to use the C VMethod: |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
[% IF danger_alert.list.size %] |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
[% FOREACH each_danger_alert IN danger_alert.list %] |
32
|
|
|
|
|
|
|
[% each_danger_alert %] |
33
|
|
|
|
|
|
|
[% END %] |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
[% END %] |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Calling C on an actual list just returns the list, so essentially a no-op. |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
See http://www.template-toolkit.org/docs/manual/VMethods.html#section_list |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
In MyApp.pm: |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
use Catalyst qr/ |
44
|
|
|
|
|
|
|
BootstrapAlert |
45
|
|
|
|
|
|
|
/; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
In controller where you want to save a message for display on the next page: |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
$c->response->redirect( "/?mid=" . $c->set_success_alert("It worked!") ); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Or, to save an danger message: |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
$c->response->redirect( "/?mid=" . $c->set_error_msg("Error deleting widget") ); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Then, in the controller action that corresponds to the redirect above: |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub list :Path { |
58
|
|
|
|
|
|
|
my ($self, $c) = @_; |
59
|
|
|
|
|
|
|
... |
60
|
|
|
|
|
|
|
$c->load_bootstrap_alerts; |
61
|
|
|
|
|
|
|
... |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
This would mean simply changing C if using C. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
And, to display the output (here using L): |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
... |
69
|
|
|
|
|
|
|
[% success_alert %] |
70
|
|
|
|
|
|
|
[% error_msg %] |
71
|
|
|
|
|
|
|
... |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head1 METHODS |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head2 load_bootstrap_alerts |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
Load all alerts that match the token parameter on the URL (e.g., |
78
|
|
|
|
|
|
|
http://example.com/dashboard?mid=1234567890) into the stash |
79
|
|
|
|
|
|
|
for display by the viewer. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
In general, you will want to include this in an C or "base" (if |
82
|
|
|
|
|
|
|
using Chained dispatch) controller action. Then, if you have a |
83
|
|
|
|
|
|
|
"template wrapper page" that displays "C", "C", |
84
|
|
|
|
|
|
|
"C", and "C", you can automatically and safely send status messages to |
85
|
|
|
|
|
|
|
any related controller action. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=cut |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub load_bootstrap_alerts |
90
|
|
|
|
|
|
|
{ |
91
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
my $config = $self->_plugin_bootstrap_alert_config; |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
0
|
|
|
|
my $param = $self->request->params->{ $config->{ alert_param } } || return; |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
foreach my $type ( @{ $config->{ alert_types } } ) |
|
0
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
{ |
99
|
0
|
|
|
|
|
|
my $key = $type . '_alert_stash_key'; |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
my $value = $self->_get_bootstrap_alert( $type, $param ); |
102
|
|
|
|
|
|
|
|
103
|
0
|
0
|
|
|
|
|
if ( defined $value ) |
104
|
|
|
|
|
|
|
{ |
105
|
0
|
|
|
|
|
|
$self->stash( $config->{ $key } => $value ); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# the aliases for Catalyst::Plugin::StatusMessage |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
my $alias_key = $type . '_alert_stash_key_alias'; |
111
|
|
|
|
|
|
|
|
112
|
0
|
0
|
|
|
|
|
if ( my $alias = $config->{ $alias_key } ) |
113
|
|
|
|
|
|
|
{ |
114
|
0
|
0
|
|
|
|
|
if ( exists $self->stash->{ $config->{ $key } } ) |
115
|
|
|
|
|
|
|
{ |
116
|
0
|
|
|
|
|
|
$self->stash( $alias => $self->stash->{ $config->{ $key } } ); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head2 set_success_alert |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Sets the success alert text. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=cut |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
0
|
1
|
|
sub set_success_alert { return shift->_set_bootstrap_alert( 'success', shift ) } |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head2 set_info_alert |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Sets the info alert text. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=cut |
135
|
|
|
|
|
|
|
|
136
|
0
|
|
|
0
|
1
|
|
sub set_info_alert { return shift->_set_bootstrap_alert( 'info', shift ) } |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head2 set_warning_alert |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Sets the warning alert text. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=cut |
143
|
|
|
|
|
|
|
|
144
|
0
|
|
|
0
|
1
|
|
sub set_warning_alert { return shift->_set_bootstrap_alert( 'warning', shift ) } |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head2 set_danger_alert |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Sets the danger alert text. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=cut |
151
|
|
|
|
|
|
|
|
152
|
0
|
|
|
0
|
1
|
|
sub set_danger_alert { return shift->_set_bootstrap_alert( 'danger', shift ) } |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head2 set_status_msg |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Sets the success alert text - this is an alias for when you're switching out Catalyst::Plugin::StatusMessage. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=cut |
159
|
|
|
|
|
|
|
|
160
|
0
|
|
|
0
|
1
|
|
sub set_status_msg { return shift->set_success_alert( shift ) } |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head2 set_error_msg |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Sets the danger alert text - this is an alias for when you're switching out Catalyst::Plugin::StatusMessage. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=cut |
167
|
|
|
|
|
|
|
|
168
|
0
|
|
|
0
|
1
|
|
sub set_error_msg { return shift->set_danger_alert( shift ) } |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 CONFIGURABLE OPTIONS |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Here is a quick example showing how Catalyst::Plugin::BootstrapAlert |
173
|
|
|
|
|
|
|
can be configured: |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
# Configure Catalyst::Plugin::BootstrapAlert |
176
|
|
|
|
|
|
|
__PACKAGE__->config( |
177
|
|
|
|
|
|
|
'Plugin::BootstrapAlert' => { |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
session_key => 'my_status_msg', |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
alert_param => 'my_mid', |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
alert_types => [ qw( success info warning danger ) ], |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
success_alert_stash_key => 'success_alert', |
186
|
|
|
|
|
|
|
info_alert_stash_key => 'info_alert', |
187
|
|
|
|
|
|
|
warning_alert_stash_key => 'warning_alert', |
188
|
|
|
|
|
|
|
danger_alert_stash_key => 'danger_alert', |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
success_alert_stash_key_alias => 'status_msg', |
191
|
|
|
|
|
|
|
danger_alert_stash_key_alias => 'error_msg', |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
); |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head2 _plugin_bootstrap_alert_config |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Subref that handles default values and lets them be overriden from the MyApp |
198
|
|
|
|
|
|
|
configuration. |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=cut |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
sub _plugin_bootstrap_alert_config |
203
|
|
|
|
|
|
|
{ |
204
|
0
|
|
|
0
|
|
|
my $self = shift; |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
my %config = ( |
207
|
|
|
|
|
|
|
session_key => 'bootstrap_alerts', |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
alert_param => 'mid', |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
alert_types => [ qw( success info warning danger ) ], |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
success_alert_stash_key => 'success_alert', |
214
|
|
|
|
|
|
|
info_alert_stash_key => 'info_alert', |
215
|
|
|
|
|
|
|
warning_alert_stash_key => 'warning_alert', |
216
|
|
|
|
|
|
|
danger_alert_stash_key => 'danger_alert', |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
success_alert_stash_key_alias => 'status_msg', |
219
|
|
|
|
|
|
|
danger_alert_stash_key_alias => 'error_msg', |
220
|
|
|
|
|
|
|
|
221
|
0
|
0
|
|
|
|
|
%{ $self->config->{ "Plugin::BootstrapAlert" } || {} } |
|
0
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
); |
223
|
|
|
|
|
|
|
|
224
|
0
|
|
|
|
|
|
return \%config; |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head1 INTERNALS |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
Note: You normally shouldn't need any of the information in this section |
230
|
|
|
|
|
|
|
to use L. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head2 _set_bootstrap_alert |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
This is called by all of the public methods, passing the type of alert, and the alert message. |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=cut |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
sub _set_bootstrap_alert |
239
|
|
|
|
|
|
|
{ |
240
|
0
|
|
|
0
|
|
|
my $self = shift; |
241
|
|
|
|
|
|
|
|
242
|
0
|
|
|
|
|
|
my $type = shift; |
243
|
|
|
|
|
|
|
|
244
|
0
|
|
|
|
|
|
my $msg = shift; # could be an array-ref of multiple msgs |
245
|
|
|
|
|
|
|
|
246
|
0
|
|
|
|
|
|
my $config = $self->_plugin_bootstrap_alert_config; |
247
|
|
|
|
|
|
|
|
248
|
0
|
|
|
|
|
|
my $param = int( rand( 90_000_000 ) ) + 10_000_000; |
249
|
|
|
|
|
|
|
|
250
|
0
|
|
|
|
|
|
$self->session->{ $config->{ session_key } }->{ $type }->{ $param } = $msg; |
251
|
|
|
|
|
|
|
|
252
|
0
|
|
|
|
|
|
return $param; |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=head2 _get_bootstrap_alert |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
Fetch the requested message type from the user's session |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=cut |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
sub _get_bootstrap_alert |
262
|
|
|
|
|
|
|
{ |
263
|
0
|
|
|
0
|
|
|
my $self = shift; |
264
|
|
|
|
|
|
|
|
265
|
0
|
|
|
|
|
|
my $type = shift; |
266
|
|
|
|
|
|
|
|
267
|
0
|
|
|
|
|
|
my $param = shift; |
268
|
|
|
|
|
|
|
|
269
|
0
|
|
|
|
|
|
my $config = $self->_plugin_bootstrap_alert_config; |
270
|
|
|
|
|
|
|
|
271
|
0
|
0
|
|
|
|
|
if ( exists $self->session->{ $config->{ session_key } }->{ $type }->{ $param } ) |
272
|
|
|
|
|
|
|
{ |
273
|
0
|
|
|
|
|
|
return delete( $self->session->{ $config->{ session_key } }->{ $type }->{ $param } ); |
274
|
|
|
|
|
|
|
} |
275
|
|
|
|
|
|
|
|
276
|
0
|
|
|
|
|
|
return undef; |
277
|
|
|
|
|
|
|
} |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=head1 AUTHOR |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
Rob Brown, C<< >> |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
=head1 BUGS |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
287
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you will |
288
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
=head1 SUPPORT |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
perldoc Catalyst::Plugin::BootstrapAlert |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
You can also look for information at: |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
=over 4 |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker (report bugs here) |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
L |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
L |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
=item * CPAN Ratings |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
L |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
=item * Search CPAN |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
L |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
=back |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
Copyright 2017 Rob Brown. |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
324
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
325
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
326
|
|
|
|
|
|
|
|
327
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
=cut |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
1; |
332
|
|
|
|
|
|
|
|