4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
29 use vars qw($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.
52 @EXPORT = qw(&GetLetterList &getletter &addalert &getalert &delalert &findrelatedto &sendalerts);
56 parameter : $module : the name of the module
57 This sub returns an array of hashes with all letters from a given module
58 Each hash entry contains :
59 - module : the module name
60 - code : the code of the letter, char(20)
61 - name : the complete name of the letter, char(200)
62 - title : the title that will be used as "subject" in mails, char(200)
63 - content : the content of the letter. Each field to be replaced by a value at runtime is enclosed in << and >>. The fields usually have the same name as in the DB
69 my $dbh = C4::Context->dbh;
70 my $sth = $dbh->prepare("select * from letter where module=?");
71 $sth->execute($module);
73 while (my $line = $sth->fetchrow_hashref) {
80 my ($module,$code) = @_;
81 my $dbh = C4::Context->dbh;
82 my $sth = $dbh->prepare("select * from letter where module=? and code=?");
83 $sth->execute($module,$code);
84 my $line = $sth->fetchrow_hashref;
91 - $borrowernumber : the number of the borrower subscribing to the alert
92 - $type : the type of alert.
93 - externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
95 create an alert and return the alertid (primary key)
100 my ($borrowernumber,$type,$externalid) = @_;
101 my $dbh=C4::Context->dbh;
102 my $sth = $dbh->prepare("insert into alert (borrowernumber, type, externalid) values (?,?,?)");
103 $sth->execute($borrowernumber,$type,$externalid);
104 # get the alert number newly created and return it
105 my $alertid = $dbh->{'mysql_insertid'};
111 - alertid : the alert id
117 # warn "ALERTID : $alertid";
118 my $dbh = C4::Context->dbh;
119 my $sth = $dbh->prepare("delete from alert where alertid=?");
120 $sth->execute($alertid);
126 - $borrowernumber : the number of the borrower subscribing to the alert
127 - $type : the type of alert.
128 - externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
129 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.
134 my ($borrowernumber,$type,$externalid) = @_;
135 my $dbh=C4::Context->dbh;
136 my $query = "select * from alert where";
138 if ($borrowernumber) {
139 $query .= " borrowernumber=? and";
140 push @bind,$borrowernumber;
143 $query .= " type=? and";
147 $query .= " externalid=? and";
148 push @bind,$externalid;
151 my $sth = $dbh->prepare($query);
152 $sth->execute(@bind);
154 while (my $line = $sth->fetchrow_hashref) {
157 return \@result if $#result >=0; # return only if there is one result.
163 - $type : the type of alert
164 - $externalid : the id of the "object" to query
166 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.
167 When type=issue, the id is related to a subscriptionid and this sub returns the name of the biblio.
168 When type=virtual, the id is related to a virtual shelf and this sub returns the name of the sub
172 my ($type,$externalid) = @_;
173 my $dbh=C4::Context->dbh;
175 if ($type eq 'issue') {
176 $sth=$dbh->prepare("select title as result from subscription left join biblio on subscription.biblionumber=biblio.biblionumber where subscriptionid=?");
178 if ($type eq 'borrower') {
179 $sth=$dbh->prepare("select concat(firstname,' ',surname) from borrowers where borrowernumber=?");
181 $sth->execute($externalid);
182 my ($result) = $sth->fetchrow;
188 - $type : the type of alert
189 - $externalid : the id of the "object" to query
190 - $letter : the letter to send.
192 send an alert to all borrowers having put an alert on a given subject.
197 my ($type,$externalid,$letter)=@_;
198 warn "sendalerts : ($type,$externalid,$letter)";
199 my $dbh=C4::Context->dbh;
200 if ($type eq 'issue') {
201 # warn "sending issues...";
202 my $letter = getletter('serial',$letter);
203 # prepare the letter...
204 # search the biblionumber
205 my $sth=$dbh->prepare("select biblionumber from subscription where subscriptionid=?");
206 $sth->execute($externalid);
207 my ($biblionumber)=$sth->fetchrow;
208 parseletter($letter,'biblio',$biblionumber);
209 parseletter($letter,'biblioitems',$biblionumber);
210 # find the list of borrowers to alert
211 my $alerts = getalert('','issue',$externalid);
213 my $innerletter = $letter;
214 my $borinfo = getmember('',$_->{'borrowernumber'});
215 parseletter($innerletter,'borrowers',$_->{'borrowernumber'});
216 my $userenv = C4::Context->userenv;
217 if ($borinfo->{emailaddress}) {
218 my %mail = ( To => $borinfo->{emailaddress},
219 From => $userenv->{emailaddress},
220 Subject => "".$innerletter->{title},
221 Message => "".$innerletter->{content},
224 # warn "sending to $mail{To} From $mail{From} subj $mail{Subject} Mess $mail{Message}";
232 - $letter : a hash to letter fields (title & content useful)
233 - $table : the Koha table to parse.
234 - $pk : the primary key to query on the $table table
235 parse all fields from a table, and replace values in title & content with the appropriate value
238 my ($letter,$table,$pk) = @_;
239 # warn "Parseletter : ($letter,$table,$pk)";
240 my $dbh=C4::Context->dbh;
242 if ($table eq 'biblio') {
243 $sth = $dbh->prepare("select * from biblio where biblionumber=?");
244 } elsif ($table eq 'biblioitems') {
245 $sth = $dbh->prepare("select * from biblioitems where biblionumber=?");
246 } elsif ($table eq 'borrowers') {
247 $sth = $dbh->prepare("select * from borrowers where borrowernumber=?");
250 # store the result in an hash
251 my $values = $sth->fetchrow_hashref;
252 # and get all fields from the table
253 $sth = $dbh->prepare("show columns from $table");
255 while ((my $field) = $sth->fetchrow_array) {
256 my $replacefield="<<$table.$field>>";
257 my $replacedby = $values->{$field};
258 # warn "REPLACE $replacefield by $replacedby";
259 $letter->{title} =~ s/$replacefield/$replacedby/g;
260 $letter->{content} =~ s/$replacefield/$replacedby/g;
264 END { } # module clean-up code here (global destructor)