Split off koha-common.
[koha.git] / C4 / Maintainance.pm
1 package C4::Maintainance; #assumes C4/Maintainance
2
3 #package to deal with marking up output
4
5
6 # Copyright 2000-2002 Katipo Communications
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
23 use strict;
24 #use warnings; FIXME - Bug 2505
25 use C4::Context;
26
27 require Exporter;
28
29 use vars qw($VERSION @ISA @EXPORT);
30
31 # set the version for version checking
32 $VERSION = 0.01;
33
34 =head1 NAME
35
36 C4::Maintenance - Koha catalog maintenance functions
37
38 =head1 SYNOPSIS
39
40   use C4::Maintenance;
41
42 =head1 DESCRIPTION
43
44 The functions in this module perform various catalog-maintenance
45 functions, including deleting and undeleting books, fixing
46 miscategorized items, etc.
47
48 =head1 FUNCTIONS
49
50 =over 2
51
52 =cut
53
54 @ISA = qw(Exporter);
55 @EXPORT = qw(&listsubjects &shiftgroup &deletedbib &undeletebib
56 &updatetype &logaction);
57
58 =item listsubjects
59
60   ($count, $results) = &listsubjects($subject, $n, $offset);
61
62 Finds the subjects that begin with C<$subject> in the bibliosubject
63 table of the Koha database.
64
65 C<&listsubjects> returns a two-element array. C<$results> is a
66 reference-to-array, in which each element is a reference-to-hash
67 giving information about the given subject. C<$count> is the number of
68 elements in C<@{$results}>.
69
70 Probably the only interesting field in C<$results->[$i]> is
71 C<subject>, the subject in question.
72
73 C<&listsubject> returns up to C<$n> items, starting at C<$offset>. If
74 C<$n> is 0, it will return all matching subjects.
75
76 =cut
77 #'
78 # FIXME - This API is bogus. The way it's currently used, it should
79 # just return a list of strings.
80 sub listsubjects {
81   my ($sub,$num,$offset)=@_;
82   my $dbh = C4::Context->dbh;
83   my $query="Select * from bibliosubject where subject like ? group by subject";
84   my @bind = ("$sub%");
85   # FIXME - Make $num and $offset optional.
86   # If $num was given, make sure $offset was, too.
87   if ($num != 0){
88     $query.=" limit ?,?";
89     push(@bind,$offset,$num);
90   }
91   my $sth=$dbh->prepare($query);
92 #  print $query;
93   $sth->execute(@bind);
94   my @results;
95   my $i=0;
96   while (my $data=$sth->fetchrow_hashref){
97     $results[$i]=$data;
98     $i++;
99   }
100   $sth->finish;
101   return($i,\@results);
102 }
103
104 =item shiftgroup
105
106   &shiftgroup($biblionumber, $biblioitemnumber);
107
108 Changes the biblionumber associated with a given biblioitem.
109 C<$biblioitemnumber> is the number of the biblioitem to change.
110 C<$biblionumber> is the biblionumber to associate it with.
111
112 =cut
113 #'
114 sub shiftgroup{
115   my ($biblionumber,$bi)=@_;
116   my $dbh = C4::Context->dbh;
117   my $sth=$dbh->prepare("update biblioitems set biblionumber=? where biblioitemnumber=?");
118   $sth->execute($biblionumber,$bi);
119   $sth->finish;
120   $sth=$dbh->prepare("update items set biblionumber=? where biblioitemnumber=?");
121   $sth->execute($biblionumber,$bi);
122   $sth->finish;
123 }
124
125 =item deletedbib
126
127   ($count, $results) = &deletedbib($title);
128
129 Looks up deleted books whose title begins with C<$title>.
130
131 C<&deletedbib> returns a two-element list. C<$results> is a
132 reference-to-array; each element is a reference-to-hash whose keys are
133 the fields of the deletedbiblio table in the Koha database. C<$count>
134 is the number of elements in C<$results>.
135
136 =cut
137 #'
138 sub deletedbib{
139   my ($title)=@_;
140   my $dbh = C4::Context->dbh;
141   my $sth=$dbh->prepare("Select * from deletedbiblio where title like ? order by title");
142   $sth->execute("$title%");
143   my @results;
144   my $i=0;
145   while (my $data=$sth->fetchrow_hashref){
146     $results[$i]=$data;
147     $i++;
148   }
149   $sth->finish;
150   return($i,\@results);
151 }
152
153 =item undeletebib
154
155   &undeletebib($biblionumber);
156
157 Undeletes a book. C<&undeletebib> looks up the book with the given
158 biblionumber in the deletedbiblio table of the Koha database, and
159 moves its entry to the biblio table.
160
161 =cut
162 #'
163 sub undeletebib{
164   my ($biblionumber)=@_;
165   my $dbh = C4::Context->dbh;
166   my $sth=$dbh->prepare("select * from deletedbiblio where biblionumber=?");
167   $sth->execute($biblionumber);
168   if (my @data=$sth->fetchrow_array){
169     $sth->finish;
170     # FIXME - Doesn't this keep the same biblionumber? Isn't this
171     # forbidden by the definition of 'biblio'? Or doesn't it matter?
172     my $query="INSERT INTO biblio VALUES (";
173    my $count = @data;
174     $query .= ("?," x $count);
175     $query=~ s/\,$/\)/;
176     #   print $query;
177     $sth=$dbh->prepare($query);
178     $sth->execute(@data);
179     $sth->finish;
180   }
181   $sth=$dbh->prepare("DELETE FROM deletedbiblio WHERE biblionumber=?");
182   $sth->execute($biblionumber);
183   $sth->finish;
184 }
185
186 =item updatetype
187
188   &updatetype($biblioitemnumber, $itemtype);
189
190 Changes the type of the item with the given biblioitemnumber to be
191 C<$itemtype>.
192
193 =cut
194 #'
195 sub updatetype{
196   my ($bi,$type)=@_;
197   my $dbh = C4::Context->dbh;
198   my $sth=$dbh->prepare("Update biblioitems set itemtype=? where biblioitemnumber=?");
199   $sth->execute($type,$bi);
200   $sth->finish;
201 }
202
203 END { }       # module clean-up code here (global destructor)
204
205 1;
206 __END__
207
208 =back
209
210 =head1 AUTHOR
211
212 Koha Developement team <info@koha.org>
213
214 =cut