3 # Copyright 2015 Koha Team
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
28 use C4::Tags qw( get_tags );
30 use Koha::Virtualshelves;
34 my $template_name = $query->param('rss') ? "opac-shelves-rss.tt" : "opac-shelves.tt";
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
37 template_name => $template_name,
40 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
43 my $op = $query->param('op') || 'list';
44 my $referer = $query->param('referer') || $op;
45 my $category = $query->param('category') || 1;
46 my ( $shelf, $shelfnumber, @messages );
48 if ( $op eq 'add_form' ) {
50 } elsif ( $op eq 'edit_form' ) {
51 $shelfnumber = $query->param('shelfnumber');
52 $shelf = Koha::Virtualshelves->find($shelfnumber);
55 $category = $shelf->category;
56 my $patron = GetMember( 'borrowernumber' => $shelf->owner );
57 $template->param( owner => $patron, );
58 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
59 push @messages, { type => 'error', code => 'unauthorized_on_update' };
63 push @messages, { type => 'error', code => 'does_not_exist' };
65 } elsif ( $op eq 'add' ) {
66 if ( $loggedinuser ) {
68 $shelf = Koha::Virtualshelf->new(
69 { shelfname => $query->param('shelfname'),
70 sortfield => $query->param('sortfield'),
71 category => $query->param('category') || 1,
72 allow_add => $query->param('allow_add'),
73 allow_delete_own => $query->param('allow_delete_own'),
74 allow_delete_other => $query->param('allow_delete_other'),
75 owner => $loggedinuser,
79 $shelfnumber = $shelf->shelfnumber;
82 push @messages, { type => 'error', code => ref($@), msg => $@ };
83 } elsif ( not $shelf ) {
84 push @messages, { type => 'error', code => 'error_on_insert' };
86 push @messages, { type => 'message', code => 'success_on_insert' };
90 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
93 } elsif ( $op eq 'edit' ) {
94 $shelfnumber = $query->param('shelfnumber');
95 $shelf = Koha::Virtualshelves->find($shelfnumber);
98 if ( $shelf->can_be_managed( $loggedinuser ) ) {
99 $shelf->shelfname( $query->param('shelfname') );
100 $shelf->sortfield( $query->param('sortfield') );
101 $shelf->allow_add( $query->param('allow_add') );
102 $shelf->allow_delete_own( $query->param('allow_delete_own') );
103 $shelf->allow_delete_other( $query->param('allow_delete_other') );
104 $shelf->category( $query->param('category') );
105 eval { $shelf->store };
108 push @messages, { type => 'error', code => 'error_on_update' };
111 push @messages, { type => 'message', code => 'success_on_update' };
114 push @messages, { type => 'error', code => 'unauthorized_on_update' };
117 push @messages, { type => 'error', code => 'does_not_exist' };
119 } elsif ( $op eq 'delete' ) {
120 $shelfnumber = $query->param('shelfnumber');
121 $shelf = Koha::Virtualshelves->find($shelfnumber);
123 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
124 eval { $shelf->delete; };
126 push @messages, { type => 'error', code => ref($@), msg => $@ };
128 push @messages, { type => 'message', code => 'success_on_delete' };
131 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
134 push @messages, { type => 'error', code => 'does_not_exist' };
137 } elsif ( $op eq 'remove_share' ) {
138 $shelfnumber = $query->param('shelfnumber');
139 $shelf = Koha::Virtualshelves->find($shelfnumber);
141 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
143 push @messages, { type => 'error', code => ref($@), msg => $@ };
144 } elsif ( $removed ) {
145 push @messages, { type => 'message', code => 'success_on_remove_share' };
147 push @messages, { type => 'error', code => 'error_on_remove_share' };
150 push @messages, { type => 'error', code => 'does_not_exist' };
154 } elsif ( $op eq 'add_biblio' ) {
155 $shelfnumber = $query->param('shelfnumber');
156 $shelf = Koha::Virtualshelves->find($shelfnumber);
158 if( my $barcode = $query->param('barcode') ) {
159 my $item = GetItem( 0, $barcode);
160 if (defined $item && $item->{itemnumber}) {
161 my $biblio = GetBiblioFromItemNumber( $item->{itemnumber} );
162 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
163 my $added = eval { $shelf->add_biblio( $biblio->{biblionumber}, $loggedinuser ); };
165 push @messages, { type => 'error', code => ref($@), msg => $@ };
167 push @messages, { type => 'message', code => 'success_on_add_biblio' };
169 push @messages, { type => 'message', code => 'error_on_add_biblio' };
172 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
175 push @messages, { type => 'error', code => 'item_does_not_exist' };
179 push @messages, { type => 'error', code => 'does_not_exist' };
182 } elsif ( $op eq 'remove_biblios' ) {
183 $shelfnumber = $query->param('shelfnumber');
184 $shelf = Koha::Virtualshelves->find($shelfnumber);
185 my @biblionumber = $query->param('biblionumber');
187 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
188 my $number_of_biblios_removed = eval {
189 $shelf->remove_biblios(
191 biblionumbers => \@biblionumber,
192 borrowernumber => $loggedinuser,
197 push @messages, { type => 'error', code => ref($@), msg => $@ };
198 } elsif ( $number_of_biblios_removed ) {
199 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
201 push @messages, { type => 'error', code => 'no_biblio_removed' };
204 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
207 push @messages, { type => 'error', code => 'does_not_exist' };
212 if ( $op eq 'view' ) {
213 $shelfnumber ||= $query->param('shelfnumber');
214 $shelf = Koha::Virtualshelves->find($shelfnumber);
216 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
217 $category = $shelf->category;
218 my $sortfield = $query->param('sortfield') || $shelf->sortfield; # Passed in sorting overrides default sorting
219 my $direction = $query->param('direction') || 'asc';
221 unless ( $query->param('print') or $query->param('rss') ) {
222 $rows = C4::Context->preference('OPACnumSearchResults') || 20;
223 $page = ( $query->param('page') ? $query->param('page') : 1 );
225 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.itemcallnumber' : $sortfield;
226 my $contents = $shelf->get_contents->search(
229 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
232 order_by => "$order_by $direction",
236 # get biblionumbers stored in the cart
238 if(my $cart_list = $query->cookie('bib_list')){
239 @cart_list = split(/\//, $cart_list);
242 my $borrower = GetMember( borrowernumber => $loggedinuser );
245 while ( my $content = $contents->next ) {
247 my $biblionumber = $content->biblionumber->biblionumber;
248 my $record = GetMarcBiblio($biblionumber);
250 if ( C4::Context->preference("OPACXSLTResultsDisplay") ) {
251 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "OPACXSLTResultsDisplay" );
254 my $marcflavour = C4::Context->preference("marcflavour");
255 my $itemtypeinfo = getitemtypeinfo( $content->biblionumber->biblioitems->first->itemtype, 'intranet' );
256 $this_item->{imageurl} = $itemtypeinfo->{imageurl};
257 $this_item->{description} = $itemtypeinfo->{description};
258 $this_item->{notforloan} = $itemtypeinfo->{notforloan};
259 $this_item->{'coins'} = GetCOinSBiblio($record);
260 $this_item->{'subtitle'} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
261 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
262 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
263 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
264 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
266 unless ( defined $this_item->{size} ) {
268 #TT has problems with size
269 $this_item->{size} = q||;
272 # Getting items infos for location display
273 my @items_infos = &GetItemsLocationInfo( $biblionumber );
274 $this_item->{'ITEM_RESULTS'} = \@items_infos;
276 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
277 $this_item->{TagLoop} = get_tags({
278 biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
279 limit => C4::Context->preference('TagsShowOnList'),
283 $this_item->{allow_onshelf_holds} = C4::Reserves::OnShelfHoldsAllowed($this_item, $borrower);
286 if ( grep {$_ eq $biblionumber} @cart_list) {
287 $this_item->{incart} = 1;
290 if ( $query->param('rss') ) {
291 $this_item->{title} = $content->biblionumber->title;
292 $this_item->{author} = $content->biblionumber->author;
295 $this_item->{biblionumber} = $biblionumber;
296 push @items, $this_item;
300 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
301 can_delete_shelf => $shelf->can_be_deleted($loggedinuser),
302 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
303 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
304 sortfield => $sortfield,
305 itemsloop => \@items,
306 sortfield => $sortfield,
307 direction => $direction,
310 my $pager = $contents->pager;
312 pagination_bar => pagination_bar(
313 q||, $pager->last_page - $pager->first_page + 1,
314 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
319 push @messages, { type => 'error', code => 'unauthorized_on_view' };
322 push @messages, { type => 'error', code => 'does_not_exist' };
326 if ( $op eq 'list' ) {
328 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
329 if ( $category == 1 ) {
330 $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
332 $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
335 my $pager = $shelves->pager;
338 pagination_bar => pagination_bar(
339 q||, $pager->last_page - $pager->first_page + 1,
340 $page, "page", { op => 'list', category => $category, }
349 messages => \@messages,
350 category => $category,
351 print => $query->param('print') || 0,
355 output_html_with_http_headers $query, $cookie, $template->output;