Got undeleting biblios going (catalogue maintenance should probably be in a
[koha.git] / C4 / Maintainance.pm
1 package C4::Maintainance; #asummes C4/Maintainance
2
3 #package to deal with marking up output
4
5 use strict;
6 use C4::Database;
7
8 require Exporter;
9
10 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
11
12 # set the version for version checking
13 $VERSION = 0.01;
14
15 @ISA = qw(Exporter);
16 @EXPORT = qw(&listsubjects &updatesub &shiftgroup &deletedbib &undeletebib);
17 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
18
19 # your exported package globals go here,
20 # as well as any optionally exported functions
21
22 @EXPORT_OK   = qw($Var1 %Hashit);
23
24
25 # non-exported package globals go here
26 use vars qw(@more $stuff);
27
28 # initalize package globals, first exported ones
29
30 my $Var1   = '';
31 my %Hashit = ();
32
33
34 # then the others (which are still accessible as $Some::Module::stuff)
35 my $stuff  = '';
36 my @more   = ();
37
38 # all file-scoped lexicals must be created before
39 # the functions below that use them.
40
41 # file-private lexicals go here
42 my $priv_var    = '';
43 my %secret_hash = ();
44
45 # here's a file-private function as a closure,
46 # callable as &$priv_func;  it cannot be prototyped.
47 my $priv_func = sub {
48 # stuff goes here.
49   };
50    
51 # make all your functions, whether exported or not;
52  
53 sub listsubjects {
54   my ($sub,$num,$offset)=@_;
55   my $dbh=C4Connect;
56   my $query="Select * from bibliosubject where subject like '$sub%' group by subject";
57   if ($num != 0){
58     $query.=" limit $offset,$num";
59   }
60   my $sth=$dbh->prepare($query);
61 #  print $query;
62   $sth->execute;
63   my @results;
64   my $i=0;
65   while (my $data=$sth->fetchrow_hashref){
66     $results[$i]=$data;
67     $i++;
68   }
69   $sth->finish;
70   $dbh->disconnect;
71   return($i,\@results);
72 }
73
74 sub updatesub{
75   my ($sub,$oldsub)=@_;
76   my $dbh=C4Connect;
77   my $query="update bibliosubject set subject='$sub' where subject='$oldsub'";
78   my $sth=$dbh->prepare($query);
79   $sth->execute;
80   $sth->finish;
81   $dbh->disconnect;
82 }
83
84 sub shiftgroup{
85   my ($bib,$bi)=@_;
86   my $dbh=C4Connect;
87   my $query="update biblioitems set biblionumber=$bib where biblioitemnumber=$bi";
88   my $sth=$dbh->prepare($query);
89   $sth->execute;
90   $sth->finish;
91   $query="update items set biblionumber=$bib where biblioitemnumber=$bi";
92   $sth=$dbh->prepare($query);
93   $sth->execute;
94   $sth->finish;
95   $dbh->disconnect;
96 }
97
98 sub deletedbib{
99   my ($title)=@_;
100   my $dbh=C4Connect;
101   my $query="Select * from deletedbiblio where title like '$title%' order by title";
102   my $sth=$dbh->prepare($query);
103   $sth->execute;
104   my @results;
105   my $i=0;
106   while (my $data=$sth->fetchrow_hashref){
107     $results[$i]=$data;
108     $i++;
109   }
110   $sth->finish;
111   $dbh->disconnect;
112   return($i,\@results);
113 }
114
115 sub undeletebib{
116   my ($bib)=@_;
117   my $dbh=C4Connect;
118   my $query="select * from deletedbiblio where biblionumber=$bib";
119   my $sth=$dbh->prepare($query);                         
120   $sth->execute;             
121   if (my @data=$sth->fetchrow_array){  
122     $sth->finish;                      
123     $query="Insert into biblio values (";    
124     foreach my $temp (@data){                
125       $temp=~ s/\'/\\\'/g;                      
126       $query=$query."'$temp',";      
127     }                
128     $query=~ s/\,$/\)/;    
129     #   print $query;                    
130     $sth=$dbh->prepare($query);    
131     $sth->execute;          
132     $sth->finish;          
133   }
134   $query="Delete from deletedbiblio where biblionumber=$bib";
135   $sth=$dbh->prepare($query);
136   $sth->execute;
137   $sth->finish;
138   $dbh->disconnect;
139 }
140
141
142 END { }       # module clean-up code here (global destructor)
143