Bug 17578 [QA Followup] - Update number of tests
[koha.git] / virtualshelves / shelves.pl
blob691e2153b692b83916733f3972a510da19efeae3
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 => 'error', code => 'unauthorized_on_update' };
63 $op = 'list';
65 } else {
66 push @messages, { type => 'error', 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 => 'error', code => ref($@), msg => $@ };
85 } elsif ( not $shelf ) {
86 push @messages, { type => 'error', code => 'error_on_insert' };
87 } else {
88 push @messages, { type => 'message', code => 'success_on_insert' };
89 $op = 'view';
91 } elsif ( $op eq 'edit' ) {
92 $shelfnumber = $query->param('shelfnumber');
93 $shelf = Koha::Virtualshelves->find($shelfnumber);
95 if ( $shelf ) {
96 $op = $referer;
97 if ( $shelf->can_be_managed( $loggedinuser ) ) {
98 $shelf->shelfname( scalar $query->param('shelfname') );
99 $shelf->sortfield( scalar $query->param('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 };
106 if ($@) {
107 push @messages, { type => 'error', code => 'error_on_update' };
108 $op = 'edit_form';
109 } else {
110 push @messages, { type => 'message', code => 'success_on_update' };
112 } else {
113 push @messages, { type => 'error', code => 'unauthorized_on_update' };
115 } else {
116 push @messages, { type => 'error', code => 'does_not_exist' };
118 } elsif ( $op eq 'delete' ) {
119 $shelfnumber = $query->param('shelfnumber');
120 $shelf = Koha::Virtualshelves->find($shelfnumber);
121 if ($shelf) {
122 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
123 eval { $shelf->delete; };
124 if ($@) {
125 push @messages, { type => 'error', code => ref($@), msg => $@ };
126 } else {
127 push @messages, { type => 'message', code => 'success_on_delete' };
129 } else {
130 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
132 } else {
133 push @messages, { type => 'error', code => 'does_not_exist' };
135 $op = 'list';
136 } elsif ( $op eq 'add_biblio' ) {
137 $shelfnumber = $query->param('shelfnumber');
138 $shelf = Koha::Virtualshelves->find($shelfnumber);
139 if ($shelf) {
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 ); };
146 if ($@) {
147 push @messages, { type => 'error', code => ref($@), msg => $@ };
148 } elsif ( $added ) {
149 push @messages, { type => 'message', code => 'success_on_add_biblio' };
150 } else {
151 push @messages, { type => 'message', code => 'error_on_add_biblio' };
153 } else {
154 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
156 } else {
157 push @messages, { type => 'error', code => 'item_does_not_exist' };
160 } else {
161 push @messages, { type => 'error', code => 'does_not_exist' };
163 $op = $referer;
164 } elsif ( $op eq 'remove_biblios' ) {
165 $shelfnumber = $query->param('shelfnumber');
166 $shelf = Koha::Virtualshelves->find($shelfnumber);
167 my @biblionumbers = $query->multi_param('biblionumber');
168 if ($shelf) {
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,
178 if ($@) {
179 push @messages, { type => 'error', code => ref($@), msg => $@ };
180 } elsif ( $number_of_biblios_removed ) {
181 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
182 } else {
183 push @messages, { type => 'error', code => 'no_biblio_removed' };
185 } else {
186 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
188 } else {
189 push @messages, { type => 'error', code => 'does_not_exist' };
191 $op = $referer;
194 if ( $op eq 'view' ) {
195 $shelfnumber ||= $query->param('shelfnumber');
196 $shelf = Koha::Virtualshelves->find($shelfnumber);
197 if ( $shelf ) {
198 if ( $shelf->can_be_viewed( $loggedinuser ) ) {
199 my $sortfield = $query->param('sortfield') || $shelf->sortfield || 'title'; # Passed in sorting overrides default sorting
200 my $direction = $query->param('direction') || 'asc';
201 $direction = 'asc' if $direction ne 'asc' and $direction ne 'desc';
202 my ( $rows, $page );
203 unless ( $query->param('print') ) {
204 $rows = C4::Context->preference('numSearchResults') || 20;
205 $page = ( $query->param('page') ? $query->param('page') : 1 );
208 my $order_by = $sortfield eq 'itemcallnumber' ? 'items.itemcallnumber' : $sortfield;
209 my $contents = $shelf->get_contents->search(
212 prefetch => [ { 'biblionumber' => { 'biblioitems' => 'items' } } ],
213 page => $page,
214 rows => $rows,
215 order_by => { "-$direction" => $order_by },
219 my $borrower = GetMember( borrowernumber => $loggedinuser );
221 my $xslfile = C4::Context->preference('XSLTListsDisplay');
222 my $lang = $xslfile ? C4::Languages::getlanguage() : undef;
223 my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
225 my @items;
226 while ( my $content = $contents->next ) {
227 my $this_item;
228 my $biblionumber = $content->biblionumber;
229 my $record = GetMarcBiblio($biblionumber);
231 if ( $xslfile ) {
232 $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "XSLTListsDisplay",
233 1, undef, $sysxml, $xslfile, $lang);
236 my $marcflavour = C4::Context->preference("marcflavour");
237 my $itemtype = Koha::Biblioitems->search({ biblionumber => $content->biblionumber })->next->itemtype;
238 my $itemtypeinfo = getitemtypeinfo( $itemtype, 'intranet' );
239 my $biblio = Koha::Biblios->find( $content->biblionumber );
240 $this_item->{title} = $biblio->title;
241 $this_item->{author} = $biblio->author;
242 $this_item->{dateadded} = $content->dateadded;
243 $this_item->{imageurl} = $itemtypeinfo->{imageurl};
244 $this_item->{description} = $itemtypeinfo->{description};
245 $this_item->{notforloan} = $itemtypeinfo->{notforloan};
246 $this_item->{'coins'} = GetCOinSBiblio($record);
247 $this_item->{'subtitle'} = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
248 $this_item->{'normalized_upc'} = GetNormalizedUPC( $record, $marcflavour );
249 $this_item->{'normalized_ean'} = GetNormalizedEAN( $record, $marcflavour );
250 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
251 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
253 unless ( defined $this_item->{size} ) {
255 #TT has problems with size
256 $this_item->{size} = q||;
259 # Getting items infos for location display
260 my @items_infos = &GetItemsLocationInfo( $biblionumber );
261 $this_item->{'ITEM_RESULTS'} = \@items_infos;
262 $this_item->{biblionumber} = $biblionumber;
263 push @items, $this_item;
266 my $some_private_shelves = Koha::Virtualshelves->get_some_shelves(
268 borrowernumber => $loggedinuser,
269 add_allowed => 1,
270 category => 1,
273 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
275 borrowernumber => $loggedinuser,
276 add_allowed => 1,
277 category => 2,
281 $template->param(
282 add_to_some_private_shelves => $some_private_shelves,
283 add_to_some_public_shelves => $some_public_shelves,
284 can_manage_shelf => $shelf->can_be_managed($loggedinuser),
285 can_remove_shelf => $shelf->can_be_deleted($loggedinuser),
286 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
287 can_add_biblios => $shelf->can_biblios_be_added($loggedinuser),
288 sortfield => $sortfield,
289 itemsloop => \@items,
290 sortfield => $sortfield,
291 direction => $direction,
293 if ( $page ) {
294 my $pager = $contents->pager;
295 $template->param(
296 pagination_bar => pagination_bar(
297 q||, $pager->last_page - $pager->first_page + 1,
298 $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
302 } else {
303 push @messages, { type => 'error', code => 'unauthorized_on_view' };
304 undef $shelf;
306 } else {
307 push @messages, { type => 'error', code => 'does_not_exist' };
311 $template->param(
312 op => $op,
313 referer => $referer,
314 shelf => $shelf,
315 messages => \@messages,
316 category => $category,
317 print => scalar $query->param('print') || 0,
318 csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc' }) ],
321 output_html_with_http_headers $query, $cookie, $template->output;