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.
27 use C4
::Members
::Attributes
qw(GetBorrowerAttributes);
34 use Date
::Calc
qw( Add_Delta_Days );
38 use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
42 # set the version for version checking
43 $VERSION = 3.07.00.049;
46 &GetLetters &GetPreparedLetter &GetWrappedLetter &addalert &getalert &delalert &findrelatedto &SendAlerts &GetPrintMessages
52 C4::Letters - Give functions for Letters management
60 "Letters" is the tool used in Koha to manage informations sent to the patrons and/or the library. This include some cron jobs like
61 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)
63 Letters are managed through "alerts" sent by Koha on some events. All "alert" related functions are in this module too.
65 =head2 GetLetters([$category])
67 $letters = &GetLetters($category);
68 returns informations about letters.
69 if needed, $category filters for letters given category
70 Create a letter selector with the following code
74 my $letters = GetLetters($cat);
76 foreach my $thisletter (keys %$letters) {
77 my $selected = 1 if $thisletter eq $letter;
80 selected => $selected,
81 lettername => $letters->{$thisletter},
83 push @letterloop, \%row;
85 $template->param(LETTERLOOP => \@letterloop);
89 <select name="letter">
90 <option value="">Default</option>
91 <!-- TMPL_LOOP name="LETTERLOOP" -->
92 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="lettername" --></option>
100 # returns a reference to a hash of references to ALL letters...
103 my $dbh = C4
::Context
->dbh;
106 my $query = "SELECT * FROM letter WHERE module = ? ORDER BY name";
107 $sth = $dbh->prepare($query);
111 my $query = "SELECT * FROM letter ORDER BY name";
112 $sth = $dbh->prepare($query);
115 while ( my $letter = $sth->fetchrow_hashref ) {
116 $letters{ $letter->{'code'} } = $letter->{'name'};
123 my ( $module, $code, $branchcode ) = @_;
127 if ( C4
::Context
->preference('IndependantBranches')
129 and C4
::Context
->userenv ) {
131 $branchcode = C4
::Context
->userenv->{'branch'};
134 if ( my $l = $letter{$module}{$code}{$branchcode} ) {
135 return { %$l }; # deep copy
138 my $dbh = C4
::Context
->dbh;
139 my $sth = $dbh->prepare("select * from letter where module=? and code=? and (branchcode = ? or branchcode = '') order by branchcode desc limit 1");
140 $sth->execute( $module, $code, $branchcode );
141 my $line = $sth->fetchrow_hashref
143 $line->{'content-type'} = 'text/html; charset="UTF-8"' if $line->{is_html
};
144 $letter{$module}{$code}{$branchcode} = $line;
148 =head2 addalert ($borrowernumber, $type, $externalid)
151 - $borrowernumber : the number of the borrower subscribing to the alert
152 - $type : the type of alert.
153 - $externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
155 create an alert and return the alertid (primary key)
160 my ( $borrowernumber, $type, $externalid ) = @_;
161 my $dbh = C4
::Context
->dbh;
164 "insert into alert (borrowernumber, type, externalid) values (?,?,?)");
165 $sth->execute( $borrowernumber, $type, $externalid );
167 # get the alert number newly created and return it
168 my $alertid = $dbh->{'mysql_insertid'};
172 =head2 delalert ($alertid)
175 - alertid : the alert id
181 my $alertid = shift or die "delalert() called without valid argument (alertid)"; # it's gonna die anyway.
182 $debug and warn "delalert: deleting alertid $alertid";
183 my $sth = C4
::Context
->dbh->prepare("delete from alert where alertid=?");
184 $sth->execute($alertid);
187 =head2 getalert ([$borrowernumber], [$type], [$externalid])
190 - $borrowernumber : the number of the borrower subscribing to the alert
191 - $type : the type of alert.
192 - $externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
193 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.
198 my ( $borrowernumber, $type, $externalid ) = @_;
199 my $dbh = C4
::Context
->dbh;
200 my $query = "SELECT a.*, b.branchcode FROM alert a JOIN borrowers b USING(borrowernumber) WHERE";
202 if ($borrowernumber and $borrowernumber =~ /^\d+$/) {
203 $query .= " borrowernumber=? AND ";
204 push @bind, $borrowernumber;
207 $query .= " type=? AND ";
211 $query .= " externalid=? AND ";
212 push @bind, $externalid;
214 $query =~ s/ AND $//;
215 my $sth = $dbh->prepare($query);
216 $sth->execute(@bind);
217 return $sth->fetchall_arrayref({});
220 =head2 findrelatedto($type, $externalid)
223 - $type : the type of alert
224 - $externalid : the id of the "object" to query
226 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.
227 When type=issue, the id is related to a subscriptionid and this sub returns the name of the biblio.
232 # When type=virtual, the id is related to a virtual shelf and this sub returns the name of the sub
235 my $type = shift or return;
236 my $externalid = shift or return;
237 my $q = ($type eq 'issue' ) ?
238 "select title as result from subscription left join biblio on subscription.biblionumber=biblio.biblionumber where subscriptionid=?" :
239 ($type eq 'borrower') ?
240 "select concat(firstname,' ',surname) from borrowers where borrowernumber=?" : undef;
242 warn "findrelatedto(): Illegal type '$type'";
245 my $sth = C4
::Context
->dbh->prepare($q);
246 $sth->execute($externalid);
247 my ($result) = $sth->fetchrow;
254 - $type : the type of alert
255 - $externalid : the id of the "object" to query
256 - $letter_code : the letter to send.
258 send an alert to all borrowers having put an alert on a given subject.
263 my ( $type, $externalid, $letter_code ) = @_;
264 my $dbh = C4
::Context
->dbh;
265 if ( $type eq 'issue' ) {
267 # prepare the letter...
268 # search the biblionumber
271 "SELECT biblionumber FROM subscription WHERE subscriptionid=?");
272 $sth->execute($externalid);
273 my ($biblionumber) = $sth->fetchrow
274 or warn( "No subscription for '$externalid'" ),
278 # find the list of borrowers to alert
279 my $alerts = getalert
( '', 'issue', $externalid );
282 my $borinfo = C4
::Members
::GetMember
('borrowernumber' => $_->{'borrowernumber'});
283 my $email = $borinfo->{email
} or next;
285 # warn "sending issues...";
286 my $userenv = C4
::Context
->userenv;
287 my $letter = GetPreparedLetter
(
289 letter_code
=> $letter_code,
290 branchcode
=> $userenv->{branch
},
292 'branches' => $_->{branchcode
},
293 'biblio' => $biblionumber,
294 'biblioitems' => $biblionumber,
295 'borrowers' => $borinfo,
304 Subject
=> Encode
::encode
( "utf8", "" . $letter->{title
} ),
305 Message
=> Encode
::encode
( "utf8", "" . $letter->{content
} ),
306 'Content-Type' => 'text/plain; charset="utf8"',
308 sendmail
(%mail) or carp
$Mail::Sendmail
::error
;
311 elsif ( $type eq 'claimacquisition' or $type eq 'claimissues' ) {
313 # prepare the letter...
314 # search the biblionumber
315 my $strsth = $type eq 'claimacquisition'
317 SELECT aqorders
.*,aqbasket
.*,biblio
.*,biblioitems
.*,aqbooksellers
.*,
318 aqbooksellers
.id AS booksellerid
320 LEFT JOIN aqbasket ON aqbasket
.basketno
=aqorders
.basketno
321 LEFT JOIN biblio ON aqorders
.biblionumber
=biblio
.biblionumber
322 LEFT JOIN biblioitems ON aqorders
.biblioitemnumber
=biblioitems
.biblioitemnumber
323 LEFT JOIN aqbooksellers ON aqbasket
.booksellerid
=aqbooksellers
.id
324 WHERE aqorders
.ordernumber IN
(
327 SELECT serial
.*,subscription
.*, biblio
.*, aqbooksellers
.*,
328 aqbooksellers
.id AS booksellerid
330 LEFT JOIN subscription ON serial
.subscriptionid
=subscription
.subscriptionid
331 LEFT JOIN biblio ON serial
.biblionumber
=biblio
.biblionumber
332 LEFT JOIN aqbooksellers ON subscription
.aqbooksellerid
=aqbooksellers
.id
333 WHERE serial
.serialid IN
(
335 $strsth .= join( ",", @
$externalid ) . ")";
336 my $sthorders = $dbh->prepare($strsth);
338 my $dataorders = $sthorders->fetchall_arrayref( {} );
341 $dbh->prepare("select * from aqbooksellers where id=?");
342 $sthbookseller->execute( $dataorders->[0]->{booksellerid
} );
343 my $databookseller = $sthbookseller->fetchrow_hashref;
346 push @email, $databookseller->{bookselleremail
} if $databookseller->{bookselleremail
};
347 push @email, $databookseller->{contemail
} if $databookseller->{contemail
};
349 warn "Bookseller $dataorders->[0]->{booksellerid} without emails";
350 return { error
=> "no_email" };
353 my $userenv = C4
::Context
->userenv;
354 my $letter = GetPreparedLetter
(
356 letter_code
=> $letter_code,
357 branchcode
=> $userenv->{branch
},
359 'branches' => $userenv->{branch
},
360 'aqbooksellers' => $databookseller,
362 repeat
=> $dataorders,
368 To
=> join( ',', @email),
369 From
=> $userenv->{emailaddress
},
370 Subject
=> Encode
::encode
( "utf8", "" . $letter->{title
} ),
371 Message
=> Encode
::encode
( "utf8", "" . $letter->{content
} ),
372 'Content-Type' => 'text/plain; charset="utf8"',
374 sendmail
(%mail) or carp
$Mail::Sendmail
::error
;
378 $type eq 'claimissues' ?
"CLAIM ISSUE" : "ACQUISITION CLAIM",
381 . $databookseller->{contemail
}
386 ) if C4
::Context
->preference("LetterLog");
388 # send an "account details" notice to a newly created user
389 elsif ( $type eq 'members' ) {
390 my $branchdetails = GetBranchDetail
($externalid->{'branchcode'});
391 my $letter = GetPreparedLetter
(
393 letter_code
=> $letter_code,
394 branchcode
=> $externalid->{'branchcode'},
396 'branches' => $branchdetails,
397 'borrowers' => $externalid->{'borrowernumber'},
399 substitute
=> { 'borrowers.password' => $externalid->{'password'} },
403 return { error
=> "no_email" } unless $externalid->{'emailaddr'};
405 To
=> $externalid->{'emailaddr'},
406 From
=> $branchdetails->{'branchemail'} || C4
::Context
->preference("KohaAdminEmailAddress"),
407 Subject
=> Encode
::encode
( "utf8", $letter->{'title'} ),
408 Message
=> Encode
::encode
( "utf8", $letter->{'content'} ),
409 'Content-Type' => 'text/plain; charset="utf8"',
411 sendmail
(%mail) or carp
$Mail::Sendmail
::error
;
415 =head2 GetPreparedLetter( %params )
418 module => letter module, mandatory
419 letter_code => letter code, mandatory
420 branchcode => for letter selection, if missing default system letter taken
421 tables => a hashref with table names as keys. Values are either:
422 - a scalar - primary key value
423 - an arrayref - primary key values
424 - a hashref - full record
425 substitute => custom substitution key/value pairs
426 repeat => records to be substituted on consecutive lines:
427 - an arrayref - tries to guess what needs substituting by
428 taking remaining << >> tokensr; not recommended
429 - a hashref token => @tables - replaces <token> << >> << >> </token>
430 subtemplate for each @tables row; table is a hashref as above
431 want_librarian => boolean, if set to true triggers librarian details
432 substitution from the userenv
434 letter fields hashref (title & content useful)
438 sub GetPreparedLetter
{
441 my $module = $params{module
} or croak
"No module";
442 my $letter_code = $params{letter_code
} or croak
"No letter_code";
443 my $branchcode = $params{branchcode
} || '';
445 my $letter = getletter
( $module, $letter_code, $branchcode )
446 or warn( "No $module $letter_code letter"),
449 my $tables = $params{tables
};
450 my $substitute = $params{substitute
};
451 my $repeat = $params{repeat
};
452 $tables || $substitute || $repeat
453 or carp
( "ERROR: nothing to substitute - both 'tables' and 'substitute' are empty" ),
455 my $want_librarian = $params{want_librarian
};
458 while ( my ($token, $val) = each %$substitute ) {
459 $letter->{title
} =~ s/<<$token>>/$val/g;
460 $letter->{content
} =~ s/<<$token>>/$val/g;
464 my $OPACBaseURL = C4
::Context
->preference('OPACBaseURL');
465 $letter->{content
} =~ s/<<OPACBaseURL>>/$OPACBaseURL/go;
467 if ($want_librarian) {
468 # parsing librarian name
469 my $userenv = C4
::Context
->userenv;
470 $letter->{content
} =~ s/<<LibrarianFirstname>>/$userenv->{firstname}/go;
471 $letter->{content
} =~ s/<<LibrarianSurname>>/$userenv->{surname}/go;
472 $letter->{content
} =~ s/<<LibrarianEmailaddress>>/$userenv->{emailaddress}/go;
475 my ($repeat_no_enclosing_tags, $repeat_enclosing_tags);
478 if (ref ($repeat) eq 'ARRAY' ) {
479 $repeat_no_enclosing_tags = $repeat;
481 $repeat_enclosing_tags = $repeat;
485 if ($repeat_enclosing_tags) {
486 while ( my ($tag, $tag_tables) = each %$repeat_enclosing_tags ) {
487 if ( $letter->{content
} =~ m!<$tag>(.*)</$tag>!s ) {
490 my %subletter = ( title
=> '', content
=> $subcontent );
491 _substitute_tables
( \
%subletter, $_ );
494 $letter->{content
} =~ s!<$tag>.*</$tag>!join( "\n", @lines )!se;
500 _substitute_tables
( $letter, $tables );
503 if ($repeat_no_enclosing_tags) {
504 if ( $letter->{content
} =~ m/[^\n]*<<.*>>[^\n]*/so ) {
509 $c =~ s/<<count>>/$i/go;
510 foreach my $field ( keys %{$_} ) {
511 $c =~ s/(<<[^\.]+.$field>>)/$_->{$field}/;
515 } @
$repeat_no_enclosing_tags;
517 my $replaceby = join( "\n", @lines );
518 $letter->{content
} =~ s/\Q$line\E/$replaceby/s;
522 $letter->{content
} =~ s/<<\S*>>//go; #remove any stragglers
523 # $letter->{content} =~ s/<<[^>]*>>//go;
528 sub _substitute_tables
{
529 my ( $letter, $tables ) = @_;
530 while ( my ($table, $param) = each %$tables ) {
533 my $ref = ref $param;
536 if ($ref && $ref eq 'HASH') {
541 my $sth = _parseletter_sth
($table);
543 warn "_parseletter_sth('$table') failed to return a valid sth. No substitution will be done for that table.";
546 $sth->execute( $ref ? @
$param : $param );
548 $values = $sth->fetchrow_hashref;
551 _parseletter
( $letter, $table, $values );
556 sub _parseletter_sth
{
559 carp
"ERROR: _parseletter_sth() called without argument (table)";
563 (defined $handles{$table}) and return $handles{$table};
565 ($table eq 'biblio' ) ?
"SELECT * FROM $table WHERE biblionumber = ?" :
566 ($table eq 'biblioitems' ) ?
"SELECT * FROM $table WHERE biblionumber = ?" :
567 ($table eq 'items' ) ?
"SELECT * FROM $table WHERE itemnumber = ?" :
568 ($table eq 'issues' ) ?
"SELECT * FROM $table WHERE itemnumber = ?" :
569 ($table eq 'old_issues' ) ?
"SELECT * FROM $table WHERE itemnumber = ? ORDER BY timestamp DESC LIMIT 1" :
570 ($table eq 'reserves' ) ?
"SELECT * FROM $table WHERE borrowernumber = ? and biblionumber = ?" :
571 ($table eq 'borrowers' ) ?
"SELECT * FROM $table WHERE borrowernumber = ?" :
572 ($table eq 'branches' ) ?
"SELECT * FROM $table WHERE branchcode = ?" :
573 ($table eq 'suggestions' ) ?
"SELECT * FROM $table WHERE suggestionid = ?" :
574 ($table eq 'aqbooksellers') ?
"SELECT * FROM $table WHERE id = ?" :
575 ($table eq 'aqorders' ) ?
"SELECT * FROM $table WHERE ordernumber = ?" :
576 ($table eq 'opac_news' ) ?
"SELECT * FROM $table WHERE idnew = ?" :
577 ($table eq 'borrower_modifications') ?
"SELECT * FROM $table WHERE borrowernumber = ? OR verification_token =?":
580 warn "ERROR: No _parseletter_sth query for table '$table'";
581 return; # nothing to get
583 unless ($handles{$table} = C4
::Context
->dbh->prepare($query)) {
584 warn "ERROR: Failed to prepare query: '$query'";
587 return $handles{$table}; # now cache is populated for that $table
590 =head2 _parseletter($letter, $table, $values)
593 - $letter : a hash to letter fields (title & content useful)
594 - $table : the Koha table to parse.
595 - $values : table record hashref
596 parse all fields from a table, and replace values in title & content with the appropriate value
597 (not exported sub, used only internally)
603 my ( $letter, $table, $values ) = @_;
605 if ( $table eq 'reserves' && $values->{'waitingdate'} ) {
606 my @waitingdate = split /-/, $values->{'waitingdate'};
608 my $dt = dt_from_string
();
609 $dt->add( days
=> C4
::Context
->preference('ReservesMaxPickUpDelay') );
610 $values->{'expirationdate'} = output_pref
( $dt, undef, 1 );
612 $values->{'waitingdate'} = output_pref
( dt_from_string
( $values->{'waitingdate'} ), undef, 1 );
616 if ($letter->{content
} && $letter->{content
} =~ /<<today>>/) {
617 my @da = localtime();
618 my $todaysdate = "$da[2]:$da[1] " . C4
::Dates
->today();
619 $letter->{content
} =~ s/<<today>>/$todaysdate/go;
622 while ( my ($field, $val) = each %$values ) {
623 my $replacetablefield = "<<$table.$field>>";
624 my $replacefield = "<<$field>>";
625 $val =~ s/\p{P}(?=$)//g if $val;
626 my $replacedby = defined ($val) ?
$val : '';
627 ($letter->{title
} ) and do {
628 $letter->{title
} =~ s/$replacetablefield/$replacedby/g;
629 $letter->{title
} =~ s/$replacefield/$replacedby/g;
631 ($letter->{content
}) and do {
632 $letter->{content
} =~ s/$replacetablefield/$replacedby/g;
633 $letter->{content
} =~ s/$replacefield/$replacedby/g;
637 if ($table eq 'borrowers' && $letter->{content
}) {
638 if ( my $attributes = GetBorrowerAttributes
($values->{borrowernumber
}) ) {
640 foreach (@
$attributes) {
641 my $code = $_->{code
};
642 my $val = $_->{value_description
} || $_->{value
};
643 $val =~ s/\p{P}(?=$)//g if $val;
644 next unless $val gt '';
646 push @
{ $attr{$code} }, $val;
648 while ( my ($code, $val_ar) = each %attr ) {
649 my $replacefield = "<<borrower-attribute:$code>>";
650 my $replacedby = join ',', @
$val_ar;
651 $letter->{content
} =~ s/$replacefield/$replacedby/g;
660 my $success = EnqueueLetter( { letter => $letter,
661 borrowernumber => '12', message_transport_type => 'email' } )
663 places a letter in the message_queue database table, which will
664 eventually get processed (sent) by the process_message_queue.pl
665 cronjob when it calls SendQueuedMessages.
667 return message_id on success
672 my $params = shift or return;
674 return unless exists $params->{'letter'};
675 # return unless exists $params->{'borrowernumber'};
676 return unless exists $params->{'message_transport_type'};
678 my $content = $params->{letter
}->{content
};
679 $content =~ s/\s+//g if(defined $content);
680 if ( not defined $content or $content eq '' ) {
681 warn "Trying to add an empty message to the message queue" if $debug;
685 # If we have any attachments we should encode then into the body.
686 if ( $params->{'attachments'} ) {
687 $params->{'letter'} = _add_attachments
(
688 { letter
=> $params->{'letter'},
689 attachments
=> $params->{'attachments'},
690 message
=> MIME
::Lite
->new( Type
=> 'multipart/mixed' ),
695 my $dbh = C4
::Context
->dbh();
696 my $statement = << 'ENDSQL';
697 INSERT INTO message_queue
698 ( borrowernumber
, subject
, content
, metadata
, letter_code
, message_transport_type
, status
, time_queued
, to_address
, from_address
, content_type
)
700 ( ?
, ?
, ?
, ?
, ?
, ?
, ?
, NOW
(), ?
, ?
, ?
)
703 my $sth = $dbh->prepare($statement);
704 my $result = $sth->execute(
705 $params->{'borrowernumber'}, # borrowernumber
706 $params->{'letter'}->{'title'}, # subject
707 $params->{'letter'}->{'content'}, # content
708 $params->{'letter'}->{'metadata'} || '', # metadata
709 $params->{'letter'}->{'code'} || '', # letter_code
710 $params->{'message_transport_type'}, # message_transport_type
712 $params->{'to_address'}, # to_address
713 $params->{'from_address'}, # from_address
714 $params->{'letter'}->{'content-type'}, # content_type
716 return $dbh->last_insert_id(undef,undef,'message_queue', undef);
719 =head2 SendQueuedMessages ([$hashref])
721 my $sent = SendQueuedMessages( { verbose => 1 } );
723 sends all of the 'pending' items in the message queue.
725 returns number of messages sent.
729 sub SendQueuedMessages
{
732 my $unsent_messages = _get_unsent_messages
();
733 MESSAGE
: foreach my $message ( @
$unsent_messages ) {
734 # warn Data::Dumper->Dump( [ $message ], [ 'message' ] );
735 warn sprintf( 'sending %s message to patron: %s',
736 $message->{'message_transport_type'},
737 $message->{'borrowernumber'} || 'Admin' )
738 if $params->{'verbose'} or $debug;
739 # This is just begging for subclassing
740 next MESSAGE
if ( lc($message->{'message_transport_type'}) eq 'rss' );
741 if ( lc( $message->{'message_transport_type'} ) eq 'email' ) {
742 _send_message_by_email
( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
744 elsif ( lc( $message->{'message_transport_type'} ) eq 'sms' ) {
745 _send_message_by_sms
( $message );
748 return scalar( @
$unsent_messages );
751 =head2 GetRSSMessages
753 my $message_list = GetRSSMessages( { limit => 10, borrowernumber => '14' } )
755 returns a listref of all queued RSS messages for a particular person.
762 return unless $params;
763 return unless ref $params;
764 return unless $params->{'borrowernumber'};
766 return _get_unsent_messages
( { message_transport_type
=> 'rss',
767 limit
=> $params->{'limit'},
768 borrowernumber
=> $params->{'borrowernumber'}, } );
771 =head2 GetPrintMessages
773 my $message_list = GetPrintMessages( { borrowernumber => $borrowernumber } )
775 Returns a arrayref of all queued print messages (optionally, for a particular
780 sub GetPrintMessages
{
781 my $params = shift || {};
783 return _get_unsent_messages
( { message_transport_type
=> 'print',
784 borrowernumber
=> $params->{'borrowernumber'},
788 =head2 GetQueuedMessages ([$hashref])
790 my $messages = GetQueuedMessage( { borrowernumber => '123', limit => 20 } );
792 fetches messages out of the message queue.
795 list of hashes, each has represents a message in the message queue.
799 sub GetQueuedMessages
{
802 my $dbh = C4
::Context
->dbh();
803 my $statement = << 'ENDSQL';
804 SELECT message_id
, borrowernumber
, subject
, content
, message_transport_type
, status
, time_queued
810 if ( exists $params->{'borrowernumber'} ) {
811 push @whereclauses, ' borrowernumber = ? ';
812 push @query_params, $params->{'borrowernumber'};
815 if ( @whereclauses ) {
816 $statement .= ' WHERE ' . join( 'AND', @whereclauses );
819 if ( defined $params->{'limit'} ) {
820 $statement .= ' LIMIT ? ';
821 push @query_params, $params->{'limit'};
824 my $sth = $dbh->prepare( $statement );
825 my $result = $sth->execute( @query_params );
826 return $sth->fetchall_arrayref({});
829 =head2 _add_attachements
832 letter - the standard letter hashref
833 attachments - listref of attachments. each attachment is a hashref of:
834 type - the mime type, like 'text/plain'
835 content - the actual attachment
836 filename - the name of the attachment.
837 message - a MIME::Lite object to attach these to.
839 returns your letter object, with the content updated.
843 sub _add_attachments
{
846 my $letter = $params->{'letter'};
847 my $attachments = $params->{'attachments'};
848 return $letter unless @
$attachments;
849 my $message = $params->{'message'};
851 # First, we have to put the body in as the first attachment
853 Type
=> $letter->{'content-type'} || 'TEXT',
854 Data
=> $letter->{'is_html'}
855 ? _wrap_html
($letter->{'content'}, $letter->{'title'})
856 : $letter->{'content'},
859 foreach my $attachment ( @
$attachments ) {
861 Type
=> $attachment->{'type'},
862 Data
=> $attachment->{'content'},
863 Filename
=> $attachment->{'filename'},
866 # we're forcing list context here to get the header, not the count back from grep.
867 ( $letter->{'content-type'} ) = grep( /^Content-Type:/, split( /\n/, $params->{'message'}->header_as_string ) );
868 $letter->{'content-type'} =~ s/^Content-Type:\s+//;
869 $letter->{'content'} = $message->body_as_string;
875 sub _get_unsent_messages
{
878 my $dbh = C4
::Context
->dbh();
879 my $statement = << 'ENDSQL';
880 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
881 FROM message_queue mq
882 LEFT JOIN borrowers b ON b
.borrowernumber
= mq
.borrowernumber
886 my @query_params = ('pending');
888 if ( $params->{'message_transport_type'} ) {
889 $statement .= ' AND message_transport_type = ? ';
890 push @query_params, $params->{'message_transport_type'};
892 if ( $params->{'borrowernumber'} ) {
893 $statement .= ' AND borrowernumber = ? ';
894 push @query_params, $params->{'borrowernumber'};
896 if ( $params->{'limit'} ) {
897 $statement .= ' limit ? ';
898 push @query_params, $params->{'limit'};
902 $debug and warn "_get_unsent_messages SQL: $statement";
903 $debug and warn "_get_unsent_messages params: " . join(',',@query_params);
904 my $sth = $dbh->prepare( $statement );
905 my $result = $sth->execute( @query_params );
906 return $sth->fetchall_arrayref({});
909 sub _send_message_by_email
{
910 my $message = shift or return;
911 my ($username, $password, $method) = @_;
913 my $to_address = $message->{to_address
};
914 unless ($to_address) {
915 my $member = C4
::Members
::GetMember
( 'borrowernumber' => $message->{'borrowernumber'} );
917 warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})";
918 _set_message_status
( { message_id
=> $message->{'message_id'},
919 status
=> 'failed' } );
922 my $which_address = C4
::Context
->preference('AutoEmailPrimaryAddress');
923 # If the system preference is set to 'first valid' (value == OFF), look up email address
924 if ($which_address eq 'OFF') {
925 $to_address = GetFirstValidEmailAddress
( $message->{'borrowernumber'} );
927 $to_address = $member->{$which_address};
929 unless ($to_address) {
930 # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})";
931 # warning too verbose for this more common case?
932 _set_message_status
( { message_id
=> $message->{'message_id'},
933 status
=> 'failed' } );
938 my $utf8 = decode
('MIME-Header', $message->{'subject'} );
939 $message->{subject
}= encode
('MIME-Header', $utf8);
940 my $subject = encode
('utf8', $message->{'subject'});
941 my $content = encode
('utf8', $message->{'content'});
942 my $content_type = $message->{'content_type'} || 'text/plain; charset="UTF-8"';
943 my $is_html = $content_type =~ m/html/io;
944 my %sendmail_params = (
946 From
=> $message->{'from_address'} || C4
::Context
->preference('KohaAdminEmailAddress'),
949 Message
=> $is_html ? _wrap_html
($content, $subject) : $content,
950 'content-type' => $content_type,
952 $sendmail_params{'Auth'} = {user
=> $username, pass
=> $password, method
=> $method} if $username;
953 if ( my $bcc = C4
::Context
->preference('OverdueNoticeBcc') ) {
954 $sendmail_params{ Bcc
} = $bcc;
957 _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
958 if ( sendmail
( %sendmail_params ) ) {
959 _set_message_status
( { message_id
=> $message->{'message_id'},
960 status
=> 'sent' } );
963 _set_message_status
( { message_id
=> $message->{'message_id'},
964 status
=> 'failed' } );
965 carp
$Mail::Sendmail
::error
;
971 my ($content, $title) = @_;
973 my $css = C4
::Context
->preference("NoticeCSS") || '';
974 $css = qq{<link rel
="stylesheet" type
="text/css" href
="$css">} if $css;
976 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
977 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
978 <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
980 <title>$title</title>
981 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
991 sub _send_message_by_sms
{
992 my $message = shift or return;
993 my $member = C4
::Members
::GetMember
( 'borrowernumber' => $message->{'borrowernumber'} );
994 return unless $member->{'smsalertnumber'};
996 my $success = C4
::SMS
->send_sms( { destination
=> $member->{'smsalertnumber'},
997 message
=> $message->{'content'},
999 _set_message_status
( { message_id
=> $message->{'message_id'},
1000 status
=> ($success ?
'sent' : 'failed') } );
1004 sub _update_message_to_address
{
1006 my $dbh = C4
::Context
->dbh();
1007 $dbh->do('UPDATE message_queue SET to_address=? WHERE message_id=?',undef,($to,$id));
1010 sub _set_message_status
{
1011 my $params = shift or return;
1013 foreach my $required_parameter ( qw( message_id status ) ) {
1014 return unless exists $params->{ $required_parameter };
1017 my $dbh = C4
::Context
->dbh();
1018 my $statement = 'UPDATE message_queue SET status= ? WHERE message_id = ?';
1019 my $sth = $dbh->prepare( $statement );
1020 my $result = $sth->execute( $params->{'status'},
1021 $params->{'message_id'} );