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 ) {
201 - $type : the type of alert
202 - $externalid : the id of the "object" to query
204 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.
205 When type=issue, the id is related to a subscriptionid and this sub returns the name of the biblio.
206 When type=virtual, the id is related to a virtual shelf and this sub returns the name of the sub
210 my ( $type, $externalid ) = @_;
211 my $dbh = C4
::Context
->dbh;
213 if ( $type eq 'issue' ) {
216 "select title as result from subscription left join biblio on subscription.biblionumber=biblio.biblionumber where subscriptionid=?"
219 if ( $type eq 'borrower' ) {
222 "select concat(firstname,' ',surname) from borrowers where borrowernumber=?"
225 $sth->execute($externalid);
226 my ($result) = $sth->fetchrow;
233 - $type : the type of alert
234 - $externalid : the id of the "object" to query
235 - $letter : the letter to send.
237 send an alert to all borrowers having put an alert on a given subject.
242 my ( $type, $externalid, $letter ) = @_;
243 my $dbh = C4
::Context
->dbh;
244 if ( $type eq 'issue' ) {
246 # warn "sending issues...";
247 my $letter = getletter
( 'serial', $letter );
249 # prepare the letter...
250 # search the biblionumber
253 "select biblionumber from subscription where subscriptionid=?");
254 $sth->execute($externalid);
255 my ($biblionumber) = $sth->fetchrow;
257 # parsing branch info
258 my $userenv = C4
::Context
->userenv;
259 parseletter
( $letter, 'branches', $userenv->{branch
} );
261 # parsing librarian name
262 $letter->{content
} =~ s/<<LibrarianFirstname>>/$userenv->{firstname}/g;
263 $letter->{content
} =~ s/<<LibrarianSurname>>/$userenv->{surname}/g;
264 $letter->{content
} =~
265 s/<<LibrarianEmailaddress>>/$userenv->{emailaddress}/g;
267 # parsing biblio information
268 parseletter
( $letter, 'biblio', $biblionumber );
269 parseletter
( $letter, 'biblioitems', $biblionumber );
271 # find the list of borrowers to alert
272 my $alerts = getalert
( '', 'issue', $externalid );
275 # and parse borrower ...
276 my $innerletter = $letter;
277 my $borinfo = GetMember
( $_->{'borrowernumber'}, 'borrowernumber' );
278 parseletter
( $innerletter, 'borrowers', $_->{'borrowernumber'} );
281 if ( $borinfo->{emailaddress
} ) {
283 To
=> $borinfo->{emailaddress
},
284 From
=> $userenv->{emailaddress
},
285 Subject
=> "" . $innerletter->{title
},
286 Message
=> "" . $innerletter->{content
},
290 # warn "sending to $mail{To} From $mail{From} subj $mail{Subject} Mess $mail{Message}";
294 elsif ( $type eq 'claimacquisition' ) {
296 # warn "sending issues...";
297 my $letter = getletter
( 'claimacquisition', $letter );
299 # prepare the letter...
300 # search the biblionumber
302 "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 ("
303 . join( ",", @
$externalid ) . ")";
304 my $sthorders = $dbh->prepare($strsth);
306 my $dataorders = $sthorders->fetchall_arrayref( {} );
307 parseletter
( $letter, 'aqbooksellers',
308 $dataorders->[0]->{booksellerid
} );
310 $dbh->prepare("select * from aqbooksellers where id=?");
311 $sthbookseller->execute( $dataorders->[0]->{booksellerid
} );
312 my $databookseller = $sthbookseller->fetchrow_hashref;
314 # parsing branch info
315 my $userenv = C4
::Context
->userenv;
316 parseletter
( $letter, 'branches', $userenv->{branch
} );
318 # parsing librarian name
319 $letter->{content
} =~ s/<<LibrarianFirstname>>/$userenv->{firstname}/g;
320 $letter->{content
} =~ s/<<LibrarianSurname>>/$userenv->{surname}/g;
321 $letter->{content
} =~
322 s/<<LibrarianEmailaddress>>/$userenv->{emailaddress}/g;
323 foreach my $data (@
$dataorders) {
324 my $line = $1 if ( $letter->{content
} =~ m/(<<.*>>)/ );
325 foreach my $field ( keys %$data ) {
326 $line =~ s/(<<[^\.]+.$field>>)/$data->{$field}/;
328 $letter->{content
} =~ s/(<<.*>>)/$line\n$1/;
330 $letter->{content
} =~ s/<<[^>]*>>//g;
331 my $innerletter = $letter;
334 if ( $databookseller->{bookselleremail
}
335 || $databookseller->{contemail
} )
338 To
=> $databookseller->{bookselleremail
}
340 $databookseller->{contemail
}
341 ?
"," . $databookseller->{contemail
}
344 From
=> $userenv->{emailaddress
},
345 Subject
=> "" . $innerletter->{title
},
346 Message
=> "" . $innerletter->{content
},
347 'Content-Type' => 'text/plain; charset="utf8"',
351 "sending to $mail{To} From $mail{From} subj $mail{Subject} Mess $mail{Message}";
353 if ( C4
::Context
->preference("LetterLog") ) {
357 "Send Acquisition claim letter",
360 . join( ",", @
$externalid )
361 . "\n$innerletter->{title}\n$innerletter->{content}"
365 elsif ( $type eq 'claimissues' ) {
367 # warn "sending issues...";
368 my $letter = getletter
( 'claimissues', $letter );
370 # prepare the letter...
371 # search the biblionumber
373 "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 ("
374 . join( ",", @
$externalid ) . ")";
375 my $sthorders = $dbh->prepare($strsth);
377 my $dataorders = $sthorders->fetchall_arrayref( {} );
378 parseletter
( $letter, 'aqbooksellers',
379 $dataorders->[0]->{aqbooksellerid
} );
381 $dbh->prepare("select * from aqbooksellers where id=?");
382 $sthbookseller->execute( $dataorders->[0]->{aqbooksellerid
} );
383 my $databookseller = $sthbookseller->fetchrow_hashref;
385 # parsing branch info
386 my $userenv = C4
::Context
->userenv;
387 parseletter
( $letter, 'branches', $userenv->{branch
} );
389 # parsing librarian name
390 $letter->{content
} =~ s/<<LibrarianFirstname>>/$userenv->{firstname}/g;
391 $letter->{content
} =~ s/<<LibrarianSurname>>/$userenv->{surname}/g;
392 $letter->{content
} =~
393 s/<<LibrarianEmailaddress>>/$userenv->{emailaddress}/g;
394 foreach my $data (@
$dataorders) {
395 my $line = $1 if ( $letter->{content
} =~ m/(<<.*>>)/ );
396 foreach my $field ( keys %$data ) {
397 $line =~ s/(<<[^\.]+.$field>>)/$data->{$field}/;
399 $letter->{content
} =~ s/(<<.*>>)/$line\n$1/;
401 $letter->{content
} =~ s/<<[^>]*>>//g;
402 my $innerletter = $letter;
405 if ( $databookseller->{bookselleremail
}
406 || $databookseller->{contemail
} )
409 To
=> $databookseller->{bookselleremail
}
411 $databookseller->{contemail
}
412 ?
"," . $databookseller->{contemail
}
415 From
=> $userenv->{emailaddress
},
416 Subject
=> "" . $innerletter->{title
},
417 Message
=> "" . $innerletter->{content
},
421 C4
::Context
->userenv->{'number'},
426 . $databookseller->{contemail
}
428 . $innerletter->{title
}
430 . $innerletter->{content
}
431 ) if C4
::Context
->preference("LetterLog");
434 "sending to From $userenv->{emailaddress} subj $innerletter->{title} Mess $innerletter->{content}";
441 - $letter : a hash to letter fields (title & content useful)
442 - $table : the Koha table to parse.
443 - $pk : the primary key to query on the $table table
444 parse all fields from a table, and replace values in title & content with the appropriate value
445 (not exported sub, used only internally)
450 my ( $letter, $table, $pk ) = @_;
452 # warn "Parseletter : ($letter,$table,$pk)";
453 my $dbh = C4
::Context
->dbh;
455 if ( $table eq 'biblio' ) {
456 $sth = $dbh->prepare("select * from biblio where biblionumber=?");
458 elsif ( $table eq 'biblioitems' ) {
459 $sth = $dbh->prepare("select * from biblioitems where biblionumber=?");
461 elsif ( $table eq 'borrowers' ) {
462 $sth = $dbh->prepare("select * from borrowers where borrowernumber=?");
464 elsif ( $table eq 'branches' ) {
465 $sth = $dbh->prepare("select * from branches where branchcode=?");
467 elsif ( $table eq 'aqbooksellers' ) {
468 $sth = $dbh->prepare("select * from aqbooksellers where id=?");
472 # store the result in an hash
473 my $values = $sth->fetchrow_hashref;
475 # and get all fields from the table
476 $sth = $dbh->prepare("show columns from $table");
478 while ( ( my $field ) = $sth->fetchrow_array ) {
479 my $replacefield = "<<$table.$field>>";
480 my $replacedby = $values->{$field};
482 # warn "REPLACE $replacefield by $replacedby";
483 $letter->{title
} =~ s/$replacefield/$replacedby/g;
484 $letter->{content
} =~ s/$replacefield/$replacedby/g;
488 END { } # module clean-up code here (global destructor)