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;
33 use Koha::CsvProfiles;
34 use Koha::Virtualshelves;
36 use constant ANYONE => 2;
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41 { template_name => "virtualshelves/shelves.tt",
45 flagsrequired => { catalogue => 1 },
49 my $op = $query->param('op') || 'list';
50 my $referer = $query->param('referer') || $op;
51 my $category = $query->param('category') || 1;
52 my ( $shelf, $shelfnumber, @messages );
54 if ( $op eq 'add_form' ) {
56 $shelf = { allow_change_from_owner => 1 };
57 } elsif ( $op eq 'edit_form' ) {
58 $shelfnumber = $query->param('shelfnumber');
59 $shelf = Koha::Virtualshelves->find($shelfnumber);
62 $category = $shelf->category;
63 my $patron = GetMember( 'borrowernumber' => $shelf->owner );
64 $template->param( owner => $patron, );
65 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
66 push @messages, { type => 'alert', code => 'unauthorized_on_update' };
70 push @messages, { type => 'alert', code => 'does_not_exist' };
72 } elsif ( $op eq 'add' ) {
73 my $allow_changes_from = $query->param('allow_changes_from');
75 $shelf = Koha::Virtualshelf->new(
76 { shelfname => scalar $query->param('shelfname'),
77 sortfield => scalar $query->param('sortfield'),
78 category => scalar $query->param('category'),
79 allow_change_from_owner => $allow_changes_from > 0,
80 allow_change_from_others => $allow_changes_from == ANYONE,
81 owner => scalar $query->param('owner'),
85 $shelfnumber = $shelf->shelfnumber;
88 push @messages, { type => 'alert', code => ref($@), msg => $@ };
89 } elsif ( not $shelf ) {
90 push @messages, { type => 'alert', code => 'error_on_insert' };
93 push @messages, { type => 'message', code => 'success_on_insert' };
96 } elsif ( $op eq 'edit' ) {
97 $shelfnumber = $query->param('shelfnumber');
98 $shelf = Koha::Virtualshelves->find($shelfnumber);
102 my $sortfield = $query->param('sortfield');
103 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
104 if ( $shelf->can_be_managed( $loggedinuser ) ) {
105 $shelf->shelfname( scalar $query->param('shelfname') );
106 $shelf->sortfield( $sortfield );
107 my $allow_changes_from = $query->param('allow_changes_from');
108 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
109 $shelf->allow_change_from_others( $allow_changes_from == ANYONE );
110 $shelf->category( scalar $query->param('category') );
111 eval { $shelf->store };
114 push @messages, { type => 'alert', code => 'error_on_update' };
117 push @messages, { type => 'message', code => 'success_on_update' };
120 push @messages, { type => 'alert', code => 'unauthorized_on_update' };
123 push @messages, { type => 'alert', code => 'does_not_exist' };
125 } elsif ( $op eq 'delete' ) {
126 $shelfnumber = $query->param('shelfnumber');
127 $shelf = Koha::Virtualshelves->find($shelfnumber);
129 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
130 eval { $shelf->delete; };
132 push @messages, { type => 'alert', code => ref($@), msg => $@ };
134 push @messages, { type => 'message', code => 'success_on_delete' };
137 push @messages, { type => 'alert', code => 'unauthorized_on_delete' };
140 push @messages, { type => 'alert', code => 'does_not_exist' };
143 } elsif ( $op eq 'add_biblio' ) {
144 $shelfnumber = $query->param('shelfnumber');
145 $shelf = Koha::Virtualshelves->find($shelfnumber);
147 if( my $barcodes = $query->param('barcodes') ) {
148 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
149 my @barcodes = split /\n/, $barcodes; # Entries are effectively passed in as a <cr> separated list
150 foreach my $barcode (@barcodes){
151 $barcode =~ s/\r$//; # strip any naughty return chars
152 next if $barcode eq '';
153 my $item = GetItem( 0, $barcode);
154 if (defined $item && $item->{itemnumber}) {
155 my $added = eval { $shelf->add_biblio( $item->{biblionumber}, $loggedinuser ); };
157 push @messages, { item_barcode => $barcode, type => 'alert', code => ref($@), msg => $@ };
159 push @messages, { item_barcode => $barcode, type => 'message', code => 'success_on_add_biblio' };
161 push @messages, { item_barcode => $barcode, type => 'message', code => 'error_on_add_biblio' };
164 push @messages, { item_barcode => $barcode, type => 'alert', code => 'item_does_not_exist' };
168 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
172 push @messages, { type => 'alert', code => 'does_not_exist' };
175 } elsif ( $op eq 'remove_biblios' ) {
176 $shelfnumber = $query->param('shelfnumber');
177 $shelf = Koha::Virtualshelves->find($shelfnumber);
178 my @biblionumbers = $query->multi_param('biblionumber');
180 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
181 my $number_of_biblios_removed = eval {
182 $shelf->remove_biblios(
184 biblionumbers => \@biblionumbers,
185 borrowernumber => $loggedinuser,
190 push @messages, { type => 'alert', code => ref($@), msg => $@ };
191 } elsif ( $number_of_biblios_removed ) {
192 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
194 push @messages, { type => 'alert', code => 'no_biblio_removed' };
197 push @messages, { type => 'alert', code => 'unauthorized_on_remove_biblios' };
200 push @messages, { type => 'alert', code => 'does_not_exist' };
205 if ( $op eq 'view' ) {
206 $shelfnumber ||= $query->param('shelfnumber');
207 $shelf = Koha::Virtualshelves->find($shelfnumber);
209 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
210 my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title'; # Passed in sorting overrides default sorting
211 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
212 my $direction = $query->param('direction') || 'asc';
213 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
215 unless ( $query->param('print') ) {
216 $rows = C4::Context->preference('numSearchResults') || 20;
217 $page = ( $query->param('page') ? $query->param('page') : 1 );
220 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.itemcallnumber' : $sortfield;
221 my $contents = $shelf->get_contents->search(
224 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
227 order_by => { "-$direction" => $order_by },
231 my $borrower = GetMember( borrowernumber => $loggedinuser );
233 my $xslfile = C4::Context->preference('XSLTListsDisplay');
234 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
235 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
238 while ( my $content = $contents->next ) {
240 my $biblionumber = $content->biblionumber;
241 my $record = GetMarcBiblio($biblionumber);
244 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "XSLTListsDisplay",
245 1, undef, $sysxml, $xslfile, $lang);
248 my $marcflavour = C4::Context->preference("marcflavour");
249 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
250 $itemtype = Koha::ItemTypes->find( $itemtype );
251 my $biblio = Koha::Biblios->find( $content->biblionumber );
252 $this_item->{title} = $biblio->title;
253 $this_item->{author} = $biblio->author;
254 $this_item->{dateadded} = $content->dateadded;
255 $this_item->{imageurl} = $itemtype ? C4::Koha::getitemtypeimagelocation( 'intranet', $itemtype->imageurl ) : q{};
256 $this_item->{description} = $itemtype ? $itemtype->description : q{}; #FIXME Should this be translated_description ?
257 $this_item->{notforloan} = $itemtype->notforloan if $itemtype;
258 $this_item->{'coins'} = GetCOinSBiblio($record);
259 $this_item->{'subtitle'} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
260 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
261 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
262 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
263 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
265 unless ( defined $this_item->{size} ) {
267 #TT has problems with size
268 $this_item->{size} = q||;
271 # Getting items infos for location display
272 my @items_infos = &GetItemsLocationInfo( $biblionumber );
273 $this_item->{'ITEM_RESULTS'} = \@items_infos;
274 $this_item->{biblionumber} = $biblionumber;
275 push @items, $this_item;
278 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
280 borrowernumber => $loggedinuser,
285 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
287 borrowernumber => $loggedinuser,
294 add_to_some_private_shelves => $some_private_shelves,
295 add_to_some_public_shelves => $some_public_shelves,
296 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
297 can_remove_shelf => $shelf->can_be_deleted($loggedinuser),
298 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
299 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
300 sortfield => $sortfield,
301 itemsloop => \@items,
302 sortfield => $sortfield,
303 direction => $direction,
306 my $pager = $contents->pager;
308 pagination_bar => pagination_bar(
309 q||, $pager->last_page - $pager->first_page + 1,
310 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
315 push @messages, { type => 'error', code => 'unauthorized_on_view' };
319 push @messages, { type => 'alert', code => 'does_not_exist' };
327 messages => \@messages,
328 category => $category,
329 print => scalar $query->param('print') || 0,
330 csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' }) ],
333 output_html_with_http_headers $query, $cookie, $template->output;