Bug 18001 - Unit Test
[koha.git] / cataloguing / value_builder / marc21_linking_section.pl
blob7efa49eee37c714a4a24bda1eccffd7ca5de69b9
1 #!/usr/bin/perl
3 # Converted to new plugin style (Bug 13437)
5 # Copyright Biblibre 2007 - CILEA 2011
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Modern::Perl;
24 use CGI qw ( -utf8 );
25 use C4::Output;
26 use C4::Context;
27 use C4::Search;
28 use C4::Auth;
29 use C4::Output;
31 use C4::Biblio;
32 use C4::Koha;
33 use MARC::Record;
35 use Koha::ItemTypes;
37 use Koha::SearchEngine;
38 use Koha::SearchEngine::Search;
40 my $builder = sub {
41 my ( $params ) = @_;
42 my $function_name = $params->{id};
43 my $res = "
44 <script type='text/javascript'>
45 function Click$function_name(event) {
46 defaultvalue=document.getElementById(event.data.id).value;
47 window.open(\"/cgi-bin/koha/cataloguing/plugin_launcher.pl?plugin_name=marc21_linking_section.pl&index=\" + event.data.id + \"&result=\"+defaultvalue, 'tag_editor', 'width=900,height=700,toolbar=false,scrollbars=yes');
50 </script>
53 return $res;
56 my $launcher = sub {
57 my ( $params ) = @_;
58 my $query = $params->{cgi};
59 my $dbh = C4::Context->dbh;
60 my $op = $query->param('op');
61 # -- op could be equal to
62 # * fillinput
63 # * do_search
65 my $type = $query->param('type');
66 my $startfrom = $query->param('startfrom');
67 $startfrom = 0 if ( !defined $startfrom );
68 my ( $template, $loggedinuser, $cookie );
69 my $resultsperpage;
71 if ( $op eq "fillinput" ) {
72 my $biblionumber = $query->param('biblionumber');
73 my $index = $query->param('index');
74 my $marcrecord;
76 # open template
77 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
78 { template_name => "cataloguing/value_builder/marc21_linking_section.tt",
79 query => $query,
80 type => "intranet",
81 authnotrequired => 0,
82 flagsrequired => { editcatalogue => '*' },
83 debug => 1,
87 #get marc record
88 $marcrecord = GetMarcBiblio($biblionumber);
90 my $subfield_value_9 = $biblionumber;
91 my $subfield_value_0 = $biblionumber;
93 #my $subfield_value_0;
94 #$subfield_value_0 = $marcrecord->field('001')->data
95 # if $marcrecord->field('001');
96 my $subfield_value_w;
97 if ( $marcrecord->field('001') ) {
98 $subfield_value_w = $marcrecord->field('001')->data;
99 } else {
100 $subfield_value_w = $biblionumber;
103 my $subfield_value_a;
104 my $subfield_value_c;
105 my $subfield_value_d;
106 my $subfield_value_e;
108 my $subfield_value_h;
110 my $subfield_value_i;
112 my $subfield_value_p;
114 my $subfield_value_t;
115 if ( $marcrecord->field('245') ) {
116 $subfield_value_t = $marcrecord->title();
119 my $subfield_value_u;
120 my $subfield_value_v;
121 my $subfield_value_x;
122 my $subfield_value_y;
123 my $subfield_value_z;
125 $subfield_value_x = $marcrecord->field('022')->subfield("a")
126 if ( $marcrecord->field('022') );
127 $subfield_value_z = $marcrecord->field('020')->subfield("a")
128 if ( $marcrecord->field('020') );
130 # escape the 's
131 $subfield_value_9 =~ s/'/\\'/g;
132 $subfield_value_0 =~ s/'/\\'/g;
133 $subfield_value_a =~ s/'/\\'/g;
134 $subfield_value_c =~ s/'/\\'/g;
135 $subfield_value_d =~ s/'/\\'/g;
136 $subfield_value_e =~ s/'/\\'/g;
137 $subfield_value_h =~ s/'/\\'/g;
138 $subfield_value_i =~ s/'/\\'/g;
139 $subfield_value_p =~ s/'/\\'/g;
140 $subfield_value_t =~ s/'/\\'/g;
141 $subfield_value_u =~ s/'/\\'/g;
142 $subfield_value_v =~ s/'/\\'/g;
143 $subfield_value_w =~ s/'/\\'/g;
144 $subfield_value_x =~ s/'/\\'/g;
145 $subfield_value_y =~ s/'/\\'/g;
146 $subfield_value_z =~ s/'/\\'/g;
147 $template->param(
148 fillinput => 1,
149 index => scalar $query->param('index') . "",
150 biblionumber => $biblionumber ? $biblionumber : "",
151 subfield_value_9 => "$subfield_value_9",
152 subfield_value_0 => "$subfield_value_0",
153 subfield_value_a => "$subfield_value_a",
154 subfield_value_c => "$subfield_value_c",
155 subfield_value_d => "$subfield_value_d",
156 subfield_value_e => "$subfield_value_e",
157 subfield_value_h => "$subfield_value_h",
158 subfield_value_i => "$subfield_value_i",
159 subfield_value_p => "$subfield_value_p",
160 subfield_value_t => "$subfield_value_t",
161 subfield_value_u => "$subfield_value_u",
162 subfield_value_v => "$subfield_value_v",
163 subfield_value_w => "$subfield_value_w",
164 subfield_value_x => "$subfield_value_x",
165 subfield_value_y => "$subfield_value_y",
166 subfield_value_z => "$subfield_value_z",
168 ###############################################################
169 } elsif ( $op eq "do_search" ) {
170 my $search = $query->param('search');
171 my $itype = $query->param('itype');
172 my $startfrom = $query->param('startfrom');
173 my $resultsperpage = $query->param('resultsperpage') || 20;
174 my $orderby;
175 my $QParser;
176 $QParser = C4::Context->queryparser if ( C4::Context->preference('UseQueryParser') );
177 my $op;
179 if ($QParser) {
180 $op = '&&';
181 } else {
182 $op = 'and';
184 my $searcher = Koha::SearchEngine::Search->new(
185 { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
186 $search = 'kw:' . $search . " $op mc-itemtype:" . $itype if $itype;
187 my ( $errors, $results, $total_hits ) =
188 $searcher->simple_search_compat( $search,
189 $startfrom * $resultsperpage,
190 $resultsperpage );
191 if ( defined $errors ) {
192 $results = [];
194 my $total = @{$results};
196 # warn " biblio count : ".$total;
198 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
199 { template_name => "cataloguing/value_builder/marc21_linking_section.tt",
200 query => $query,
201 type => 'intranet',
202 authnotrequired => 0,
203 debug => 1,
207 # multi page display gestion
208 my $displaynext = 0;
209 my $displayprev = $startfrom;
211 if ( ( $total_hits - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
212 $displaynext = 1;
214 my @arrayresults;
215 my @field_data = ($search);
216 for ( my $i = 0 ; $i < $resultsperpage ; $i++ ) {
217 my $record = C4::Search::new_record_from_zebra( 'biblioserver', $results->[$i] );
218 my $rechash = TransformMarcToKoha( $record );
219 my $pos;
220 my $countitems = $rechash->{itembumber} ? 1 : 0;
221 while ( index( $rechash->{itemnumber}, '|', $pos ) > 0 ) {
222 $countitems += 1;
223 $pos = index( $rechash->{itemnumber}, '|', $pos ) + 1;
225 $rechash->{totitem} = $countitems;
226 my @holdingbranches = split /\|/, $rechash->{holdingbranch};
227 my @itemcallnumbers = split /\|/, $rechash->{itemcallnumber};
228 my $CN;
229 for ( my $i = 0 ; $i < @holdingbranches ; $i++ ) {
230 $CN .= $holdingbranches[$i] . " ( " . $itemcallnumbers[$i] . " ) |";
232 $CN =~ s/ \|$//;
233 $rechash->{CN} = $CN;
234 push @arrayresults, $rechash;
237 # for(my $i = 0 ; $i <= $#marclist ; $i++)
239 # push @field_data, { term => "marclist", val=>$marclist[$i] };
240 # push @field_data, { term => "and_or", val=>$and_or[$i] };
241 # push @field_data, { term => "excluding", val=>$excluding[$i] };
242 # push @field_data, { term => "operator", val=>$operator[$i] };
243 # push @field_data, { term => "value", val=>$value[$i] };
246 my @numbers = ();
248 if ( $total > $resultsperpage ) {
249 for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
250 if ( $i < 16 ) {
251 my $highlight = 0;
252 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
253 push @numbers,
254 { number => $i,
255 highlight => $highlight,
256 searchdata => \@field_data,
257 startfrom => ( $i - 1 )
263 my $from = $startfrom * $resultsperpage + 1;
264 my $to;
266 if ( $total_hits < $from + $resultsperpage ) {
267 $to = $total_hits;
268 } else {
269 $to = $from + $resultsperpage;
271 my $defaultview = 'BiblioDefaultView' . C4::Context->preference('BiblioDefaultView');
273 # my $link="/cgi-bin/koha/cataloguing/value_builder/unimarc4XX.pl?op=do_search&q=$search_desc&resultsperpage=$resultsperpage&startfrom=$startfrom&search=$search";
274 # foreach my $sort (@sort_by){
275 # $link.="&sort_by=".$sort."&";
277 # $template->param(
278 # pagination_bar => pagination_bar(
279 # $link,
280 # getnbpages($hits, $results_per_page),
281 # $page,
282 # 'page'
283 # ),
284 # );
285 $template->param(
286 result => \@arrayresults,
287 index => scalar $query->param('index') . "",
288 startfrom => $startfrom,
289 displaynext => $displaynext,
290 displayprev => $displayprev,
291 resultsperpage => $resultsperpage,
292 orderby => $orderby,
293 startfromnext => $startfrom + 1,
294 startfromprev => $startfrom - 1,
295 searchdata => \@field_data,
296 total => $total_hits,
297 from => $from,
298 to => $to,
299 numbers => \@numbers,
300 search => $search,
301 $defaultview => 1,
302 Search => 0
305 } else {
306 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
307 { template_name => "cataloguing/value_builder/marc21_linking_section.tt",
308 query => $query,
309 type => "intranet",
310 authnotrequired => 0,
314 my @itemtypes = Koha::ItemTypes->search;
316 $template->param(
317 itypeloop => \@itemtypes,
318 index => scalar $query->param('index'),
319 Search => 1,
322 output_html_with_http_headers $query, $cookie, $template->output;
325 return { builder => $builder, launcher => $launcher };