Bug 15067: Follow up to fix sorting
[koha.git] / C4 / Form / MessagingPreferences.pm
1 package C4::Form::MessagingPreferences;
2
3 # Copyright 2008-2009 LibLime
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 strict;
21 use warnings;
22
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Members::Messaging;
26
27 use constant MAX_DAYS_IN_ADVANCE => 30;
28
29 =head1 NAME
30
31 C4::Form::MessagingPreferences - manage messaging preferences form
32
33 =head1 SYNOPSIS
34
35 In script:
36
37     use C4::Form::MessagingPreferences;
38     C4::Form::MessagingPreferences::set_form_value({ borrowernumber => 51 }, $template);
39     C4::Form::MessagingPreferences::handle_form_action($input, { categorycode => 'CPL' }, $template);
40
41 In HTML template:
42
43     <!-- TMPL_INCLUDE NAME="messaging-preference-form.inc" -->
44
45 =head1 DESCRIPTION
46
47 This module manages input and output for the messaging preferences form
48 that is used in the staff patron editor, the staff patron category editor,
49 and the OPAC patron messaging prefereneces form.  It in its current form,
50 it essentially serves only to eliminate copy-and-paste code, but suggests
51 at least one approach for reconciling functionality that does mostly
52 the same thing in staff and OPAC.
53
54 =head1 FUNCTIONS
55
56 =head2 handle_form_action
57
58     C4::Form::MessagingPreferences::handle_form_action($input, { categorycode => 'CPL' }, $template, $insert);
59
60 Processes CGI parameters and updates the target patron or patron category's
61 preferences.
62
63 C<$input> is the CGI query object.
64
65 C<$target_params> is a hashref containing either a C<categorycode> key or a C<borrowernumber> key 
66 identifying the patron or patron category whose messaging preferences are to be updated.
67
68 C<$template> is the Template::Toolkit object for the response; this routine
69 adds a settings_updated template variable.
70
71 =cut
72
73 sub handle_form_action {
74     my ($query, $target_params, $template, $insert, $categorycode) = @_;
75     my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
76     # TODO: If a "NONE" box and another are checked somehow (javascript failed), we should pay attention to the "NONE" box
77     my $prefs_set = 0;
78     OPTION: foreach my $option ( @$messaging_options ) {
79         my $updater = { %{ $target_params }, 
80                         message_attribute_id    => $option->{'message_attribute_id'} };
81         
82         # find the desired transports
83         @{$updater->{'message_transport_types'}} = $query->multi_param( $option->{'message_attribute_id'} );
84         next OPTION unless $updater->{'message_transport_types'};
85
86         if ( $option->{'has_digest'} ) {
87             if ( List::Util::first { $_ == $option->{'message_attribute_id'} } $query->multi_param( 'digest' ) ) {
88                 $updater->{'wants_digest'} = 1;
89             }
90         }
91
92         if ( $option->{'takes_days'} ) {
93             if ( defined $query->param( $option->{'message_attribute_id'} . '-DAYS' ) ) {
94                 $updater->{'days_in_advance'} = $query->param( $option->{'message_attribute_id'} . '-DAYS' );
95             }
96         }
97
98         C4::Members::Messaging::SetMessagingPreference( $updater );
99
100         if ($query->param( $option->{'message_attribute_id'})){
101             $prefs_set = 1;
102         }
103     }
104     if (! $prefs_set && $insert){
105         # this is new borrower, and we have no preferences set, use the defaults
106         $target_params->{categorycode} = $categorycode;
107         C4::Members::Messaging::SetMessagingPreferencesFromDefaults( $target_params );
108     }
109     # show the success message
110     $template->param( settings_updated => 1 );
111 }
112
113 =head2 set_form_values
114
115     C4::Form::MessagingPreferences::set_form_value({ borrowernumber => 51 }, $template);
116
117 Retrieves the messaging preferences for the specified patron or patron category
118 and fills the corresponding template variables.
119
120 C<$target_params> is a hashref containing either a C<categorycode> key or a C<borrowernumber> key 
121 identifying the patron or patron category.
122
123 C<$template> is the Template::Toolkit object for the response.
124
125 =cut
126
127 sub set_form_values {
128     my ($target_params, $template) = @_;
129     # walk through the options and update them with these borrower_preferences
130     my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
131     PREF: foreach my $option ( @$messaging_options ) {
132         my $pref = C4::Members::Messaging::GetMessagingPreferences( { %{ $target_params }, message_name => $option->{'message_name'} } );
133         $option->{ $option->{'message_name'} } = 1;
134         # make a hashref of the days, selecting one.
135         if ( $option->{'takes_days'} ) {
136             my $days_in_advance = $pref->{'days_in_advance'} ? $pref->{'days_in_advance'} : 0;
137             $option->{days_in_advance} = $days_in_advance;
138             @{$option->{'select_days'}} = map { {
139                 day        => $_,
140                 selected   => $_ == $days_in_advance  }
141             } ( 0..MAX_DAYS_IN_ADVANCE );
142         }
143         foreach my $transport ( keys %{$pref->{'transports'}} ) {
144             $option->{'transports_'.$transport} = 1;
145         }
146         $option->{'digest'} = 1 if $pref->{'wants_digest'};
147     }
148     $template->param(messaging_preferences => $messaging_options);
149 }
150
151 =head1 TODO
152
153 =over 4
154
155 =item Reduce coupling between processing CGI parameters and updating the messaging preferences
156
157 =item Handle when form input is invalid
158
159 =item Generalize into a system of form handler clases
160
161 =back
162
163 =head1 SEE ALSO
164
165 L<C4::Members::Messaging>, F<admin/categories.pl>, F<opac/opac-messaging.pl>, F<members/messaging.pl>
166
167 =head1 AUTHOR
168
169 Koha Development Team <http://koha-community.org/>
170
171 Galen Charlton <galen.charlton@liblime.com> refactoring code by Andrew Moore.
172
173 =cut
174
175 1;