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 my $U = 'OpenILS::Application::AppUtils';
34 __PACKAGE__->register_method(
35 method => 'send_email_notify_pub',
36 api_name => 'open-ils.circ.send_hold_notify.email',
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';
56 # ---------------------------------------------------------------
57 # Define the notifier object
58 # ---------------------------------------------------------------
60 my @AUTOLOAD_FIELDS = qw/
75 my $type = ref($self) or die "$self is not an object";
80 unless (grep { $_ eq $name } @AUTOLOAD_FIELDS) {
81 $logger->error("$type: invalid autoload field: $name");
82 die "$type: invalid autoload field: $name\n"
87 *{"${type}::${name}"} = sub {
90 $s->{$name} = $v if defined $v;
94 return $self->$name($data);
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});
108 sub send_email_notify {
111 my $sc = OpenSRF::Utils::SettingsClient->new;
112 my $setting = $sc->config_value(
113 qw/ apps open-ils.circ app_settings notify_hold email / );
115 $logger->debug("hold_notify: email enabled setting = $setting");
117 if( !$setting or $setting ne 'true' ) {
118 $logger->info("hold_notify: not sending hold notify - email notifications disabled");
122 $logger->info("hold_notify: attempting email notify on hold ".$self->hold->id);
124 return OpenILS::Event->new('PATRON_NO_EMAIL_ADDRESS')
125 unless $self->patron->email and
126 $self->patron->email =~ /.+\@.+/; # see if it's remotely email-esque
128 my $sclient = OpenSRF::Utils::SettingsClient->new;
129 $self->settings_client($sclient);
130 my $template = $sclient->config_value('email_notify', 'template');
131 my $str = $self->flesh_template($self->load_template($template));
134 $logger->error("No email notifiy template found - cannot notify");
138 $logger->info("hold_notify: fleshed template: $str");
140 $self->send_email($str);
142 my $notify = Fieldmapper::action::hold_notification->new;
143 $notify->hold($self->hold->id);
144 $notify->notify_staff($self->editor->requestor->id);
145 $notify->notify_time('now');
146 $notify->method('email');
148 $self->editor->create_action_hold_notification($notify)
149 or return $self->editor->event;
155 my( $self, $text ) = @_;
157 my $smtp = $self->settings_client->config_value('email_notify', 'smtp_server');
159 $logger->info("hold_notify: sending email notice to ".
160 $self->patron->email." with SMTP server $smtp");
162 my $sender = Email::Send->new({mailer => 'SMTP'});
163 $sender->mailer_args([Host => $smtp]);
164 my $stat = $sender->send($text);
166 if( $stat->type eq 'success' ) {
167 $logger->info("hold_notify: successfully sent hold notification");
170 $logger->warn("hold_notify: unable to send hold notification: ".Dumper($stat));
178 # -------------------------------------------------------------------------
179 # Fetches all of the hold-related data
180 # -------------------------------------------------------------------------
184 my $e = $self->editor;
186 $self->hold($e->retrieve_action_hold_request($holdid)) or return $self->event($e->event);
187 $self->copy($e->retrieve_asset_copy($self->hold->current_copy)) or return $self->event($e->event);
188 $self->volume($e->retrieve_asset_call_number($self->copy->call_number)) or return $self->event($e->event);
189 $self->title($e->retrieve_biblio_record_entry($self->volume->record)) or return $self->event($e->event);
190 $self->patron($e->retrieve_actor_user($self->hold->usr)) or return $self->event($e->event);
191 $self->pickup_lib($e->retrieve_actor_org_unit($self->hold->pickup_lib)) or return $self->event($e->event);
197 my $e = $self->editor;
199 my $patron = $self->patron;
200 my $o_name = $self->pickup_lib->name;
201 my $p_name = $patron->first_given_name .' '.$patron->family_name;
203 # try to find a suitable address for the patron
207 $e->retrieve_actor_user_address($patron->billing_address)) {
209 $e->retrieve_actor_user_address($patron->mailing_address)) {
210 $logger->warn("No address for user ".$patron->id);
215 unless( defined $p_addrs ) {
217 $p_addr->street1." ".
218 $p_addr->street2." ".
224 my $l_addr = $e->retrieve_actor_org_address($self->pickup_lib->holds_address);
225 my $l_addrs = (!$l_addr) ? "" :
226 $l_addr->street1." ".
227 $l_addr->street2." ".
235 if( $self->title->id == OILS_PRECAT_RECORD ) {
236 $title = ($self->copy->dummy_title) ?
237 $self->copy->dummy_title : "";
238 $author = ($self->copy->dummy_author) ?
239 $self->copy->dummy_author : "";
241 my $mods = $U->record_to_mvr($self->title);
242 $title = ($mods->title) ? $mods->title : "";
243 $author = ($mods->author) ? $mods->author : "";
248 patron_email => $self->patron->email,
249 pickup_lib_name => $o_name,
250 pickup_lib_addr => $l_addrs,
251 patron_name => $p_name,
252 patron_addr => $p_addrs,
255 call_number => $self->volume->label,
256 copy_barcode => $self->copy->barcode,
257 copy_number => $self->copy->copy_number,
265 my $template = shift;
267 unless( open(F, $template) ) {
268 $logger->error("Unable to open hold notification template file: $template");
272 # load the template, strip comments
279 next if $_ =~ /^\s*\#/o;
288 my( $self, $str ) = @_;
289 return undef unless $str;
291 my @time = CORE::localtime();
293 my $month = $time[4] + 1;
294 my $year = $time[5] + 1900;
296 my $data = $self->extract_data;
298 my $email = $$data{patron_email};
299 my $p_name = $$data{patron_name};
300 my $p_addr = $$data{patron_addr};
301 my $o_name = $$data{pickup_lib_name};
302 my $o_addr = $$data{pickup_lib_addr};
303 my $title = $$data{title};
304 my $author = $$data{author};
305 my $cn = $$data{call_number};
306 my $barcode = $$data{copy_barcode};
307 my $copy_number = $$data{copy_number};
309 my $sender = $self->settings_client->config_value('email_notify', 'sender_address');
310 my $reply_to = $self->pickup_lib->email;
311 $reply_to ||= $sender;
314 $str =~ s/\${EMAIL_SENDER}/$sender/;
315 $str =~ s/\${EMAIL_RECIPIENT}/$email/;
316 $str =~ s/\${EMAIL_REPLY_TO}/$reply_to/;
317 $str =~ s/\${EMAIL_HEADERS}//;
319 $str =~ s/\${DATE}/$year-$month-$day/;
320 $str =~ s/\${LIBRARY}/$o_name/;
321 $str =~ s/\${LIBRARY_ADDRESS}/$o_addr/;
322 $str =~ s/\${PATRON_NAME}/$p_name/;
323 $str =~ s/\${PATRON_ADDRESS}/$p_addr/;
325 $str =~ s/\${TITLE}/$title/;
326 $str =~ s/\${AUTHOR}/$author/;
327 $str =~ s/\${CALL_NUMBER}/$cn/;
328 $str =~ s/\${COPY_BARCODE}/$barcode/;
329 $str =~ s/\${COPY_NUMBER}/$copy_number/;