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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
27 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
31 # set the version for version checking
35 &GetLetters &getletter &addalert &getalert &delalert &findrelatedto &SendAlerts
41 C4::Letters - Give functions for Letters management
49 "Letters" is the tool used in Koha to manage informations sent to the patrons and/or the library. This include some cron jobs like
50 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)
52 Letters are managed through "alerts" sent by Koha on some events. All "alert" related functions are in this module too.
58 $letters = &getletters($category);
59 returns informations about letters.
60 if needed, $category filters for letters given category
61 Create a letter selector with the following code
65 my $letters = GetLetters($cat);
67 foreach my $thisletter (keys %$letters) {
68 my $selected = 1 if $thisletter eq $letter;
71 selected => $selected,
72 lettername => $letters->{$thisletter},
74 push @letterloop, \%row;
79 <select name="letter">
80 <option value="">Default</option>
81 <!-- TMPL_LOOP name="letterloop" -->
82 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="lettername" --></option>
90 # returns a reference to a hash of references to ALL letters...
93 my $dbh = C4::Context->dbh;
97 my $query = "SELECT * FROM letter WHERE module = ? ORDER BY name";
98 $sth = $dbh->prepare($query);
102 my $query = " SELECT * FROM letter ORDER BY name";
103 $sth = $dbh->prepare($query);
106 while ( my $letter = $sth->fetchrow_hashref ) {
107 $letters{ $letter->{'code'} } = $letter->{'name'};
113 my ( $module, $code ) = @_;
114 my $dbh = C4::Context->dbh;
115 my $sth = $dbh->prepare("select * from letter where module=? and code=?");
116 $sth->execute( $module, $code );
117 my $line = $sth->fetchrow_hashref;
124 - $borrowernumber : the number of the borrower subscribing to the alert
125 - $type : the type of alert.
126 - externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
128 create an alert and return the alertid (primary key)
133 my ( $borrowernumber, $type, $externalid ) = @_;
134 my $dbh = C4::Context->dbh;
137 "insert into alert (borrowernumber, type, externalid) values (?,?,?)");
138 $sth->execute( $borrowernumber, $type, $externalid );
140 # get the alert number newly created and return it
141 my $alertid = $dbh->{'mysql_insertid'};
148 - alertid : the alert id
156 #warn "ALERTID : $alertid";
157 my $dbh = C4::Context->dbh;
158 my $sth = $dbh->prepare("delete from alert where alertid=?");
159 $sth->execute($alertid);
165 - $borrowernumber : the number of the borrower subscribing to the alert
166 - $type : the type of alert.
167 - externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
168 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.
173 my ( $borrowernumber, $type, $externalid ) = @_;
174 my $dbh = C4::Context->dbh;
175 my $query = "SELECT * FROM alert WHERE";
177 if ($borrowernumber =~ /^\d+$/) {
178 $query .= " borrowernumber=? AND ";
179 push @bind, $borrowernumber;
182 $query .= " type=? AND ";
186 $query .= " externalid=? AND ";
187 push @bind, $externalid;
189 $query =~ s/ AND $//;
190 my $sth = $dbh->prepare($query);
191 $sth->execute(@bind);
193 while ( my $line = $sth->fetchrow_hashref ) {
202 - $type : the type of alert
203 - $externalid : the id of the "object" to query
205 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.
206 When type=issue, the id is related to a subscriptionid and this sub returns the name of the biblio.
207 When type=virtual, the id is related to a virtual shelf and this sub returns the name of the sub
212 my ( $type, $externalid ) = @_;
213 my $dbh = C4::Context->dbh;
215 if ( $type eq 'issue' ) {
218 "select title as result from subscription left join biblio on subscription.biblionumber=biblio.biblionumber where subscriptionid=?"
221 if ( $type eq 'borrower' ) {
224 "select concat(firstname,' ',surname) from borrowers where borrowernumber=?"
227 $sth->execute($externalid);
228 my ($result) = $sth->fetchrow;
235 - $type : the type of alert
236 - $externalid : the id of the "object" to query
237 - $letter : the letter to send.
239 send an alert to all borrowers having put an alert on a given subject.
244 my ( $type, $externalid, $letter ) = @_;
245 my $dbh = C4::Context->dbh;
246 if ( $type eq 'issue' ) {
248 # warn "sending issues...";
249 my $letter = getletter( 'serial', $letter );
251 # prepare the letter...
252 # search the biblionumber
255 "SELECT biblionumber FROM subscription WHERE subscriptionid=?");
256 $sth->execute($externalid);
257 my ($biblionumber) = $sth->fetchrow;
259 # parsing branch info
260 my $userenv = C4::Context->userenv;
261 parseletter( $letter, 'branches', $userenv->{branch} );
263 # parsing librarian name
264 $letter->{content} =~ s/<<LibrarianFirstname>>/$userenv->{firstname}/g;
265 $letter->{content} =~ s/<<LibrarianSurname>>/$userenv->{surname}/g;
266 $letter->{content} =~
267 s/<<LibrarianEmailaddress>>/$userenv->{emailaddress}/g;
269 # parsing biblio information
270 parseletter( $letter, 'biblio', $biblionumber );
271 parseletter( $letter, 'biblioitems', $biblionumber );
273 # find the list of borrowers to alert
274 my $alerts = getalert( '', 'issue', $externalid );
277 # and parse borrower ...
278 my $innerletter = $letter;
279 my $borinfo = GetMember( $_->{'borrowernumber'}, 'borrowernumber' );
280 parseletter( $innerletter, 'borrowers', $_->{'borrowernumber'} );
283 if ( $borinfo->{email} ) {
285 To => $borinfo->{email},
286 From => $borinfo->{email},
287 Subject => "" . $innerletter->{title},
288 Message => "" . $innerletter->{content},
289 'Content-Type' => 'text/plain; charset="utf8"',
293 # warn "sending to $mail{To} From $mail{From} subj $mail{Subject} Mess $mail{Message}";
297 elsif ( $type eq 'claimacquisition' ) {
299 # warn "sending issues...";
300 my $letter = getletter( 'claimacquisition', $letter );
302 # prepare the letter...
303 # search the biblionumber
305 "select aqorders.*,aqbasket.*,biblio.*,biblioitems.* from aqorders LEFT JOIN aqbasket on aqbasket.basketno=aqorders.basketno LEFT JOIN biblio on aqorders.biblionumber=biblio.biblionumber LEFT JOIN biblioitems on aqorders.biblioitemnumber=biblioitems.biblioitemnumber where aqorders.ordernumber IN ("
306 . join( ",", @$externalid ) . ")";
307 my $sthorders = $dbh->prepare($strsth);
309 my $dataorders = $sthorders->fetchall_arrayref( {} );
310 parseletter( $letter, 'aqbooksellers',
311 $dataorders->[0]->{booksellerid} );
313 $dbh->prepare("select * from aqbooksellers where id=?");
314 $sthbookseller->execute( $dataorders->[0]->{booksellerid} );
315 my $databookseller = $sthbookseller->fetchrow_hashref;
317 # parsing branch info
318 my $userenv = C4::Context->userenv;
319 parseletter( $letter, 'branches', $userenv->{branch} );
321 # parsing librarian name
322 $letter->{content} =~ s/<<LibrarianFirstname>>/$userenv->{firstname}/g;
323 $letter->{content} =~ s/<<LibrarianSurname>>/$userenv->{surname}/g;
324 $letter->{content} =~
325 s/<<LibrarianEmailaddress>>/$userenv->{emailaddress}/g;
326 foreach my $data (@$dataorders) {
327 my $line = $1 if ( $letter->{content} =~ m/(<<.*>>)/ );
328 foreach my $field ( keys %$data ) {
329 $line =~ s/(<<[^\.]+.$field>>)/$data->{$field}/;
331 $letter->{content} =~ s/(<<.*>>)/$line\n$1/;
333 $letter->{content} =~ s/<<[^>]*>>//g;
334 my $innerletter = $letter;
337 if ( $databookseller->{bookselleremail}
338 || $databookseller->{contemail} )
341 To => $databookseller->{bookselleremail}
343 $databookseller->{contemail}
344 ? "," . $databookseller->{contemail}
347 From => $userenv->{emailaddress},
348 Subject => "" . $innerletter->{title},
349 Message => "" . $innerletter->{content},
350 'Content-Type' => 'text/plain; charset="utf8"',
354 "sending to $mail{To} From $mail{From} subj $mail{Subject} Mess $mail{Message}";
356 if ( C4::Context->preference("LetterLog") ) {
359 "Send Acquisition claim letter",
362 . join( ",", @$externalid )
363 . "\n$innerletter->{title}\n$innerletter->{content}"
367 elsif ( $type eq 'claimissues' ) {
369 # warn "sending issues...";
370 my $letter = getletter( 'claimissues', $letter );
372 # prepare the letter...
373 # search the biblionumber
375 "select serial.*,subscription.*, biblio.* from serial LEFT JOIN subscription on serial.subscriptionid=subscription.subscriptionid LEFT JOIN biblio on serial.biblionumber=biblio.biblionumber where serial.serialid IN ("
376 . join( ",", @$externalid ) . ")";
377 my $sthorders = $dbh->prepare($strsth);
379 my $dataorders = $sthorders->fetchall_arrayref( {} );
380 parseletter( $letter, 'aqbooksellers',
381 $dataorders->[0]->{aqbooksellerid} );
383 $dbh->prepare("select * from aqbooksellers where id=?");
384 $sthbookseller->execute( $dataorders->[0]->{aqbooksellerid} );
385 my $databookseller = $sthbookseller->fetchrow_hashref;
387 # parsing branch info
388 my $userenv = C4::Context->userenv;
389 parseletter( $letter, 'branches', $userenv->{branch} );
391 # parsing librarian name
392 $letter->{content} =~ s/<<LibrarianFirstname>>/$userenv->{firstname}/g;
393 $letter->{content} =~ s/<<LibrarianSurname>>/$userenv->{surname}/g;
394 $letter->{content} =~
395 s/<<LibrarianEmailaddress>>/$userenv->{emailaddress}/g;
396 foreach my $data (@$dataorders) {
397 my $line = $1 if ( $letter->{content} =~ m/(<<.*>>)/ );
398 foreach my $field ( keys %$data ) {
399 $line =~ s/(<<[^\.]+.$field>>)/$data->{$field}/;
401 $letter->{content} =~ s/(<<.*>>)/$line\n$1/;
403 $letter->{content} =~ s/<<[^>]*>>//g;
404 my $innerletter = $letter;
407 if ( $databookseller->{bookselleremail}
408 || $databookseller->{contemail} )
411 To => $databookseller->{bookselleremail}
413 $databookseller->{contemail}
414 ? "," . $databookseller->{contemail}
417 From => $userenv->{emailaddress},
418 Subject => "" . $innerletter->{title},
419 Message => "" . $innerletter->{content},
420 'Content-Type' => 'text/plain; charset="utf8"',
428 . $databookseller->{contemail}
430 . $innerletter->{title}
432 . $innerletter->{content}
433 ) if C4::Context->preference("LetterLog");
436 "sending to From $userenv->{emailaddress} subj $innerletter->{title} Mess $innerletter->{content}";
438 # send an "account details" notice to a newly created user
439 elsif ( $type eq 'members' ) {
440 $letter->{content} =~ s/<<borrowers.title>>/$externalid->{'title'}/g;
441 $letter->{content} =~ s/<<borrowers.firstname>>/$externalid->{'firstname'}/g;
442 $letter->{content} =~ s/<<borrowers.surname>>/$externalid->{'surname'}/g;
443 $letter->{content} =~ s/<<borrowers.userid>>/$externalid->{'userid'}/g;
444 $letter->{content} =~ s/<<borrowers.password>>/$externalid->{'password'}/g;
447 To => $externalid->{'emailaddr'},
448 From => C4::Context->preference("KohaAdminEmailAddress"),
449 Subject => $letter->{'title'},
450 Message => $letter->{'content'},
451 'Content-Type' => 'text/plain; charset="utf8"',
460 - $letter : a hash to letter fields (title & content useful)
461 - $table : the Koha table to parse.
462 - $pk : the primary key to query on the $table table
463 parse all fields from a table, and replace values in title & content with the appropriate value
464 (not exported sub, used only internally)
469 my ( $letter, $table, $pk ) = @_;
471 # warn "Parseletter : ($letter,$table,$pk)";
472 my $dbh = C4::Context->dbh;
474 if ( $table eq 'biblio' ) {
475 $sth = $dbh->prepare("select * from biblio where biblionumber=?");
477 elsif ( $table eq 'biblioitems' ) {
478 $sth = $dbh->prepare("select * from biblioitems where biblionumber=?");
480 elsif ( $table eq 'borrowers' ) {
481 $sth = $dbh->prepare("select * from borrowers where borrowernumber=?");
483 elsif ( $table eq 'branches' ) {
484 $sth = $dbh->prepare("select * from branches where branchcode=?");
486 elsif ( $table eq 'aqbooksellers' ) {
487 $sth = $dbh->prepare("select * from aqbooksellers where id=?");
491 # store the result in an hash
492 my $values = $sth->fetchrow_hashref;
494 # and get all fields from the table
495 $sth = $dbh->prepare("show columns from $table");
497 while ( ( my $field ) = $sth->fetchrow_array ) {
498 my $replacefield = "<<$table.$field>>";
499 my $replacedby = $values->{$field};
501 # warn "REPLACE $replacefield by $replacedby";
502 $letter->{title} =~ s/$replacefield/$replacedby/g;
503 $letter->{content} =~ s/$replacefield/$replacedby/g;
511 my $success = EnqueueLetter( { letter => $letter, borrowernumber => '12', message_transport_type => 'email' } )
513 places a letter in the message_queue database table, which will
514 eventually get processed (sent) by the process_message_queue.pl
515 cronjob when it calls SendQueuedMessages.
517 return true on success
526 return unless exists $params->{'letter'};
527 return unless exists $params->{'borrowernumber'};
528 return unless exists $params->{'message_transport_type'};
530 # If we have any attachments we should encode then into the body.
531 if ( $params->{'attachments'} ) {
532 $params->{'letter'} = _add_attachments(
533 { letter => $params->{'letter'},
534 attachments => $params->{'attachments'},
535 message => MIME::Lite->new( Type => 'multipart/mixed' ),
540 my $dbh = C4::Context->dbh();
541 my $statement = << 'ENDSQL';
542 INSERT INTO message_queue
543 ( borrowernumber, subject, content, message_transport_type, status, time_queued, to_address, from_address, content_type )
545 ( ?, ?, ?, ?, ?, NOW(), ?, ?, ? )
548 my $sth = $dbh->prepare($statement);
549 my $result = $sth->execute(
550 $params->{'borrowernumber'}, # borrowernumber
551 $params->{'letter'}->{'title'}, # subject
552 $params->{'letter'}->{'content'}, # content
553 $params->{'message_transport_type'}, # message_transport_type
555 $params->{'to_address'}, # to_address
556 $params->{'from_address'}, # from_address
557 $params->{'letter'}->{'content-type'}, # content_type
562 =head2 SendQueuedMessages
568 sends all of the 'pending' items in the message queue.
570 my $sent = SendQueuedMessages( { verbose => 1 } )
572 returns number of messages sent.
578 sub SendQueuedMessages {
581 my $unsent_messages = _get_unsent_messages();
582 MESSAGE: foreach my $message ( @$unsent_messages ) {
583 # warn Data::Dumper->Dump( [ $message ], [ 'message' ] );
584 warn sprintf( 'sending %s message to patron: %s',
585 $message->{'message_transport_type'},
586 $message->{'borrowernumber'} || 'Admin' )
587 if $params->{'verbose'};
588 # This is just begging for subclassing
589 next MESSAGE if ( lc( $message->{'message_transport_type'} eq 'rss' ) );
590 if ( lc( $message->{'message_transport_type'} ) eq 'email' ) {
591 _send_message_by_email( $message );
593 if ( lc( $message->{'message_transport_type'} ) eq 'sms' ) {
594 _send_message_by_sms( $message );
597 return scalar( @$unsent_messages );
600 =head2 GetRSSMessages
604 my $message_list = GetRSSMessages( { limit => 10, borrowernumber => '14' } )
606 returns a listref of all queued RSS messages for a particular person.
615 return unless $params;
616 return unless ref $params;
617 return unless $params->{'borrowernumber'};
619 return _get_unsent_messages( { message_transport_type => 'rss',
620 limit => $params->{'limit'},
621 borrowernumber => $params->{'borrowernumber'}, } );
624 =head2 GetQueuedMessages
628 my $messages = GetQueuedMessage( { borrowernumber => '123', limit => 20 } );
630 fetches messages out of the message queue.
633 list of hashes, each has represents a message in the message queue.
639 sub GetQueuedMessages {
642 my $dbh = C4::Context->dbh();
643 my $statement = << 'ENDSQL';
644 SELECT message_id, borrowernumber, subject, content, message_transport_type, status, time_queued
650 if ( exists $params->{'borrowernumber'} ) {
651 push @whereclauses, ' borrowernumber = ? ';
652 push @query_params, $params->{'borrowernumber'};
655 if ( @whereclauses ) {
656 $statement .= ' WHERE ' . join( 'AND', @whereclauses );
659 if ( defined $params->{'limit'} ) {
660 $statement .= ' LIMIT ? ';
661 push @query_params, $params->{'limit'};
664 my $sth = $dbh->prepare( $statement );
665 my $result = $sth->execute( @query_params );
666 my $messages = $sth->fetchall_arrayref({});
670 =head2 _add_attachements
673 letter - the standard letter hashref
674 attachments - listref of attachments. each attachment is a hashref of:
675 type - the mime type, like 'text/plain'
676 content - the actual attachment
677 filename - the name of the attachment.
678 message - a MIME::Lite object to attach these to.
680 returns your letter object, with the content updated.
684 sub _add_attachments {
687 return unless 'HASH' eq ref $params;
688 foreach my $required_parameter (qw( letter attachments message )) {
689 return unless exists $params->{$required_parameter};
691 return $params->{'letter'} unless @{ $params->{'attachments'} };
693 # First, we have to put the body in as the first attachment
694 $params->{'message'}->attach(
696 Data => $params->{'letter'}->{'content'},
699 foreach my $attachment ( @{ $params->{'attachments'} } ) {
700 $params->{'message'}->attach(
701 Type => $attachment->{'type'},
702 Data => $attachment->{'content'},
703 Filename => $attachment->{'filename'},
706 # we're forcing list context here to get the header, not the count back from grep.
707 ( $params->{'letter'}->{'content-type'} ) = grep( /^Content-Type:/, split( /\n/, $params->{'message'}->header_as_string ) );
708 $params->{'letter'}->{'content-type'} =~ s/^Content-Type:\s+//;
709 $params->{'letter'}->{'content'} = $params->{'message'}->body_as_string;
711 return $params->{'letter'};
715 sub _get_unsent_messages {
718 my $dbh = C4::Context->dbh();
719 my $statement = << 'ENDSQL';
720 SELECT message_id, borrowernumber, subject, content, message_transport_type, status, time_queued, from_address, to_address, content_type
722 WHERE status = 'pending'
727 if ( $params->{'message_transport_type'} ) {
728 $statement .= ' AND message_transport_type = ? ';
729 push @query_params, $params->{'message_transport_type'};
731 if ( $params->{'borrowernumber'} ) {
732 $statement .= ' AND borrowernumber = ? ';
733 push @query_params, $params->{'borrowernumber'};
735 if ( $params->{'limit'} ) {
736 $statement .= ' limit ? ';
737 push @query_params, $params->{'limit'};
741 my $sth = $dbh->prepare( $statement );
742 my $result = $sth->execute( @query_params );
743 my $unsent_messages = $sth->fetchall_arrayref({});
744 return $unsent_messages;
747 sub _send_message_by_email {
750 my $member = C4::Members::GetMember( $message->{'borrowernumber'} );
752 my %sendmail_params = (
753 To => $message->{'to_address'} || $member->{'email'},
754 From => $message->{'from_address'} || C4::Context->preference('KohaAdminEmailAddress'),
755 Subject => $message->{'subject'},
756 Message => $message->{'content'},
758 if ($message->{'content_type'}) {
759 $sendmail_params{'content-type'} = $message->{'content_type'};
761 my $success = sendmail( %sendmail_params );
764 # warn "OK. Log says:\n", $Mail::Sendmail::log;
765 _set_message_status( { message_id => $message->{'message_id'},
766 status => 'sent' } );
769 # warn $Mail::Sendmail::error;
770 _set_message_status( { message_id => $message->{'message_id'},
771 status => 'failed' } );
776 sub _send_message_by_sms {
779 my $member = C4::Members::GetMember( $message->{'borrowernumber'} );
780 return unless $member->{'smsalertnumber'};
782 my $success = C4::SMS->send_sms( { destination => $member->{'smsalertnumber'},
783 message => $message->{'content'},
786 _set_message_status( { message_id => $message->{'message_id'},
787 status => 'sent' } );
790 _set_message_status( { message_id => $message->{'message_id'},
791 status => 'failed' } );
796 sub _set_message_status {
799 foreach my $required_parameter ( qw( message_id status ) ) {
800 return unless exists $params->{ $required_parameter };
803 my $dbh = C4::Context->dbh();
804 my $statement = 'UPDATE message_queue SET status= ? WHERE message_id = ?';
805 my $sth = $dbh->prepare( $statement );
806 my $result = $sth->execute( $params->{'status'},
807 $params->{'message_id'} );