Letters / alert system, continuing...
[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 C4::Date;
23 use Date::Manip;
24 use C4::Suggestions;
25 require Exporter;
26
27 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28
29 # set the version for version checking
30 $VERSION = 0.01;
31
32 =head1 NAME
33
34 C4::Letters - Give functions for Letters management
35
36 =head1 SYNOPSIS
37
38   use C4::Letters;
39
40 =head1 DESCRIPTION
41
42   "Letters" is the tool used in Koha to manage informations sent to the patrons and/or the library. This include some cron jobs like
43   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)
44   
45   Letters are managed through "alerts" sent by Koha on some events. All "alert" related functions are in this module too.
46
47 =cut
48
49 @ISA = qw(Exporter);
50 @EXPORT = qw(&GetLetterList &addalert &getalert &delalert &findrelatedto);
51
52 =head2 GetLetterList
53
54         parameter : $module : the name of the module
55         This sub returns an array of hashes with all letters from a given module
56         Each hash entry contains :
57         - module : the module name
58         - code : the code of the letter, char(20)
59         - name : the complete name of the letter, char(200)
60         - title : the title that will be used as "subject" in mails, char(200)
61         - 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 
62
63 =cut
64
65 sub GetLetterList {
66         my ($module) = @_;
67         my $dbh = C4::Context->dbh;
68         my $sth = $dbh->prepare("select * from letter where module=?");
69         $sth->execute($module);
70         my @result;
71         while (my $line = $sth->fetchrow_hashref) {
72                 push @result,$line;
73         }
74         return @result;
75 }
76
77 =head2 addalert
78
79         parameters : 
80         - $borrowernumber : the number of the borrower subscribing to the alert
81         - $type : the type of alert.
82         - externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
83
84         create an alert and return the alertid (primary key)
85         
86 =cut
87
88 sub addalert {
89         my ($borrowernumber,$type,$externalid) = @_;
90         my $dbh=C4::Context->dbh;
91         my $sth = $dbh->prepare("insert into alert (borrowernumber, type, externalid) values (?,?,?)");
92         $sth->execute($borrowernumber,$type,$externalid);
93         # get the alert number newly created and return it
94         my $alertid = $dbh->{'mysql_insertid'};
95         return $alertid;
96 }
97
98 =head2 delalert
99         parameters :
100         - alertid : the alert id
101         deletes the alert
102 =cut
103
104 sub delalert {
105         my ($alertid)=@_;
106 #       warn "ALERTID : $alertid";
107         my $dbh = C4::Context->dbh;
108         my $sth = $dbh->prepare("delete from alert where alertid=?");
109         $sth->execute($alertid);
110 }
111
112 =head2 getalert
113
114         parameters :
115         - $borrowernumber : the number of the borrower subscribing to the alert
116         - $type : the type of alert.
117         - externalid : the primary key of the object to put alert on. For issues, the alert is made on subscriptionid.
118         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.
119         
120 =cut
121
122 sub getalert {
123         my ($borrowernumber,$type,$externalid) = @_;
124         my $dbh=C4::Context->dbh;
125         my $query = "select * from alert where";
126         my @bind;
127         if ($borrowernumber) {
128                 $query .= " borrowernumber=? and";
129                 push @bind,$borrowernumber;
130         }
131         if ($type) {
132                 $query .= " type=? and";
133                 push @bind,$type;
134         }
135         if ($externalid) {
136                 $query .= " externalid=? and";
137                 push @bind,$externalid;
138         }
139         $query =~ s/ and$//;
140 #       warn "Q : $query";
141         my $sth = $dbh->prepare($query);
142         $sth->execute(@bind);
143         my @result;
144         while (my $line = $sth->fetchrow_hashref) {
145                 push @result,$line;
146         }
147         return \@result if $#result >=0; # return only if there is one result.
148         return;
149 }
150 =head2 findrelatedto
151         parameters :
152         - $type : the type of alert
153         - $externalid : the id of the "object" to query
154         
155         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.
156         When type=issue, the id is related to a subscriptionid and this sub returns the name of the biblio.
157         When type=virtual, the id is related to a virtual shelf and this sub returns the name of the sub
158 =cut
159 sub findrelatedto {
160         my ($type,$externalid) = @_;
161         my $dbh=C4::Context->dbh;
162         my $sth;
163         if ($type eq "issue") {
164                 $sth=$dbh->prepare("select title as result from subscription left join biblio on subscription.biblionumber=biblio.biblionumber where subscriptionid=?");
165         }
166         $sth->execute($externalid);
167         my ($result) = $sth->fetchrow;
168         return $result;
169 }
170
171 END { }       # module clean-up code here (global destructor)