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 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
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.
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.
25 use C4::Members::Messaging;
30 C4::Form::MessagingPreferences - manage messaging prefernces form
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);
42 <!-- TMPL_INCLUDE NAME="messaging-preference-form.inc" -->
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.
55 =head2 handle_form_action
57 C4::Form::MessagingPreferences::handle_form_action($input, { categorycode => 'CPL' }, $template);
59 Processes CGI parameters and updates the target patron or patron category's
62 C<$input> is the CGI query object.
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.
67 C<$template> is the HTML::Template::Pro object for the response; this routine
68 adds a settings_updated template variable.
72 sub handle_form_action {
73 my ($query, $target_params, $template) = @_;
74 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->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->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 );
101 # show the success message
102 $template->param( settings_updated => 1 );
105 =head2 set_form_values
107 C4::Form::MessagingPreferences::set_form_value({ borrowernumber => 51 }, $template);
109 Retrieves the messaging preferences for the specified patron or patron category
110 and fills the corresponding template variables.
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.
115 C<$template> is the HTML::Template::Pro object for the response.
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 }, message_name => $option->{'message_name'} } );
125 $option->{ $option->{'message_name'} } = 1;
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 {; {
132 selected => $_ == $days_in_advance ? 'selected="selected"' :'' }
133 } ( 0..30 ); # FIXME: 30 is a magic number.
135 foreach my $transport ( @{$pref->{'transports'}} ) {
136 $option->{'transport_'.$transport} = 'checked="checked"';
138 $option->{'digest'} = 'checked="checked"' if $pref->{'wants_digest'};
140 $template->param(messaging_preferences => $messaging_options);
147 =item Reduce coupling between processing CGI parameters and updating the messaging preferences
149 =item Handle when form input is invalid
151 =item Generalize into a system of form handler clases
157 L<C4::Members::Messaging>, F<admin/categorie.pl>, F<opac/opac-messaging.pl>, F<members/messaging.pl>
161 Koha Development Team <http://koha-community.org/>
163 Galen Charlton <galen.charlton@liblime.com> refactoring code by Andrew Moore.