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
55 # FIXME - This is never used
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;
136 =item SearchAuthority
138 $id = &SearchAuthority($dbh,$category,$branch,$searchstring,$type,$offset,$pagesize);
140 searches for an authority
142 C<$dbh> is a DBI::db handle for the Koha database.
144 C<$category> is the category of the authority
146 C<$branch> can contain a branch hierarchy. For example, if C<$branch> contains 1024|2345, SearchAuthority will return only
147 entries beginning by 1024|2345
149 C<$searchstring> contains a string. Only entries beginning by C<$searchstring> are returned
153 sub searchauthority {
154 my ($env,$category,$branch,$searchstring,$offset,$pagesize)=@_;
155 $offset=0 unless ($offset);
156 # warn "==> ($env,$category,$branch,$searchstring,$offset,$pagesize)";
157 my $dbh = C4::Context->dbh;
158 $searchstring=~ s/\'/\\\'/g;
159 my $query="Select stdlib,freelib,father,id,hierarchy,level from bibliothesaurus where (category =\"$category\")";
160 $query .= " and hierarchy='$branch'" if ($branch);
161 $query .= " and match (category,freelib) AGAINST ('$searchstring')" if ($searchstring);
162 # $query .= " and freelib like \"$searchstring%\"" if ($searchstring);
163 $query .= " order by category,freelib limit $offset,".($pagesize*4);
165 my $sth=$dbh->prepare($query);
169 while (my $data=$sth->fetchrow_hashref){
170 push(@results,$data);
173 $query="Select count(*) from bibliothesaurus where (category =\"$category\")";
174 $query .= " and hierarchy='$branch'" if ($branch);
175 $query .= " and stdlib like \"$searchstring%\"" if ($searchstring);
177 $sth=$dbh->prepare($query);
179 my ($cnt) = $sth->fetchrow;
180 $cnt = $pagesize+1 if ($cnt>$pagesize);
181 return ($cnt,\@results);
187 $id = &delauthority($id);
189 delete an authority and all it's "childs" and "related"
191 C<$id> is the id of the authority
196 my $dbh = C4::Context->dbh;
197 # we must delete : - the id, every sons from the id.
198 # to do this, we can : reconstruct the full hierarchy of the id and delete with hierarchy as a key.
199 my $sth=$dbh->prepare("select hierarchy from bibliothesaurus where id=?");
201 my ($hierarchy) = $sth->fetchrow;
203 $dbh->do("delete from bibliothesaurus where hierarchy like '$hierarchy|$id|%'");
204 # warn("delete from bibliothesaurus where hierarchy like '$hierarchy|$id|%'");
206 $dbh->do("delete from bibliothesaurus where hierarchy like '$id|%'");
207 # warn("delete from bibliothesaurus where hierarchy like '$id|%'");
209 # warn("delete from bibliothesaurus where id='$id|'");
210 $dbh->do("delete from bibliothesaurus where id='$id|'");
212 END { } # module clean-up code here (global destructor)