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 &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 $dbh = C4
::Context
->dbh;
79 my $letters = $dbh->selectall_arrayref(
81 SELECT module
, code
, branchcode
, name
85 . ( $module ? q
| AND module
= ?
| : q
|| )
86 . ( $code ? q
| AND code
= ?
| : q
|| )
87 . q
| GROUP BY code ORDER BY name
|, { Slice
=> {} }
88 , ( $module ?
$module : () )
89 , ( $code ?
$code : () )
95 # FIXME: using our here means that a Plack server will need to be
96 # restarted fairly regularly when working with this routine.
97 # A better option would be to use Koha::Cache and use a cache
98 # that actually works in a persistent environment, but as a
99 # short-term fix, our will work.
102 my ( $module, $code, $branchcode, $message_transport_type ) = @_;
103 $message_transport_type ||= 'email';
106 if ( C4
::Context
->preference('IndependentBranches')
108 and C4
::Context
->userenv ) {
110 $branchcode = C4
::Context
->userenv->{'branch'};
114 if ( my $l = $letter{$module}{$code}{$branchcode}{$message_transport_type} ) {
115 return { %$l }; # deep copy
118 my $dbh = C4
::Context
->dbh;
119 my $sth = $dbh->prepare(q{
122 WHERE module=? AND code=? AND (branchcode = ? OR branchcode = '') AND message_transport_type = ?
123 ORDER BY branchcode DESC LIMIT 1
125 $sth->execute( $module, $code, $branchcode, $message_transport_type );
126 my $line = $sth->fetchrow_hashref
128 $line->{'content-type'} = 'text/html; charset="UTF-8"' if $line->{is_html
};
129 $letter{$module}{$code}{$branchcode}{$message_transport_type} = $line;
133 =head2 addalert ($borrowernumber, $type, $externalid)
136 - $borrowernumber : the number of the borrower subscribing to the alert
137 - $type : the type of alert.
138 - $externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
140 create an alert and return the alertid (primary key)
145 my ( $borrowernumber, $type, $externalid ) = @_;
146 my $dbh = C4
::Context
->dbh;
149 "insert into alert (borrowernumber, type, externalid) values (?,?,?)");
150 $sth->execute( $borrowernumber, $type, $externalid );
152 # get the alert number newly created and return it
153 my $alertid = $dbh->{'mysql_insertid'};
157 =head2 delalert ($alertid)
160 - alertid : the alert id
166 my $alertid = shift or die "delalert() called without valid argument (alertid)"; # it's gonna die anyway.
167 $debug and warn "delalert: deleting alertid $alertid";
168 my $sth = C4
::Context
->dbh->prepare("delete from alert where alertid=?");
169 $sth->execute($alertid);
172 =head2 getalert ([$borrowernumber], [$type], [$externalid])
175 - $borrowernumber : the number of the borrower subscribing to the alert
176 - $type : the type of alert.
177 - $externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
178 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.
183 my ( $borrowernumber, $type, $externalid ) = @_;
184 my $dbh = C4
::Context
->dbh;
185 my $query = "SELECT a.*, b.branchcode FROM alert a JOIN borrowers b USING(borrowernumber) WHERE";
187 if ($borrowernumber and $borrowernumber =~ /^\d+$/) {
188 $query .= " borrowernumber=? AND ";
189 push @bind, $borrowernumber;
192 $query .= " type=? AND ";
196 $query .= " externalid=? AND ";
197 push @bind, $externalid;
199 $query =~ s/ AND $//;
200 my $sth = $dbh->prepare($query);
201 $sth->execute(@bind);
202 return $sth->fetchall_arrayref({});
205 =head2 findrelatedto($type, $externalid)
208 - $type : the type of alert
209 - $externalid : the id of the "object" to query
211 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.
212 When type=issue, the id is related to a subscriptionid and this sub returns the name of the biblio.
217 # When type=virtual, the id is related to a virtual shelf and this sub returns the name of the sub
220 my $type = shift or return;
221 my $externalid = shift or return;
222 my $q = ($type eq 'issue' ) ?
223 "select title as result from subscription left join biblio on subscription.biblionumber=biblio.biblionumber where subscriptionid=?" :
224 ($type eq 'borrower') ?
225 "select concat(firstname,' ',surname) from borrowers where borrowernumber=?" : undef;
227 warn "findrelatedto(): Illegal type '$type'";
230 my $sth = C4
::Context
->dbh->prepare($q);
231 $sth->execute($externalid);
232 my ($result) = $sth->fetchrow;
239 - $type : the type of alert
240 - $externalid : the id of the "object" to query
241 - $letter_code : the letter to send.
243 send an alert to all borrowers having put an alert on a given subject.
248 my ( $type, $externalid, $letter_code ) = @_;
249 my $dbh = C4
::Context
->dbh;
250 if ( $type eq 'issue' ) {
252 # prepare the letter...
253 # search the biblionumber
256 "SELECT biblionumber FROM subscription WHERE subscriptionid=?");
257 $sth->execute($externalid);
258 my ($biblionumber) = $sth->fetchrow
259 or warn( "No subscription for '$externalid'" ),
263 # find the list of borrowers to alert
264 my $alerts = getalert
( '', 'issue', $externalid );
266 my $borinfo = C4
::Members
::GetMember
('borrowernumber' => $_->{'borrowernumber'});
267 my $email = $borinfo->{email
} or next;
269 # warn "sending issues...";
270 my $userenv = C4
::Context
->userenv;
271 my $branchdetails = GetBranchDetail
($_->{'branchcode'});
272 my $letter = GetPreparedLetter
(
274 letter_code
=> $letter_code,
275 branchcode
=> $userenv->{branch
},
277 'branches' => $_->{branchcode
},
278 'biblio' => $biblionumber,
279 'biblioitems' => $biblionumber,
280 'borrowers' => $borinfo,
286 my $message = Koha
::Email
->new();
287 my %mail = $message->create_message_headers(
290 from
=> $branchdetails->{'branchemail'},
291 replyto
=> $branchdetails->{'branchreplyto'},
292 sender
=> $branchdetails->{'branchreturnpath'},
293 subject
=> Encode
::encode
( "utf8", "" . $letter->{title
} ),
295 Encode
::encode
( "utf8", "" . $letter->{content
} ),
296 contenttype
=> 'text/plain; charset="utf8"',
300 sendmail
(%mail) or carp
$Mail::Sendmail
::error
;
303 elsif ( $type eq 'claimacquisition' or $type eq 'claimissues' ) {
305 # prepare the letter...
306 # search the biblionumber
307 my $strsth = $type eq 'claimacquisition'
309 SELECT aqorders
.*,aqbasket
.*,biblio
.*,biblioitems
.*
311 LEFT JOIN aqbasket ON aqbasket
.basketno
=aqorders
.basketno
312 LEFT JOIN biblio ON aqorders
.biblionumber
=biblio
.biblionumber
313 LEFT JOIN biblioitems ON aqorders
.biblionumber
=biblioitems
.biblionumber
314 WHERE aqorders
.ordernumber IN
(
317 SELECT serial
.*,subscription
.*, biblio
.*, aqbooksellers
.*,
318 aqbooksellers
.id AS booksellerid
320 LEFT JOIN subscription ON serial
.subscriptionid
=subscription
.subscriptionid
321 LEFT JOIN biblio ON serial
.biblionumber
=biblio
.biblionumber
322 LEFT JOIN aqbooksellers ON subscription
.aqbooksellerid
=aqbooksellers
.id
323 WHERE serial
.serialid IN
(
327 carp
"No Order seleted";
328 return { error
=> "no_order_seleted" };
331 $strsth .= join( ",", @
$externalid ) . ")";
332 my $sthorders = $dbh->prepare($strsth);
334 my $dataorders = $sthorders->fetchall_arrayref( {} );
337 $dbh->prepare("select * from aqbooksellers where id=?");
338 $sthbookseller->execute( $dataorders->[0]->{booksellerid
} );
339 my $databookseller = $sthbookseller->fetchrow_hashref;
340 my $addressee = $type eq 'claimacquisition' ?
'acqprimary' : 'serialsprimary';
342 $dbh->prepare("SELECT * FROM aqcontacts WHERE booksellerid=? AND $type=1 ORDER BY $addressee DESC");
343 $sthcontact->execute( $dataorders->[0]->{booksellerid
} );
344 my $datacontact = $sthcontact->fetchrow_hashref;
348 push @email, $databookseller->{bookselleremail
} if $databookseller->{bookselleremail
};
349 push @email, $datacontact->{email
} if ( $datacontact && $datacontact->{email
} );
351 warn "Bookseller $dataorders->[0]->{booksellerid} without emails";
352 return { error
=> "no_email" };
355 while ($addlcontact = $sthcontact->fetchrow_hashref) {
356 push @cc, $addlcontact->{email
} if ( $addlcontact && $addlcontact->{email
} );
359 my $userenv = C4
::Context
->userenv;
360 my $letter = GetPreparedLetter
(
362 letter_code
=> $letter_code,
363 branchcode
=> $userenv->{branch
},
365 'branches' => $userenv->{branch
},
366 'aqbooksellers' => $databookseller,
367 'aqcontacts' => $datacontact,
369 repeat
=> $dataorders,
375 To
=> join( ',', @email),
376 Cc
=> join( ',', @cc),
377 From
=> $userenv->{emailaddress
},
378 Subject
=> Encode
::encode
( "utf8", "" . $letter->{title
} ),
379 Message
=> Encode
::encode
( "utf8", "" . $letter->{content
} ),
380 'Content-Type' => 'text/plain; charset="utf8"',
383 $mail{'Reply-to'} = C4
::Context
->preference('ReplytoDefault')
384 if C4
::Context
->preference('ReplytoDefault');
385 $mail{'Sender'} = C4
::Context
->preference('ReturnpathDefault')
386 if C4
::Context
->preference('ReturnpathDefault');
388 unless ( sendmail
(%mail) ) {
389 carp
$Mail::Sendmail
::error
;
390 return { error
=> $Mail::Sendmail
::error
};
395 $type eq 'claimissues' ?
"CLAIM ISSUE" : "ACQUISITION CLAIM",
398 . join( ',', @email )
403 ) if C4
::Context
->preference("LetterLog");
405 # send an "account details" notice to a newly created user
406 elsif ( $type eq 'members' ) {
407 my $branchdetails = GetBranchDetail
($externalid->{'branchcode'});
408 my $letter = GetPreparedLetter
(
410 letter_code
=> $letter_code,
411 branchcode
=> $externalid->{'branchcode'},
413 'branches' => $branchdetails,
414 'borrowers' => $externalid->{'borrowernumber'},
416 substitute
=> { 'borrowers.password' => $externalid->{'password'} },
419 return { error
=> "no_email" } unless $externalid->{'emailaddr'};
420 my $email = Koha
::Email
->new();
421 my %mail = $email->create_message_headers(
423 to
=> $externalid->{'emailaddr'},
424 from
=> $branchdetails->{'branchemail'},
425 replyto
=> $branchdetails->{'branchreplyto'},
426 sender
=> $branchdetails->{'branchreturnpath'},
427 subject
=> Encode
::encode
( "utf8", "" . $letter->{'title'} ),
428 message
=> Encode
::encode
( "utf8", "" . $letter->{'content'} ),
429 contenttype
=> 'text/plain; charset="utf8"'
432 sendmail
(%mail) or carp
$Mail::Sendmail
::error
;
436 =head2 GetPreparedLetter( %params )
439 module => letter module, mandatory
440 letter_code => letter code, mandatory
441 branchcode => for letter selection, if missing default system letter taken
442 tables => a hashref with table names as keys. Values are either:
443 - a scalar - primary key value
444 - an arrayref - primary key values
445 - a hashref - full record
446 substitute => custom substitution key/value pairs
447 repeat => records to be substituted on consecutive lines:
448 - an arrayref - tries to guess what needs substituting by
449 taking remaining << >> tokensr; not recommended
450 - a hashref token => @tables - replaces <token> << >> << >> </token>
451 subtemplate for each @tables row; table is a hashref as above
452 want_librarian => boolean, if set to true triggers librarian details
453 substitution from the userenv
455 letter fields hashref (title & content useful)
459 sub GetPreparedLetter
{
462 my $module = $params{module
} or croak
"No module";
463 my $letter_code = $params{letter_code
} or croak
"No letter_code";
464 my $branchcode = $params{branchcode
} || '';
465 my $mtt = $params{message_transport_type
} || 'email';
467 my $letter = getletter
( $module, $letter_code, $branchcode, $mtt )
468 or warn( "No $module $letter_code letter transported by " . $mtt ),
471 my $tables = $params{tables
};
472 my $substitute = $params{substitute
};
473 my $repeat = $params{repeat
};
474 $tables || $substitute || $repeat
475 or carp
( "ERROR: nothing to substitute - both 'tables' and 'substitute' are empty" ),
477 my $want_librarian = $params{want_librarian
};
480 while ( my ($token, $val) = each %$substitute ) {
481 $letter->{title
} =~ s/<<$token>>/$val/g;
482 $letter->{content
} =~ s/<<$token>>/$val/g;
486 my $OPACBaseURL = C4
::Context
->preference('OPACBaseURL');
487 $letter->{content
} =~ s/<<OPACBaseURL>>/$OPACBaseURL/go;
489 if ($want_librarian) {
490 # parsing librarian name
491 my $userenv = C4
::Context
->userenv;
492 $letter->{content
} =~ s/<<LibrarianFirstname>>/$userenv->{firstname}/go;
493 $letter->{content
} =~ s/<<LibrarianSurname>>/$userenv->{surname}/go;
494 $letter->{content
} =~ s/<<LibrarianEmailaddress>>/$userenv->{emailaddress}/go;
497 my ($repeat_no_enclosing_tags, $repeat_enclosing_tags);
500 if (ref ($repeat) eq 'ARRAY' ) {
501 $repeat_no_enclosing_tags = $repeat;
503 $repeat_enclosing_tags = $repeat;
507 if ($repeat_enclosing_tags) {
508 while ( my ($tag, $tag_tables) = each %$repeat_enclosing_tags ) {
509 if ( $letter->{content
} =~ m!<$tag>(.*)</$tag>!s ) {
512 my %subletter = ( title
=> '', content
=> $subcontent );
513 _substitute_tables
( \
%subletter, $_ );
516 $letter->{content
} =~ s!<$tag>.*</$tag>!join( "\n", @lines )!se;
522 _substitute_tables
( $letter, $tables );
525 if ($repeat_no_enclosing_tags) {
526 if ( $letter->{content
} =~ m/[^\n]*<<.*>>[^\n]*/so ) {
531 $c =~ s/<<count>>/$i/go;
532 foreach my $field ( keys %{$_} ) {
533 $c =~ s/(<<[^\.]+.$field>>)/$_->{$field}/;
537 } @
$repeat_no_enclosing_tags;
539 my $replaceby = join( "\n", @lines );
540 $letter->{content
} =~ s/\Q$line\E/$replaceby/s;
544 $letter->{content
} =~ s/<<\S*>>//go; #remove any stragglers
545 # $letter->{content} =~ s/<<[^>]*>>//go;
550 sub _substitute_tables
{
551 my ( $letter, $tables ) = @_;
552 while ( my ($table, $param) = each %$tables ) {
555 my $ref = ref $param;
558 if ($ref && $ref eq 'HASH') {
563 my $sth = _parseletter_sth
($table);
565 warn "_parseletter_sth('$table') failed to return a valid sth. No substitution will be done for that table.";
568 $sth->execute( $ref ? @
$param : $param );
570 $values = $sth->fetchrow_hashref;
574 _parseletter
( $letter, $table, $values );
578 sub _parseletter_sth
{
582 carp
"ERROR: _parseletter_sth() called without argument (table)";
585 # NOTE: we used to check whether we had a statement handle cached in
586 # a %handles module-level variable. This was a dumb move and
587 # broke things for the rest of us. prepare_cached is a better
588 # way to cache statement handles anyway.
590 ($table eq 'biblio' ) ?
"SELECT * FROM $table WHERE biblionumber = ?" :
591 ($table eq 'biblioitems' ) ?
"SELECT * FROM $table WHERE biblionumber = ?" :
592 ($table eq 'items' ) ?
"SELECT * FROM $table WHERE itemnumber = ?" :
593 ($table eq 'issues' ) ?
"SELECT * FROM $table WHERE itemnumber = ?" :
594 ($table eq 'old_issues' ) ?
"SELECT * FROM $table WHERE itemnumber = ? ORDER BY timestamp DESC LIMIT 1" :
595 ($table eq 'reserves' ) ?
"SELECT * FROM $table WHERE borrowernumber = ? and biblionumber = ?" :
596 ($table eq 'borrowers' ) ?
"SELECT * FROM $table WHERE borrowernumber = ?" :
597 ($table eq 'branches' ) ?
"SELECT * FROM $table WHERE branchcode = ?" :
598 ($table eq 'suggestions' ) ?
"SELECT * FROM $table WHERE suggestionid = ?" :
599 ($table eq 'aqbooksellers') ?
"SELECT * FROM $table WHERE id = ?" :
600 ($table eq 'aqorders' ) ?
"SELECT * FROM $table WHERE ordernumber = ?" :
601 ($table eq 'opac_news' ) ?
"SELECT * FROM $table WHERE idnew = ?" :
602 ($table eq 'borrower_modifications') ?
"SELECT * FROM $table WHERE ( borrowernumber = 0 OR borrowernumber = ? ) AND ( verification_token = '' OR verification_token = ? ) AND ( verification_token != '' OR borrowernumber != 0 )" :
605 warn "ERROR: No _parseletter_sth query for table '$table'";
606 return; # nothing to get
608 unless ($sth = C4
::Context
->dbh->prepare_cached($query)) {
609 warn "ERROR: Failed to prepare query: '$query'";
612 return $sth; # now cache is populated for that $table
615 =head2 _parseletter($letter, $table, $values)
618 - $letter : a hash to letter fields (title & content useful)
619 - $table : the Koha table to parse.
620 - $values : table record hashref
621 parse all fields from a table, and replace values in title & content with the appropriate value
622 (not exported sub, used only internally)
627 my ( $letter, $table, $values ) = @_;
629 if ( $table eq 'reserves' && $values->{'waitingdate'} ) {
630 my @waitingdate = split /-/, $values->{'waitingdate'};
632 $values->{'expirationdate'} = '';
633 if( C4
::Context
->preference('ExpireReservesMaxPickUpDelay') &&
634 C4
::Context
->preference('ReservesMaxPickUpDelay') ) {
635 my $dt = dt_from_string
();
636 $dt->add( days
=> C4
::Context
->preference('ReservesMaxPickUpDelay') );
637 $values->{'expirationdate'} = output_pref
({ dt
=> $dt, dateonly
=> 1 });
640 $values->{'waitingdate'} = output_pref
({ dt
=> dt_from_string
( $values->{'waitingdate'} ), dateonly
=> 1 });
644 if ($letter->{content
} && $letter->{content
} =~ /<<today>>/) {
645 my $todaysdate = output_pref
( DateTime
->now() );
646 $letter->{content
} =~ s/<<today>>/$todaysdate/go;
649 while ( my ($field, $val) = each %$values ) {
650 my $replacetablefield = "<<$table.$field>>";
651 my $replacefield = "<<$field>>";
652 $val =~ s/\p{P}$// if $val && $table=~/biblio/;
653 #BZ 9886: Assuming that we want to eliminate ISBD punctuation here
654 #Therefore adding the test on biblio. This includes biblioitems,
655 #but excludes items. Removed unneeded global and lookahead.
657 $val = GetAuthorisedValueByCode
('ROADTYPE', $val, 0) if $table=~/^borrowers$/ && $field=~/^streettype$/;
658 my $replacedby = defined ($val) ?
$val : '';
660 and not $replacedby =~ m
|0000-00-00|
661 and not $replacedby =~ m
|9999-12-31|
662 and $replacedby =~ m
|^\d
{4}-\d
{2}-\d
{2}( \d
{2}:\d
{2}:\d
{2})?
$| )
664 # If the value is XXXX-YY-ZZ[ AA:BB:CC] we assume it is a date
665 my $dateonly = defined $1 ?
0 : 1; #$1 refers to the capture group wrapped in parentheses. In this case, that's the hours, minutes, seconds.
667 $replacedby = output_pref
({ dt
=> dt_from_string
( $replacedby ), dateonly
=> $dateonly });
669 warn "$replacedby seems to be a date but an error occurs on generating it ($@)" if $@
;
671 ($letter->{title
} ) and do {
672 $letter->{title
} =~ s/$replacetablefield/$replacedby/g;
673 $letter->{title
} =~ s/$replacefield/$replacedby/g;
675 ($letter->{content
}) and do {
676 $letter->{content
} =~ s/$replacetablefield/$replacedby/g;
677 $letter->{content
} =~ s/$replacefield/$replacedby/g;
681 if ($table eq 'borrowers' && $letter->{content
}) {
682 if ( my $attributes = GetBorrowerAttributes
($values->{borrowernumber
}) ) {
684 foreach (@
$attributes) {
685 my $code = $_->{code
};
686 my $val = $_->{value_description
} || $_->{value
};
687 $val =~ s/\p{P}(?=$)//g if $val;
688 next unless $val gt '';
690 push @
{ $attr{$code} }, $val;
692 while ( my ($code, $val_ar) = each %attr ) {
693 my $replacefield = "<<borrower-attribute:$code>>";
694 my $replacedby = join ',', @
$val_ar;
695 $letter->{content
} =~ s/$replacefield/$replacedby/g;
704 my $success = EnqueueLetter( { letter => $letter,
705 borrowernumber => '12', message_transport_type => 'email' } )
707 places a letter in the message_queue database table, which will
708 eventually get processed (sent) by the process_message_queue.pl
709 cronjob when it calls SendQueuedMessages.
711 return message_id on success
716 my $params = shift or return;
718 return unless exists $params->{'letter'};
719 # return unless exists $params->{'borrowernumber'};
720 return unless exists $params->{'message_transport_type'};
722 my $content = $params->{letter
}->{content
};
723 $content =~ s/\s+//g if(defined $content);
724 if ( not defined $content or $content eq '' ) {
725 warn "Trying to add an empty message to the message queue" if $debug;
729 # If we have any attachments we should encode then into the body.
730 if ( $params->{'attachments'} ) {
731 $params->{'letter'} = _add_attachments
(
732 { letter
=> $params->{'letter'},
733 attachments
=> $params->{'attachments'},
734 message
=> MIME
::Lite
->new( Type
=> 'multipart/mixed' ),
739 my $dbh = C4
::Context
->dbh();
740 my $statement = << 'ENDSQL';
741 INSERT INTO message_queue
742 ( borrowernumber
, subject
, content
, metadata
, letter_code
, message_transport_type
, status
, time_queued
, to_address
, from_address
, content_type
)
744 ( ?
, ?
, ?
, ?
, ?
, ?
, ?
, NOW
(), ?
, ?
, ?
)
747 my $sth = $dbh->prepare($statement);
748 my $result = $sth->execute(
749 $params->{'borrowernumber'}, # borrowernumber
750 $params->{'letter'}->{'title'}, # subject
751 $params->{'letter'}->{'content'}, # content
752 $params->{'letter'}->{'metadata'} || '', # metadata
753 $params->{'letter'}->{'code'} || '', # letter_code
754 $params->{'message_transport_type'}, # message_transport_type
756 $params->{'to_address'}, # to_address
757 $params->{'from_address'}, # from_address
758 $params->{'letter'}->{'content-type'}, # content_type
760 return $dbh->last_insert_id(undef,undef,'message_queue', undef);
763 =head2 SendQueuedMessages ([$hashref])
765 my $sent = SendQueuedMessages( { verbose => 1 } );
767 sends all of the 'pending' items in the message queue.
769 returns number of messages sent.
773 sub SendQueuedMessages
{
776 my $unsent_messages = _get_unsent_messages
();
777 MESSAGE
: foreach my $message ( @
$unsent_messages ) {
778 # warn Data::Dumper->Dump( [ $message ], [ 'message' ] );
779 warn sprintf( 'sending %s message to patron: %s',
780 $message->{'message_transport_type'},
781 $message->{'borrowernumber'} || 'Admin' )
782 if $params->{'verbose'} or $debug;
783 # This is just begging for subclassing
784 next MESSAGE
if ( lc($message->{'message_transport_type'}) eq 'rss' );
785 if ( lc( $message->{'message_transport_type'} ) eq 'email' ) {
786 _send_message_by_email
( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
788 elsif ( lc( $message->{'message_transport_type'} ) eq 'sms' ) {
789 _send_message_by_sms
( $message );
792 return scalar( @
$unsent_messages );
795 =head2 GetRSSMessages
797 my $message_list = GetRSSMessages( { limit => 10, borrowernumber => '14' } )
799 returns a listref of all queued RSS messages for a particular person.
806 return unless $params;
807 return unless ref $params;
808 return unless $params->{'borrowernumber'};
810 return _get_unsent_messages
( { message_transport_type
=> 'rss',
811 limit
=> $params->{'limit'},
812 borrowernumber
=> $params->{'borrowernumber'}, } );
815 =head2 GetPrintMessages
817 my $message_list = GetPrintMessages( { borrowernumber => $borrowernumber } )
819 Returns a arrayref of all queued print messages (optionally, for a particular
824 sub GetPrintMessages
{
825 my $params = shift || {};
827 return _get_unsent_messages
( { message_transport_type
=> 'print',
828 borrowernumber
=> $params->{'borrowernumber'},
832 =head2 GetQueuedMessages ([$hashref])
834 my $messages = GetQueuedMessage( { borrowernumber => '123', limit => 20 } );
836 fetches messages out of the message queue.
839 list of hashes, each has represents a message in the message queue.
843 sub GetQueuedMessages
{
846 my $dbh = C4
::Context
->dbh();
847 my $statement = << 'ENDSQL';
848 SELECT message_id
, borrowernumber
, subject
, content
, message_transport_type
, status
, time_queued
854 if ( exists $params->{'borrowernumber'} ) {
855 push @whereclauses, ' borrowernumber = ? ';
856 push @query_params, $params->{'borrowernumber'};
859 if ( @whereclauses ) {
860 $statement .= ' WHERE ' . join( 'AND', @whereclauses );
863 if ( defined $params->{'limit'} ) {
864 $statement .= ' LIMIT ? ';
865 push @query_params, $params->{'limit'};
868 my $sth = $dbh->prepare( $statement );
869 my $result = $sth->execute( @query_params );
870 return $sth->fetchall_arrayref({});
873 =head2 GetMessageTransportTypes
875 my @mtt = GetMessageTransportTypes();
877 returns an arrayref of transport types
881 sub GetMessageTransportTypes
{
882 my $dbh = C4
::Context
->dbh();
883 my $mtts = $dbh->selectcol_arrayref("
884 SELECT message_transport_type
885 FROM message_transport_types
886 ORDER BY message_transport_type
891 =head2 _add_attachements
894 letter - the standard letter hashref
895 attachments - listref of attachments. each attachment is a hashref of:
896 type - the mime type, like 'text/plain'
897 content - the actual attachment
898 filename - the name of the attachment.
899 message - a MIME::Lite object to attach these to.
901 returns your letter object, with the content updated.
905 sub _add_attachments
{
908 my $letter = $params->{'letter'};
909 my $attachments = $params->{'attachments'};
910 return $letter unless @
$attachments;
911 my $message = $params->{'message'};
913 # First, we have to put the body in as the first attachment
915 Type
=> $letter->{'content-type'} || 'TEXT',
916 Data
=> $letter->{'is_html'}
917 ? _wrap_html
($letter->{'content'}, $letter->{'title'})
918 : $letter->{'content'},
921 foreach my $attachment ( @
$attachments ) {
923 Type
=> $attachment->{'type'},
924 Data
=> $attachment->{'content'},
925 Filename
=> $attachment->{'filename'},
928 # we're forcing list context here to get the header, not the count back from grep.
929 ( $letter->{'content-type'} ) = grep( /^Content-Type:/, split( /\n/, $params->{'message'}->header_as_string ) );
930 $letter->{'content-type'} =~ s/^Content-Type:\s+//;
931 $letter->{'content'} = $message->body_as_string;
937 sub _get_unsent_messages
{
940 my $dbh = C4
::Context
->dbh();
941 my $statement = << 'ENDSQL';
942 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
943 FROM message_queue mq
944 LEFT JOIN borrowers b ON b
.borrowernumber
= mq
.borrowernumber
948 my @query_params = ('pending');
950 if ( $params->{'message_transport_type'} ) {
951 $statement .= ' AND message_transport_type = ? ';
952 push @query_params, $params->{'message_transport_type'};
954 if ( $params->{'borrowernumber'} ) {
955 $statement .= ' AND borrowernumber = ? ';
956 push @query_params, $params->{'borrowernumber'};
958 if ( $params->{'limit'} ) {
959 $statement .= ' limit ? ';
960 push @query_params, $params->{'limit'};
964 $debug and warn "_get_unsent_messages SQL: $statement";
965 $debug and warn "_get_unsent_messages params: " . join(',',@query_params);
966 my $sth = $dbh->prepare( $statement );
967 my $result = $sth->execute( @query_params );
968 return $sth->fetchall_arrayref({});
971 sub _send_message_by_email
{
972 my $message = shift or return;
973 my ($username, $password, $method) = @_;
975 my $member = C4
::Members
::GetMember
( 'borrowernumber' => $message->{'borrowernumber'} );
976 my $to_address = $message->{'to_address'};
977 unless ($to_address) {
979 warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})";
980 _set_message_status
( { message_id
=> $message->{'message_id'},
981 status
=> 'failed' } );
984 $to_address = C4
::Members
::GetNoticeEmailAddress
( $message->{'borrowernumber'} );
985 unless ($to_address) {
986 # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})";
987 # warning too verbose for this more common case?
988 _set_message_status
( { message_id
=> $message->{'message_id'},
989 status
=> 'failed' } );
994 my $utf8 = decode
('MIME-Header', $message->{'subject'} );
995 $message->{subject
}= encode
('MIME-Header', $utf8);
996 my $subject = encode
('utf8', $message->{'subject'});
997 my $content = encode
('utf8', $message->{'content'});
998 my $content_type = $message->{'content_type'} || 'text/plain; charset="UTF-8"';
999 my $is_html = $content_type =~ m/html/io;
1000 my $branch_email = undef;
1001 my $branch_replyto = undef;
1002 my $branch_returnpath = undef;
1004 my $branchdetail = GetBranchDetail
( $member->{'branchcode'} );
1005 $branch_email = $branchdetail->{'branchemail'};
1006 $branch_replyto = $branchdetail->{'branchreplyto'};
1007 $branch_returnpath = $branchdetail->{'branchreturnpath'};
1009 my $email = Koha
::Email
->new();
1010 my %sendmail_params = $email->create_message_headers(
1013 from
=> $message->{'from_address'} || $branch_email,
1014 replyto
=> $branch_replyto,
1015 sender
=> $branch_returnpath,
1016 subject
=> $subject,
1017 message
=> $is_html ? _wrap_html
( $content, $subject ) : $content,
1018 contenttype
=> $content_type
1022 $sendmail_params{'Auth'} = {user
=> $username, pass
=> $password, method
=> $method} if $username;
1023 if ( my $bcc = C4
::Context
->preference('OverdueNoticeBcc') ) {
1024 $sendmail_params{ Bcc
} = $bcc;
1027 _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
1028 if ( sendmail
( %sendmail_params ) ) {
1029 _set_message_status
( { message_id
=> $message->{'message_id'},
1030 status
=> 'sent' } );
1033 _set_message_status
( { message_id
=> $message->{'message_id'},
1034 status
=> 'failed' } );
1035 carp
$Mail::Sendmail
::error
;
1041 my ($content, $title) = @_;
1043 my $css = C4
::Context
->preference("NoticeCSS") || '';
1044 $css = qq{<link rel
="stylesheet" type
="text/css" href
="$css">} if $css;
1046 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
1047 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1048 <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
1050 <title>$title</title>
1051 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
1062 my ( $message ) = @_;
1063 my $dbh = C4
::Context
->dbh;
1064 my $count = $dbh->selectrow_array(q
|
1067 WHERE message_transport_type
= ?
1068 AND borrowernumber
= ?
1070 AND CAST
(time_queued AS date
) = CAST
(NOW
() AS date
)
1073 |, {}, $message->{message_transport_type
}, $message->{borrowernumber
}, $message->{letter_code
}, $message->{content
} );
1077 sub _send_message_by_sms
{
1078 my $message = shift or return;
1079 my $member = C4
::Members
::GetMember
( 'borrowernumber' => $message->{'borrowernumber'} );
1081 unless ( $member->{smsalertnumber
} ) {
1082 _set_message_status
( { message_id
=> $message->{'message_id'},
1083 status
=> 'failed' } );
1087 if ( _is_duplicate
( $message ) ) {
1088 _set_message_status
( { message_id
=> $message->{'message_id'},
1089 status
=> 'failed' } );
1093 my $success = C4
::SMS
->send_sms( { destination
=> $member->{'smsalertnumber'},
1094 message
=> $message->{'content'},
1096 _set_message_status
( { message_id
=> $message->{'message_id'},
1097 status
=> ($success ?
'sent' : 'failed') } );
1101 sub _update_message_to_address
{
1103 my $dbh = C4
::Context
->dbh();
1104 $dbh->do('UPDATE message_queue SET to_address=? WHERE message_id=?',undef,($to,$id));
1107 sub _set_message_status
{
1108 my $params = shift or return;
1110 foreach my $required_parameter ( qw( message_id status ) ) {
1111 return unless exists $params->{ $required_parameter };
1114 my $dbh = C4
::Context
->dbh();
1115 my $statement = 'UPDATE message_queue SET status= ? WHERE message_id = ?';
1116 my $sth = $dbh->prepare( $statement );
1117 my $result = $sth->execute( $params->{'status'},
1118 $params->{'message_id'} );