DBI changes as part of bug 662
[koha.git] / C4 / Authorities.pm
1 package C4::Authorities;
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 require Exporter;
24 use DBI;
25 use C4::Context;
26 use vars qw($VERSION @ISA @EXPORT);
27
28 # set the version for version checking
29 $VERSION = 0.01;
30
31 =head1 NAME
32
33 C4::Accounts - Functions for dealing with Koha authorities
34
35 =head1 SYNOPSIS
36
37   use C4::Authorities;
38
39 =head1 DESCRIPTION
40
41 The functions in this module deal with the authorities table in koha.
42 It contains every functions to manage/find authorities.
43
44 =head1 FUNCTIONS
45
46 =over 2
47
48 =cut
49
50 @ISA = qw(Exporter);
51 @EXPORT = qw(   &newauthority
52                                                 &searchauthority
53                                                 &delauthority
54                                                 &modauthority
55                                                 &SearchDeeper
56                                         );
57
58 =item newauthority
59
60   $id = &newauthority($dbh,$category,$stdlib,$freelib,$father,$level,$hierarchy);
61
62   adds an authority entry in the db.
63   It calculates the level of the authority with the authoritysep and the complete hierarchy.
64
65 C<$dbh> is a DBI::db handle for the Koha database.
66 C<$category> is the category of the entry
67 C<$stdlib> is the authority form to be created
68 C<$freelib> is a free form for the authority
69 C<$father> is the father in case of creation of a thesaurus sub-entry
70 C<$level> is the level of the entry (1 being the 1st thasaurus level)
71 C<$hierarchy> is the id of all the fathers of the enty.
72
73 Note :
74  you can safely pass a full hierarchy without testing the existence of the father.
75  As many father, grand-father... as needed are created.
76
77  Usually, this function is called with '',1,'' as the 3 lasts parameters.
78  if not provided, it's the default value.
79
80  The function is recursive
81
82  The function uses the authoritysep defined in systempreferences table to split the lib.
83
84 =cut
85
86 sub newauthority  {
87         my ($dbh,$category,$stdlib,$freelib,$father,$level,$hierarchy)=@_;
88         exit unless ($stdlib);
89         $freelib = $stdlib unless ($freelib);
90         my $dbh = C4::Context->dbh;
91         my $sth1b=$dbh->prepare("select id from bibliothesaurus where freelib=? and hierarchy=? and category=?");
92         my $sth2 =$dbh->prepare("insert into bibliothesaurus (category,stdlib,freelib,father,level,hierarchy) values (?,?,?,?,?,?)");
93         $freelib=$stdlib unless ($freelib);
94         my $authoritysep = C4::Context->preference('authoritysep');
95         my @Thierarchy = split(/$authoritysep/,$stdlib);
96         #---- split freelib. If not same structure as stdlib (different number of authoritysep),
97         #---- then, drop it => we will use stdlib to build hiearchy, freelib will be used only for last occurence.
98         my @Fhierarchy = split(/$authoritysep/,$freelib);
99         if ($#Fhierarchy eq 0) {
100                 $#Fhierarchy=-1;
101         }
102         for (my $xi=0;$xi<$#Thierarchy;$xi++) {
103                 $Thierarchy[$xi] =~ s/^\s+//;
104                 $Thierarchy[$xi] =~ s/\s+$//;
105                 my $x = &newauthority($dbh,$category,$Thierarchy[$xi],$Fhierarchy[$xi]?$Fhierarchy[$xi]:$Thierarchy[$xi],$father,$level,$hierarchy);
106                 $father .= $Thierarchy[$xi]." $authoritysep ";
107                 $hierarchy .= "$x|" if ($x);
108                 $level++;
109         }
110         my $id;
111         if ($#Thierarchy >=0) {
112                 # free form
113                 $level='' unless $level;
114                 $hierarchy='' unless $hierarchy;
115                 $sth1b->execute($freelib,$hierarchy,$category);
116                 ($id) = $sth1b->fetchrow;
117                 unless ($id) {
118                         $Thierarchy[$#Thierarchy] =~ s/^\s+//;
119                         $Thierarchy[$#Thierarchy] =~ s/\s+$//;
120                         $Fhierarchy[$#Fhierarchy] =~ s/^\s+// if ($#Fhierarchy>=0);
121                         $Fhierarchy[$#Fhierarchy] =~ s/\s+$// if ($#Fhierarchy>=0);
122                         $freelib =~ s/\s+$//;
123                         $sth2->execute($category,$Thierarchy[$#Thierarchy],$#Fhierarchy==$#Thierarchy?$Fhierarchy[$#Fhierarchy]:$freelib,$father,$level,$hierarchy);
124                 }
125                 # authority form
126                 $sth1b->execute($Thierarchy[$#Thierarchy],$hierarchy,$category);
127                 ($id) = $sth1b->fetchrow;
128                 unless ($id) {
129                         $Thierarchy[$#Thierarchy] =~ s/^\s+//;
130                         $Thierarchy[$#Thierarchy] =~ s/\s+$//;
131                         $sth2->execute($category,$Thierarchy[$#Thierarchy],$Thierarchy[$#Thierarchy],$father,$level,$hierarchy);
132                         $sth1b->execute($stdlib,$hierarchy,$category);
133                         ($id) = $sth1b->fetchrow;
134                 }
135         }
136         return $id;
137 }
138
139 =item ModAuthority
140
141   $id = &ModAuthority($dbh,$id,$freelib);
142
143   modify a free lib
144
145  C<$dbh> is a DBI::db handle for the Koha database.
146  C<$id> is the entry id
147  C<$freelib> is the new freelib
148
149 =cut
150 sub modauthority {
151         my ($dbh,$id,$freelib) = @_;
152         my $sth = $dbh->prepare("update bibliothesaurus set freelib=? where id=?");
153         $sth->execute($freelib,$id);
154 }
155
156 =item SearchAuthority
157
158   ($count, \@array) = &SearchAuthority($dbh,$category,$branch,$searchstring,$type,$offset,$pagesize);
159
160   searches for an authority
161
162 C<$dbh> is a DBI::db handle for the Koha database.
163
164 C<$category> is the category of the authority
165
166 C<$branch> can contain a branch hierarchy. For example, if C<$branch> contains 1024|2345, SearchAuthority will return only
167 entries beginning by 1024|2345
168
169 C<$searchstring> contains a string. Only entries beginning by C<$searchstring> are returned
170
171 return :
172 C<$count> : the number of authorities found
173 C<\@array> : the authorities found. The array contains stdlib,freelib,father,id,hierarchy and level
174
175 =cut
176 sub searchauthority  {
177         my ($env,$category,$branch,$searchstring,$offset,$pagesize)=@_;
178         $offset=0 unless ($offset);
179 #       warn "==> ($env,$category,$branch,$searchstring,$offset,$pagesize)";
180         my $dbh = C4::Context->dbh;
181         my $query="Select stdlib,freelib,father,id,hierarchy,level from bibliothesaurus where category=?";
182         my @bind=($category);
183         if ($branch) {
184                 $query .= " and hierarchy=?";
185                 push(@bind,$branch);
186                 }
187         if ($searchstring) {
188                 $query .= " and match (category,freelib) AGAINST (?)";
189                 push(@bind,$searchstring);
190                 }
191 #       $query .= " and freelib like \"$searchstring%\"" if ($searchstring);
192         $query .= " order by category,freelib limit ?,?";
193         push(@bind,$offset,($pagesize*4));
194 #       warn "q : $query";
195         my $sth=$dbh->prepare($query);
196         $sth->execute(@bind);
197         my @results;
198         my $old_stdlib="";
199         while (my $data=$sth->fetchrow_hashref){
200                         push(@results,$data);
201         }
202         $sth->finish;
203         $query="Select count(*) from bibliothesaurus where category =?";
204         @bind=($category);
205         if ($branch) {
206                 $query .= " and hierarchy=?";
207                 push(@bind,$branch);
208                 }
209         if ($searchstring) {
210                 $query .= " and stdlib like ?";
211                 push(@bind,"$searchstring%");
212                 }
213         $sth=$dbh->prepare($query);
214         $sth->execute(@bind);
215         my ($cnt) = $sth->fetchrow;
216         $cnt = $pagesize+1 if ($cnt>$pagesize);
217         $sth->finish();
218         return ($cnt,\@results);
219 }
220
221 =item SearchDeeper
222
223  @array = &SearchAuthority($dbh,$category,$father);
224
225   Finds everything depending on the parameter.
226
227 C<$dbh> is a DBI::db handle for the Koha database.
228
229 C<$category> is the category of the authority
230
231 C<$father> Is the string "father".
232
233 return :
234 @array : the authorities found. The array contains stdlib,freelib,father,id,hierarchy and level
235
236 For example :
237 Geography -- Europe is the father and the result is : France and Germany if there is
238 Geography -- Europe -- France and Geography -- Europe -- Germany in the thesaurus
239
240
241 =cut
242 sub SearchDeeper  {
243         my ($category,$father)=@_;
244         my $dbh = C4::Context->dbh;
245         my $sth=$dbh->prepare("Select distinct level,stdlib,father from bibliothesaurus where category =? and father =? order by category,stdlib");
246         $sth->execute($category,"$father --");
247         my @results;
248         while (my ($level,$stdlib,$father)=$sth->fetchrow){
249                         my %line;
250                         $line{level} = $level;
251                         $line{stdlib}= $stdlib;
252                         $line{father} = $father;
253                         push(@results,\%line);
254         }
255         $sth->finish;
256         return (@results);
257 }
258
259
260 =item delauthority
261
262   $id = &delauthority($id);
263
264   delete an authority and all it's "childs" and "related"
265
266 C<$id> is the id of the authority
267
268 =cut
269 sub delauthority {
270         my ($id) = @_;
271         my $dbh = C4::Context->dbh;
272         # we must delete : - the id, every sons from the id.
273         # to do this, we can : reconstruct the full hierarchy of the id and delete with hierarchy as a key.
274         my $sth=$dbh->prepare("select hierarchy from bibliothesaurus where id=?");
275         $sth->execute($id);
276         my ($hierarchy) = $sth->fetchrow;
277         if ($hierarchy) {
278                 $dbh->do("delete from bibliothesaurus where hierarchy like '$hierarchy|$id|%'");
279 #               warn("delete from bibliothesaurus where hierarchy like '$hierarchy|$id|%'");
280         } else {
281                 $dbh->do("delete from bibliothesaurus where hierarchy like '$id|%'");
282 #               warn("delete from bibliothesaurus where hierarchy like '$id|%'");
283         }
284 #       warn("delete from bibliothesaurus where id='$id|'");
285         $dbh->do("delete from bibliothesaurus where id='$id|'");
286 }
287 END { }       # module clean-up code here (global destructor)
288
289 1;
290 __END__
291
292 =back
293
294 =head1 SEE ALSO
295
296 =cut