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