3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Carp qw( carp croak );
24 use Module::Load::Conditional qw( can_load );
28 use C4::Log qw( logaction );
32 use Koha::Auth::TwoFactorAuth;
33 use Koha::DateUtils qw( dt_from_string output_pref );
36 use Koha::Notice::Messages;
37 use Koha::Notice::Templates;
38 use Koha::Notice::Util;
40 use Koha::SMS::Providers;
41 use Koha::SMTP::Servers;
42 use Koha::Subscriptions;
44 use constant SERIALIZED_EMAIL_CONTENT_TYPE => 'message/rfc822';
46 our (@ISA, @EXPORT_OK);
52 GetLettersAvailableForALibrary
61 GetMessageTransportTypes
69 our $domain_limits = {};
73 C4::Letters - Give functions for Letters management
81 "Letters" is the tool used in Koha to manage informations sent to the patrons and/or the library. This include some cron jobs like
82 late issues, as well as other tasks like sending a mail to users that have subscribed to a "serial issue alert" (= being warned every time a new issue has arrived at the library)
84 Letters are managed through "alerts" sent by Koha on some events. All "alert" related functions are in this module too.
86 =head2 GetLetters([$module])
88 $letters = &GetLetters($module);
89 returns informations about letters.
90 if needed, $module filters for letters given module
92 DEPRECATED - You must use Koha::Notice::Templates instead
93 The group by clause is confusing and can lead to issues
99 my $module = $filters->{module};
100 my $code = $filters->{code};
101 my $branchcode = $filters->{branchcode};
102 my $dbh = C4::Context->dbh;
103 my $letters = $dbh->selectall_arrayref(
105 SELECT code, module, name
109 . ( $module ? q| AND module = ?| : q|| )
110 . ( $code ? q| AND code = ?| : q|| )
111 . ( defined $branchcode ? q| AND branchcode = ?| : q|| )
112 . q| GROUP BY code, module, name ORDER BY name|, { Slice => {} }
113 , ( $module ? $module : () )
114 , ( $code ? $code : () )
115 , ( defined $branchcode ? $branchcode : () )
121 =head2 GetLetterTemplates
123 my $letter_templates = GetLetterTemplates(
125 module => 'circulation',
127 branchcode => 'CPL', # '' for default,
131 Return a hashref of letter templates.
135 sub GetLetterTemplates {
138 my $module = $params->{module};
139 my $code = $params->{code};
140 my $branchcode = $params->{branchcode} // '';
141 my $dbh = C4::Context->dbh;
142 return Koha::Notice::Templates->search(
146 branchcode => $branchcode,
148 C4::Context->preference('TranslateNotices')
150 : ( lang => 'default' )
156 =head2 GetLettersAvailableForALibrary
158 my $letters = GetLettersAvailableForALibrary(
160 branchcode => 'CPL', # '' for default
161 module => 'circulation',
165 Return an arrayref of letters, sorted by name.
166 If a specific letter exist for the given branchcode, it will be retrieve.
167 Otherwise the default letter will be.
171 sub GetLettersAvailableForALibrary {
173 my $branchcode = $filters->{branchcode};
174 my $module = $filters->{module};
176 croak "module should be provided" unless $module;
178 my $dbh = C4::Context->dbh;
179 my $default_letters = $dbh->selectall_arrayref(
181 SELECT module, code, branchcode, name
185 . q| AND branchcode = ''|
186 . ( $module ? q| AND module = ?| : q|| )
187 . q| ORDER BY name|, { Slice => {} }
188 , ( $module ? $module : () )
191 my $specific_letters;
193 $specific_letters = $dbh->selectall_arrayref(
195 SELECT module, code, branchcode, name
199 . q| AND branchcode = ?|
200 . ( $module ? q| AND module = ?| : q|| )
201 . q| ORDER BY name|, { Slice => {} }
203 , ( $module ? $module : () )
208 for my $l (@$default_letters) {
209 $letters{ $l->{code} } = $l;
211 for my $l (@$specific_letters) {
212 # Overwrite the default letter with the specific one.
213 $letters{ $l->{code} } = $l;
216 return [ map { $letters{$_} }
217 sort { $letters{$a}->{name} cmp $letters{$b}->{name} }
227 module => 'circulation',
233 Delete the letter. The mtt parameter is facultative.
234 If not given, all templates mathing the other parameters will be removed.
240 my $branchcode = $params->{branchcode};
241 my $module = $params->{module};
242 my $code = $params->{code};
243 my $mtt = $params->{mtt};
244 my $lang = $params->{lang};
245 my $dbh = C4::Context->dbh;
252 . ( $mtt ? q| AND message_transport_type = ?| : q|| )
253 . ( $lang? q| AND lang = ?| : q|| )
254 , undef, $branchcode, $module, $code, ( $mtt ? $mtt : () ), ( $lang ? $lang : () ) );
259 my $err = &SendAlerts($type, $externalid, $letter_code);
262 - $type : the type of alert
263 - $externalid : the id of the "object" to query
264 - $letter_code : the notice template to use
266 C<&SendAlerts> sends an email notice directly to a patron or a vendor.
268 Currently it supports ($type):
269 - claim serial issues (claimissues)
270 - claim acquisition orders (claimacquisition)
271 - send acquisition orders to the vendor (orderacquisition)
272 - notify patrons about newly received serial issues (issue)
273 - notify patrons when their account is created (members)
275 Returns undef or { error => 'message } on failure.
276 Returns true on success.
281 my ( $type, $externalid, $letter_code ) = @_;
282 my $dbh = C4::Context->dbh;
285 if ( $type eq 'issue' ) {
287 # prepare the letter...
288 # search the subscriptionid
291 "SELECT subscriptionid FROM serial WHERE serialid=?");
292 $sth->execute($externalid);
293 my ($subscriptionid) = $sth->fetchrow
294 or warn( "No subscription for '$externalid'" ),
297 # search the biblionumber
300 "SELECT biblionumber FROM subscription WHERE subscriptionid=?");
301 $sth->execute($subscriptionid);
302 my ($biblionumber) = $sth->fetchrow
303 or warn( "No biblionumber for '$subscriptionid'" ),
306 # find the list of subscribers to notify
307 my $subscription = Koha::Subscriptions->find( $subscriptionid );
308 my $subscribers = $subscription->subscribers;
309 while ( my $patron = $subscribers->next ) {
310 my $email = $patron->email or next;
312 # warn "sending issues...";
313 my $userenv = C4::Context->userenv;
314 my $library = $patron->library;
315 my $letter = GetPreparedLetter (
317 letter_code => $letter_code,
318 branchcode => $userenv->{branch},
320 'branches' => $library->branchcode,
321 'biblio' => $biblionumber,
322 'biblioitems' => $biblionumber,
323 'borrowers' => $patron->unblessed,
324 'subscription' => $subscriptionid,
325 'serial' => $externalid,
330 # FIXME: This 'default' behaviour should be moved to Koha::Email
331 my $mail = Koha::Email->create(
334 from => $library->branchemail,
335 reply_to => $library->branchreplyto,
336 sender => $library->branchreturnpath,
337 subject => "" . $letter->{title},
341 if ( $letter->{is_html} ) {
342 $mail->html_body( _wrap_html( $letter->{content}, "" . $letter->{title} ) );
345 $mail->text_body( $letter->{content} );
349 $mail->send_or_die({ transport => $library->smtp_server->transport });
352 # We expect ref($_) eq 'Email::Sender::Failure'
353 $error = $_->message;
359 return { error => $error }
363 elsif ( $type eq 'claimacquisition' or $type eq 'claimissues' or $type eq 'orderacquisition' ) {
365 # prepare the letter...
372 if ( $type eq 'claimacquisition') {
374 SELECT aqorders.*,aqbasket.*,biblio.*,biblioitems.*
376 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
377 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
378 LEFT JOIN biblioitems ON aqorders.biblionumber=biblioitems.biblionumber
379 WHERE aqorders.ordernumber IN (
383 carp "No order selected";
384 return { error => "no_order_selected" };
386 $strsth .= join( ",", ('?') x @$externalid ) . ")";
387 $action = "ACQUISITION CLAIM";
388 $sthorders = $dbh->prepare($strsth);
389 $sthorders->execute( @$externalid );
390 $dataorders = $sthorders->fetchall_arrayref( {} );
393 if ($type eq 'claimissues') {
395 SELECT serial.*,subscription.*, biblio.*, biblioitems.*, aqbooksellers.*,
396 aqbooksellers.id AS booksellerid
398 LEFT JOIN subscription ON serial.subscriptionid=subscription.subscriptionid
399 LEFT JOIN biblio ON serial.biblionumber=biblio.biblionumber
400 LEFT JOIN biblioitems ON serial.biblionumber = biblioitems.biblionumber
401 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
402 WHERE serial.serialid IN (
406 carp "No issues selected";
407 return { error => "no_issues_selected" };
410 $strsth .= join( ",", ('?') x @$externalid ) . ")";
411 $action = "SERIAL CLAIM";
412 $sthorders = $dbh->prepare($strsth);
413 $sthorders->execute( @$externalid );
414 $dataorders = $sthorders->fetchall_arrayref( {} );
417 if ( $type eq 'orderacquisition') {
418 $basketno = $externalid;
420 SELECT aqorders.*,aqbasket.*,biblio.*,biblioitems.*
422 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
423 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
424 LEFT JOIN biblioitems ON aqorders.biblionumber=biblioitems.biblionumber
425 WHERE aqbasket.basketno = ?
426 AND orderstatus IN ('new','ordered')
429 unless ( $basketno ) {
430 carp "No basketnumber given";
431 return { error => "no_basketno" };
433 $action = "ACQUISITION ORDER";
434 $sthorders = $dbh->prepare($strsth);
435 $sthorders->execute($basketno);
436 $dataorders = $sthorders->fetchall_arrayref( {} );
438 aqorders => [ map { $_->{ordernumber} } @$dataorders ]
442 my $booksellerid = $dataorders->[0]->{booksellerid};
443 my $addressee = $type eq 'claimacquisition' || $type eq 'orderacquisition' ? 'acqprimary' : 'serialsprimary';
446 $dbh->prepare("SELECT * FROM aqcontacts WHERE booksellerid=? AND $type=1 ORDER BY $addressee DESC");
447 $sthcontact->execute( $booksellerid );
448 my $datacontact = $sthcontact->fetchrow_hashref;
452 push @email, $datacontact->{email} if ( $datacontact && $datacontact->{email} );
454 warn "Bookseller $booksellerid without emails";
455 return { error => "no_email" };
458 while ($addlcontact = $sthcontact->fetchrow_hashref) {
459 push @cc, $addlcontact->{email} if ( $addlcontact && $addlcontact->{email} );
462 my $userenv = C4::Context->userenv;
463 my $letter = GetPreparedLetter (
465 letter_code => $letter_code,
466 branchcode => $userenv->{branch},
468 'branches' => $userenv->{branch},
469 'aqbooksellers' => $booksellerid,
470 'aqcontacts' => $datacontact,
471 'aqbasket' => $basketno,
473 repeat => $dataorders,
476 ) or return { error => "no_letter" };
478 # Remove the order tag
479 $letter->{content} =~ s/<order>(.*?)<\/order>/$1/gxms;
482 my $library = Koha::Libraries->find( $userenv->{branch} );
483 my $mail = Koha::Email->create(
485 to => join( ',', @email ),
486 cc => join( ',', @cc ),
489 C4::Context->preference("ClaimsBccCopy")
490 && ( $type eq 'claimacquisition'
491 || $type eq 'claimissues' )
493 ? ( bcc => $userenv->{emailaddress} )
496 from => $library->branchemail
497 || C4::Context->preference('KohaAdminEmailAddress'),
498 subject => "" . $letter->{title},
502 if ( $letter->{is_html} ) {
503 $mail->html_body( _wrap_html( $letter->{content}, "" . $letter->{title} ) );
506 $mail->text_body( "" . $letter->{content} );
510 $mail->send_or_die({ transport => $library->smtp_server->transport });
513 # We expect ref($_) eq 'Email::Sender::Failure'
514 $error = $_->message;
520 return { error => $error }
523 my $log_object = $action eq 'ACQUISITION ORDER' ? $externalid : undef;
524 my $module = $action eq 'ACQUISITION ORDER' ? 'ACQUISITIONS' : 'CLAIMS';
530 . join( ',', @email )
535 ) if C4::Context->preference("ClaimsLog");
538 # If we come here, return an OK status
542 =head2 GetPreparedLetter( %params )
545 module => letter module, mandatory
546 letter_code => letter code, mandatory
547 branchcode => for letter selection, if missing default system letter taken
548 tables => a hashref with table names as keys. Values are either:
549 - a scalar - primary key value
550 - an arrayref - primary key values
551 - a hashref - full record
552 substitute => custom substitution key/value pairs
553 repeat => records to be substituted on consecutive lines:
554 - an arrayref - tries to guess what needs substituting by
555 taking remaining << >> tokensr; not recommended
556 - a hashref token => @tables - replaces <token> << >> << >> </token>
557 subtemplate for each @tables row; table is a hashref as above
558 want_librarian => boolean, if set to true triggers librarian details
559 substitution from the userenv
561 letter fields hashref (title & content useful)
565 sub GetPreparedLetter {
568 my $letter = $params{letter};
569 my $lang = $params{lang} || 'default';
572 my $module = $params{module} or croak "No module";
573 my $letter_code = $params{letter_code} or croak "No letter_code";
574 my $branchcode = $params{branchcode} || '';
575 my $mtt = $params{message_transport_type} || 'email';
577 my $template = Koha::Notice::Templates->find_effective_template(
580 code => $letter_code,
581 branchcode => $branchcode,
582 message_transport_type => $mtt,
587 unless ( $template ) {
588 warn( "No $module $letter_code letter transported by " . $mtt );
592 $letter = $template->unblessed;
593 $letter->{'content-type'} = 'text/html; charset="UTF-8"' if $letter->{is_html};
596 my $objects = $params{objects} || {};
597 my $tables = $params{tables} || {};
598 my $substitute = $params{substitute} || {};
599 my $loops = $params{loops} || {}; # loops is not supported for historical notices syntax
600 my $repeat = $params{repeat};
601 %$tables || %$substitute || $repeat || %$loops || %$objects
602 or carp( "ERROR: nothing to substitute - all of 'objects', 'tables', 'loops' and 'substitute' are empty" ),
604 my $want_librarian = $params{want_librarian};
607 while ( my ($token, $val) = each %$substitute ) {
609 if ( $token eq 'items.content' ) {
610 $val =~ s|\n|<br/>|g if $letter->{is_html};
613 $letter->{title} =~ s/<<$token>>/$val/g;
614 $letter->{content} =~ s/<<$token>>/$val/g;
618 my $OPACBaseURL = C4::Context->preference('OPACBaseURL');
619 $letter->{content} =~ s/<<OPACBaseURL>>/$OPACBaseURL/go;
621 if ($want_librarian) {
622 # parsing librarian name
623 my $userenv = C4::Context->userenv;
624 $letter->{content} =~ s/<<LibrarianFirstname>>/$userenv->{firstname}/go;
625 $letter->{content} =~ s/<<LibrarianSurname>>/$userenv->{surname}/go;
626 $letter->{content} =~ s/<<LibrarianEmailaddress>>/$userenv->{emailaddress}/go;
629 my ($repeat_no_enclosing_tags, $repeat_enclosing_tags);
632 if (ref ($repeat) eq 'ARRAY' ) {
633 $repeat_no_enclosing_tags = $repeat;
635 $repeat_enclosing_tags = $repeat;
639 if ($repeat_enclosing_tags) {
640 while ( my ($tag, $tag_tables) = each %$repeat_enclosing_tags ) {
641 if ( $letter->{content} =~ m!<$tag>(.*)</$tag>!s ) {
644 my %subletter = ( title => '', content => $subcontent );
645 _substitute_tables( \%subletter, $_ );
648 $letter->{content} =~ s!<$tag>.*</$tag>!join( "\n", @lines )!se;
654 _substitute_tables( $letter, $tables );
657 if ($repeat_no_enclosing_tags) {
658 if ( $letter->{content} =~ m/[^\n]*<<.*>>[^\n]*/so ) {
663 $c =~ s/<<count>>/$i/go;
664 foreach my $field ( keys %{$_} ) {
665 $c =~ s/(<<[^\.]+.$field>>)/$_->{$field}/;
669 } @$repeat_no_enclosing_tags;
671 my $replaceby = join( "\n", @lines );
672 $letter->{content} =~ s/\Q$line\E/$replaceby/s;
676 $letter->{content} = _process_tt(
678 content => $letter->{content},
682 substitute => $substitute,
687 $letter->{title} = _process_tt(
689 content => $letter->{title},
693 substitute => $substitute,
698 $letter->{content} =~ s/<<\S*>>//go; #remove any stragglers
703 sub _substitute_tables {
704 my ( $letter, $tables ) = @_;
705 while ( my ($table, $param) = each %$tables ) {
708 my $ref = ref $param;
711 if ($ref && $ref eq 'HASH') {
715 my $sth = _parseletter_sth($table);
717 warn "_parseletter_sth('$table') failed to return a valid sth. No substitution will be done for that table.";
720 $sth->execute( $ref ? @$param : $param );
722 $values = $sth->fetchrow_hashref;
726 _parseletter ( $letter, $table, $values );
730 sub _parseletter_sth {
734 carp "ERROR: _parseletter_sth() called without argument (table)";
737 # NOTE: we used to check whether we had a statement handle cached in
738 # a %handles module-level variable. This was a dumb move and
739 # broke things for the rest of us. prepare_cached is a better
740 # way to cache statement handles anyway.
742 ($table eq 'accountlines' ) ? "SELECT * FROM $table WHERE accountlines_id = ?" :
743 ($table eq 'biblio' ) ? "SELECT * FROM $table WHERE biblionumber = ?" :
744 ($table eq 'biblioitems' ) ? "SELECT * FROM $table WHERE biblionumber = ?" :
745 ($table eq 'tickets' ) ? "SELECT * FROM $table WHERE id = ?" :
746 ($table eq 'ticket_updates' ) ? "SELECT * FROM $table WHERE id = ?" :
747 ($table eq 'credits' ) ? "SELECT * FROM accountlines WHERE accountlines_id = ?" :
748 ($table eq 'debits' ) ? "SELECT * FROM accountlines WHERE accountlines_id = ?" :
749 ($table eq 'items' ) ? "SELECT * FROM $table WHERE itemnumber = ?" :
750 ($table eq 'issues' ) ? "SELECT * FROM $table WHERE itemnumber = ?" :
751 ($table eq 'old_issues' ) ? "SELECT * FROM $table WHERE issue_id = ?" :
752 ($table eq 'reserves' ) ? "SELECT * FROM $table WHERE borrowernumber = ? and biblionumber = ?" :
753 ($table eq 'borrowers' ) ? "SELECT * FROM $table WHERE borrowernumber = ?" :
754 ($table eq 'branches' ) ? "SELECT * FROM $table WHERE branchcode = ?" :
755 ($table eq 'suggestions' ) ? "SELECT * FROM $table WHERE suggestionid = ?" :
756 ($table eq 'aqbooksellers') ? "SELECT * FROM $table WHERE id = ?" :
757 ($table eq 'aqorders' ) ? "SELECT * FROM $table WHERE ordernumber = ?" :
758 ($table eq 'aqbasket' ) ? "SELECT * FROM $table WHERE basketno = ?" :
759 ($table eq 'illrequests' ) ? "SELECT * FROM $table WHERE illrequest_id = ?" :
760 ($table eq 'article_requests') ? "SELECT * FROM $table WHERE id = ?" :
761 ($table eq 'borrower_modifications') ? "SELECT * FROM $table WHERE verification_token = ?" :
762 ($table eq 'subscription') ? "SELECT * FROM $table WHERE subscriptionid = ?" :
763 ($table eq 'serial') ? "SELECT * FROM $table WHERE serialid = ?" :
764 ($table eq 'problem_reports') ? "SELECT * FROM $table WHERE reportid = ?" :
765 ($table eq 'additional_contents' || $table eq 'opac_news') ? "SELECT * FROM additional_contents WHERE idnew = ?" :
766 ($table eq 'recalls') ? "SELECT * FROM $table WHERE recall_id = ?" :
769 warn "ERROR: No _parseletter_sth query for table '$table'";
770 return; # nothing to get
772 unless ($sth = C4::Context->dbh->prepare_cached($query)) {
773 warn "ERROR: Failed to prepare query: '$query'";
776 return $sth; # now cache is populated for that $table
779 =head2 _parseletter($letter, $table, $values)
782 - $letter : a hash to letter fields (title & content useful)
783 - $table : the Koha table to parse.
784 - $values_in : table record hashref
785 parse all fields from a table, and replace values in title & content with the appropriate value
786 (not exported sub, used only internally)
791 my ( $letter, $table, $values_in ) = @_;
793 # Work on a local copy of $values_in (passed by reference) to avoid side effects
794 # in callers ( by changing / formatting values )
795 my $values = $values_in ? { %$values_in } : {};
797 # FIXME Dates formatting must be done in notice's templates
798 if ( $table eq 'borrowers' && $values->{'dateexpiry'} ){
799 $values->{'dateexpiry'} = output_pref({ dt => dt_from_string( $values->{'dateexpiry'} ), dateonly => 1 });
802 if ( $table eq 'reserves' && $values->{'waitingdate'} ) {
803 $values->{'waitingdate'} = output_pref({ dt => dt_from_string( $values->{'waitingdate'} ), dateonly => 1 });
806 if ($letter->{content} && $letter->{content} =~ /<<today>>/) {
807 my $todaysdate = output_pref( dt_from_string() );
808 $letter->{content} =~ s/<<today>>/$todaysdate/go;
811 while ( my ($field, $val) = each %$values ) {
812 $val =~ s/\p{P}$// if $val && $table=~/biblio/;
813 #BZ 9886: Assuming that we want to eliminate ISBD punctuation here
814 #Therefore adding the test on biblio. This includes biblioitems,
815 #but excludes items. Removed unneeded global and lookahead.
817 if ( $table=~/^borrowers$/ && $field=~/^streettype$/ ) {
818 my $av = Koha::AuthorisedValues->search({ category => 'ROADTYPE', authorised_value => $val });
819 $val = $av->count ? $av->next->lib : '';
823 my $replacedby = defined ($val) ? $val : '';
825 and not $replacedby =~ m|9999-12-31|
826 and $replacedby =~ m|^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}:\d{2})?$| )
828 # If the value is XXXX-YY-ZZ[ AA:BB:CC] we assume it is a date
829 my $dateonly = defined $1 ? 0 : 1; #$1 refers to the capture group wrapped in parentheses. In this case, that's the hours, minutes, seconds.
830 my $re_dateonly_filter = qr{ $field( \s* \| \s* dateonly\s*)?>> }xms;
832 for my $letter_field ( qw( title content ) ) {
833 my $filter_string_used = q{};
834 if ( $letter->{ $letter_field } =~ $re_dateonly_filter ) {
835 # We overwrite $dateonly if the filter exists and we have a time in the datetime
836 $filter_string_used = $1 || q{};
837 $dateonly = $1 unless $dateonly;
839 my $replacedby_date = eval {
840 output_pref({ dt => scalar dt_from_string( $replacedby ), dateonly => $dateonly });
842 $replacedby_date //= q{};
844 if ( $letter->{ $letter_field } ) {
845 $letter->{ $letter_field } =~ s/\Q<<$table.$field$filter_string_used>>\E/$replacedby_date/g;
846 $letter->{ $letter_field } =~ s/\Q<<$field$filter_string_used>>\E/$replacedby_date/g;
850 # Other fields replacement
852 for my $letter_field ( qw( title content ) ) {
853 if ( $letter->{ $letter_field } ) {
854 $letter->{ $letter_field } =~ s/<<$table.$field>>/$replacedby/g;
855 $letter->{ $letter_field } =~ s/<<$field>>/$replacedby/g;
861 if ($table eq 'borrowers' && $letter->{content}) {
862 my $patron = Koha::Patrons->find( $values->{borrowernumber} );
864 my $attributes = $patron->extended_attributes;
866 while ( my $attribute = $attributes->next ) {
867 my $code = $attribute->code;
868 my $val = $attribute->description; # FIXME - we always display intranet description here!
869 $val =~ s/\p{P}(?=$)//g if $val;
870 next unless $val gt '';
872 push @{ $attr{$code} }, $val;
874 while ( my ($code, $val_ar) = each %attr ) {
875 my $replacefield = "<<borrower-attribute:$code>>";
876 my $replacedby = join ',', @$val_ar;
877 $letter->{content} =~ s/$replacefield/$replacedby/g;
886 my $success = EnqueueLetter( { letter => $letter,
887 borrowernumber => '12', message_transport_type => 'email' } )
889 Places a letter in the message_queue database table, which will
890 eventually get processed (sent) by the process_message_queue.pl
891 cronjob when it calls SendQueuedMessages.
893 Return message_id on success
896 * letter - required; A letter hashref as returned from GetPreparedLetter
897 * message_transport_type - required; One of the available mtts
898 * borrowernumber - optional if 'to_address' is passed; The borrowernumber of the patron we enqueuing the notice for
899 * to_address - optional if 'borrowernumber' is passed; The destination email address for the notice (defaults to patron->notice_email_address)
900 * from_address - optional; The from address for the notice, defaults to patron->library->from_email_address
901 * reply_address - optional; The reply address for the notice, defaults to patron->library->reply_to
906 my $params = shift or return;
908 return unless exists $params->{'letter'};
909 # return unless exists $params->{'borrowernumber'};
910 return unless exists $params->{'message_transport_type'};
912 my $content = $params->{letter}->{content};
913 $content =~ s/\s+//g if(defined $content);
914 if ( not defined $content or $content eq '' ) {
915 Koha::Logger->get->info("Trying to add an empty message to the message queue");
919 # If we have any attachments we should encode then into the body.
920 if ( $params->{'attachments'} ) {
921 $params->{'letter'} = _add_attachments(
922 { letter => $params->{'letter'},
923 attachments => $params->{'attachments'},
928 my $dbh = C4::Context->dbh();
929 my $statement = << 'ENDSQL';
930 INSERT INTO message_queue
931 ( letter_id, borrowernumber, subject, content, metadata, letter_code, message_transport_type, status, time_queued, to_address, from_address, reply_address, content_type, failure_code )
932 VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, CAST(NOW() AS DATETIME), ?, ?, ?, ?, ? )
935 my $sth = $dbh->prepare($statement);
936 my $result = $sth->execute(
937 $params->{letter}->{id} || undef, # letter.id
938 $params->{'borrowernumber'}, # borrowernumber
939 $params->{'letter'}->{'title'}, # subject
940 $params->{'letter'}->{'content'}, # content
941 $params->{'letter'}->{'metadata'} || '', # metadata
942 $params->{'letter'}->{'code'} || '', # letter_code
943 $params->{'message_transport_type'}, # message_transport_type
945 $params->{'to_address'}, # to_address
946 $params->{'from_address'}, # from_address
947 $params->{'reply_address'}, # reply_address
948 $params->{'letter'}->{'content-type'}, # content_type
949 $params->{'failure_code'} || '', # failure_code
951 return $dbh->last_insert_id(undef,undef,'message_queue', undef);
954 =head2 SendQueuedMessages ([$hashref])
956 my $sent = SendQueuedMessages({
958 borrowernumber => $who_letter_is_for,
959 letter_code => $letter_code, # can be scalar or arrayref
960 type => $type, # can be scalar or arrayref
966 Sends 'pending' messages from the queue, based on parameters.
968 The (optional) message_id, borrowernumber, letter_code, type and where
969 parameter are used to select which pending messages will be processed. The
970 limit parameter determines the volume of results, i.e. sent messages.
972 The optional verbose parameter can be used to generate debugging output.
974 Returns number of messages sent.
978 sub SendQueuedMessages {
980 my $limit = $params->{limit};
981 my $where = $params->{where};
983 Koha::Exceptions::BadParameter->throw("Parameter message_id cannot be empty if passed.")
984 if ( exists( $params->{message_id} ) && !$params->{message_id} );
986 my $smtp_transports = {};
988 my $count_messages = 0;
989 my $unsent_messages = Koha::Notice::Messages->search({
991 $params->{message_id} ? ( message_id => $params->{message_id} ) : (),
992 $params->{borrowernumber} ? ( borrowernumber => $params->{borrowernumber} ) : (),
993 # Check for scalar or array in letter_code and type
994 ref($params->{letter_code}) && @{$params->{letter_code}} ? ( letter_code => $params->{letter_code} ) : (),
995 !ref($params->{letter_code}) && $params->{letter_code} ? ( letter_code => $params->{letter_code} ) : (),
996 ref($params->{type}) && @{$params->{type}} ? ( message_transport_type => $params->{type} ) : (), #TODO Existing inconsistency
997 !ref($params->{type}) && $params->{type} ? ( message_transport_type => $params->{type} ) : (), #TODO Existing inconsistency
999 $unsent_messages = $unsent_messages->search( \$where ) if $where;
1001 $domain_limits = Koha::Notice::Util->load_domain_limits; # (re)initialize per run
1002 while( ( my $message_object = $unsent_messages->next ) && ( !$limit || $count_messages < $limit ) ) {
1003 my $message = $message_object->unblessed;
1005 # If this fails the database is unwritable and we won't manage to send a message that continues to be marked 'pending'
1006 $message_object->make_column_dirty('status');
1007 return unless $message_object->store;
1009 # warn Data::Dumper->Dump( [ $message ], [ 'message' ] );
1010 warn sprintf( 'Processing %s message to patron: %s',
1011 $message->{'message_transport_type'},
1012 $message->{'borrowernumber'} || 'Admin' )
1013 if $params->{'verbose'};
1014 # This is just begging for subclassing
1015 next if ( lc($message->{'message_transport_type'}) eq 'rss' );
1016 if ( lc( $message->{'message_transport_type'} ) eq 'email' ) {
1017 my $rv = _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'}, $smtp_transports );
1018 $count_messages++ if $rv;
1020 elsif ( lc( $message->{'message_transport_type'} ) eq 'sms' ) {
1021 if ( C4::Context->preference('SMSSendDriver') eq 'Email' ) {
1022 my $patron = Koha::Patrons->find( $message->{borrowernumber} );
1023 my $sms_provider = Koha::SMS::Providers->find( $patron->sms_provider_id );
1024 unless ( $sms_provider ) {
1025 warn sprintf( "Patron %s has no sms provider id set!", $message->{'borrowernumber'} ) if $params->{'verbose'};
1026 _set_message_status( { message_id => $message->{'message_id'}, status => 'failed' } );
1029 unless ( $patron->smsalertnumber ) {
1030 _set_message_status( { message_id => $message->{'message_id'}, status => 'failed' } );
1031 warn sprintf( "No smsalertnumber found for patron %s!", $message->{'borrowernumber'} ) if $params->{'verbose'};
1034 $message->{to_address} = $patron->smsalertnumber; #Sometime this is set to email - sms should always use smsalertnumber
1035 $message->{to_address} .= '@' . $sms_provider->domain();
1037 # Check for possible from_address override
1038 my $from_address = C4::Context->preference('EmailSMSSendDriverFromAddress');
1039 if ($from_address && $message->{from_address} ne $from_address) {
1040 $message->{from_address} = $from_address;
1041 _update_message_from_address($message->{'message_id'}, $message->{from_address});
1044 _update_message_to_address($message->{'message_id'}, $message->{to_address});
1045 my $rv = _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
1046 $count_messages++ if $rv;
1048 my $rv = _send_message_by_sms( $message );
1049 $count_messages++ if $rv;
1053 return $count_messages;
1056 =head2 GetRSSMessages
1058 my $message_list = GetRSSMessages( { limit => 10, borrowernumber => '14' } )
1060 returns a listref of all queued RSS messages for a particular person.
1064 sub GetRSSMessages {
1067 return unless $params;
1068 return unless ref $params;
1069 return unless $params->{'borrowernumber'};
1071 return _get_unsent_messages( { message_transport_type => 'rss',
1072 limit => $params->{'limit'},
1073 borrowernumber => $params->{'borrowernumber'}, } );
1076 =head2 GetPrintMessages
1078 my $message_list = GetPrintMessages( { borrowernumber => $borrowernumber } )
1080 Returns a arrayref of all queued print messages (optionally, for a particular
1085 sub GetPrintMessages {
1086 my $params = shift || {};
1088 return _get_unsent_messages( { message_transport_type => 'print',
1089 borrowernumber => $params->{'borrowernumber'},
1093 =head2 GetQueuedMessages ([$hashref])
1095 my $messages = GetQueuedMessage( { borrowernumber => '123', limit => 20 } );
1097 Fetches a list of messages from the message queue optionally filtered by borrowernumber
1098 and limited to specified limit.
1100 Return is an arrayref of hashes, each has represents a message in the message queue.
1104 sub GetQueuedMessages {
1107 my $dbh = C4::Context->dbh();
1108 my $statement = << 'ENDSQL';
1109 SELECT message_id, borrowernumber, subject, content, message_transport_type, status, time_queued, updated_on, failure_code
1115 if ( exists $params->{'borrowernumber'} ) {
1116 push @whereclauses, ' borrowernumber = ? ';
1117 push @query_params, $params->{'borrowernumber'};
1120 if ( @whereclauses ) {
1121 $statement .= ' WHERE ' . join( 'AND', @whereclauses );
1124 if ( defined $params->{'limit'} ) {
1125 $statement .= ' LIMIT ? ';
1126 push @query_params, $params->{'limit'};
1129 my $sth = $dbh->prepare( $statement );
1130 my $result = $sth->execute( @query_params );
1131 return $sth->fetchall_arrayref({});
1134 =head2 GetMessageTransportTypes
1136 my @mtt = GetMessageTransportTypes();
1138 returns an arrayref of transport types
1142 sub GetMessageTransportTypes {
1143 my $dbh = C4::Context->dbh();
1144 my $mtts = $dbh->selectcol_arrayref("
1145 SELECT message_transport_type
1146 FROM message_transport_types
1147 ORDER BY message_transport_type
1154 my $message = C4::Letters::Message($message_id);
1159 my ( $message_id ) = @_;
1160 return unless $message_id;
1161 my $dbh = C4::Context->dbh;
1162 return $dbh->selectrow_hashref(q|
1163 SELECT message_id, borrowernumber, subject, content, metadata, letter_code, message_transport_type, status, time_queued, updated_on, to_address, from_address, reply_address, content_type, failure_code
1165 WHERE message_id = ?
1166 |, {}, $message_id );
1169 =head2 ResendMessage
1171 Attempt to resend a message which has failed previously.
1173 my $has_been_resent = C4::Letters::ResendMessage($message_id);
1175 Updates the message to 'pending' status so that
1176 it will be resent later on.
1178 returns 1 on success, 0 on failure, undef if no message was found
1183 my $message_id = shift;
1184 return unless $message_id;
1186 my $message = GetMessage( $message_id );
1187 return unless $message;
1189 if ( $message->{status} ne 'pending' ) {
1190 $rv = C4::Letters::_set_message_status({
1191 message_id => $message_id,
1192 status => 'pending',
1194 $rv = $rv > 0? 1: 0;
1195 # Clear destination email address to force address update
1196 _update_message_to_address( $message_id, undef ) if $rv &&
1197 $message->{message_transport_type} eq 'email';
1202 =head2 _add_attachements
1204 _add_attachments({ letter => $letter, attachments => $attachments });
1207 letter - the standard letter hashref
1208 attachments - listref of attachments. each attachment is a hashref of:
1209 type - the mime type, like 'text/plain'
1210 content - the actual attachment
1211 filename - the name of the attachment.
1213 returns your letter object, with the content updated.
1214 This routine picks the I<content> of I<letter> and generates a MIME
1215 email, attaching the passed I<attachments> using Koha::Email. The
1216 content is replaced by the string representation of the MIME object,
1217 and the content-type is updated for later handling.
1221 sub _add_attachments {
1224 my $letter = $params->{letter};
1225 my $attachments = $params->{attachments};
1226 return $letter unless @$attachments;
1228 my $message = Koha::Email->new;
1230 if ( $letter->{is_html} ) {
1231 $message->html_body( _wrap_html( $letter->{content}, $letter->{title} ) );
1234 $message->text_body( $letter->{content} );
1237 foreach my $attachment ( @$attachments ) {
1239 Encode::encode( "UTF-8", $attachment->{content} ),
1240 content_type => $attachment->{type} || 'application/octet-stream',
1241 name => $attachment->{filename},
1242 disposition => 'attachment',
1246 $letter->{'content-type'} = SERIALIZED_EMAIL_CONTENT_TYPE;
1247 $letter->{content} = $message->as_string;
1253 =head2 _get_unsent_messages
1255 This function's parameter hash reference takes the following
1256 optional named parameters:
1257 message_transport_type: method of message sending (e.g. email, sms, etc.)
1258 Can be a single string, or an arrayref of strings
1259 borrowernumber : who the message is to be sent
1260 letter_code : type of message being sent (e.g. PASSWORD_RESET)
1261 Can be a single string, or an arrayref of strings
1262 message_id : the message_id of the message. In that case the sub will return only 1 result
1263 limit : maximum number of messages to send
1265 This function returns an array of matching hash referenced rows from
1266 message_queue with some borrower information added.
1270 sub _get_unsent_messages {
1273 my $dbh = C4::Context->dbh();
1275 SELECT mq.message_id, mq.borrowernumber, mq.subject, mq.content, mq.message_transport_type, mq.status, mq.time_queued, mq.from_address, mq.reply_address, mq.to_address, mq.content_type, b.branchcode, mq.letter_code, mq.failure_code
1276 FROM message_queue mq
1277 LEFT JOIN borrowers b ON b.borrowernumber = mq.borrowernumber
1281 my @query_params = ('pending');
1282 if ( ref $params ) {
1283 if ( $params->{'borrowernumber'} ) {
1284 $statement .= ' AND mq.borrowernumber = ? ';
1285 push @query_params, $params->{'borrowernumber'};
1287 if ( $params->{'letter_code'} ) {
1288 my @letter_codes = ref $params->{'letter_code'} eq "ARRAY" ? @{$params->{'letter_code'}} : $params->{'letter_code'};
1289 if ( @letter_codes ) {
1290 my $q = join( ",", ("?") x @letter_codes );
1291 $statement .= " AND mq.letter_code IN ( $q ) ";
1292 push @query_params, @letter_codes;
1295 if ( $params->{'message_transport_type'} ) {
1296 my @types = ref $params->{'message_transport_type'} eq "ARRAY" ? @{$params->{'message_transport_type'}} : $params->{'message_transport_type'};
1298 my $q = join( ",", ("?") x @types );
1299 $statement .= " AND message_transport_type IN ( $q ) ";
1300 push @query_params, @types;
1303 if ( $params->{message_id} ) {
1304 $statement .= ' AND message_id = ?';
1305 push @query_params, $params->{message_id};
1307 if ( $params->{where} ) {
1308 $statement .= " AND $params->{where} ";
1310 if ( $params->{'limit'} ) {
1311 $statement .= ' limit ? ';
1312 push @query_params, $params->{'limit'};
1316 my $sth = $dbh->prepare( $statement );
1317 my $result = $sth->execute( @query_params );
1318 return $sth->fetchall_arrayref({});
1321 sub _send_message_by_email {
1322 my $message = shift or return;
1323 my ( $username, $password, $method, $smtp_transports ) = @_;
1326 my $to_address = $message->{'to_address'};
1327 unless ($to_address) {
1328 $patron = Koha::Patrons->find( $message->{borrowernumber} );
1330 warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})";
1331 _set_message_status(
1333 message_id => $message->{'message_id'},
1335 failure_code => 'INVALID_BORNUMBER'
1340 $to_address = $patron->notice_email_address;
1341 unless ($to_address) {
1342 # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})";
1343 # warning too verbose for this more common case?
1344 _set_message_status(
1346 message_id => $message->{'message_id'},
1348 failure_code => 'NO_EMAIL'
1355 # Skip this message if we exceed domain limits in this run
1356 if( Koha::Notice::Util->exceeds_limit({ to => $to_address, limits => $domain_limits }) ) {
1357 # Save the to_address if you delay the message so that we dont need to look it up again
1358 _update_message_to_address( $message->{'message_id'}, $to_address )
1359 if !$message->{to_address};
1363 my $subject = $message->{'subject'};
1365 my $content = $message->{'content'};
1366 my $content_type = $message->{'content_type'} || 'text/plain; charset="UTF-8"';
1367 my $is_html = $content_type =~ m/html/io;
1369 my $branch_email = undef;
1370 my $branch_replyto = undef;
1371 my $branch_returnpath = undef;
1374 $patron //= Koha::Patrons->find( $message->{borrowernumber} ); # we might already found him
1376 $library = $patron->library;
1377 $branch_email = $library->from_email_address;
1378 $branch_replyto = $library->branchreplyto;
1379 $branch_returnpath = $library->branchreturnpath;
1382 # NOTE: Patron may not be defined above so branch_email may be undefined still
1383 # so we need to fallback to KohaAdminEmailAddress as a last resort.
1385 $message->{'from_address'}
1387 || C4::Context->preference('KohaAdminEmailAddress');
1388 if( !$from_address ) {
1389 _set_message_status(
1391 message_id => $message->{'message_id'},
1393 failure_code => 'NO_FROM',
1405 C4::Context->preference('NoticeBcc')
1406 ? ( bcc => C4::Context->preference('NoticeBcc') )
1409 from => $from_address,
1410 reply_to => $message->{'reply_address'} || $branch_replyto,
1411 sender => $branch_returnpath,
1412 subject => "" . $message->{subject}
1415 if ( $message->{'content_type'} && $message->{'content_type'} eq SERIALIZED_EMAIL_CONTENT_TYPE ) {
1417 # The message has been previously composed as a valid MIME object
1418 # and serialized as a string on the DB
1419 $email = Koha::Email->new_from_string($content);
1420 $email->create($params);
1422 $email = Koha::Email->create($params);
1424 $email->html_body( _wrap_html( $content, $subject ) );
1426 $email->text_body($content);
1431 if ( ref($_) eq 'Koha::Exceptions::BadParameter' ) {
1432 _set_message_status(
1434 message_id => $message->{'message_id'},
1436 failure_code => "INVALID_EMAIL:".$_->parameter
1440 _set_message_status(
1442 message_id => $message->{'message_id'},
1444 failure_code => 'UNKNOWN_ERROR'
1450 return unless $email;
1454 $smtp_server = $library->smtp_server;
1457 $smtp_server = Koha::SMTP::Servers->get_default;
1463 sasl_username => $username,
1464 sasl_password => $password,
1469 # if initial message address was empty, coming here means that a to address was found and
1470 # queue should be updated; same if to address was overriden by Koha::Email->create
1471 _update_message_to_address( $message->{'message_id'}, $email->email->header('To') )
1472 if !$message->{to_address}
1473 || $message->{to_address} ne $email->email->header('To');
1475 $smtp_transports->{ $smtp_server->id // 'default' } ||= $smtp_server->transport;
1476 my $smtp_transport = $smtp_transports->{ $smtp_server->id // 'default' };
1479 $email->send_or_die({ transport => $smtp_transport });
1481 _set_message_status(
1483 message_id => $message->{'message_id'},
1491 _set_message_status(
1493 message_id => $message->{'message_id'},
1495 failure_code => 'SENDMAIL'
1499 carp "$Mail::Sendmail::error";
1505 my ($content, $title) = @_;
1507 my $css = C4::Context->preference("NoticeCSS") || '';
1508 $css = qq{<link rel="stylesheet" type="text/css" href="$css">} if $css;
1510 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
1511 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1512 <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
1514 <title>$title</title>
1515 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
1526 my ( $message ) = @_;
1527 my $dbh = C4::Context->dbh;
1528 my $count = $dbh->selectrow_array(q|
1531 WHERE message_transport_type = ?
1532 AND borrowernumber = ?
1534 AND CAST(updated_on AS date) = CAST(NOW() AS date)
1537 |, {}, $message->{message_transport_type}, $message->{borrowernumber}, $message->{letter_code}, $message->{content} );
1541 sub _send_message_by_sms {
1542 my $message = shift or return;
1543 my $patron = Koha::Patrons->find( $message->{borrowernumber} );
1544 _update_message_to_address($message->{message_id}, $patron->smsalertnumber) if $patron;
1546 unless ( $patron and $patron->smsalertnumber ) {
1547 _set_message_status( { message_id => $message->{'message_id'},
1549 failure_code => 'MISSING_SMS' } );
1553 if ( _is_duplicate( $message ) ) {
1554 _set_message_status(
1556 message_id => $message->{'message_id'},
1558 failure_code => 'DUPLICATE_MESSAGE'
1564 my $success = C4::SMS->send_sms(
1566 destination => $patron->smsalertnumber,
1567 message => $message->{'content'},
1572 _set_message_status(
1574 message_id => $message->{'message_id'},
1581 _set_message_status(
1583 message_id => $message->{'message_id'},
1585 failure_code => 'NO_NOTES'
1593 sub _update_message_to_address {
1595 my $dbh = C4::Context->dbh();
1596 $dbh->do('UPDATE message_queue SET to_address=? WHERE message_id=?',undef,($to,$id));
1599 sub _update_message_from_address {
1600 my ($message_id, $from_address) = @_;
1601 my $dbh = C4::Context->dbh();
1602 $dbh->do('UPDATE message_queue SET from_address = ? WHERE message_id = ?', undef, ($from_address, $message_id));
1605 sub _set_message_status {
1606 my $params = shift or return;
1608 foreach my $required_parameter ( qw( message_id status ) ) {
1609 return unless exists $params->{ $required_parameter };
1612 my $dbh = C4::Context->dbh();
1613 my $statement = 'UPDATE message_queue SET status= ?, failure_code= ? WHERE message_id = ?';
1614 my $sth = $dbh->prepare( $statement );
1615 my $result = $sth->execute( $params->{'status'},
1616 $params->{'failure_code'} || '',
1617 $params->{'message_id'} );
1622 my ( $params ) = @_;
1624 my $content = $params->{content};
1625 my $tables = $params->{tables};
1626 my $loops = $params->{loops};
1627 my $objects = $params->{objects} || {};
1628 my $substitute = $params->{substitute} || {};
1629 my $lang = defined($params->{lang}) && $params->{lang} ne 'default' ? $params->{lang} : 'en';
1630 my ($theme, $availablethemes);
1632 my $htdocs = C4::Context->config('intrahtdocs');
1633 ($theme, $lang, $availablethemes)= C4::Templates::availablethemes( $htdocs, 'about.tt', 'intranet', $lang);
1635 foreach (@$availablethemes) {
1636 push @includes, "$htdocs/$_/$lang/includes";
1637 push @includes, "$htdocs/$_/en/includes" unless $lang eq 'en';
1640 my $use_template_cache = C4::Context->config('template_cache_dir') && defined $ENV{GATEWAY_INTERFACE};
1641 my $template = Template->new(
1645 PLUGIN_BASE => 'Koha::Template::Plugin',
1646 COMPILE_EXT => $use_template_cache ? '.ttc' : '',
1647 COMPILE_DIR => $use_template_cache ? C4::Context->config('template_cache_dir') : '',
1648 INCLUDE_PATH => \@includes,
1650 ENCODING => 'UTF-8',
1652 ) or die Template->error();
1654 my $tt_params = { %{ _get_tt_params( $tables ) }, %{ _get_tt_params( $loops, 'is_a_loop' ) }, %$substitute, %$objects };
1656 $content = add_tt_filters( $content );
1657 $content = qq|[% USE KohaDates %][% USE Remove_MARC_punctuation %]$content|;
1660 my $schema = Koha::Database->new->schema;
1662 my $processed = try {
1663 $template->process( \$content, $tt_params, \$output );
1666 $schema->txn_rollback;
1668 croak "ERROR PROCESSING TEMPLATE: " . $template->error() unless $processed;
1673 sub _get_tt_params {
1674 my ($tables, $is_a_loop) = @_;
1680 article_requests => {
1681 module => 'Koha::ArticleRequests',
1682 singular => 'article_request',
1683 plural => 'article_requests',
1687 module => 'Koha::Acquisition::Baskets',
1688 singular => 'basket',
1689 plural => 'baskets',
1693 module => 'Koha::Acquisition::Booksellers',
1694 singular => 'bookseller',
1695 plural => 'booksellers',
1699 module => 'Koha::Biblios',
1700 singular => 'biblio',
1701 plural => 'biblios',
1702 pk => 'biblionumber',
1705 module => 'Koha::Biblioitems',
1706 singular => 'biblioitem',
1707 plural => 'biblioitems',
1708 pk => 'biblioitemnumber',
1711 module => 'Koha::Patrons',
1712 singular => 'borrower',
1713 plural => 'borrowers',
1714 pk => 'borrowernumber',
1717 module => 'Koha::Libraries',
1718 singular => 'branch',
1719 plural => 'branches',
1723 module => 'Koha::Account::Lines',
1724 singular => 'credit',
1725 plural => 'credits',
1726 pk => 'accountlines_id',
1729 module => 'Koha::Account::Lines',
1730 singular => 'debit',
1732 pk => 'accountlines_id',
1735 module => 'Koha::Items',
1740 additional_contents => {
1741 module => 'Koha::AdditionalContents',
1742 singular => 'additional_content',
1743 plural => 'additional_contents',
1747 module => 'Koha::AdditionalContents',
1753 module => 'Koha::Acquisition::Orders',
1754 singular => 'order',
1756 pk => 'ordernumber',
1759 module => 'Koha::Holds',
1765 module => 'Koha::Serials',
1766 singular => 'serial',
1767 plural => 'serials',
1771 module => 'Koha::Subscriptions',
1772 singular => 'subscription',
1773 plural => 'subscriptions',
1774 pk => 'subscriptionid',
1777 module => 'Koha::Suggestions',
1778 singular => 'suggestion',
1779 plural => 'suggestions',
1780 pk => 'suggestionid',
1783 module => 'Koha::Tickets',
1784 singular => 'ticket',
1785 plural => 'tickets',
1789 module => 'Koha::Ticket::Updates',
1790 singular => 'ticket_update',
1791 plural => 'ticket_updates',
1795 module => 'Koha::Checkouts',
1796 singular => 'checkout',
1797 plural => 'checkouts',
1801 module => 'Koha::Old::Checkouts',
1802 singular => 'old_checkout',
1803 plural => 'old_checkouts',
1807 module => 'Koha::Checkouts',
1808 singular => 'overdue',
1809 plural => 'overdues',
1812 borrower_modifications => {
1813 module => 'Koha::Patron::Modifications',
1814 singular => 'patron_modification',
1815 plural => 'patron_modifications',
1816 fk => 'verification_token',
1819 module => 'Koha::Illrequests',
1820 singular => 'illrequest',
1821 plural => 'illrequests',
1822 pk => 'illrequest_id'
1826 foreach my $table ( keys %$tables ) {
1827 next unless $config->{$table};
1829 my $ref = ref( $tables->{$table} ) || q{};
1830 my $module = $config->{$table}->{module};
1832 if ( can_load( modules => { $module => undef } ) ) {
1833 my $pk = $config->{$table}->{pk};
1834 my $fk = $config->{$table}->{fk};
1837 my $values = $tables->{$table} || [];
1838 unless ( ref( $values ) eq 'ARRAY' ) {
1839 croak "ERROR processing table $table. Wrong API call.";
1841 my $key = $pk ? $pk : $fk;
1842 # $key does not come from user input
1843 my $objects = $module->search(
1844 { $key => $values },
1846 # We want to retrieve the data in the same order
1848 # field is a MySQLism, but they are no other way to do it
1849 # To be generic we could do it in perl, but we will need to fetch
1850 # all the data then order them
1851 @$values ? ( order_by => \[ "field($key, " . join( ', ', @$values ) . ")" ] ) : ()
1854 $params->{ $config->{$table}->{plural} } = $objects;
1856 elsif ( $ref eq q{} || $ref eq 'HASH' ) {
1857 my $id = ref $ref eq 'HASH' ? $tables->{$table}->{$pk} : $tables->{$table};
1859 if ( $fk ) { # Using a foreign key for lookup
1860 if ( ref( $fk ) eq 'ARRAY' ) { # Foreign key is multi-column
1862 foreach my $key ( @$fk ) {
1863 $search->{$key} = $id->{$key};
1865 $object = $module->search( $search )->last();
1866 } else { # Foreign key is single column
1867 $object = $module->search( { $fk => $id } )->last();
1869 } else { # using the table's primary key for lookup
1870 $object = $module->find($id);
1872 $params->{ $config->{$table}->{singular} } = $object;
1874 else { # $ref eq 'ARRAY'
1876 if ( @{ $tables->{$table} } == 1 ) { # Param is a single key
1877 $object = $module->search( { $pk => $tables->{$table} } )->last();
1879 else { # Params are mutliple foreign keys
1880 croak "Multiple foreign keys (table $table) should be passed using an hashref";
1882 $params->{ $config->{$table}->{singular} } = $object;
1886 croak "ERROR LOADING MODULE $module: $Module::Load::Conditional::ERROR";
1890 $params->{today} = output_pref({ dt => dt_from_string, dateformat => 'iso' });
1895 =head3 add_tt_filters
1897 $content = add_tt_filters( $content );
1899 Add TT filters to some specific fields if needed.
1901 For now we only add the Remove_MARC_punctuation TT filter to biblio and biblioitem fields
1905 sub add_tt_filters {
1906 my ( $content ) = @_;
1907 $content =~ s|\[%\s*biblio\.(.*?)\s*%\]|[% biblio.$1 \| \$Remove_MARC_punctuation %]|gxms;
1908 $content =~ s|\[%\s*biblioitem\.(.*?)\s*%\]|[% biblioitem.$1 \| \$Remove_MARC_punctuation %]|gxms;
1912 =head2 get_item_content
1914 my $item = Koha::Items->find(...)->unblessed;
1915 my @item_content_fields = qw( date_due title barcode author itemnumber );
1916 my $item_content = C4::Letters::get_item_content({
1918 item_content_fields => \@item_content_fields
1921 This function generates a tab-separated list of values for the passed item. Dates
1922 are formatted following the current setup.
1926 sub get_item_content {
1927 my ( $params ) = @_;
1928 my $item = $params->{item};
1929 my $dateonly = $params->{dateonly} || 0;
1930 my $item_content_fields = $params->{item_content_fields} || [];
1932 return unless $item;
1934 my @item_info = map {
1938 { dt => dt_from_string( $item->{$_} ), dateonly => $dateonly } );
1942 } @$item_content_fields;
1943 return join( "\t", @item_info ) . "\n";