2 new features :
[koha.git] / C4 / Letters.pm
1 package C4::Letters;
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
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
11 # version.
12 #
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.
16 #
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
20
21 use strict;
22 use Mail::Sendmail;
23 use C4::Date;
24 use Date::Manip;
25 use C4::Suggestions;
26 use C4::Members;
27 require Exporter;
28
29 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
30
31 # set the version for version checking
32 $VERSION = 0.01;
33
34 =head1 NAME
35
36 C4::Letters - Give functions for Letters management
37
38 =head1 SYNOPSIS
39
40   use C4::Letters;
41
42 =head1 DESCRIPTION
43
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)
46   
47   Letters are managed through "alerts" sent by Koha on some events. All "alert" related functions are in this module too.
48
49 =cut
50
51 @ISA = qw(Exporter);
52 @EXPORT = qw(&GetLetterList &getletter &addalert &getalert &delalert &findrelatedto &sendalerts);
53
54 =head2 GetLetterList
55
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 
64
65 =cut
66
67 sub GetLetterList {
68         my ($module) = @_;
69         my $dbh = C4::Context->dbh;
70         my $sth = $dbh->prepare("select * from letter where module=?");
71         $sth->execute($module);
72         my @result;
73         while (my $line = $sth->fetchrow_hashref) {
74                 push @result,$line;
75         }
76         return @result;
77 }
78
79 sub getletter {
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;
85         return $line;
86 }
87
88 =head2 addalert
89
90         parameters : 
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.
94
95         create an alert and return the alertid (primary key)
96         
97 =cut
98
99 sub addalert {
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'};
106         return $alertid;
107 }
108
109 =head2 delalert
110         parameters :
111         - alertid : the alert id
112         deletes the alert
113 =cut
114
115 sub delalert {
116         my ($alertid)=@_;
117 #       warn "ALERTID : $alertid";
118         my $dbh = C4::Context->dbh;
119         my $sth = $dbh->prepare("delete from alert where alertid=?");
120         $sth->execute($alertid);
121 }
122
123 =head2 getalert
124
125         parameters :
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.
130         
131 =cut
132
133 sub getalert {
134         my ($borrowernumber,$type,$externalid) = @_;
135         my $dbh=C4::Context->dbh;
136         my $query = "select * from alert where";
137         my @bind;
138         if ($borrowernumber) {
139                 $query .= " borrowernumber=? and";
140                 push @bind,$borrowernumber;
141         }
142         if ($type) {
143                 $query .= " type=? and";
144                 push @bind,$type;
145         }
146         if ($externalid) {
147                 $query .= " externalid=? and";
148                 push @bind,$externalid;
149         }
150         $query =~ s/ and$//;
151         my $sth = $dbh->prepare($query);
152         $sth->execute(@bind);
153         my @result;
154         while (my $line = $sth->fetchrow_hashref) {
155                 push @result,$line;
156         }
157         return \@result if $#result >=0; # return only if there is one result.
158         return;
159 }
160
161 =head2 findrelatedto
162         parameters :
163         - $type : the type of alert
164         - $externalid : the id of the "object" to query
165         
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
169 =cut
170
171 sub findrelatedto {
172         my ($type,$externalid) = @_;
173         my $dbh=C4::Context->dbh;
174         my $sth;
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=?");
177         }
178         if ($type eq 'borrower') {
179                 $sth=$dbh->prepare("select concat(firstname,' ',surname) from borrowers where borrowernumber=?");
180         }
181         $sth->execute($externalid);
182         my ($result) = $sth->fetchrow;
183         return $result;
184 }
185
186 =head2 sendalert
187         parameters :
188         - $type : the type of alert
189         - $externalid : the id of the "object" to query
190         - $letter : the letter to send.
191
192         send an alert to all borrowers having put an alert on a given subject.
193
194 =cut
195
196 sub sendalerts {
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);
212                 foreach (@$alerts) {
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 => 'paul.poulain@free.fr',#.$userenv->{emailaddress},
220                                                         Subject => "".$innerletter->{title},
221                                                         Message => "".$innerletter->{content},
222                                                         );
223                                 sendmail(%mail);
224 #                               warn "sending to $mail{To} From $mail{From} subj $mail{Subject} Mess $mail{Message}";
225                         }
226                 }
227         }
228 }
229
230 =head2
231         parameters :
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
236 =cut
237 sub parseletter {
238         my ($letter,$table,$pk) = @_;
239 #       warn "Parseletter : ($letter,$table,$pk)";
240         my $dbh=C4::Context->dbh;
241         my $sth;
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=?");
248         }
249         $sth->execute($pk);
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");
254         $sth->execute;
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;
261         }
262 }
263
264 END { }       # module clean-up code here (global destructor)