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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 use C4
::Koha
qw(GetAuthorisedValueByCode);
28 use C4
::Members
::Attributes
qw(GetBorrowerAttributes);
34 use Date
::Calc
qw( Add_Delta_Days );
39 use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
43 # set the version for version checking
44 $VERSION = 3.07.00.049;
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
} }
202 # FIXME: using our here means that a Plack server will need to be
203 # restarted fairly regularly when working with this routine.
204 # A better option would be to use Koha::Cache and use a cache
205 # that actually works in a persistent environment, but as a
206 # short-term fix, our will work.
209 my ( $module, $code, $branchcode, $message_transport_type ) = @_;
210 $message_transport_type ||= 'email';
213 if ( C4
::Context
->preference('IndependentBranches')
215 and C4
::Context
->userenv ) {
217 $branchcode = C4
::Context
->userenv->{'branch'};
221 if ( my $l = $letter{$module}{$code}{$branchcode}{$message_transport_type} ) {
222 return { %$l }; # deep copy
225 my $dbh = C4
::Context
->dbh;
226 my $sth = $dbh->prepare(q{
229 WHERE module=? AND code=? AND (branchcode = ? OR branchcode = '') AND message_transport_type = ?
230 ORDER BY branchcode DESC LIMIT 1
232 $sth->execute( $module, $code, $branchcode, $message_transport_type );
233 my $line = $sth->fetchrow_hashref
235 $line->{'content-type'} = 'text/html; charset="UTF-8"' if $line->{is_html
};
236 $letter{$module}{$code}{$branchcode}{$message_transport_type} = $line;
245 module => 'circulation',
251 Delete the letter. The mtt parameter is facultative.
252 If not given, all templates mathing the other parameters will be removed.
258 my $branchcode = $params->{branchcode
};
259 my $module = $params->{module
};
260 my $code = $params->{code
};
261 my $mtt = $params->{mtt
};
262 my $dbh = C4
::Context
->dbh;
268 | . ( $mtt ? q
| AND message_transport_type
= ?
| : q
|| )
269 , undef, $branchcode, $module, $code, ( $mtt ?
$mtt : () ) );
272 =head2 addalert ($borrowernumber, $type, $externalid)
275 - $borrowernumber : the number of the borrower subscribing to the alert
276 - $type : the type of alert.
277 - $externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
279 create an alert and return the alertid (primary key)
284 my ( $borrowernumber, $type, $externalid ) = @_;
285 my $dbh = C4
::Context
->dbh;
288 "insert into alert (borrowernumber, type, externalid) values (?,?,?)");
289 $sth->execute( $borrowernumber, $type, $externalid );
291 # get the alert number newly created and return it
292 my $alertid = $dbh->{'mysql_insertid'};
296 =head2 delalert ($alertid)
299 - alertid : the alert id
305 my $alertid = shift or die "delalert() called without valid argument (alertid)"; # it's gonna die anyway.
306 $debug and warn "delalert: deleting alertid $alertid";
307 my $sth = C4
::Context
->dbh->prepare("delete from alert where alertid=?");
308 $sth->execute($alertid);
311 =head2 getalert ([$borrowernumber], [$type], [$externalid])
314 - $borrowernumber : the number of the borrower subscribing to the alert
315 - $type : the type of alert.
316 - $externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
317 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.
322 my ( $borrowernumber, $type, $externalid ) = @_;
323 my $dbh = C4
::Context
->dbh;
324 my $query = "SELECT a.*, b.branchcode FROM alert a JOIN borrowers b USING(borrowernumber) WHERE";
326 if ($borrowernumber and $borrowernumber =~ /^\d+$/) {
327 $query .= " borrowernumber=? AND ";
328 push @bind, $borrowernumber;
331 $query .= " type=? AND ";
335 $query .= " externalid=? AND ";
336 push @bind, $externalid;
338 $query =~ s/ AND $//;
339 my $sth = $dbh->prepare($query);
340 $sth->execute(@bind);
341 return $sth->fetchall_arrayref({});
344 =head2 findrelatedto($type, $externalid)
347 - $type : the type of alert
348 - $externalid : the id of the "object" to query
350 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.
351 When type=issue, the id is related to a subscriptionid and this sub returns the name of the biblio.
356 # When type=virtual, the id is related to a virtual shelf and this sub returns the name of the sub
359 my $type = shift or return;
360 my $externalid = shift or return;
361 my $q = ($type eq 'issue' ) ?
362 "select title as result from subscription left join biblio on subscription.biblionumber=biblio.biblionumber where subscriptionid=?" :
363 ($type eq 'borrower') ?
364 "select concat(firstname,' ',surname) from borrowers where borrowernumber=?" : undef;
366 warn "findrelatedto(): Illegal type '$type'";
369 my $sth = C4
::Context
->dbh->prepare($q);
370 $sth->execute($externalid);
371 my ($result) = $sth->fetchrow;
378 - $type : the type of alert
379 - $externalid : the id of the "object" to query
380 - $letter_code : the letter to send.
382 send an alert to all borrowers having put an alert on a given subject.
387 my ( $type, $externalid, $letter_code ) = @_;
388 my $dbh = C4
::Context
->dbh;
389 if ( $type eq 'issue' ) {
391 # prepare the letter...
392 # search the biblionumber
395 "SELECT biblionumber FROM subscription WHERE subscriptionid=?");
396 $sth->execute($externalid);
397 my ($biblionumber) = $sth->fetchrow
398 or warn( "No subscription for '$externalid'" ),
402 # find the list of borrowers to alert
403 my $alerts = getalert
( '', 'issue', $externalid );
405 my $borinfo = C4
::Members
::GetMember
('borrowernumber' => $_->{'borrowernumber'});
406 my $email = $borinfo->{email
} or next;
408 # warn "sending issues...";
409 my $userenv = C4
::Context
->userenv;
410 my $branchdetails = GetBranchDetail
($_->{'branchcode'});
411 my $letter = GetPreparedLetter
(
413 letter_code
=> $letter_code,
414 branchcode
=> $userenv->{branch
},
416 'branches' => $_->{branchcode
},
417 'biblio' => $biblionumber,
418 'biblioitems' => $biblionumber,
419 'borrowers' => $borinfo,
425 my $message = Koha
::Email
->new();
426 my %mail = $message->create_message_headers(
429 from
=> $branchdetails->{'branchemail'},
430 replyto
=> $branchdetails->{'branchreplyto'},
431 sender
=> $branchdetails->{'branchreturnpath'},
432 subject
=> Encode
::encode
( "utf8", "" . $letter->{title
} ),
434 Encode
::encode
( "utf8", "" . $letter->{content
} ),
435 contenttype
=> 'text/plain; charset="utf8"',
439 sendmail
(%mail) or carp
$Mail::Sendmail
::error
;
442 elsif ( $type eq 'claimacquisition' or $type eq 'claimissues' ) {
444 # prepare the letter...
445 # search the biblionumber
446 my $strsth = $type eq 'claimacquisition'
448 SELECT aqorders
.*,aqbasket
.*,biblio
.*,biblioitems
.*
450 LEFT JOIN aqbasket ON aqbasket
.basketno
=aqorders
.basketno
451 LEFT JOIN biblio ON aqorders
.biblionumber
=biblio
.biblionumber
452 LEFT JOIN biblioitems ON aqorders
.biblionumber
=biblioitems
.biblionumber
453 WHERE aqorders
.ordernumber IN
(
456 SELECT serial
.*,subscription
.*, biblio
.*, aqbooksellers
.*,
457 aqbooksellers
.id AS booksellerid
459 LEFT JOIN subscription ON serial
.subscriptionid
=subscription
.subscriptionid
460 LEFT JOIN biblio ON serial
.biblionumber
=biblio
.biblionumber
461 LEFT JOIN aqbooksellers ON subscription
.aqbooksellerid
=aqbooksellers
.id
462 WHERE serial
.serialid IN
(
466 carp
"No Order seleted";
467 return { error
=> "no_order_seleted" };
470 $strsth .= join( ",", @
$externalid ) . ")";
471 my $sthorders = $dbh->prepare($strsth);
473 my $dataorders = $sthorders->fetchall_arrayref( {} );
476 $dbh->prepare("select * from aqbooksellers where id=?");
477 $sthbookseller->execute( $dataorders->[0]->{booksellerid
} );
478 my $databookseller = $sthbookseller->fetchrow_hashref;
479 my $addressee = $type eq 'claimacquisition' ?
'acqprimary' : 'serialsprimary';
481 $dbh->prepare("SELECT * FROM aqcontacts WHERE booksellerid=? AND $type=1 ORDER BY $addressee DESC");
482 $sthcontact->execute( $dataorders->[0]->{booksellerid
} );
483 my $datacontact = $sthcontact->fetchrow_hashref;
487 push @email, $databookseller->{bookselleremail
} if $databookseller->{bookselleremail
};
488 push @email, $datacontact->{email
} if ( $datacontact && $datacontact->{email
} );
490 warn "Bookseller $dataorders->[0]->{booksellerid} without emails";
491 return { error
=> "no_email" };
494 while ($addlcontact = $sthcontact->fetchrow_hashref) {
495 push @cc, $addlcontact->{email
} if ( $addlcontact && $addlcontact->{email
} );
498 my $userenv = C4
::Context
->userenv;
499 my $letter = GetPreparedLetter
(
501 letter_code
=> $letter_code,
502 branchcode
=> $userenv->{branch
},
504 'branches' => $userenv->{branch
},
505 'aqbooksellers' => $databookseller,
506 'aqcontacts' => $datacontact,
508 repeat
=> $dataorders,
512 # Remove the order tag
513 $letter->{content
} =~ s/<order>(.*?)<\/order>/$1/gxms
;
517 To
=> join( ',', @email),
518 Cc
=> join( ',', @cc),
519 From
=> $userenv->{emailaddress
},
520 Subject
=> Encode
::encode
( "utf8", "" . $letter->{title
} ),
521 Message
=> Encode
::encode
( "utf8", "" . $letter->{content
} ),
522 'Content-Type' => 'text/plain; charset="utf8"',
525 $mail{'Reply-to'} = C4
::Context
->preference('ReplytoDefault')
526 if C4
::Context
->preference('ReplytoDefault');
527 $mail{'Sender'} = C4
::Context
->preference('ReturnpathDefault')
528 if C4
::Context
->preference('ReturnpathDefault');
530 unless ( sendmail
(%mail) ) {
531 carp
$Mail::Sendmail
::error
;
532 return { error
=> $Mail::Sendmail
::error
};
537 $type eq 'claimissues' ?
"CLAIM ISSUE" : "ACQUISITION CLAIM",
540 . join( ',', @email )
545 ) if C4
::Context
->preference("LetterLog");
547 # send an "account details" notice to a newly created user
548 elsif ( $type eq 'members' ) {
549 my $branchdetails = GetBranchDetail
($externalid->{'branchcode'});
550 my $letter = GetPreparedLetter
(
552 letter_code
=> $letter_code,
553 branchcode
=> $externalid->{'branchcode'},
555 'branches' => $branchdetails,
556 'borrowers' => $externalid->{'borrowernumber'},
558 substitute
=> { 'borrowers.password' => $externalid->{'password'} },
561 return { error
=> "no_email" } unless $externalid->{'emailaddr'};
562 my $email = Koha
::Email
->new();
563 my %mail = $email->create_message_headers(
565 to
=> $externalid->{'emailaddr'},
566 from
=> $branchdetails->{'branchemail'},
567 replyto
=> $branchdetails->{'branchreplyto'},
568 sender
=> $branchdetails->{'branchreturnpath'},
569 subject
=> Encode
::encode
( "utf8", "" . $letter->{'title'} ),
570 message
=> Encode
::encode
( "utf8", "" . $letter->{'content'} ),
571 contenttype
=> 'text/plain; charset="utf8"'
574 sendmail
(%mail) or carp
$Mail::Sendmail
::error
;
578 =head2 GetPreparedLetter( %params )
581 module => letter module, mandatory
582 letter_code => letter code, mandatory
583 branchcode => for letter selection, if missing default system letter taken
584 tables => a hashref with table names as keys. Values are either:
585 - a scalar - primary key value
586 - an arrayref - primary key values
587 - a hashref - full record
588 substitute => custom substitution key/value pairs
589 repeat => records to be substituted on consecutive lines:
590 - an arrayref - tries to guess what needs substituting by
591 taking remaining << >> tokensr; not recommended
592 - a hashref token => @tables - replaces <token> << >> << >> </token>
593 subtemplate for each @tables row; table is a hashref as above
594 want_librarian => boolean, if set to true triggers librarian details
595 substitution from the userenv
597 letter fields hashref (title & content useful)
601 sub GetPreparedLetter
{
604 my $module = $params{module
} or croak
"No module";
605 my $letter_code = $params{letter_code
} or croak
"No letter_code";
606 my $branchcode = $params{branchcode
} || '';
607 my $mtt = $params{message_transport_type
} || 'email';
609 my $letter = getletter
( $module, $letter_code, $branchcode, $mtt )
610 or warn( "No $module $letter_code letter transported by " . $mtt ),
613 my $tables = $params{tables
};
614 my $substitute = $params{substitute
};
615 my $repeat = $params{repeat
};
616 $tables || $substitute || $repeat
617 or carp
( "ERROR: nothing to substitute - both 'tables' and 'substitute' are empty" ),
619 my $want_librarian = $params{want_librarian
};
622 while ( my ($token, $val) = each %$substitute ) {
623 $letter->{title
} =~ s/<<$token>>/$val/g;
624 $letter->{content
} =~ s/<<$token>>/$val/g;
628 my $OPACBaseURL = C4
::Context
->preference('OPACBaseURL');
629 $letter->{content
} =~ s/<<OPACBaseURL>>/$OPACBaseURL/go;
631 if ($want_librarian) {
632 # parsing librarian name
633 my $userenv = C4
::Context
->userenv;
634 $letter->{content
} =~ s/<<LibrarianFirstname>>/$userenv->{firstname}/go;
635 $letter->{content
} =~ s/<<LibrarianSurname>>/$userenv->{surname}/go;
636 $letter->{content
} =~ s/<<LibrarianEmailaddress>>/$userenv->{emailaddress}/go;
639 my ($repeat_no_enclosing_tags, $repeat_enclosing_tags);
642 if (ref ($repeat) eq 'ARRAY' ) {
643 $repeat_no_enclosing_tags = $repeat;
645 $repeat_enclosing_tags = $repeat;
649 if ($repeat_enclosing_tags) {
650 while ( my ($tag, $tag_tables) = each %$repeat_enclosing_tags ) {
651 if ( $letter->{content
} =~ m!<$tag>(.*)</$tag>!s ) {
654 my %subletter = ( title
=> '', content
=> $subcontent );
655 _substitute_tables
( \
%subletter, $_ );
658 $letter->{content
} =~ s!<$tag>.*</$tag>!join( "\n", @lines )!se;
664 _substitute_tables
( $letter, $tables );
667 if ($repeat_no_enclosing_tags) {
668 if ( $letter->{content
} =~ m/[^\n]*<<.*>>[^\n]*/so ) {
673 $c =~ s/<<count>>/$i/go;
674 foreach my $field ( keys %{$_} ) {
675 $c =~ s/(<<[^\.]+.$field>>)/$_->{$field}/;
679 } @
$repeat_no_enclosing_tags;
681 my $replaceby = join( "\n", @lines );
682 $letter->{content
} =~ s/\Q$line\E/$replaceby/s;
686 $letter->{content
} =~ s/<<\S*>>//go; #remove any stragglers
687 # $letter->{content} =~ s/<<[^>]*>>//go;
692 sub _substitute_tables
{
693 my ( $letter, $tables ) = @_;
694 while ( my ($table, $param) = each %$tables ) {
697 my $ref = ref $param;
700 if ($ref && $ref eq 'HASH') {
704 my $sth = _parseletter_sth
($table);
706 warn "_parseletter_sth('$table') failed to return a valid sth. No substitution will be done for that table.";
709 $sth->execute( $ref ? @
$param : $param );
711 $values = $sth->fetchrow_hashref;
715 _parseletter
( $letter, $table, $values );
719 sub _parseletter_sth
{
723 carp
"ERROR: _parseletter_sth() called without argument (table)";
726 # NOTE: we used to check whether we had a statement handle cached in
727 # a %handles module-level variable. This was a dumb move and
728 # broke things for the rest of us. prepare_cached is a better
729 # way to cache statement handles anyway.
731 ($table eq 'biblio' ) ?
"SELECT * FROM $table WHERE biblionumber = ?" :
732 ($table eq 'biblioitems' ) ?
"SELECT * FROM $table WHERE biblionumber = ?" :
733 ($table eq 'items' ) ?
"SELECT * FROM $table WHERE itemnumber = ?" :
734 ($table eq 'issues' ) ?
"SELECT * FROM $table WHERE itemnumber = ?" :
735 ($table eq 'old_issues' ) ?
"SELECT * FROM $table WHERE itemnumber = ? ORDER BY timestamp DESC LIMIT 1" :
736 ($table eq 'reserves' ) ?
"SELECT * FROM $table WHERE borrowernumber = ? and biblionumber = ?" :
737 ($table eq 'borrowers' ) ?
"SELECT * FROM $table WHERE borrowernumber = ?" :
738 ($table eq 'branches' ) ?
"SELECT * FROM $table WHERE branchcode = ?" :
739 ($table eq 'suggestions' ) ?
"SELECT * FROM $table WHERE suggestionid = ?" :
740 ($table eq 'aqbooksellers') ?
"SELECT * FROM $table WHERE id = ?" :
741 ($table eq 'aqorders' ) ?
"SELECT * FROM $table WHERE ordernumber = ?" :
742 ($table eq 'opac_news' ) ?
"SELECT * FROM $table WHERE idnew = ?" :
743 ($table eq 'borrower_modifications') ?
"SELECT * FROM $table WHERE verification_token = ?" :
746 warn "ERROR: No _parseletter_sth query for table '$table'";
747 return; # nothing to get
749 unless ($sth = C4
::Context
->dbh->prepare_cached($query)) {
750 warn "ERROR: Failed to prepare query: '$query'";
753 return $sth; # now cache is populated for that $table
756 =head2 _parseletter($letter, $table, $values)
759 - $letter : a hash to letter fields (title & content useful)
760 - $table : the Koha table to parse.
761 - $values : table record hashref
762 parse all fields from a table, and replace values in title & content with the appropriate value
763 (not exported sub, used only internally)
768 my ( $letter, $table, $values ) = @_;
770 if ( $table eq 'reserves' && $values->{'waitingdate'} ) {
771 my @waitingdate = split /-/, $values->{'waitingdate'};
773 $values->{'expirationdate'} = '';
774 if( C4
::Context
->preference('ExpireReservesMaxPickUpDelay') &&
775 C4
::Context
->preference('ReservesMaxPickUpDelay') ) {
776 my $dt = dt_from_string
();
777 $dt->add( days
=> C4
::Context
->preference('ReservesMaxPickUpDelay') );
778 $values->{'expirationdate'} = output_pref
({ dt
=> $dt, dateonly
=> 1 });
781 $values->{'waitingdate'} = output_pref
({ dt
=> dt_from_string
( $values->{'waitingdate'} ), dateonly
=> 1 });
785 if ($letter->{content
} && $letter->{content
} =~ /<<today>>/) {
786 my $todaysdate = output_pref
( DateTime
->now() );
787 $letter->{content
} =~ s/<<today>>/$todaysdate/go;
790 while ( my ($field, $val) = each %$values ) {
791 my $replacetablefield = "<<$table.$field>>";
792 my $replacefield = "<<$field>>";
793 $val =~ s/\p{P}$// if $val && $table=~/biblio/;
794 #BZ 9886: Assuming that we want to eliminate ISBD punctuation here
795 #Therefore adding the test on biblio. This includes biblioitems,
796 #but excludes items. Removed unneeded global and lookahead.
798 $val = GetAuthorisedValueByCode
('ROADTYPE', $val, 0) if $table=~/^borrowers$/ && $field=~/^streettype$/;
799 my $replacedby = defined ($val) ?
$val : '';
801 and not $replacedby =~ m
|0000-00-00|
802 and not $replacedby =~ m
|9999-12-31|
803 and $replacedby =~ m
|^\d
{4}-\d
{2}-\d
{2}( \d
{2}:\d
{2}:\d
{2})?
$| )
805 # If the value is XXXX-YY-ZZ[ AA:BB:CC] we assume it is a date
806 my $dateonly = defined $1 ?
0 : 1; #$1 refers to the capture group wrapped in parentheses. In this case, that's the hours, minutes, seconds.
808 $replacedby = output_pref
({ dt
=> dt_from_string
( $replacedby ), dateonly
=> $dateonly });
810 warn "$replacedby seems to be a date but an error occurs on generating it ($@)" if $@
;
812 ($letter->{title
} ) and do {
813 $letter->{title
} =~ s/$replacetablefield/$replacedby/g;
814 $letter->{title
} =~ s/$replacefield/$replacedby/g;
816 ($letter->{content
}) and do {
817 $letter->{content
} =~ s/$replacetablefield/$replacedby/g;
818 $letter->{content
} =~ s/$replacefield/$replacedby/g;
822 if ($table eq 'borrowers' && $letter->{content
}) {
823 if ( my $attributes = GetBorrowerAttributes
($values->{borrowernumber
}) ) {
825 foreach (@
$attributes) {
826 my $code = $_->{code
};
827 my $val = $_->{value_description
} || $_->{value
};
828 $val =~ s/\p{P}(?=$)//g if $val;
829 next unless $val gt '';
831 push @
{ $attr{$code} }, $val;
833 while ( my ($code, $val_ar) = each %attr ) {
834 my $replacefield = "<<borrower-attribute:$code>>";
835 my $replacedby = join ',', @
$val_ar;
836 $letter->{content
} =~ s/$replacefield/$replacedby/g;
845 my $success = EnqueueLetter( { letter => $letter,
846 borrowernumber => '12', message_transport_type => 'email' } )
848 places a letter in the message_queue database table, which will
849 eventually get processed (sent) by the process_message_queue.pl
850 cronjob when it calls SendQueuedMessages.
852 return message_id on success
857 my $params = shift or return;
859 return unless exists $params->{'letter'};
860 # return unless exists $params->{'borrowernumber'};
861 return unless exists $params->{'message_transport_type'};
863 my $content = $params->{letter
}->{content
};
864 $content =~ s/\s+//g if(defined $content);
865 if ( not defined $content or $content eq '' ) {
866 warn "Trying to add an empty message to the message queue" if $debug;
870 # If we have any attachments we should encode then into the body.
871 if ( $params->{'attachments'} ) {
872 $params->{'letter'} = _add_attachments
(
873 { letter
=> $params->{'letter'},
874 attachments
=> $params->{'attachments'},
875 message
=> MIME
::Lite
->new( Type
=> 'multipart/mixed' ),
880 my $dbh = C4
::Context
->dbh();
881 my $statement = << 'ENDSQL';
882 INSERT INTO message_queue
883 ( borrowernumber
, subject
, content
, metadata
, letter_code
, message_transport_type
, status
, time_queued
, to_address
, from_address
, content_type
)
885 ( ?
, ?
, ?
, ?
, ?
, ?
, ?
, NOW
(), ?
, ?
, ?
)
888 my $sth = $dbh->prepare($statement);
889 my $result = $sth->execute(
890 $params->{'borrowernumber'}, # borrowernumber
891 $params->{'letter'}->{'title'}, # subject
892 $params->{'letter'}->{'content'}, # content
893 $params->{'letter'}->{'metadata'} || '', # metadata
894 $params->{'letter'}->{'code'} || '', # letter_code
895 $params->{'message_transport_type'}, # message_transport_type
897 $params->{'to_address'}, # to_address
898 $params->{'from_address'}, # from_address
899 $params->{'letter'}->{'content-type'}, # content_type
901 return $dbh->last_insert_id(undef,undef,'message_queue', undef);
904 =head2 SendQueuedMessages ([$hashref])
906 my $sent = SendQueuedMessages( { verbose => 1 } );
908 sends all of the 'pending' items in the message queue.
910 returns number of messages sent.
914 sub SendQueuedMessages
{
917 my $unsent_messages = _get_unsent_messages
();
918 MESSAGE
: foreach my $message ( @
$unsent_messages ) {
919 # warn Data::Dumper->Dump( [ $message ], [ 'message' ] );
920 warn sprintf( 'sending %s message to patron: %s',
921 $message->{'message_transport_type'},
922 $message->{'borrowernumber'} || 'Admin' )
923 if $params->{'verbose'} or $debug;
924 # This is just begging for subclassing
925 next MESSAGE
if ( lc($message->{'message_transport_type'}) eq 'rss' );
926 if ( lc( $message->{'message_transport_type'} ) eq 'email' ) {
927 _send_message_by_email
( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
929 elsif ( lc( $message->{'message_transport_type'} ) eq 'sms' ) {
930 _send_message_by_sms
( $message );
933 return scalar( @
$unsent_messages );
936 =head2 GetRSSMessages
938 my $message_list = GetRSSMessages( { limit => 10, borrowernumber => '14' } )
940 returns a listref of all queued RSS messages for a particular person.
947 return unless $params;
948 return unless ref $params;
949 return unless $params->{'borrowernumber'};
951 return _get_unsent_messages
( { message_transport_type
=> 'rss',
952 limit
=> $params->{'limit'},
953 borrowernumber
=> $params->{'borrowernumber'}, } );
956 =head2 GetPrintMessages
958 my $message_list = GetPrintMessages( { borrowernumber => $borrowernumber } )
960 Returns a arrayref of all queued print messages (optionally, for a particular
965 sub GetPrintMessages
{
966 my $params = shift || {};
968 return _get_unsent_messages
( { message_transport_type
=> 'print',
969 borrowernumber
=> $params->{'borrowernumber'},
973 =head2 GetQueuedMessages ([$hashref])
975 my $messages = GetQueuedMessage( { borrowernumber => '123', limit => 20 } );
977 fetches messages out of the message queue.
980 list of hashes, each has represents a message in the message queue.
984 sub GetQueuedMessages
{
987 my $dbh = C4
::Context
->dbh();
988 my $statement = << 'ENDSQL';
989 SELECT message_id
, borrowernumber
, subject
, content
, message_transport_type
, status
, time_queued
995 if ( exists $params->{'borrowernumber'} ) {
996 push @whereclauses, ' borrowernumber = ? ';
997 push @query_params, $params->{'borrowernumber'};
1000 if ( @whereclauses ) {
1001 $statement .= ' WHERE ' . join( 'AND', @whereclauses );
1004 if ( defined $params->{'limit'} ) {
1005 $statement .= ' LIMIT ? ';
1006 push @query_params, $params->{'limit'};
1009 my $sth = $dbh->prepare( $statement );
1010 my $result = $sth->execute( @query_params );
1011 return $sth->fetchall_arrayref({});
1014 =head2 GetMessageTransportTypes
1016 my @mtt = GetMessageTransportTypes();
1018 returns an arrayref of transport types
1022 sub GetMessageTransportTypes
{
1023 my $dbh = C4
::Context
->dbh();
1024 my $mtts = $dbh->selectcol_arrayref("
1025 SELECT message_transport_type
1026 FROM message_transport_types
1027 ORDER BY message_transport_type
1032 =head2 _add_attachements
1035 letter - the standard letter hashref
1036 attachments - listref of attachments. each attachment is a hashref of:
1037 type - the mime type, like 'text/plain'
1038 content - the actual attachment
1039 filename - the name of the attachment.
1040 message - a MIME::Lite object to attach these to.
1042 returns your letter object, with the content updated.
1046 sub _add_attachments
{
1049 my $letter = $params->{'letter'};
1050 my $attachments = $params->{'attachments'};
1051 return $letter unless @
$attachments;
1052 my $message = $params->{'message'};
1054 # First, we have to put the body in as the first attachment
1056 Type
=> $letter->{'content-type'} || 'TEXT',
1057 Data
=> $letter->{'is_html'}
1058 ? _wrap_html
($letter->{'content'}, $letter->{'title'})
1059 : $letter->{'content'},
1062 foreach my $attachment ( @
$attachments ) {
1064 Type
=> $attachment->{'type'},
1065 Data
=> $attachment->{'content'},
1066 Filename
=> $attachment->{'filename'},
1069 # we're forcing list context here to get the header, not the count back from grep.
1070 ( $letter->{'content-type'} ) = grep( /^Content-Type:/, split( /\n/, $params->{'message'}->header_as_string ) );
1071 $letter->{'content-type'} =~ s/^Content-Type:\s+//;
1072 $letter->{'content'} = $message->body_as_string;
1078 sub _get_unsent_messages
{
1081 my $dbh = C4
::Context
->dbh();
1082 my $statement = << 'ENDSQL';
1083 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
1084 FROM message_queue mq
1085 LEFT JOIN borrowers b ON b
.borrowernumber
= mq
.borrowernumber
1089 my @query_params = ('pending');
1090 if ( ref $params ) {
1091 if ( $params->{'message_transport_type'} ) {
1092 $statement .= ' AND message_transport_type = ? ';
1093 push @query_params, $params->{'message_transport_type'};
1095 if ( $params->{'borrowernumber'} ) {
1096 $statement .= ' AND borrowernumber = ? ';
1097 push @query_params, $params->{'borrowernumber'};
1099 if ( $params->{'limit'} ) {
1100 $statement .= ' limit ? ';
1101 push @query_params, $params->{'limit'};
1105 $debug and warn "_get_unsent_messages SQL: $statement";
1106 $debug and warn "_get_unsent_messages params: " . join(',',@query_params);
1107 my $sth = $dbh->prepare( $statement );
1108 my $result = $sth->execute( @query_params );
1109 return $sth->fetchall_arrayref({});
1112 sub _send_message_by_email
{
1113 my $message = shift or return;
1114 my ($username, $password, $method) = @_;
1116 my $member = C4
::Members
::GetMember
( 'borrowernumber' => $message->{'borrowernumber'} );
1117 my $to_address = $message->{'to_address'};
1118 unless ($to_address) {
1120 warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})";
1121 _set_message_status
( { message_id
=> $message->{'message_id'},
1122 status
=> 'failed' } );
1125 $to_address = C4
::Members
::GetNoticeEmailAddress
( $message->{'borrowernumber'} );
1126 unless ($to_address) {
1127 # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})";
1128 # warning too verbose for this more common case?
1129 _set_message_status
( { message_id
=> $message->{'message_id'},
1130 status
=> 'failed' } );
1135 my $utf8 = decode
('MIME-Header', $message->{'subject'} );
1136 $message->{subject
}= encode
('MIME-Header', $utf8);
1137 my $subject = encode
('utf8', $message->{'subject'});
1138 my $content = encode
('utf8', $message->{'content'});
1139 my $content_type = $message->{'content_type'} || 'text/plain; charset="UTF-8"';
1140 my $is_html = $content_type =~ m/html/io;
1141 my $branch_email = undef;
1142 my $branch_replyto = undef;
1143 my $branch_returnpath = undef;
1145 my $branchdetail = GetBranchDetail
( $member->{'branchcode'} );
1146 $branch_email = $branchdetail->{'branchemail'};
1147 $branch_replyto = $branchdetail->{'branchreplyto'};
1148 $branch_returnpath = $branchdetail->{'branchreturnpath'};
1150 my $email = Koha
::Email
->new();
1151 my %sendmail_params = $email->create_message_headers(
1154 from
=> $message->{'from_address'} || $branch_email,
1155 replyto
=> $branch_replyto,
1156 sender
=> $branch_returnpath,
1157 subject
=> $subject,
1158 message
=> $is_html ? _wrap_html
( $content, $subject ) : $content,
1159 contenttype
=> $content_type
1163 $sendmail_params{'Auth'} = {user
=> $username, pass
=> $password, method
=> $method} if $username;
1164 if ( my $bcc = C4
::Context
->preference('OverdueNoticeBcc') ) {
1165 $sendmail_params{ Bcc
} = $bcc;
1168 _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
1169 if ( sendmail
( %sendmail_params ) ) {
1170 _set_message_status
( { message_id
=> $message->{'message_id'},
1171 status
=> 'sent' } );
1174 _set_message_status
( { message_id
=> $message->{'message_id'},
1175 status
=> 'failed' } );
1176 carp
$Mail::Sendmail
::error
;
1182 my ($content, $title) = @_;
1184 my $css = C4
::Context
->preference("NoticeCSS") || '';
1185 $css = qq{<link rel
="stylesheet" type
="text/css" href
="$css">} if $css;
1187 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
1188 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1189 <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
1191 <title>$title</title>
1192 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
1203 my ( $message ) = @_;
1204 my $dbh = C4
::Context
->dbh;
1205 my $count = $dbh->selectrow_array(q
|
1208 WHERE message_transport_type
= ?
1209 AND borrowernumber
= ?
1211 AND CAST
(time_queued AS date
) = CAST
(NOW
() AS date
)
1214 |, {}, $message->{message_transport_type
}, $message->{borrowernumber
}, $message->{letter_code
}, $message->{content
} );
1218 sub _send_message_by_sms
{
1219 my $message = shift or return;
1220 my $member = C4
::Members
::GetMember
( 'borrowernumber' => $message->{'borrowernumber'} );
1222 unless ( $member->{smsalertnumber
} ) {
1223 _set_message_status
( { message_id
=> $message->{'message_id'},
1224 status
=> 'failed' } );
1228 if ( _is_duplicate
( $message ) ) {
1229 _set_message_status
( { message_id
=> $message->{'message_id'},
1230 status
=> 'failed' } );
1234 my $success = C4
::SMS
->send_sms( { destination
=> $member->{'smsalertnumber'},
1235 message
=> $message->{'content'},
1237 _set_message_status
( { message_id
=> $message->{'message_id'},
1238 status
=> ($success ?
'sent' : 'failed') } );
1242 sub _update_message_to_address
{
1244 my $dbh = C4
::Context
->dbh();
1245 $dbh->do('UPDATE message_queue SET to_address=? WHERE message_id=?',undef,($to,$id));
1248 sub _set_message_status
{
1249 my $params = shift or return;
1251 foreach my $required_parameter ( qw( message_id status ) ) {
1252 return unless exists $params->{ $required_parameter };
1255 my $dbh = C4
::Context
->dbh();
1256 my $statement = 'UPDATE message_queue SET status= ? WHERE message_id = ?';
1257 my $sth = $dbh->prepare( $statement );
1258 my $result = $sth->execute( $params->{'status'},
1259 $params->{'message_id'} );