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