Merge remote-tracking branch 'origin/new/bug_8092'
[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 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.
11 #
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.
15 #
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.
19
20 use strict;
21 use warnings;
22
23 use CGI;
24 use C4::Context;
25 use C4::Members::Messaging;
26 use C4::Debug;
27
28 =head1 NAME
29
30 C4::Form::MessagingPreferences - manage messaging prefernces form
31
32 =head1 SYNOPSIS
33
34 In script:
35
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);
39
40 In HTML template:
41
42     <!-- TMPL_INCLUDE NAME="messaging-preference-form.inc" -->
43
44 =head1 DESCRIPTION
45
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.
52
53 =head1 FUNCTIONS
54
55 =head2 handle_form_action
56
57     C4::Form::MessagingPreferences::handle_form_action($input, { categorycode => 'CPL' }, $template, $insert);
58
59 Processes CGI parameters and updates the target patron or patron category's
60 preferences.
61
62 C<$input> is the CGI query object.
63
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.
66
67 C<$template> is the Template::Toolkit object for the response; this routine
68 adds a settings_updated template variable.
69
70 =cut
71
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'} };
80         
81         # find the desired transports
82         @{$updater->{'message_transport_types'}} = $query->param( $option->{'message_attribute_id'} );
83         next OPTION unless $updater->{'message_transport_types'};
84
85         if ( $option->{'has_digest'} ) {
86             if ( List::Util::first { $_ == $option->{'message_attribute_id'} } $query->param( 'digest' ) ) {
87                 $updater->{'wants_digest'} = 1;
88             }
89         }
90
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' );
94             }
95         }
96
97         C4::Members::Messaging::SetMessagingPreference( $updater );
98
99         if ($query->param( $option->{'message_attribute_id'})){
100             $prefs_set = 1;
101         }
102     }
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 );
107     }
108     # show the success message
109     $template->param( settings_updated => 1 );
110 }
111
112 =head2 set_form_values
113
114     C4::Form::MessagingPreferences::set_form_value({ borrowernumber => 51 }, $template);
115
116 Retrieves the messaging preferences for the specified patron or patron category
117 and fills the corresponding template variables.
118
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.
121
122 C<$template> is the Template::Toolkit object for the response.
123
124 =cut
125
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.
141         }
142         foreach my $transport ( keys %{$pref->{'transports'}} ) {
143             $option->{'transports_'.$transport} = 1;
144         }
145         $option->{'digest'} = 1 if $pref->{'wants_digest'};
146     }
147     $template->param(messaging_preferences => $messaging_options);
148 }
149
150 =head1 TODO
151
152 =over 4
153
154 =item Reduce coupling between processing CGI parameters and updating the messaging preferences
155
156 =item Handle when form input is invalid
157
158 =item Generalize into a system of form handler clases
159
160 =back
161
162 =head1 SEE ALSO
163
164 L<C4::Members::Messaging>, F<admin/categorie.pl>, F<opac/opac-messaging.pl>, F<members/messaging.pl>
165
166 =head1 AUTHOR
167
168 Koha Development Team <http://koha-community.org/>
169
170 Galen Charlton <galen.charlton@liblime.com> refactoring code by Andrew Moore.
171
172 =cut
173
174 1;