1 package C4::VirtualShelves::Page;
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
21 # perldoc at the end of the file, per convention.
26 use C4::VirtualShelves qw/:DEFAULT RefreshShelvesSummary/;
30 use C4::Auth qw/get_session/;
33 use C4::Dates qw/format_date/;
38 use vars qw($debug @EXPORT @ISA $VERSION);
43 @EXPORT = qw(&shelfpage);
44 $debug = $ENV{DEBUG} || 0;
49 redirect=>'/cgi-bin/koha/virtualshelves/shelves.pl',
52 redirect=>'/cgi-bin/koha/opac-shelves.pl',
56 sub shelfpage ($$$$$) {
57 my ($type, $query, $template, $loggedinuser, $cookie ) = @_;
58 ($pages{$type}) or $type = 'opac';
59 $query or die "No query";
60 $template or die "No template";
61 $template->param( { loggedinuser => $loggedinuser } );
64 my $shelfoff = ($query->param('shelfoff') ? $query->param('shelfoff') : 1);
65 my $itemoff = ($query->param('itemoff') ? $query->param('itemoff') : 1);
66 my $displaymode = ($query->param('display') ? $query->param('display') : 'publicshelves');
67 my ($shelflimit, $shelfoffset, $shelveslimit, $shelvesoffset);
68 # FIXME: These limits should not be hardcoded...
69 $shelflimit = 20; # Limits number of items returned for a given query
70 $shelfoffset = ($itemoff - 1) * 20; # Sets the offset to begin retrieving items at
71 $shelveslimit = 20; # Limits number of shelves returned for a given query (row_count)
72 $shelvesoffset = ($shelfoff - 1) * 20; # Sets the offset to begin retrieving shelves at (offset)
73 # getting the Shelves list
74 my $category = (($displaymode eq 'privateshelves') ? 1 : 2);
75 my ($shelflist, $totshelves) = GetShelves( $category, $shelveslimit, $shelvesoffset, $loggedinuser );
76 #Get a list of private shelves for possible deletion. Only do this when we've defaulted to public shelves
77 my ($privshelflist, $privtotshelves);
79 ($privshelflist, $privtotshelves) = GetShelves( 1, $shelveslimit, $shelvesoffset, $loggedinuser );
81 my $op = $query->param('op');
82 # my $imgdir = getitemtypeimagesrc();
83 # my $itemtypes = GetItemTypes();
85 # the format of this is unindented for ease of diff comparison to the old script
86 # Note: do not mistake the assignment statements below for comparisons!
88 if ( $query->param('modifyshelfcontents') ) {
89 my ($shelfnumber,$barcode,$item,$biblio);
90 if ($shelfnumber = $query->param('viewshelf')) {
91 if (ShelfPossibleAction($loggedinuser, $shelfnumber, 'manage')) {
92 if ($barcode = $query->param('addbarcode')) {
93 if ($item = GetItem( 0, $barcode )) {
94 $biblio = GetBiblioFromItemNumber($item->{'itemnumber'});
95 AddToShelf($biblio->{'biblionumber'}, $shelfnumber) or
96 push @paramsloop, {duplicatebiblio=>$barcode};
97 } else { push @paramsloop, {failgetitem=>$barcode}; }
99 (grep {/REM-(\d+)/} $query->param) or push @paramsloop, {nobarcode=>1};
100 foreach ($query->param) {
103 "SHELVES: user $loggedinuser removing item $1 from shelf $shelfnumber";
104 DelFromShelf($1, $shelfnumber); # $1 is biblionumber
107 } else { push @paramsloop, {nopermission=>$shelfnumber}; }
108 } else { push @paramsloop, {noshelfnumber=>1}; }
112 # set the default tab, etc. (for OPAC)
113 my $shelf_type = ($query->param('display') ? $query->param('display') : 'publicshelves');
114 if (defined $shelf_type) {
115 if ($shelf_type eq 'privateshelves') {
116 $template->param(showprivateshelves => 1);
117 } elsif ($shelf_type eq 'publicshelves') {
118 $template->param(showpublicshelves => 1);
121 $debug and warn "Invalid 'display' param ($shelf_type)";
123 } elsif ($loggedinuser == -1) {
124 $template->param(showpublicshelves => 1);
126 $template->param(showprivateshelves => 1);
129 my($okmanage, $okview);
130 my $shelfnumber = $query->param('shelfnumber') || $query->param('viewshelf');
132 $okmanage = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
133 $okview = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' );
141 push @paramsloop, {nopermission=>$shelfnumber};
144 if ( $op eq 'modifsave' ) {
146 'shelfname' => $query->param('shelfname'),
147 'category' => $query->param('category'),
148 'sortfield' => $query->param('sortfield'),
151 ModShelf( $shelfnumber, $shelf );
153 } elsif ( $op eq 'modif' ) {
154 my ( $shelfnumber2, $shelfname, $owner, $category, $sortfield ) =GetShelf( $shelfnumber );
155 my $member = GetMember('borrowernumber'=>$owner);
156 my $ownername = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
159 shelfnumber => $shelfnumber2,
160 shelfname => $shelfname,
162 ownername => $ownername,
163 "category$category" => 1,
164 category => $category,
165 "sort_$sortfield" => 1,
170 if ($shelfnumber = $query->param('viewshelf') ) {
171 #check that the user can view the shelf
172 if ( ShelfPossibleAction( $loggedinuser, $shelfnumber, 'view' ) ) {
176 my $sortfield = ($query->param('sortfield') ? $query->param('sortfield') : 'title');
177 if ($sortfield eq 'author') {
178 $authorsort = 'author';
180 if ($sortfield eq 'year'){
183 ($items, $totitems) = GetShelfContents($shelfnumber, $shelflimit, $shelfoffset);
184 for my $this_item (@$items) {
185 my $record = GetMarcBiblio($this_item->{'biblionumber'});
186 # the virtualshelfcontents table does not store these columns nor are they retrieved from the items
187 # and itemtypes tables, so I'm commenting them out for now to quiet the log -crn
188 #$this_item->{imageurl} = $imgdir."/".$itemtypes->{ $this_item->{itemtype} }->{'imageurl'};
189 #$this_item->{'description'} = $itemtypes->{ $this_item->{itemtype} }->{'description'};
190 $this_item->{'dateadded'} = format_date($this_item->{'dateadded'});
191 $this_item->{'imageurl'} = getitemtypeinfo($this_item->{'itemtype'})->{'imageurl'};
192 $this_item->{'coins'} = GetCOinSBiblio($this_item->{'biblionumber'});
193 $this_item->{'subtitle'} = C4::Biblio::get_koha_field_from_marc('bibliosubtitle', 'subtitle', $record, '');
195 # Getting items infos for location display
196 my @items_infos = &GetItemsInfo($this_item->{'biblionumber'}, $type);
197 $this_item->{'ITEM_RESULTS'} = \@items_infos;
200 push @paramsloop, {display => 'privateshelves'} if $category == 1;
203 my $manageshelf = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
205 shelfname => $shelflist->{$shelfnumber}->{'shelfname'} || $privshelflist->{$shelfnumber}->{'shelfname'},
206 shelfnumber => $shelfnumber,
207 viewshelf => $shelfnumber,
208 authorsort => $authorsort,
209 yearsort => $yearsort,
210 manageshelf => $manageshelf,
213 } else { push @paramsloop, {nopermission=>$shelfnumber} };
216 if ( $query->param('shelves') ) {
218 if (my $newshelf = $query->param('addshelf')) {
219 # note: a user can always add a new shelf
220 my $shelfnumber = AddShelf(
222 $query->param('owner'),
223 $query->param('category'),
224 $query->param('sortfield')
227 if ( $shelfnumber == -1 ) { #shelf already exists.
229 push @paramsloop, { already => $newshelf };
230 $template->param(shelfnumber => $shelfnumber);
232 print $query->redirect($pages{$type}->{redirect} . "?viewshelf=$shelfnumber");
236 foreach ($query->param()) {
240 unless (defined $shelflist->{$number} || defined $privshelflist->{$number}) {
241 push(@paramsloop, {unrecognized=>$number}); last;
243 unless (ShelfPossibleAction($loggedinuser, $number, 'manage')) {
244 push(@paramsloop, {nopermission=>$shelfnumber}); last;
247 ($contents, $totshelves) = GetShelfContents($number, $shelveslimit, $shelvesoffset);
248 if (my $count = scalar @$contents){
249 unless (scalar grep {/^CONFIRM-$number$/} $query->param()) {
250 if (defined $shelflist->{$number}) {
251 push(@paramsloop, {need_confirm=>$shelflist->{$number}->{shelfname}, count=>$count});
252 $shelflist->{$number}->{confirm} = $number;
254 push(@paramsloop, {need_confirm=>$privshelflist->{$number}->{shelfname}, count=>$count});
255 $privshelflist->{$number}->{confirm} = $number;
262 if (defined $shelflist->{$number}) {
263 $name = $shelflist->{$number}->{'shelfname'};
264 delete $shelflist->{$number};
266 $name = $privshelflist->{$number}->{'shelfname'};
267 delete $privshelflist->{$number};
269 unless (DelShelf($number)) {
270 push(@paramsloop, {delete_fail=>$name}); last;
272 push(@paramsloop, {delete_ok=>$name});
273 # print $query->redirect($pages{$type}->{redirect}); exit;
277 $stay and $template->param(shelves => 1);
282 (@paramsloop) and $template->param(paramsloop => \@paramsloop);
283 $showadd and $template->param(showadd => 1);
286 my $numberCanManage = 0;
288 # rebuild shelflist in case a shelf has been added
289 ($shelflist, $totshelves) = GetShelves( $category, $shelveslimit, $shelvesoffset, $loggedinuser ) unless $delflag;
290 foreach my $element (sort { lc($shelflist->{$a}->{'shelfname'}) cmp lc($shelflist->{$b}->{'shelfname'}) } keys %$shelflist) {
292 $shelflist->{$element}->{shelf} = $element;
293 my $category = $shelflist->{$element}->{'category'};
294 my $owner = $shelflist->{$element}->{ 'owner' };
295 my $canmanage = ShelfPossibleAction( $loggedinuser, $element, 'manage' );
296 $shelflist->{$element}->{"viewcategory$category"} = 1;
297 $shelflist->{$element}->{manageshelf} = $canmanage;
298 if ($owner eq $loggedinuser or $canmanage) {
299 $shelflist->{$element}->{'mine'} = 1;
301 my $member = GetMember('borrowernumber' => $owner);
302 $shelflist->{$element}->{ownername} = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
303 $numberCanManage++ if $canmanage; # possibly outmoded
304 if ($shelflist->{$element}->{'category'} eq '1') {
305 push (@shelveslooppriv, $shelflist->{$element});
307 push (@shelvesloop, $shelflist->{$element});
311 my $url = $type eq 'opac' ? "/cgi-bin/koha/opac-shelves.pl" : "/cgi-bin/koha/virtualshelves/shelves.pl";
313 foreach (qw(display viewshelf sortfield)) {
314 $qhash{$_} = $query->param($_) if $query->param($_);
316 (scalar keys %qhash) and $url .= '?' . join '&', map {"$_=$qhash{$_}"} keys %qhash;
317 if ($query->param('viewshelf')) {
318 $template->param( {pagination_bar => pagination_bar($url, (int($totitems/$shelflimit)) + (($totitems % $shelflimit) > 0 ? 1 : 0), $itemoff, "itemoff")} );
320 $template->param( {pagination_bar => pagination_bar($url, (int($totshelves/$shelveslimit)) + (($totshelves % $shelveslimit) > 0 ? 1 : 0), $shelfoff, "shelfoff")} );
323 shelveslooppriv => \@shelveslooppriv,
324 shelvesloop => \@shelvesloop,
325 shelvesloopall => [(@shelvesloop, @shelveslooppriv)],
326 numberCanManage => $numberCanManage,
327 "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
328 csv_profiles => GetCsvProfilesLoop()
330 if ($template->param('viewshelf') or
331 $template->param( 'shelves' ) or
332 $template->param( 'edit' ) ) {
333 $template->param(vseflag => 1);
335 if ($template->param( 'shelves' ) or # note: this part looks duplicative, but is intentional
336 $template->param( 'edit' ) ) {
337 $template->param( seflag => 1);
340 #FIXME: This refresh really only needs to happen when there is a modification of some sort
341 # to the shelves, but the above code is so convoluted in its handling of the various
342 # options, it is easier to do this refresh every time C4::VirtualShelves::Page.pm is
345 my ($total, $pubshelves, $barshelves) = RefreshShelvesSummary($query->cookie("CGISESSID"),$loggedinuser,($loggedinuser == -1 ? 20 : 10));
347 if (defined $barshelves) {
348 $template->param( barshelves => scalar (@{$barshelves->[0]}),
349 barshelvesloop => $barshelves->[0],
351 $template->param( bartotal => $total->{'bartotal'}, ) if ($total->{'bartotal'} > scalar (@{$barshelves->[0]}));
354 if (defined $pubshelves) {
355 $template->param( pubshelves => scalar (@{$pubshelves->[0]}),
356 pubshelvesloop => $pubshelves->[0],
358 $template->param( pubtotal => $total->{'pubtotal'}, ) if ($total->{'pubtotal'} > scalar (@{$pubshelves->[0]}));
361 output_html_with_http_headers $query, $cookie, $template->output;
369 VirtualShelves/Page.pm
373 Module used for both OPAC and intranet pages.
375 =head1 CGI PARAMETERS
379 =item C<modifyshelfcontents>
381 If this script has to modify the shelf content.
385 To know on which shelf to work.
392 * modif: show the template allowing modification of the shelves;
393 * modifsave: save changes from modif mode.
397 Load template with 'viewshelves param' displaying the shelf's information.
401 If the param shelves == 1, then add or delete a shelf.
405 If the param shelves == 1, then addshelf is the name of the shelf to add.