fix a common "developement" typo
[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);
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 HTML::Template::Pro 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) = @_;
74     my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
75
76     # TODO: If a "NONE" box and another are checked somehow (javascript failed), we should pay attention to the "NONE" box
77     
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->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->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
101     # show the success message
102     $template->param( settings_updated => 1 );
103 }
104
105 =head2 set_form_values
106
107     C4::Form::MessagingPreferences::set_form_value({ borrowernumber => 51 }, $template);
108
109 Retrieves the messaging preferences for the specified patron or patron category
110 and fills the corresponding template variables.
111
112 C<$target_params> is a hashref containing either a C<categorycode> key or a C<borrowernumber> key 
113 identifying the patron or patron category.
114
115 C<$template> is the HTML::Template::Pro object for the response.
116
117 =cut
118
119 sub set_form_values {
120     my ($target_params, $template) = @_;
121     # walk through the options and update them with these borrower_preferences
122     my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
123     PREF: foreach my $option ( @$messaging_options ) {
124         my $pref = C4::Members::Messaging::GetMessagingPreferences( { %{ $target_params },
125                                                                     message_name       => $option->{'message_name'} } );
126         # make a hashref of the days, selecting one.
127         if ( $option->{'takes_days'} ) {
128             my $days_in_advance = $pref->{'days_in_advance'} ? $pref->{'days_in_advance'} : 0;
129             $option->{days_in_advance} = $days_in_advance;
130             @{$option->{'select_days'}} = map {; {
131                 day        => $_,
132                 selected   => $_ == $days_in_advance ? 'selected="selected"' :'' } 
133             } ( 0..30 ); # FIXME: 30 is a magic number.
134         }
135         foreach my $transport ( @{$pref->{'transports'}} ) {
136             $option->{'transport-'.$transport} = 'checked="checked"';
137         }
138         $option->{'digest'} = 'checked="checked"' if $pref->{'wants_digest'};
139     }
140     $template->param(messaging_preferences => $messaging_options);
141 }
142
143 =head1 TODO
144
145 =over 4
146
147 =item Reduce coupling between processing CGI parameters and updating the messaging preferences
148
149 =item Handle when form input is invalid
150
151 =item Generalize into a system of form handler clases
152
153 =back
154
155 =head1 SEE ALSO
156
157 L<C4::Members::Messaging>, F<admin/categorie.pl>, F<opac/opac-messaging.pl>, F<members/messaging.pl>
158
159 =head1 AUTHOR
160
161 Koha Development Team <info@koha.org>
162
163 Galen Charlton <galen.charlton@liblime.com> refactoring code by Andrew Moore.
164
165 =cut
166
167 1;