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
59 $id = &newauthority($dbh,$category,$stdlib,$freelib,$father,$level,$hierarchy);
61 adds an authority entry in the db.
62 It calculates the level of the authority with the authoritysep and the complete hierarchy.
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.
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.
76 Usually, this function is called with '',1,'' as the 3 lasts parameters.
77 if not provided, it's the default value.
79 The function is recursive
81 The function uses the authoritysep defined in systempreferences table to split the lib.
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) {
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);
110 if ($#Thierarchy >=0) {
112 $sth1b->execute($freelib,$hierarchy,$category);
113 ($id) = $sth1b->fetchrow;
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);
123 $sth1b->execute($Thierarchy[$#Thierarchy],$hierarchy,$category);
124 ($id) = $sth1b->fetchrow;
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;
138 $id = &ModAuthority($dbh,$id,$freelib);
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
148 my ($dbh,$id,$freelib) = @_;
149 my $sth = $dbh->prepare("update bibliothesaurus set freelib=? where id=?");
150 $sth->execute($freelib,$id);
153 =item SearchAuthority
155 $id = &SearchAuthority($dbh,$category,$branch,$searchstring,$type,$offset,$pagesize);
157 searches for an authority
159 C<$dbh> is a DBI::db handle for the Koha database.
161 C<$category> is the category of the authority
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
166 C<$searchstring> contains a string. Only entries beginning by C<$searchstring> are returned
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);
182 my $sth=$dbh->prepare($query);
186 while (my $data=$sth->fetchrow_hashref){
187 push(@results,$data);
190 $query="Select count(*) from bibliothesaurus where (category =\"$category\")";
191 $query .= " and hierarchy='$branch'" if ($branch);
192 $query .= " and stdlib like \"$searchstring%\"" if ($searchstring);
194 $sth=$dbh->prepare($query);
196 my ($cnt) = $sth->fetchrow;
197 $cnt = $pagesize+1 if ($cnt>$pagesize);
198 return ($cnt,\@results);
204 $id = &delauthority($id);
206 delete an authority and all it's "childs" and "related"
208 C<$id> is the id of the authority
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=?");
218 my ($hierarchy) = $sth->fetchrow;
220 $dbh->do("delete from bibliothesaurus where hierarchy like '$hierarchy|$id|%'");
221 # warn("delete from bibliothesaurus where hierarchy like '$hierarchy|$id|%'");
223 $dbh->do("delete from bibliothesaurus where hierarchy like '$id|%'");
224 # warn("delete from bibliothesaurus where hierarchy like '$id|%'");
226 # warn("delete from bibliothesaurus where id='$id|'");
227 $dbh->do("delete from bibliothesaurus where id='$id|'");
229 END { } # module clean-up code here (global destructor)