]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/HoldNotify.pm
updated some logging
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ / HoldNotify.pm
1 # ---------------------------------------------------------------
2 # Copyright (C) 2005  Georgia Public Library Service 
3 # Bill Erickson <billserickson@gmail.com>
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 # ---------------------------------------------------------------
15
16
17 package OpenILS::Application::Circ::HoldNotify;
18 use base qw/OpenSRF::Application/;
19 use strict; use warnings;
20 use OpenSRF::EX qw(:try);
21 use vars q/$AUTOLOAD/;
22 use OpenILS::Event;
23 use OpenSRF::Utils::Logger qw(:logger);
24 use OpenILS::Utils::CStoreEditor q/:funcs/;
25 use OpenSRF::Utils::SettingsClient;
26 use OpenILS::Application::AppUtils;
27 use OpenILS::Const qw/:const/;
28 use OpenILS::Utils::Fieldmapper;
29 use Email::Send;
30 use Data::Dumper;
31 use OpenSRF::EX qw/:try/;
32 my $U = 'OpenILS::Application::AppUtils';
33
34 use open ':utf8';
35
36
37 __PACKAGE__->register_method(
38         method => 'send_email_notify_pub',
39         api_name => 'open-ils.circ.send_hold_notify.email',
40 );
41
42
43 sub send_email_notify_pub {
44         my( $self, $conn, $auth, $hold_id ) = @_;
45         my $e = new_editor(authtoken => $auth, xact =>1);
46         return $e->event unless $e->checkauth;
47         return $e->event unless $e->allowed('CREATE_HOLD_NOTIFICATION');
48         my $notifier = __PACKAGE__->new(editor=> $e, hold_id => $hold_id);
49         return $notifier->event if $notifier->event;
50         my $stat = $notifier->send_email_notify;
51         $e->commit if $stat == '1';
52         return $stat;
53 }
54
55
56
57
58
59 # ---------------------------------------------------------------
60 # Define the notifier object
61 # ---------------------------------------------------------------
62
63 my @AUTOLOAD_FIELDS = qw/
64         hold
65         copy
66         volume
67         title
68         editor
69         patron
70         event
71         pickup_lib
72         smtp_server
73         settings_client
74 /;
75
76 sub AUTOLOAD {
77         my $self = shift;
78         my $type = ref($self) or die "$self is not an object";
79         my $data = shift;
80         my $name = $AUTOLOAD;
81         $name =~ s/.*://o;   
82
83         unless (grep { $_ eq $name } @AUTOLOAD_FIELDS) {
84                 $logger->error("hold_notify: $type: invalid autoload field: $name");
85                 die "$type: invalid autoload field: $name\n" 
86         }
87
88         {
89                 no strict 'refs';
90                 *{"${type}::${name}"} = sub {
91                         my $s = shift;
92                         my $v = shift;
93                         $s->{$name} = $v if defined $v;
94                         return $s->{$name};
95                 }
96         }
97         return $self->$name($data);
98 }
99
100
101 sub new {
102         my( $class, %args ) = @_;
103         $class = ref($class) || $class;
104         my $self = bless( {}, $class );
105         $self->editor( ($args{editor}) ? $args{editor} : new_editor());
106         $self->fetch_data($args{hold_id});
107         return $self;
108 }
109
110
111 sub send_email_notify {
112         my $self = shift;
113
114         my $sc = OpenSRF::Utils::SettingsClient->new;
115         my $setting = $sc->config_value(
116                 qw/ apps open-ils.circ app_settings notify_hold email / );
117
118         $logger->debug("hold_notify: email enabled setting = $setting");
119
120         if( !$setting or $setting ne 'true' ) {
121                 $logger->info("hold_notify: not sending hold notify - email notifications disabled");
122                 return 0;
123         }
124
125         unless ($U->is_true($self->hold->email_notify)) {
126                 $logger->info("hold_notify: not sending hold notification becaue email_notify is false");
127                 return 0;
128         }
129
130         $logger->info("hold_notify: attempting email notify on hold ".$self->hold->id);
131
132         return OpenILS::Event->new('PATRON_NO_EMAIL_ADDRESS')
133                 unless $self->patron->email and
134                 $self->patron->email =~ /.+\@.+/; # see if it's remotely email-esque
135
136         my $sclient = OpenSRF::Utils::SettingsClient->new;
137         $self->settings_client($sclient);
138         my $template = $sclient->config_value('email_notify', 'template');
139         my $str = $self->flesh_template($self->load_template($template));
140
141         unless( $str ) {
142                 $logger->error("hold_notify: No email notifiy template found - cannot notify");
143                 return 0;
144         }
145
146         $logger->info("hold_notify: fleshed template: $str");
147
148         $self->send_email($str);
149
150         my $notify = Fieldmapper::action::hold_notification->new;
151         $notify->hold($self->hold->id);
152         $notify->notify_staff($self->editor->requestor->id);
153         $notify->notify_time('now');
154         $notify->method('email');
155         
156         $self->editor->create_action_hold_notification($notify)
157                 or return $self->editor->event;
158
159         return 1;
160 }
161
162 sub send_email {
163         my( $self, $text ) = @_;
164
165         my $smtp = $self->settings_client->config_value('email_notify', 'smtp_server');
166
167         $logger->info("hold_notify: sending email notice to ".
168                 $self->patron->email." with SMTP server $smtp");
169
170         my $sender = Email::Send->new({mailer => 'SMTP'});
171         $sender->mailer_args([Host => $smtp]);
172
173         my $stat;
174         my $err;
175
176         try {
177                 $stat = $sender->send($text);
178         } catch Error with {
179                 $err = $stat = shift;
180                 $logger->error("hold_notify: Email notify failed with error: $err");
181         };
182
183         if( !$err and $stat and $stat->type eq 'success' ) {
184                 $logger->info("hold_notify: successfully sent hold notification");
185                 return 1;
186         } else {
187                 $logger->warn("hold_notify: unable to send hold notification: ".Dumper($stat));
188                 return 0;
189         }
190
191         return undef;
192 }
193
194
195 # -------------------------------------------------------------------------
196 # Fetches all of the hold-related data
197 # -------------------------------------------------------------------------
198 sub fetch_data {
199         my $self                = shift;
200         my $holdid      = shift;
201         my $e                   = $self->editor;
202
203         $self->hold($e->retrieve_action_hold_request($holdid)) or return $self->event($e->event);
204         $self->copy($e->retrieve_asset_copy($self->hold->current_copy)) or return $self->event($e->event);
205         $self->volume($e->retrieve_asset_call_number($self->copy->call_number)) or return $self->event($e->event);
206         $self->title($e->retrieve_biblio_record_entry($self->volume->record)) or return $self->event($e->event);
207         $self->patron($e->retrieve_actor_user($self->hold->usr)) or return $self->event($e->event);
208         $self->pickup_lib($e->retrieve_actor_org_unit($self->hold->pickup_lib)) or return $self->event($e->event);
209 }
210
211
212 sub extract_data {
213         my $self = shift;
214         my $e = $self->editor;
215
216         my $patron = $self->patron;
217         my $o_name = $self->pickup_lib->name;
218         my $p_name = $patron->first_given_name .' '.$patron->family_name;
219
220         # try to find a suitable address for the patron
221         my $p_addr;
222         my $p_addrs;
223         unless( $p_addr = 
224                         $e->retrieve_actor_user_address($patron->billing_address)) {
225                 unless( $p_addr = 
226                                 $e->retrieve_actor_user_address($patron->mailing_address)) {
227                         $logger->warn("hold_notify: No address for user ".$patron->id);
228                         $p_addrs = "";
229                 }
230         }
231
232         unless( defined $p_addrs ) {
233                 $p_addrs = 
234                         $p_addr->street1." ".
235                         $p_addr->street2." ".
236                         $p_addr->city." ".
237                         $p_addr->state." ".
238                         $p_addr->post_code;
239         }
240
241         my $l_addr = $e->retrieve_actor_org_address($self->pickup_lib->holds_address);
242         my $l_addrs = (!$l_addr) ? "" : 
243                         $l_addr->street1." ".
244                         $l_addr->street2." ".
245                         $l_addr->city." ".
246                         $l_addr->state." ".
247                         $l_addr->post_code;
248
249         my $title;      
250         my $author;
251
252         if( $self->title->id == OILS_PRECAT_RECORD ) {
253                 $title = ($self->copy->dummy_title) ? 
254                         $self->copy->dummy_title : "";
255                 $author = ($self->copy->dummy_author) ? 
256                         $self->copy->dummy_author : "";
257         } else {
258                 my $mods        = $U->record_to_mvr($self->title);
259                 $title  = ($mods->title) ? $mods->title : "";
260                 $author = ($mods->author) ? $mods->author : "";
261         }
262
263
264         return { 
265                 patron_email => $self->patron->email,
266                 pickup_lib_name => $o_name,
267                 pickup_lib_addr => $l_addrs,
268                 patron_name => $p_name, 
269                 patron_addr => $p_addrs, 
270                 title => $title, 
271                 author => $author, 
272                 call_number => $self->volume->label,
273                 copy_barcode => $self->copy->barcode,
274                 copy_number => $self->copy->copy_number,
275         };
276 }
277
278
279
280 sub load_template {
281         my $self = shift;
282         my $template = shift;
283
284         unless( open(F, $template) ) {
285                 $logger->error("hold_notify: Unable to open hold notification template file: $template");
286                 return undef;
287         }
288
289         # load the template, strip comments
290         my @lines = <F>;
291         close(F);
292
293         my $str = '';
294         for(@lines) {
295         chomp $_;
296         next if $_ =~ /^\s*\#/o;
297         $_ =~ s/\#.*//og;
298         $str .= "$_\n";
299         }
300
301         return $str;
302 }
303
304 sub flesh_template {
305         my( $self, $str ) = @_;
306         return undef unless $str;
307
308         my @time        = CORE::localtime();
309         my $day                 = $time[3];
310         my $month       = $time[4] + 1;
311         my $year        = $time[5] + 1900;
312
313         my $data = $self->extract_data;
314
315         my $email               = $$data{patron_email};
316         my $p_name              = $$data{patron_name};
317         my $p_addr              = $$data{patron_addr};
318         my $o_name              = $$data{pickup_lib_name};
319         my $o_addr              = $$data{pickup_lib_addr};
320         my $title               = $$data{title};
321         my $author              = $$data{author};
322         my $cn                  = $$data{call_number};
323         my $barcode             = $$data{copy_barcode};
324         my $copy_number = $$data{copy_number};
325
326         my $sender = $self->settings_client->config_value('email_notify', 'sender_address');
327         my $reply_to = $self->pickup_lib->email;
328         $reply_to ||= $sender; 
329
330
331    $str =~ s/\${EMAIL_SENDER}/$sender/;
332    $str =~ s/\${EMAIL_RECIPIENT}/$email/;
333    $str =~ s/\${EMAIL_REPLY_TO}/$reply_to/;
334    $str =~ s/\${EMAIL_HEADERS}//;
335
336    $str =~ s/\${DATE}/$year-$month-$day/;
337    $str =~ s/\${LIBRARY}/$o_name/;
338    $str =~ s/\${LIBRARY_ADDRESS}/$o_addr/;
339    $str =~ s/\${PATRON_NAME}/$p_name/;
340    $str =~ s/\${PATRON_ADDRESS}/$p_addr/;
341
342    $str =~ s/\${TITLE}/$title/;
343    $str =~ s/\${AUTHOR}/$author/;
344    $str =~ s/\${CALL_NUMBER}/$cn/;
345    $str =~ s/\${COPY_BARCODE}/$barcode/;
346    $str =~ s/\${COPY_NUMBER}/$copy_number/;
347
348         return $str;
349 }
350
351
352
353
354
355 1;