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>.
22 use C4::VirtualShelves;
31 use Koha::Virtualshelves;
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36 { template_name => "virtualshelves/shelves.tt",
40 flagsrequired => { catalogue => 1 },
44 my $op = $query->param('op') || 'list';
45 my $referer = $query->param('referer') || $op;
46 my $category = $query->param('category') || 1;
47 my ( $shelf, $shelfnumber, @messages );
49 if ( $op eq 'add_form' ) {
51 } elsif ( $op eq 'edit_form' ) {
52 $shelfnumber = $query->param('shelfnumber');
53 $shelf = Koha::Virtualshelves->find($shelfnumber);
56 $category = $shelf->category;
57 my $patron = GetMember( 'borrowernumber' => $shelf->owner );
58 $template->param( owner => $patron, );
59 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
60 push @messages, { type => 'error', code => 'unauthorized_on_update' };
64 push @messages, { type => 'error', code => 'does_not_exist' };
66 } elsif ( $op eq 'add' ) {
68 $shelf = Koha::Virtualshelf->new(
69 { shelfname => $query->param('shelfname'),
70 sortfield => $query->param('sortfield'),
71 category => $query->param('category'),
72 allow_add => $query->param('allow_add'),
73 allow_delete_own => $query->param('allow_delete_own'),
74 allow_delete_other => $query->param('allow_delete_other'),
75 owner => $query->param('owner'),
79 $shelfnumber = $shelf->shelfnumber;
82 push @messages, { type => 'error', code => ref($@), msg => $@ };
83 } elsif ( not $shelf ) {
84 push @messages, { type => 'error', code => 'error_on_insert' };
86 push @messages, { type => 'message', code => 'success_on_insert' };
89 } elsif ( $op eq 'edit' ) {
90 $shelfnumber = $query->param('shelfnumber');
91 $shelf = Koha::Virtualshelves->find($shelfnumber);
95 if ( $shelf->can_be_managed( $loggedinuser ) ) {
96 $shelf->shelfname( $query->param('shelfname') );
97 $shelf->sortfield( $query->param('sortfield') );
98 $shelf->allow_add( $query->param('allow_add') );
99 $shelf->allow_delete_own( $query->param('allow_delete_own') );
100 $shelf->allow_delete_other( $query->param('allow_delete_other') );
101 $shelf->category( $query->param('category') );
102 eval { $shelf->store };
105 push @messages, { type => 'error', code => 'error_on_update' };
108 push @messages, { type => 'message', code => 'success_on_update' };
111 push @messages, { type => 'error', code => 'unauthorized_on_update' };
114 push @messages, { type => 'error', code => 'does_not_exist' };
116 } elsif ( $op eq 'delete' ) {
117 $shelfnumber = $query->param('shelfnumber');
118 $shelf = Koha::Virtualshelves->find($shelfnumber);
120 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
121 eval { $shelf->delete; };
123 push @messages, { type => 'error', code => ref($@), msg => $@ };
125 push @messages, { type => 'message', code => 'success_on_delete' };
128 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
131 push @messages, { type => 'error', code => 'does_not_exist' };
134 } elsif ( $op eq 'add_biblio' ) {
135 $shelfnumber = $query->param('shelfnumber');
136 $shelf = Koha::Virtualshelves->find($shelfnumber);
138 if( my $barcode = $query->param('barcode') ) {
139 my $item = GetItem( 0, $barcode);
140 if (defined $item && $item->{itemnumber}) {
141 my $biblio = GetBiblioFromItemNumber( $item->{itemnumber} );
142 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
143 my $added = eval { $shelf->add_biblio( $biblio->{biblionumber}, $loggedinuser ); };
145 push @messages, { type => 'error', code => ref($@), msg => $@ };
147 push @messages, { type => 'message', code => 'success_on_add_biblio' };
149 push @messages, { type => 'message', code => 'error_on_add_biblio' };
152 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
155 push @messages, { type => 'error', code => 'item_does_not_exist' };
159 push @messages, { type => 'error', code => 'does_not_exist' };
162 } elsif ( $op eq 'remove_biblios' ) {
163 $shelfnumber = $query->param('shelfnumber');
164 $shelf = Koha::Virtualshelves->find($shelfnumber);
165 my @biblionumbers = $query->param('biblionumber');
167 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
168 my $number_of_biblios_removed = eval {
169 $shelf->remove_biblios(
171 biblionumbers => \@biblionumbers,
172 borrowernumber => $loggedinuser,
177 push @messages, { type => 'error', code => ref($@), msg => $@ };
178 } elsif ( $number_of_biblios_removed ) {
179 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
181 push @messages, { type => 'error', code => 'no_biblio_removed' };
184 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
187 push @messages, { type => 'error', code => 'does_not_exist' };
192 if ( $op eq 'view' ) {
193 $shelfnumber ||= $query->param('shelfnumber');
194 $shelf = Koha::Virtualshelves->find($shelfnumber);
196 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
197 my $sortfield = $query->param('sortfield') || $shelf->sortfield; # Passed in sorting overrides default sorting
198 my $direction = $query->param('direction') || 'asc';
199 my ( $shelflimit, $shelfoffset, $itemoff );
200 unless ( $query->param('print') ) {
201 $shelflimit = C4::Context->preference('numSearchResults') || 20;
202 $itemoff = ( $query->param('itemoff') ? $query->param('itemoff') : 1 );
203 $shelfoffset = ( $itemoff - 1 ) * $shelflimit; # Sets the offset to begin retrieving items at
205 my ( $items, $totitems ) = GetShelfContents( $shelfnumber, $shelflimit, $shelfoffset, $sortfield, $direction );
207 my $borrower = GetMember( borrowernumber => $loggedinuser );
209 for my $this_item (@$items) {
210 my $biblionumber = $this_item->{biblionumber};
211 my $record = GetMarcBiblio($biblionumber);
213 if ( C4::Context->preference("XSLTResultsDisplay") ) {
214 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "XSLTResultsDisplay" );
217 my $marcflavour = C4::Context->preference("marcflavour");
218 $this_item->{'imageurl'} = getitemtypeinfo( $this_item->{'itemtype'}, 'intranet' )->{'imageurl'};
219 $this_item->{'coins'} = GetCOinSBiblio($record);
220 $this_item->{'subtitle'} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $this_item->{'biblionumber'} ) );
221 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
222 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
223 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
224 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
226 unless ( defined $this_item->{size} ) {
228 #TT has problems with size
229 $this_item->{size} = q||;
232 # Getting items infos for location display
233 my @items_infos = &GetItemsLocationInfo( $this_item->{'biblionumber'} );
234 $this_item->{'ITEM_RESULTS'} = \@items_infos;
237 # Build drop-down list for 'Add To:' menu...
238 my ( $totalref, $pubshelves, $barshelves ) = C4::VirtualShelves::GetSomeShelfNames( $loggedinuser, 'COMBO', 1 );
240 addbarshelves => $totalref->{bartotal},
241 addbarshelvesloop => $barshelves,
242 addpubshelves => $totalref->{pubtotal},
243 addpubshelvesloop => $pubshelves,
244 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
245 can_remove_shelf => $shelf->can_be_deleted($loggedinuser),
246 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
247 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
248 sortfield => $sortfield,
250 sortfield => $sortfield,
251 direction => $direction,
255 pagination_bar => pagination_bar(
256 q||, ( int( $totitems / $shelflimit ) ) + ( ( $totitems % $shelflimit ) > 0 ? 1 : 0 ),
257 $itemoff, "itemoff", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
262 push @messages, { type => 'error', code => 'unauthorized_on_view' };
265 push @messages, { type => 'error', code => 'does_not_exist' };
273 messages => \@messages,
274 category => $category,
275 print => $query->param('print') || 0,
276 csv_profiles => GetCsvProfilesLoop('marc'),
279 output_html_with_http_headers $query, $cookie, $template->output;