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
24 # use C4::Suggestions;
29 our ( $VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS );
31 # set the version for version checking
36 C4::Letters - Give functions for Letters management
44 "Letters" is the tool used in Koha to manage informations sent to the patrons and/or the library. This include some cron jobs like
45 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)
47 Letters are managed through "alerts" sent by Koha on some events. All "alert" related functions are in this module too.
53 qw(&GetLetters &getletter &addalert &getalert &delalert &findrelatedto &SendAlerts);
57 $letters = &getletters($category);
58 returns informations about letters.
59 if needed, $category filters for letters given category
60 Create a letter selector with the following code
64 my $letters = GetLetters($cat);
66 foreach my $thisletter (keys %$letters) {
67 my $selected = 1 if $thisletter eq $letter;
70 selected => $selected,
71 lettername => $letters->{$thisletter},
73 push @letterloop, \%row;
78 <select name="letter">
79 <option value="">Default</option>
80 <!-- TMPL_LOOP name="letterloop" -->
81 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="lettername" --></option>
89 # returns a reference to a hash of references to ALL letters...
92 my $dbh = C4::Context->dbh;
96 my $query = "SELECT * FROM letter WHERE module = ? ORDER BY name";
97 $sth = $dbh->prepare($query);
101 my $query = " SELECT * FROM letter ORDER BY name";
102 $sth = $dbh->prepare($query);
105 while ( my $letter = $sth->fetchrow_hashref ) {
106 $letters{ $letter->{'code'} } = $letter->{'name'};
112 my ( $module, $code ) = @_;
113 my $dbh = C4::Context->dbh;
114 my $sth = $dbh->prepare("select * from letter where module=? and code=?");
115 $sth->execute( $module, $code );
116 my $line = $sth->fetchrow_hashref;
123 - $borrowernumber : the number of the borrower subscribing to the alert
124 - $type : the type of alert.
125 - externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
127 create an alert and return the alertid (primary key)
132 my ( $borrowernumber, $type, $externalid ) = @_;
133 my $dbh = C4::Context->dbh;
136 "insert into alert (borrowernumber, type, externalid) values (?,?,?)");
137 $sth->execute( $borrowernumber, $type, $externalid );
139 # get the alert number newly created and return it
140 my $alertid = $dbh->{'mysql_insertid'};
147 - alertid : the alert id
155 #warn "ALERTID : $alertid";
156 my $dbh = C4::Context->dbh;
157 my $sth = $dbh->prepare("delete from alert where alertid=?");
158 $sth->execute($alertid);
164 - $borrowernumber : the number of the borrower subscribing to the alert
165 - $type : the type of alert.
166 - externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
167 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.
172 my ( $borrowernumber, $type, $externalid ) = @_;
173 my $dbh = C4::Context->dbh;
174 my $query = "select * from alert where";
176 if ($borrowernumber) {
177 $query .= " borrowernumber=? and";
178 push @bind, $borrowernumber;
181 $query .= " type=? and";
185 $query .= " externalid=? and";
186 push @bind, $externalid;
189 my $sth = $dbh->prepare($query);
190 $sth->execute(@bind);
192 while ( my $line = $sth->fetchrow_hashref ) {
195 return \@result if $#result >= 0; # return only if there is one result.
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
211 my ( $type, $externalid ) = @_;
212 my $dbh = C4::Context->dbh;
214 if ( $type eq 'issue' ) {
217 "select title as result from subscription left join biblio on subscription.biblionumber=biblio.biblionumber where subscriptionid=?"
220 if ( $type eq 'borrower' ) {
223 "select concat(firstname,' ',surname) from borrowers where borrowernumber=?"
226 $sth->execute($externalid);
227 my ($result) = $sth->fetchrow;
234 - $type : the type of alert
235 - $externalid : the id of the "object" to query
236 - $letter : the letter to send.
238 send an alert to all borrowers having put an alert on a given subject.
243 my ( $type, $externalid, $letter ) = @_;
244 my $dbh = C4::Context->dbh;
245 if ( $type eq 'issue' ) {
247 # warn "sending issues...";
248 my $letter = getletter( 'serial', $letter );
250 # prepare the letter...
251 # search the biblionumber
254 "select biblionumber from subscription where subscriptionid=?");
255 $sth->execute($externalid);
256 my ($biblionumber) = $sth->fetchrow;
258 # parsing branch info
259 my $userenv = C4::Context->userenv;
260 parseletter( $letter, 'branches', $userenv->{branch} );
262 # parsing librarian name
263 $letter->{content} =~ s/<<LibrarianFirstname>>/$userenv->{firstname}/g;
264 $letter->{content} =~ s/<<LibrarianSurname>>/$userenv->{surname}/g;
265 $letter->{content} =~
266 s/<<LibrarianEmailaddress>>/$userenv->{emailaddress}/g;
268 # parsing biblio information
269 parseletter( $letter, 'biblio', $biblionumber );
270 parseletter( $letter, 'biblioitems', $biblionumber );
272 # find the list of borrowers to alert
273 my $alerts = getalert( '', 'issue', $externalid );
276 # and parse borrower ...
277 my $innerletter = $letter;
278 my $borinfo = GetMember( $_->{'borrowernumber'}, 'borrowernumber' );
279 parseletter( $innerletter, 'borrowers', $_->{'borrowernumber'} );
282 if ( $borinfo->{emailaddress} ) {
284 To => $borinfo->{emailaddress},
285 From => $userenv->{emailaddress},
286 Subject => "" . $innerletter->{title},
287 Message => "" . $innerletter->{content},
291 # warn "sending to $mail{To} From $mail{From} subj $mail{Subject} Mess $mail{Message}";
295 elsif ( $type eq 'claimacquisition' ) {
297 # warn "sending issues...";
298 my $letter = getletter( 'claimacquisition', $letter );
300 # prepare the letter...
301 # search the biblionumber
303 "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 ("
304 . join( ",", @$externalid ) . ")";
305 my $sthorders = $dbh->prepare($strsth);
307 my $dataorders = $sthorders->fetchall_arrayref( {} );
308 parseletter( $letter, 'aqbooksellers',
309 $dataorders->[0]->{booksellerid} );
311 $dbh->prepare("select * from aqbooksellers where id=?");
312 $sthbookseller->execute( $dataorders->[0]->{booksellerid} );
313 my $databookseller = $sthbookseller->fetchrow_hashref;
315 # parsing branch info
316 my $userenv = C4::Context->userenv;
317 parseletter( $letter, 'branches', $userenv->{branch} );
319 # parsing librarian name
320 $letter->{content} =~ s/<<LibrarianFirstname>>/$userenv->{firstname}/g;
321 $letter->{content} =~ s/<<LibrarianSurname>>/$userenv->{surname}/g;
322 $letter->{content} =~
323 s/<<LibrarianEmailaddress>>/$userenv->{emailaddress}/g;
324 foreach my $data (@$dataorders) {
325 my $line = $1 if ( $letter->{content} =~ m/(<<.*>>)/ );
326 foreach my $field ( keys %$data ) {
327 $line =~ s/(<<[^\.]+.$field>>)/$data->{$field}/;
329 $letter->{content} =~ s/(<<.*>>)/$line\n$1/;
331 $letter->{content} =~ s/<<[^>]*>>//g;
332 my $innerletter = $letter;
335 if ( $databookseller->{bookselleremail}
336 || $databookseller->{contemail} )
339 To => $databookseller->{bookselleremail}
341 $databookseller->{contemail}
342 ? "," . $databookseller->{contemail}
345 From => $userenv->{emailaddress},
346 Subject => "" . $innerletter->{title},
347 Message => "" . $innerletter->{content},
348 'Content-Type' => 'text/plain; charset="utf8"',
352 "sending to $mail{To} From $mail{From} subj $mail{Subject} Mess $mail{Message}";
354 if ( C4::Context->preference("LetterLog") ) {
358 "Send Acquisition claim letter",
361 . join( ",", @$externalid )
362 . "\n$innerletter->{title}\n$innerletter->{content}"
366 elsif ( $type eq 'claimissues' ) {
368 # warn "sending issues...";
369 my $letter = getletter( 'claimissues', $letter );
371 # prepare the letter...
372 # search the biblionumber
374 "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 ("
375 . join( ",", @$externalid ) . ")";
376 my $sthorders = $dbh->prepare($strsth);
378 my $dataorders = $sthorders->fetchall_arrayref( {} );
379 parseletter( $letter, 'aqbooksellers',
380 $dataorders->[0]->{aqbooksellerid} );
382 $dbh->prepare("select * from aqbooksellers where id=?");
383 $sthbookseller->execute( $dataorders->[0]->{aqbooksellerid} );
384 my $databookseller = $sthbookseller->fetchrow_hashref;
386 # parsing branch info
387 my $userenv = C4::Context->userenv;
388 parseletter( $letter, 'branches', $userenv->{branch} );
390 # parsing librarian name
391 $letter->{content} =~ s/<<LibrarianFirstname>>/$userenv->{firstname}/g;
392 $letter->{content} =~ s/<<LibrarianSurname>>/$userenv->{surname}/g;
393 $letter->{content} =~
394 s/<<LibrarianEmailaddress>>/$userenv->{emailaddress}/g;
395 foreach my $data (@$dataorders) {
396 my $line = $1 if ( $letter->{content} =~ m/(<<.*>>)/ );
397 foreach my $field ( keys %$data ) {
398 $line =~ s/(<<[^\.]+.$field>>)/$data->{$field}/;
400 $letter->{content} =~ s/(<<.*>>)/$line\n$1/;
402 $letter->{content} =~ s/<<[^>]*>>//g;
403 my $innerletter = $letter;
406 if ( $databookseller->{bookselleremail}
407 || $databookseller->{contemail} )
410 To => $databookseller->{bookselleremail}
412 $databookseller->{contemail}
413 ? "," . $databookseller->{contemail}
416 From => $userenv->{emailaddress},
417 Subject => "" . $innerletter->{title},
418 Message => "" . $innerletter->{content},
422 C4::Context->userenv->{'number'},
427 . $databookseller->{contemail}
429 . $innerletter->{title}
431 . $innerletter->{content}
432 ) if C4::Context->preference("LetterLog");
435 "sending to From $userenv->{emailaddress} subj $innerletter->{title} Mess $innerletter->{content}";
442 - $letter : a hash to letter fields (title & content useful)
443 - $table : the Koha table to parse.
444 - $pk : the primary key to query on the $table table
445 parse all fields from a table, and replace values in title & content with the appropriate value
446 (not exported sub, used only internally)
451 my ( $letter, $table, $pk ) = @_;
453 # warn "Parseletter : ($letter,$table,$pk)";
454 my $dbh = C4::Context->dbh;
456 if ( $table eq 'biblio' ) {
457 $sth = $dbh->prepare("select * from biblio where biblionumber=?");
459 elsif ( $table eq 'biblioitems' ) {
460 $sth = $dbh->prepare("select * from biblioitems where biblionumber=?");
462 elsif ( $table eq 'borrowers' ) {
463 $sth = $dbh->prepare("select * from borrowers where borrowernumber=?");
465 elsif ( $table eq 'branches' ) {
466 $sth = $dbh->prepare("select * from branches where branchcode=?");
468 elsif ( $table eq 'aqbooksellers' ) {
469 $sth = $dbh->prepare("select * from aqbooksellers where id=?");
473 # store the result in an hash
474 my $values = $sth->fetchrow_hashref;
476 # and get all fields from the table
477 $sth = $dbh->prepare("show columns from $table");
479 while ( ( my $field ) = $sth->fetchrow_array ) {
480 my $replacefield = "<<$table.$field>>";
481 my $replacedby = $values->{$field};
483 # warn "REPLACE $replacefield by $replacedby";
484 $letter->{title} =~ s/$replacefield/$replacedby/g;
485 $letter->{content} =~ s/$replacefield/$replacedby/g;
489 END { } # module clean-up code here (global destructor)