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);
33 use Date
::Calc
qw( Add_Delta_Days );
37 use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
41 # set the version for version checking
42 $VERSION = 3.07.00.049;
45 &GetLetters &GetPreparedLetter &GetWrappedLetter &addalert &getalert &delalert &findrelatedto &SendAlerts &GetPrintMessages
51 C4::Letters - Give functions for Letters management
59 "Letters" is the tool used in Koha to manage informations sent to the patrons and/or the library. This include some cron jobs like
60 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)
62 Letters are managed through "alerts" sent by Koha on some events. All "alert" related functions are in this module too.
64 =head2 GetLetters([$category])
66 $letters = &GetLetters($category);
67 returns informations about letters.
68 if needed, $category filters for letters given category
69 Create a letter selector with the following code
73 my $letters = GetLetters($cat);
75 foreach my $thisletter (keys %$letters) {
76 my $selected = 1 if $thisletter eq $letter;
79 selected => $selected,
80 lettername => $letters->{$thisletter},
82 push @letterloop, \%row;
84 $template->param(LETTERLOOP => \@letterloop);
88 <select name="letter">
89 <option value="">Default</option>
90 <!-- TMPL_LOOP name="LETTERLOOP" -->
91 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="lettername" --></option>
99 # returns a reference to a hash of references to ALL letters...
102 my $dbh = C4
::Context
->dbh;
105 my $query = "SELECT * FROM letter WHERE module = ? ORDER BY name";
106 $sth = $dbh->prepare($query);
110 my $query = "SELECT * FROM letter ORDER BY name";
111 $sth = $dbh->prepare($query);
114 while ( my $letter = $sth->fetchrow_hashref ) {
115 $letters{ $letter->{'code'} } = $letter->{'name'};
120 # FIXME: using our here means that a Plack server will need to be
121 # restarted fairly regularly when working with this routine.
122 # A better option would be to use Koha::Cache and use a cache
123 # that actually works in a persistent environment, but as a
124 # short-term fix, our will work.
127 my ( $module, $code, $branchcode ) = @_;
131 if ( C4
::Context
->preference('IndependentBranches')
133 and C4
::Context
->userenv ) {
135 $branchcode = C4
::Context
->userenv->{'branch'};
138 if ( my $l = $letter{$module}{$code}{$branchcode} ) {
139 return { %$l }; # deep copy
142 my $dbh = C4
::Context
->dbh;
143 my $sth = $dbh->prepare("select * from letter where module=? and code=? and (branchcode = ? or branchcode = '') order by branchcode desc limit 1");
144 $sth->execute( $module, $code, $branchcode );
145 my $line = $sth->fetchrow_hashref
147 $line->{'content-type'} = 'text/html; charset="UTF-8"' if $line->{is_html
};
148 $letter{$module}{$code}{$branchcode} = $line;
152 =head2 addalert ($borrowernumber, $type, $externalid)
155 - $borrowernumber : the number of the borrower subscribing to the alert
156 - $type : the type of alert.
157 - $externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
159 create an alert and return the alertid (primary key)
164 my ( $borrowernumber, $type, $externalid ) = @_;
165 my $dbh = C4
::Context
->dbh;
168 "insert into alert (borrowernumber, type, externalid) values (?,?,?)");
169 $sth->execute( $borrowernumber, $type, $externalid );
171 # get the alert number newly created and return it
172 my $alertid = $dbh->{'mysql_insertid'};
176 =head2 delalert ($alertid)
179 - alertid : the alert id
185 my $alertid = shift or die "delalert() called without valid argument (alertid)"; # it's gonna die anyway.
186 $debug and warn "delalert: deleting alertid $alertid";
187 my $sth = C4
::Context
->dbh->prepare("delete from alert where alertid=?");
188 $sth->execute($alertid);
191 =head2 getalert ([$borrowernumber], [$type], [$externalid])
194 - $borrowernumber : the number of the borrower subscribing to the alert
195 - $type : the type of alert.
196 - $externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
197 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.
202 my ( $borrowernumber, $type, $externalid ) = @_;
203 my $dbh = C4
::Context
->dbh;
204 my $query = "SELECT a.*, b.branchcode FROM alert a JOIN borrowers b USING(borrowernumber) WHERE";
206 if ($borrowernumber and $borrowernumber =~ /^\d+$/) {
207 $query .= " borrowernumber=? AND ";
208 push @bind, $borrowernumber;
211 $query .= " type=? AND ";
215 $query .= " externalid=? AND ";
216 push @bind, $externalid;
218 $query =~ s/ AND $//;
219 my $sth = $dbh->prepare($query);
220 $sth->execute(@bind);
221 return $sth->fetchall_arrayref({});
224 =head2 findrelatedto($type, $externalid)
227 - $type : the type of alert
228 - $externalid : the id of the "object" to query
230 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.
231 When type=issue, the id is related to a subscriptionid and this sub returns the name of the biblio.
236 # When type=virtual, the id is related to a virtual shelf and this sub returns the name of the sub
239 my $type = shift or return;
240 my $externalid = shift or return;
241 my $q = ($type eq 'issue' ) ?
242 "select title as result from subscription left join biblio on subscription.biblionumber=biblio.biblionumber where subscriptionid=?" :
243 ($type eq 'borrower') ?
244 "select concat(firstname,' ',surname) from borrowers where borrowernumber=?" : undef;
246 warn "findrelatedto(): Illegal type '$type'";
249 my $sth = C4
::Context
->dbh->prepare($q);
250 $sth->execute($externalid);
251 my ($result) = $sth->fetchrow;
258 - $type : the type of alert
259 - $externalid : the id of the "object" to query
260 - $letter_code : the letter to send.
262 send an alert to all borrowers having put an alert on a given subject.
267 my ( $type, $externalid, $letter_code ) = @_;
268 my $dbh = C4
::Context
->dbh;
269 if ( $type eq 'issue' ) {
271 # prepare the letter...
272 # search the biblionumber
275 "SELECT biblionumber FROM subscription WHERE subscriptionid=?");
276 $sth->execute($externalid);
277 my ($biblionumber) = $sth->fetchrow
278 or warn( "No subscription for '$externalid'" ),
282 # find the list of borrowers to alert
283 my $alerts = getalert
( '', 'issue', $externalid );
286 my $borinfo = C4
::Members
::GetMember
('borrowernumber' => $_->{'borrowernumber'});
287 my $email = $borinfo->{email
} or next;
289 # warn "sending issues...";
290 my $userenv = C4
::Context
->userenv;
291 my $branchdetails = GetBranchDetail
($_->{'branchcode'});
292 my $letter = GetPreparedLetter
(
294 letter_code
=> $letter_code,
295 branchcode
=> $userenv->{branch
},
297 'branches' => $_->{branchcode
},
298 'biblio' => $biblionumber,
299 'biblioitems' => $biblionumber,
300 'borrowers' => $borinfo,
308 From
=> $branchdetails->{'branchemail'} || C4
::Context
->preference("KohaAdminEmailAddress"),
309 Subject
=> Encode
::encode
( "utf8", "" . $letter->{title
} ),
310 Message
=> Encode
::encode
( "utf8", "" . $letter->{content
} ),
311 'Content-Type' => 'text/plain; charset="utf8"',
313 sendmail
(%mail) or carp
$Mail::Sendmail
::error
;
316 elsif ( $type eq 'claimacquisition' or $type eq 'claimissues' ) {
318 # prepare the letter...
319 # search the biblionumber
320 my $strsth = $type eq 'claimacquisition'
322 SELECT aqorders
.*,aqbasket
.*,biblio
.*,biblioitems
.*,aqbooksellers
.*,
323 aqbooksellers
.id AS booksellerid
325 LEFT JOIN aqbasket ON aqbasket
.basketno
=aqorders
.basketno
326 LEFT JOIN biblio ON aqorders
.biblionumber
=biblio
.biblionumber
327 LEFT JOIN biblioitems ON aqorders
.biblionumber
=biblioitems
.biblionumber
328 LEFT JOIN aqbooksellers ON aqbasket
.booksellerid
=aqbooksellers
.id
329 WHERE aqorders
.ordernumber IN
(
332 SELECT serial
.*,subscription
.*, biblio
.*, aqbooksellers
.*,
333 aqbooksellers
.id AS booksellerid
335 LEFT JOIN subscription ON serial
.subscriptionid
=subscription
.subscriptionid
336 LEFT JOIN biblio ON serial
.biblionumber
=biblio
.biblionumber
337 LEFT JOIN aqbooksellers ON subscription
.aqbooksellerid
=aqbooksellers
.id
338 WHERE serial
.serialid IN
(
340 $strsth .= join( ",", @
$externalid ) . ")";
341 my $sthorders = $dbh->prepare($strsth);
343 my $dataorders = $sthorders->fetchall_arrayref( {} );
346 $dbh->prepare("select * from aqbooksellers where id=?");
347 $sthbookseller->execute( $dataorders->[0]->{booksellerid
} );
348 my $databookseller = $sthbookseller->fetchrow_hashref;
351 push @email, $databookseller->{bookselleremail
} if $databookseller->{bookselleremail
};
352 push @email, $databookseller->{contemail
} if $databookseller->{contemail
};
354 warn "Bookseller $dataorders->[0]->{booksellerid} without emails";
355 return { error
=> "no_email" };
358 my $userenv = C4
::Context
->userenv;
359 my $letter = GetPreparedLetter
(
361 letter_code
=> $letter_code,
362 branchcode
=> $userenv->{branch
},
364 'branches' => $userenv->{branch
},
365 'aqbooksellers' => $databookseller,
367 repeat
=> $dataorders,
373 To
=> join( ',', @email),
374 From
=> $userenv->{emailaddress
},
375 Subject
=> Encode
::encode
( "utf8", "" . $letter->{title
} ),
376 Message
=> Encode
::encode
( "utf8", "" . $letter->{content
} ),
377 'Content-Type' => 'text/plain; charset="utf8"',
379 sendmail
(%mail) or carp
$Mail::Sendmail
::error
;
383 $type eq 'claimissues' ?
"CLAIM ISSUE" : "ACQUISITION CLAIM",
386 . $databookseller->{contemail
}
391 ) if C4
::Context
->preference("LetterLog");
393 # send an "account details" notice to a newly created user
394 elsif ( $type eq 'members' ) {
395 my $branchdetails = GetBranchDetail
($externalid->{'branchcode'});
396 my $letter = GetPreparedLetter
(
398 letter_code
=> $letter_code,
399 branchcode
=> $externalid->{'branchcode'},
401 'branches' => $branchdetails,
402 'borrowers' => $externalid->{'borrowernumber'},
404 substitute
=> { 'borrowers.password' => $externalid->{'password'} },
408 return { error
=> "no_email" } unless $externalid->{'emailaddr'};
410 To
=> $externalid->{'emailaddr'},
411 From
=> $branchdetails->{'branchemail'} || C4
::Context
->preference("KohaAdminEmailAddress"),
412 Subject
=> Encode
::encode
( "utf8", $letter->{'title'} ),
413 Message
=> Encode
::encode
( "utf8", $letter->{'content'} ),
414 'Content-Type' => 'text/plain; charset="utf8"',
416 sendmail
(%mail) or carp
$Mail::Sendmail
::error
;
420 =head2 GetPreparedLetter( %params )
423 module => letter module, mandatory
424 letter_code => letter code, mandatory
425 branchcode => for letter selection, if missing default system letter taken
426 tables => a hashref with table names as keys. Values are either:
427 - a scalar - primary key value
428 - an arrayref - primary key values
429 - a hashref - full record
430 substitute => custom substitution key/value pairs
431 repeat => records to be substituted on consecutive lines:
432 - an arrayref - tries to guess what needs substituting by
433 taking remaining << >> tokensr; not recommended
434 - a hashref token => @tables - replaces <token> << >> << >> </token>
435 subtemplate for each @tables row; table is a hashref as above
436 want_librarian => boolean, if set to true triggers librarian details
437 substitution from the userenv
439 letter fields hashref (title & content useful)
443 sub GetPreparedLetter
{
446 my $module = $params{module
} or croak
"No module";
447 my $letter_code = $params{letter_code
} or croak
"No letter_code";
448 my $branchcode = $params{branchcode
} || '';
450 my $letter = getletter
( $module, $letter_code, $branchcode )
451 or warn( "No $module $letter_code letter"),
454 my $tables = $params{tables
};
455 my $substitute = $params{substitute
};
456 my $repeat = $params{repeat
};
457 $tables || $substitute || $repeat
458 or carp
( "ERROR: nothing to substitute - both 'tables' and 'substitute' are empty" ),
460 my $want_librarian = $params{want_librarian
};
463 while ( my ($token, $val) = each %$substitute ) {
464 $letter->{title
} =~ s/<<$token>>/$val/g;
465 $letter->{content
} =~ s/<<$token>>/$val/g;
469 my $OPACBaseURL = C4
::Context
->preference('OPACBaseURL');
470 $letter->{content
} =~ s/<<OPACBaseURL>>/$OPACBaseURL/go;
472 if ($want_librarian) {
473 # parsing librarian name
474 my $userenv = C4
::Context
->userenv;
475 $letter->{content
} =~ s/<<LibrarianFirstname>>/$userenv->{firstname}/go;
476 $letter->{content
} =~ s/<<LibrarianSurname>>/$userenv->{surname}/go;
477 $letter->{content
} =~ s/<<LibrarianEmailaddress>>/$userenv->{emailaddress}/go;
480 my ($repeat_no_enclosing_tags, $repeat_enclosing_tags);
483 if (ref ($repeat) eq 'ARRAY' ) {
484 $repeat_no_enclosing_tags = $repeat;
486 $repeat_enclosing_tags = $repeat;
490 if ($repeat_enclosing_tags) {
491 while ( my ($tag, $tag_tables) = each %$repeat_enclosing_tags ) {
492 if ( $letter->{content
} =~ m!<$tag>(.*)</$tag>!s ) {
495 my %subletter = ( title
=> '', content
=> $subcontent );
496 _substitute_tables
( \
%subletter, $_ );
499 $letter->{content
} =~ s!<$tag>.*</$tag>!join( "\n", @lines )!se;
505 _substitute_tables
( $letter, $tables );
508 if ($repeat_no_enclosing_tags) {
509 if ( $letter->{content
} =~ m/[^\n]*<<.*>>[^\n]*/so ) {
514 $c =~ s/<<count>>/$i/go;
515 foreach my $field ( keys %{$_} ) {
516 $c =~ s/(<<[^\.]+.$field>>)/$_->{$field}/;
520 } @
$repeat_no_enclosing_tags;
522 my $replaceby = join( "\n", @lines );
523 $letter->{content
} =~ s/\Q$line\E/$replaceby/s;
527 $letter->{content
} =~ s/<<\S*>>//go; #remove any stragglers
528 # $letter->{content} =~ s/<<[^>]*>>//go;
533 sub _substitute_tables
{
534 my ( $letter, $tables ) = @_;
535 while ( my ($table, $param) = each %$tables ) {
538 my $ref = ref $param;
541 if ($ref && $ref eq 'HASH') {
546 my $sth = _parseletter_sth
($table);
548 warn "_parseletter_sth('$table') failed to return a valid sth. No substitution will be done for that table.";
551 $sth->execute( $ref ? @
$param : $param );
553 $values = $sth->fetchrow_hashref;
557 _parseletter
( $letter, $table, $values );
561 sub _parseletter_sth
{
565 carp
"ERROR: _parseletter_sth() called without argument (table)";
568 # NOTE: we used to check whether we had a statement handle cached in
569 # a %handles module-level variable. This was a dumb move and
570 # broke things for the rest of us. prepare_cached is a better
571 # way to cache statement handles anyway.
573 ($table eq 'biblio' ) ?
"SELECT * FROM $table WHERE biblionumber = ?" :
574 ($table eq 'biblioitems' ) ?
"SELECT * FROM $table WHERE biblionumber = ?" :
575 ($table eq 'items' ) ?
"SELECT * FROM $table WHERE itemnumber = ?" :
576 ($table eq 'issues' ) ?
"SELECT * FROM $table WHERE itemnumber = ?" :
577 ($table eq 'old_issues' ) ?
"SELECT * FROM $table WHERE itemnumber = ? ORDER BY timestamp DESC LIMIT 1" :
578 ($table eq 'reserves' ) ?
"SELECT * FROM $table WHERE borrowernumber = ? and biblionumber = ?" :
579 ($table eq 'borrowers' ) ?
"SELECT * FROM $table WHERE borrowernumber = ?" :
580 ($table eq 'branches' ) ?
"SELECT * FROM $table WHERE branchcode = ?" :
581 ($table eq 'suggestions' ) ?
"SELECT * FROM $table WHERE suggestionid = ?" :
582 ($table eq 'aqbooksellers') ?
"SELECT * FROM $table WHERE id = ?" :
583 ($table eq 'aqorders' ) ?
"SELECT * FROM $table WHERE ordernumber = ?" :
584 ($table eq 'opac_news' ) ?
"SELECT * FROM $table WHERE idnew = ?" :
585 ($table eq 'borrower_modifications') ?
"SELECT * FROM $table WHERE borrowernumber = ? OR verification_token =?":
588 warn "ERROR: No _parseletter_sth query for table '$table'";
589 return; # nothing to get
591 unless ($sth = C4
::Context
->dbh->prepare_cached($query)) {
592 warn "ERROR: Failed to prepare query: '$query'";
595 return $sth; # now cache is populated for that $table
598 =head2 _parseletter($letter, $table, $values)
601 - $letter : a hash to letter fields (title & content useful)
602 - $table : the Koha table to parse.
603 - $values : table record hashref
604 parse all fields from a table, and replace values in title & content with the appropriate value
605 (not exported sub, used only internally)
610 my ( $letter, $table, $values ) = @_;
612 if ( $table eq 'reserves' && $values->{'waitingdate'} ) {
613 my @waitingdate = split /-/, $values->{'waitingdate'};
615 my $dt = dt_from_string
();
616 $dt->add( days
=> C4
::Context
->preference('ReservesMaxPickUpDelay') || 0);
617 $values->{'expirationdate'} = output_pref
({ dt
=> $dt, dateonly
=> 1 });
619 $values->{'waitingdate'} = output_pref
({ dt
=> dt_from_string
( $values->{'waitingdate'} ), dateonly
=> 1 });
623 if ($letter->{content
} && $letter->{content
} =~ /<<today>>/) {
624 my $todaysdate = output_pref
( DateTime
->now() );
625 $letter->{content
} =~ s/<<today>>/$todaysdate/go;
628 while ( my ($field, $val) = each %$values ) {
629 my $replacetablefield = "<<$table.$field>>";
630 my $replacefield = "<<$field>>";
631 $val =~ s/\p{P}$// if $val && $table=~/biblio/;
632 #BZ 9886: Assuming that we want to eliminate ISBD punctuation here
633 #Therefore adding the test on biblio. This includes biblioitems,
634 #but excludes items. Removed unneeded global and lookahead.
636 my $replacedby = defined ($val) ?
$val : '';
637 ($letter->{title
} ) and do {
638 $letter->{title
} =~ s/$replacetablefield/$replacedby/g;
639 $letter->{title
} =~ s/$replacefield/$replacedby/g;
641 ($letter->{content
}) and do {
642 $letter->{content
} =~ s/$replacetablefield/$replacedby/g;
643 $letter->{content
} =~ s/$replacefield/$replacedby/g;
647 if ($table eq 'borrowers' && $letter->{content
}) {
648 if ( my $attributes = GetBorrowerAttributes
($values->{borrowernumber
}) ) {
650 foreach (@
$attributes) {
651 my $code = $_->{code
};
652 my $val = $_->{value_description
} || $_->{value
};
653 $val =~ s/\p{P}(?=$)//g if $val;
654 next unless $val gt '';
656 push @
{ $attr{$code} }, $val;
658 while ( my ($code, $val_ar) = each %attr ) {
659 my $replacefield = "<<borrower-attribute:$code>>";
660 my $replacedby = join ',', @
$val_ar;
661 $letter->{content
} =~ s/$replacefield/$replacedby/g;
670 my $success = EnqueueLetter( { letter => $letter,
671 borrowernumber => '12', message_transport_type => 'email' } )
673 places a letter in the message_queue database table, which will
674 eventually get processed (sent) by the process_message_queue.pl
675 cronjob when it calls SendQueuedMessages.
677 return message_id on success
682 my $params = shift or return;
684 return unless exists $params->{'letter'};
685 # return unless exists $params->{'borrowernumber'};
686 return unless exists $params->{'message_transport_type'};
688 my $content = $params->{letter
}->{content
};
689 $content =~ s/\s+//g if(defined $content);
690 if ( not defined $content or $content eq '' ) {
691 warn "Trying to add an empty message to the message queue" if $debug;
695 # If we have any attachments we should encode then into the body.
696 if ( $params->{'attachments'} ) {
697 $params->{'letter'} = _add_attachments
(
698 { letter
=> $params->{'letter'},
699 attachments
=> $params->{'attachments'},
700 message
=> MIME
::Lite
->new( Type
=> 'multipart/mixed' ),
705 my $dbh = C4
::Context
->dbh();
706 my $statement = << 'ENDSQL';
707 INSERT INTO message_queue
708 ( borrowernumber
, subject
, content
, metadata
, letter_code
, message_transport_type
, status
, time_queued
, to_address
, from_address
, content_type
)
710 ( ?
, ?
, ?
, ?
, ?
, ?
, ?
, NOW
(), ?
, ?
, ?
)
713 my $sth = $dbh->prepare($statement);
714 my $result = $sth->execute(
715 $params->{'borrowernumber'}, # borrowernumber
716 $params->{'letter'}->{'title'}, # subject
717 $params->{'letter'}->{'content'}, # content
718 $params->{'letter'}->{'metadata'} || '', # metadata
719 $params->{'letter'}->{'code'} || '', # letter_code
720 $params->{'message_transport_type'}, # message_transport_type
722 $params->{'to_address'}, # to_address
723 $params->{'from_address'}, # from_address
724 $params->{'letter'}->{'content-type'}, # content_type
726 return $dbh->last_insert_id(undef,undef,'message_queue', undef);
729 =head2 SendQueuedMessages ([$hashref])
731 my $sent = SendQueuedMessages( { verbose => 1 } );
733 sends all of the 'pending' items in the message queue.
735 returns number of messages sent.
739 sub SendQueuedMessages
{
742 my $unsent_messages = _get_unsent_messages
();
743 MESSAGE
: foreach my $message ( @
$unsent_messages ) {
744 # warn Data::Dumper->Dump( [ $message ], [ 'message' ] );
745 warn sprintf( 'sending %s message to patron: %s',
746 $message->{'message_transport_type'},
747 $message->{'borrowernumber'} || 'Admin' )
748 if $params->{'verbose'} or $debug;
749 # This is just begging for subclassing
750 next MESSAGE
if ( lc($message->{'message_transport_type'}) eq 'rss' );
751 if ( lc( $message->{'message_transport_type'} ) eq 'email' ) {
752 _send_message_by_email
( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} );
754 elsif ( lc( $message->{'message_transport_type'} ) eq 'sms' ) {
755 _send_message_by_sms
( $message );
758 return scalar( @
$unsent_messages );
761 =head2 GetRSSMessages
763 my $message_list = GetRSSMessages( { limit => 10, borrowernumber => '14' } )
765 returns a listref of all queued RSS messages for a particular person.
772 return unless $params;
773 return unless ref $params;
774 return unless $params->{'borrowernumber'};
776 return _get_unsent_messages
( { message_transport_type
=> 'rss',
777 limit
=> $params->{'limit'},
778 borrowernumber
=> $params->{'borrowernumber'}, } );
781 =head2 GetPrintMessages
783 my $message_list = GetPrintMessages( { borrowernumber => $borrowernumber } )
785 Returns a arrayref of all queued print messages (optionally, for a particular
790 sub GetPrintMessages
{
791 my $params = shift || {};
793 return _get_unsent_messages
( { message_transport_type
=> 'print',
794 borrowernumber
=> $params->{'borrowernumber'},
798 =head2 GetQueuedMessages ([$hashref])
800 my $messages = GetQueuedMessage( { borrowernumber => '123', limit => 20 } );
802 fetches messages out of the message queue.
805 list of hashes, each has represents a message in the message queue.
809 sub GetQueuedMessages
{
812 my $dbh = C4
::Context
->dbh();
813 my $statement = << 'ENDSQL';
814 SELECT message_id
, borrowernumber
, subject
, content
, message_transport_type
, status
, time_queued
820 if ( exists $params->{'borrowernumber'} ) {
821 push @whereclauses, ' borrowernumber = ? ';
822 push @query_params, $params->{'borrowernumber'};
825 if ( @whereclauses ) {
826 $statement .= ' WHERE ' . join( 'AND', @whereclauses );
829 if ( defined $params->{'limit'} ) {
830 $statement .= ' LIMIT ? ';
831 push @query_params, $params->{'limit'};
834 my $sth = $dbh->prepare( $statement );
835 my $result = $sth->execute( @query_params );
836 return $sth->fetchall_arrayref({});
839 =head2 _add_attachements
842 letter - the standard letter hashref
843 attachments - listref of attachments. each attachment is a hashref of:
844 type - the mime type, like 'text/plain'
845 content - the actual attachment
846 filename - the name of the attachment.
847 message - a MIME::Lite object to attach these to.
849 returns your letter object, with the content updated.
853 sub _add_attachments
{
856 my $letter = $params->{'letter'};
857 my $attachments = $params->{'attachments'};
858 return $letter unless @
$attachments;
859 my $message = $params->{'message'};
861 # First, we have to put the body in as the first attachment
863 Type
=> $letter->{'content-type'} || 'TEXT',
864 Data
=> $letter->{'is_html'}
865 ? _wrap_html
($letter->{'content'}, $letter->{'title'})
866 : $letter->{'content'},
869 foreach my $attachment ( @
$attachments ) {
871 Type
=> $attachment->{'type'},
872 Data
=> $attachment->{'content'},
873 Filename
=> $attachment->{'filename'},
876 # we're forcing list context here to get the header, not the count back from grep.
877 ( $letter->{'content-type'} ) = grep( /^Content-Type:/, split( /\n/, $params->{'message'}->header_as_string ) );
878 $letter->{'content-type'} =~ s/^Content-Type:\s+//;
879 $letter->{'content'} = $message->body_as_string;
885 sub _get_unsent_messages
{
888 my $dbh = C4
::Context
->dbh();
889 my $statement = << 'ENDSQL';
890 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
891 FROM message_queue mq
892 LEFT JOIN borrowers b ON b
.borrowernumber
= mq
.borrowernumber
896 my @query_params = ('pending');
898 if ( $params->{'message_transport_type'} ) {
899 $statement .= ' AND message_transport_type = ? ';
900 push @query_params, $params->{'message_transport_type'};
902 if ( $params->{'borrowernumber'} ) {
903 $statement .= ' AND borrowernumber = ? ';
904 push @query_params, $params->{'borrowernumber'};
906 if ( $params->{'limit'} ) {
907 $statement .= ' limit ? ';
908 push @query_params, $params->{'limit'};
912 $debug and warn "_get_unsent_messages SQL: $statement";
913 $debug and warn "_get_unsent_messages params: " . join(',',@query_params);
914 my $sth = $dbh->prepare( $statement );
915 my $result = $sth->execute( @query_params );
916 return $sth->fetchall_arrayref({});
919 sub _send_message_by_email
{
920 my $message = shift or return;
921 my ($username, $password, $method) = @_;
923 my $member = C4
::Members
::GetMember
( 'borrowernumber' => $message->{'borrowernumber'} );
924 my $to_address = $message->{'to_address'};
925 unless ($to_address) {
927 warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})";
928 _set_message_status
( { message_id
=> $message->{'message_id'},
929 status
=> 'failed' } );
932 $to_address = C4
::Members
::GetNoticeEmailAddress
( $message->{'borrowernumber'} );
933 unless ($to_address) {
934 # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})";
935 # warning too verbose for this more common case?
936 _set_message_status
( { message_id
=> $message->{'message_id'},
937 status
=> 'failed' } );
942 my $utf8 = decode
('MIME-Header', $message->{'subject'} );
943 $message->{subject
}= encode
('MIME-Header', $utf8);
944 my $subject = encode
('utf8', $message->{'subject'});
945 my $content = encode
('utf8', $message->{'content'});
946 my $content_type = $message->{'content_type'} || 'text/plain; charset="UTF-8"';
947 my $is_html = $content_type =~ m/html/io;
949 my $branch_email = ( $member ) ? GetBranchDetail
( $member->{'branchcode'} )->{'branchemail'} : undef;
951 my %sendmail_params = (
953 From
=> $message->{'from_address'} || $branch_email || C4
::Context
->preference('KohaAdminEmailAddress'),
956 Message
=> $is_html ? _wrap_html
($content, $subject) : $content,
957 'content-type' => $content_type,
959 $sendmail_params{'Auth'} = {user
=> $username, pass
=> $password, method
=> $method} if $username;
960 if ( my $bcc = C4
::Context
->preference('OverdueNoticeBcc') ) {
961 $sendmail_params{ Bcc
} = $bcc;
964 _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
965 if ( sendmail
( %sendmail_params ) ) {
966 _set_message_status
( { message_id
=> $message->{'message_id'},
967 status
=> 'sent' } );
970 _set_message_status
( { message_id
=> $message->{'message_id'},
971 status
=> 'failed' } );
972 carp
$Mail::Sendmail
::error
;
978 my ($content, $title) = @_;
980 my $css = C4
::Context
->preference("NoticeCSS") || '';
981 $css = qq{<link rel
="stylesheet" type
="text/css" href
="$css">} if $css;
983 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
984 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
985 <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
987 <title>$title</title>
988 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
998 sub _send_message_by_sms
{
999 my $message = shift or return;
1000 my $member = C4
::Members
::GetMember
( 'borrowernumber' => $message->{'borrowernumber'} );
1002 unless ( $member->{smsalertnumber
} ) {
1003 _set_message_status
( { message_id
=> $message->{'message_id'},
1004 status
=> 'failed' } );
1008 my $success = C4
::SMS
->send_sms( { destination
=> $member->{'smsalertnumber'},
1009 message
=> $message->{'content'},
1011 _set_message_status
( { message_id
=> $message->{'message_id'},
1012 status
=> ($success ?
'sent' : 'failed') } );
1016 sub _update_message_to_address
{
1018 my $dbh = C4
::Context
->dbh();
1019 $dbh->do('UPDATE message_queue SET to_address=? WHERE message_id=?',undef,($to,$id));
1022 sub _set_message_status
{
1023 my $params = shift or return;
1025 foreach my $required_parameter ( qw( message_id status ) ) {
1026 return unless exists $params->{ $required_parameter };
1029 my $dbh = C4
::Context
->dbh();
1030 my $statement = 'UPDATE message_queue SET status= ? WHERE message_id = ?';
1031 my $sth = $dbh->prepare( $statement );
1032 my $result = $sth->execute( $params->{'status'},
1033 $params->{'message_id'} );