adding printcirculationsplit parameter (already existed, but was not in systempref...
[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         $level=1 unless $level;
90         $freelib = $stdlib unless ($freelib);
91         my $dbh = C4::Context->dbh;
92         my $sth1b=$dbh->prepare("select id from bibliothesaurus where freelib=? and hierarchy=? and category=?");
93         my $sth2 =$dbh->prepare("insert into bibliothesaurus (category,stdlib,freelib,father,level,hierarchy) values (?,?,?,?,?,?)");
94         $freelib=$stdlib unless ($freelib);
95         my $authoritysep = C4::Context->preference('authoritysep');
96         my @Thierarchy = split(/$authoritysep/,$stdlib);
97         #---- split freelib. If not same structure as stdlib (different number of authoritysep),
98         #---- then, drop it => we will use stdlib to build hiearchy, freelib will be used only for last occurence.
99         my @Fhierarchy = split(/$authoritysep/,$freelib);
100         if ($#Fhierarchy eq 0) {
101                 $#Fhierarchy=-1;
102         }
103         for (my $xi=0;$xi<$#Thierarchy;$xi++) {
104                 $Thierarchy[$xi] =~ s/^\s+//;
105                 $Thierarchy[$xi] =~ s/\s+$//;
106                 my $x = &newauthority($dbh,$category,$Thierarchy[$xi],$Fhierarchy[$xi]?$Fhierarchy[$xi]:$Thierarchy[$xi],$father,$level,$hierarchy);
107                 $father .= $Thierarchy[$xi]." $authoritysep ";
108                 $hierarchy .= "$x|" if ($x);
109                 $level++;
110         }
111         my $id;
112         if ($#Thierarchy >=0) {
113                 # free form
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                 } else {
125                 }
126                 # authority form
127                 $sth1b->execute($Thierarchy[$#Thierarchy],$hierarchy,$category);
128                 ($id) = $sth1b->fetchrow;
129                 unless ($id) {
130                         $Thierarchy[$#Thierarchy] =~ s/^\s+//;
131                         $Thierarchy[$#Thierarchy] =~ s/\s+$//;
132                         $sth1b->execute($stdlib,$hierarchy,$category);
133                         ($id) = $sth1b->fetchrow;
134                         unless ($id) {
135                                 $sth2->execute($category,$Thierarchy[$#Thierarchy],$Thierarchy[$#Thierarchy],$father,$level,$hierarchy);
136                         }
137                 }
138         }
139         return $id;
140 }
141
142 =item ModAuthority
143
144   $id = &ModAuthority($dbh,$id,$freelib);
145
146   modify a free lib
147
148  C<$dbh> is a DBI::db handle for the Koha database.
149  C<$id> is the entry id
150  C<$freelib> is the new freelib
151
152 =cut
153 sub modauthority {
154         my ($dbh,$id,$freelib) = @_;
155         my $sth = $dbh->prepare("update bibliothesaurus set freelib=? where id=?");
156         $sth->execute($freelib,$id);
157 }
158
159 =item SearchAuthority
160
161   ($count, \@array) = &SearchAuthority($dbh,$category,$branch,$searchstring,$type,$offset,$pagesize);
162
163   searches for an authority
164
165 C<$dbh> is a DBI::db handle for the Koha database.
166
167 C<$category> is the category of the authority
168
169 C<$branch> can contain a branch hierarchy. For example, if C<$branch> contains 1024|2345, SearchAuthority will return only
170 entries beginning by 1024|2345
171
172 C<$searchstring> contains a string. Only entries beginning by C<$searchstring> are returned
173
174 return :
175 C<$count> : the number of authorities found
176 C<\@array> : the authorities found. The array contains stdlib,freelib,father,id,hierarchy and level
177
178 =cut
179 sub searchauthority  {
180         my ($env,$category,$branch,$searchstring,$offset,$pagesize)=@_;
181         $offset=0 unless ($offset);
182 #       warn "==> ($env,$category,$branch,$searchstring,$offset,$pagesize)";
183         my $dbh = C4::Context->dbh;
184         my $query="Select stdlib,freelib,father,id,hierarchy,level from bibliothesaurus where category=?";
185         my @bind=($category);
186         if ($branch) {
187                 $query .= " and hierarchy=?";
188                 push(@bind,$branch);
189                 }
190         if ($searchstring) {
191                 $query .= " and match (category,freelib) AGAINST (?)";
192                 push(@bind,$searchstring);
193                 }
194 #       $query .= " and freelib like \"$searchstring%\"" if ($searchstring);
195         $query .= " limit ?,?";
196         push(@bind,$offset,($pagesize*4));
197         warn "q : $query".@bind;
198         my $sth=$dbh->prepare($query);
199         $sth->execute(@bind);
200         my @results;
201         my $old_stdlib="";
202         while (my $data=$sth->fetchrow_hashref){
203                         push(@results,$data);
204         }
205         $sth->finish;
206         $query="Select count(*) from bibliothesaurus where category =?";
207         @bind=($category);
208         if ($branch) {
209                 $query .= " and hierarchy=?";
210                 push(@bind,$branch);
211                 }
212         if ($searchstring) {
213                 $query .= " and stdlib like ?";
214                 push(@bind,"$searchstring%");
215                 }
216         $sth=$dbh->prepare($query);
217         $sth->execute(@bind);
218         my ($cnt) = $sth->fetchrow;
219         $cnt = $pagesize+1 if ($cnt>$pagesize);
220         $sth->finish();
221         return ($#results,\@results);
222 }
223
224 =item SearchDeeper
225
226  @array = &SearchAuthority($dbh,$category,$father);
227
228   Finds everything depending on the parameter.
229
230 C<$dbh> is a DBI::db handle for the Koha database.
231
232 C<$category> is the category of the authority
233
234 C<$father> Is the string "father".
235
236 return :
237 @array : the authorities found. The array contains stdlib,freelib,father,id,hierarchy and level
238
239 For example :
240 Geography -- Europe is the father and the result is : France and Germany if there is
241 Geography -- Europe -- France and Geography -- Europe -- Germany in the thesaurus
242
243
244 =cut
245 sub SearchDeeper  {
246         my ($category,$father)=@_;
247         my $dbh = C4::Context->dbh;
248         my $sth=$dbh->prepare("Select distinct level,stdlib,father from bibliothesaurus where category =? and father =? order by category,stdlib");
249         $sth->execute($category,"$father --");
250         my @results;
251         while (my ($level,$stdlib,$father)=$sth->fetchrow){
252                         my %line;
253                         $line{level} = $level;
254                         $line{stdlib}= $stdlib;
255                         $line{father} = $father;
256                         push(@results,\%line);
257         }
258         $sth->finish;
259         return (@results);
260 }
261
262
263 =item delauthority
264
265   $id = &delauthority($id);
266
267   delete an authority and all it's "childs" and "related"
268
269 C<$id> is the id of the authority
270
271 =cut
272 sub delauthority {
273         my ($id) = @_;
274         my $dbh = C4::Context->dbh;
275         # we must delete : - the id, every sons from the id.
276         # to do this, we can : reconstruct the full hierarchy of the id and delete with hierarchy as a key.
277         my $sth=$dbh->prepare("select hierarchy from bibliothesaurus where id=?");
278         $sth->execute($id);
279         my ($hierarchy) = $sth->fetchrow;
280         if ($hierarchy) {
281                 $dbh->do("delete from bibliothesaurus where hierarchy like '$hierarchy|$id|%'");
282 #               warn("delete from bibliothesaurus where hierarchy like '$hierarchy|$id|%'");
283         } else {
284                 $dbh->do("delete from bibliothesaurus where hierarchy like '$id|%'");
285 #               warn("delete from bibliothesaurus where hierarchy like '$id|%'");
286         }
287 #       warn("delete from bibliothesaurus where id='$id|'");
288         $dbh->do("delete from bibliothesaurus where id='$id|'");
289 }
290 END { }       # module clean-up code here (global destructor)
291
292 1;
293 __END__
294
295 =back
296
297 =head1 SEE ALSO
298
299 =cut