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
28 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
30 # set the version for version checking
35 C4::Letters - Give functions for Letters management
43 "Letters" is the tool used in Koha to manage informations sent to the patrons and/or the library. This include some cron jobs like
44 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)
46 Letters are managed through "alerts" sent by Koha on some events. All "alert" related functions are in this module too.
51 @EXPORT = qw(&GetLetterList &getletter &addalert &getalert &delalert &findrelatedto &sendalerts);
55 parameter : $module : the name of the module
56 This sub returns an array of hashes with all letters from a given module
57 Each hash entry contains :
58 - module : the module name
59 - code : the code of the letter, char(20)
60 - name : the complete name of the letter, char(200)
61 - title : the title that will be used as "subject" in mails, char(200)
62 - 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
68 my $dbh = C4::Context->dbh;
69 my $sth = $dbh->prepare("select * from letter where module=?");
70 $sth->execute($module);
72 while (my $line = $sth->fetchrow_hashref) {
79 my ($module,$code) = @_;
80 my $dbh = C4::Context->dbh;
81 my $sth = $dbh->prepare("select * from letter where module=? and code=?");
82 $sth->execute($module,$code);
83 my $line = $sth->fetchrow_hashref;
90 - $borrowernumber : the number of the borrower subscribing to the alert
91 - $type : the type of alert.
92 - externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
94 create an alert and return the alertid (primary key)
99 my ($borrowernumber,$type,$externalid) = @_;
100 my $dbh=C4::Context->dbh;
101 my $sth = $dbh->prepare("insert into alert (borrowernumber, type, externalid) values (?,?,?)");
102 $sth->execute($borrowernumber,$type,$externalid);
103 # get the alert number newly created and return it
104 my $alertid = $dbh->{'mysql_insertid'};
110 - alertid : the alert id
116 # warn "ALERTID : $alertid";
117 my $dbh = C4::Context->dbh;
118 my $sth = $dbh->prepare("delete from alert where alertid=?");
119 $sth->execute($alertid);
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.
128 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.
133 my ($borrowernumber,$type,$externalid) = @_;
134 my $dbh=C4::Context->dbh;
135 my $query = "select * from alert where";
137 if ($borrowernumber) {
138 $query .= " borrowernumber=? and";
139 push @bind,$borrowernumber;
142 $query .= " type=? and";
146 $query .= " externalid=? and";
147 push @bind,$externalid;
150 my $sth = $dbh->prepare($query);
151 $sth->execute(@bind);
153 while (my $line = $sth->fetchrow_hashref) {
156 return \@result if $#result >=0; # return only if there is one result.
162 - $type : the type of alert
163 - $externalid : the id of the "object" to query
165 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.
166 When type=issue, the id is related to a subscriptionid and this sub returns the name of the biblio.
167 When type=virtual, the id is related to a virtual shelf and this sub returns the name of the sub
171 my ($type,$externalid) = @_;
172 my $dbh=C4::Context->dbh;
174 if ($type eq 'issue') {
175 $sth=$dbh->prepare("select title as result from subscription left join biblio on subscription.biblionumber=biblio.biblionumber where subscriptionid=?");
177 if ($type eq 'borrower') {
178 $sth=$dbh->prepare("select concat(firstname,' ',surname) from borrowers where borrowernumber=?");
180 $sth->execute($externalid);
181 my ($result) = $sth->fetchrow;
187 - $type : the type of alert
188 - $externalid : the id of the "object" to query
189 - $letter : the letter to send.
191 send an alert to all borrowers having put an alert on a given subject.
196 my ($type,$externalid,$letter)=@_;
197 my $dbh=C4::Context->dbh;
198 if ($type eq 'issue') {
199 # warn "sending issues...";
200 my $letter = getletter('serial',$letter);
201 # prepare the letter...
202 # search the biblionumber
203 my $sth=$dbh->prepare("select biblionumber from subscription where subscriptionid=?");
204 $sth->execute($externalid);
205 my ($biblionumber)=$sth->fetchrow;
206 # parsing branch info
207 my $userenv = C4::Context->userenv;
208 parseletter($letter,'branches',$userenv->{branch});
209 # parsing librarian name
210 $letter->{content} =~ s/<<LibrarianFirstname>>/$userenv->{firstname}/g;
211 $letter->{content} =~ s/<<LibrarianSurname>>/$userenv->{surname}/g;
212 $letter->{content} =~ s/<<LibrarianEmailaddress>>/$userenv->{emailaddress}/g;
213 # parsing biblio information
214 parseletter($letter,'biblio',$biblionumber);
215 parseletter($letter,'biblioitems',$biblionumber);
216 # find the list of borrowers to alert
217 my $alerts = getalert('','issue',$externalid);
219 # and parse borrower ...
220 my $innerletter = $letter;
221 my $borinfo = getmember('',$_->{'borrowernumber'});
222 parseletter($innerletter,'borrowers',$_->{'borrowernumber'});
224 if ($borinfo->{emailaddress}) {
225 my %mail = ( To => $borinfo->{emailaddress},
226 From => $userenv->{emailaddress},
227 Subject => "".$innerletter->{title},
228 Message => "".$innerletter->{content},
231 # warn "sending to $mail{To} From $mail{From} subj $mail{Subject} Mess $mail{Message}";
239 - $letter : a hash to letter fields (title & content useful)
240 - $table : the Koha table to parse.
241 - $pk : the primary key to query on the $table table
242 parse all fields from a table, and replace values in title & content with the appropriate value
243 (not exported sub, used only internally)
246 my ($letter,$table,$pk) = @_;
247 # warn "Parseletter : ($letter,$table,$pk)";
248 my $dbh=C4::Context->dbh;
250 if ($table eq 'biblio') {
251 $sth = $dbh->prepare("select * from biblio where biblionumber=?");
252 } elsif ($table eq 'biblioitems') {
253 $sth = $dbh->prepare("select * from biblioitems where biblionumber=?");
254 } elsif ($table eq 'borrowers') {
255 $sth = $dbh->prepare("select * from borrowers where borrowernumber=?");
256 } elsif ($table eq 'branches') {
257 $sth = $dbh->prepare("select * from branches where branchcode=?");
260 # store the result in an hash
261 my $values = $sth->fetchrow_hashref;
262 # and get all fields from the table
263 $sth = $dbh->prepare("show columns from $table");
265 while ((my $field) = $sth->fetchrow_array) {
266 my $replacefield="<<$table.$field>>";
267 my $replacedby = $values->{$field};
268 # warn "REPLACE $replacefield by $replacedby";
269 $letter->{title} =~ s/$replacefield/$replacedby/g;
270 $letter->{content} =~ s/$replacefield/$replacedby/g;
274 END { } # module clean-up code here (global destructor)