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 push @paramsloop, {display => 'privateshelves'} if $category == 1;
198 my $manageshelf = ShelfPossibleAction( $loggedinuser, $shelfnumber, 'manage' );
200 shelfname => $shelflist->{$shelfnumber}->{'shelfname'} || $privshelflist->{$shelfnumber}->{'shelfname'},
201 shelfnumber => $shelfnumber,
202 viewshelf => $shelfnumber,
203 authorsort => $authorsort,
204 yearsort => $yearsort,
205 manageshelf => $manageshelf,
208 } else { push @paramsloop, {nopermission=>$shelfnumber} };
211 if ( $query->param('shelves') ) {
213 if (my $newshelf = $query->param('addshelf')) {
214 # note: a user can always add a new shelf
215 my $shelfnumber = AddShelf(
217 $query->param('owner'),
218 $query->param('category'),
219 $query->param('sortfield')
222 if ( $shelfnumber == -1 ) { #shelf already exists.
224 push @paramsloop, { already => $newshelf };
225 $template->param(shelfnumber => $shelfnumber);
227 print $query->redirect($pages{$type}->{redirect} . "?viewshelf=$shelfnumber");
231 foreach ($query->param()) {
235 unless (defined $shelflist->{$number} || defined $privshelflist->{$number}) {
236 push(@paramsloop, {unrecognized=>$number}); last;
238 unless (ShelfPossibleAction($loggedinuser, $number, 'manage')) {
239 push(@paramsloop, {nopermission=>$shelfnumber}); last;
242 ($contents, $totshelves) = GetShelfContents($number, $shelveslimit, $shelvesoffset);
243 if (my $count = scalar @$contents){
244 unless (scalar grep {/^CONFIRM-$number$/} $query->param()) {
245 if (defined $shelflist->{$number}) {
246 push(@paramsloop, {need_confirm=>$shelflist->{$number}->{shelfname}, count=>$count});
247 $shelflist->{$number}->{confirm} = $number;
249 push(@paramsloop, {need_confirm=>$privshelflist->{$number}->{shelfname}, count=>$count});
250 $privshelflist->{$number}->{confirm} = $number;
257 if (defined $shelflist->{$number}) {
258 $name = $shelflist->{$number}->{'shelfname'};
259 delete $shelflist->{$number};
261 $name = $privshelflist->{$number}->{'shelfname'};
262 delete $privshelflist->{$number};
264 unless (DelShelf($number)) {
265 push(@paramsloop, {delete_fail=>$name}); last;
267 push(@paramsloop, {delete_ok=>$name});
268 # print $query->redirect($pages{$type}->{redirect}); exit;
272 $stay and $template->param(shelves => 1);
277 (@paramsloop) and $template->param(paramsloop => \@paramsloop);
278 $showadd and $template->param(showadd => 1);
281 my $numberCanManage = 0;
283 # rebuild shelflist in case a shelf has been added
284 ($shelflist, $totshelves) = GetShelves( $category, $shelveslimit, $shelvesoffset, $loggedinuser ) unless $delflag;
285 foreach my $element (sort { lc($shelflist->{$a}->{'shelfname'}) cmp lc($shelflist->{$b}->{'shelfname'}) } keys %$shelflist) {
287 $shelflist->{$element}->{shelf} = $element;
288 my $category = $shelflist->{$element}->{'category'};
289 my $owner = $shelflist->{$element}->{ 'owner' };
290 my $canmanage = ShelfPossibleAction( $loggedinuser, $element, 'manage' );
291 $shelflist->{$element}->{"viewcategory$category"} = 1;
292 $shelflist->{$element}->{manageshelf} = $canmanage;
293 if ($owner eq $loggedinuser or $canmanage) {
294 $shelflist->{$element}->{'mine'} = 1;
296 my $member = GetMember('borrowernumber' => $owner);
297 $shelflist->{$element}->{ownername} = defined($member) ? $member->{firstname} . " " . $member->{surname} : '';
298 $numberCanManage++ if $canmanage; # possibly outmoded
299 if ($shelflist->{$element}->{'category'} eq '1') {
300 push (@shelveslooppriv, $shelflist->{$element});
302 push (@shelvesloop, $shelflist->{$element});
306 my $url = $type eq 'opac' ? "/cgi-bin/koha/opac-shelves.pl" : "/cgi-bin/koha/virtualshelves/shelves.pl";
308 foreach (qw(display viewshelf sortfield)) {
309 $qhash{$_} = $query->param($_) if $query->param($_);
311 (scalar keys %qhash) and $url .= '?' . join '&', map {"$_=$qhash{$_}"} keys %qhash;
312 if ($query->param('viewshelf')) {
313 $template->param( {pagination_bar => pagination_bar($url, (int($totitems/$shelflimit)) + (($totitems % $shelflimit) > 0 ? 1 : 0), $itemoff, "itemoff")} );
315 $template->param( {pagination_bar => pagination_bar($url, (int($totshelves/$shelveslimit)) + (($totshelves % $shelveslimit) > 0 ? 1 : 0), $shelfoff, "shelfoff")} );
318 shelveslooppriv => \@shelveslooppriv,
319 shelvesloop => \@shelvesloop,
320 shelvesloopall => [(@shelvesloop, @shelveslooppriv)],
321 numberCanManage => $numberCanManage,
322 "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
323 csv_profiles => GetCsvProfilesLoop()
325 if ($template->param('viewshelf') or
326 $template->param( 'shelves' ) or
327 $template->param( 'edit' ) ) {
328 $template->param(vseflag => 1);
330 if ($template->param( 'shelves' ) or # note: this part looks duplicative, but is intentional
331 $template->param( 'edit' ) ) {
332 $template->param( seflag => 1);
335 #FIXME: This refresh really only needs to happen when there is a modification of some sort
336 # to the shelves, but the above code is so convoluted in its handling of the various
337 # options, it is easier to do this refresh every time C4::VirtualShelves::Page.pm is
340 my ($total, $pubshelves, $barshelves) = RefreshShelvesSummary($query->cookie("CGISESSID"),$loggedinuser,($loggedinuser == -1 ? 20 : 10));
342 if (defined $barshelves) {
343 $template->param( barshelves => scalar (@{$barshelves->[0]}),
344 barshelvesloop => $barshelves->[0],
346 $template->param( bartotal => $total->{'bartotal'}, ) if ($total->{'bartotal'} > scalar (@{$barshelves->[0]}));
349 if (defined $pubshelves) {
350 $template->param( pubshelves => scalar (@{$pubshelves->[0]}),
351 pubshelvesloop => $pubshelves->[0],
353 $template->param( pubtotal => $total->{'pubtotal'}, ) if ($total->{'pubtotal'} > scalar (@{$pubshelves->[0]}));
356 output_html_with_http_headers $query, $cookie, $template->output;
364 VirtualShelves/Page.pm
368 Module used for both OPAC and intranet pages.
370 =head1 CGI PARAMETERS
374 =item C<modifyshelfcontents>
376 If this script has to modify the shelf content.
380 To know on which shelf to work.
387 * modif: show the template allowing modification of the shelves;
388 * modifsave: save changes from modif mode.
392 Load template with 'viewshelves param' displaying the shelf's information.
396 If the param shelves == 1, then add or delete a shelf.
400 If the param shelves == 1, then addshelf is the name of the shelf to add.