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>.
30 use Koha
::Virtualshelves
;
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
35 { template_name
=> "virtualshelves/shelves.tt",
39 flagsrequired
=> { catalogue
=> 1 },
43 my $op = $query->param('op') || 'list';
44 my $referer = $query->param('referer') || $op;
45 my $category = $query->param('category') || 1;
46 my ( $shelf, $shelfnumber, @messages );
48 if ( $op eq 'add_form' ) {
50 } elsif ( $op eq 'edit_form' ) {
51 $shelfnumber = $query->param('shelfnumber');
52 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
55 $category = $shelf->category;
56 my $patron = GetMember
( 'borrowernumber' => $shelf->owner );
57 $template->param( owner
=> $patron, );
58 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
59 push @messages, { type
=> 'alert', code
=> 'unauthorized_on_update' };
63 push @messages, { type
=> 'alert', code
=> 'does_not_exist' };
65 } elsif ( $op eq 'add' ) {
67 $shelf = Koha
::Virtualshelf
->new(
68 { shelfname
=> scalar $query->param('shelfname'),
69 sortfield
=> scalar $query->param('sortfield'),
70 category
=> scalar $query->param('category'),
71 allow_add
=> scalar $query->param('allow_add'),
72 allow_delete_own
=> scalar $query->param('allow_delete_own'),
73 allow_delete_other
=> scalar $query->param('allow_delete_other'),
74 owner
=> scalar $query->param('owner'),
78 $shelfnumber = $shelf->shelfnumber;
81 push @messages, { type
=> 'alert', code
=> ref($@
), msg
=> $@
};
82 } elsif ( not $shelf ) {
83 push @messages, { type
=> 'alert', 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 my $sortfield = $query->param('sortfield');
96 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
97 if ( $shelf->can_be_managed( $loggedinuser ) ) {
98 $shelf->shelfname( scalar $query->param('shelfname') );
99 $shelf->sortfield( $sortfield );
100 $shelf->allow_add( scalar $query->param('allow_add') );
101 $shelf->allow_delete_own( scalar $query->param('allow_delete_own') );
102 $shelf->allow_delete_other( scalar $query->param('allow_delete_other') );
103 $shelf->category( scalar $query->param('category') );
104 eval { $shelf->store };
107 push @messages, { type
=> 'alert', code
=> 'error_on_update' };
110 push @messages, { type
=> 'message', code
=> 'success_on_update' };
113 push @messages, { type
=> 'alert', code
=> 'unauthorized_on_update' };
116 push @messages, { type
=> 'alert', code
=> 'does_not_exist' };
118 } elsif ( $op eq 'delete' ) {
119 $shelfnumber = $query->param('shelfnumber');
120 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
122 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
123 eval { $shelf->delete; };
125 push @messages, { type
=> 'alert', code
=> ref($@
), msg
=> $@
};
127 push @messages, { type
=> 'message', code
=> 'success_on_delete' };
130 push @messages, { type
=> 'alert', code
=> 'unauthorized_on_delete' };
133 push @messages, { type
=> 'alert', code
=> 'does_not_exist' };
136 } elsif ( $op eq 'add_biblio' ) {
137 $shelfnumber = $query->param('shelfnumber');
138 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
140 if( my $barcode = $query->param('barcode') ) {
141 my $item = GetItem
( 0, $barcode);
142 if (defined $item && $item->{itemnumber
}) {
143 my $biblio = GetBiblioFromItemNumber
( $item->{itemnumber
} );
144 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
145 my $added = eval { $shelf->add_biblio( $biblio->{biblionumber
}, $loggedinuser ); };
147 push @messages, { type
=> 'alert', code
=> ref($@
), msg
=> $@
};
149 push @messages, { type
=> 'message', code
=> 'success_on_add_biblio' };
151 push @messages, { type
=> 'message', code
=> 'error_on_add_biblio' };
154 push @messages, { type
=> 'alert', code
=> 'unauthorized_on_add_biblio' };
157 push @messages, { type
=> 'alert', code
=> 'item_does_not_exist' };
161 push @messages, { type
=> 'alert', code
=> 'does_not_exist' };
164 } elsif ( $op eq 'remove_biblios' ) {
165 $shelfnumber = $query->param('shelfnumber');
166 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
167 my @biblionumbers = $query->multi_param('biblionumber');
169 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
170 my $number_of_biblios_removed = eval {
171 $shelf->remove_biblios(
173 biblionumbers
=> \
@biblionumbers,
174 borrowernumber
=> $loggedinuser,
179 push @messages, { type
=> 'alert', code
=> ref($@
), msg
=> $@
};
180 } elsif ( $number_of_biblios_removed ) {
181 push @messages, { type
=> 'message', code
=> 'success_on_remove_biblios' };
183 push @messages, { type
=> 'alert', code
=> 'no_biblio_removed' };
186 push @messages, { type
=> 'alert', code
=> 'unauthorized_on_remove_biblios' };
189 push @messages, { type
=> 'alert', code
=> 'does_not_exist' };
194 if ( $op eq 'view' ) {
195 $shelfnumber ||= $query->param('shelfnumber');
196 $shelf = Koha
::Virtualshelves
->find($shelfnumber);
198 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
199 my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title'; # Passed in sorting overrides default sorting
200 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
201 my $direction = $query->param('direction') || 'asc';
202 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
204 unless ( $query->param('print') ) {
205 $rows = C4
::Context
->preference('numSearchResults') || 20;
206 $page = ( $query->param('page') ?
$query->param('page') : 1 );
209 my $order_by = $sortfield eq 'itemcallnumber' ?
'items.itemcallnumber' : $sortfield;
210 my $contents = $shelf->get_contents->search(
213 prefetch
=> [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
216 order_by
=> { "-$direction" => $order_by },
220 my $borrower = GetMember
( borrowernumber
=> $loggedinuser );
222 my $xslfile = C4
::Context
->preference('XSLTResultsDisplay');
223 my $lang = $xslfile ? C4
::Languages
::getlanguage
() : undef;
224 my $sysxml = $xslfile ? C4
::XSLT
::get_xslt_sysprefs
() : undef;
227 while ( my $content = $contents->next ) {
229 my $biblionumber = $content->biblionumber->biblionumber;
230 my $record = GetMarcBiblio
($biblionumber);
233 $this_item->{XSLTBloc
} = XSLTParse4Display
( $biblionumber, $record, "XSLTResultsDisplay",
234 1, undef, $sysxml, $xslfile, $lang);
237 my $marcflavour = C4
::Context
->preference("marcflavour");
238 my $itemtypeinfo = getitemtypeinfo
( $content->biblionumber->biblioitems->first->itemtype, 'intranet' );
239 $this_item->{title
} = $content->biblionumber->title;
240 $this_item->{author
} = $content->biblionumber->author;
241 $this_item->{dateadded
} = $content->dateadded;
242 $this_item->{imageurl
} = $itemtypeinfo->{imageurl
};
243 $this_item->{description
} = $itemtypeinfo->{description
};
244 $this_item->{notforloan
} = $itemtypeinfo->{notforloan
};
245 $this_item->{'coins'} = GetCOinSBiblio
($record);
246 $this_item->{'subtitle'} = GetRecordValue
( 'subtitle', $record, GetFrameworkCode
( $biblionumber ) );
247 $this_item->{'normalized_upc'} = GetNormalizedUPC
( $record, $marcflavour );
248 $this_item->{'normalized_ean'} = GetNormalizedEAN
( $record, $marcflavour );
249 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber
( $record, $marcflavour );
250 $this_item->{'normalized_isbn'} = GetNormalizedISBN
( undef, $record, $marcflavour );
252 unless ( defined $this_item->{size
} ) {
254 #TT has problems with size
255 $this_item->{size
} = q
||;
258 # Getting items infos for location display
259 my @items_infos = &GetItemsLocationInfo
( $biblionumber );
260 $this_item->{'ITEM_RESULTS'} = \
@items_infos;
261 $this_item->{biblionumber
} = $biblionumber;
262 push @items, $this_item;
265 my $some_private_shelves = Koha
::Virtualshelves
->get_some_shelves(
267 borrowernumber
=> $loggedinuser,
272 my $some_public_shelves = Koha
::Virtualshelves
->get_some_shelves(
274 borrowernumber
=> $loggedinuser,
281 add_to_some_private_shelves
=> $some_private_shelves,
282 add_to_some_public_shelves
=> $some_public_shelves,
283 can_manage_shelf
=> $shelf->can_be_managed($loggedinuser),
284 can_remove_shelf
=> $shelf->can_be_deleted($loggedinuser),
285 can_remove_biblios
=> $shelf->can_biblios_be_removed($loggedinuser),
286 can_add_biblios
=> $shelf->can_biblios_be_added($loggedinuser),
287 sortfield
=> $sortfield,
288 itemsloop
=> \
@items,
289 sortfield
=> $sortfield,
290 direction
=> $direction,
293 my $pager = $contents->pager;
295 pagination_bar
=> pagination_bar
(
296 q
||, $pager->last_page - $pager->first_page + 1,
297 $page, "page", { op
=> 'view', shelfnumber
=> $shelf->shelfnumber, sortfield
=> $sortfield, direction
=> $direction, }
302 push @messages, { type
=> 'error', code
=> 'unauthorized_on_view' };
306 push @messages, { type
=> 'alert', code
=> 'does_not_exist' };
314 messages
=> \
@messages,
315 category
=> $category,
316 print => scalar $query->param('print') || 0,
317 csv_profiles
=> GetCsvProfilesLoop
('marc'),
320 output_html_with_http_headers
$query, $cookie, $template->output;