File: | C4/Form/MessagingPreferences.pm |
Coverage: | 26.1% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
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 | 3 3 3 | 36963 12 127 | use strict; | |||
21 | 3 3 3 | 26 9 154 | use warnings; | |||
22 | ||||||
23 | 3 3 3 | 27098 196129 64 | use CGI; | |||
24 | 3 3 3 | 371 40 67 | use C4::Context; | |||
25 | 3 3 3 | 251 11 84 | use C4::Members::Messaging; | |||
26 | 3 3 3 | 19 9 2139 | use C4::Debug; | |||
27 | ||||||
28 - 70 | =head1 NAME C4::Form::MessagingPreferences - manage messaging prefernces form =head1 SYNOPSIS In script: use C4::Form::MessagingPreferences; C4::Form::MessagingPreferences::set_form_value({ borrowernumber => 51 }, $template); C4::Form::MessagingPreferences::handle_form_action($input, { categorycode => 'CPL' }, $template); In HTML template: <!-- TMPL_INCLUDE NAME="messaging-preference-form.inc" --> =head1 DESCRIPTION This module manages input and output for the messaging preferences form that is used in the staff patron editor, the staff patron category editor, and the OPAC patron messaging prefereneces form. It in its current form, it essentially serves only to eliminate copy-and-paste code, but suggests at least one approach for reconciling functionality that does mostly the same thing in staff and OPAC. =head1 FUNCTIONS =head2 handle_form_action C4::Form::MessagingPreferences::handle_form_action($input, { categorycode => 'CPL' }, $template, $insert); Processes CGI parameters and updates the target patron or patron category's preferences. C<$input> is the CGI query object. C<$target_params> is a hashref containing either a C<categorycode> key or a C<borrowernumber> key identifying the patron or patron category whose messaging preferences are to be updated. C<$template> is the Template::Toolkit object for the response; this routine adds a settings_updated template variable. =cut | |||||
71 | ||||||
72 | sub handle_form_action { | |||||
73 | 0 | my ($query, $target_params, $template, $insert, $categorycode) = @_; | ||||
74 | 0 | 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 | 0 | my $prefs_set = 0; | ||||
77 | 0 | OPTION: foreach my $option ( @$messaging_options ) { | ||||
78 | 0 0 | my $updater = { %{ $target_params }, | ||||
79 | message_attribute_id => $option->{'message_attribute_id'} }; | |||||
80 | ||||||
81 | # find the desired transports | |||||
82 | 0 0 | @{$updater->{'message_transport_types'}} = $query->param( $option->{'message_attribute_id'} ); | ||||
83 | 0 | next OPTION unless $updater->{'message_transport_types'}; | ||||
84 | ||||||
85 | 0 | if ( $option->{'has_digest'} ) { | ||||
86 | 0 0 | if ( List::Util::first { $_ == $option->{'message_attribute_id'} } $query->param( 'digest' ) ) { | ||||
87 | 0 | $updater->{'wants_digest'} = 1; | ||||
88 | } | |||||
89 | } | |||||
90 | ||||||
91 | 0 | if ( $option->{'takes_days'} ) { | ||||
92 | 0 | if ( defined $query->param( $option->{'message_attribute_id'} . '-DAYS' ) ) { | ||||
93 | 0 | $updater->{'days_in_advance'} = $query->param( $option->{'message_attribute_id'} . '-DAYS' ); | ||||
94 | } | |||||
95 | } | |||||
96 | ||||||
97 | 0 | C4::Members::Messaging::SetMessagingPreference( $updater ); | ||||
98 | ||||||
99 | 0 | if ($query->param( $option->{'message_attribute_id'})){ | ||||
100 | 0 | $prefs_set = 1; | ||||
101 | } | |||||
102 | } | |||||
103 | 0 | if (! $prefs_set && $insert){ | ||||
104 | # this is new borrower, and we have no preferences set, use the defaults | |||||
105 | 0 | $target_params->{categorycode} = $categorycode; | ||||
106 | 0 | C4::Members::Messaging::SetMessagingPreferencesFromDefaults( $target_params ); | ||||
107 | } | |||||
108 | # show the success message | |||||
109 | 0 | $template->param( settings_updated => 1 ); | ||||
110 | } | |||||
111 | ||||||
112 - 124 | =head2 set_form_values C4::Form::MessagingPreferences::set_form_value({ borrowernumber => 51 }, $template); Retrieves the messaging preferences for the specified patron or patron category and fills the corresponding template variables. C<$target_params> is a hashref containing either a C<categorycode> key or a C<borrowernumber> key identifying the patron or patron category. C<$template> is the Template::Toolkit object for the response. =cut | |||||
125 | ||||||
126 | sub set_form_values { | |||||
127 | 0 | my ($target_params, $template) = @_; | ||||
128 | # walk through the options and update them with these borrower_preferences | |||||
129 | 0 | my $messaging_options = C4::Members::Messaging::GetMessagingOptions(); | ||||
130 | 0 | PREF: foreach my $option ( @$messaging_options ) { | ||||
131 | 0 0 | my $pref = C4::Members::Messaging::GetMessagingPreferences( { %{ $target_params }, message_name => $option->{'message_name'} } ); | ||||
132 | 0 | $option->{ $option->{'message_name'} } = 1; | ||||
133 | # make a hashref of the days, selecting one. | |||||
134 | 0 | if ( $option->{'takes_days'} ) { | ||||
135 | 0 | my $days_in_advance = $pref->{'days_in_advance'} ? $pref->{'days_in_advance'} : 0; | ||||
136 | 0 | $option->{days_in_advance} = $days_in_advance; | ||||
137 | 0 0 | @{$option->{'select_days'}} = map {; { | ||||
138 | 0 | day => $_, | ||||
139 | selected => $_ == $days_in_advance ? 'selected="selected"' :'' } | |||||
140 | } ( 0..30 ); # FIXME: 30 is a magic number. | |||||
141 | } | |||||
142 | 0 0 | foreach my $transport ( @{$pref->{'transports'}} ) { | ||||
143 | 0 | $option->{'transports_'.$transport} = 1; | ||||
144 | } | |||||
145 | 0 | $option->{'digest'} = 1 if $pref->{'wants_digest'}; | ||||
146 | } | |||||
147 | 0 | $template->param(messaging_preferences => $messaging_options); | ||||
148 | } | |||||
149 | ||||||
150 - 172 | =head1 TODO =over 4 =item Reduce coupling between processing CGI parameters and updating the messaging preferences =item Handle when form input is invalid =item Generalize into a system of form handler clases =back =head1 SEE ALSO L<C4::Members::Messaging>, F<admin/categorie.pl>, F<opac/opac-messaging.pl>, F<members/messaging.pl> =head1 AUTHOR Koha Development Team <http://koha-community.org/> Galen Charlton <galen.charlton@liblime.com> refactoring code by Andrew Moore. =cut | |||||
173 | ||||||
174 | 1; |