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>.
25 use C4::Members::Messaging;
27 use constant MAX_DAYS_IN_ADVANCE => 30;
31 C4::Form::MessagingPreferences - manage messaging preferences form
37 use C4::Form::MessagingPreferences;
38 C4::Form::MessagingPreferences::set_form_value({ borrowernumber => 51 }, $template);
39 C4::Form::MessagingPreferences::handle_form_action($input, { categorycode => 'CPL' }, $template);
43 <!-- TMPL_INCLUDE NAME="messaging-preference-form.inc" -->
47 This module manages input and output for the messaging preferences form
48 that is used in the staff patron editor, the staff patron category editor,
49 and the OPAC patron messaging prefereneces form. It in its current form,
50 it essentially serves only to eliminate copy-and-paste code, but suggests
51 at least one approach for reconciling functionality that does mostly
52 the same thing in staff and OPAC.
56 =head2 handle_form_action
58 C4::Form::MessagingPreferences::handle_form_action($input, { categorycode => 'CPL' }, $template, $insert);
60 Processes CGI parameters and updates the target patron or patron category's
63 C<$input> is the CGI query object.
65 C<$target_params> is a hashref containing either a C<categorycode> key or a C<borrowernumber> key
66 identifying the patron or patron category whose messaging preferences are to be updated.
68 C<$template> is the Template::Toolkit object for the response; this routine
69 adds a settings_updated template variable.
73 sub handle_form_action {
74 my ($query, $target_params, $template, $insert, $categorycode) = @_;
75 my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
76 # TODO: If a "NONE" box and another are checked somehow (javascript failed), we should pay attention to the "NONE" box
78 OPTION: foreach my $option ( @$messaging_options ) {
79 my $updater = { %{ $target_params },
80 message_attribute_id => $option->{'message_attribute_id'} };
82 # find the desired transports
83 @{$updater->{'message_transport_types'}} = $query->multi_param( $option->{'message_attribute_id'} );
84 next OPTION unless $updater->{'message_transport_types'};
86 if ( $option->{'has_digest'} ) {
87 if ( List::Util::first { $_ == $option->{'message_attribute_id'} } $query->multi_param( 'digest' ) ) {
88 $updater->{'wants_digest'} = 1;
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' );
98 C4::Members::Messaging::SetMessagingPreference( $updater );
100 if ($query->param( $option->{'message_attribute_id'})){
104 if (! $prefs_set && $insert){
105 # this is new borrower, and we have no preferences set, use the defaults
106 $target_params->{categorycode} = $categorycode;
107 C4::Members::Messaging::SetMessagingPreferencesFromDefaults( $target_params );
109 # show the success message
110 $template->param( settings_updated => 1 );
113 =head2 set_form_values
115 C4::Form::MessagingPreferences::set_form_value({ borrowernumber => 51 }, $template);
117 Retrieves the messaging preferences for the specified patron or patron category
118 and fills the corresponding template variables.
120 C<$target_params> is a hashref containing either a C<categorycode> key or a C<borrowernumber> key
121 identifying the patron or patron category.
123 C<$template> is the Template::Toolkit object for the response.
127 sub set_form_values {
128 my ($target_params, $template) = @_;
129 # walk through the options and update them with these borrower_preferences
130 my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
131 PREF: foreach my $option ( @$messaging_options ) {
132 my $pref = C4::Members::Messaging::GetMessagingPreferences( { %{ $target_params }, message_name => $option->{'message_name'} } );
133 $option->{ $option->{'message_name'} } = 1;
134 # make a hashref of the days, selecting one.
135 if ( $option->{'takes_days'} ) {
136 my $days_in_advance = $pref->{'days_in_advance'} ? $pref->{'days_in_advance'} : 0;
137 $option->{days_in_advance} = $days_in_advance;
138 @{$option->{'select_days'}} = map { {
140 selected => $_ == $days_in_advance }
141 } ( 0..MAX_DAYS_IN_ADVANCE );
143 foreach my $transport ( keys %{$pref->{'transports'}} ) {
144 $option->{'transports_'.$transport} = 1;
146 $option->{'digest'} = 1 if $pref->{'wants_digest'};
148 $template->param(messaging_preferences => $messaging_options);
155 =item Reduce coupling between processing CGI parameters and updating the messaging preferences
157 =item Handle when form input is invalid
159 =item Generalize into a system of form handler clases
165 L<C4::Members::Messaging>, F<admin/categories.pl>, F<opac/opac-messaging.pl>, F<members/messaging.pl>
169 Koha Development Team <http://koha-community.org/>
171 Galen Charlton <galen.charlton@liblime.com> refactoring code by Andrew Moore.