Bug 18927: Use fully qualified subroutine names in C4::Items
[koha.git] / virtualshelves / shelves.pl
blobd3aea7e8cdd0daf9b66d4cac67f7de1d874ceb43
1 #!/usr/bin/perl
3 # Copyright 2015 Koha Team
5 # This file is part of Koha.
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.
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.
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>.
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;
30 use Koha::Biblios;
31 use Koha::Biblioitems;
32 use Koha::ItemTypes;
33 use Koha::CsvProfiles;
34 use Koha::Patrons;
35 use Koha::Virtualshelves;
37 use constant ANYONE => 2;
39 my $query = new CGI;
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 },
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 );
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);
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';
70 } else {
71 push @messages, { type => 'alert', code => 'does_not_exist' };
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'),
85 $shelf->store;
86 $shelfnumber = $shelf->shelfnumber;
88 if ($@) {
89 push @messages, { type => 'alert', code => ref($@), msg => $@ };
90 } elsif ( not $shelf ) {
91 push @messages, { type => 'alert', code => 'error_on_insert' };
93 } else {
94 push @messages, { type => 'message', code => 'success_on_insert' };
95 $op = 'view';
97 } elsif ( $op eq 'edit' ) {
98 $shelfnumber = $query->param('shelfnumber');
99 $shelf = Koha::Virtualshelves->find($shelfnumber);
101 if ( $shelf ) {
102 $op = $referer;
103 my $sortfield = $query->param('sortfield');
104 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
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 };
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' };
120 } else {
121 push @messages, { type => 'alert', code => 'unauthorized_on_update' };
123 } else {
124 push @messages, { type => 'alert', code => 'does_not_exist' };
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' };
137 } else {
138 push @messages, { type => 'alert', code => 'unauthorized_on_delete' };
140 } else {
141 push @messages, { type => 'alert', code => 'does_not_exist' };
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' };
164 } else {
165 push @messages, { item_barcode => $barcode, type => 'alert', code => 'item_does_not_exist' };
168 } else {
169 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
172 } else {
173 push @messages, { type => 'alert', code => 'does_not_exist' };
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(
185 biblionumbers => \@biblionumbers,
186 borrowernumber => $loggedinuser,
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' };
197 } else {
198 push @messages, { type => 'alert', code => 'unauthorized_on_remove_biblios' };
200 } else {
201 push @messages, { type => 'alert', code => 'does_not_exist' };
203 $op = $referer;
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 );
221 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.itemcallnumber' : $sortfield;
222 my $contents = $shelf->get_contents->search(
225 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
226 page => $page,
227 rows => $rows,
228 order_by => { "-$direction" => $order_by },
232 my $xslfile = C4::Context->preference('XSLTListsDisplay');
233 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
234 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
236 my @items;
237 while ( my $content = $contents->next ) {
238 my $this_item;
239 my $biblionumber = $content->biblionumber;
240 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
242 if ( $xslfile ) {
243 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "XSLTListsDisplay",
244 1, undef, $sysxml, $xslfile, $lang);
247 my $marcflavour = C4::Context->preference("marcflavour");
248 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
249 $itemtype = Koha::ItemTypes->find( $itemtype );
250 my $biblio = Koha::Biblios->find( $content->biblionumber );
251 $this_item->{title} = $biblio->title;
252 $this_item->{author} = $biblio->author;
253 $this_item->{dateadded} = $content->dateadded;
254 $this_item->{imageurl} = $itemtype ? C4::Koha::getitemtypeimagelocation( 'intranet', $itemtype->imageurl ) : q{};
255 $this_item->{description} = $itemtype ? $itemtype->description : q{}; #FIXME Should this be translated_description ?
256 $this_item->{notforloan} = $itemtype->notforloan if $itemtype;
257 $this_item->{'coins'} = GetCOinSBiblio($record);
258 $this_item->{'subtitle'} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
259 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
260 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
261 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
262 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
264 unless ( defined $this_item->{size} ) {
266 #TT has problems with size
267 $this_item->{size} = q||;
270 # Getting items infos for location display
271 my @items_infos = &GetItemsLocationInfo( $biblionumber );
272 $this_item->{'ITEM_RESULTS'} = \@items_infos;
273 $this_item->{biblionumber} = $biblionumber;
274 push @items, $this_item;
277 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
279 borrowernumber => $loggedinuser,
280 add_allowed => 1,
281 category => 1,
284 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
286 borrowernumber => $loggedinuser,
287 add_allowed => 1,
288 category => 2,
292 $template->param(
293 add_to_some_private_shelves => $some_private_shelves,
294 add_to_some_public_shelves => $some_public_shelves,
295 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
296 can_remove_shelf => $shelf->can_be_deleted($loggedinuser),
297 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
298 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
299 sortfield => $sortfield,
300 itemsloop => \@items,
301 sortfield => $sortfield,
302 direction => $direction,
304 if ( $page ) {
305 my $pager = $contents->pager;
306 $template->param(
307 pagination_bar => pagination_bar(
308 q||, $pager->last_page - $pager->first_page + 1,
309 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
313 } else {
314 push @messages, { type => 'error', code => 'unauthorized_on_view' };
315 undef $shelf;
317 } else {
318 push @messages, { type => 'alert', code => 'does_not_exist' };
322 $template->param(
323 op => $op,
324 referer => $referer,
325 shelf => $shelf,
326 messages => \@messages,
327 category => $category,
328 print => scalar $query->param('print') || 0,
329 csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' }) ],
332 output_html_with_http_headers $query, $cookie, $template->output;