1 package C4::Authorities;
5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
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
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.
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
26 use vars qw($VERSION @ISA @EXPORT);
28 # set the version for version checking
33 C4::Accounts - Functions for dealing with Koha authorities
41 The functions in this module deal with the authorities table in koha.
42 It contains every functions to manage/find authorities.
51 @EXPORT = qw( &newauthority
60 $id = &newauthority($dbh,$category,$stdlib,$freelib,$father,$level,$hierarchy);
62 adds an authority entry in the db.
63 It calculates the level of the authority with the authoritysep and the complete hierarchy.
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.
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.
77 Usually, this function is called with '',1,'' as the 3 lasts parameters.
78 if not provided, it's the default value.
80 The function is recursive
82 The function uses the authoritysep defined in systempreferences table to split the lib.
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) {
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);
111 if ($#Thierarchy >=0) {
113 $level='' unless $level;
114 $hierarchy='' unless $hierarchy;
115 $sth1b->execute($freelib,$hierarchy,$category);
116 ($id) = $sth1b->fetchrow;
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);
126 $sth1b->execute($Thierarchy[$#Thierarchy],$hierarchy,$category);
127 ($id) = $sth1b->fetchrow;
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;
141 $id = &ModAuthority($dbh,$id,$freelib);
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
151 my ($dbh,$id,$freelib) = @_;
152 my $sth = $dbh->prepare("update bibliothesaurus set freelib=? where id=?");
153 $sth->execute($freelib,$id);
156 =item SearchAuthority
158 ($count, \@array) = &SearchAuthority($dbh,$category,$branch,$searchstring,$type,$offset,$pagesize);
160 searches for an authority
162 C<$dbh> is a DBI::db handle for the Koha database.
164 C<$category> is the category of the authority
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
169 C<$searchstring> contains a string. Only entries beginning by C<$searchstring> are returned
172 C<$count> : the number of authorities found
173 C<\@array> : the authorities found. The array contains stdlib,freelib,father,id,hierarchy and level
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);
184 $query .= " and hierarchy=?";
188 $query .= " and match (category,freelib) AGAINST (?)";
189 push(@bind,$searchstring);
191 # $query .= " and freelib like \"$searchstring%\"" if ($searchstring);
192 $query .= " order by category,freelib limit ?,?";
193 push(@bind,$offset,($pagesize*4));
195 my $sth=$dbh->prepare($query);
196 $sth->execute(@bind);
199 while (my $data=$sth->fetchrow_hashref){
200 push(@results,$data);
203 $query="Select count(*) from bibliothesaurus where category =?";
206 $query .= " and hierarchy=?";
210 $query .= " and stdlib like ?";
211 push(@bind,"$searchstring%");
213 $sth=$dbh->prepare($query);
214 $sth->execute(@bind);
215 my ($cnt) = $sth->fetchrow;
216 $cnt = $pagesize+1 if ($cnt>$pagesize);
218 return ($cnt,\@results);
223 @array = &SearchAuthority($dbh,$category,$father);
225 Finds everything depending on the parameter.
227 C<$dbh> is a DBI::db handle for the Koha database.
229 C<$category> is the category of the authority
231 C<$father> Is the string "father".
234 @array : the authorities found. The array contains stdlib,freelib,father,id,hierarchy and level
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
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 --");
248 while (my ($level,$stdlib,$father)=$sth->fetchrow){
250 $line{level} = $level;
251 $line{stdlib}= $stdlib;
252 $line{father} = $father;
253 push(@results,\%line);
262 $id = &delauthority($id);
264 delete an authority and all it's "childs" and "related"
266 C<$id> is the id of the authority
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=?");
276 my ($hierarchy) = $sth->fetchrow;
278 $dbh->do("delete from bibliothesaurus where hierarchy like '$hierarchy|$id|%'");
279 # warn("delete from bibliothesaurus where hierarchy like '$hierarchy|$id|%'");
281 $dbh->do("delete from bibliothesaurus where hierarchy like '$id|%'");
282 # warn("delete from bibliothesaurus where hierarchy like '$id|%'");
284 # warn("delete from bibliothesaurus where id='$id|'");
285 $dbh->do("delete from bibliothesaurus where id='$id|'");
287 END { } # module clean-up code here (global destructor)