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
;
35 use Koha
::Virtualshelves
;
37 use constant ANYONE
=> 2;
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
42 { template_name
=> "virtualshelves/shelves.tt",
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 $category = 2 if $category ne "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 {/^$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 = GetItem
( 0, $barcode);
156 if (defined $item && $item->{itemnumber
}) {
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 {/^$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->{author
} = $biblio->author;
278 $this_item->{dateadded
} = $content->dateadded;
279 $this_item->{imageurl
} = $itemtype ? C4
::Koha
::getitemtypeimagelocation
( 'intranet', $itemtype->imageurl ) : q{};
280 $this_item->{description
} = $itemtype ?
$itemtype->description : q{}; #FIXME Should this be translated_description ?
281 $this_item->{notforloan
} = $itemtype->notforloan if $itemtype;
282 $this_item->{'coins'} = GetCOinSBiblio
($record);
283 $this_item->{'subtitle'} = GetRecordValue
( 'subtitle', $record, GetFrameworkCode
( $biblionumber ) );
284 $this_item->{'normalized_upc'} = GetNormalizedUPC
( $record, $marcflavour );
285 $this_item->{'normalized_ean'} = GetNormalizedEAN
( $record, $marcflavour );
286 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber
( $record, $marcflavour );
287 $this_item->{'normalized_isbn'} = GetNormalizedISBN
( undef, $record, $marcflavour );
289 unless ( defined $this_item->{size
} ) {
291 #TT has problems with size
292 $this_item->{size
} = q
||;
295 # Getting items infos for location display
296 my @items_infos = &GetItemsLocationInfo
( $biblionumber );
297 $this_item->{'ITEM_RESULTS'} = \
@items_infos;
298 $this_item->{biblionumber
} = $biblionumber;
299 push @items, $this_item;
302 my $some_private_shelves = Koha
::Virtualshelves
->get_some_shelves(
304 borrowernumber
=> $loggedinuser,
309 my $some_public_shelves = Koha
::Virtualshelves
->get_some_shelves(
311 borrowernumber
=> $loggedinuser,
318 add_to_some_private_shelves
=> $some_private_shelves,
319 add_to_some_public_shelves
=> $some_public_shelves,
320 can_manage_shelf
=> $shelf->can_be_managed($loggedinuser),
321 can_remove_shelf
=> $shelf->can_be_deleted($loggedinuser),
322 can_remove_biblios
=> $shelf->can_biblios_be_removed($loggedinuser),
323 can_add_biblios
=> $shelf->can_biblios_be_added($loggedinuser),
324 sortfield
=> $sortfield,
325 itemsloop
=> \
@items,
326 sortfield
=> $sortfield,
327 direction
=> $direction,
330 my $pager = $contents->pager;
332 pagination_bar
=> pagination_bar
(
333 q
||, $pager->last_page - $pager->first_page + 1,
334 $page, "page", { op
=> 'view', shelfnumber
=> $shelf->shelfnumber, sortfield
=> $sortfield, direction
=> $direction, }
339 push @messages, { type
=> 'error', code
=> 'unauthorized_on_view' };
343 push @messages, { type
=> 'alert', code
=> 'does_not_exist' };
351 messages
=> \
@messages,
352 category
=> $category,
353 print => scalar $query->param('print') || 0,
354 csv_profiles
=> [ Koha
::CsvProfiles
->search({ type
=> 'marc', used_for
=> 'export_records' }) ],
357 output_html_with_http_headers
$query, $cookie, $template->output;