View | Details | Raw Unified | Return to bug 11904
Collapse All | Expand All

(-)a/Koha/Messages.pm (+141 lines)
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;
(-)a/Koha/Template/Plugin/Messages.pm (+12 lines)
Line 0 Link Here
1
package Koha::Template::Plugin::Messages;
2
3
use Modern::Perl;
4
5
use base qw( Template::Plugin );
6
use Koha::Messages;
7
8
sub Get {
9
    return messages_get();
10
}
11
12
1;
(-)a/koha-tmpl/intranet-tmpl/prog/en/includes/messages.inc (+25 lines)
Line 0 Link Here
1
[% USE Messages %]
2
[% contents = Messages.Get() %]
3
4
[% FOREACH type IN contents.keys %]
5
  [% messages = contents.$type %]
6
7
  [% IF messages.size > 0 %]
8
    [% class = 'message' %]
9
    [% IF type == 'error' || type == 'warning' %]
10
      [% class = 'alert' %]
11
    [% END %]
12
13
    <div class="dialog [% class %]">
14
      [% IF messages.size > 1 %]
15
        <ul>
16
          [% FOREACH message IN contents.$type %]
17
            <li>[% message %]</li>
18
          [% END %]
19
        </ul>
20
      [% ELSE %]
21
        [% messages.0 %]
22
      [% END %]
23
    </div>
24
  [% END %]
25
[% END %]
(-)a/koha-tmpl/opac-tmpl/bootstrap/en/includes/messages.inc (-1 / +30 lines)
Line 0 Link Here
0
- 
1
[% USE Messages %]
2
[% contents = Messages.Get() %]
3
4
[% FOREACH type IN contents.keys %]
5
  [% messages = contents.$type %]
6
7
  [% IF messages.size > 0 %]
8
    [% class = 'alert-info' %]
9
    [% SWITCH type %]
10
      [% CASE 'ok' %]
11
        [% class = 'alert-success' %]
12
      [% CASE 'warning' %]
13
        [% class = 'alert-warning' %]
14
      [% CASE 'error' %]
15
        [% class = 'alert-danger' %]
16
    [% END %]
17
18
    <div class="alert [% class %]">
19
      [% IF messages.size > 1 %]
20
        <ul>
21
          [% FOREACH message IN contents.$type %]
22
            <li>[% message %]</li>
23
          [% END %]
24
        </ul>
25
      [% ELSE %]
26
        [% messages.0 %]
27
      [% END %]
28
    </div>
29
  [% END %]
30
[% END %]

Return to bug 11904