1 # ---------------------------------------------------------------
2 # Copyright (C) 2005 Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.com>
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.
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 # ---------------------------------------------------------------
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/;
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;
31 use OpenSRF::EX qw/:try/;
32 my $U = 'OpenILS::Application::AppUtils';
37 __PACKAGE__->register_method(
38 method => 'send_email_notify_pub',
39 api_name => 'open-ils.circ.send_hold_notify.email',
43 sub send_email_notify_pub {
44 my( $self, $conn, $auth, $hold_id ) = @_;
45 my $e = new_editor(authtoken => $auth);
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';
59 # ---------------------------------------------------------------
60 # Define the notifier object
61 # ---------------------------------------------------------------
63 my @AUTOLOAD_FIELDS = qw/
78 my $type = ref($self) or die "$self is not an object";
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"
90 *{"${type}::${name}"} = sub {
93 $s->{$name} = $v if defined $v;
97 return $self->$name($data);
102 my( $class, %args ) = @_;
103 $class = ref($class) || $class;
104 my $self = bless( {}, $class );
105 $self->editor($args{editor});
106 $logger->debug("circulator: creating new hold-notifier with requestor ".
107 $self->editor->requestor->id);
108 $self->fetch_data($args{hold_id});
113 sub send_email_notify {
116 my $sc = OpenSRF::Utils::SettingsClient->new;
117 my $setting = $sc->config_value(
118 qw/ apps open-ils.circ app_settings notify_hold email / );
120 $logger->debug("hold_notify: email enabled setting = $setting");
122 if( !$setting or $setting ne 'true' ) {
123 $logger->info("hold_notify: not sending hold notify - email notifications disabled");
127 unless ($U->is_true($self->hold->email_notify)) {
128 $logger->info("hold_notify: not sending hold notification becaue email_notify is false");
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
136 $logger->info("hold_notify: attempting email notify on hold ".$self->hold->id);
138 my $sclient = OpenSRF::Utils::SettingsClient->new;
139 $self->settings_client($sclient);
140 my $template = $sclient->config_value('email_notify', 'template');
141 my $str = $self->flesh_template($self->load_template($template));
144 $logger->error("hold_notify: No email notifiy template found - cannot notify");
148 return 0 unless $self->send_email($str);
150 # ------------------------------------------------------------------
151 # If the hold email takes too long to send, the existing editor
152 # transaction may have timed out. Create a one-off editor to write
153 # the notification to the DB.
154 # ------------------------------------------------------------------
155 my $we = new_editor(xact=>1, requestor=>$self->editor->requestor);
157 my $notify = Fieldmapper::action::hold_notification->new;
158 $notify->hold($self->hold->id);
159 $notify->notify_staff($we->requestor->id);
160 $notify->notify_time('now');
161 $notify->method('email');
163 $we->create_action_hold_notification($notify)
164 or return $we->die_event;
171 my( $self, $text ) = @_;
173 my $smtp = $self->settings_client->config_value('email_notify', 'smtp_server');
175 $logger->info("hold_notify: sending email notice to ".
176 $self->patron->email." with SMTP server $smtp");
178 my $sender = Email::Send->new({mailer => 'SMTP'});
179 $sender->mailer_args([Host => $smtp]);
185 $stat = $sender->send($text);
187 $err = $stat = shift;
188 $logger->error("hold_notify: Email notify failed with error: $err");
191 if( !$err and $stat and $stat->type eq 'success' ) {
192 $logger->info("hold_notify: successfully sent hold notification");
195 $logger->warn("hold_notify: unable to send hold notification: ".Dumper($stat));
203 # -------------------------------------------------------------------------
204 # Fetches all of the hold-related data
205 # -------------------------------------------------------------------------
209 my $e = $self->editor;
211 $logger->debug("circulator: fetching hold notify data");
213 $self->hold($e->retrieve_action_hold_request($holdid)) or return $self->event($e->event);
214 $self->copy($e->retrieve_asset_copy($self->hold->current_copy)) or return $self->event($e->event);
215 $self->volume($e->retrieve_asset_call_number($self->copy->call_number)) or return $self->event($e->event);
216 $self->title($e->retrieve_biblio_record_entry($self->volume->record)) or return $self->event($e->event);
217 $self->patron($e->retrieve_actor_user($self->hold->usr)) or return $self->event($e->event);
218 $self->pickup_lib($e->retrieve_actor_org_unit($self->hold->pickup_lib)) or return $self->event($e->event);
224 my $e = $self->editor;
226 my $patron = $self->patron;
227 my $o_name = $self->pickup_lib->name;
228 my $p_name = $patron->first_given_name .' '.$patron->family_name;
230 # try to find a suitable address for the patron
234 $e->retrieve_actor_user_address($patron->billing_address)) {
236 $e->retrieve_actor_user_address($patron->mailing_address)) {
237 $logger->warn("hold_notify: No address for user ".$patron->id);
242 unless( defined $p_addrs ) {
244 $p_addr->street1." ".
245 $p_addr->street2." ".
251 my $l_addr = $e->retrieve_actor_org_address($self->pickup_lib->holds_address);
252 my $l_addrs = (!$l_addr) ? "" :
253 $l_addr->street1." ".
254 $l_addr->street2." ".
262 if( $self->title->id == OILS_PRECAT_RECORD ) {
263 $title = ($self->copy->dummy_title) ?
264 $self->copy->dummy_title : "";
265 $author = ($self->copy->dummy_author) ?
266 $self->copy->dummy_author : "";
268 my $mods = $U->record_to_mvr($self->title);
269 $title = ($mods->title) ? $mods->title : "";
270 $author = ($mods->author) ? $mods->author : "";
275 patron_email => $self->patron->email,
276 pickup_lib_name => $o_name,
277 pickup_lib_addr => $l_addrs,
278 patron_name => $p_name,
279 patron_addr => $p_addrs,
282 call_number => $self->volume->label,
283 copy_barcode => $self->copy->barcode,
284 copy_number => $self->copy->copy_number,
292 my $template = shift;
294 unless( open(F, $template) ) {
295 $logger->error("hold_notify: Unable to open hold notification template file: $template");
299 # load the template, strip comments
306 next if $_ =~ /^\s*\#/o;
315 my( $self, $str ) = @_;
316 return undef unless $str;
318 my @time = CORE::localtime();
320 my $month = $time[4] + 1;
321 my $year = $time[5] + 1900;
323 my $data = $self->extract_data;
325 my $email = $$data{patron_email};
326 my $p_name = $$data{patron_name};
327 my $p_addr = $$data{patron_addr};
328 my $o_name = $$data{pickup_lib_name};
329 my $o_addr = $$data{pickup_lib_addr};
330 my $title = $$data{title};
331 my $author = $$data{author};
332 my $cn = $$data{call_number};
333 my $barcode = $$data{copy_barcode};
334 my $copy_number = $$data{copy_number};
336 my $sender = $self->settings_client->config_value('email_notify', 'sender_address');
337 my $reply_to = $self->pickup_lib->email;
338 $reply_to ||= $sender;
341 $str =~ s/\${EMAIL_SENDER}/$sender/;
342 $str =~ s/\${EMAIL_RECIPIENT}/$email/;
343 $str =~ s/\${EMAIL_REPLY_TO}/$reply_to/;
344 $str =~ s/\${EMAIL_HEADERS}//;
346 $str =~ s/\${DATE}/$year-$month-$day/;
347 $str =~ s/\${LIBRARY}/$o_name/;
348 $str =~ s/\${LIBRARY_ADDRESS}/$o_addr/;
349 $str =~ s/\${PATRON_NAME}/$p_name/;
350 $str =~ s/\${PATRON_ADDRESS}/$p_addr/;
352 $str =~ s/\${TITLE}/$title/;
353 $str =~ s/\${AUTHOR}/$author/;
354 $str =~ s/\${CALL_NUMBER}/$cn/;
355 $str =~ s/\${COPY_BARCODE}/$barcode/;
356 $str =~ s/\${COPY_NUMBER}/$copy_number/;