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>.
24 use Date::Calc qw( Add_Delta_Days );
28 use Module::Load::Conditional qw(can_load);
31 use C4::Members::Attributes qw(GetBorrowerAttributes);
36 use Koha::SMS::Providers;
39 use Koha::DateUtils qw( format_sqldatetime dt_from_string );
41 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
47 &GetLetters &GetLettersAvailableForALibrary &GetLetterTemplates &DelLetter &GetPreparedLetter &GetWrappedLetter &addalert &getalert &delalert &findrelatedto &SendAlerts &GetPrintMessages &GetMessageTransportTypes
53 C4::Letters - Give functions for Letters management
61 "Letters" is the tool used in Koha to manage informations sent to the patrons and/or the library. This include some cron jobs like
62 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)
64 Letters are managed through "alerts" sent by Koha on some events. All "alert" related functions are in this module too.
66 =head2 GetLetters([$module])
68 $letters = &GetLetters($module);
69 returns informations about letters.
70 if needed, $module filters for letters given module
76 my $module = $filters->{module};
77 my $code = $filters->{code};
78 my $branchcode = $filters->{branchcode};
79 my $dbh = C4::Context->dbh;
80 my $letters = $dbh->selectall_arrayref(
82 SELECT module, code, branchcode, name
86 . ( $module ? q| AND module = ?| : q|| )
87 . ( $code ? q| AND code = ?| : q|| )
88 . ( defined $branchcode ? q| AND branchcode = ?| : q|| )
89 . q| GROUP BY code ORDER BY name|, { Slice => {} }
90 , ( $module ? $module : () )
91 , ( $code ? $code : () )
92 , ( defined $branchcode ? $branchcode : () )
98 =head2 GetLetterTemplates
100 my $letter_templates = GetLetterTemplates(
102 module => 'circulation',
104 branchcode => 'CPL', # '' for default,
108 Return a hashref of letter templates.
109 The key will be the message transport type.
113 sub GetLetterTemplates {
116 my $module = $params->{module};
117 my $code = $params->{code};
118 my $branchcode = $params->{branchcode} // '';
119 my $dbh = C4::Context->dbh;
120 my $letters = $dbh->selectall_hashref(
122 SELECT module, code, branchcode, name, is_html, title, content, message_transport_type
128 , 'message_transport_type'
130 , $module, $code, $branchcode
136 =head2 GetLettersAvailableForALibrary
138 my $letters = GetLettersAvailableForALibrary(
140 branchcode => 'CPL', # '' for default
141 module => 'circulation',
145 Return an arrayref of letters, sorted by name.
146 If a specific letter exist for the given branchcode, it will be retrieve.
147 Otherwise the default letter will be.
151 sub GetLettersAvailableForALibrary {
153 my $branchcode = $filters->{branchcode};
154 my $module = $filters->{module};
156 croak "module should be provided" unless $module;
158 my $dbh = C4::Context->dbh;
159 my $default_letters = $dbh->selectall_arrayref(
161 SELECT module, code, branchcode, name
165 . q| AND branchcode = ''|
166 . ( $module ? q| AND module = ?| : q|| )
167 . q| ORDER BY name|, { Slice => {} }
168 , ( $module ? $module : () )
171 my $specific_letters;
173 $specific_letters = $dbh->selectall_arrayref(
175 SELECT module, code, branchcode, name
179 . q| AND branchcode = ?|
180 . ( $module ? q| AND module = ?| : q|| )
181 . q| ORDER BY name|, { Slice => {} }
183 , ( $module ? $module : () )
188 for my $l (@$default_letters) {
189 $letters{ $l->{code} } = $l;
191 for my $l (@$specific_letters) {
192 # Overwrite the default letter with the specific one.
193 $letters{ $l->{code} } = $l;
196 return [ map { $letters{$_} }
197 sort { $letters{$a}->{name} cmp $letters{$b}->{name} }
203 my ( $module, $code, $branchcode, $message_transport_type ) = @_;
204 $message_transport_type //= '%';
206 if ( C4::Context->preference('IndependentBranches')
208 and C4::Context->userenv ) {
210 $branchcode = C4::Context->userenv->{'branch'};
214 my $dbh = C4::Context->dbh;
215 my $sth = $dbh->prepare(q{
218 WHERE module=? AND code=? AND (branchcode = ? OR branchcode = '')
219 AND message_transport_type LIKE ?
220 ORDER BY branchcode DESC LIMIT 1
222 $sth->execute( $module, $code, $branchcode, $message_transport_type );
223 my $line = $sth->fetchrow_hashref
225 $line->{'content-type'} = 'text/html; charset="UTF-8"' if $line->{is_html};
235 module => 'circulation',
241 Delete the letter. The mtt parameter is facultative.
242 If not given, all templates mathing the other parameters will be removed.
248 my $branchcode = $params->{branchcode};
249 my $module = $params->{module};
250 my $code = $params->{code};
251 my $mtt = $params->{mtt};
252 my $dbh = C4::Context->dbh;
258 | . ( $mtt ? q| AND message_transport_type = ?| : q|| )
259 , undef, $branchcode, $module, $code, ( $mtt ? $mtt : () ) );
262 =head2 addalert ($borrowernumber, $type, $externalid)
265 - $borrowernumber : the number of the borrower subscribing to the alert
266 - $type : the type of alert.
267 - $externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
269 create an alert and return the alertid (primary key)
274 my ( $borrowernumber, $type, $externalid ) = @_;
275 my $dbh = C4::Context->dbh;
278 "insert into alert (borrowernumber, type, externalid) values (?,?,?)");
279 $sth->execute( $borrowernumber, $type, $externalid );
281 # get the alert number newly created and return it
282 my $alertid = $dbh->{'mysql_insertid'};
286 =head2 delalert ($alertid)
289 - alertid : the alert id
295 my $alertid = shift or die "delalert() called without valid argument (alertid)"; # it's gonna die anyway.
296 $debug and warn "delalert: deleting alertid $alertid";
297 my $sth = C4::Context->dbh->prepare("delete from alert where alertid=?");
298 $sth->execute($alertid);
301 =head2 getalert ([$borrowernumber], [$type], [$externalid])
304 - $borrowernumber : the number of the borrower subscribing to the alert
305 - $type : the type of alert.
306 - $externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
307 all parameters NON mandatory. If a parameter is omitted, the query is done without the corresponding parameter. For example, without $externalid, returns all alerts for a borrower on a topic.
312 my ( $borrowernumber, $type, $externalid ) = @_;
313 my $dbh = C4::Context->dbh;
314 my $query = "SELECT a.*, b.branchcode FROM alert a JOIN borrowers b USING(borrowernumber) WHERE 1";
316 if ($borrowernumber and $borrowernumber =~ /^\d+$/) {
317 $query .= " AND borrowernumber=?";
318 push @bind, $borrowernumber;
321 $query .= " AND type=?";
325 $query .= " AND externalid=?";
326 push @bind, $externalid;
328 my $sth = $dbh->prepare($query);
329 $sth->execute(@bind);
330 return $sth->fetchall_arrayref({});
333 =head2 findrelatedto($type, $externalid)
336 - $type : the type of alert
337 - $externalid : the id of the "object" to query
339 In the table alert, a "id" is stored in the externalid field. This "id" is related to another table, depending on the type of the alert.
340 When type=issue, the id is related to a subscriptionid and this sub returns the name of the biblio.
345 # When type=virtual, the id is related to a virtual shelf and this sub returns the name of the sub
348 my $type = shift or return;
349 my $externalid = shift or return;
350 my $q = ($type eq 'issue' ) ?
351 "select title as result from subscription left join biblio on subscription.biblionumber=biblio.biblionumber where subscriptionid=?" :
352 ($type eq 'borrower') ?
353 "select concat(firstname,' ',surname) from borrowers where borrowernumber=?" : undef;
355 warn "findrelatedto(): Illegal type '$type'";
358 my $sth = C4::Context->dbh->prepare($q);
359 $sth->execute($externalid);
360 my ($result) = $sth->fetchrow;
366 my $err = &SendAlerts($type, $externalid, $letter_code);
369 - $type : the type of alert
370 - $externalid : the id of the "object" to query
371 - $letter_code : the notice template to use
373 C<&SendAlerts> sends an email notice directly to a patron or a vendor.
374 Currently it supports ($type):
375 - claim serial issues (claimissues)
376 - claim acquisition orders (claimacquisition)
377 - send acquisition orders to the vendor (orderacquisition)
378 - notify patrons about newly received serial issues (issue)
379 - notify patrons when their account is created (members)
381 Returns undef or { error => 'message } on failure.
382 Returns true on success.
387 my ( $type, $externalid, $letter_code ) = @_;
388 my $dbh = C4::Context->dbh;
389 if ( $type eq 'issue' ) {
391 # prepare the letter...
392 # search the subscriptionid
395 "SELECT subscriptionid FROM serial WHERE serialid=?");
396 $sth->execute($externalid);
397 my ($subscriptionid) = $sth->fetchrow
398 or warn( "No subscription for '$externalid'" ),
401 # search the biblionumber
404 "SELECT biblionumber FROM subscription WHERE subscriptionid=?");
405 $sth->execute($subscriptionid);
406 my ($biblionumber) = $sth->fetchrow
407 or warn( "No biblionumber for '$subscriptionid'" ),
411 # find the list of borrowers to alert
412 my $alerts = getalert( '', 'issue', $subscriptionid );
414 my $borinfo = C4::Members::GetMember('borrowernumber' => $_->{'borrowernumber'});
415 my $email = $borinfo->{email} or next;
417 # warn "sending issues...";
418 my $userenv = C4::Context->userenv;
419 my $library = Koha::Libraries->find( $_->{branchcode} );
420 my $letter = GetPreparedLetter (
422 letter_code => $letter_code,
423 branchcode => $userenv->{branch},
425 'branches' => $_->{branchcode},
426 'biblio' => $biblionumber,
427 'biblioitems' => $biblionumber,
428 'borrowers' => $borinfo,
429 'subscription' => $subscriptionid,
430 'serial' => $externalid,
436 my $message = Koha::Email->new();
437 my %mail = $message->create_message_headers(
440 from => $library->branchemail,
441 replyto => $library->branchreplyto,
442 sender => $library->branchreturnpath,
443 subject => Encode::encode( "UTF-8", "" . $letter->{title} ),
444 message => $letter->{'is_html'}
445 ? _wrap_html( Encode::encode( "UTF-8", $letter->{'content'} ),
446 Encode::encode( "UTF-8", "" . $letter->{'title'} ))
447 : Encode::encode( "UTF-8", "" . $letter->{'content'} ),
448 contenttype => $letter->{'is_html'}
449 ? 'text/html; charset="utf-8"'
450 : 'text/plain; charset="utf-8"',
453 unless( sendmail(%mail) ) {
454 carp $Mail::Sendmail::error;
455 return { error => $Mail::Sendmail::error };
459 elsif ( $type eq 'claimacquisition' or $type eq 'claimissues' or $type eq 'orderacquisition' ) {
461 # prepare the letter...
466 if ( $type eq 'claimacquisition') {
468 SELECT aqorders.*,aqbasket.*,biblio.*,biblioitems.*
470 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
471 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
472 LEFT JOIN biblioitems ON aqorders.biblionumber=biblioitems.biblionumber
473 WHERE aqorders.ordernumber IN (
477 carp "No order selected";
478 return { error => "no_order_selected" };
480 $strsth .= join( ",", @$externalid ) . ")";
481 $action = "ACQUISITION CLAIM";
482 $sthorders = $dbh->prepare($strsth);
484 $dataorders = $sthorders->fetchall_arrayref( {} );
487 if ($type eq 'claimissues') {
489 SELECT serial.*,subscription.*, biblio.*, aqbooksellers.*,
490 aqbooksellers.id AS booksellerid
492 LEFT JOIN subscription ON serial.subscriptionid=subscription.subscriptionid
493 LEFT JOIN biblio ON serial.biblionumber=biblio.biblionumber
494 LEFT JOIN aqbooksellers ON subscription.aqbooksellerid=aqbooksellers.id
495 WHERE serial.serialid IN (
499 carp "No Order selected";
500 return { error => "no_order_selected" };
503 $strsth .= join( ",", @$externalid ) . ")";
504 $action = "CLAIM ISSUE";
505 $sthorders = $dbh->prepare($strsth);
507 $dataorders = $sthorders->fetchall_arrayref( {} );
510 if ( $type eq 'orderacquisition') {
512 SELECT aqorders.*,aqbasket.*,biblio.*,biblioitems.*
514 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
515 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
516 LEFT JOIN biblioitems ON aqorders.biblionumber=biblioitems.biblionumber
517 WHERE aqbasket.basketno = ?
518 AND orderstatus IN ('new','ordered')
522 carp "No basketnumber given";
523 return { error => "no_basketno" };
525 $action = "ACQUISITION ORDER";
526 $sthorders = $dbh->prepare($strsth);
527 $sthorders->execute($externalid);
528 $dataorders = $sthorders->fetchall_arrayref( {} );
532 $dbh->prepare("select * from aqbooksellers where id=?");
533 $sthbookseller->execute( $dataorders->[0]->{booksellerid} );
534 my $databookseller = $sthbookseller->fetchrow_hashref;
536 my $addressee = $type eq 'claimacquisition' || $type eq 'orderacquisition' ? 'acqprimary' : 'serialsprimary';
539 $dbh->prepare("SELECT * FROM aqcontacts WHERE booksellerid=? AND $type=1 ORDER BY $addressee DESC");
540 $sthcontact->execute( $dataorders->[0]->{booksellerid} );
541 my $datacontact = $sthcontact->fetchrow_hashref;
545 push @email, $databookseller->{bookselleremail} if $databookseller->{bookselleremail};
546 push @email, $datacontact->{email} if ( $datacontact && $datacontact->{email} );
548 warn "Bookseller $dataorders->[0]->{booksellerid} without emails";
549 return { error => "no_email" };
552 while ($addlcontact = $sthcontact->fetchrow_hashref) {
553 push @cc, $addlcontact->{email} if ( $addlcontact && $addlcontact->{email} );
556 my $userenv = C4::Context->userenv;
557 my $letter = GetPreparedLetter (
559 letter_code => $letter_code,
560 branchcode => $userenv->{branch},
562 'branches' => $userenv->{branch},
563 'aqbooksellers' => $databookseller,
564 'aqcontacts' => $datacontact,
566 repeat => $dataorders,
570 # Remove the order tag
571 $letter->{content} =~ s/<order>(.*?)<\/order>/$1/gxms;
575 To => join( ',', @email),
576 Cc => join( ',', @cc),
577 From => $userenv->{emailaddress},
578 Subject => Encode::encode( "UTF-8", "" . $letter->{title} ),
579 Message => $letter->{'is_html'}
580 ? _wrap_html( Encode::encode( "UTF-8", $letter->{'content'} ),
581 Encode::encode( "UTF-8", "" . $letter->{'title'} ))
582 : Encode::encode( "UTF-8", "" . $letter->{'content'} ),
583 'Content-Type' => $letter->{'is_html'}
584 ? 'text/html; charset="utf-8"'
585 : 'text/plain; charset="utf-8"',
588 if ($type eq 'claimacquisition' || $type eq 'claimissues' ) {
589 $mail{'Reply-to'} = C4::Context->preference('ReplytoDefault')
590 if C4::Context->preference('ReplytoDefault');
591 $mail{'Sender'} = C4::Context->preference('ReturnpathDefault')
592 if C4::Context->preference('ReturnpathDefault');
593 $mail{'Bcc'} = $userenv->{emailaddress}
594 if C4::Context->preference("ClaimsBccCopy");
597 unless ( sendmail(%mail) ) {
598 carp $Mail::Sendmail::error;
599 return { error => $Mail::Sendmail::error };
607 . join( ',', @email )
612 ) if C4::Context->preference("LetterLog");
614 # send an "account details" notice to a newly created user
615 elsif ( $type eq 'members' ) {
616 my $library = Koha::Libraries->find( $externalid->{branchcode} )->unblessed;
617 my $letter = GetPreparedLetter (
619 letter_code => $letter_code,
620 branchcode => $externalid->{'branchcode'},
622 'branches' => $library,
623 'borrowers' => $externalid->{'borrowernumber'},
625 substitute => { 'borrowers.password' => $externalid->{'password'} },
628 return { error => "no_email" } unless $externalid->{'emailaddr'};
629 my $email = Koha::Email->new();
630 my %mail = $email->create_message_headers(
632 to => $externalid->{'emailaddr'},
633 from => $library->{branchemail},
634 replyto => $library->{branchreplyto},
635 sender => $library->{branchreturnpath},
636 subject => Encode::encode( "UTF-8", "" . $letter->{'title'} ),
637 message => $letter->{'is_html'}
638 ? _wrap_html( Encode::encode( "UTF-8", $letter->{'content'} ),
639 Encode::encode( "UTF-8", "" . $letter->{'title'} ) )
640 : Encode::encode( "UTF-8", "" . $letter->{'content'} ),
641 contenttype => $letter->{'is_html'}
642 ? 'text/html; charset="utf-8"'
643 : 'text/plain; charset="utf-8"',
646 unless( sendmail(%mail) ) {
647 carp $Mail::Sendmail::error;
648 return { error => $Mail::Sendmail::error };
652 # If we come here, return an OK status
656 =head2 GetPreparedLetter( %params )
659 module => letter module, mandatory
660 letter_code => letter code, mandatory
661 branchcode => for letter selection, if missing default system letter taken
662 tables => a hashref with table names as keys. Values are either:
663 - a scalar - primary key value
664 - an arrayref - primary key values
665 - a hashref - full record
666 substitute => custom substitution key/value pairs
667 repeat => records to be substituted on consecutive lines:
668 - an arrayref - tries to guess what needs substituting by
669 taking remaining << >> tokensr; not recommended
670 - a hashref token => @tables - replaces <token> << >> << >> </token>
671 subtemplate for each @tables row; table is a hashref as above
672 want_librarian => boolean, if set to true triggers librarian details
673 substitution from the userenv
675 letter fields hashref (title & content useful)
679 sub GetPreparedLetter {
682 my $module = $params{module} or croak "No module";
683 my $letter_code = $params{letter_code} or croak "No letter_code";
684 my $branchcode = $params{branchcode} || '';
685 my $mtt = $params{message_transport_type} || 'email';
687 my $letter = getletter( $module, $letter_code, $branchcode, $mtt )
688 or warn( "No $module $letter_code letter transported by " . $mtt ),
691 my $tables = $params{tables};
692 my $substitute = $params{substitute};
693 my $repeat = $params{repeat};
694 $tables || $substitute || $repeat
695 or carp( "ERROR: nothing to substitute - both 'tables' and 'substitute' are empty" ),
697 my $want_librarian = $params{want_librarian};
700 while ( my ($token, $val) = each %$substitute ) {
701 if ( $token eq 'items.content' ) {
702 $val =~ s|\n|<br/>|g if $letter->{is_html};
705 $letter->{title} =~ s/<<$token>>/$val/g;
706 $letter->{content} =~ s/<<$token>>/$val/g;
710 my $OPACBaseURL = C4::Context->preference('OPACBaseURL');
711 $letter->{content} =~ s/<<OPACBaseURL>>/$OPACBaseURL/go;
713 if ($want_librarian) {
714 # parsing librarian name
715 my $userenv = C4::Context->userenv;
716 $letter->{content} =~ s/<<LibrarianFirstname>>/$userenv->{firstname}/go;
717 $letter->{content} =~ s/<<LibrarianSurname>>/$userenv->{surname}/go;
718 $letter->{content} =~ s/<<LibrarianEmailaddress>>/$userenv->{emailaddress}/go;
721 my ($repeat_no_enclosing_tags, $repeat_enclosing_tags);
724 if (ref ($repeat) eq 'ARRAY' ) {
725 $repeat_no_enclosing_tags = $repeat;
727 $repeat_enclosing_tags = $repeat;
731 if ($repeat_enclosing_tags) {
732 while ( my ($tag, $tag_tables) = each %$repeat_enclosing_tags ) {
733 if ( $letter->{content} =~ m!<$tag>(.*)</$tag>!s ) {
736 my %subletter = ( title => '', content => $subcontent );
737 _substitute_tables( \%subletter, $_ );
740 $letter->{content} =~ s!<$tag>.*</$tag>!join( "\n", @lines )!se;
746 _substitute_tables( $letter, $tables );
749 if ($repeat_no_enclosing_tags) {
750 if ( $letter->{content} =~ m/[^\n]*<<.*>>[^\n]*/so ) {
755 $c =~ s/<<count>>/$i/go;
756 foreach my $field ( keys %{$_} ) {
757 $c =~ s/(<<[^\.]+.$field>>)/$_->{$field}/;
761 } @$repeat_no_enclosing_tags;
763 my $replaceby = join( "\n", @lines );
764 $letter->{content} =~ s/\Q$line\E/$replaceby/s;
768 $letter->{content} = _process_tt(
770 content => $letter->{content},
775 $letter->{content} =~ s/<<\S*>>//go; #remove any stragglers
780 sub _substitute_tables {
781 my ( $letter, $tables ) = @_;
782 while ( my ($table, $param) = each %$tables ) {
785 my $ref = ref $param;
788 if ($ref && $ref eq 'HASH') {
792 my $sth = _parseletter_sth($table);
794 warn "_parseletter_sth('$table') failed to return a valid sth. No substitution will be done for that table.";
797 $sth->execute( $ref ? @$param : $param );
799 $values = $sth->fetchrow_hashref;
803 _parseletter ( $letter, $table, $values );
807 sub _parseletter_sth {
811 carp "ERROR: _parseletter_sth() called without argument (table)";
814 # NOTE: we used to check whether we had a statement handle cached in
815 # a %handles module-level variable. This was a dumb move and
816 # broke things for the rest of us. prepare_cached is a better
817 # way to cache statement handles anyway.
819 ($table eq 'biblio' ) ? "SELECT * FROM $table WHERE biblionumber = ?" :
820 ($table eq 'biblioitems' ) ? "SELECT * FROM $table WHERE biblionumber = ?" :
821 ($table eq 'items' ) ? "SELECT * FROM $table WHERE itemnumber = ?" :
822 ($table eq 'issues' ) ? "SELECT * FROM $table WHERE itemnumber = ?" :
823 ($table eq 'old_issues' ) ? "SELECT * FROM $table WHERE itemnumber = ? ORDER BY timestamp DESC LIMIT 1" :
824 ($table eq 'reserves' ) ? "SELECT * FROM $table WHERE borrowernumber = ? and biblionumber = ?" :
825 ($table eq 'borrowers' ) ? "SELECT * FROM $table WHERE borrowernumber = ?" :
826 ($table eq 'branches' ) ? "SELECT * FROM $table WHERE branchcode = ?" :
827 ($table eq 'suggestions' ) ? "SELECT * FROM $table WHERE suggestionid = ?" :
828 ($table eq 'aqbooksellers') ? "SELECT * FROM $table WHERE id = ?" :
829 ($table eq 'aqorders' ) ? "SELECT * FROM $table WHERE ordernumber = ?" :
830 ($table eq 'opac_news' ) ? "SELECT * FROM $table WHERE idnew = ?" :
831 ($table eq 'article_requests') ? "SELECT * FROM $table WHERE id = ?" :
832 ($table eq 'borrower_modifications') ? "SELECT * FROM $table WHERE verification_token = ?" :
833 ($table eq 'subscription') ? "SELECT * FROM $table WHERE subscriptionid = ?" :
834 ($table eq 'serial') ? "SELECT * FROM $table WHERE serialid = ?" :
837 warn "ERROR: No _parseletter_sth query for table '$table'";
838 return; # nothing to get
840 unless ($sth = C4::Context->dbh->prepare_cached($query)) {
841 warn "ERROR: Failed to prepare query: '$query'";
844 return $sth; # now cache is populated for that $table
847 =head2 _parseletter($letter, $table, $values)
850 - $letter : a hash to letter fields (title & content useful)
851 - $table : the Koha table to parse.
852 - $values_in : table record hashref
853 parse all fields from a table, and replace values in title & content with the appropriate value
854 (not exported sub, used only internally)
859 my ( $letter, $table, $values_in ) = @_;
861 # Work on a local copy of $values_in (passed by reference) to avoid side effects
862 # in callers ( by changing / formatting values )
863 my $values = $values_in ? { %$values_in } : {};
865 if ( $table eq 'borrowers' && $values->{'dateexpiry'} ){
866 $values->{'dateexpiry'} = format_sqldatetime( $values->{'dateexpiry'} );
869 if ( $table eq 'reserves' && $values->{'waitingdate'} ) {
870 my @waitingdate = split /-/, $values->{'waitingdate'};
872 $values->{'expirationdate'} = '';
873 if ( C4::Context->preference('ReservesMaxPickUpDelay') ) {
874 my $dt = dt_from_string();
875 $dt->add( days => C4::Context->preference('ReservesMaxPickUpDelay') );
876 $values->{'expirationdate'} = output_pref( { dt => $dt, dateonly => 1 } );
879 $values->{'waitingdate'} = output_pref({ dt => dt_from_string( $values->{'waitingdate'} ), dateonly => 1 });
883 if ($letter->{content} && $letter->{content} =~ /<<today>>/) {
884 my $todaysdate = output_pref( DateTime->now() );
885 $letter->{content} =~ s/<<today>>/$todaysdate/go;
888 while ( my ($field, $val) = each %$values ) {
889 $val =~ s/\p{P}$// if $val && $table=~/biblio/;
890 #BZ 9886: Assuming that we want to eliminate ISBD punctuation here
891 #Therefore adding the test on biblio. This includes biblioitems,
892 #but excludes items. Removed unneeded global and lookahead.
894 if ( $table=~/^borrowers$/ && $field=~/^streettype$/ ) {
895 my $av = Koha::AuthorisedValues->search({ category => 'ROADTYPE', authorised_value => $val });
896 $val = $av->count ? $av->next->lib : '';
900 my $replacedby = defined ($val) ? $val : '';
902 and not $replacedby =~ m|0000-00-00|
903 and not $replacedby =~ m|9999-12-31|
904 and $replacedby =~ m|^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}:\d{2})?$| )
906 # If the value is XXXX-YY-ZZ[ AA:BB:CC] we assume it is a date
907 my $dateonly = defined $1 ? 0 : 1; #$1 refers to the capture group wrapped in parentheses. In this case, that's the hours, minutes, seconds.
908 my $re_dateonly_filter = qr{ $field( \s* \| \s* dateonly\s*)?>> }xms;
910 for my $letter_field ( qw( title content ) ) {
911 my $filter_string_used = q{};
912 if ( $letter->{ $letter_field } =~ $re_dateonly_filter ) {
913 # We overwrite $dateonly if the filter exists and we have a time in the datetime
914 $filter_string_used = $1 || q{};
915 $dateonly = $1 unless $dateonly;
917 my $replacedby_date = eval {
918 output_pref({ dt => dt_from_string( $replacedby ), dateonly => $dateonly });
921 if ( $letter->{ $letter_field } ) {
922 $letter->{ $letter_field } =~ s/\Q<<$table.$field$filter_string_used>>\E/$replacedby_date/g;
923 $letter->{ $letter_field } =~ s/\Q<<$field$filter_string_used>>\E/$replacedby_date/g;
927 # Other fields replacement
929 for my $letter_field ( qw( title content ) ) {
930 if ( $letter->{ $letter_field } ) {
931 $letter->{ $letter_field } =~ s/<<$table.$field>>/$replacedby/g;
932 $letter->{ $letter_field } =~ s/<<$field>>/$replacedby/g;
938 if ($table eq 'borrowers' && $letter->{content}) {
939 if ( my $attributes = GetBorrowerAttributes($values->{borrowernumber}) ) {
941 foreach (@$attributes) {
942 my $code = $_->{code};
943 my $val = $_->{value_description} || $_->{value};
944 $val =~ s/\p{P}(?=$)//g if $val;
945 next unless $val gt '';
947 push @{ $attr{$code} }, $val;
949 while ( my ($code, $val_ar) = each %attr ) {
950 my $replacefield = "<<borrower-attribute:$code>>";
951 my $replacedby = join ',', @$val_ar;
952 $letter->{content} =~ s/$replacefield/$replacedby/g;
961 my $success = EnqueueLetter( { letter => $letter,
962 borrowernumber => '12', message_transport_type => 'email' } )
964 places a letter in the message_queue database table, which will
965 eventually get processed (sent) by the process_message_queue.pl
966 cronjob when it calls SendQueuedMessages.
968 return message_id on success
973 my $params = shift or return;
975 return unless exists $params->{'letter'};
976 # return unless exists $params->{'borrowernumber'};
977 return unless exists $params->{'message_transport_type'};
979 my $content = $params->{letter}->{content};
980 $content =~ s/\s+//g if(defined $content);
981 if ( not defined $content or $content eq '' ) {
982 warn "Trying to add an empty message to the message queue" if $debug;
986 # If we have any attachments we should encode then into the body.
987 if ( $params->{'attachments'} ) {
988 $params->{'letter'} = _add_attachments(
989 { letter => $params->{'letter'},
990 attachments => $params->{'attachments'},
991 message => MIME::Lite->new( Type => 'multipart/mixed' ),
996 my $dbh = C4::Context->dbh();
997 my $statement = << 'ENDSQL';
998 INSERT INTO message_queue
999 ( borrowernumber, subject, content, metadata, letter_code, message_transport_type, status, time_queued, to_address, from_address, content_type )
1001 ( ?, ?, ?, ?, ?, ?, ?, NOW(), ?, ?, ? )
1004 my $sth = $dbh->prepare($statement);
1005 my $result = $sth->execute(
1006 $params->{'borrowernumber'}, # borrowernumber
1007 $params->{'letter'}->{'title'}, # subject
1008 $params->{'letter'}->{'content'}, # content
1009 $params->{'letter'}->{'metadata'} || '', # metadata
1010 $params->{'letter'}->{'code'} || '', # letter_code
1011 $params->{'message_transport_type'}, # message_transport_type
1013 $params->{'to_address'}, # to_address
1014 $params->{'from_address'}, # from_address
1015 $params->{'letter'}->{'content-type'}, # content_type
1017 return $dbh->last_insert_id(undef,undef,'message_queue', undef);
1020 =head2 SendQueuedMessages ([$hashref])
1022 my $sent = SendQueuedMessages( { verbose => 1 } );
1024 sends all of the 'pending' items in the message queue.
1026 returns number of messages sent.
1030 sub SendQueuedMessages {
1033 my $unsent_messages = _get_unsent_messages();
1034 MESSAGE: foreach my $message ( @$unsent_messages ) {
1035 # warn Data::Dumper->Dump( [ $message ], [ 'message' ] );
1036 warn sprintf( 'sending %s message to patron: %s',
1037 $message->{'message_transport_type'},
1038 $message->{'borrowernumber'} || 'Admin' )
1039 if $params->{'verbose'} or $debug;
1040 # This is just begging for subclassing
1041 next MESSAGE if ( lc($message->{'message_transport_type'}) eq 'rss' );
1042 if ( lc( $message->{'message_transport_type'} ) eq 'email' ) {
1043 _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
1045 elsif ( lc( $message->{'message_transport_type'} ) eq 'sms' ) {
1046 if ( C4::Context->preference('SMSSendDriver') eq 'Email' ) {
1047 my $member = C4::Members::GetMember( 'borrowernumber' => $message->{'borrowernumber'} );
1048 my $sms_provider = Koha::SMS::Providers->find( $member->{'sms_provider_id'} );
1049 $message->{to_address} .= '@' . $sms_provider->domain();
1050 _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
1052 _send_message_by_sms( $message );
1056 return scalar( @$unsent_messages );
1059 =head2 GetRSSMessages
1061 my $message_list = GetRSSMessages( { limit => 10, borrowernumber => '14' } )
1063 returns a listref of all queued RSS messages for a particular person.
1067 sub GetRSSMessages {
1070 return unless $params;
1071 return unless ref $params;
1072 return unless $params->{'borrowernumber'};
1074 return _get_unsent_messages( { message_transport_type => 'rss',
1075 limit => $params->{'limit'},
1076 borrowernumber => $params->{'borrowernumber'}, } );
1079 =head2 GetPrintMessages
1081 my $message_list = GetPrintMessages( { borrowernumber => $borrowernumber } )
1083 Returns a arrayref of all queued print messages (optionally, for a particular
1088 sub GetPrintMessages {
1089 my $params = shift || {};
1091 return _get_unsent_messages( { message_transport_type => 'print',
1092 borrowernumber => $params->{'borrowernumber'},
1096 =head2 GetQueuedMessages ([$hashref])
1098 my $messages = GetQueuedMessage( { borrowernumber => '123', limit => 20 } );
1100 fetches messages out of the message queue.
1103 list of hashes, each has represents a message in the message queue.
1107 sub GetQueuedMessages {
1110 my $dbh = C4::Context->dbh();
1111 my $statement = << 'ENDSQL';
1112 SELECT message_id, borrowernumber, subject, content, message_transport_type, status, time_queued
1118 if ( exists $params->{'borrowernumber'} ) {
1119 push @whereclauses, ' borrowernumber = ? ';
1120 push @query_params, $params->{'borrowernumber'};
1123 if ( @whereclauses ) {
1124 $statement .= ' WHERE ' . join( 'AND', @whereclauses );
1127 if ( defined $params->{'limit'} ) {
1128 $statement .= ' LIMIT ? ';
1129 push @query_params, $params->{'limit'};
1132 my $sth = $dbh->prepare( $statement );
1133 my $result = $sth->execute( @query_params );
1134 return $sth->fetchall_arrayref({});
1137 =head2 GetMessageTransportTypes
1139 my @mtt = GetMessageTransportTypes();
1141 returns an arrayref of transport types
1145 sub GetMessageTransportTypes {
1146 my $dbh = C4::Context->dbh();
1147 my $mtts = $dbh->selectcol_arrayref("
1148 SELECT message_transport_type
1149 FROM message_transport_types
1150 ORDER BY message_transport_type
1157 my $message = C4::Letters::Message($message_id);
1162 my ( $message_id ) = @_;
1163 return unless $message_id;
1164 my $dbh = C4::Context->dbh;
1165 return $dbh->selectrow_hashref(q|
1166 SELECT message_id, borrowernumber, subject, content, metadata, letter_code, message_transport_type, status, time_queued, to_address, from_address, content_type
1168 WHERE message_id = ?
1169 |, {}, $message_id );
1172 =head2 ResendMessage
1174 Attempt to resend a message which has failed previously.
1176 my $has_been_resent = C4::Letters::ResendMessage($message_id);
1178 Updates the message to 'pending' status so that
1179 it will be resent later on.
1181 returns 1 on success, 0 on failure, undef if no message was found
1186 my $message_id = shift;
1187 return unless $message_id;
1189 my $message = GetMessage( $message_id );
1190 return unless $message;
1192 if ( $message->{status} ne 'pending' ) {
1193 $rv = C4::Letters::_set_message_status({
1194 message_id => $message_id,
1195 status => 'pending',
1197 $rv = $rv > 0? 1: 0;
1198 # Clear destination email address to force address update
1199 _update_message_to_address( $message_id, undef ) if $rv &&
1200 $message->{message_transport_type} eq 'email';
1205 =head2 _add_attachements
1208 letter - the standard letter hashref
1209 attachments - listref of attachments. each attachment is a hashref of:
1210 type - the mime type, like 'text/plain'
1211 content - the actual attachment
1212 filename - the name of the attachment.
1213 message - a MIME::Lite object to attach these to.
1215 returns your letter object, with the content updated.
1219 sub _add_attachments {
1222 my $letter = $params->{'letter'};
1223 my $attachments = $params->{'attachments'};
1224 return $letter unless @$attachments;
1225 my $message = $params->{'message'};
1227 # First, we have to put the body in as the first attachment
1229 Type => $letter->{'content-type'} || 'TEXT',
1230 Data => $letter->{'is_html'}
1231 ? _wrap_html($letter->{'content'}, $letter->{'title'})
1232 : $letter->{'content'},
1235 foreach my $attachment ( @$attachments ) {
1237 Type => $attachment->{'type'},
1238 Data => $attachment->{'content'},
1239 Filename => $attachment->{'filename'},
1242 # we're forcing list context here to get the header, not the count back from grep.
1243 ( $letter->{'content-type'} ) = grep( /^Content-Type:/, split( /\n/, $params->{'message'}->header_as_string ) );
1244 $letter->{'content-type'} =~ s/^Content-Type:\s+//;
1245 $letter->{'content'} = $message->body_as_string;
1251 sub _get_unsent_messages {
1254 my $dbh = C4::Context->dbh();
1255 my $statement = << 'ENDSQL';
1256 SELECT mq.message_id, mq.borrowernumber, mq.subject, mq.content, mq.message_transport_type, mq.status, mq.time_queued, mq.from_address, mq.to_address, mq.content_type, b.branchcode, mq.letter_code
1257 FROM message_queue mq
1258 LEFT JOIN borrowers b ON b.borrowernumber = mq.borrowernumber
1262 my @query_params = ('pending');
1263 if ( ref $params ) {
1264 if ( $params->{'message_transport_type'} ) {
1265 $statement .= ' AND message_transport_type = ? ';
1266 push @query_params, $params->{'message_transport_type'};
1268 if ( $params->{'borrowernumber'} ) {
1269 $statement .= ' AND borrowernumber = ? ';
1270 push @query_params, $params->{'borrowernumber'};
1272 if ( $params->{'limit'} ) {
1273 $statement .= ' limit ? ';
1274 push @query_params, $params->{'limit'};
1278 $debug and warn "_get_unsent_messages SQL: $statement";
1279 $debug and warn "_get_unsent_messages params: " . join(',',@query_params);
1280 my $sth = $dbh->prepare( $statement );
1281 my $result = $sth->execute( @query_params );
1282 return $sth->fetchall_arrayref({});
1285 sub _send_message_by_email {
1286 my $message = shift or return;
1287 my ($username, $password, $method) = @_;
1289 my $member = C4::Members::GetMember( 'borrowernumber' => $message->{'borrowernumber'} );
1290 my $to_address = $message->{'to_address'};
1291 unless ($to_address) {
1293 warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})";
1294 _set_message_status( { message_id => $message->{'message_id'},
1295 status => 'failed' } );
1298 $to_address = C4::Members::GetNoticeEmailAddress( $message->{'borrowernumber'} );
1299 unless ($to_address) {
1300 # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})";
1301 # warning too verbose for this more common case?
1302 _set_message_status( { message_id => $message->{'message_id'},
1303 status => 'failed' } );
1308 my $utf8 = decode('MIME-Header', $message->{'subject'} );
1309 $message->{subject}= encode('MIME-Header', $utf8);
1310 my $subject = encode('UTF-8', $message->{'subject'});
1311 my $content = encode('UTF-8', $message->{'content'});
1312 my $content_type = $message->{'content_type'} || 'text/plain; charset="UTF-8"';
1313 my $is_html = $content_type =~ m/html/io;
1314 my $branch_email = undef;
1315 my $branch_replyto = undef;
1316 my $branch_returnpath = undef;
1318 my $library = Koha::Libraries->find( $member->{branchcode} );
1319 $branch_email = $library->branchemail;
1320 $branch_replyto = $library->branchreplyto;
1321 $branch_returnpath = $library->branchreturnpath;
1323 my $email = Koha::Email->new();
1324 my %sendmail_params = $email->create_message_headers(
1327 from => $message->{'from_address'} || $branch_email,
1328 replyto => $branch_replyto,
1329 sender => $branch_returnpath,
1330 subject => $subject,
1331 message => $is_html ? _wrap_html( $content, $subject ) : $content,
1332 contenttype => $content_type
1336 $sendmail_params{'Auth'} = {user => $username, pass => $password, method => $method} if $username;
1337 if ( my $bcc = C4::Context->preference('OverdueNoticeBcc') ) {
1338 $sendmail_params{ Bcc } = $bcc;
1341 _update_message_to_address($message->{'message_id'},$to_address) unless $message->{to_address}; #if initial message address was empty, coming here means that a to address was found and queue should be updated
1343 if ( sendmail( %sendmail_params ) ) {
1344 _set_message_status( { message_id => $message->{'message_id'},
1345 status => 'sent' } );
1348 _set_message_status( { message_id => $message->{'message_id'},
1349 status => 'failed' } );
1350 carp $Mail::Sendmail::error;
1356 my ($content, $title) = @_;
1358 my $css = C4::Context->preference("NoticeCSS") || '';
1359 $css = qq{<link rel="stylesheet" type="text/css" href="$css">} if $css;
1361 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
1362 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1363 <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
1365 <title>$title</title>
1366 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
1377 my ( $message ) = @_;
1378 my $dbh = C4::Context->dbh;
1379 my $count = $dbh->selectrow_array(q|
1382 WHERE message_transport_type = ?
1383 AND borrowernumber = ?
1385 AND CAST(time_queued AS date) = CAST(NOW() AS date)
1388 |, {}, $message->{message_transport_type}, $message->{borrowernumber}, $message->{letter_code}, $message->{content} );
1392 sub _send_message_by_sms {
1393 my $message = shift or return;
1394 my $member = C4::Members::GetMember( 'borrowernumber' => $message->{'borrowernumber'} );
1396 unless ( $member->{smsalertnumber} ) {
1397 _set_message_status( { message_id => $message->{'message_id'},
1398 status => 'failed' } );
1402 if ( _is_duplicate( $message ) ) {
1403 _set_message_status( { message_id => $message->{'message_id'},
1404 status => 'failed' } );
1408 my $success = C4::SMS->send_sms( { destination => $member->{'smsalertnumber'},
1409 message => $message->{'content'},
1411 _set_message_status( { message_id => $message->{'message_id'},
1412 status => ($success ? 'sent' : 'failed') } );
1416 sub _update_message_to_address {
1418 my $dbh = C4::Context->dbh();
1419 $dbh->do('UPDATE message_queue SET to_address=? WHERE message_id=?',undef,($to,$id));
1422 sub _set_message_status {
1423 my $params = shift or return;
1425 foreach my $required_parameter ( qw( message_id status ) ) {
1426 return unless exists $params->{ $required_parameter };
1429 my $dbh = C4::Context->dbh();
1430 my $statement = 'UPDATE message_queue SET status= ? WHERE message_id = ?';
1431 my $sth = $dbh->prepare( $statement );
1432 my $result = $sth->execute( $params->{'status'},
1433 $params->{'message_id'} );
1438 my ( $params ) = @_;
1440 my $content = $params->{content};
1441 my $tables = $params->{tables};
1443 my $use_template_cache = C4::Context->config('template_cache_dir') && defined $ENV{GATEWAY_INTERFACE};
1444 my $template = Template->new(
1448 PLUGIN_BASE => 'Koha::Template::Plugin',
1449 COMPILE_EXT => $use_template_cache ? '.ttc' : '',
1450 COMPILE_DIR => $use_template_cache ? C4::Context->config('template_cache_dir') : '',
1452 ENCODING => 'UTF-8',
1454 ) or die Template->error();
1456 my $tt_params = _get_tt_params( $tables );
1459 $template->process( \$content, $tt_params, \$output ) || croak "ERROR PROCESSING TEMPLATE: " . $template->error();
1464 sub _get_tt_params {
1471 module => 'Koha::Biblios',
1472 singular => 'biblio',
1473 plural => 'biblios',
1474 pk => 'biblionumber',
1477 module => 'Koha::Patrons',
1478 singular => 'borrower',
1479 plural => 'borrowers',
1480 pk => 'borrowernumber',
1483 module => 'Koha::Libraries',
1484 singular => 'branch',
1485 plural => 'branches',
1489 module => 'Koha::Items',
1495 module => 'Koha::News',
1501 module => 'Koha::Holds',
1504 fk => [ 'borrowernumber', 'biblionumber' ],
1507 module => 'Koha::Serials',
1508 singular => 'serial',
1509 plural => 'serials',
1513 module => 'Koha::Subscriptions',
1514 singular => 'subscription',
1515 plural => 'subscriptions',
1516 pk => 'subscriptionid',
1519 module => 'Koha::Suggestions',
1520 singular => 'suggestion',
1521 plural => 'suggestions',
1522 pk => 'suggestionid',
1525 module => 'Koha::Checkouts',
1526 singular => 'checkout',
1527 plural => 'checkouts',
1530 borrower_modifications => {
1531 module => 'Koha::Patron::Modifications',
1532 singular => 'patron_modification',
1533 plural => 'patron_modifications',
1534 fk => 'verification_token',
1538 foreach my $table ( keys %$tables ) {
1539 next unless $config->{$table};
1541 my $ref = ref( $tables->{$table} ) || q{};
1542 my $module = $config->{$table}->{module};
1544 if ( can_load( modules => { $module => undef } ) ) {
1545 my $pk = $config->{$table}->{pk};
1546 my $fk = $config->{$table}->{fk};
1548 if ( $ref eq q{} || $ref eq 'HASH' ) {
1549 my $id = ref $ref eq 'HASH' ? $tables->{$table}->{$pk} : $tables->{$table};
1551 if ( $fk ) { # Using a foreign key for lookup
1552 if ( ref( $fk ) eq 'ARRAY' ) { # Foreign key is multi-column
1554 foreach my $key ( @$fk ) {
1555 $search->{$key} = $id->{$key};
1557 $object = $module->search( $search )->next();
1558 } else { # Foreign key is single column
1559 $object = $module->search( { $fk => $id } )->next();
1561 } else { # using the table's primary key for lookup
1562 $object = $module->find($id);
1564 $params->{ $config->{$table}->{singular} } = $object;
1566 else { # $ref eq 'ARRAY'
1568 if ( @{ $tables->{$table} } == 1 ) { # Param is a single key
1569 $object = $module->search( { $pk => $tables->{$table} } )->next();
1571 else { # Params are mutliple foreign keys
1572 my @values = @{ $tables->{$table} };
1573 my @keys = @{ $config->{$table}->{fk} };
1574 my %params = map { $_ => shift(@values) } @keys;
1575 $object = $module->search( \%params )->next();
1577 $params->{ $config->{$table}->{singular} } = $object;
1581 croak "ERROR LOADING MODULE $module: $Module::Load::Conditional::ERROR";
1585 $params->{today} = dt_from_string();