Bug 25898: Prohibit indirect object notation
[koha.git] / reports / itemslost.pl
blob3fcb75d632752d02ee7c2a1e5f99b418077a43d0
1 #!/usr/bin/perl
3 # Copyright Liblime 2007
4 # Copyright Biblibre 2009
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 =head1 itemslost
24 This script displays lost items.
26 =cut
28 use Modern::Perl;
30 use CGI qw ( -utf8 );
31 use Text::CSV_XS;
32 use C4::Auth;
33 use C4::Output;
34 use C4::Biblio;
35 use C4::Items;
37 use Koha::AuthorisedValues;
38 use Koha::CsvProfiles;
39 use Koha::DateUtils;
41 my $query = CGI->new;
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44 template_name => "reports/itemslost.tt",
45 query => $query,
46 type => "intranet",
47 flagsrequired => { reports => '*' },
48 debug => 1,
52 my $params = $query->Vars;
53 my $get_items = $params->{'get_items'};
54 my $op = $query->param('op') || '';
56 if ( $op eq 'export' ) {
57 my @itemnumbers = $query->multi_param('itemnumber');
58 my $csv_profile_id = $query->param('csv_profile_id');
59 my @rows;
60 if ($csv_profile_id) {
61 # FIXME This following code has the same logic as GetBasketAsCSV
62 # We should refactor all the CSV export code
63 # Note: For MARC it is already done in Koha::Exporter::Record but not for SQL CSV profiles type
64 my $csv_profile = Koha::CsvProfiles->find( $csv_profile_id );
65 die "There is no valid csv profile given" unless $csv_profile;
67 my $csv = Text::CSV_XS->new({'quote_char'=>'"','escape_char'=>'"','sep_char'=>$csv_profile->csv_separator,'binary'=>1});
68 my $csv_profile_content = $csv_profile->content;
69 my ( @headers, @fields );
70 while ( $csv_profile_content =~ /
71 ([^=\|]+) # header
73 ([^\|]*) # fieldname (table.row or row)
74 \|? /gxms
75 ) {
76 my $header = $1;
77 my $field = ($2 eq '') ? $1 : $2;
79 $header =~ s/^\s+|\s+$//g; # Trim whitespaces
80 push @headers, $header;
82 $field =~ s/[^\.]*\.{1}//; # Remove the table name if exists.
83 $field =~ s/^\s+|\s+$//g; # Trim whitespaces
84 push @fields, $field;
86 my $items = Koha::Items->search({ itemnumber => { -in => \@itemnumbers } });
87 while ( my $item = $items->next ) {
88 my @row;
89 my $all_fields = $item->unblessed;
90 $all_fields = { %$all_fields, %{$item->biblio->unblessed}, %{$item->biblioitem->unblessed} };
91 for my $field (@fields) {
92 push @row, $all_fields->{$field};
94 push @rows, \@row;
96 my $content = join( $csv_profile->csv_separator, @headers ) . "\n";
97 for my $row ( @rows ) {
98 $csv->combine(@$row);
99 my $string = $csv->string;
100 $content .= $string . "\n";
102 print $query->header(
103 -type => 'text/csv',
104 -attachment => 'lost_items.csv',
106 print $content;
107 exit;
109 } elsif ( $get_items ) {
110 my $branchfilter = $params->{'branchfilter'} || undef;
111 my $barcodefilter = $params->{'barcodefilter'} || undef;
112 my $itemtypesfilter = $params->{'itemtypesfilter'} || undef;
113 my $loststatusfilter = $params->{'loststatusfilter'} || undef;
114 my $notforloanfilter = $params->{'notforloanfilter'} || undef;
116 my $params = {
117 ( $branchfilter ? ( homebranch => $branchfilter ) : () ),
119 $loststatusfilter
120 ? ( itemlost => $loststatusfilter )
121 : ( itemlost => { '!=' => 0 } )
124 $notforloanfilter
125 ? ( notforloan => $notforloanfilter )
126 : ()
128 ( $barcodefilter ? ( barcode => { like => "%$barcodefilter%" } ) : () ),
131 my $attributes;
132 if ($itemtypesfilter) {
133 if ( C4::Context->preference('item-level_itypes') ) {
134 $params->{itype} = $itemtypesfilter;
136 else {
137 # We want a join on biblioitems
138 $attributes = { join => 'biblioitem' };
139 $params->{'biblioitem.itemtype'} = $itemtypesfilter;
143 my $items = Koha::Items->search( $params, $attributes );
145 $template->param(
146 items => $items,
147 get_items => $get_items,
151 # getting all itemtypes
152 my $itemtypes = Koha::ItemTypes->search_with_localization;
154 my $csv_profiles = Koha::CsvProfiles->search({ type => 'sql', used_for => 'export_lost_items' });
156 $template->param(
157 itemtypes => $itemtypes,
158 csv_profiles => $csv_profiles,
161 # writing the template
162 output_html_with_http_headers $query, $cookie, $template->output;