road to 1.3.2 :
[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 &searchauthority
52                                         );
53 # FIXME - This is never used
54
55 =item newauthority
56
57   $id = &newauthority($dbh,$hash);
58
59   adds an authority entry in the db.
60   It calculates the level of the authority with the authoritysep and the complete hierarchy.
61
62 C<$dbh> is a DBI::db handle for the Koha database.
63
64 C<$hash> is a hash containing freelib,stdlib,category and father.
65
66 =cut
67 sub newauthority  {
68 }
69
70 =item SearchAuthority
71
72   $id = &SearchAuthority($dbh,$category,$toponly,$branch,$searchstring,$type);
73
74   searches for an authority
75
76 C<$dbh> is a DBI::db handle for the Koha database.
77
78 C<$category> is the category of the authority
79
80 C<$toponly> if set, returns only one level of entries. If unset, returns the main level and the sub entries.
81
82 C<$branch> can contain a branch hierarchy. For example, if C<$branch> contains 1024|2345, SearchAuthority will return only
83 entries beginning by 1024|2345
84
85 C<$searchstring> contains a string. Only entries beginning by C<$searchstring> are returned
86
87
88 =cut
89 sub searchauthority  {
90         my ($env,$category,$toponly,$branch,$searchstring)=@_;
91         my $dbh = C4::Context->dbh;
92         $searchstring=~ s/\'/\\\'/g;
93         my $query="Select distinct stdlib,id,hierarchy,level from bibliothesaurus where (category like \"$category%\")";
94         $query .= " and hierarchy='$branch'" if ($branch && $toponly);
95         $query .= " and hierarchy like \"$branch%\"" if ($branch && !$toponly);
96         $query .= " and hierarchy=''" if (!$branch & $toponly);
97         $query .= " and stdlib like \"$searchstring%\"" if ($searchstring);
98         $query .= " order by category,stdlib";
99         my $sth=$dbh->prepare($query);
100         $sth->execute;
101         my @results;
102         my $cnt=0;
103         my $old_stdlib="";
104         while (my $data=$sth->fetchrow_hashref){
105         if ($old_stdlib ne $data->{'stdlib'}) {
106                 $cnt ++;
107                 push(@results,$data);
108         }
109         $old_stdlib = $data->{'stdlib'};
110         }
111         $sth->finish;
112         return ($cnt,\@results);
113 }
114
115
116 END { }       # module clean-up code here (global destructor)
117
118 1;
119 __END__
120
121 =back
122
123 =head1 SEE ALSO
124
125 =cut