From c2baf8c1c83cc6f62d84571504898690854a0aa5 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Mon, 10 Feb 2014 11:18:36 +0100 Subject: [PATCH] Bug 11904: New module Koha::Messages This patch provides a uniform way to send messages to user interface. It uses CGI::Session to store messages and allow to render them thanks to a Template::Toolkit plugin (Koha::Template::Plugin::Messages) and a template include file (messages.inc) This patch doesn't introduce the new template include file anywhere. It will require additional patches to make it work. To use it, simply call messages_set() in a Perl script or module, then put [% INCLUDE 'messages.inc' %] at the right place in the right template file. See POD documentation of Koha::Messages for more information. --- Koha/Messages.pm | 141 +++++++++++++++++++++ Koha/Template/Plugin/Messages.pm | 12 ++ .../intranet-tmpl/prog/en/includes/messages.inc | 25 ++++ .../opac-tmpl/bootstrap/en/includes/messages.inc | 30 +++++ 4 files changed, 208 insertions(+) create mode 100644 Koha/Messages.pm create mode 100644 Koha/Template/Plugin/Messages.pm create mode 100644 koha-tmpl/intranet-tmpl/prog/en/includes/messages.inc create mode 100644 koha-tmpl/opac-tmpl/bootstrap/en/includes/messages.inc diff --git a/Koha/Messages.pm b/Koha/Messages.pm new file mode 100644 index 0000000..8288ae3 --- /dev/null +++ b/Koha/Messages.pm @@ -0,0 +1,141 @@ +package Koha::Messages; + +# Copyright 2014 BibLibre +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use CGI; +use C4::Auth qw( get_session ); + +use base qw( Exporter ); +our @EXPORT = qw( messages_set messages_get ); + +=head1 NAME + +Koha::Messages - Messages in user's session + +=head1 SYNOPSIS + + use Koha::Messages; + + messages_set("Operation succeeded"); + messages_set("Something odd happened", "warning"); + messages_set("Operation failed", "error"); + + my $messages = messages_get(); + +=head1 DESCRIPTION + +This module provides a uniform way to send messages to user interface. + +=head1 FUNCTIONS + +=cut + +sub session { + my $cgi = new CGI; + my $cgisessid = $cgi->cookie('CGISESSID') || ''; + return get_session($cgisessid); +} + +=head2 messages_set + + messages_set($message); + messages_set($message, $type); + +This function store a message into user session with a given type. + +=head3 Parameters + +=over 2 + +=item * $message: The message string to store + +=item * $type: The type of message. Can be one of 'ok', 'warning', or 'error'. +If not given, defaults to 'ok'. + +=back + +=cut + +sub messages_set { + my ($message, $type) = @_; + + return unless $message; + + $type //= 'ok'; + + my $session = session; + my $messages = $session->param('messages') // {}; + $messages->{$type} //= []; + + push @{ $messages->{$type} }, $message; + + $session->param('messages', $messages); + + # Save session + $session->flush; +} + +=head2 messages_get + + $messages = messages_get(); + +This function retrieves all messages in a hashref and remove them from user +session. + +=head3 Return value + +A hashref where keys are the type of messages and values are arrayrefs of +messages. + +Example: + + $messages = { + 'ok' => [ + "Everything is ok", + "Operation succeeded" + ], + 'warning' => [ + "Something odd happended" + ], + }; + +=cut + +sub messages_get { + my $session = session; + + my $messages = $session->param('messages') // {}; + + # Empty messages + $session->param('messages', {}); + + # Save session + $session->flush; + + return $messages; +} + +=head1 SEE ALSO + +Koha::Template::Plugin::Messages + +=cut + +1; diff --git a/Koha/Template/Plugin/Messages.pm b/Koha/Template/Plugin/Messages.pm new file mode 100644 index 0000000..6b2d437 --- /dev/null +++ b/Koha/Template/Plugin/Messages.pm @@ -0,0 +1,12 @@ +package Koha::Template::Plugin::Messages; + +use Modern::Perl; + +use base qw( Template::Plugin ); +use Koha::Messages; + +sub Get { + return messages_get(); +} + +1; diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/messages.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/messages.inc new file mode 100644 index 0000000..6ea9d9c --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/messages.inc @@ -0,0 +1,25 @@ +[% USE Messages %] +[% contents = Messages.Get() %] + +[% FOREACH type IN contents.keys %] + [% messages = contents.$type %] + + [% IF messages.size > 0 %] + [% class = 'message' %] + [% IF type == 'error' || type == 'warning' %] + [% class = 'alert' %] + [% END %] + +
+ [% IF messages.size > 1 %] +
    + [% FOREACH message IN contents.$type %] +
  • [% message %]
  • + [% END %] +
+ [% ELSE %] + [% messages.0 %] + [% END %] +
+ [% END %] +[% END %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/includes/messages.inc b/koha-tmpl/opac-tmpl/bootstrap/en/includes/messages.inc new file mode 100644 index 0000000..d18fb54 --- /dev/null +++ b/koha-tmpl/opac-tmpl/bootstrap/en/includes/messages.inc @@ -0,0 +1,30 @@ +[% USE Messages %] +[% contents = Messages.Get() %] + +[% FOREACH type IN contents.keys %] + [% messages = contents.$type %] + + [% IF messages.size > 0 %] + [% class = 'alert-info' %] + [% SWITCH type %] + [% CASE 'ok' %] + [% class = 'alert-success' %] + [% CASE 'warning' %] + [% class = 'alert-warning' %] + [% CASE 'error' %] + [% class = 'alert-danger' %] + [% END %] + +
+ [% IF messages.size > 1 %] +
    + [% FOREACH message IN contents.$type %] +
  • [% message %]
  • + [% END %] +
+ [% ELSE %] + [% messages.0 %] + [% END %] +
+ [% END %] +[% END %] -- 1.9.1