3 # Copyright 2008 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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
25 use C4
::Auth
; # checkauth, getborrowernumber.
30 use C4
::Dates qw
/format_date/;
32 use C4
::Members
::Messaging
;
35 my $query = CGI
->new();
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
39 template_name
=> 'opac-messaging.tmpl',
43 flagsrequired
=> { borrow
=> 1 },
48 my $borrower = GetMemberDetails
( $borrowernumber );
49 my $messaging_options = C4
::Members
::Messaging
::GetMessagingOptions
();
51 if ( defined $query->param('modify') && $query->param('modify') eq 'yes' ) {
53 # If they've modified the SMS number, record it.
54 if ( ( defined $query->param('SMSnumber') ) && ( $query->param('SMSnumber') ne $borrower->{'mobile'} ) ) {
55 ModMember
( borrowernumber
=> $borrowernumber,
56 smsalertnumber
=> $query->param('SMSnumber') );
57 $borrower = GetMemberDetails
( $borrowernumber );
60 # TODO: If a "NONE" box and another are checked somehow (javascript failed), we should pay attention to the "NONE" box
62 # warn( Data::Dumper->Dump( [ $messaging_options ], [ 'messaging_options' ] ) );
63 OPTION
: foreach my $option ( @
$messaging_options ) {
64 # warn( Data::Dumper->Dump( [ $option ], [ 'option' ] ) );
65 my $updater = { borrowernumber
=> $borrower->{'borrowernumber'},
66 message_attribute_id
=> $option->{'message_attribute_id'} };
68 # find the desired transports
69 @
{$updater->{'message_transport_types'}} = $query->param( $option->{'message_attribute_id'} );
70 next OPTION
unless $updater->{'message_transport_types'};
72 if ( $option->{'has_digest'} ) {
73 if ( List
::Util
::first
{ $_ == $option->{'message_attribute_id'} } $query->param( 'digest' ) ) {
74 $updater->{'wants_digest'} = 1;
78 if ( $option->{'takes_days'} ) {
79 if ( defined $query->param( $option->{'message_attribute_id'} . '-DAYS' ) ) {
80 $updater->{'days_in_advance'} = $query->param( $option->{'message_attribute_id'} . '-DAYS' );
84 # warn( 'calling SetMessaginPreferencse with ' . Data::Dumper->Dump( [ $updater ], [ 'updater' ] ) );
85 C4
::Members
::Messaging
::SetMessagingPreference
( $updater );
88 # show the success message
89 $template->param( settings_updated
=> 1 );
92 $messaging_options = C4
::Members
::Messaging
::GetMessagingOptions
();
93 # walk through the options and update them with these borrower_preferences
94 PREF
: foreach my $option ( @
$messaging_options ) {
95 my $pref = C4
::Members
::Messaging
::GetMessagingPreferences
( { borrowernumber
=> $borrower->{'borrowernumber'},
96 message_name
=> $option->{'message_name'} } );
97 # warn( Data::Dumper->Dump( [ $pref ], [ 'pref' ] ) );
98 # make a hashref of the days, selecting one.
99 if ( $option->{'takes_days'} ) {
100 foreach my $day ( 0..30 ) { # FIXME: 30 is a magic number.
101 my $dayrow = { day
=> $day,
103 if ( defined $pref->{'days_in_advance'} && $pref->{'days_in_advance'} == $day ) {
104 $dayrow->{'selected'} = 'SELECTED';
106 push @
{$option->{'select_days'}}, $dayrow
109 foreach my $transport ( @
{$pref->{'transports'}} ) {
110 $option->{'transport-'.$transport} = 'CHECKED';
113 $option->{'digest'} = $pref->{'wants_digest'} ?
'CHECKED' : '';
116 # warn( Data::Dumper->Dump( [ $messaging_options ], [ 'messaging_options' ] ) );
117 $template->param( BORROWER_INFO
=> [ $borrower ],
119 messaging_preferences
=> $messaging_options,
120 SMSnumber
=> defined $borrower->{'smsalertnumber'} ?
$borrower->{'smsalertnumber'} : $borrower->{'mobile'},
121 SMSSendDriver
=> C4
::Context
->preference("SMSSendDriver") );
123 output_html_with_http_headers
$query, $cookie, $template->output;