Increment version for 3.22.21
[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::Csv;
25 use C4::Koha;
26 use C4::Items;
27 use C4::Members;
28 use C4::Output;
29 use C4::XSLT;
30 use Koha::Virtualshelves;
31
32 my $query = new CGI;
33
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
35     {   template_name   => "virtualshelves/shelves.tt",
36         query           => $query,
37         type            => "intranet",
38         authnotrequired => 0,
39         flagsrequired   => { catalogue => 1 },
40     }
41 );
42
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 );
47
48 if ( $op eq 'add_form' ) {
49     # Nothing to do
50 } elsif ( $op eq 'edit_form' ) {
51     $shelfnumber = $query->param('shelfnumber');
52     $shelf       = Koha::Virtualshelves->find($shelfnumber);
53
54     if ( $shelf ) {
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' };
60             $op = 'list';
61         }
62     } else {
63         push @messages, { type => 'error', code => 'does_not_exist' };
64     }
65 } elsif ( $op eq 'add' ) {
66     eval {
67         $shelf = Koha::Virtualshelf->new(
68             {   shelfname          => scalar $query->param('shelfname'),
69                 sortfield          => scalar $query->param('sortfield'),
70                 category           => scalar $query->param('category'),
71                 allow_add          => scalar $query->param('allow_add'),
72                 allow_delete_own   => scalar $query->param('allow_delete_own'),
73                 allow_delete_other => scalar $query->param('allow_delete_other'),
74                 owner              => scalar $query->param('owner'),
75             }
76         );
77         $shelf->store;
78         $shelfnumber = $shelf->shelfnumber;
79     };
80     if ($@) {
81         push @messages, { type => 'error', code => ref($@), msg => $@ };
82     } elsif ( not $shelf ) {
83         push @messages, { type => 'error', code => 'error_on_insert' };
84     } else {
85         push @messages, { type => 'message', code => 'success_on_insert' };
86         $op = 'view';
87     }
88 } elsif ( $op eq 'edit' ) {
89     $shelfnumber = $query->param('shelfnumber');
90     $shelf       = Koha::Virtualshelves->find($shelfnumber);
91
92     if ( $shelf ) {
93         $op = $referer;
94         my $sortfield = $query->param('sortfield');
95         $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
96         if ( $shelf->can_be_managed( $loggedinuser ) ) {
97             $shelf->shelfname( scalar $query->param('shelfname') );
98             $shelf->sortfield( $sortfield );
99             $shelf->allow_add( scalar $query->param('allow_add') );
100             $shelf->allow_delete_own( scalar $query->param('allow_delete_own') );
101             $shelf->allow_delete_other( scalar $query->param('allow_delete_other') );
102             $shelf->category( scalar $query->param('category') );
103             eval { $shelf->store };
104
105             if ($@) {
106                 push @messages, { type => 'error', code => 'error_on_update' };
107                 $op = 'edit_form';
108             } else {
109                 push @messages, { type => 'message', code => 'success_on_update' };
110             }
111         } else {
112             push @messages, { type => 'error', code => 'unauthorized_on_update' };
113         }
114     } else {
115         push @messages, { type => 'error', code => 'does_not_exist' };
116     }
117 } elsif ( $op eq 'delete' ) {
118     $shelfnumber = $query->param('shelfnumber');
119     $shelf       = Koha::Virtualshelves->find($shelfnumber);
120     if ($shelf) {
121         if ( $shelf->can_be_deleted( $loggedinuser ) ) {
122             eval { $shelf->delete; };
123             if ($@) {
124                 push @messages, { type => 'error', code => ref($@), msg => $@ };
125             } else {
126                 push @messages, { type => 'message', code => 'success_on_delete' };
127             }
128         } else {
129             push @messages, { type => 'error', code => 'unauthorized_on_delete' };
130         }
131     } else {
132         push @messages, { type => 'error', code => 'does_not_exist' };
133     }
134     $op = 'list';
135 } elsif ( $op eq 'add_biblio' ) {
136     $shelfnumber = $query->param('shelfnumber');
137     $shelf = Koha::Virtualshelves->find($shelfnumber);
138     if ($shelf) {
139         if( my $barcode = $query->param('barcode') ) {
140             my $item = GetItem( 0, $barcode);
141             if (defined $item && $item->{itemnumber}) {
142                 my $biblio = GetBiblioFromItemNumber( $item->{itemnumber} );
143                 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
144                     my $added = eval { $shelf->add_biblio( $biblio->{biblionumber}, $loggedinuser ); };
145                     if ($@) {
146                         push @messages, { type => 'error', code => ref($@), msg => $@ };
147                     } elsif ( $added ) {
148                         push @messages, { type => 'message', code => 'success_on_add_biblio' };
149                     } else {
150                         push @messages, { type => 'message', code => 'error_on_add_biblio' };
151                     }
152                 } else {
153                     push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
154                 }
155             } else {
156                 push @messages, { type => 'error', code => 'item_does_not_exist' };
157             }
158         }
159     } else {
160         push @messages, { type => 'error', code => 'does_not_exist' };
161     }
162     $op = $referer;
163 } elsif ( $op eq 'remove_biblios' ) {
164     $shelfnumber = $query->param('shelfnumber');
165     $shelf = Koha::Virtualshelves->find($shelfnumber);
166     my @biblionumbers = $query->multi_param('biblionumber');
167     if ($shelf) {
168         if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
169             my $number_of_biblios_removed = eval {
170                 $shelf->remove_biblios(
171                     {
172                         biblionumbers => \@biblionumbers,
173                         borrowernumber => $loggedinuser,
174                     }
175                 );
176             };
177             if ($@) {
178                 push @messages, { type => 'error', code => ref($@), msg => $@ };
179             } elsif ( $number_of_biblios_removed ) {
180                 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
181             } else {
182                 push @messages, { type => 'error', code => 'no_biblio_removed' };
183             }
184         } else {
185             push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
186         }
187     } else {
188         push @messages, { type => 'error', code => 'does_not_exist' };
189     }
190     $op = $referer;
191 }
192
193 if ( $op eq 'view' ) {
194     $shelfnumber ||= $query->param('shelfnumber');
195     $shelf = Koha::Virtualshelves->find($shelfnumber);
196     if ( $shelf ) {
197         if ( $shelf->can_be_viewed( $loggedinuser ) ) {
198             my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title';    # Passed in sorting overrides default sorting
199             $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
200             my $direction = $query->param('direction') || 'asc';
201             $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
202             my ( $rows, $page );
203             unless ( $query->param('print') ) {
204                 $rows = C4::Context->preference('numSearchResults') || 20;
205                 $page = ( $query->param('page') ? $query->param('page') : 1 );
206             }
207
208             my $order_by = $sortfield eq 'itemcallnumber' ? 'items.itemcallnumber' : $sortfield;
209             my $contents = $shelf->get_contents->search(
210                 {},
211                 {
212                     prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
213                     page     => $page,
214                     rows     => $rows,
215                     order_by => { "-$direction" => $order_by },
216                 }
217             );
218
219             my $borrower = GetMember( borrowernumber => $loggedinuser );
220
221             my @items;
222             while ( my $content = $contents->next ) {
223                 my $this_item;
224                 my $biblionumber = $content->biblionumber->biblionumber;
225                 my $record       = GetMarcBiblio($biblionumber);
226
227                 if ( C4::Context->preference("XSLTResultsDisplay") ) {
228                     $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "XSLTResultsDisplay" );
229                 }
230
231                 my $marcflavour = C4::Context->preference("marcflavour");
232                 my $itemtypeinfo = getitemtypeinfo( $content->biblionumber->biblioitems->first->itemtype, 'intranet' );
233                 $this_item->{title}             = $content->biblionumber->title;
234                 $this_item->{author}            = $content->biblionumber->author;
235                 $this_item->{dateadded}         = $content->dateadded;
236                 $this_item->{imageurl}          = $itemtypeinfo->{imageurl};
237                 $this_item->{description}       = $itemtypeinfo->{description};
238                 $this_item->{notforloan}        = $itemtypeinfo->{notforloan};
239                 $this_item->{'coins'}           = GetCOinSBiblio($record);
240                 $this_item->{'subtitle'}        = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
241                 $this_item->{'normalized_upc'}  = GetNormalizedUPC( $record, $marcflavour );
242                 $this_item->{'normalized_ean'}  = GetNormalizedEAN( $record, $marcflavour );
243                 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
244                 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
245
246                 unless ( defined $this_item->{size} ) {
247
248                     #TT has problems with size
249                     $this_item->{size} = q||;
250                 }
251
252                 # Getting items infos for location display
253                 my @items_infos = &GetItemsLocationInfo( $biblionumber );
254                 $this_item->{'ITEM_RESULTS'} = \@items_infos;
255                 $this_item->{biblionumber} = $biblionumber;
256                 push @items, $this_item;
257             }
258
259             my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
260                 {
261                     borrowernumber => $loggedinuser,
262                     add_allowed    => 1,
263                     category       => 1,
264                 }
265             );
266             my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
267                 {
268                     borrowernumber => $loggedinuser,
269                     add_allowed    => 1,
270                     category       => 2,
271                 }
272             );
273
274             $template->param(
275                 add_to_some_private_shelves => $some_private_shelves,
276                 add_to_some_public_shelves  => $some_public_shelves,
277                 can_manage_shelf   => $shelf->can_be_managed($loggedinuser),
278                 can_remove_shelf   => $shelf->can_be_deleted($loggedinuser),
279                 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
280                 can_add_biblios    => $shelf->can_biblios_be_added($loggedinuser),
281                 sortfield          => $sortfield,
282                 itemsloop          => \@items,
283                 sortfield          => $sortfield,
284                 direction          => $direction,
285             );
286             if ( $page ) {
287                 my $pager = $contents->pager;
288                 $template->param(
289                     pagination_bar => pagination_bar(
290                         q||, $pager->last_page - $pager->first_page + 1,
291                         $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
292                     ),
293                 );
294             }
295         } else {
296             push @messages, { type => 'error', code => 'unauthorized_on_view' };
297             undef $shelf;
298         }
299     } else {
300         push @messages, { type => 'error', code => 'does_not_exist' };
301     }
302 }
303
304 $template->param(
305     op       => $op,
306     referer  => $referer,
307     shelf    => $shelf,
308     messages => \@messages,
309     category => $category,
310     print    => scalar $query->param('print') || 0,
311     csv_profiles => GetCsvProfilesLoop('marc'),
312 );
313
314 output_html_with_http_headers $query, $cookie, $template->output;