Added a FIXME comment.
[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 with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA  02111-1307 USA
22
23 use strict;
24 use C4::Context;
25
26 require Exporter;
27
28 use vars qw($VERSION @ISA @EXPORT);
29
30 # set the version for version checking
31 $VERSION = 0.01;
32
33 =head1 NAME
34
35 C4::Maintenance - Koha catalog maintenance functions
36
37 =head1 SYNOPSIS
38
39   use C4::Maintenance;
40
41 =head1 DESCRIPTION
42
43 The functions in this module perform various catalog-maintenance
44 functions, including deleting and undeleting books, fixing
45 miscategorized items, etc.
46
47 =head1 FUNCTIONS
48
49 =over 2
50
51 =cut
52
53 @ISA = qw(Exporter);
54 @EXPORT = qw(&listsubjects &updatesub &shiftgroup &deletedbib &undeletebib
55 &updatetype);
56
57 =item listsubjects
58
59   ($count, $results) = &listsubjects($subject, $n, $offset);
60
61 Finds the subjects that begin with C<$subject> in the bibliosubject
62 table of the Koha database.
63
64 C<&listsubjects> returns a two-element array. C<$results> is a
65 reference-to-array, in which each element is a reference-to-hash
66 giving information about the given subject. C<$count> is the number of
67 elements in C<@{$results}>.
68
69 Probably the only interesting field in C<$results->[$i]> is
70 C<subject>, the subject in question.
71
72 C<&listsubject> returns up to C<$n> items, starting at C<$offset>. If
73 C<$n> is 0, it will return all matching subjects.
74
75 =cut
76 #'
77 # FIXME - This API is bogus. The way it's currently used, it should
78 # just return a list of strings.
79 sub listsubjects {
80   my ($sub,$num,$offset)=@_;
81   my $dbh = C4::Context->dbh;
82   my $query="Select * from bibliosubject where subject like '$sub%' group by subject";
83   # FIXME - Make $num and $offset optional.
84   # If $num was given, make sure $offset was, too.
85   if ($num != 0){
86     $query.=" limit $offset,$num";
87   }
88   my $sth=$dbh->prepare($query);
89 #  print $query;
90   $sth->execute;
91   my @results;
92   my $i=0;
93   while (my $data=$sth->fetchrow_hashref){
94     $results[$i]=$data;
95     $i++;
96   }
97   $sth->finish;
98   return($i,\@results);
99 }
100
101 =item updatesub
102
103   &updatesub($newsubject, $oldsubject);
104
105 Renames a subject from C<$oldsubject> to C<$newsubject> in the
106 bibliosubject table of the Koha database.
107
108 =cut
109 #'
110 sub updatesub{
111   my ($sub,$oldsub)=@_;
112   my $dbh = C4::Context->dbh;
113   $sub=$dbh->quote($sub);
114   $oldsub=$dbh->quote($oldsub);
115   # FIXME - Just use $dbh->do();
116   my $query="update bibliosubject set subject=$sub where subject=$oldsub";
117   my $sth=$dbh->prepare($query);
118   $sth->execute;
119   $sth->finish;
120 }
121
122 =item shiftgroup
123
124   &shiftgroup($biblionumber, $biblioitemnumber);
125
126 Changes the biblionumber associated with a given biblioitem.
127 C<$biblioitemnumber> is the number of the biblioitem to change.
128 C<$biblionumber> is the biblionumber to associate it with.
129
130 =cut
131 #'
132 sub shiftgroup{
133   my ($bib,$bi)=@_;
134   my $dbh = C4::Context->dbh;
135   # FIXME - Just use $dbh->do();
136   my $query="update biblioitems set biblionumber=$bib where biblioitemnumber=$bi";
137   my $sth=$dbh->prepare($query);
138   $sth->execute;
139   $sth->finish;
140   # FIXME - Just use $dbh->do();
141   $query="update items set biblionumber=$bib where biblioitemnumber=$bi";
142   $sth=$dbh->prepare($query);
143   $sth->execute;
144   $sth->finish;
145 }
146
147 =item deletedbib
148
149   ($count, $results) = &deletedbib($title);
150
151 Looks up deleted books whose title begins with C<$title>.
152
153 C<&deletedbib> returns a two-element list. C<$results> is a
154 reference-to-array; each element is a reference-to-hash whose keys are
155 the fields of the deletedbiblio table in the Koha database. C<$count>
156 is the number of elements in C<$results>.
157
158 =cut
159 #'
160 sub deletedbib{
161   my ($title)=@_;
162   my $dbh = C4::Context->dbh;
163   my $query="Select * from deletedbiblio where title like '$title%' order by title";
164   my $sth=$dbh->prepare($query);
165   $sth->execute;
166   my @results;
167   my $i=0;
168   while (my $data=$sth->fetchrow_hashref){
169     $results[$i]=$data;
170     $i++;
171   }
172   $sth->finish;
173   return($i,\@results);
174 }
175
176 =item undeletebib
177
178   &undeletebib($biblionumber);
179
180 Undeletes a book. C<&undeletebib> looks up the book with the given
181 biblionumber in the deletedbiblio table of the Koha database, and
182 moves its entry to the biblio table.
183
184 =cut
185 #'
186 sub undeletebib{
187   my ($bib)=@_;
188   my $dbh = C4::Context->dbh;
189   my $query="select * from deletedbiblio where biblionumber=$bib";
190   my $sth=$dbh->prepare($query);
191   $sth->execute;
192   if (my @data=$sth->fetchrow_array){
193     $sth->finish;
194     # FIXME - Doesn't this keep the same biblionumber? Isn't this
195     # forbidden by the definition of 'biblio'? Or doesn't it matter?
196     $query="Insert into biblio values (";
197     foreach my $temp (@data){
198       $temp=~ s/\'/\\\'/g;
199       $query=$query."'$temp',";
200     }
201     $query=~ s/\,$/\)/;
202     #   print $query;
203     $sth=$dbh->prepare($query);
204     $sth->execute;
205     $sth->finish;
206   }
207   $query="Delete from deletedbiblio where biblionumber=$bib";
208   $sth=$dbh->prepare($query);
209   $sth->execute;
210   $sth->finish;
211 }
212
213 =item updatetype
214
215   &updatetype($biblioitemnumber, $itemtype);
216
217 Changes the type of the item with the given biblioitemnumber to be
218 C<$itemtype>.
219
220 =cut
221 #'
222 sub updatetype{
223   my ($bi,$type)=@_;
224   my $dbh = C4::Context->dbh;
225   # FIXME - Use $dbh->do(...);
226   my $sth=$dbh->prepare("Update biblioitems set itemtype='$type' where biblioitemnumber=$bi");
227   $sth->execute;
228   $sth->finish;
229 }
230
231 END { }       # module clean-up code here (global destructor)
232
233 1;
234 __END__
235
236 =back
237
238 =head1 AUTHOR
239
240 Koha Developement team <info@koha.org>
241
242 =head1 SEE ALSO
243
244 L<perl>.
245
246 =cut