Bug 16497: Add missing field in definitions due to bug 18066
[koha.git] / virtualshelves / shelves.pl
1 #!/usr/bin/perl
2
3 # Copyright 2015 Koha Team
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use C4::Auth;
23 use C4::Biblio;
24 use C4::Koha;
25 use C4::Items;
26 use C4::Members;
27 use C4::Output;
28 use C4::XSLT;
29
30 use Koha::Biblios;
31 use Koha::Biblioitems;
32 use Koha::ItemTypes;
33 use Koha::CsvProfiles;
34 use Koha::Patrons;
35 use Koha::Virtualshelves;
36
37 use constant ANYONE => 2;
38
39 my $query = new CGI;
40
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42     {   template_name   => "virtualshelves/shelves.tt",
43         query           => $query,
44         type            => "intranet",
45         authnotrequired => 0,
46         flagsrequired   => { catalogue => 1 },
47     }
48 );
49
50 my $op       = $query->param('op')       || 'list';
51 my $referer  = $query->param('referer')  || $op;
52 my $category = $query->param('category') || 1;
53 my ( $shelf, $shelfnumber, @messages );
54
55 if ( $op eq 'add_form' ) {
56     # Only pass default
57     $shelf = { allow_change_from_owner => 1 };
58 } elsif ( $op eq 'edit_form' ) {
59     $shelfnumber = $query->param('shelfnumber');
60     $shelf       = Koha::Virtualshelves->find($shelfnumber);
61
62     if ( $shelf ) {
63         $category = $shelf->category;
64         my $patron = Koha::Patrons->find( $shelf->owner )->unblessed;
65         $template->param( owner => $patron, );
66         unless ( $shelf->can_be_managed( $loggedinuser ) ) {
67             push @messages, { type => 'alert', code => 'unauthorized_on_update' };
68             $op = 'list';
69         }
70     } else {
71         push @messages, { type => 'alert', code => 'does_not_exist' };
72     }
73 } elsif ( $op eq 'add' ) {
74     my $allow_changes_from = $query->param('allow_changes_from');
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'),
80                 allow_change_from_owner => $allow_changes_from > 0,
81                 allow_change_from_others => $allow_changes_from == ANYONE,
82                 owner              => scalar $query->param('owner'),
83             }
84         );
85         $shelf->store;
86         $shelfnumber = $shelf->shelfnumber;
87     };
88     if ($@) {
89         push @messages, { type => 'alert', code => ref($@), msg => $@ };
90     } elsif ( not $shelf ) {
91         push @messages, { type => 'alert', code => 'error_on_insert' };
92
93     } else {
94         push @messages, { type => 'message', code => 'success_on_insert' };
95         $op = 'view';
96     }
97 } elsif ( $op eq 'edit' ) {
98     $shelfnumber = $query->param('shelfnumber');
99     $shelf       = Koha::Virtualshelves->find($shelfnumber);
100
101     if ( $shelf ) {
102         $op = $referer;
103         my $sortfield = $query->param('sortfield');
104         $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber dateadded );
105         if ( $shelf->can_be_managed( $loggedinuser ) ) {
106             $shelf->shelfname( scalar $query->param('shelfname') );
107             $shelf->sortfield( $sortfield );
108             my $allow_changes_from = $query->param('allow_changes_from');
109             $shelf->allow_change_from_owner( $allow_changes_from > 0 );
110             $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
111             $shelf->category( scalar $query->param('category') );
112             eval { $shelf->store };
113
114             if ($@) {
115                 push @messages, { type => 'alert', code => 'error_on_update' };
116                 $op = 'edit_form';
117             } else {
118                 push @messages, { type => 'message', code => 'success_on_update' };
119             }
120         } else {
121             push @messages, { type => 'alert', code => 'unauthorized_on_update' };
122         }
123     } else {
124         push @messages, { type => 'alert', code => 'does_not_exist' };
125     }
126 } elsif ( $op eq 'delete' ) {
127     $shelfnumber = $query->param('shelfnumber');
128     $shelf       = Koha::Virtualshelves->find($shelfnumber);
129     if ($shelf) {
130         if ( $shelf->can_be_deleted( $loggedinuser ) ) {
131             eval { $shelf->delete; };
132             if ($@) {
133                 push @messages, { type => 'alert', code => ref($@), msg => $@ };
134             } else {
135                 push @messages, { type => 'message', code => 'success_on_delete' };
136             }
137         } else {
138             push @messages, { type => 'alert', code => 'unauthorized_on_delete' };
139         }
140     } else {
141         push @messages, { type => 'alert', code => 'does_not_exist' };
142     }
143     $op = 'list';
144 } elsif ( $op eq 'add_biblio' ) {
145     $shelfnumber = $query->param('shelfnumber');
146     $shelf = Koha::Virtualshelves->find($shelfnumber);
147     if ($shelf) {
148         if( my $barcodes = $query->param('barcodes') ) {
149             if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
150                 my @barcodes = split /\n/, $barcodes; # Entries are effectively passed in as a <cr> separated list
151                 foreach my $barcode (@barcodes){
152                     $barcode =~ s/\r$//; # strip any naughty return chars
153                     next if $barcode eq '';
154                     my $item = GetItem( 0, $barcode);
155                     if (defined $item && $item->{itemnumber}) {
156                         my $added = eval { $shelf->add_biblio( $item->{biblionumber}, $loggedinuser ); };
157                         if ($@) {
158                             push @messages, { item_barcode => $barcode, type => 'alert', code => ref($@), msg => $@ };
159                         } elsif ( $added ) {
160                             push @messages, { item_barcode => $barcode, type => 'message', code => 'success_on_add_biblio' };
161                         } else {
162                             push @messages, { item_barcode => $barcode, type => 'message', code => 'error_on_add_biblio' };
163                         }
164                     } else {
165                         push @messages, { item_barcode => $barcode, type => 'alert', code => 'item_does_not_exist' };
166                     }
167                 }
168             } else {
169                 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
170             }
171         }
172         if ( my $biblionumbers = $query->param('biblionumbers') ) {
173             if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
174                 my @biblionumbers = split /\n/, $biblionumbers;
175                 foreach my $biblionumber (@biblionumbers) {
176                     $biblionumber =~ s/\r$//; # strip any naughty return chars
177                     next if $biblionumber eq '';
178                     my $biblio = Koha::Biblios->find($biblionumber);
179                     if (defined $biblio) {
180                         my $added = eval { $shelf->add_biblio( $biblionumber, $loggedinuser ); };
181                         if ($@) {
182                             push @messages, { bibnum => $biblionumber, type => 'alert', code => ref($@), msg => $@ };
183                         } elsif ( $added ) {
184                             push @messages, { bibnum => $biblionumber, type => 'message', code => 'success_on_add_biblio' };
185                         } else {
186                             push @messages, { bibnum => $biblionumber, type => 'message', code => 'error_on_add_biblio' };
187                         }
188                     } else {
189                         push @messages, { bibnum => $biblionumber, type => 'alert', code => 'item_does_not_exist' };
190                     }
191                 }
192             } else {
193                 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
194             }
195         }
196     } else {
197         push @messages, { type => 'alert', code => 'does_not_exist' };
198     }
199     $op = $referer;
200 } elsif ( $op eq 'remove_biblios' ) {
201     $shelfnumber = $query->param('shelfnumber');
202     $shelf = Koha::Virtualshelves->find($shelfnumber);
203     my @biblionumbers = $query->multi_param('biblionumber');
204     if ($shelf) {
205         if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
206             my $number_of_biblios_removed = eval {
207                 $shelf->remove_biblios(
208                     {
209                         biblionumbers => \@biblionumbers,
210                         borrowernumber => $loggedinuser,
211                     }
212                 );
213             };
214             if ($@) {
215                 push @messages, { type => 'alert', code => ref($@), msg => $@ };
216             } elsif ( $number_of_biblios_removed ) {
217                 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
218             } else {
219                 push @messages, { type => 'alert', code => 'no_biblio_removed' };
220             }
221         } else {
222             push @messages, { type => 'alert', code => 'unauthorized_on_remove_biblios' };
223         }
224     } else {
225         push @messages, { type => 'alert', code => 'does_not_exist' };
226     }
227     $op = $referer;
228 }
229
230 if ( $op eq 'view' ) {
231     $shelfnumber ||= $query->param('shelfnumber');
232     $shelf = Koha::Virtualshelves->find($shelfnumber);
233     if ( $shelf ) {
234         if ( $shelf->can_be_viewed( $loggedinuser ) ) {
235             my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title';    # Passed in sorting overrides default sorting
236             $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber dateadded );
237             my $direction = $query->param('direction') || 'asc';
238             $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
239             my ( $rows, $page );
240             unless ( $query->param('print') ) {
241                 $rows = C4::Context->preference('numSearchResults') || 20;
242                 $page = ( $query->param('page') ? $query->param('page') : 1 );
243             }
244
245             my $order_by = $sortfield eq 'itemcallnumber' ? 'items.cn_sort' : $sortfield;
246             my $contents = $shelf->get_contents->search(
247                 {},
248                 {
249                     prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
250                     page     => $page,
251                     rows     => $rows,
252                     order_by => { "-$direction" => $order_by },
253                 }
254             );
255
256             my $xslfile = C4::Context->preference('XSLTListsDisplay');
257             my $lang   = $xslfile ? C4::Languages::getlanguage()  : undef;
258             my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
259
260             my @items;
261             while ( my $content = $contents->next ) {
262                 my $this_item;
263                 my $biblionumber = $content->biblionumber;
264                 my $record       = GetMarcBiblio({ biblionumber => $biblionumber });
265
266                 if ( $xslfile ) {
267                     $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "XSLTListsDisplay",
268                                                                 1, undef, $sysxml, $xslfile, $lang);
269                 }
270
271                 my $marcflavour = C4::Context->preference("marcflavour");
272                 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
273                 $itemtype = Koha::ItemTypes->find( $itemtype );
274                 my $biblio = Koha::Biblios->find( $content->biblionumber );
275                 $this_item->{title}             = $biblio->title;
276                 $this_item->{author}            = $biblio->author;
277                 $this_item->{dateadded}         = $content->dateadded;
278                 $this_item->{imageurl}          = $itemtype ? C4::Koha::getitemtypeimagelocation( 'intranet', $itemtype->imageurl ) : q{};
279                 $this_item->{description}       = $itemtype ? $itemtype->description : q{}; #FIXME Should this be translated_description ?
280                 $this_item->{notforloan}        = $itemtype->notforloan if $itemtype;
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 );
287
288                 unless ( defined $this_item->{size} ) {
289
290                     #TT has problems with size
291                     $this_item->{size} = q||;
292                 }
293
294                 # Getting items infos for location display
295                 my @items_infos = &GetItemsLocationInfo( $biblionumber );
296                 $this_item->{'ITEM_RESULTS'} = \@items_infos;
297                 $this_item->{biblionumber} = $biblionumber;
298                 push @items, $this_item;
299             }
300
301             my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
302                 {
303                     borrowernumber => $loggedinuser,
304                     add_allowed    => 1,
305                     category       => 1,
306                 }
307             );
308             my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
309                 {
310                     borrowernumber => $loggedinuser,
311                     add_allowed    => 1,
312                     category       => 2,
313                 }
314             );
315
316             $template->param(
317                 add_to_some_private_shelves => $some_private_shelves,
318                 add_to_some_public_shelves  => $some_public_shelves,
319                 can_manage_shelf   => $shelf->can_be_managed($loggedinuser),
320                 can_remove_shelf   => $shelf->can_be_deleted($loggedinuser),
321                 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
322                 can_add_biblios    => $shelf->can_biblios_be_added($loggedinuser),
323                 sortfield          => $sortfield,
324                 itemsloop          => \@items,
325                 sortfield          => $sortfield,
326                 direction          => $direction,
327             );
328             if ( $page ) {
329                 my $pager = $contents->pager;
330                 $template->param(
331                     pagination_bar => pagination_bar(
332                         q||, $pager->last_page - $pager->first_page + 1,
333                         $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
334                     ),
335                 );
336             }
337         } else {
338             push @messages, { type => 'error', code => 'unauthorized_on_view' };
339             undef $shelf;
340         }
341     } else {
342         push @messages, { type => 'alert', code => 'does_not_exist' };
343     }
344 }
345
346 $template->param(
347     op       => $op,
348     referer  => $referer,
349     shelf    => $shelf,
350     messages => \@messages,
351     category => $category,
352     print    => scalar $query->param('print') || 0,
353     csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' }) ],
354 );
355
356 output_html_with_http_headers $query, $cookie, $template->output;