commenting out debug warns
[koha.git] / members / 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
21 use strict;
22 use CGI;
23 use C4::Context;
24 use C4::Auth;
25 use C4::Output;
26 use C4::Members;
27 use C4::Members::Messaging;
28 use C4::Dates;
29 use C4::Reserves;
30 use C4::Circulation;
31 use C4::Koha;
32 use C4::Letters;
33 use C4::Biblio;
34 use C4::Reserves;
35 use C4::Branch; # GetBranchName
36
37 use Data::Dumper;
38
39 use vars qw($debug);
40
41 BEGIN {
42         $debug = $ENV{DEBUG} || 0;
43 }
44
45 my $dbh = C4::Context->dbh;
46
47 my $query = CGI->new();
48
49 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
50     {
51         template_name   => 'members/messaging.tmpl',
52         query           => $query,
53         type            => "intranet",
54         authnotrequired => 0,
55         flagsrequired   => { borrowers => 1 },
56         debug           => 1,
57     }
58 );
59 my $borrowernumber = $query->param('borrowernumber');
60 my $borrower       = GetMember( $borrowernumber ,'borrowernumber');
61 my $branch         = C4::Context->userenv->{'branch'};
62
63 $template->param( $borrower );
64
65 my $borrower = GetMemberDetails( $borrowernumber );
66
67 my $messaging_options = C4::Members::Messaging::GetMessagingOptions();
68 my $messaging_preferences;
69
70 if ( defined $query->param('modify') && $query->param('modify') eq 'yes' ) {
71
72     # If they've modified the SMS number, record it.
73     if ( ( defined $query->param('SMSnumber') ) && ( $query->param('SMSnumber') ne $borrower->{'mobile'} ) ) {
74         ModMember( borrowernumber => $borrowernumber,
75                    smsalertnumber => $query->param('SMSnumber') );
76         $borrower = GetMemberDetails( $borrowernumber );
77     }
78
79     # TODO: If a "NONE" box and another are checked somehow (javascript failed), we should pay attention to the "NONE" box
80     
81     # warn( Data::Dumper->Dump( [ $messaging_options ], [ 'messaging_options' ] ) );
82     OPTION: foreach my $option ( @$messaging_options ) {
83         # warn( Data::Dumper->Dump( [ $option ], [ 'option' ] ) );
84         my $updater = { borrowernumber          => $borrower->{'borrowernumber'},
85                         message_attribute_id    => $option->{'message_attribute_id'} };
86         
87         # find the desired transports
88         @{$updater->{'message_transport_types'}} = $query->param( $option->{'message_attribute_id'} );
89         next OPTION unless $updater->{'message_transport_types'};
90
91         if ( $option->{'has_digest'} ) {
92             if ( List::Util::first { $_ == $option->{'message_attribute_id'} } $query->param( 'digest' ) ) {
93                 $updater->{'wants_digest'} = 1;
94             }
95         }
96
97         if ( $option->{'takes_days'} ) {
98             if ( defined $query->param( $option->{'message_attribute_id'} . '-DAYS' ) ) {
99                 $updater->{'days_in_advance'} = $query->param( $option->{'message_attribute_id'} . '-DAYS' );
100             }
101         }
102
103         #warn( 'calling SetMessaginPreferencse with ' . Data::Dumper->Dump( [ $updater ], [ 'updater' ] ) );
104         C4::Members::Messaging::SetMessagingPreference( $updater );
105     }
106
107     # show the success message
108     $template->param( settings_updated => 1 );
109
110
111 # walk through the options and update them with these borrower_preferences
112 PREF: foreach my $option ( @$messaging_options ) {
113     my $pref = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber     => $borrower->{'borrowernumber'},
114                                                                   message_name       => $option->{'message_name'} } );
115     #warn( Data::Dumper->Dump( [ $pref ], [ 'pref' ] ) );
116     # make a hashref of the days, selecting one.
117     if ( $option->{'takes_days'} ) {
118         @{$option->{'select_days'}} = map {; { day        => $_,
119                                                selected   => $_ == $pref->{'days_in_advance'} ? 'SELECTED' :'' } } ( 0..30 ); # FIXME: 30 is a magic number.
120     }
121     foreach my $transport ( @{$pref->{'transports'}} ) {
122         $option->{'transport-'.$transport} = 'CHECKED';
123     }
124     $option->{'digest'} = 'CHECKED' if $pref->{'wants_digest'};
125 }
126
127     if ( $borrower->{'category_type'} eq 'C') {
128         my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
129         my $cnt = scalar(@$catcodes);
130         $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
131         $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
132     }
133         
134 my ($picture, $dberror) = GetPatronImage($borrower->{'cardnumber'});
135 $template->param( picture => 1 ) if $picture;
136
137 # get some recent messages sent to this borrower for display:
138 my $message_queue = C4::Letters::GetQueuedMessages( { borrowernumber => $query->param('borrowernumber') } );
139
140 $template->param( messagingview               => 1,
141                   messaging_preferences       => [ $messaging_preferences ],
142                   message_queue               => $message_queue,
143                   DHTMLcalendar_dateformat    => C4::Dates->DHTMLcalendar(), 
144                   borrowernumber              => $borrowernumber,
145                   branchcode                  => $borrower->{'branchcode'},
146                   branchname                  => GetBranchName($borrower->{'branchcode'}),
147                   dateformat                  => C4::Context->preference("dateformat"),
148                   categoryname                => $borrower->{'description'},
149                   $borrower->{'categorycode'} => 1,
150 );
151
152 $messaging_preferences->{'SMSnumber'}{'value'} = defined $borrower->{'smsalertnumber'}
153   ? $borrower->{'smsalertnumber'} : $borrower->{'mobile'};
154
155 $template->param( BORROWER_INFO         => [ $borrower ],
156                   messagingview         => 1,
157                   messaging_preferences => $messaging_options,
158                                   is_child        => ($borrower->{'category_type'} eq 'C'),
159                   SMSnumber             => defined $borrower->{'smsalertnumber'} ? $borrower->{'smsalertnumber'} : $borrower->{'mobile'} );
160
161 output_html_with_http_headers $query, $cookie, $template->output;