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