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
::CsvProfiles
;
32 use Koha
::Virtualshelves
;
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
37 { template_name
=> "virtualshelves/shelves.tt",
41 flagsrequired
=> { catalogue
=> 1 },
45 my $op = $query->param('op') || 'list';
46 my $referer = $query->param('referer') || $op;
47 my $category = $query->param('category') || 1;
48 my ( $shelf, $shelfnumber, @messages );
50 if ( $op eq 'add_form' ) {
52 } elsif ( $op eq 'edit_form' ) {
53 $shelfnumber = $query->param('shelfnumber');
54 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
57 $category = $shelf->category;
58 my $patron = GetMember
( 'borrowernumber' => $shelf->owner );
59 $template->param( owner
=> $patron, );
60 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
61 push @messages, { type
=> 'error', code
=> 'unauthorized_on_update' };
65 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
67 } elsif ( $op eq 'add' ) {
69 $shelf = Koha
::Virtualshelf
->new(
70 { shelfname
=> scalar $query->param('shelfname'),
71 sortfield
=> scalar $query->param('sortfield'),
72 category
=> scalar $query->param('category'),
73 allow_add
=> scalar $query->param('allow_add'),
74 allow_delete_own
=> scalar $query->param('allow_delete_own'),
75 allow_delete_other
=> scalar $query->param('allow_delete_other'),
76 owner
=> scalar $query->param('owner'),
80 $shelfnumber = $shelf->shelfnumber;
83 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
84 } elsif ( not $shelf ) {
85 push @messages, { type
=> 'error', code
=> 'error_on_insert' };
87 push @messages, { type
=> 'message', code
=> 'success_on_insert' };
90 } elsif ( $op eq 'edit' ) {
91 $shelfnumber = $query->param('shelfnumber');
92 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
96 if ( $shelf->can_be_managed( $loggedinuser ) ) {
97 $shelf->shelfname( scalar $query->param('shelfname') );
98 $shelf->sortfield( scalar $query->param('sortfield') );
99 $shelf->allow_add( scalar $query->param('allow_add') );
100 $shelf->allow_delete_own( scalar $query->param('allow_delete_own') );
101 $shelf->allow_delete_other( scalar $query->param('allow_delete_other') );
102 $shelf->category( scalar $query->param('category') );
103 eval { $shelf->store };
106 push @messages, { type
=> 'error', code
=> 'error_on_update' };
109 push @messages, { type
=> 'message', code
=> 'success_on_update' };
112 push @messages, { type
=> 'error', code
=> 'unauthorized_on_update' };
115 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
117 } elsif ( $op eq 'delete' ) {
118 $shelfnumber = $query->param('shelfnumber');
119 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
121 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
122 eval { $shelf->delete; };
124 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
126 push @messages, { type
=> 'message', code
=> 'success_on_delete' };
129 push @messages, { type
=> 'error', code
=> 'unauthorized_on_delete' };
132 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
135 } elsif ( $op eq 'add_biblio' ) {
136 $shelfnumber = $query->param('shelfnumber');
137 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
139 if( my $barcode = $query->param('barcode') ) {
140 my $item = GetItem
( 0, $barcode);
141 if (defined $item && $item->{itemnumber
}) {
142 my $biblio = GetBiblioFromItemNumber
( $item->{itemnumber
} );
143 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
144 my $added = eval { $shelf->add_biblio( $biblio->{biblionumber
}, $loggedinuser ); };
146 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
148 push @messages, { type
=> 'message', code
=> 'success_on_add_biblio' };
150 push @messages, { type
=> 'message', code
=> 'error_on_add_biblio' };
153 push @messages, { type
=> 'error', code
=> 'unauthorized_on_add_biblio' };
156 push @messages, { type
=> 'error', code
=> 'item_does_not_exist' };
160 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
163 } elsif ( $op eq 'remove_biblios' ) {
164 $shelfnumber = $query->param('shelfnumber');
165 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
166 my @biblionumbers = $query->multi_param('biblionumber');
168 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
169 my $number_of_biblios_removed = eval {
170 $shelf->remove_biblios(
172 biblionumbers
=> \
@biblionumbers,
173 borrowernumber
=> $loggedinuser,
178 push @messages, { type
=> 'error', code
=> ref($@
), msg
=> $@
};
179 } elsif ( $number_of_biblios_removed ) {
180 push @messages, { type
=> 'message', code
=> 'success_on_remove_biblios' };
182 push @messages, { type
=> 'error', code
=> 'no_biblio_removed' };
185 push @messages, { type
=> 'error', code
=> 'unauthorized_on_remove_biblios' };
188 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
193 if ( $op eq 'view' ) {
194 $shelfnumber ||= $query->param('shelfnumber');
195 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
197 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
198 my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title'; # Passed in sorting overrides default sorting
199 my $direction = $query->param('direction') || 'asc';
200 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
202 unless ( $query->param('print') ) {
203 $rows = C4
::Context
->preference('numSearchResults') || 20;
204 $page = ( $query->param('page') ?
$query->param('page') : 1 );
207 my $order_by = $sortfield eq 'itemcallnumber' ?
'items.itemcallnumber' : $sortfield;
208 my $contents = $shelf->get_contents->search(
211 prefetch
=> [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
214 order_by
=> { "-$direction" => $order_by },
218 my $borrower = GetMember
( borrowernumber
=> $loggedinuser );
220 my $xslfile = C4
::Context
->preference('XSLTListsDisplay');
221 my $lang = $xslfile ? C4
::Languages
::getlanguage
() : undef;
222 my $sysxml = $xslfile ? C4
::XSLT
::get_xslt_sysprefs
() : undef;
225 while ( my $content = $contents->next ) {
227 my $biblionumber = $content->biblionumber->biblionumber;
228 my $record = GetMarcBiblio
($biblionumber);
231 $this_item->{XSLTBloc
} = XSLTParse4Display
( $biblionumber, $record, "XSLTListsDisplay",
232 1, undef, $sysxml, $xslfile, $lang);
235 my $marcflavour = C4
::Context
->preference("marcflavour");
236 my $itemtypeinfo = getitemtypeinfo
( $content->biblionumber->biblioitems->first->itemtype, 'intranet' );
237 $this_item->{title
} = $content->biblionumber->title;
238 $this_item->{author
} = $content->biblionumber->author;
239 $this_item->{dateadded
} = $content->dateadded;
240 $this_item->{imageurl
} = $itemtypeinfo->{imageurl
};
241 $this_item->{description
} = $itemtypeinfo->{description
};
242 $this_item->{notforloan
} = $itemtypeinfo->{notforloan
};
243 $this_item->{'coins'} = GetCOinSBiblio
($record);
244 $this_item->{'subtitle'} = GetRecordValue
( 'subtitle', $record, GetFrameworkCode
( $biblionumber ) );
245 $this_item->{'normalized_upc'} = GetNormalizedUPC
( $record, $marcflavour );
246 $this_item->{'normalized_ean'} = GetNormalizedEAN
( $record, $marcflavour );
247 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber
( $record, $marcflavour );
248 $this_item->{'normalized_isbn'} = GetNormalizedISBN
( undef, $record, $marcflavour );
250 unless ( defined $this_item->{size
} ) {
252 #TT has problems with size
253 $this_item->{size
} = q
||;
256 # Getting items infos for location display
257 my @items_infos = &GetItemsLocationInfo
( $biblionumber );
258 $this_item->{'ITEM_RESULTS'} = \
@items_infos;
259 $this_item->{biblionumber
} = $biblionumber;
260 push @items, $this_item;
263 my $some_private_shelves = Koha
::Virtualshelves
->get_some_shelves(
265 borrowernumber
=> $loggedinuser,
270 my $some_public_shelves = Koha
::Virtualshelves
->get_some_shelves(
272 borrowernumber
=> $loggedinuser,
279 add_to_some_private_shelves
=> $some_private_shelves,
280 add_to_some_public_shelves
=> $some_public_shelves,
281 can_manage_shelf
=> $shelf->can_be_managed($loggedinuser),
282 can_remove_shelf
=> $shelf->can_be_deleted($loggedinuser),
283 can_remove_biblios
=> $shelf->can_biblios_be_removed($loggedinuser),
284 can_add_biblios
=> $shelf->can_biblios_be_added($loggedinuser),
285 sortfield
=> $sortfield,
286 itemsloop
=> \
@items,
287 sortfield
=> $sortfield,
288 direction
=> $direction,
291 my $pager = $contents->pager;
293 pagination_bar
=> pagination_bar
(
294 q
||, $pager->last_page - $pager->first_page + 1,
295 $page, "page", { op
=> 'view', shelfnumber
=> $shelf->shelfnumber, sortfield
=> $sortfield, direction
=> $direction, }
300 push @messages, { type
=> 'error', code
=> 'unauthorized_on_view' };
303 push @messages, { type
=> 'error', code
=> 'does_not_exist' };
311 messages
=> \
@messages,
312 category
=> $category,
313 print => scalar $query->param('print') || 0,
314 csv_profiles
=> [ Koha
::CsvProfiles
->search({ type
=> 'marc' }) ],
317 output_html_with_http_headers
$query, $cookie, $template->output;