Bug 17310: Broken URLs in 'Item renewed' / 'Cannot renew' messages
[koha.git] / virtualshelves / shelves.pl
blob11a90aed431f27a33cb06709515f698d0d2239f4
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::CsvProfiles;
31 use Koha::Virtualshelves;
33 my $query = new CGI;
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36 { template_name => "virtualshelves/shelves.tt",
37 query => $query,
38 type => "intranet",
39 authnotrequired => 0,
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' ) {
50 # Nothing to do
51 } elsif ( $op eq 'edit_form' ) {
52 $shelfnumber = $query->param('shelfnumber');
53 $shelf = Koha::Virtualshelves->find($shelfnumber);
55 if ( $shelf ) {
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' };
61 $op = 'list';
63 } else {
64 push @messages, { type => 'error', code => 'does_not_exist' };
66 } elsif ( $op eq 'add' ) {
67 eval {
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'),
78 $shelf->store;
79 $shelfnumber = $shelf->shelfnumber;
81 if ($@) {
82 push @messages, { type => 'error', code => ref($@), msg => $@ };
83 } elsif ( not $shelf ) {
84 push @messages, { type => 'error', code => 'error_on_insert' };
85 } else {
86 push @messages, { type => 'message', code => 'success_on_insert' };
87 $op = 'view';
89 } elsif ( $op eq 'edit' ) {
90 $shelfnumber = $query->param('shelfnumber');
91 $shelf = Koha::Virtualshelves->find($shelfnumber);
93 if ( $shelf ) {
94 $op = $referer;
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 };
104 if ($@) {
105 push @messages, { type => 'error', code => 'error_on_update' };
106 $op = 'edit_form';
107 } else {
108 push @messages, { type => 'message', code => 'success_on_update' };
110 } else {
111 push @messages, { type => 'error', code => 'unauthorized_on_update' };
113 } else {
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);
119 if ($shelf) {
120 if ( $shelf->can_be_deleted( $loggedinuser ) ) {
121 eval { $shelf->delete; };
122 if ($@) {
123 push @messages, { type => 'error', code => ref($@), msg => $@ };
124 } else {
125 push @messages, { type => 'message', code => 'success_on_delete' };
127 } else {
128 push @messages, { type => 'error', code => 'unauthorized_on_delete' };
130 } else {
131 push @messages, { type => 'error', code => 'does_not_exist' };
133 $op = 'list';
134 } elsif ( $op eq 'add_biblio' ) {
135 $shelfnumber = $query->param('shelfnumber');
136 $shelf = Koha::Virtualshelves->find($shelfnumber);
137 if ($shelf) {
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 ); };
144 if ($@) {
145 push @messages, { type => 'error', code => ref($@), msg => $@ };
146 } elsif ( $added ) {
147 push @messages, { type => 'message', code => 'success_on_add_biblio' };
148 } else {
149 push @messages, { type => 'message', code => 'error_on_add_biblio' };
151 } else {
152 push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
154 } else {
155 push @messages, { type => 'error', code => 'item_does_not_exist' };
158 } else {
159 push @messages, { type => 'error', code => 'does_not_exist' };
161 $op = $referer;
162 } elsif ( $op eq 'remove_biblios' ) {
163 $shelfnumber = $query->param('shelfnumber');
164 $shelf = Koha::Virtualshelves->find($shelfnumber);
165 my @biblionumbers = $query->multi_param('biblionumber');
166 if ($shelf) {
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,
176 if ($@) {
177 push @messages, { type => 'error', code => ref($@), msg => $@ };
178 } elsif ( $number_of_biblios_removed ) {
179 push @messages, { type => 'message', code => 'success_on_remove_biblios' };
180 } else {
181 push @messages, { type => 'error', code => 'no_biblio_removed' };
183 } else {
184 push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
186 } else {
187 push @messages, { type => 'error', code => 'does_not_exist' };
189 $op = $referer;
192 if ( $op eq 'view' ) {
193 $shelfnumber ||= $query->param('shelfnumber');
194 $shelf = Koha::Virtualshelves->find($shelfnumber);
195 if ( $shelf ) {
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';
200 my ( $rows, $page );
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' } } ],
211 page => $page,
212 rows => $rows,
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;
223 my @items;
224 while ( my $content = $contents->next ) {
225 my $this_item;
226 my $biblionumber = $content->biblionumber->biblionumber;
227 my $record = GetMarcBiblio($biblionumber);
229 if ( $xslfile ) {
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,
265 add_allowed => 1,
266 category => 1,
269 my $some_public_shelves = Koha::Virtualshelves->get_some_shelves(
271 borrowernumber => $loggedinuser,
272 add_allowed => 1,
273 category => 2,
277 $template->param(
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,
289 if ( $page ) {
290 my $pager = $contents->pager;
291 $template->param(
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, }
298 } else {
299 push @messages, { type => 'error', code => 'unauthorized_on_view' };
300 undef $shelf;
302 } else {
303 push @messages, { type => 'error', code => 'does_not_exist' };
307 $template->param(
308 op => $op,
309 referer => $referer,
310 shelf => $shelf,
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;