Bug 24412: (follow-up) prevent request.pl from failing
[koha.git] / C4 / Form / MessagingPreferences.pm
blobb98015f85537caa0e1b9a3d8ba75f9fb1745c1d7
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
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.
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.
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>.
20 use strict;
21 use warnings;
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Members::Messaging;
26 use C4::Debug;
28 use constant MAX_DAYS_IN_ADVANCE => 30;
30 =head1 NAME
32 C4::Form::MessagingPreferences - manage messaging preferences form
34 =head1 SYNOPSIS
36 In script:
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);
42 In HTML template:
44 <!-- TMPL_INCLUDE NAME="messaging-preference-form.inc" -->
46 =head1 DESCRIPTION
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.
55 =head1 FUNCTIONS
57 =head2 handle_form_action
59 C4::Form::MessagingPreferences::handle_form_action($input, { categorycode => 'CPL' }, $template, $insert);
61 Processes CGI parameters and updates the target patron or patron category's
62 preferences.
64 C<$input> is the CGI query object.
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.
69 C<$template> is the Template::Toolkit object for the response; this routine
70 adds a settings_updated template variable.
72 =cut
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'} };
83 # find the desired transports
84 @{$updater->{'message_transport_types'}} = $query->multi_param( $option->{'message_attribute_id'} );
85 next OPTION unless $updater->{'message_transport_types'};
87 if ( $option->{'has_digest'} ) {
88 if ( List::Util::first { $_ == $option->{'message_attribute_id'} } $query->multi_param( 'digest' ) ) {
89 $updater->{'wants_digest'} = 1;
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' );
99 C4::Members::Messaging::SetMessagingPreference( $updater );
101 if ($query->param( $option->{'message_attribute_id'})){
102 $prefs_set = 1;
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 );
110 # show the success message
111 $template->param( settings_updated => 1 );
114 =head2 set_form_values
116 C4::Form::MessagingPreferences::set_form_value({ borrowernumber => 51 }, $template);
118 Retrieves the messaging preferences for the specified patron or patron category
119 and fills the corresponding template variables.
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.
124 C<$template> is the Template::Toolkit object for the response.
126 =cut
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 );
144 foreach my $transport ( keys %{$pref->{'transports'}} ) {
145 $option->{'transports_'.$transport} = 1;
147 $option->{'digest'} = 1 if $pref->{'wants_digest'};
149 $template->param(messaging_preferences => $messaging_options);
152 =head1 TODO
154 =over 4
156 =item Reduce coupling between processing CGI parameters and updating the messaging preferences
158 =item Handle when form input is invalid
160 =item Generalize into a system of form handler clases
162 =back
164 =head1 SEE ALSO
166 L<C4::Members::Messaging>, F<admin/categories.pl>, F<opac/opac-messaging.pl>, F<members/messaging.pl>
168 =head1 AUTHOR
170 Koha Development Team <http://koha-community.org/>
172 Galen Charlton <galen.charlton@liblime.com> refactoring code by Andrew Moore.
174 =cut