Bug 9239: Allow the use of QueryParser for all queries
[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             my $QParser;
194             $QParser = C4::Context->queryparser if (C4::Context->preference('UseQueryParser'));
195             my $op;
196             if ($QParser) {
197                 $op = '&&';
198             } else {
199                 $op = 'and';
200             }
201            $search = 'kw:' . $search . " $op mc-itemtype:" . $itype if $itype;
202                my ( $errors, $results, $total_hits ) =
203                   SimpleSearch( $search, $startfrom * $resultsperpage,
204                  $resultsperpage );
205              if ( defined $errors ) {
206                        $results = [];
207          }
208               my $total = @{$results};
209
210               #        warn " biblio count : ".$total;
211
212               ( $template, $loggedinuser, $cookie ) = get_template_and_user(
213                  {
214                               template_name =>
215                                  "cataloguing/value_builder/marc21_linking_section.tt",
216                                query           => $query,
217                              type            => 'intranet',
218                          authnotrequired => 0,
219                           debug           => 1,
220                   }
221               );
222
223             # multi page display gestion
224            my $displaynext = 0;
225            my $displayprev = $startfrom;
226
227          if ( ( $total_hits - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 )
228                 {
229                       $displaynext = 1;
230               }
231               my @arrayresults;
232               my @field_data = ($search);
233             for ( my $i = 0 ; $i < $resultsperpage ; $i++ ) {
234                       my $record = MARC::Record::new_from_usmarc( $results->[$i] );
235                   my $rechash = TransformMarcToKoha( $dbh, $record );
236                     my $pos;
237                        my $countitems = $rechash->{itembumber} ? 1 : 0;
238                       while ( index( $rechash->{itemnumber}, '|', $pos ) > 0 ) {
239                              $countitems += 1;
240                               $pos = index( $rechash->{itemnumber}, '|', $pos ) + 1;
241                  }
242                       $rechash->{totitem} = $countitems;
243                      my @holdingbranches = split /\|/, $rechash->{holdingbranch};
244                    my @itemcallnumbers = split /\|/, $rechash->{itemcallnumber};
245                   my $CN;
246                         for ( my $i = 0 ; $i < @holdingbranches ; $i++ ) {
247                              $CN .=
248                            $holdingbranches[$i] . " ( " . $itemcallnumbers[$i] . " ) |";
249                         }
250                       $CN =~ s/ \|$//;
251                        $rechash->{CN} = $CN;
252                   push @arrayresults, $rechash;
253           }
254
255    #         for(my $i = 0 ; $i <= $#marclist ; $i++)
256    #         {
257    #             push @field_data, { term => "marclist", val=>$marclist[$i] };
258    #             push @field_data, { term => "and_or", val=>$and_or[$i] };
259    #             push @field_data, { term => "excluding", val=>$excluding[$i] };
260    #             push @field_data, { term => "operator", val=>$operator[$i] };
261    #             push @field_data, { term => "value", val=>$value[$i] };
262    #         }
263
264              my @numbers = ();
265
266              if ( $total > $resultsperpage ) {
267                       for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
268                          if ( $i < 16 ) {
269                                        my $highlight = 0;
270                                      ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
271                                     push @numbers,
272                                    {
273                                             number     => $i,
274                                               highlight  => $highlight,
275                                               searchdata => \@field_data,
276                                             startfrom  => ( $i - 1 )
277                                          };
278                            }
279                       }
280               }
281
282              my $from = $startfrom * $resultsperpage + 1;
283            my $to;
284
285                if ( $total_hits < $from + $resultsperpage ) {
286                  $to = $total_hits;
287              }
288               else {
289                  $to = $from + $resultsperpage;
290          }
291               my $defaultview =
292                 'BiblioDefaultView' . C4::Context->preference('BiblioDefaultView');
293
294 #         my $link="/cgi-bin/koha/cataloguing/value_builder/unimarc4XX.pl?op=do_search&q=$search_desc&resultsperpage=$resultsperpage&startfrom=$startfrom&search=$search";
295 #           foreach my $sort (@sort_by){
296 #             $link.="&sort_by=".$sort."&";
297 #           }
298 #           $template->param(
299 #             pagination_bar => pagination_bar(
300 #                     $link,
301 #                     getnbpages($hits, $results_per_page),
302 #                     $page,
303 #                     'page'
304 #             ),
305 #           );
306           $template->param(
307                       result         => \@arrayresults,
308                       index          => $query->param('index') . "",
309                  startfrom      => $startfrom,
310                   displaynext    => $displaynext,
311                         displayprev    => $displayprev,
312                         resultsperpage => $resultsperpage,
313                      orderby        => $orderby,
314                     startfromnext  => $startfrom + 1,
315                       startfromprev  => $startfrom - 1,
316                       searchdata     => \@field_data,
317                         total          => $total_hits,
318                  from           => $from,
319                        to             => $to,
320                  numbers        => \@numbers,
321                    search         => $search,
322                      $defaultview   => 1,
323                    Search         => 0
324             );
325
326     }
327       else {
328          ( $template, $loggedinuser, $cookie ) = get_template_and_user(
329                  {
330                               template_name =>
331                                  "cataloguing/value_builder/marc21_linking_section.tt",
332                                query           => $query,
333                              type            => "intranet",
334                          authnotrequired => 0,
335                   }
336               );
337
338             my $sth = $dbh->prepare(
339                        "Select itemtype,description from itemtypes order by description");
340             $sth->execute;
341          my @itemtype;
342           my %itemtypes;
343          push @itemtype, "";
344             $itemtypes{''} = "";
345            while ( my ( $value, $lib ) = $sth->fetchrow_array ) {
346                  push @itemtype, $value;
347                         $itemtypes{$value} = $lib;
348              }
349
350              my $CGIitemtype = CGI::scrolling_list(
351                  -name     => 'value',
352                   -values   => \@itemtype,
353                        -labels   => \%itemtypes,
354                       -size     => 1,
355                         -multiple => 0
356          );
357              $sth->finish;
358
359          my @branchloop;
360                 my @select_branch;
361              my %select_branches;
362            my $branches = GetBranches;
363             push @select_branch, "";
364                $select_branches{''} = "";
365              foreach my $thisbranch ( keys %$branches ) {
366                    push @select_branch, $branches->{$thisbranch}->{'branchcode'};
367                  $select_branches{ $branches->{$thisbranch}->{'branchcode'} } =
368                    $branches->{$thisbranch}->{'branchname'};
369             }
370               my $CGIbranch = CGI::scrolling_list(
371                    -name     => 'value',
372                   -values   => \@select_branch,
373                   -labels   => \%select_branches,
374                         -size     => 1,
375                         -multiple => 0
376          );
377              $sth->finish;
378
379          my $req = $dbh->prepare(
380 "select distinctrow left(publishercode,45) from biblioitems order by publishercode"
381            );
382              $req->execute;
383          my @select;
384             push @select, "";
385               while ( my ($value) = $req->fetchrow ) {
386                        push @select, $value;
387           }
388               my $CGIpublisher = CGI::scrolling_list(
389                         -name     => 'value',
390                   -id       => 'publisher',
391                       -values   => \@select,
392                  -size     => 1,
393                         -multiple => 0
394          );
395
396 #         my $sth=$dbh->prepare("select description,itemtype from itemtypes order by description");
397 #         $sth->execute;
398 #         while (my ($description,$itemtype) = $sth->fetchrow) {
399 #             $classlist.="<option value=\"$itemtype\">$description</option>\n";
400 #         }
401 #         $sth->finish;
402
403                 my @itemtypes = C4::ItemType->all;
404
405             $template->param(    #classlist => $classlist,
406                  CGIitemtype  => $CGIitemtype,
407                   CGIbranch    => $CGIbranch,
408                     CGIPublisher => $CGIpublisher,
409                  itypeloop    => \@itemtypes,
410                    index        => $query->param('index'),
411                         Search       => 1,
412              );
413      }
414       output_html_with_http_headers $query, $cookie, $template->output;
415 }
416
417 1;