kivitendo/SL/Helper/Flash.pm @ 7b1da9c3
39294de3 | Moritz Bunkus | package SL::Helper::Flash;
|
||
use strict;
|
||||
require Exporter;
|
||||
4065042c | Moritz Bunkus | our @ISA = qw(Exporter);
|
||
our @EXPORT = qw(flash flash_later);
|
||||
fa125787 | Sven Schöling | our @EXPORT_OK = qw(render_flash delay_flash);
|
||
4065042c | Moritz Bunkus | |||
aa7f51db | Sven Schöling | my %valid_categories = (
|
||
map({$_ => 'info'} qw(information message)),
|
||||
6a12a968 | Niclas Zimmermann | map({$_ => $_} qw(info error warning ok)),
|
||
aa7f51db | Sven Schöling | );
|
||
4065042c | Moritz Bunkus | #
|
||
# public functions
|
||||
#
|
||||
39294de3 | Moritz Bunkus | |||
sub flash {
|
||||
4065042c | Moritz Bunkus | $::form->{FLASH} = _store_flash($::form->{FLASH}, @_);
|
||
}
|
||||
39294de3 | Moritz Bunkus | |||
4065042c | Moritz Bunkus | sub flash_later {
|
||
bc9d2f36 | Moritz Bunkus | $::auth->set_session_value({ key => "FLASH", value => _store_flash($::auth->get_session_value('FLASH'), @_), auto_restore => 1 });
|
||
39294de3 | Moritz Bunkus | }
|
||
fa125787 | Sven Schöling | sub delay_flash {
|
||
my $store = $::form->{FLASH} || { };
|
||||
flash_later($_ => @{ $store->{$_} || [] }) for keys %$store;
|
||||
}
|
||||
39294de3 | Moritz Bunkus | sub render_flash {
|
||
return $::form->parse_html_template('common/flash');
|
||||
}
|
||||
4065042c | Moritz Bunkus | #
|
||
# private functions
|
||||
#
|
||||
sub _store_flash {
|
||||
my $store = shift || { };
|
||||
aa7f51db | Sven Schöling | my $category = _check_category(+shift);
|
||
4065042c | Moritz Bunkus | |||
$store->{ $category } ||= [ ];
|
||||
push @{ $store->{ $category } }, @_;
|
||||
return $store;
|
||||
}
|
||||
aa7f51db | Sven Schöling | sub _check_category {
|
||
my ($c) = @_;
|
||||
return $valid_categories{$c}
|
||||
45c42be0 | Sven Schöling | || do {
|
||
require Carp;
|
||||
3a45f0c9 | Sven Schöling | Carp->import;
|
||
45c42be0 | Sven Schöling | croak("invalid category '$c' for flash");
|
||
};
|
||||
aa7f51db | Sven Schöling | }
|
||
39294de3 | Moritz Bunkus | 1;
|
||
a7c70594 | Moritz Bunkus | |||
__END__
|
||||
=head1 NAME
|
||||
ea65e003 | Moritz Bunkus | SL::Helper::Flash - helper functions for storing messages to be
|
||
a7c70594 | Moritz Bunkus | displayed to the user
|
||
=head1 SYNOPSIS
|
||||
The flash is a store for messages that should be displayed to the
|
||||
user. Each message has a category which is usually C<information>,
|
||||
C<warning> or C<error>. The messages in each category are grouped and
|
||||
displayed in colors appropriate for their severity (e.g. errors in
|
||||
red).
|
||||
Messages are rendered either by calling the function C<render_flash>
|
||||
or by including the flash sub-template from a template with the
|
||||
following code:
|
||||
[%- INCLUDE 'common/flash.html' %]
|
||||
4065042c | Moritz Bunkus | =head1 EXPORTS
|
||
The functions L</flash> and L</flash_later> are always exported.
|
||||
The function L</render_flash> is only exported upon request.
|
||||
a7c70594 | Moritz Bunkus | =head1 FUNCTIONS
|
||
=over 4
|
||||
4065042c | Moritz Bunkus | =item C<flash $category, @messages>
|
||
a7c70594 | Moritz Bunkus | |||
4065042c | Moritz Bunkus | Stores messages for the given category. The category can be either
|
||
a7c70594 | Moritz Bunkus | C<information>, C<warning> or C<error>. C<info> can also be used as an
|
||
alias for C<information>.
|
||||
4065042c | Moritz Bunkus | =item C<flash_later $category, @messages>
|
||
Stores messages for the given category for the next request. The
|
||||
category can be either C<information>, C<warning> or C<error>. C<info>
|
||||
can also be used as an alias for C<information>.
|
||||
The messages are stored in the user's session and restored upon the
|
||||
next request. Can be used for transmitting information over HTTP
|
||||
redirects.
|
||||
a7c70594 | Moritz Bunkus | =item C<render_flash>
|
||
Outputs the flash message by parsing the C<common/flash.html> template
|
||||
file.
|
||||
4065042c | Moritz Bunkus | This function is not exported by default.
|
||
fa125787 | Sven Schöling | =item C<delay_flash>
|
||
Delays flash, as if all flash messages in this request would have been
|
||||
C<flash_later>
|
||||
Not exported by default.
|
||||
a7c70594 | Moritz Bunkus | =back
|
||
=head1 AUTHOR
|
||||
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
|
||||
=cut
|