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 $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) {
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);
112 if ($#Thierarchy >=0) {
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);
127 $sth1b->execute($Thierarchy[$#Thierarchy],$hierarchy,$category);
128 ($id) = $sth1b->fetchrow;
130 $Thierarchy[$#Thierarchy] =~ s/^\s+//;
131 $Thierarchy[$#Thierarchy] =~ s/\s+$//;
132 $sth1b->execute($stdlib,$hierarchy,$category);
133 ($id) = $sth1b->fetchrow;
135 $sth2->execute($category,$Thierarchy[$#Thierarchy],$Thierarchy[$#Thierarchy],$father,$level,$hierarchy);
144 $id = &ModAuthority($dbh,$id,$freelib);
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
154 my ($dbh,$id,$freelib) = @_;
155 my $sth = $dbh->prepare("update bibliothesaurus set freelib=? where id=?");
156 $sth->execute($freelib,$id);
159 =item SearchAuthority
161 ($count, \@array) = &SearchAuthority($dbh,$category,$branch,$searchstring,$type,$offset,$pagesize);
163 searches for an authority
165 C<$dbh> is a DBI::db handle for the Koha database.
167 C<$category> is the category of the authority
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
172 C<$searchstring> contains a string. Only entries beginning by C<$searchstring> are returned
175 C<$count> : the number of authorities found
176 C<\@array> : the authorities found. The array contains stdlib,freelib,father,id,hierarchy and level
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);
187 $query .= " and hierarchy=?";
191 $query .= " and match (category,freelib) AGAINST (?)";
192 push(@bind,$searchstring);
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);
202 while (my $data=$sth->fetchrow_hashref){
203 push(@results,$data);
206 $query="Select count(*) from bibliothesaurus where category =?";
209 $query .= " and hierarchy=?";
213 $query .= " and stdlib like ?";
214 push(@bind,"$searchstring%");
216 $sth=$dbh->prepare($query);
217 $sth->execute(@bind);
218 my ($cnt) = $sth->fetchrow;
219 $cnt = $pagesize+1 if ($cnt>$pagesize);
221 return ($#results,\@results);
226 @array = &SearchAuthority($dbh,$category,$father);
228 Finds everything depending on the parameter.
230 C<$dbh> is a DBI::db handle for the Koha database.
232 C<$category> is the category of the authority
234 C<$father> Is the string "father".
237 @array : the authorities found. The array contains stdlib,freelib,father,id,hierarchy and level
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
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 --");
251 while (my ($level,$stdlib,$father)=$sth->fetchrow){
253 $line{level} = $level;
254 $line{stdlib}= $stdlib;
255 $line{father} = $father;
256 push(@results,\%line);
265 $id = &delauthority($id);
267 delete an authority and all it's "childs" and "related"
269 C<$id> is the id of the authority
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=?");
279 my ($hierarchy) = $sth->fetchrow;
281 $dbh->do("delete from bibliothesaurus where hierarchy like '$hierarchy|$id|%'");
282 # warn("delete from bibliothesaurus where hierarchy like '$hierarchy|$id|%'");
284 $dbh->do("delete from bibliothesaurus where hierarchy like '$id|%'");
285 # warn("delete from bibliothesaurus where hierarchy like '$id|%'");
287 # warn("delete from bibliothesaurus where id='$id|'");
288 $dbh->do("delete from bibliothesaurus where id='$id|'");
290 END { } # module clean-up code here (global destructor)