Line 0
Link Here
|
|
|
1 |
package Koha::Messages; |
2 |
|
3 |
# Copyright 2014 BibLibre |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use CGI; |
23 |
use C4::Auth qw( get_session ); |
24 |
|
25 |
use base qw( Exporter ); |
26 |
our @EXPORT = qw( messages_set messages_get ); |
27 |
|
28 |
=head1 NAME |
29 |
|
30 |
Koha::Messages - Messages in user's session |
31 |
|
32 |
=head1 SYNOPSIS |
33 |
|
34 |
use Koha::Messages; |
35 |
|
36 |
messages_set("Operation succeeded"); |
37 |
messages_set("Something odd happened", "warning"); |
38 |
messages_set("Operation failed", "error"); |
39 |
|
40 |
my $messages = messages_get(); |
41 |
|
42 |
=head1 DESCRIPTION |
43 |
|
44 |
This module provides a uniform way to send messages to user interface. |
45 |
|
46 |
=head1 FUNCTIONS |
47 |
|
48 |
=cut |
49 |
|
50 |
sub session { |
51 |
my $cgi = new CGI; |
52 |
my $cgisessid = $cgi->cookie('CGISESSID') || ''; |
53 |
return get_session($cgisessid); |
54 |
} |
55 |
|
56 |
=head2 messages_set |
57 |
|
58 |
messages_set($message); |
59 |
messages_set($message, $type); |
60 |
|
61 |
This function store a message into user session with a given type. |
62 |
|
63 |
=head3 Parameters |
64 |
|
65 |
=over 2 |
66 |
|
67 |
=item * $message: The message string to store |
68 |
|
69 |
=item * $type: The type of message. Can be one of 'ok', 'warning', or 'error'. |
70 |
If not given, defaults to 'ok'. |
71 |
|
72 |
=back |
73 |
|
74 |
=cut |
75 |
|
76 |
sub messages_set { |
77 |
my ($message, $type) = @_; |
78 |
|
79 |
return unless $message; |
80 |
|
81 |
$type //= 'ok'; |
82 |
|
83 |
my $session = session; |
84 |
my $messages = $session->param('messages') // {}; |
85 |
$messages->{$type} //= []; |
86 |
|
87 |
push @{ $messages->{$type} }, $message; |
88 |
|
89 |
$session->param('messages', $messages); |
90 |
|
91 |
# Save session |
92 |
$session->flush; |
93 |
} |
94 |
|
95 |
=head2 messages_get |
96 |
|
97 |
$messages = messages_get(); |
98 |
|
99 |
This function retrieves all messages in a hashref and remove them from user |
100 |
session. |
101 |
|
102 |
=head3 Return value |
103 |
|
104 |
A hashref where keys are the type of messages and values are arrayrefs of |
105 |
messages. |
106 |
|
107 |
Example: |
108 |
|
109 |
$messages = { |
110 |
'ok' => [ |
111 |
"Everything is ok", |
112 |
"Operation succeeded" |
113 |
], |
114 |
'warning' => [ |
115 |
"Something odd happended" |
116 |
], |
117 |
}; |
118 |
|
119 |
=cut |
120 |
|
121 |
sub messages_get { |
122 |
my $session = session; |
123 |
|
124 |
my $messages = $session->param('messages') // {}; |
125 |
|
126 |
# Empty messages |
127 |
$session->param('messages', {}); |
128 |
|
129 |
# Save session |
130 |
$session->flush; |
131 |
|
132 |
return $messages; |
133 |
} |
134 |
|
135 |
=head1 SEE ALSO |
136 |
|
137 |
Koha::Template::Plugin::Messages |
138 |
|
139 |
=cut |
140 |
|
141 |
1; |