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