Bug 18182: Make TestBuilder capable of returning Koha::Object
[koha.git] / virtualshelves / shelves.pl
blobc328869fd4210f8612158c17dacef49d64a1bc99
1 #!/usr/bin/perl
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>.
20 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use C4::Auth;
23 use C4::Biblio;
24 use C4::Koha;
25 use C4::Items;
26 use C4::Members;
27 use C4::Output;
28 use C4::XSLT;
30 use Koha::Biblios;
31 use Koha::Biblioitems;
32 use Koha::CsvProfiles;
33 use Koha::Virtualshelves;
35 my $query = new CGI;
37 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
38 { template_name => "virtualshelves/shelves.tt",
39 query => $query,
40 type => "intranet",
41 authnotrequired => 0,
42 flagsrequired => { catalogue => 1 },
46 my $op = $query->param('op') || 'list';
47 my $referer = $query->param('referer') || $op;
48 my $category = $query->param('category') || 1;
49 my ( $shelf, $shelfnumber, @messages );
51 if ( $op eq 'add_form' ) {
52 # Nothing to do
53 } elsif ( $op eq 'edit_form' ) {
54 $shelfnumber = $query->param('shelfnumber');
55 $shelf = Koha::Virtualshelves->find($shelfnumber);
57 if ( $shelf ) {
58 $category = $shelf->category;
59 my $patron = GetMember( 'borrowernumber' => $shelf->owner );
60 $template->param( owner => $patron, );
61 unless ( $shelf->can_be_managed( $loggedinuser ) ) {
62 push @messages, { type => 'alert', code => 'unauthorized_on_update' };
63 $op = 'list';
65 } else {
66 push @messages, { type => 'alert', code => 'does_not_exist' };
68 } elsif ( $op eq 'add' ) {
69 eval {
70 $shelf = Koha::Virtualshelf->new(
71 { shelfname => scalar $query->param('shelfname'),
72 sortfield => scalar $query->param('sortfield'),
73 category => scalar $query->param('category'),
74 allow_add => scalar $query->param('allow_add'),
75 allow_delete_own => scalar $query->param('allow_delete_own'),
76 allow_delete_other => scalar $query->param('allow_delete_other'),
77 owner => scalar $query->param('owner'),
80 $shelf->store;
81 $shelfnumber = $shelf->shelfnumber;
83 if ($@) {
84 push @messages, { type => 'alert', code => ref($@), msg => $@ };
85 } elsif ( not $shelf ) {
86 push @messages, { type => 'alert', code => 'error_on_insert' };
88 } else {
89 push @messages, { type => 'message', code => 'success_on_insert' };
90 $op = 'view';
92 } elsif ( $op eq 'edit' ) {
93 $shelfnumber = $query->param('shelfnumber');
94 $shelf = Koha::Virtualshelves->find($shelfnumber);
96 if ( $shelf ) {
97 $op = $referer;
98 my $sortfield = $query->param('sortfield');
99 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
100 if ( $shelf->can_be_managed( $loggedinuser ) ) {
101 $shelf->shelfname( scalar $query->param('shelfname') );
102 $shelf->sortfield( $sortfield );
103 $shelf->allow_add( scalar $query->param('allow_add') );
104 $shelf->allow_delete_own( scalar $query->param('allow_delete_own') );
105 $shelf->allow_delete_other( scalar $query->param('allow_delete_other') );
106 $shelf->category( scalar $query->param('category') );
107 eval { $shelf->store };
109 if ($@) {
110 push @messages, { type => 'alert', code => 'error_on_update' };
111 $op = 'edit_form';
112 } else {
113 push @messages, { type => 'message', code => 'success_on_update' };
115 } else {
116 push @messages, { type => 'alert', code => 'unauthorized_on_update' };
118 } else {
119 push @messages, { type => 'alert', code => 'does_not_exist' };
121 } elsif ( $op eq 'delete' ) {
122 $shelfnumber = $query->param('shelfnumber');
123 $shelf = Koha::Virtualshelves->find($shelfnumber);
124 if ($shelf) {
125 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
126 eval { $shelf->delete; };
127 if ($@) {
128 push @messages, { type => 'alert', code => ref($@), msg => $@ };
129 } else {
130 push @messages, { type => 'message', code => 'success_on_delete' };
132 } else {
133 push @messages, { type => 'alert', code => 'unauthorized_on_delete' };
135 } else {
136 push @messages, { type => 'alert', code => 'does_not_exist' };
138 $op = 'list';
139 } elsif ( $op eq 'add_biblio' ) {
140 $shelfnumber = $query->param('shelfnumber');
141 $shelf = Koha::Virtualshelves->find($shelfnumber);
142 if ($shelf) {
143 if( my $barcodes = $query->param('barcodes') ) {
144 if ( $shelf->can_biblios_be_added( $loggedinuser ) ) {
145 my @barcodes = split /\n/, $barcodes; # Entries are effectively passed in as a <cr> separated list
146 foreach my $barcode (@barcodes){
147 $barcode =~ s/\r$//; # strip any naughty return chars
148 next if $barcode eq '';
149 my $item = GetItem( 0, $barcode);
150 if (defined $item && $item->{itemnumber}) {
151 my $biblio = GetBiblioFromItemNumber( $item->{itemnumber} );
152 my $added = eval { $shelf->add_biblio( $biblio->{biblionumber}, $loggedinuser ); };
153 if ($@) {
154 push @messages, { item_barcode => $barcode, type => 'alert', code => ref($@), msg => $@ };
155 } elsif ( $added ) {
156 push @messages, { item_barcode => $barcode, type => 'message', code => 'success_on_add_biblio' };
157 } else {
158 push @messages, { item_barcode => $barcode, type => 'message', code => 'error_on_add_biblio' };
160 } else {
161 push @messages, { item_barcode => $barcode, type => 'alert', code => 'item_does_not_exist' };
164 } else {
165 push @messages, { type => 'alert', code => 'unauthorized_on_add_biblio' };
168 } else {
169 push @messages, { type => 'alert', code => 'does_not_exist' };
171 $op = $referer;
172 } elsif ( $op eq 'remove_biblios' ) {
173 $shelfnumber = $query->param('shelfnumber');
174 $shelf = Koha::Virtualshelves->find($shelfnumber);
175 my @biblionumbers = $query->multi_param('biblionumber');
176 if ($shelf) {
177 if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
178 my $number_of_biblios_removed = eval {
179 $shelf->remove_biblios(
181 biblionumbers => \@biblionumbers,
182 borrowernumber => $loggedinuser,
186 if ($@) {
187 push @messages, { type => 'alert', code => ref($@), msg => $@ };
188 } elsif ( $number_of_biblios_removed ) {
189 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
190 } else {
191 push @messages, { type => 'alert', code => 'no_biblio_removed' };
193 } else {
194 push @messages, { type => 'alert', code => 'unauthorized_on_remove_biblios' };
196 } else {
197 push @messages, { type => 'alert', code => 'does_not_exist' };
199 $op = $referer;
202 if ( $op eq 'view' ) {
203 $shelfnumber ||= $query->param('shelfnumber');
204 $shelf = Koha::Virtualshelves->find($shelfnumber);
205 if ( $shelf ) {
206 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
207 my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title'; # Passed in sorting overrides default sorting
208 $sortfield = 'title' unless grep {/^$sortfield$/}qw( title author copyrightdate itemcallnumber );
209 my $direction = $query->param('direction') || 'asc';
210 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
211 my ( $rows, $page );
212 unless ( $query->param('print') ) {
213 $rows = C4::Context->preference('numSearchResults') || 20;
214 $page = ( $query->param('page') ? $query->param('page') : 1 );
217 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.itemcallnumber' : $sortfield;
218 my $contents = $shelf->get_contents->search(
221 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
222 page => $page,
223 rows => $rows,
224 order_by => { "-$direction" => $order_by },
228 my $borrower = GetMember( borrowernumber => $loggedinuser );
230 my $xslfile = C4::Context->preference('XSLTListsDisplay');
231 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
232 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
234 my @items;
235 while ( my $content = $contents->next ) {
236 my $this_item;
237 my $biblionumber = $content->biblionumber;
238 my $record = GetMarcBiblio($biblionumber);
240 if ( $xslfile ) {
241 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "XSLTListsDisplay",
242 1, undef, $sysxml, $xslfile, $lang);
245 my $marcflavour = C4::Context->preference("marcflavour");
246 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
247 my $itemtypeinfo = getitemtypeinfo( $itemtype, 'intranet' );
248 my $biblio = Koha::Biblios->find( $content->biblionumber );
249 $this_item->{title} = $biblio->title;
250 $this_item->{author} = $biblio->author;
251 $this_item->{dateadded} = $content->dateadded;
252 $this_item->{imageurl} = $itemtypeinfo->{imageurl};
253 $this_item->{description} = $itemtypeinfo->{description};
254 $this_item->{notforloan} = $itemtypeinfo->{notforloan};
255 $this_item->{'coins'} = GetCOinSBiblio($record);
256 $this_item->{'subtitle'} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
257 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
258 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
259 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
260 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
262 unless ( defined $this_item->{size} ) {
264 #TT has problems with size
265 $this_item->{size} = q||;
268 # Getting items infos for location display
269 my @items_infos = &GetItemsLocationInfo( $biblionumber );
270 $this_item->{'ITEM_RESULTS'} = \@items_infos;
271 $this_item->{biblionumber} = $biblionumber;
272 push @items, $this_item;
275 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
277 borrowernumber => $loggedinuser,
278 add_allowed => 1,
279 category => 1,
282 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
284 borrowernumber => $loggedinuser,
285 add_allowed => 1,
286 category => 2,
290 $template->param(
291 add_to_some_private_shelves => $some_private_shelves,
292 add_to_some_public_shelves => $some_public_shelves,
293 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
294 can_remove_shelf => $shelf->can_be_deleted($loggedinuser),
295 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
296 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
297 sortfield => $sortfield,
298 itemsloop => \@items,
299 sortfield => $sortfield,
300 direction => $direction,
302 if ( $page ) {
303 my $pager = $contents->pager;
304 $template->param(
305 pagination_bar => pagination_bar(
306 q||, $pager->last_page - $pager->first_page + 1,
307 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
311 } else {
312 push @messages, { type => 'error', code => 'unauthorized_on_view' };
313 undef $shelf;
315 } else {
316 push @messages, { type => 'alert', code => 'does_not_exist' };
320 $template->param(
321 op => $op,
322 referer => $referer,
323 shelf => $shelf,
324 messages => \@messages,
325 category => $category,
326 print => scalar $query->param('print') || 0,
327 csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc' }) ],
330 output_html_with_http_headers $query, $cookie, $template->output;