Bug 9259: Use is instead of is_deeply
[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         if ( $shelf->can_be_managed( $loggedinuser ) ) {
95             $shelf->shelfname( scalar $query->param('shelfname') );
96             $shelf->sortfield( scalar $query->param('sortfield') );
97             $shelf->allow_add( scalar $query->param('allow_add') );
98             $shelf->allow_delete_own( scalar $query->param('allow_delete_own') );
99             $shelf->allow_delete_other( scalar $query->param('allow_delete_other') );
100             $shelf->category( scalar $query->param('category') );
101             eval { $shelf->store };
102
103             if ($@) {
104                 push @messages, { type => 'error', code => 'error_on_update' };
105                 $op = 'edit_form';
106             } else {
107                 push @messages, { type => 'message', code => 'success_on_update' };
108             }
109         } else {
110             push @messages, { type => 'error', code => 'unauthorized_on_update' };
111         }
112     } else {
113         push @messages, { type => 'error', code => 'does_not_exist' };
114     }
115 } elsif ( $op eq 'delete' ) {
116     $shelfnumber = $query->param('shelfnumber');
117     $shelf       = Koha::Virtualshelves->find($shelfnumber);
118     if ($shelf) {
119         if ( $shelf->can_be_deleted( $loggedinuser ) ) {
120             eval { $shelf->delete; };
121             if ($@) {
122                 push @messages, { type => 'error', code => ref($@), msg => $@ };
123             } else {
124                 push @messages, { type => 'message', code => 'success_on_delete' };
125             }
126         } else {
127             push @messages, { type => 'error', code => 'unauthorized_on_delete' };
128         }
129     } else {
130         push @messages, { type => 'error', code => 'does_not_exist' };
131     }
132     $op = 'list';
133 } elsif ( $op eq 'add_biblio' ) {
134     $shelfnumber = $query->param('shelfnumber');
135     $shelf = Koha::Virtualshelves->find($shelfnumber);
136     if ($shelf) {
137         if( my $barcode = $query->param('barcode') ) {
138             my $item = GetItem( 0, $barcode);
139             if (defined $item && $item->{itemnumber}) {
140                 my $biblio = GetBiblioFromItemNumber( $item->{itemnumber} );
141                 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
142                     my $added = eval { $shelf->add_biblio( $biblio->{biblionumber}, $loggedinuser ); };
143                     if ($@) {
144                         push @messages, { type => 'error', code => ref($@), msg => $@ };
145                     } elsif ( $added ) {
146                         push @messages, { type => 'message', code => 'success_on_add_biblio' };
147                     } else {
148                         push @messages, { type => 'message', code => 'error_on_add_biblio' };
149                     }
150                 } else {
151                     push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
152                 }
153             } else {
154                 push @messages, { type => 'error', code => 'item_does_not_exist' };
155             }
156         }
157     } else {
158         push @messages, { type => 'error', code => 'does_not_exist' };
159     }
160     $op = $referer;
161 } elsif ( $op eq 'remove_biblios' ) {
162     $shelfnumber = $query->param('shelfnumber');
163     $shelf = Koha::Virtualshelves->find($shelfnumber);
164     my @biblionumbers = $query->multi_param('biblionumber');
165     if ($shelf) {
166         if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
167             my $number_of_biblios_removed = eval {
168                 $shelf->remove_biblios(
169                     {
170                         biblionumbers => \@biblionumbers,
171                         borrowernumber => $loggedinuser,
172                     }
173                 );
174             };
175             if ($@) {
176                 push @messages, { type => 'error', code => ref($@), msg => $@ };
177             } elsif ( $number_of_biblios_removed ) {
178                 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
179             } else {
180                 push @messages, { type => 'error', code => 'no_biblio_removed' };
181             }
182         } else {
183             push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
184         }
185     } else {
186         push @messages, { type => 'error', code => 'does_not_exist' };
187     }
188     $op = $referer;
189 }
190
191 if ( $op eq 'view' ) {
192     $shelfnumber ||= $query->param('shelfnumber');
193     $shelf = Koha::Virtualshelves->find($shelfnumber);
194     if ( $shelf ) {
195         if ( $shelf->can_be_viewed( $loggedinuser ) ) {
196             my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title';    # Passed in sorting overrides default sorting
197             my $direction = $query->param('direction') || 'asc';
198             $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
199             my ( $rows, $page );
200             unless ( $query->param('print') ) {
201                 $rows = C4::Context->preference('numSearchResults') || 20;
202                 $page = ( $query->param('page') ? $query->param('page') : 1 );
203             }
204
205             my $order_by = $sortfield eq 'itemcallnumber' ? 'items.itemcallnumber' : $sortfield;
206             my $contents = $shelf->get_contents->search(
207                 {},
208                 {
209                     prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
210                     page     => $page,
211                     rows     => $rows,
212                     order_by => { "-$direction" => $order_by },
213                 }
214             );
215
216             my $borrower = GetMember( borrowernumber => $loggedinuser );
217
218             my $xslfile = C4::Context->preference('XSLTListsDisplay');
219             my $lang   = $xslfile ? C4::Languages::getlanguage()  : undef;
220             my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
221
222             my @items;
223             while ( my $content = $contents->next ) {
224                 my $this_item;
225                 my $biblionumber = $content->biblionumber->biblionumber;
226                 my $record       = GetMarcBiblio($biblionumber);
227
228                 if ( $xslfile ) {
229                     $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "XSLTListsDisplay",
230                                                                 1, undef, $sysxml, $xslfile, $lang);
231                 }
232
233                 my $marcflavour = C4::Context->preference("marcflavour");
234                 my $itemtypeinfo = getitemtypeinfo( $content->biblionumber->biblioitems->first->itemtype, 'intranet' );
235                 $this_item->{title}             = $content->biblionumber->title;
236                 $this_item->{author}            = $content->biblionumber->author;
237                 $this_item->{dateadded}         = $content->dateadded;
238                 $this_item->{imageurl}          = $itemtypeinfo->{imageurl};
239                 $this_item->{description}       = $itemtypeinfo->{description};
240                 $this_item->{notforloan}        = $itemtypeinfo->{notforloan};
241                 $this_item->{'coins'}           = GetCOinSBiblio($record);
242                 $this_item->{'subtitle'}        = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
243                 $this_item->{'normalized_upc'}  = GetNormalizedUPC( $record, $marcflavour );
244                 $this_item->{'normalized_ean'}  = GetNormalizedEAN( $record, $marcflavour );
245                 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
246                 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
247
248                 unless ( defined $this_item->{size} ) {
249
250                     #TT has problems with size
251                     $this_item->{size} = q||;
252                 }
253
254                 # Getting items infos for location display
255                 my @items_infos = &GetItemsLocationInfo( $biblionumber );
256                 $this_item->{'ITEM_RESULTS'} = \@items_infos;
257                 $this_item->{biblionumber} = $biblionumber;
258                 push @items, $this_item;
259             }
260
261             my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
262                 {
263                     borrowernumber => $loggedinuser,
264                     add_allowed    => 1,
265                     category       => 1,
266                 }
267             );
268             my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
269                 {
270                     borrowernumber => $loggedinuser,
271                     add_allowed    => 1,
272                     category       => 2,
273                 }
274             );
275
276             $template->param(
277                 add_to_some_private_shelves => $some_private_shelves,
278                 add_to_some_public_shelves  => $some_public_shelves,
279                 can_manage_shelf   => $shelf->can_be_managed($loggedinuser),
280                 can_remove_shelf   => $shelf->can_be_deleted($loggedinuser),
281                 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
282                 can_add_biblios    => $shelf->can_biblios_be_added($loggedinuser),
283                 sortfield          => $sortfield,
284                 itemsloop          => \@items,
285                 sortfield          => $sortfield,
286                 direction          => $direction,
287             );
288             if ( $page ) {
289                 my $pager = $contents->pager;
290                 $template->param(
291                     pagination_bar => pagination_bar(
292                         q||, $pager->last_page - $pager->first_page + 1,
293                         $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
294                     ),
295                 );
296             }
297         } else {
298             push @messages, { type => 'error', code => 'unauthorized_on_view' };
299         }
300     } else {
301         push @messages, { type => 'error', code => 'does_not_exist' };
302     }
303 }
304
305 $template->param(
306     op       => $op,
307     referer  => $referer,
308     shelf    => $shelf,
309     messages => \@messages,
310     category => $category,
311     print    => scalar $query->param('print') || 0,
312     csv_profiles => GetCsvProfilesLoop('marc'),
313 );
314
315 output_html_with_http_headers $query, $cookie, $template->output;