see mail on koha-devel : code cleaning on Search.pm + normalizing API + use of biblio...
[koha.git] / opac / opac-shelves.pl
1 #!/usr/bin/perl
2 #script to provide bookshelf management
3 # WARNING: This file uses 4-character tabs!
4 #
5 # $Header$
6 #
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 use strict;
25 use C4::Search;
26 use CGI;
27 use C4::Output;
28 use C4::BookShelves;
29 use C4::Circulation::Circ2;
30 use C4::Auth;
31 use C4::Interface::CGI::Output;
32 use HTML::Template;
33
34 my $env;
35 my $query = new CGI;
36
37 my ($template, $loggedinuser, $cookie)
38     = get_template_and_user({template_name => "opac-shelves.tmpl",
39                                                         query => $query,
40                                                         type => "opac",
41                                                         authnotrequired => 1,
42                                                 });
43
44 if ($query->param('modifyshelfcontents')) {
45         my $shelfnumber=$query->param('shelfnumber');
46         my $barcode=$query->param('addbarcode');
47         my ($item) = getiteminformation($env, 0, $barcode);
48         if (ShelfPossibleAction($loggedinuser,$shelfnumber,'manage')) {
49                 AddToShelf($env, $item->{'itemnumber'}, $shelfnumber);
50                 foreach ($query->param) {
51                         if (/REM-(\d*)/) {
52                                 my $itemnumber=$1;
53                                 RemoveFromShelf($env, $itemnumber, $shelfnumber);
54                         }
55                 }
56         }
57 }
58 my ($shelflist) = GetShelfList($loggedinuser,2);
59
60 $template->param({      loggedinuser => $loggedinuser,
61                                         suggestion => C4::Context->preference("suggestion"),
62                                         virtualshelves => C4::Context->preference("virtualshelves"),
63                                 });
64 SWITCH: {
65         if ($query->param('op') eq 'modifsave') {
66                 ModifShelf($query->param('shelfnumber'),$query->param('shelfname'),$loggedinuser,$query->param('category'));
67                 last SWITCH;
68         }
69         if ($query->param('op') eq 'modif') {
70                 my ($shelfnumber,$shelfname,$owner,$category) = GetShelf($query->param('shelf'));
71                 $template->param(edit => 1,
72                                                 shelfnumber => $shelfnumber,
73                                                 shelfname => $shelfname,
74                                                 "category$category" => 1);
75 #               editshelf($query->param('shelf'));
76                 last SWITCH;
77         }
78         if ($query->param('viewshelf')) {
79                 viewshelf($query->param('viewshelf'));
80                 last SWITCH;
81         }
82         if ($query->param('shelves')) {
83                 shelves();
84                 last SWITCH;
85         }
86 }
87
88 ($shelflist) = GetShelfList($loggedinuser,2); # rebuild shelflist in case a shelf has been added
89
90 my $color='';
91 my @shelvesloop;
92 foreach my $element (sort keys %$shelflist) {
93                 my %line;
94                 ($color eq 0) ? ($color=1) : ($color=0);
95                 $line{'color'}= $color;
96                 $line{'shelf'}=$element;
97                 $line{'shelfname'}=$shelflist->{$element}->{'shelfname'};
98                 $line{"category".$shelflist->{$element}->{'category'}} = 1;
99                 $line{'mine'} = 1 if $shelflist->{$element}->{'owner'} eq $loggedinuser;
100                 $line{'shelfbookcount'}=$shelflist->{$element}->{'count'};
101                 $line{'canmanage'} = ShelfPossibleAction($loggedinuser,$element,'manage');
102                 $line{'firstname'}=$shelflist->{$element}->{'firstname'} unless $shelflist->{$element}->{'owner'} eq $loggedinuser;
103                 $line{'surname'}=$shelflist->{$element}->{'surname'} unless $shelflist->{$element}->{'owner'} eq $loggedinuser;
104 ;
105                 push (@shelvesloop, \%line);
106 }
107 $template->param(shelvesloop => \@shelvesloop);
108
109 output_html_with_http_headers $query, $cookie, $template->output;
110
111 # sub editshelf {
112 #       my ($shelfnumber) = @_;
113 #       my ($shelfnumber,$shelfname,$owner,$category) = GetShelf($shelfnumber);
114 #       $template->param(edit => 1,
115 #                                       shelfnumber => $shelfnumber,
116 #                                       shelfname => $shelfname,
117 #                                       "category$category" => 1);
118 # }
119 sub shelves {
120         if (my $newshelf=$query->param('addshelf')) {
121                 my ($status, $string) = AddShelf($env,$newshelf,$query->param('owner'),$query->param('category'));
122                 if ($status) {
123                         $template->param(status1 => $status, string1 => $string);
124                 }
125         }
126         my @paramsloop;
127         foreach ($query->param()) {
128                 my %line;
129                 if (/DEL-(\d+)/) {
130                         my $delshelf=$1;
131                         my ($status, $string) = RemoveShelf($env,$delshelf);
132                         if ($status) {
133                                 $line{'status'}=$status;
134                                 $line{'string'} = $string;
135                         }
136                 }
137                 #if the shelf is not deleted, %line points on null
138                 push(@paramsloop,\%line);
139         }
140         $template->param(paramsloop => \@paramsloop);
141         my ($shelflist) = GetShelfList($loggedinuser,2);
142         my $color='';
143         my @shelvesloop;
144         foreach my $element (sort keys %$shelflist) {
145                 my %line;
146                 ($color eq 0) ? ($color=1) : ($color=0);
147                 $line{'color'}=$color;
148                 $line{'shelf'}=$element;
149                 $line{'shelfname'}=$shelflist->{$element}->{'shelfname'} ;
150                 $line{'shelfbookcount'}=$shelflist->{$element}->{'count'} ;
151                 push(@shelvesloop, \%line);
152         }
153         $template->param(shelvesloop=>\@shelvesloop,
154                                                         shelves => 1,
155                                                 );
156 }
157
158 sub viewshelf {
159         my $shelfnumber=shift;
160         #check that the user can view the shelf
161         return unless (ShelfPossibleAction($loggedinuser,$shelfnumber,'view'));
162         my ($itemlist) = GetShelfContents($env, $shelfnumber);
163         my $item='';
164         my $color='';
165         my @itemsloop;
166         foreach $item (sort {$a->{'barcode'} cmp $b->{'barcode'}} @$itemlist) {
167                 my %line;
168                 ($color eq 0) ? ($color=1) : ($color=0);
169                 $line{'color'}=$color;
170                 $line{'itemnumber'}=$item->{'itemnumber'};
171                 $line{'barcode'}=$item->{'barcode'};
172                 $line{'title'}=$item->{'title'};
173                 $line{'author'}=$item->{'author'};
174                 $line{'classification'}=$item->{'classification'};              
175                 $line{'itemtype'}=$item->{'itemtype'};          
176                 $line{biblionumber} = $item->{biblionumber};
177                 push(@itemsloop, \%line);
178         }
179         $template->param(       itemsloop => \@itemsloop,
180                                                 shelfname => $shelflist->{$shelfnumber}->{'shelfname'},
181                                                 shelfnumber => $shelfnumber,
182                                                 viewshelf => $query->param('viewshelf'),
183                                                 manageshelf => &ShelfPossibleAction($loggedinuser,$shelfnumber,'manage'),
184                                         );
185 }
186
187 #
188 # $Log$
189 # Revision 1.7  2005/05/04 09:02:38  tipaul
190 # synch'ing 2.2 and head
191 #
192 # Revision 1.3.2.4  2005/03/25 17:04:28  tipaul
193 # adding virtual shelves & suggestions button to the top
194 #
195 # Revision 1.3.2.3  2005/01/27 17:18:28  oleonard
196 # Taking table cell background color information out of the script and moving it into the template (requires update to opac-shelves.tmpl)
197 #
198 # Revision 1.3.2.2  2005/01/11 20:18:29  oleonard
199 # Adding call number and item type to list of returned variables
200 #
201 # Revision 1.3.2.1  2005/01/11 16:33:57  tipaul
202 # fix for http://bugs.koha.org/cgi-bin/bugzilla/show_bug.cgi?id=811 :
203 # The OPAC requires uses to log in to view virtual shelves, and it requires a user
204 # with librarian privileges.  Virtual shelves should be viewable by all users,
205 # logged in or not, and editable by all logged-in users in good standing.
206 #
207 # Revision 1.3  2005/01/03 11:09:34  tipaul
208 # synch'ing virtual shelves management in opac with the librarian one, that has more features
209 #
210 # Revision 1.5  2004/12/16 11:30:57  tipaul
211 # adding bookshelf features :
212 # * create bookshelf on the fly
213 # * modify a bookshelf name & status
214 #
215 # Revision 1.4  2004/12/15 17:28:23  tipaul
216 # adding bookshelf features :
217 # * create bookshelf on the fly
218 # * modify a bookshelf (this being not finished, will commit the rest soon)
219 #
220 # Revision 1.3  2004/12/02 16:38:50  tipaul
221 # improvement in book shelves
222 #
223 # Revision 1.2  2004/11/19 16:31:30  tipaul
224 # bugfix for bookshelves not in official CVS
225 #
226 # Revision 1.1.2.1  2004/03/10 15:08:18  tipaul
227 # modifying shelves : introducing category of shelf : private, public, free for all
228 #
229 # Revision 1.13  2004/02/11 08:35:31  tipaul
230 # synch'ing 2.0.0 branch and head
231 #
232 # Revision 1.12.2.1  2004/02/06 14:22:19  tipaul
233 # fixing bugs in bookshelves management.
234 #
235 # Revision 1.12  2003/02/05 10:04:14  acli
236 # Worked around weirdness with HTML::Template; without the {}, it complains
237 # of being passed an odd number of arguments even though we are not
238 #
239 # Revision 1.11  2003/02/05 09:23:03  acli
240 # Fixed a few minor errors to make it run
241 # Noted correct tab size
242 #
243 # Revision 1.10  2003/02/02 07:18:37  acli
244 # Moved C4/Charset.pm to C4/Interface/CGI/Output.pm
245 #
246 # Create output_html_with_http_headers function to contain the "print $query
247 # ->header(-type => guesstype...),..." call. This is in preparation for
248 # non-HTML output (e.g., text/xml) and charset conversion before output in
249 # the future.
250 #
251 # Created C4/Interface/CGI/Template.pm to hold convenience functions specific
252 # to the CGI interface using HTML::Template
253 #
254 # Modified moremembers.pl to make the "sex" field localizable for languages
255 # where M and F doesn't make sense
256 #
257 # Revision 1.9  2002/12/19 18:55:40  hdl
258 # Templating reservereport et shelves.
259 #
260 # Revision 1.9  2002/08/14 18:12:51  hdl
261 # Templating files
262 #
263 # Revision 1.8  2002/08/14 18:12:51  tonnesen
264 # Added copyright statement to all .pl and .pm files
265 #
266 # Revision 1.7  2002/07/05 05:03:37  tonnesen
267 # Minor changes to authentication routines.
268 #
269 # Revision 1.5  2002/07/04 19:42:48  tonnesen
270 # Minor changes
271 #
272 # Revision 1.4  2002/07/04 19:21:29  tonnesen
273 # Beginning of authentication api.  Applied to shelves.pl for now as a test case.
274 #
275 # Revision 1.2.2.1  2002/06/26 20:28:15  tonnesen
276 # Some udpates that I made here locally a while ago.  Still won't be useful, but
277 # should be functional
278 #
279 #
280 #
281
282
283
284
285 # Local Variables:
286 # tab-width: 4
287 # End: