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
::CsvProfiles
;
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
=> scalar $query->param('shelfname'),
70 sortfield
=> scalar $query->param('sortfield'),
71 category
=> scalar $query->param('category'),
72 allow_add
=> scalar $query->param('allow_add'),
73 allow_delete_own
=> scalar $query->param('allow_delete_own'),
74 allow_delete_other
=> scalar $query->param('allow_delete_other'),
75 owner
=> scalar $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( scalar $query->param('shelfname') );
97 $shelf->sortfield( scalar $query->param('sortfield') );
98 $shelf->allow_add( scalar $query->param('allow_add') );
99 $shelf->allow_delete_own( scalar $query->param('allow_delete_own') );
100 $shelf->allow_delete_other( scalar $query->param('allow_delete_other') );
101 $shelf->category( scalar $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->multi_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 || 'title'; # Passed in sorting overrides default sorting
198 my $direction = $query->param('direction') || 'asc';
199 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
201 unless ( $query->param('print') ) {
202 $rows = C4
::Context
->preference('numSearchResults') || 20;
203 $page = ( $query->param('page') ?
$query->param('page') : 1 );
206 my $order_by = $sortfield eq 'itemcallnumber' ?
'items.itemcallnumber' : $sortfield;
207 my $contents = $shelf->get_contents->search(
210 prefetch
=> [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
213 order_by
=> { "-$direction" => $order_by },
217 my $borrower = GetMember
( borrowernumber
=> $loggedinuser );
219 my $xslfile = C4
::Context
->preference('XSLTListsDisplay');
220 my $lang = $xslfile ? C4
::Languages
::getlanguage
() : undef;
221 my $sysxml = $xslfile ? C4
::XSLT
::get_xslt_sysprefs
() : undef;
224 while ( my $content = $contents->next ) {
226 my $biblionumber = $content->biblionumber->biblionumber;
227 my $record = GetMarcBiblio
($biblionumber);
230 $this_item->{XSLTBloc
} = XSLTParse4Display
( $biblionumber, $record, "XSLTListsDisplay",
231 1, undef, $sysxml, $xslfile, $lang);
234 my $marcflavour = C4
::Context
->preference("marcflavour");
235 my $itemtypeinfo = getitemtypeinfo
( $content->biblionumber->biblioitems->first->itemtype, 'intranet' );
236 $this_item->{title
} = $content->biblionumber->title;
237 $this_item->{author
} = $content->biblionumber->author;
238 $this_item->{dateadded
} = $content->dateadded;
239 $this_item->{imageurl
} = $itemtypeinfo->{imageurl
};
240 $this_item->{description
} = $itemtypeinfo->{description
};
241 $this_item->{notforloan
} = $itemtypeinfo->{notforloan
};
242 $this_item->{'coins'} = GetCOinSBiblio
($record);
243 $this_item->{'subtitle'} = GetRecordValue
( 'subtitle', $record, GetFrameworkCode
( $biblionumber ) );
244 $this_item->{'normalized_upc'} = GetNormalizedUPC
( $record, $marcflavour );
245 $this_item->{'normalized_ean'} = GetNormalizedEAN
( $record, $marcflavour );
246 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber
( $record, $marcflavour );
247 $this_item->{'normalized_isbn'} = GetNormalizedISBN
( undef, $record, $marcflavour );
249 unless ( defined $this_item->{size
} ) {
251 #TT has problems with size
252 $this_item->{size
} = q
||;
255 # Getting items infos for location display
256 my @items_infos = &GetItemsLocationInfo
( $biblionumber );
257 $this_item->{'ITEM_RESULTS'} = \
@items_infos;
258 $this_item->{biblionumber
} = $biblionumber;
259 push @items, $this_item;
262 my $some_private_shelves = Koha
::Virtualshelves
->get_some_shelves(
264 borrowernumber
=> $loggedinuser,
269 my $some_public_shelves = Koha
::Virtualshelves
->get_some_shelves(
271 borrowernumber
=> $loggedinuser,
278 add_to_some_private_shelves
=> $some_private_shelves,
279 add_to_some_public_shelves
=> $some_public_shelves,
280 can_manage_shelf
=> $shelf->can_be_managed($loggedinuser),
281 can_remove_shelf
=> $shelf->can_be_deleted($loggedinuser),
282 can_remove_biblios
=> $shelf->can_biblios_be_removed($loggedinuser),
283 can_add_biblios
=> $shelf->can_biblios_be_added($loggedinuser),
284 sortfield
=> $sortfield,
285 itemsloop
=> \
@items,
286 sortfield
=> $sortfield,
287 direction
=> $direction,
290 my $pager = $contents->pager;
292 pagination_bar
=> pagination_bar
(
293 q
||, $pager->last_page - $pager->first_page + 1,
294 $page, "page", { op
=> 'view', shelfnumber
=> $shelf->shelfnumber, sortfield
=> $sortfield, direction
=> $direction, }
299 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;