Bug 14544: Get rid of GetShelfContent
[koha.git] / virtualshelves / shelves.pl
1 #!/usr/bin/perl
2
3 # Copyright 2015 Koha Team
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use C4::VirtualShelves;
23 use C4::Auth;
24 use C4::Biblio;
25 use C4::Csv;
26 use C4::Koha;
27 use C4::Items;
28 use C4::Members;
29 use C4::Output;
30 use C4::XSLT;
31 use Koha::Virtualshelves;
32
33 my $query = new CGI;
34
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 },
41     }
42 );
43
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 );
48
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);
54
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';
62         }
63     } else {
64         push @messages, { type => 'error', code => 'does_not_exist' };
65     }
66 } elsif ( $op eq 'add' ) {
67     eval {
68         $shelf = Koha::Virtualshelf->new(
69             {   shelfname          => $query->param('shelfname'),
70                 sortfield          => $query->param('sortfield'),
71                 category           => $query->param('category'),
72                 allow_add          => $query->param('allow_add'),
73                 allow_delete_own   => $query->param('allow_delete_own'),
74                 allow_delete_other => $query->param('allow_delete_other'),
75                 owner              => $query->param('owner'),
76             }
77         );
78         $shelf->store;
79         $shelfnumber = $shelf->shelfnumber;
80     };
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';
88     }
89 } elsif ( $op eq 'edit' ) {
90     $shelfnumber = $query->param('shelfnumber');
91     $shelf       = Koha::Virtualshelves->find($shelfnumber);
92
93     if ( $shelf ) {
94         $op = $referer;
95         if ( $shelf->can_be_managed( $loggedinuser ) ) {
96             $shelf->shelfname( $query->param('shelfname') );
97             $shelf->sortfield( $query->param('sortfield') );
98             $shelf->allow_add( $query->param('allow_add') );
99             $shelf->allow_delete_own( $query->param('allow_delete_own') );
100             $shelf->allow_delete_other( $query->param('allow_delete_other') );
101             $shelf->category( $query->param('category') );
102             eval { $shelf->store };
103
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' };
109             }
110         } else {
111             push @messages, { type => 'error', code => 'unauthorized_on_update' };
112         }
113     } else {
114         push @messages, { type => 'error', code => 'does_not_exist' };
115     }
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' };
126             }
127         } else {
128             push @messages, { type => 'error', code => 'unauthorized_on_delete' };
129         }
130     } else {
131         push @messages, { type => 'error', code => 'does_not_exist' };
132     }
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' };
150                     }
151                 } else {
152                     push @messages, { type => 'error', code => 'unauthorized_on_add_biblio' };
153                 }
154             } else {
155                 push @messages, { type => 'error', code => 'item_does_not_exist' };
156             }
157         }
158     } else {
159         push @messages, { type => 'error', code => 'does_not_exist' };
160     }
161     $op = $referer;
162 } elsif ( $op eq 'remove_biblios' ) {
163     $shelfnumber = $query->param('shelfnumber');
164     $shelf = Koha::Virtualshelves->find($shelfnumber);
165     my @biblionumbers = $query->param('biblionumber');
166     if ($shelf) {
167         if ( $shelf->can_biblios_be_removed( $loggedinuser ) ) {
168             my $number_of_biblios_removed = eval {
169                 $shelf->remove_biblios(
170                     {
171                         biblionumbers => \@biblionumbers,
172                         borrowernumber => $loggedinuser,
173                     }
174                 );
175             };
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' };
182             }
183         } else {
184             push @messages, { type => 'error', code => 'unauthorized_on_remove_biblios' };
185         }
186     } else {
187         push @messages, { type => 'error', code => 'does_not_exist' };
188     }
189     $op = $referer;
190 }
191
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;    # Passed in sorting overrides default sorting
198             my $direction = $query->param('direction') || 'asc';
199             my ( $rows, $page );
200             unless ( $query->param('print') ) {
201                 $rows = C4::Context->preference('numSearchResults') || 20;
202                 $page = ( $query->param('page') ? $query->param('page') : 1 );
203             }
204
205             my $contents = $shelf->get_contents->search({}, { join => [ 'biblionumber' ], page => $page, rows => $rows, order_by => "$sortfield $direction", });
206
207             my $borrower = GetMember( borrowernumber => $loggedinuser );
208
209             my @items;
210             while ( my $content = $contents->next ) {
211                 my $this_item;
212                 my $biblionumber = $content->biblionumber->biblionumber;
213                 my $record       = GetMarcBiblio($biblionumber);
214
215                 if ( C4::Context->preference("XSLTResultsDisplay") ) {
216                     $this_item->{XSLTBloc} = XSLTParse4Display( $biblionumber, $record, "XSLTResultsDisplay" );
217                 }
218
219                 my $marcflavour = C4::Context->preference("marcflavour");
220                 my $itemtypeinfo = getitemtypeinfo( $content->biblionumber->biblioitems->first->itemtype, 'intranet' );
221                 $this_item->{imageurl}          = $itemtypeinfo->{imageurl};
222                 $this_item->{description}       = $itemtypeinfo->{description};
223                 $this_item->{notforloan}        = $itemtypeinfo->{notforloan};
224                 $this_item->{'coins'}           = GetCOinSBiblio($record);
225                 $this_item->{'subtitle'}        = GetRecordValue( 'subtitle', $record, GetFrameworkCode( $biblionumber ) );
226                 $this_item->{'normalized_upc'}  = GetNormalizedUPC( $record, $marcflavour );
227                 $this_item->{'normalized_ean'}  = GetNormalizedEAN( $record, $marcflavour );
228                 $this_item->{'normalized_oclc'} = GetNormalizedOCLCNumber( $record, $marcflavour );
229                 $this_item->{'normalized_isbn'} = GetNormalizedISBN( undef, $record, $marcflavour );
230
231                 unless ( defined $this_item->{size} ) {
232
233                     #TT has problems with size
234                     $this_item->{size} = q||;
235                 }
236
237                 # Getting items infos for location display
238                 my @items_infos = &GetItemsLocationInfo( $biblionumber );
239                 $this_item->{'ITEM_RESULTS'} = \@items_infos;
240                 $this_item->{biblionumber} = $biblionumber;
241                 push @items, $this_item;
242             }
243
244             # Build drop-down list for 'Add To:' menu...
245             my ( $totalref, $pubshelves, $barshelves ) = C4::VirtualShelves::GetSomeShelfNames( $loggedinuser, 'COMBO', 1 );
246             $template->param(
247                 addbarshelves      => $totalref->{bartotal},
248                 addbarshelvesloop  => $barshelves,
249                 addpubshelves      => $totalref->{pubtotal},
250                 addpubshelvesloop  => $pubshelves,
251                 can_manage_shelf   => $shelf->can_be_managed($loggedinuser),
252                 can_remove_shelf   => $shelf->can_be_deleted($loggedinuser),
253                 can_remove_biblios => $shelf->can_biblios_be_removed($loggedinuser),
254                 can_add_biblios    => $shelf->can_biblios_be_added($loggedinuser),
255                 sortfield          => $sortfield,
256                 itemsloop          => \@items,
257                 sortfield          => $sortfield,
258                 direction          => $direction,
259             );
260             if ( $page ) {
261                 my $pager = $contents->pager;
262                 $template->param(
263                     pagination_bar => pagination_bar(
264                         q||, $pager->last_page - $pager->first_page + 1,
265                         $page, "page", { op => 'view', shelfnumber => $shelf->shelfnumber, sortfield => $sortfield, direction => $direction, }
266                     ),
267                 );
268             }
269         } else {
270             push @messages, { type => 'error', code => 'unauthorized_on_view' };
271         }
272     } else {
273         push @messages, { type => 'error', code => 'does_not_exist' };
274     }
275 }
276
277 $template->param(
278     op       => $op,
279     referer  => $referer,
280     shelf    => $shelf,
281     messages => \@messages,
282     category => $category,
283     print    => $query->param('print') || 0,
284     csv_profiles => GetCsvProfilesLoop('marc'),
285 );
286
287 output_html_with_http_headers $query, $cookie, $template->output;