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 $category = 2 if $category ne "1";
55 my ( $shelf, $shelfnumber, @messages );
57 if ( $op eq 'add_form' ) {
59 $shelf = { allow_change_from_owner
=> 1 };
60 } elsif ( $op eq 'edit_form' ) {
61 $shelfnumber = $query->param('shelfnumber');
62 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
65 $category = $shelf->category;
66 my $patron = Koha
::Patrons
->find( $shelf->owner )->unblessed;
67 $template->param( owner
=> $patron, );
68 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
69 push @messages, { type
=> 'alert', code
=> 'unauthorized_on_update' };
73 push @messages, { type
=> 'alert', code
=> 'does_not_exist' };
75 } elsif ( $op eq 'add' ) {
76 my $allow_changes_from = $query->param('allow_changes_from');
78 $shelf = Koha
::Virtualshelf
->new(
79 { shelfname
=> scalar $query->param('shelfname'),
80 sortfield
=> scalar $query->param('sortfield'),
81 category
=> scalar $query->param('category'),
82 allow_change_from_owner
=> $allow_changes_from > 0,
83 allow_change_from_others
=> $allow_changes_from == ANYONE
,
84 owner
=> scalar $query->param('owner'),
88 $shelfnumber = $shelf->shelfnumber;
91 push @messages, { type
=> 'alert', code
=> ref($@
), msg
=> $@
};
92 } elsif ( not $shelf ) {
93 push @messages, { type
=> 'alert', code
=> 'error_on_insert' };
96 push @messages, { type
=> 'message', code
=> 'success_on_insert' };
99 } elsif ( $op eq 'edit' ) {
100 $shelfnumber = $query->param('shelfnumber');
101 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
105 my $sortfield = $query->param('sortfield');
106 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber dateadded );
107 if ( $shelf->can_be_managed( $loggedinuser ) ) {
108 $shelf->shelfname( scalar $query->param('shelfname') );
109 $shelf->sortfield( $sortfield );
110 my $allow_changes_from = $query->param('allow_changes_from');
111 $shelf->allow_change_from_owner( $allow_changes_from > 0 );
112 $shelf->allow_change_from_others( $allow_changes_from == ANYONE
);
113 $shelf->category( scalar $query->param('category') );
114 eval { $shelf->store };
117 push @messages, { type
=> 'alert', code
=> 'error_on_update' };
120 push @messages, { type
=> 'message', code
=> 'success_on_update' };
123 push @messages, { type
=> 'alert', code
=> 'unauthorized_on_update' };
126 push @messages, { type
=> 'alert', code
=> 'does_not_exist' };
128 } elsif ( $op eq 'delete' ) {
129 $shelfnumber = $query->param('shelfnumber');
130 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
132 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
133 eval { $shelf->delete; };
135 push @messages, { type
=> 'alert', code
=> ref($@
), msg
=> $@
};
137 push @messages, { type
=> 'message', code
=> 'success_on_delete' };
140 push @messages, { type
=> 'alert', code
=> 'unauthorized_on_delete' };
143 push @messages, { type
=> 'alert', code
=> 'does_not_exist' };
146 } elsif ( $op eq 'add_biblio' ) {
147 $shelfnumber = $query->param('shelfnumber');
148 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
150 if( my $barcodes = $query->param('barcodes') ) {
151 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
152 my @barcodes = split /\n/, $barcodes; # Entries are effectively passed in as a <cr> separated list
153 foreach my $barcode (@barcodes){
154 $barcode =~ s/\r$//; # strip any naughty return chars
155 next if $barcode eq '';
156 my $item = Koha
::Items
->find({barcode
=> $barcode});
158 my $added = eval { $shelf->add_biblio( $item->biblionumber, $loggedinuser ); };
160 push @messages, { item_barcode
=> $barcode, type
=> 'alert', code
=> ref($@
), msg
=> $@
};
162 push @messages, { item_barcode
=> $barcode, type
=> 'message', code
=> 'success_on_add_biblio' };
164 push @messages, { item_barcode
=> $barcode, type
=> 'message', code
=> 'error_on_add_biblio' };
167 push @messages, { item_barcode
=> $barcode, type
=> 'alert', code
=> 'item_does_not_exist' };
171 push @messages, { type
=> 'alert', code
=> 'unauthorized_on_add_biblio' };
174 if ( my $biblionumbers = $query->param('biblionumbers') ) {
175 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
176 my @biblionumbers = split /\n/, $biblionumbers;
177 foreach my $biblionumber (@biblionumbers) {
178 $biblionumber =~ s/\r$//; # strip any naughty return chars
179 next if $biblionumber eq '';
180 my $biblio = Koha
::Biblios
->find($biblionumber);
181 if (defined $biblio) {
182 my $added = eval { $shelf->add_biblio( $biblionumber, $loggedinuser ); };
184 push @messages, { bibnum
=> $biblionumber, type
=> 'alert', code
=> ref($@
), msg
=> $@
};
186 push @messages, { bibnum
=> $biblionumber, type
=> 'message', code
=> 'success_on_add_biblio' };
188 push @messages, { bibnum
=> $biblionumber, type
=> 'message', code
=> 'error_on_add_biblio' };
191 push @messages, { bibnum
=> $biblionumber, type
=> 'alert', code
=> 'item_does_not_exist' };
195 push @messages, { type
=> 'alert', code
=> 'unauthorized_on_add_biblio' };
199 push @messages, { type
=> 'alert', code
=> 'does_not_exist' };
202 } elsif ( $op eq 'remove_biblios' ) {
203 $shelfnumber = $query->param('shelfnumber');
204 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
205 my @biblionumbers = $query->multi_param('biblionumber');
207 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
208 my $number_of_biblios_removed = eval {
209 $shelf->remove_biblios(
211 biblionumbers
=> \
@biblionumbers,
212 borrowernumber
=> $loggedinuser,
217 push @messages, { type
=> 'alert', code
=> ref($@
), msg
=> $@
};
218 } elsif ( $number_of_biblios_removed ) {
219 push @messages, { type
=> 'message', code
=> 'success_on_remove_biblios' };
221 push @messages, { type
=> 'alert', code
=> 'no_biblio_removed' };
224 push @messages, { type
=> 'alert', code
=> 'unauthorized_on_remove_biblios' };
227 push @messages, { type
=> 'alert', code
=> 'does_not_exist' };
232 if ( $op eq 'view' ) {
233 $shelfnumber ||= $query->param('shelfnumber');
234 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
236 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
237 my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title'; # Passed in sorting overrides default sorting
238 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber dateadded );
239 my $direction = $query->param('direction') || 'asc';
240 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
242 unless ( $query->param('print') ) {
243 $rows = C4
::Context
->preference('numSearchResults') || 20;
244 $page = ( $query->param('page') ?
$query->param('page') : 1 );
247 my $order_by = $sortfield eq 'itemcallnumber' ?
'items.cn_sort' : $sortfield;
248 my $contents = $shelf->get_contents->search(
251 prefetch
=> [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
254 order_by
=> { "-$direction" => $order_by },
258 my $xslfile = C4
::Context
->preference('XSLTListsDisplay');
259 my $lang = $xslfile ? C4
::Languages
::getlanguage
() : undef;
260 my $sysxml = $xslfile ? C4
::XSLT
::get_xslt_sysprefs
() : undef;
263 while ( my $content = $contents->next ) {
265 my $biblionumber = $content->biblionumber;
266 my $record = GetMarcBiblio
({ biblionumber
=> $biblionumber });
269 $this_item->{XSLTBloc
} = XSLTParse4Display
( $biblionumber, $record, "XSLTListsDisplay",
270 1, undef, $sysxml, $xslfile, $lang);
273 my $marcflavour = C4
::Context
->preference("marcflavour");
274 my $itemtype = Koha
::Biblioitems
->search({ biblionumber
=> $content->biblionumber })->next->itemtype;
275 $itemtype = Koha
::ItemTypes
->find( $itemtype );
276 my $biblio = Koha
::Biblios
->find( $content->biblionumber );
277 $this_item->{title
} = $biblio->title;
278 $this_item->{author
} = $biblio->author;
279 $this_item->{dateadded
} = $content->dateadded;
280 $this_item->{imageurl
} = $itemtype ? C4
::Koha
::getitemtypeimagelocation
( 'intranet', $itemtype->imageurl ) : q{};
281 $this_item->{description
} = $itemtype ?
$itemtype->description : q{}; #FIXME Should this be translated_description ?
282 $this_item->{notforloan
} = $itemtype->notforloan if $itemtype;
283 $this_item->{'coins'} = $biblio->get_coins;
284 $this_item->{'subtitle'} = GetRecordValue
( 'subtitle', $record, GetFrameworkCode
( $biblionumber ) );
285 $this_item->{'normalized_upc'} = GetNormalizedUPC
( $record, $marcflavour );
286 $this_item->{'normalized_ean'} = GetNormalizedEAN
( $record, $marcflavour );
287 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber
( $record, $marcflavour );
288 $this_item->{'normalized_isbn'} = GetNormalizedISBN
( undef, $record, $marcflavour );
290 unless ( defined $this_item->{size
} ) {
292 #TT has problems with size
293 $this_item->{size
} = q
||;
296 # Getting items infos for location display
297 my @items_infos = &GetItemsLocationInfo
( $biblionumber );
298 $this_item->{'ITEM_RESULTS'} = \
@items_infos;
299 $this_item->{biblionumber
} = $biblionumber;
300 push @items, $this_item;
303 my $some_private_shelves = Koha
::Virtualshelves
->get_some_shelves(
305 borrowernumber
=> $loggedinuser,
310 my $some_public_shelves = Koha
::Virtualshelves
->get_some_shelves(
312 borrowernumber
=> $loggedinuser,
319 add_to_some_private_shelves
=> $some_private_shelves,
320 add_to_some_public_shelves
=> $some_public_shelves,
321 can_manage_shelf
=> $shelf->can_be_managed($loggedinuser),
322 can_remove_shelf
=> $shelf->can_be_deleted($loggedinuser),
323 can_remove_biblios
=> $shelf->can_biblios_be_removed($loggedinuser),
324 can_add_biblios
=> $shelf->can_biblios_be_added($loggedinuser),
325 sortfield
=> $sortfield,
326 itemsloop
=> \
@items,
327 sortfield
=> $sortfield,
328 direction
=> $direction,
331 my $pager = $contents->pager;
333 pagination_bar
=> pagination_bar
(
334 q
||, $pager->last_page - $pager->first_page + 1,
335 $page, "page", { op
=> 'view', shelfnumber
=> $shelf->shelfnumber, sortfield
=> $sortfield, direction
=> $direction, }
340 push @messages, { type
=> 'error', code
=> 'unauthorized_on_view' };
344 push @messages, { type
=> 'alert', code
=> 'does_not_exist' };
352 messages
=> \
@messages,
353 category
=> $category,
354 print => scalar $query->param('print') || 0,
355 csv_profiles
=> [ Koha
::CsvProfiles
->search({ type
=> 'marc', used_for
=> 'export_records' }) ],
358 output_html_with_http_headers
$query, $cookie, $template->output;