Bug 17032: Make sure Swagger object definition is up-to-date with Koha-object
[koha.git] / opac / opac-shelves.pl
blob25120e79473f9f8ddf4bde1c3300fa34bbf10f5a
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use C4::Auth;
24 use C4::Biblio;
25 use C4::Koha;
26 use C4::Items;
27 use C4::Members;
28 use C4::Output;
29 use C4::Tags qw( get_tags );
30 use C4::XSLT;
31 use Koha::Virtualshelves;
32 use Koha::RecordProcessor;
34 my $query = new CGI;
36 my $template_name = $query->param('rss') ? "opac-shelves-rss.tt" : "opac-shelves.tt";
38 # if virtualshelves is disabled, leave immediately
39 if ( ! C4::Context->preference('virtualshelves') ) {
40 print $query->redirect("/cgi-bin/koha/errors/404.pl");
41 exit;
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
45 template_name => $template_name,
46 query => $query,
47 type => "opac",
48 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
49 });
51 my $op = $query->param('op') || 'list';
52 my $referer = $query->param('referer') || $op;
53 my $category = $query->param('category') || 1;
54 my ( $shelf, $shelfnumber, @messages );
56 if ( $op eq 'add_form' ) {
57 # Nothing to do
58 } elsif ( $op eq 'edit_form' ) {
59 $shelfnumber = $query->param('shelfnumber');
60 $shelf = Koha::Virtualshelves->find($shelfnumber);
62 if ( $shelf ) {
63 $category = $shelf->category;
64 my $patron = GetMember( 'borrowernumber' => $shelf->owner );
65 $template->param( owner => $patron, );
66 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
67 push @messages, { type => 'error', code => 'unauthorized_on_update' };
68 $op = 'list';
70 } else {
71 push @messages, { type => 'error', code => 'does_not_exist' };
73 } elsif ( $op eq 'add' ) {
74 if ( $loggedinuser ) {
75 eval {
76 $shelf = Koha::Virtualshelf->new(
77 { shelfname => scalar $query->param('shelfname'),
78 sortfield => scalar $query->param('sortfield'),
79 category => scalar $query->param('category') || 1,
80 allow_add => scalar $query->param('allow_add'),
81 allow_delete_own => scalar $query->param('allow_delete_own'),
82 allow_delete_other => scalar $query->param('allow_delete_other'),
83 owner => scalar $loggedinuser,
86 $shelf->store;
87 $shelfnumber = $shelf->shelfnumber;
89 if ($@) {
90 push @messages, { type => 'error', code => ref($@), msg => $@ };
91 } elsif ( not $shelf ) {
92 push @messages, { type => 'error', code => 'error_on_insert' };
93 } else {
94 push @messages, { type => 'message', code => 'success_on_insert' };
95 $op = 'view';
97 } else {
98 push @messages, { type => 'error', code => 'unauthorized_on_insert' };
99 $op = 'list';
101 } elsif ( $op eq 'edit' ) {
102 $shelfnumber = $query->param('shelfnumber');
103 $shelf = Koha::Virtualshelves->find($shelfnumber);
104 if ( $shelf ) {
105 $op = $referer;
106 if ( $shelf->can_be_managed( $loggedinuser ) ) {
107 $shelf->shelfname( $query->param('shelfname') );
108 $shelf->sortfield( $query->param('sortfield') );
109 $shelf->allow_add( $query->param('allow_add') );
110 $shelf->allow_delete_own( $query->param('allow_delete_own') );
111 $shelf->allow_delete_other( $query->param('allow_delete_other') );
112 $shelf->category( $query->param('category') );
113 eval { $shelf->store };
115 if ($@) {
116 push @messages, { type => 'error', code => 'error_on_update' };
117 $op = 'edit_form';
118 } else {
119 push @messages, { type => 'message', code => 'success_on_update' };
121 } else {
122 push @messages, { type => 'error', code => 'unauthorized_on_update' };
124 } else {
125 push @messages, { type => 'error', code => 'does_not_exist' };
127 } elsif ( $op eq 'delete' ) {
128 $shelfnumber = $query->param('shelfnumber');
129 $shelf = Koha::Virtualshelves->find($shelfnumber);
130 if ($shelf) {
131 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
132 eval { $shelf->delete; };
133 if ($@) {
134 push @messages, { type => 'error', code => ref($@), msg => $@ };
135 } else {
136 push @messages, { type => 'message', code => 'success_on_delete' };
138 } else {
139 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
141 } else {
142 push @messages, { type => 'error', code => 'does_not_exist' };
144 $op = $referer;
145 } elsif ( $op eq 'remove_share' ) {
146 $shelfnumber = $query->param('shelfnumber');
147 $shelf = Koha::Virtualshelves->find($shelfnumber);
148 if ($shelf) {
149 my $removed = eval { $shelf->remove_share( $loggedinuser ); };
150 if ($@) {
151 push @messages, { type => 'error', code => ref($@), msg => $@ };
152 } elsif ( $removed ) {
153 push @messages, { type => 'message', code => 'success_on_remove_share' };
154 } else {
155 push @messages, { type => 'error', code => 'error_on_remove_share' };
157 } else {
158 push @messages, { type => 'error', code => 'does_not_exist' };
160 $op = $referer;
162 } elsif ( $op eq 'add_biblio' ) {
163 $shelfnumber = $query->param('shelfnumber');
164 $shelf = Koha::Virtualshelves->find($shelfnumber);
165 if ($shelf) {
166 if( my $barcode = $query->param('barcode') ) {
167 my $item = GetItem( 0, $barcode);
168 if (defined $item && $item->{itemnumber}) {
169 my $biblio = GetBiblioFromItemNumber( $item->{itemnumber} );
170 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
171 my $added = eval { $shelf->add_biblio( $biblio->{biblionumber}, $loggedinuser ); };
172 if ($@) {
173 push @messages, { type => 'error', code => ref($@), msg => $@ };
174 } elsif ( $added ) {
175 push @messages, { type => 'message', code => 'success_on_add_biblio' };
176 } else {
177 push @messages, { type => 'message', code => 'error_on_add_biblio' };
179 } else {
180 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
182 } else {
183 push @messages, { type => 'error', code => 'item_does_not_exist' };
186 } else {
187 push @messages, { type => 'error', code => 'does_not_exist' };
189 $op = $referer;
190 } elsif ( $op eq 'remove_biblios' ) {
191 $shelfnumber = $query->param('shelfnumber');
192 $shelf = Koha::Virtualshelves->find($shelfnumber);
193 my @biblionumber = $query->multi_param('biblionumber');
194 if ($shelf) {
195 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
196 my $number_of_biblios_removed = eval {
197 $shelf->remove_biblios(
199 biblionumbers => \@biblionumber,
200 borrowernumber => $loggedinuser,
204 if ($@) {
205 push @messages, { type => 'error', code => ref($@), msg => $@ };
206 } elsif ( $number_of_biblios_removed ) {
207 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
208 } else {
209 push @messages, { type => 'error', code => 'no_biblio_removed' };
211 } else {
212 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
214 } else {
215 push @messages, { type => 'error', code => 'does_not_exist' };
217 $op = 'view';
220 if ( $op eq 'view' ) {
221 $shelfnumber ||= $query->param('shelfnumber');
222 $shelf = Koha::Virtualshelves->find($shelfnumber);
223 if ( $shelf ) {
224 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
225 $category = $shelf->category;
226 my $sortfield = $query->param('sortfield') || $shelf->sortfield; # Passed in sorting overrides default sorting
227 my $direction = $query->param('direction') || 'asc';
228 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
229 my ( $page, $rows );
230 unless ( $query->param('print') or $query->param('rss') ) {
231 $rows = C4::Context->preference('OPACnumSearchResults') || 20;
232 $page = ( $query->param('page') ? $query->param('page') : 1 );
234 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.itemcallnumber' : $sortfield;
235 my $contents = $shelf->get_contents->search(
238 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
239 page => $page,
240 rows => $rows,
241 order_by => { "-$direction" => $order_by },
245 # get biblionumbers stored in the cart
246 my @cart_list;
247 if(my $cart_list = $query->cookie('bib_list')){
248 @cart_list = split(/\//, $cart_list);
251 my $borrower = GetMember( borrowernumber => $loggedinuser );
253 # Lists display falls back to search results configuration
254 my $xslfile = C4::Context->preference('OPACXSLTListsDisplay');
255 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
256 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
258 my $record_processor = Koha::RecordProcessor->new({ filters => 'ViewPolicy' });
259 my @items;
260 while ( my $content = $contents->next ) {
261 my $biblionumber = $content->biblionumber->biblionumber;
262 my $this_item = GetBiblioData($biblionumber);
263 my $record = GetMarcBiblio($biblionumber);
264 my $framework = GetFrameworkCode( $biblionumber );
265 $record_processor->options({
266 interface => 'opac',
267 frameworkcode => $framework
269 $record_processor->process($record);
271 if ( $xslfile ) {
272 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "OPACXSLTListsDisplay",
273 1, undef, $sysxml, $xslfile, $lang);
276 my $marcflavour = C4::Context->preference("marcflavour");
277 my $itemtypeinfo = getitemtypeinfo( $content->biblionumber->biblioitems->first->itemtype, 'opac' );
278 $this_item->{imageurl} = $itemtypeinfo->{imageurl};
279 $this_item->{description} = $itemtypeinfo->{description};
280 $this_item->{notforloan} = $itemtypeinfo->{notforloan};
281 $this_item->{'coins'} = GetCOinSBiblio($record);
282 $this_item->{'subtitle'} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
283 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
284 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
285 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
286 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
288 unless ( defined $this_item->{size} ) {
290 #TT has problems with size
291 $this_item->{size} = q||;
294 # Getting items infos for location display
295 my @items_infos = &GetItemsLocationInfo( $biblionumber );
296 $this_item->{'ITEM_RESULTS'} = \@items_infos;
298 if (C4::Context->preference('TagsEnabled') and C4::Context->preference('TagsShowOnList')) {
299 $this_item->{TagLoop} = get_tags({
300 biblionumber => $biblionumber, approved=>1, 'sort'=>'-weight',
301 limit => C4::Context->preference('TagsShowOnList'),
305 $this_item->{allow_onshelf_holds} = C4::Reserves::OnShelfHoldsAllowed($this_item, $borrower);
308 if ( grep {$_ eq $biblionumber} @cart_list) {
309 $this_item->{incart} = 1;
312 if ( $query->param('rss') ) {
313 $this_item->{title} = $content->biblionumber->title;
314 $this_item->{author} = $content->biblionumber->author;
317 $this_item->{biblionumber} = $biblionumber;
318 push @items, $this_item;
321 $template->param(
322 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
323 can_delete_shelf => $shelf->can_be_deleted($loggedinuser),
324 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
325 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
326 sortfield => $sortfield,
327 itemsloop => \@items,
328 sortfield => $sortfield,
329 direction => $direction,
331 if ( $page ) {
332 my $pager = $contents->pager;
333 $template->param(
334 pagination_bar => pagination_bar(
335 q||, $pager->last_page - $pager->first_page + 1,
336 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
340 } else {
341 push @messages, { type => 'error', code => 'unauthorized_on_view' };
343 } else {
344 push @messages, { type => 'error', code => 'does_not_exist' };
348 if ( $op eq 'list' ) {
349 my $shelves;
350 my ( $page, $rows ) = ( $query->param('page') || 1, 20 );
351 if ( $category == 1 ) {
352 $shelves = Koha::Virtualshelves->get_private_shelves({ page => $page, rows => $rows, borrowernumber => $loggedinuser, });
353 } else {
354 $shelves = Koha::Virtualshelves->get_public_shelves({ page => $page, rows => $rows, });
357 my $pager = $shelves->pager;
358 $template->param(
359 shelves => $shelves,
360 pagination_bar => pagination_bar(
361 q||, $pager->last_page - $pager->first_page + 1,
362 $page, "page", { op => 'list', category => $category, }
367 $template->param(
368 op => $op,
369 referer => $referer,
370 shelf => $shelf,
371 messages => \@messages,
372 category => $category,
373 print => scalar $query->param('print') || 0,
374 listsview => 1,
377 output_html_with_http_headers $query, $cookie, $template->output;