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;
28 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
32 # set the version for version checking
36 &GetLetters &getletter &addalert &getalert &delalert &findrelatedto &SendAlerts
42 C4::Letters - Give functions for Letters management
50 "Letters" is the tool used in Koha to manage informations sent to the patrons and/or the library. This include some cron jobs like
51 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)
53 Letters are managed through "alerts" sent by Koha on some events. All "alert" related functions are in this module too.
59 $letters = &getletters($category);
60 returns informations about letters.
61 if needed, $category filters for letters given category
62 Create a letter selector with the following code
66 my $letters = GetLetters($cat);
68 foreach my $thisletter (keys %$letters) {
69 my $selected = 1 if $thisletter eq $letter;
72 selected => $selected,
73 lettername => $letters->{$thisletter},
75 push @letterloop, \%row;
80 <select name="letter">
81 <option value="">Default</option>
82 <!-- TMPL_LOOP name="letterloop" -->
83 <option value="<!-- TMPL_VAR name="value" -->" <!-- TMPL_IF name="selected" -->selected<!-- /TMPL_IF -->><!-- TMPL_VAR name="lettername" --></option>
91 # returns a reference to a hash of references to ALL letters...
94 my $dbh = C4::Context->dbh;
98 my $query = "SELECT * FROM letter WHERE module = ? ORDER BY name";
99 $sth = $dbh->prepare($query);
103 my $query = " SELECT * FROM letter ORDER BY name";
104 $sth = $dbh->prepare($query);
107 while ( my $letter = $sth->fetchrow_hashref ) {
108 $letters{ $letter->{'code'} } = $letter->{'name'};
114 my ( $module, $code ) = @_;
115 my $dbh = C4::Context->dbh;
116 my $sth = $dbh->prepare("select * from letter where module=? and code=?");
117 $sth->execute( $module, $code );
118 my $line = $sth->fetchrow_hashref;
125 - $borrowernumber : the number of the borrower subscribing to the alert
126 - $type : the type of alert.
127 - externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
129 create an alert and return the alertid (primary key)
134 my ( $borrowernumber, $type, $externalid ) = @_;
135 my $dbh = C4::Context->dbh;
138 "insert into alert (borrowernumber, type, externalid) values (?,?,?)");
139 $sth->execute( $borrowernumber, $type, $externalid );
141 # get the alert number newly created and return it
142 my $alertid = $dbh->{'mysql_insertid'};
149 - alertid : the alert id
157 #warn "ALERTID : $alertid";
158 my $dbh = C4::Context->dbh;
159 my $sth = $dbh->prepare("delete from alert where alertid=?");
160 $sth->execute($alertid);
166 - $borrowernumber : the number of the borrower subscribing to the alert
167 - $type : the type of alert.
168 - externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
169 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.
174 my ( $borrowernumber, $type, $externalid ) = @_;
175 my $dbh = C4::Context->dbh;
176 my $query = "select * from alert where";
178 if ($borrowernumber =~ /^\d+$/) {
179 $query .= " borrowernumber=? and";
180 push @bind, $borrowernumber;
183 $query .= " type=? and";
187 $query .= " externalid=? and";
188 push @bind, $externalid;
191 my $sth = $dbh->prepare($query);
192 $sth->execute(@bind);
194 while ( my $line = $sth->fetchrow_hashref ) {
203 - $type : the type of alert
204 - $externalid : the id of the "object" to query
206 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.
207 When type=issue, the id is related to a subscriptionid and this sub returns the name of the biblio.
208 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->{emailaddress} ) {
285 To => $borinfo->{emailaddress},
286 From => $userenv->{emailaddress},
287 Subject => "" . $innerletter->{title},
288 Message => "" . $innerletter->{content},
292 # warn "sending to $mail{To} From $mail{From} subj $mail{Subject} Mess $mail{Message}";
296 elsif ( $type eq 'claimacquisition' ) {
298 # warn "sending issues...";
299 my $letter = getletter( 'claimacquisition', $letter );
301 # prepare the letter...
302 # search the biblionumber
304 "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 ("
305 . join( ",", @$externalid ) . ")";
306 my $sthorders = $dbh->prepare($strsth);
308 my $dataorders = $sthorders->fetchall_arrayref( {} );
309 parseletter( $letter, 'aqbooksellers',
310 $dataorders->[0]->{booksellerid} );
312 $dbh->prepare("select * from aqbooksellers where id=?");
313 $sthbookseller->execute( $dataorders->[0]->{booksellerid} );
314 my $databookseller = $sthbookseller->fetchrow_hashref;
316 # parsing branch info
317 my $userenv = C4::Context->userenv;
318 parseletter( $letter, 'branches', $userenv->{branch} );
320 # parsing librarian name
321 $letter->{content} =~ s/<<LibrarianFirstname>>/$userenv->{firstname}/g;
322 $letter->{content} =~ s/<<LibrarianSurname>>/$userenv->{surname}/g;
323 $letter->{content} =~
324 s/<<LibrarianEmailaddress>>/$userenv->{emailaddress}/g;
325 foreach my $data (@$dataorders) {
326 my $line = $1 if ( $letter->{content} =~ m/(<<.*>>)/ );
327 foreach my $field ( keys %$data ) {
328 $line =~ s/(<<[^\.]+.$field>>)/$data->{$field}/;
330 $letter->{content} =~ s/(<<.*>>)/$line\n$1/;
332 $letter->{content} =~ s/<<[^>]*>>//g;
333 my $innerletter = $letter;
336 if ( $databookseller->{bookselleremail}
337 || $databookseller->{contemail} )
340 To => $databookseller->{bookselleremail}
342 $databookseller->{contemail}
343 ? "," . $databookseller->{contemail}
346 From => $userenv->{emailaddress},
347 Subject => "" . $innerletter->{title},
348 Message => "" . $innerletter->{content},
349 'Content-Type' => 'text/plain; charset="utf8"',
353 "sending to $mail{To} From $mail{From} subj $mail{Subject} Mess $mail{Message}";
355 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},
423 C4::Context->userenv->{'number'},
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}";
443 - $letter : a hash to letter fields (title & content useful)
444 - $table : the Koha table to parse.
445 - $pk : the primary key to query on the $table table
446 parse all fields from a table, and replace values in title & content with the appropriate value
447 (not exported sub, used only internally)
452 my ( $letter, $table, $pk ) = @_;
454 # warn "Parseletter : ($letter,$table,$pk)";
455 my $dbh = C4::Context->dbh;
457 if ( $table eq 'biblio' ) {
458 $sth = $dbh->prepare("select * from biblio where biblionumber=?");
460 elsif ( $table eq 'biblioitems' ) {
461 $sth = $dbh->prepare("select * from biblioitems where biblionumber=?");
463 elsif ( $table eq 'borrowers' ) {
464 $sth = $dbh->prepare("select * from borrowers where borrowernumber=?");
466 elsif ( $table eq 'branches' ) {
467 $sth = $dbh->prepare("select * from branches where branchcode=?");
469 elsif ( $table eq 'aqbooksellers' ) {
470 $sth = $dbh->prepare("select * from aqbooksellers where id=?");
474 # store the result in an hash
475 my $values = $sth->fetchrow_hashref;
477 # and get all fields from the table
478 $sth = $dbh->prepare("show columns from $table");
480 while ( ( my $field ) = $sth->fetchrow_array ) {
481 my $replacefield = "<<$table.$field>>";
482 my $replacedby = $values->{$field};
484 # warn "REPLACE $replacefield by $replacedby";
485 $letter->{title} =~ s/$replacefield/$replacedby/g;
486 $letter->{content} =~ s/$replacefield/$replacedby/g;