From 1a8e827c54228b953ceaaf7d09af5ce389b8a8d0 Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Thu, 5 Oct 2017 17:51:59 +1300 Subject: [PATCH] Bug 19412 - Ability to set 2 new sysprefs to email OPAC purchase suggestions --- C4/Letters.pm | 43 ++++++++++++++++++++++ Koha/Patron.pm | 17 +++++++++ ...System_preferences_for_emailing_suggestions.sql | 3 ++ .../en/modules/admin/preferences/acquisitions.pref | 9 +++++ opac/opac-suggestions.pl | 9 +++++ 5 files changed, 81 insertions(+) create mode 100644 installer/data/mysql/atomicupdate/bug-19412_-_Add_System_preferences_for_emailing_suggestions.sql diff --git a/C4/Letters.pm b/C4/Letters.pm index 3f23090..b85c1f8 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -364,6 +364,49 @@ sub findrelatedto { return $result; } +=head2 SendSuggestionEmail + + + +=cut + +sub SendSuggestionEmail { + my ($type, $suggestion, $borrower_email_address, $code, ) = @_; + my $library = Koha::Libraries->find( $_->{branchcode} ); + my $userenv = C4::Context->userenv; + my $letter = GetPreparedLetter ( + module => 'suggestions', + letter_code => $code, + branchcode => $userenv->{branch}, + tables => { + 'suggestions' => $suggestion->{suggestionid}, + }, + want_librarian => 1, + ) or return { error => "no_letter" }; + + my $mail = Koha:;Email->new(); + my %mail = $email->create_message_headers ( + { + to => $borrower_email_address, + from => $library->{branchemail} || C4::Context->preference('KohaAdminEmailAddress'), + replyto => $library->{branchreplyto}, + sender => $library->{branchreturnpath}, + Subject => Encode::encode( "UTF-8", "" . $letter->{title} ), + Message => $letter->{'is_html'} + ? _wrap_html( Encode::encode( "UTF-8", $letter->{'content'} ), + Encode::encode( "UTF-8", "" . $letter->{'title'} )) + : Encode::encode( "UTF-8", "" . $letter->{'content'} ), + 'Content-Type' => $letter->{'is_html'} + ? 'text/html; charset="utf-8"' + : 'text/plain; charset="utf-8"', + ); + unless ( sendmail(%mail) ) { + carp $Mail::Sendmail::error; + return { error => $Mail::Sendmail::error }; + } +} + + =head2 SendAlerts my $err = &SendAlerts($type, $externalid, $letter_code); diff --git a/Koha/Patron.pm b/Koha/Patron.pm index daa972c..7c4fdb6 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -660,6 +660,22 @@ sub account_locked { =cut +=head3 find_borrower_with_borrowernumber +my $borrower_email_address = Koha::Patron->find_borrower_with_borrowernumber( $borrower_number ); + +This subroutine will return the email address assoicated with the borrower number parameter handed to this subroutine by opac-suggestions.pl +=cut + +sub find_borrower_with_borrowernumber { + my ($borrower_number) = @_; + my $dbh = C4::Context->dbh(); + my $borrower_email_sth = $dbh->prepare( 'SELECT email FROM borrowers WHERE borrowernumber=?'); + $borrower_email_execute = $borrower_email_sth->execute( $borrower_number ); + return $borrower_email_execute; +} + + + sub _type { return 'Borrower'; } @@ -668,6 +684,7 @@ sub _type { Kyle M Hall Alex Sassmannshausen +Alex Buckley =cut diff --git a/installer/data/mysql/atomicupdate/bug-19412_-_Add_System_preferences_for_emailing_suggestions.sql b/installer/data/mysql/atomicupdate/bug-19412_-_Add_System_preferences_for_emailing_suggestions.sql new file mode 100644 index 0000000..4081717 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug-19412_-_Add_System_preferences_for_emailing_suggestions.sql @@ -0,0 +1,3 @@ +INSERT INTO systempreferences (variable, explanation, type) VALUES ('EmailSuggestionsToBorrower', 'Input the borrower number of the borrower who will receieve a email for each patron suggestion in the OPAC', 'Free'); +INSERT INTO systempreferences (variable, value, options, explanation, type) VALUES ('EmailSuggestions', '0', NULL,'If enabled then when a patron submits a suggestion on the OPAC then it will be emailed to the borrower specified in the syspref EmailSuggestionsToBorrower as well as being displayed on the intranet mainpage', 'YesNo'); +INSERT INTO letter (module, code, name, is_html, title, content, message_transport_type) VALUES ('suggestions', 'SUGGESTION_SUBMITTED', 'Suggestion submitted', 0, 'Purchase suggestion submitted', 'Dear <> <>,\n\nA new purchase suggestion has been made on the OPAC by <><> for: <> by <>,\n\n<>', 'email'); diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/acquisitions.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/acquisitions.pref index 41bd29d..390dee9 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/acquisitions.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/acquisitions.pref @@ -75,6 +75,15 @@ Acquisitions: yes: Send no: Don't send - blind copy (BCC) to logged in user when sending serial or acquisitions claims notices. + - + - pref: EmailSuggestionsToBorrower + - Input the borrower number of the borrower who will receieve a email for each patron suggestion in the OPAC + - + - pref: EmailSuggestions + choices: + yes: Do + no: "Don't" + - If enabled then when a patron submits a suggestion on the OPAC then it will be emailed to the borrower specified in the syspref EmailSuggestionsToBorrower as well as being displayed on the intranet mainpage Printing: - diff --git a/opac/opac-suggestions.pl b/opac/opac-suggestions.pl index e086b14..420d336 100755 --- a/opac/opac-suggestions.pl +++ b/opac/opac-suggestions.pl @@ -152,6 +152,15 @@ if ( $op eq "add_confirm" ) { &NewSuggestion($suggestion); $patrons_pending_suggestions_count++; + #If the EmailSuggestions syspref is enabled then retrieve the email address associated with the borrower number stored in the EmailSuggestionsToBorrower syspref, and send the email to that address + if ( C4::Context->preference("EmailSuggestions") == 1) { + my $borrower_number = C4::Context->preference( "EmailSuggestionsToBorrower" ); + my $borrower_email_address = Koha::Patron->find_borrower_with_borrowernumber( $borrower_number ); + if ( $borrower_email_adddress ) { + C4::Letters->SendSuggestionEmail( 'suggestions', $suggestion, $borrower_email_address, 'SUGGESTION_SUBMITTED'); + } + } + # delete empty fields, to avoid filter in "SearchSuggestion" foreach my $field ( qw( title author publishercode copyrightdate place collectiontitle isbn STATUS ) ) { delete $suggestion->{$field}; #clear search filters (except borrower related) to show all suggestions after placing a new one -- 2.1.4