Bug 6679 Avoid critic failure caused by ambiguous code
[koha.git] / cataloguing / value_builder / marc21_linking_section.pl
1 #!/usr/bin/perl
2
3 # Copyright Biblibre 2007 - CILEA 2011
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 use CGI;
24 use C4::Output;
25 use C4::Context;
26 use C4::Search;
27 use C4::Auth;
28 use C4::Output;
29
30 use C4::Biblio;
31 use C4::Koha;
32 use MARC::Record;
33 use C4::Branch;
34 use C4::ItemType;
35
36 sub plugin_parameters {
37  my ( $dbh, $record, $tagslib, $i, $tabloop ) = @_;
38      return "";
39 }
40
41 sub plugin_javascript {
42   my ( $dbh, $record, $tagslib, $field_number, $tabloop ) = @_;
43   my $function_name = $field_number;
44      my $res           = "
45   <script type='text/javascript'>
46                 function Focus$function_name(subfield_managed) {
47                        return 1;
48               }
49
50              function Blur$function_name(subfield_managed) {
51                         return 1;
52               }
53
54              function Clic$function_name(i) {
55                        defaultvalue=document.getElementById(\"$field_number\").value;
56                  window.open(\"/cgi-bin/koha/cataloguing/plugin_launcher.pl?plugin_name=marc21_linking_section.pl&index=\" + i + \"&result=\"+defaultvalue,\"marc21_field_7\"+i+\"\",'width=900,height=700,toolbar=false,scrollbars=yes');
57
58              }
59       </script>
60       ";
61
62     return ( $function_name, $res );
63 }
64
65 # sub plugin
66 #
67 # input arg :
68 # -- op could be equals to
69 # * fillinput :
70 # * do_search :
71 #
72
73 sub plugin {
74     my ($input)   = @_;
75     my $dbh       = C4::Context->dbh;
76       my $query     = new CGI;
77        my $op        = $query->param('op');
78    my $type      = $query->param('type');
79  my $startfrom = $query->param('startfrom');
80     $startfrom = 0 if ( !defined $startfrom );
81      my ( $template, $loggedinuser, $cookie );
82       my $resultsperpage;
83     my $searchdesc;
84
85        if ( $op eq "fillinput" ) {
86             my $biblionumber = $query->param('biblionumber');
87               my $index        = $query->param('index');
88              my $marcrecord;
89
90                # open template
91                 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
92                  {
93                               template_name =>
94                                  "cataloguing/value_builder/marc21_linking_section.tt",
95                                query           => $query,
96                              type            => "intranet",
97                          authnotrequired => 0,
98                           flagsrequired   => { editcatalogue => '*' },
99                            debug           => 1,
100                   }
101               );
102
103             #get marc record
104                $marcrecord = GetMarcBiblio($biblionumber);
105
106            my $subfield_value_9 = $biblionumber;
107           my $subfield_value_0 = $biblionumber;
108
109          #my $subfield_value_0;
110          #$subfield_value_0 = $marcrecord->field('001')->data
111            #  if $marcrecord->field('001');
112                my $subfield_value_w;
113           if ( $marcrecord->field('001') ) {
114                      $subfield_value_w = $marcrecord->field('001')->data;
115            }
116               else {
117                  $subfield_value_w = $biblionumber;
118              }
119
120              my $subfield_value_a;
121           my $subfield_value_c;
122           my $subfield_value_d;
123           my $subfield_value_e;
124
125          my $subfield_value_h;
126
127          my $subfield_value_i;
128
129          my $subfield_value_p;
130
131          my $subfield_value_t;
132           if ( $marcrecord->field('245') ) {
133                      $subfield_value_t = $marcrecord->title();
134               }
135
136              my $subfield_value_u;
137           my $subfield_value_v;
138           my $subfield_value_x;
139           my $subfield_value_y;
140           my $subfield_value_z;
141
142          $subfield_value_x = $marcrecord->field('022')->subfield("a")
143              if ( $marcrecord->field('022') );
144             $subfield_value_z = $marcrecord->field('020')->subfield("a")
145              if ( $marcrecord->field('020') );
146
147            # escape the 's
148                 $subfield_value_9 =~ s/'/\\'/g;
149                 $subfield_value_0 =~ s/'/\\'/g;
150                 $subfield_value_a =~ s/'/\\'/g;
151                 $subfield_value_c =~ s/'/\\'/g;
152                 $subfield_value_d =~ s/'/\\'/g;
153                 $subfield_value_e =~ s/'/\\'/g;
154                 $subfield_value_h =~ s/'/\\'/g;
155                 $subfield_value_i =~ s/'/\\'/g;
156                 $subfield_value_p =~ s/'/\\'/g;
157                 $subfield_value_t =~ s/'/\\'/g;
158                 $subfield_value_u =~ s/'/\\'/g;
159                 $subfield_value_v =~ s/'/\\'/g;
160                 $subfield_value_w =~ s/'/\\'/g;
161                 $subfield_value_x =~ s/'/\\'/g;
162                 $subfield_value_y =~ s/'/\\'/g;
163                 $subfield_value_z =~ s/'/\\'/g;
164                 $template->param(
165                       fillinput        => 1,
166                  index            => $query->param('index') . "",
167                        biblionumber     => $biblionumber ? $biblionumber : "",
168                         subfield_value_9 => "$subfield_value_9",
169                        subfield_value_0 => "$subfield_value_0",
170                        subfield_value_a => "$subfield_value_a",
171                        subfield_value_c => "$subfield_value_c",
172                        subfield_value_d => "$subfield_value_d",
173                        subfield_value_e => "$subfield_value_e",
174                        subfield_value_h => "$subfield_value_h",
175                        subfield_value_i => "$subfield_value_i",
176                        subfield_value_p => "$subfield_value_p",
177                        subfield_value_t => "$subfield_value_t",
178                        subfield_value_u => "$subfield_value_u",
179                        subfield_value_v => "$subfield_value_v",
180                        subfield_value_w => "$subfield_value_w",
181                        subfield_value_x => "$subfield_value_x",
182                        subfield_value_y => "$subfield_value_y",
183                        subfield_value_z => "$subfield_value_z",
184                );
185 ###############################################################
186      }
187       elsif ( $op eq "do_search" ) {
188          my $search         = $query->param('search');
189           my $itype          = $query->param('itype');
190            my $startfrom      = $query->param('startfrom');
191                my $resultsperpage = $query->param('resultsperpage') || 20;
192             my $orderby;
193            $search = 'kw,wrdl=' . $search . ' and mc-itemtype=' . $itype if $itype;
194                my ( $errors, $results, $total_hits ) =
195                   SimpleSearch( $search, $startfrom * $resultsperpage,
196                  $resultsperpage );
197              if ( defined $errors ) {
198                        $results = [];
199          }
200               my $total = @{$results};
201
202               #        warn " biblio count : ".$total;
203
204               ( $template, $loggedinuser, $cookie ) = get_template_and_user(
205                  {
206                               template_name =>
207                                  "cataloguing/value_builder/marc21_linking_section.tt",
208                                query           => $query,
209                              type            => 'intranet',
210                          authnotrequired => 0,
211                           debug           => 1,
212                   }
213               );
214
215             # multi page display gestion
216            my $displaynext = 0;
217            my $displayprev = $startfrom;
218
219          if ( ( $total_hits - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 )
220                 {
221                       $displaynext = 1;
222               }
223               my @arrayresults;
224               my @field_data = ($search);
225             for ( my $i = 0 ; $i < $resultsperpage ; $i++ ) {
226                       my $record = MARC::Record::new_from_usmarc( $results->[$i] );
227                   my $rechash = TransformMarcToKoha( $dbh, $record );
228                     my $pos;
229                        my $countitems = $rechash->{itembumber} ? 1 : 0;
230                       while ( index( $rechash->{itemnumber}, '|', $pos ) > 0 ) {
231                              $countitems += 1;
232                               $pos = index( $rechash->{itemnumber}, '|', $pos ) + 1;
233                  }
234                       $rechash->{totitem} = $countitems;
235                      my @holdingbranches = split /\|/, $rechash->{holdingbranch};
236                    my @itemcallnumbers = split /\|/, $rechash->{itemcallnumber};
237                   my $CN;
238                         for ( my $i = 0 ; $i < @holdingbranches ; $i++ ) {
239                              $CN .=
240                            $holdingbranches[$i] . " ( " . $itemcallnumbers[$i] . " ) |";
241                         }
242                       $CN =~ s/ \|$//;
243                        $rechash->{CN} = $CN;
244                   push @arrayresults, $rechash;
245           }
246
247    #         for(my $i = 0 ; $i <= $#marclist ; $i++)
248    #         {
249    #             push @field_data, { term => "marclist", val=>$marclist[$i] };
250    #             push @field_data, { term => "and_or", val=>$and_or[$i] };
251    #             push @field_data, { term => "excluding", val=>$excluding[$i] };
252    #             push @field_data, { term => "operator", val=>$operator[$i] };
253    #             push @field_data, { term => "value", val=>$value[$i] };
254    #         }
255
256              my @numbers = ();
257
258              if ( $total > $resultsperpage ) {
259                       for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
260                          if ( $i < 16 ) {
261                                        my $highlight = 0;
262                                      ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
263                                     push @numbers,
264                                    {
265                                             number     => $i,
266                                               highlight  => $highlight,
267                                               searchdata => \@field_data,
268                                             startfrom  => ( $i - 1 )
269                                          };
270                            }
271                       }
272               }
273
274              my $from = $startfrom * $resultsperpage + 1;
275            my $to;
276
277                if ( $total_hits < $from + $resultsperpage ) {
278                  $to = $total_hits;
279              }
280               else {
281                  $to = $from + $resultsperpage;
282          }
283               my $defaultview =
284                 'BiblioDefaultView' . C4::Context->preference('BiblioDefaultView');
285
286 #         my $link="/cgi-bin/koha/cataloguing/value_builder/unimarc4XX.pl?op=do_search&q=$search_desc&resultsperpage=$resultsperpage&startfrom=$startfrom&search=$search";
287 #           foreach my $sort (@sort_by){
288 #             $link.="&sort_by=".$sort."&";
289 #           }
290 #           $template->param(
291 #             pagination_bar => pagination_bar(
292 #                     $link,
293 #                     getnbpages($hits, $results_per_page),
294 #                     $page,
295 #                     'page'
296 #             ),
297 #           );
298           $template->param(
299                       result         => \@arrayresults,
300                       index          => $query->param('index') . "",
301                  startfrom      => $startfrom,
302                   displaynext    => $displaynext,
303                         displayprev    => $displayprev,
304                         resultsperpage => $resultsperpage,
305                      orderby        => $orderby,
306                     startfromnext  => $startfrom + 1,
307                       startfromprev  => $startfrom - 1,
308                       searchdata     => \@field_data,
309                         total          => $total_hits,
310                  from           => $from,
311                        to             => $to,
312                  numbers        => \@numbers,
313                    search         => $search,
314                      $defaultview   => 1,
315                    Search         => 0
316             );
317
318     }
319       else {
320          ( $template, $loggedinuser, $cookie ) = get_template_and_user(
321                  {
322                               template_name =>
323                                  "cataloguing/value_builder/marc21_linking_section.tt",
324                                query           => $query,
325                              type            => "intranet",
326                          authnotrequired => 0,
327                   }
328               );
329
330             my $sth = $dbh->prepare(
331                        "Select itemtype,description from itemtypes order by description");
332             $sth->execute;
333          my @itemtype;
334           my %itemtypes;
335          push @itemtype, "";
336             $itemtypes{''} = "";
337            while ( my ( $value, $lib ) = $sth->fetchrow_array ) {
338                  push @itemtype, $value;
339                         $itemtypes{$value} = $lib;
340              }
341
342              my $CGIitemtype = CGI::scrolling_list(
343                  -name     => 'value',
344                   -values   => \@itemtype,
345                        -labels   => \%itemtypes,
346                       -size     => 1,
347                         -multiple => 0
348          );
349              $sth->finish;
350
351          my @branchloop;
352                 my @select_branch;
353              my %select_branches;
354            my $branches = GetBranches;
355             push @select_branch, "";
356                $select_branches{''} = "";
357              foreach my $thisbranch ( keys %$branches ) {
358                    push @select_branch, $branches->{$thisbranch}->{'branchcode'};
359                  $select_branches{ $branches->{$thisbranch}->{'branchcode'} } =
360                    $branches->{$thisbranch}->{'branchname'};
361             }
362               my $CGIbranch = CGI::scrolling_list(
363                    -name     => 'value',
364                   -values   => \@select_branch,
365                   -labels   => \%select_branches,
366                         -size     => 1,
367                         -multiple => 0
368          );
369              $sth->finish;
370
371          my $req = $dbh->prepare(
372 "select distinctrow left(publishercode,45) from biblioitems order by publishercode"
373            );
374              $req->execute;
375          my @select;
376             push @select, "";
377               while ( my ($value) = $req->fetchrow ) {
378                        push @select, $value;
379           }
380               my $CGIpublisher = CGI::scrolling_list(
381                         -name     => 'value',
382                   -id       => 'publisher',
383                       -values   => \@select,
384                  -size     => 1,
385                         -multiple => 0
386          );
387
388 #         my $sth=$dbh->prepare("select description,itemtype from itemtypes order by description");
389 #         $sth->execute;
390 #         while (my ($description,$itemtype) = $sth->fetchrow) {
391 #             $classlist.="<option value=\"$itemtype\">$description</option>\n";
392 #         }
393 #         $sth->finish;
394
395                 my @itemtypes = C4::ItemType->all;
396
397             $template->param(    #classlist => $classlist,
398                  CGIitemtype  => $CGIitemtype,
399                   CGIbranch    => $CGIbranch,
400                     CGIPublisher => $CGIpublisher,
401                  itypeloop    => \@itemtypes,
402                    index        => $query->param('index'),
403                         Search       => 1,
404              );
405      }
406       output_html_with_http_headers $query, $cookie, $template->output;
407 }
408
409 1;