minor fixes in thesaurus_popup & authorities.pm
[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         $searchstring=~ s/\'/\\\'/g;
182         my $query="Select stdlib,freelib,father,id,hierarchy,level from bibliothesaurus where (category =\"$category\")";
183         $query .= " and hierarchy='$branch'" if ($branch);
184         $query .= " and match (category,freelib) AGAINST ('$searchstring')" if ($searchstring);
185 #       $query .= " and freelib like \"$searchstring%\"" if ($searchstring);
186         $query .= " order by category,freelib limit $offset,".($pagesize*4);
187 #       warn "q : $query";
188         my $sth=$dbh->prepare($query);
189         $sth->execute;
190         my @results;
191         my $old_stdlib="";
192         while (my $data=$sth->fetchrow_hashref){
193                         push(@results,$data);
194         }
195         $sth->finish;
196         $query="Select count(*) from bibliothesaurus where (category =\"$category\")";
197         $query .= " and hierarchy='$branch'" if ($branch);
198         $query .= " and stdlib like \"$searchstring%\"" if ($searchstring);
199         $query .= "";
200         $sth=$dbh->prepare($query);
201         $sth->execute;
202         my ($cnt) = $sth->fetchrow;
203         $cnt = $pagesize+1 if ($cnt>$pagesize);
204         return ($cnt,\@results);
205 }
206
207 =item SearchDeeper
208
209  @array = &SearchAuthority($dbh,$category,$father);
210
211   Finds everything depending on the parameter.
212
213 C<$dbh> is a DBI::db handle for the Koha database.
214
215 C<$category> is the category of the authority
216
217 C<$father> Is the string "father".
218
219 return :
220 @array : the authorities found. The array contains stdlib,freelib,father,id,hierarchy and level
221
222 For example :
223 Geography -- Europe is the father and the result is : France and Germany if there is
224 Geography -- Europe -- France and Geography -- Europe -- Germany in the thesaurus
225
226
227 =cut
228 sub SearchDeeper  {
229         my ($category,$father)=@_;
230         my $dbh = C4::Context->dbh;
231         my $query="Select distinct level,stdlib,father from bibliothesaurus where category =? and father =? order by category,stdlib";
232         my $sth=$dbh->prepare($query);
233         $sth->execute($category,"$father --");
234         my @results;
235         while (my ($level,$stdlib,$father)=$sth->fetchrow){
236                         my %line;
237                         $line{level} = $level;
238                         $line{stdlib}= $stdlib;
239                         $line{father} = $father;
240                         push(@results,\%line);
241         }
242         $sth->finish;
243         return (@results);
244 }
245
246
247 =item delauthority
248
249   $id = &delauthority($id);
250
251   delete an authority and all it's "childs" and "related"
252
253 C<$id> is the id of the authority
254
255 =cut
256 sub delauthority {
257         my ($id) = @_;
258         my $dbh = C4::Context->dbh;
259         # we must delete : - the id, every sons from the id.
260         # to do this, we can : reconstruct the full hierarchy of the id and delete with hierarchy as a key.
261         my $sth=$dbh->prepare("select hierarchy from bibliothesaurus where id=?");
262         $sth->execute($id);
263         my ($hierarchy) = $sth->fetchrow;
264         if ($hierarchy) {
265                 $dbh->do("delete from bibliothesaurus where hierarchy like '$hierarchy|$id|%'");
266 #               warn("delete from bibliothesaurus where hierarchy like '$hierarchy|$id|%'");
267         } else {
268                 $dbh->do("delete from bibliothesaurus where hierarchy like '$id|%'");
269 #               warn("delete from bibliothesaurus where hierarchy like '$id|%'");
270         }
271 #       warn("delete from bibliothesaurus where id='$id|'");
272         $dbh->do("delete from bibliothesaurus where id='$id|'");
273 }
274 END { }       # module clean-up code here (global destructor)
275
276 1;
277 __END__
278
279 =back
280
281 =head1 SEE ALSO
282
283 =cut