kivitendo/SL/Helper/Flash.pm @ 4a663bf8
39294de3 | Moritz Bunkus | package SL::Helper::Flash;
|
||
use strict;
|
||||
require Exporter;
|
||||
4065042c | Moritz Bunkus | our @ISA = qw(Exporter);
|
||
our @EXPORT = qw(flash flash_later);
|
||||
4a663bf8 | Tamino Steinert | our @EXPORT_OK = qw(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 | }
|
||
4a663bf8 | Tamino Steinert | sub flash_contents {
|
||
return unless $::form;
|
||||
return unless $::form->{FLASH};
|
||||
return unless 'ARRAY' eq ref $::form->{FLASH};
|
||||
@{ $::form->{FLASH} }
|
||||
fa125787 | Sven Schöling | }
|
||
4a663bf8 | Tamino Steinert | sub delay_flash {
|
||
my $store = $::form->{FLASH} || [];
|
||||
flash_later(@{ $_ || [] }) for @$store;
|
||||
39294de3 | Moritz Bunkus | }
|
||
4065042c | Moritz Bunkus | #
|
||
# private functions
|
||||
#
|
||||
sub _store_flash {
|
||||
4a663bf8 | Tamino Steinert | my ($store, $type, $message, $details, $timestamp) = @_;
|
||
$store //= [ ];
|
||||
$timestamp //= time();
|
||||
my $category = _check_category($type);
|
||||
4065042c | Moritz Bunkus | |||
4a663bf8 | Tamino Steinert | push @{ $store }, [ $type, $message, $details, $timestamp ];
|
||
4065042c | Moritz Bunkus | |||
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
|
||||
4a663bf8 | Tamino Steinert | use SL::Helper::Flash qw(flash flash_later delay_flash);
|
||
# display in this request
|
||||
flash('info', 'Customer saved!');
|
||||
flash('error', 'Something went wrong', "details about what went wrong");
|
||||
flash('warning', 'this might not be a good idea');
|
||||
# display after a redirect
|
||||
flash_later('info', 'Customer saved!');
|
||||
flash_later('error', 'Something went wrong', "details about what went wrong");
|
||||
flash_later('warning', 'this might not be a good idea');
|
||||
# delay flash() calls to next request:
|
||||
delay_flash();
|
||||
=head1 DESCRIPTION
|
||||
a7c70594 | Moritz Bunkus | 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).
|
||||
4a663bf8 | Tamino Steinert | Messages are rendered by including the L<SL::Layout::Flash> sub layout.
|
||
a7c70594 | Moritz Bunkus | |||
4065042c | Moritz Bunkus | =head1 EXPORTS
|
||
The functions L</flash> and L</flash_later> are always exported.
|
||||
a7c70594 | Moritz Bunkus | =head1 FUNCTIONS
|
||
=over 4
|
||||
4a663bf8 | Tamino Steinert | =item C<flash $category, $message [, $details ]>
|
||
a7c70594 | Moritz Bunkus | |||
4a663bf8 | Tamino Steinert | Store a message with optional details for the given category. The category can
|
||
be either C<information>, C<warning> or C<error>. C<info> can also be used as
|
||||
an alias for C<information>.
|
||||
a7c70594 | Moritz Bunkus | |||
4a663bf8 | Tamino Steinert | =item C<flash_later $category, $message [, $details ]>
|
||
4065042c | Moritz Bunkus | |||
4a663bf8 | Tamino Steinert | Store a message with optional details 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>.
|
||||
4065042c | Moritz Bunkus | |||
4a663bf8 | Tamino Steinert | The message is stored in the user's session and restored upon the
|
||
4065042c | Moritz Bunkus | next request. Can be used for transmitting information over HTTP
|
||
redirects.
|
||||
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.
|
||||
4a663bf8 | Tamino Steinert | =item C<flash_contents>
|
||
The contents of the current flash accumulator.
|
||||
a7c70594 | Moritz Bunkus | =back
|
||
=head1 AUTHOR
|
||||
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
|
||||
=cut
|