removing "warn"
[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                                         );
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 ModAuthority
137
138   $id = &ModAuthority($dbh,$id,$freelib);
139
140   modify a free lib
141
142  C<$dbh> is a DBI::db handle for the Koha database.
143  C<$id> is the entry id
144  C<$freelib> is the new freelib
145
146 =cut
147 sub modauthority {
148         my ($dbh,$id,$freelib) = @_;
149         my $sth = $dbh->prepare("update bibliothesaurus set freelib=? where id=?");
150         $sth->execute($freelib,$id);
151 }
152
153 =item SearchAuthority
154
155   $id = &SearchAuthority($dbh,$category,$branch,$searchstring,$type,$offset,$pagesize);
156
157   searches for an authority
158
159 C<$dbh> is a DBI::db handle for the Koha database.
160
161 C<$category> is the category of the authority
162
163 C<$branch> can contain a branch hierarchy. For example, if C<$branch> contains 1024|2345, SearchAuthority will return only
164 entries beginning by 1024|2345
165
166 C<$searchstring> contains a string. Only entries beginning by C<$searchstring> are returned
167
168
169 =cut
170 sub searchauthority  {
171         my ($env,$category,$branch,$searchstring,$offset,$pagesize)=@_;
172         $offset=0 unless ($offset);
173 #       warn "==> ($env,$category,$branch,$searchstring,$offset,$pagesize)";
174         my $dbh = C4::Context->dbh;
175         $searchstring=~ s/\'/\\\'/g;
176         my $query="Select stdlib,freelib,father,id,hierarchy,level from bibliothesaurus where (category =\"$category\")";
177         $query .= " and hierarchy='$branch'" if ($branch);
178         $query .= " and match (category,freelib) AGAINST ('$searchstring')" if ($searchstring);
179 #       $query .= " and freelib like \"$searchstring%\"" if ($searchstring);
180         $query .= " order by category,freelib limit $offset,".($pagesize*4);
181 #       warn "q : $query";
182         my $sth=$dbh->prepare($query);
183         $sth->execute;
184         my @results;
185         my $old_stdlib="";
186         while (my $data=$sth->fetchrow_hashref){
187                         push(@results,$data);
188         }
189         $sth->finish;
190         $query="Select count(*) from bibliothesaurus where (category =\"$category\")";
191         $query .= " and hierarchy='$branch'" if ($branch);
192         $query .= " and stdlib like \"$searchstring%\"" if ($searchstring);
193         $query .= "";
194         $sth=$dbh->prepare($query);
195         $sth->execute;
196         my ($cnt) = $sth->fetchrow;
197         $cnt = $pagesize+1 if ($cnt>$pagesize);
198         return ($cnt,\@results);
199 }
200
201
202 =item delauthority
203
204   $id = &delauthority($id);
205
206   delete an authority and all it's "childs" and "related"
207
208 C<$id> is the id of the authority
209
210 =cut
211 sub delauthority {
212         my ($id) = @_;
213         my $dbh = C4::Context->dbh;
214         # we must delete : - the id, every sons from the id.
215         # to do this, we can : reconstruct the full hierarchy of the id and delete with hierarchy as a key.
216         my $sth=$dbh->prepare("select hierarchy from bibliothesaurus where id=?");
217         $sth->execute($id);
218         my ($hierarchy) = $sth->fetchrow;
219         if ($hierarchy) {
220                 $dbh->do("delete from bibliothesaurus where hierarchy like '$hierarchy|$id|%'");
221 #               warn("delete from bibliothesaurus where hierarchy like '$hierarchy|$id|%'");
222         } else {
223                 $dbh->do("delete from bibliothesaurus where hierarchy like '$id|%'");
224 #               warn("delete from bibliothesaurus where hierarchy like '$id|%'");
225         }
226 #       warn("delete from bibliothesaurus where id='$id|'");
227         $dbh->do("delete from bibliothesaurus where id='$id|'");
228 }
229 END { }       # module clean-up code here (global destructor)
230
231 1;
232 __END__
233
234 =back
235
236 =head1 SEE ALSO
237
238 =cut