Added PODs.
[koha.git] / C4 / BookShelves.pm
1 package C4::BookShelves; #asummes C4/BookShelves
2
3 #
4 # $Header$
5 #
6 #requires DBI.pm to be installed
7
8
9 # Copyright 2000-2002 Katipo Communications
10 #
11 # This file is part of Koha.
12 #
13 # Koha is free software; you can redistribute it and/or modify it under the
14 # terms of the GNU General Public License as published by the Free Software
15 # Foundation; either version 2 of the License, or (at your option) any later
16 # version.
17 #
18 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
19 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License along with
23 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
24 # Suite 330, Boston, MA  02111-1307 USA
25
26 use strict;
27 require Exporter;
28 use DBI;
29 use C4::Database;
30 use C4::Circulation::Circ2;
31 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
32   
33 # set the version for version checking
34 $VERSION = 0.01;
35     
36 @ISA = qw(Exporter);
37 @EXPORT = qw(&GetShelfList &GetShelfContents &AddToShelf &RemoveFromShelf &AddShelf &RemoveShelf);
38 %EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
39                   
40 # your exported package globals go here,
41 # as well as any optionally exported functions
42
43 @EXPORT_OK   = qw($Var1 %Hashit);
44
45
46 # non-exported package globals go here
47 use vars qw(@more $stuff);
48         
49 # initalize package globals, first exported ones
50
51 my $Var1   = '';
52 my %Hashit = ();
53                     
54 # then the others (which are still accessible as $Some::Module::stuff)
55 my $stuff  = '';
56 my @more   = ();
57         
58 # all file-scoped lexicals must be created before
59 # the functions below that use them.
60                 
61 # file-private lexicals go here
62 my $priv_var    = '';
63 my %secret_hash = ();
64                             
65 # here's a file-private function as a closure,
66 # callable as &$priv_func;  it cannot be prototyped.
67 my $priv_func = sub {
68   # stuff goes here.
69 };
70                                                     
71 # make all your functions, whether exported or not;
72
73 my $dbh=C4Connect();
74
75 sub GetShelfList {
76     my $sth=$dbh->prepare("select shelfnumber,shelfname from bookshelf");
77     $sth->execute;
78     my %shelflist;
79     while (my ($shelfnumber, $shelfname) = $sth->fetchrow) {
80         my $sti=$dbh->prepare("select count(*) from shelfcontents where shelfnumber=$shelfnumber");
81         $sti->execute;
82         my ($count) = $sti->fetchrow;
83         $shelflist{$shelfnumber}->{'shelfname'}=$shelfname;
84         $shelflist{$shelfnumber}->{'count'}=$count;
85     }
86     return(\%shelflist);
87 }
88
89
90 sub GetShelfContents {
91     my ($env, $shelfnumber) = @_;
92     my @itemlist;
93     my $sth=$dbh->prepare("select itemnumber from shelfcontents where shelfnumber=$shelfnumber order by itemnumber");
94     $sth->execute;
95     while (my ($itemnumber) = $sth->fetchrow) {
96         my ($item) = getiteminformation($env, $itemnumber, 0);
97         push (@itemlist, $item);
98     }
99     return (\@itemlist);
100 }
101
102 sub AddToShelf {
103     my ($env, $itemnumber, $shelfnumber) = @_;
104     my $sth=$dbh->prepare("select * from shelfcontents where shelfnumber=$shelfnumber and itemnumber=$itemnumber");
105     $sth->execute;
106     if ($sth->rows) {
107 # already on shelf
108     } else {
109         $sth=$dbh->prepare("insert into shelfcontents (shelfnumber, itemnumber, flags) values ($shelfnumber, $itemnumber, 0)");
110         $sth->execute;
111     }
112 }
113
114 sub RemoveFromShelf {
115     my ($env, $itemnumber, $shelfnumber) = @_;
116     my $sth=$dbh->prepare("delete from shelfcontents where shelfnumber=$shelfnumber and itemnumber=$itemnumber");
117     $sth->execute;
118 }
119
120 sub AddShelf {
121     my ($env, $shelfname) = @_;
122     my $q_shelfname=$dbh->quote($shelfname);
123     my $sth=$dbh->prepare("select * from bookshelf where shelfname=$q_shelfname");
124     $sth->execute;
125     if ($sth->rows) {
126         return(1, "Shelf \"$shelfname\" already exists");
127     } else {
128         $sth=$dbh->prepare("insert into bookshelf (shelfname) values ($q_shelfname)");
129         $sth->execute;
130         return (0, "Done");
131     }
132 }
133
134 sub RemoveShelf {
135     my ($env, $shelfnumber) = @_;
136     my $sth=$dbh->prepare("select count(*) from shelfcontents where shelfnumber=$shelfnumber");
137     $sth->execute;
138     my ($count)=$sth->fetchrow;
139     if ($count) {
140         return (1, "Shelf has $count items on it.  Please remove all items before deleting this shelf.");
141     } else {
142         $sth=$dbh->prepare("delete from bookshelf where shelfnumber=$shelfnumber");
143         $sth->execute;
144         return (0, "Done");
145     }
146 }
147
148                         
149 END { }       # module clean-up code here (global destructor)
150
151
152 #
153 # $Log$
154 # Revision 1.4  2002/08/14 18:12:51  tonnesen
155 # Added copyright statement to all .pl and .pm files
156 #
157 # Revision 1.3  2002/07/02 17:48:06  tonnesen
158 # Merged in updates from rel-1-2
159 #
160 # Revision 1.2.2.1  2002/06/26 20:46:48  tonnesen
161 # Inserting some changes I made locally a while ago.
162 #
163 #