Bug 7309 - Add NORMARCslim2intranetDetail.xsl for detail view in intranet
[koha.git] / C4 / Form / MessagingPreferences.pm
blob85ae9e8cebe28783b01385c9585af91aa61eb265
1 package C4::Form::MessagingPreferences;
3 # Copyright 2008-2009 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use strict;
21 use warnings;
23 use CGI;
24 use C4::Context;
25 use C4::Members::Messaging;
26 use C4::Debug;
28 =head1 NAME
30 C4::Form::MessagingPreferences - manage messaging prefernces form
32 =head1 SYNOPSIS
34 In script:
36 use C4::Form::MessagingPreferences;
37 C4::Form::MessagingPreferences::set_form_value({ borrowernumber => 51 }, $template);
38 C4::Form::MessagingPreferences::handle_form_action($input, { categorycode => 'CPL' }, $template);
40 In HTML template:
42 <!-- TMPL_INCLUDE NAME="messaging-preference-form.inc" -->
44 =head1 DESCRIPTION
46 This module manages input and output for the messaging preferences form
47 that is used in the staff patron editor, the staff patron category editor,
48 and the OPAC patron messaging prefereneces form. It in its current form,
49 it essentially serves only to eliminate copy-and-paste code, but suggests
50 at least one approach for reconciling functionality that does mostly
51 the same thing in staff and OPAC.
53 =head1 FUNCTIONS
55 =head2 handle_form_action
57 C4::Form::MessagingPreferences::handle_form_action($input, { categorycode => 'CPL' }, $template, $insert);
59 Processes CGI parameters and updates the target patron or patron category's
60 preferences.
62 C<$input> is the CGI query object.
64 C<$target_params> is a hashref containing either a C<categorycode> key or a C<borrowernumber> key
65 identifying the patron or patron category whose messaging preferences are to be updated.
67 C<$template> is the Template::Toolkit object for the response; this routine
68 adds a settings_updated template variable.
70 =cut
72 sub handle_form_action {
73 my ($query, $target_params, $template, $insert, $categorycode) = @_;
74 my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
75 # TODO: If a "NONE" box and another are checked somehow (javascript failed), we should pay attention to the "NONE" box
76 my $prefs_set = 0;
77 OPTION: foreach my $option ( @$messaging_options ) {
78 my $updater = { %{ $target_params },
79 message_attribute_id => $option->{'message_attribute_id'} };
81 # find the desired transports
82 @{$updater->{'message_transport_types'}} = $query->param( $option->{'message_attribute_id'} );
83 next OPTION unless $updater->{'message_transport_types'};
85 if ( $option->{'has_digest'} ) {
86 if ( List::Util::first { $_ == $option->{'message_attribute_id'} } $query->param( 'digest' ) ) {
87 $updater->{'wants_digest'} = 1;
91 if ( $option->{'takes_days'} ) {
92 if ( defined $query->param( $option->{'message_attribute_id'} . '-DAYS' ) ) {
93 $updater->{'days_in_advance'} = $query->param( $option->{'message_attribute_id'} . '-DAYS' );
97 C4::Members::Messaging::SetMessagingPreference( $updater );
99 if ($query->param( $option->{'message_attribute_id'})){
100 $prefs_set = 1;
103 if (! $prefs_set && $insert){
104 # this is new borrower, and we have no preferences set, use the defaults
105 $target_params->{categorycode} = $categorycode;
106 C4::Members::Messaging::SetMessagingPreferencesFromDefaults( $target_params );
108 # show the success message
109 $template->param( settings_updated => 1 );
112 =head2 set_form_values
114 C4::Form::MessagingPreferences::set_form_value({ borrowernumber => 51 }, $template);
116 Retrieves the messaging preferences for the specified patron or patron category
117 and fills the corresponding template variables.
119 C<$target_params> is a hashref containing either a C<categorycode> key or a C<borrowernumber> key
120 identifying the patron or patron category.
122 C<$template> is the Template::Toolkit object for the response.
124 =cut
126 sub set_form_values {
127 my ($target_params, $template) = @_;
128 # walk through the options and update them with these borrower_preferences
129 my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
130 PREF: foreach my $option ( @$messaging_options ) {
131 my $pref = C4::Members::Messaging::GetMessagingPreferences( { %{ $target_params }, message_name => $option->{'message_name'} } );
132 $option->{ $option->{'message_name'} } = 1;
133 # make a hashref of the days, selecting one.
134 if ( $option->{'takes_days'} ) {
135 my $days_in_advance = $pref->{'days_in_advance'} ? $pref->{'days_in_advance'} : 0;
136 $option->{days_in_advance} = $days_in_advance;
137 @{$option->{'select_days'}} = map {; {
138 day => $_,
139 selected => $_ == $days_in_advance ? 'selected="selected"' :'' }
140 } ( 0..30 ); # FIXME: 30 is a magic number.
142 foreach my $transport ( @{$pref->{'transports'}} ) {
143 $option->{'transports_'.$transport} = 1;
145 $option->{'digest'} = 1 if $pref->{'wants_digest'};
147 $template->param(messaging_preferences => $messaging_options);
150 =head1 TODO
152 =over 4
154 =item Reduce coupling between processing CGI parameters and updating the messaging preferences
156 =item Handle when form input is invalid
158 =item Generalize into a system of form handler clases
160 =back
162 =head1 SEE ALSO
164 L<C4::Members::Messaging>, F<admin/categorie.pl>, F<opac/opac-messaging.pl>, F<members/messaging.pl>
166 =head1 AUTHOR
168 Koha Development Team <http://koha-community.org/>
170 Galen Charlton <galen.charlton@liblime.com> refactoring code by Andrew Moore.
172 =cut