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 $biblio = GetBiblioFromItemNumber( $item->{itemnumber} );
156 my $added = eval { $shelf->add_biblio( $biblio->{biblionumber}, $loggedinuser ); };
158 push @messages, { item_barcode => $barcode, type => 'alert', code => ref($@), msg => $@ };
160 push @messages, { item_barcode => $barcode, type => 'message', code => 'success_on_add_biblio' };
162 push @messages, { item_barcode => $barcode, type => 'message', code => 'error_on_add_biblio' };
165 push @messages, { item_barcode => $barcode, type => 'alert', code => 'item_does_not_exist' };
169 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
173 push @messages, { type => 'alert', code => 'does_not_exist' };
176 } elsif ( $op eq 'remove_biblios' ) {
177 $shelfnumber = $query->param('shelfnumber');
178 $shelf = Koha::Virtualshelves->find($shelfnumber);
179 my @biblionumbers = $query->multi_param('biblionumber');
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,
191 push @messages, { type => 'alert', code => ref($@), msg => $@ };
192 } elsif ( $number_of_biblios_removed ) {
193 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
195 push @messages, { type => 'alert', code => 'no_biblio_removed' };
198 push @messages, { type => 'alert', code => 'unauthorized_on_remove_biblios' };
201 push @messages, { type => 'alert', code => 'does_not_exist' };
206 if ( $op eq 'view' ) {
207 $shelfnumber ||= $query->param('shelfnumber');
208 $shelf = Koha::Virtualshelves->find($shelfnumber);
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';
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' } } ],
228 order_by => { "-$direction" => $order_by },
232 my $borrower = GetMember( borrowernumber => $loggedinuser );
234 my $xslfile = C4::Context->preference('XSLTListsDisplay');
235 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
236 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
239 while ( my $content = $contents->next ) {
241 my $biblionumber = $content->biblionumber;
242 my $record = GetMarcBiblio($biblionumber);
245 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "XSLTListsDisplay",
246 1, undef, $sysxml, $xslfile, $lang);
249 my $marcflavour = C4::Context->preference("marcflavour");
250 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
251 $itemtype = Koha::ItemTypes->find( $itemtype );
252 my $biblio = Koha::Biblios->find( $content->biblionumber );
253 $this_item->{title} = $biblio->title;
254 $this_item->{author} = $biblio->author;
255 $this_item->{dateadded} = $content->dateadded;
256 $this_item->{imageurl} = $itemtype ? C4::Koha::getitemtypeimagelocation( 'intranet', $itemtype->imageurl ) : q{};
257 $this_item->{description} = $itemtype ? $itemtype->description : q{}; #FIXME Should this be translated_description ?
258 $this_item->{notforloan} = $itemtype->notforloan if $itemtype;
259 $this_item->{'coins'} = GetCOinSBiblio($record);
260 $this_item->{'subtitle'} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
261 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
262 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
263 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
264 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
266 unless ( defined $this_item->{size} ) {
268 #TT has problems with size
269 $this_item->{size} = q||;
272 # Getting items infos for location display
273 my @items_infos = &GetItemsLocationInfo( $biblionumber );
274 $this_item->{'ITEM_RESULTS'} = \@items_infos;
275 $this_item->{biblionumber} = $biblionumber;
276 push @items, $this_item;
279 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
281 borrowernumber => $loggedinuser,
286 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
288 borrowernumber => $loggedinuser,
295 add_to_some_private_shelves => $some_private_shelves,
296 add_to_some_public_shelves => $some_public_shelves,
297 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
298 can_remove_shelf => $shelf->can_be_deleted($loggedinuser),
299 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
300 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
301 sortfield => $sortfield,
302 itemsloop => \@items,
303 sortfield => $sortfield,
304 direction => $direction,
307 my $pager = $contents->pager;
309 pagination_bar => pagination_bar(
310 q||, $pager->last_page - $pager->first_page + 1,
311 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
316 push @messages, { type => 'error', code => 'unauthorized_on_view' };
320 push @messages, { type => 'alert', code => 'does_not_exist' };
328 messages => \@messages,
329 category => $category,
330 print => scalar $query->param('print') || 0,
331 csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc', used_for => 'export_records' }) ],
334 output_html_with_http_headers $query, $cookie, $template->output;