(bug #2811)[3.2] fix opac-renew.pl part
[koha.git] / opac / opac-messaging.pl
1 #!/usr/bin/perl
2
3 # Copyright 2008 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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use warnings;
22
23 use CGI;
24
25 use C4::Auth;    # checkauth, getborrowernumber.
26 use C4::Context;
27 use C4::Koha;
28 use C4::Circulation;
29 use C4::Output;
30 use C4::Dates qw/format_date/;
31 use C4::Members;
32 use C4::Members::Messaging;
33 use C4::Branch;
34
35 my $query = CGI->new();
36
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
38     {
39         template_name   => 'opac-messaging.tmpl',
40         query           => $query,
41         type            => 'opac',
42         authnotrequired => 0,
43         flagsrequired   => { borrow => 1 },
44         debug           => 1,
45     }
46 );
47
48 my $borrower = GetMemberDetails( $borrowernumber );
49 my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
50
51 if ( defined $query->param('modify') && $query->param('modify') eq 'yes' ) {
52
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 );
58     }
59
60     # TODO: If a "NONE" box and another are checked somehow (javascript failed), we should pay attention to the "NONE" box
61     
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'} };
67         
68         # find the desired transports
69         @{$updater->{'message_transport_types'}} = $query->param( $option->{'message_attribute_id'} );
70         next OPTION unless $updater->{'message_transport_types'};
71
72         if ( $option->{'has_digest'} ) {
73             if ( List::Util::first { $_ == $option->{'message_attribute_id'} } $query->param( 'digest' ) ) {
74                 $updater->{'wants_digest'} = 1;
75             }
76         }
77
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' );
81             }
82         }
83
84         # warn( 'calling SetMessaginPreferencse with ' . Data::Dumper->Dump( [ $updater ], [ 'updater' ] ) );
85         C4::Members::Messaging::SetMessagingPreference( $updater );
86     }
87
88     # show the success message
89     $template->param( settings_updated => 1 );
90
91
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,
102                            selected => '' };
103             if ( defined $pref->{'days_in_advance'} && $pref->{'days_in_advance'} == $day ) {
104                 $dayrow->{'selected'} = 'SELECTED';
105             }
106             push @{$option->{'select_days'}}, $dayrow
107         }
108     }
109     foreach my $transport ( @{$pref->{'transports'}} ) {
110         $option->{'transport-'.$transport} = 'CHECKED';
111     }
112
113     $option->{'digest'} = $pref->{'wants_digest'} ? 'CHECKED' : '';
114 }
115
116 # warn( Data::Dumper->Dump( [ $messaging_options ], [ 'messaging_options' ] ) );
117 $template->param( BORROWER_INFO         => [ $borrower ],
118                   messagingview         => 1,
119                   messaging_preferences => $messaging_options,
120                   SMSnumber => defined $borrower->{'smsalertnumber'} ? $borrower->{'smsalertnumber'} : $borrower->{'mobile'},
121                   SMSSendDriver                =>  C4::Context->preference("SMSSendDriver") );
122
123 output_html_with_http_headers $query, $cookie, $template->output;