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>.
31 use Koha
::Biblioitems
;
34 use Koha
::CsvProfiles
;
36 use Koha
::Virtualshelves
;
38 use constant ANYONE
=> 2;
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
43 { template_name
=> "virtualshelves/shelves.tt",
47 flagsrequired
=> { catalogue
=> 1 },
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 );
56 if ( $op eq 'add_form' ) {
58 $shelf = { allow_change_from_owner
=> 1 };
59 } elsif ( $op eq 'edit_form' ) {
60 $shelfnumber = $query->param('shelfnumber');
61 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
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' };
72 push @messages, { type
=> 'alert', code
=> 'does_not_exist' };
74 } elsif ( $op eq 'add' ) {
75 my $allow_changes_from = $query->param('allow_changes_from');
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'),
87 $shelfnumber = $shelf->shelfnumber;
90 push @messages, { type
=> 'alert', code
=> ref($@
), msg
=> $@
};
91 } elsif ( not $shelf ) {
92 push @messages, { type
=> 'alert', code
=> 'error_on_insert' };
95 push @messages, { type
=> 'message', code
=> 'success_on_insert' };
98 } elsif ( $op eq 'edit' ) {
99 $shelfnumber = $query->param('shelfnumber');
100 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
104 my $sortfield = $query->param('sortfield');
105 $sortfield = 'title' unless grep { $_ eq $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 };
116 push @messages, { type
=> 'alert', code
=> 'error_on_update' };
119 push @messages, { type
=> 'message', code
=> 'success_on_update' };
122 push @messages, { type
=> 'alert', code
=> 'unauthorized_on_update' };
125 push @messages, { type
=> 'alert', code
=> 'does_not_exist' };
127 } elsif ( $op eq 'delete' ) {
128 $shelfnumber = $query->param('shelfnumber');
129 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
131 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
132 eval { $shelf->delete; };
134 push @messages, { type
=> 'alert', code
=> ref($@
), msg
=> $@
};
136 push @messages, { type
=> 'message', code
=> 'success_on_delete' };
139 push @messages, { type
=> 'alert', code
=> 'unauthorized_on_delete' };
142 push @messages, { type
=> 'alert', code
=> 'does_not_exist' };
145 } elsif ( $op eq 'add_biblio' ) {
146 $shelfnumber = $query->param('shelfnumber');
147 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
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});
157 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
159 push @messages, { item_barcode
=> $barcode, type
=> 'alert', code
=> ref($@
), msg
=> $@
};
161 push @messages, { item_barcode
=> $barcode, type
=> 'message', code
=> 'success_on_add_biblio' };
163 push @messages, { item_barcode
=> $barcode, type
=> 'message', code
=> 'error_on_add_biblio' };
166 push @messages, { item_barcode
=> $barcode, type
=> 'alert', code
=> 'item_does_not_exist' };
170 push @messages, { type
=> 'alert', code
=> 'unauthorized_on_add_biblio' };
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 ); };
183 push @messages, { bibnum
=> $biblionumber, type
=> 'alert', code
=> ref($@
), msg
=> $@
};
185 push @messages, { bibnum
=> $biblionumber, type
=> 'message', code
=> 'success_on_add_biblio' };
187 push @messages, { bibnum
=> $biblionumber, type
=> 'message', code
=> 'error_on_add_biblio' };
190 push @messages, { bibnum
=> $biblionumber, type
=> 'alert', code
=> 'item_does_not_exist' };
194 push @messages, { type
=> 'alert', code
=> 'unauthorized_on_add_biblio' };
198 push @messages, { type
=> 'alert', code
=> 'does_not_exist' };
201 } elsif ( $op eq 'remove_biblios' ) {
202 $shelfnumber = $query->param('shelfnumber');
203 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
204 my @biblionumbers = $query->multi_param('biblionumber');
206 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
207 my $number_of_biblios_removed = eval {
208 $shelf->remove_biblios(
210 biblionumbers
=> \
@biblionumbers,
211 borrowernumber
=> $loggedinuser,
216 push @messages, { type
=> 'alert', code
=> ref($@
), msg
=> $@
};
217 } elsif ( $number_of_biblios_removed ) {
218 push @messages, { type
=> 'message', code
=> 'success_on_remove_biblios' };
220 push @messages, { type
=> 'alert', code
=> 'no_biblio_removed' };
223 push @messages, { type
=> 'alert', code
=> 'unauthorized_on_remove_biblios' };
226 push @messages, { type
=> 'alert', code
=> 'does_not_exist' };
231 if ( $op eq 'view' ) {
232 $shelfnumber ||= $query->param('shelfnumber');
233 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
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';
241 unless ( $query->param('print') ) {
242 $rows = C4
::Context
->preference('numSearchResults') || 20;
243 $page = ( $query->param('page') ?
$query->param('page') : 1 );
246 my $order_by = $sortfield eq 'itemcallnumber' ?
'items.cn_sort' : $sortfield;
247 my $contents = $shelf->get_contents->search(
250 prefetch
=> [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
253 order_by
=> { "-$direction" => $order_by },
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;
262 while ( my $content = $contents->next ) {
264 my $biblionumber = $content->biblionumber;
265 my $record = GetMarcBiblio
({ biblionumber
=> $biblionumber });
268 $this_item->{XSLTBloc
} = XSLTParse4Display
( $biblionumber, $record, "XSLTListsDisplay",
269 1, undef, $sysxml, $xslfile, $lang);
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->{subtitle
} = $biblio->subtitle;
278 $this_item->{medium
} = $biblio->medium;
279 $this_item->{part_number
} = $biblio->part_number;
280 $this_item->{part_name
} = $biblio->part_name;
281 $this_item->{author
} = $biblio->author;
282 $this_item->{dateadded
} = $content->dateadded;
283 $this_item->{imageurl
} = $itemtype ? C4
::Koha
::getitemtypeimagelocation
( 'intranet', $itemtype->imageurl ) : q{};
284 $this_item->{description
} = $itemtype ?
$itemtype->description : q{}; #FIXME Should this be translated_description ?
285 $this_item->{notforloan
} = $itemtype->notforloan if $itemtype;
286 $this_item->{'coins'} = $biblio->get_coins;
287 $this_item->{'normalized_upc'} = GetNormalizedUPC
( $record, $marcflavour );
288 $this_item->{'normalized_ean'} = GetNormalizedEAN
( $record, $marcflavour );
289 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber
( $record, $marcflavour );
290 $this_item->{'normalized_isbn'} = GetNormalizedISBN
( undef, $record, $marcflavour );
292 unless ( defined $this_item->{size
} ) {
294 #TT has problems with size
295 $this_item->{size
} = q
||;
298 # Getting items infos for location display
299 my @items_infos = &GetItemsLocationInfo
( $biblionumber );
300 $this_item->{'ITEM_RESULTS'} = \
@items_infos;
301 $this_item->{biblionumber
} = $biblionumber;
302 push @items, $this_item;
305 my $some_private_shelves = Koha
::Virtualshelves
->get_some_shelves(
307 borrowernumber
=> $loggedinuser,
312 my $some_public_shelves = Koha
::Virtualshelves
->get_some_shelves(
314 borrowernumber
=> $loggedinuser,
321 add_to_some_private_shelves
=> $some_private_shelves,
322 add_to_some_public_shelves
=> $some_public_shelves,
323 can_manage_shelf
=> $shelf->can_be_managed($loggedinuser),
324 can_remove_shelf
=> $shelf->can_be_deleted($loggedinuser),
325 can_remove_biblios
=> $shelf->can_biblios_be_removed($loggedinuser),
326 can_add_biblios
=> $shelf->can_biblios_be_added($loggedinuser),
327 sortfield
=> $sortfield,
328 itemsloop
=> \
@items,
329 sortfield
=> $sortfield,
330 direction
=> $direction,
333 my $pager = $contents->pager;
335 pagination_bar
=> pagination_bar
(
336 q
||, $pager->last_page - $pager->first_page + 1,
337 $page, "page", { op
=> 'view', shelfnumber
=> $shelf->shelfnumber, sortfield
=> $sortfield, direction
=> $direction, }
342 push @messages, { type
=> 'error', code
=> 'unauthorized_on_view' };
346 push @messages, { type
=> 'alert', code
=> 'does_not_exist' };
354 messages
=> \
@messages,
355 category
=> $category,
356 print => scalar $query->param('print') || 0,
357 csv_profiles
=> [ Koha
::CsvProfiles
->search({ type
=> 'marc', used_for
=> 'export_records' }) ],
360 output_html_with_http_headers
$query, $cookie, $template->output;