removing warnings
[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                                         );
55 # FIXME - This is never used
56
57 =item newauthority
58
59   $id = &newauthority($dbh,$category,$stdlib,$freelib,$father,$level,$hierarchy);
60
61   adds an authority entry in the db.
62   It calculates the level of the authority with the authoritysep and the complete hierarchy.
63
64 C<$dbh> is a DBI::db handle for the Koha database.
65 C<$category> is the category of the entry
66 C<$stdlib> is the authority form to be created
67 C<$freelib> is a free form for the authority
68 C<$father> is the father in case of creation of a thesaurus sub-entry
69 C<$level> is the level of the entry (1 being the 1st thasaurus level)
70 C<$hierarchy> is the id of all the fathers of the enty.
71
72 Note :
73  you can safely pass a full hierarchy without testing the existence of the father.
74  As many father, grand-father... as needed are created.
75
76  Usually, this function is called with '',1,'' as the 3 lasts parameters.
77  if not provided, it's the default value.
78
79  The function is recursive
80
81  The function uses the authoritysep defined in systempreferences table to split the lib.
82
83 =cut
84
85 sub newauthority  {
86         my ($dbh,$category,$stdlib,$freelib,$father,$level,$hierarchy)=@_;
87         exit unless ($stdlib);
88         $freelib = $stdlib unless ($freelib);
89         my $dbh = C4::Context->dbh;
90         my $sth1b=$dbh->prepare("select id from bibliothesaurus where freelib=? and hierarchy=? and category=?");
91         my $sth2 =$dbh->prepare("insert into bibliothesaurus (category,stdlib,freelib,father,level,hierarchy) values (?,?,?,?,?,?)");
92         $freelib=$stdlib unless ($freelib);
93         my $authoritysep = C4::Context->preference('authoritysep');
94         my @Thierarchy = split(/$authoritysep/,$stdlib);
95         #---- split freelib. If not same structure as stdlib (different number of authoritysep),
96         #---- then, drop it => we will use stdlib to build hiearchy, freelib will be used only for last occurence.
97         my @Fhierarchy = split(/$authoritysep/,$freelib);
98         if ($#Fhierarchy eq 0) {
99                 $#Fhierarchy=-1;
100         }
101         for (my $xi=0;$xi<$#Thierarchy;$xi++) {
102                 $Thierarchy[$xi] =~ s/^\s+//;
103                 $Thierarchy[$xi] =~ s/\s+$//;
104                 my $x = &newauthority($dbh,$category,$Thierarchy[$xi],$Fhierarchy[$xi]?$Fhierarchy[$xi]:$Thierarchy[$xi],$father,$level,$hierarchy);
105                 $father .= $Thierarchy[$xi]." $authoritysep ";
106                 $hierarchy .= "$x|" if ($x);
107                 $level++;
108         }
109         my $id;
110         if ($#Thierarchy >=0) {
111                 # free form
112                 $sth1b->execute($freelib,$hierarchy,$category);
113                 ($id) = $sth1b->fetchrow;
114                 unless ($id) {
115                         $Thierarchy[$#Thierarchy] =~ s/^\s+//;
116                         $Thierarchy[$#Thierarchy] =~ s/\s+$//;
117                         $Fhierarchy[$#Fhierarchy] =~ s/^\s+// if ($#Fhierarchy>=0);
118                         $Fhierarchy[$#Fhierarchy] =~ s/\s+$// if ($#Fhierarchy>=0);
119                         $freelib =~ s/\s+$//;
120                         $sth2->execute($category,$Thierarchy[$#Thierarchy],$#Fhierarchy==$#Thierarchy?$Fhierarchy[$#Fhierarchy]:$freelib,$father,$level,$hierarchy);
121                 }
122                 # authority form
123                 $sth1b->execute($Thierarchy[$#Thierarchy],$hierarchy,$category);
124                 ($id) = $sth1b->fetchrow;
125                 unless ($id) {
126                         $Thierarchy[$#Thierarchy] =~ s/^\s+//;
127                         $Thierarchy[$#Thierarchy] =~ s/\s+$//;
128                         $sth2->execute($category,$Thierarchy[$#Thierarchy],$Thierarchy[$#Thierarchy],$father,$level,$hierarchy);
129                         $sth1b->execute($stdlib,$hierarchy,$category);
130                         ($id) = $sth1b->fetchrow;
131                 }
132         }
133         return $id;
134 }
135
136 =item SearchAuthority
137
138   $id = &SearchAuthority($dbh,$category,$branch,$searchstring,$type,$offset,$pagesize);
139
140   searches for an authority
141
142 C<$dbh> is a DBI::db handle for the Koha database.
143
144 C<$category> is the category of the authority
145
146 C<$branch> can contain a branch hierarchy. For example, if C<$branch> contains 1024|2345, SearchAuthority will return only
147 entries beginning by 1024|2345
148
149 C<$searchstring> contains a string. Only entries beginning by C<$searchstring> are returned
150
151
152 =cut
153 sub searchauthority  {
154         my ($env,$category,$branch,$searchstring,$offset,$pagesize)=@_;
155         $offset=0 unless ($offset);
156 #       warn "==> ($env,$category,$branch,$searchstring,$offset,$pagesize)";
157         my $dbh = C4::Context->dbh;
158         $searchstring=~ s/\'/\\\'/g;
159         my $query="Select stdlib,freelib,father,id,hierarchy,level from bibliothesaurus where (category =\"$category\")";
160         $query .= " and hierarchy='$branch'" if ($branch);
161         $query .= " and match (category,freelib) AGAINST ('$searchstring')" if ($searchstring);
162 #       $query .= " and freelib like \"$searchstring%\"" if ($searchstring);
163         $query .= " order by category,freelib limit $offset,".($pagesize*4);
164 #       warn "q : $query";
165         my $sth=$dbh->prepare($query);
166         $sth->execute;
167         my @results;
168         my $old_stdlib="";
169         while (my $data=$sth->fetchrow_hashref){
170                         push(@results,$data);
171         }
172         $sth->finish;
173         $query="Select count(*) from bibliothesaurus where (category =\"$category\")";
174         $query .= " and hierarchy='$branch'" if ($branch);
175         $query .= " and stdlib like \"$searchstring%\"" if ($searchstring);
176         $query .= "";
177         $sth=$dbh->prepare($query);
178         $sth->execute;
179         my ($cnt) = $sth->fetchrow;
180         $cnt = $pagesize+1 if ($cnt>$pagesize);
181         return ($cnt,\@results);
182 }
183
184
185 =item delauthority
186
187   $id = &delauthority($id);
188
189   delete an authority and all it's "childs" and "related"
190
191 C<$id> is the id of the authority
192
193 =cut
194 sub delauthority {
195         my ($id) = @_;
196         my $dbh = C4::Context->dbh;
197         # we must delete : - the id, every sons from the id.
198         # to do this, we can : reconstruct the full hierarchy of the id and delete with hierarchy as a key.
199         my $sth=$dbh->prepare("select hierarchy from bibliothesaurus where id=?");
200         $sth->execute($id);
201         my ($hierarchy) = $sth->fetchrow;
202         if ($hierarchy) {
203                 $dbh->do("delete from bibliothesaurus where hierarchy like '$hierarchy|$id|%'");
204 #               warn("delete from bibliothesaurus where hierarchy like '$hierarchy|$id|%'");
205         } else {
206                 $dbh->do("delete from bibliothesaurus where hierarchy like '$id|%'");
207 #               warn("delete from bibliothesaurus where hierarchy like '$id|%'");
208         }
209 #       warn("delete from bibliothesaurus where id='$id|'");
210         $dbh->do("delete from bibliothesaurus where id='$id|'");
211 }
212 END { }       # module clean-up code here (global destructor)
213
214 1;
215 __END__
216
217 =back
218
219 =head1 SEE ALSO
220
221 =cut