Deleting Sub GetBorrowerIssues.
[koha.git] / C4 / Search.pm
1 package C4::Search;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18 use strict;
19 require Exporter;
20 use C4::Context;
21 use C4::Biblio;    # GetMarcFromKohaField
22 use C4::Koha;      # getFacets
23 use Lingua::Stem;
24
25 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
26
27 # set the version for version checking
28 $VERSION = do { my @v = '$Revision$' =~ /\d+/g;
29     shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v );
30 };
31
32 =head1 NAME
33
34 C4::Search - Functions for searching the Koha catalog.
35
36 =head1 SYNOPSIS
37
38 see opac/opac-search.pl or catalogue/search.pl for example of usage
39
40 =head1 DESCRIPTION
41
42 This module provides the searching facilities for the Koha into a zebra catalog.
43
44 =head1 FUNCTIONS
45
46 =cut
47
48 @ISA    = qw(Exporter);
49 @EXPORT = qw(
50   &SimpleSearch
51   &findseealso
52   &FindDuplicate
53   &searchResults
54   &getRecords
55   &buildQuery
56 );
57
58 # make all your functions, whether exported or not;
59
60 =head2 findseealso($dbh,$fields);
61
62 C<$dbh> is a link to the DB handler.
63
64 use C4::Context;
65 my $dbh =C4::Context->dbh;
66
67 C<$fields> is a reference to the fields array
68
69 This function modify the @$fields array and add related fields to search on.
70
71 =cut
72
73 sub findseealso {
74     my ( $dbh, $fields ) = @_;
75     my $tagslib = GetMarcStructure( $dbh, 1 );
76     for ( my $i = 0 ; $i <= $#{$fields} ; $i++ ) {
77         my ($tag)      = substr( @$fields[$i], 1, 3 );
78         my ($subfield) = substr( @$fields[$i], 4, 1 );
79         @$fields[$i] .= ',' . $tagslib->{$tag}->{$subfield}->{seealso}
80           if ( $tagslib->{$tag}->{$subfield}->{seealso} );
81     }
82 }
83
84 =head2 FindDuplicate
85
86 ($biblionumber,$biblionumber,$title) = FindDuplicate($record);
87
88 =cut
89
90 sub FindDuplicate {
91     my ($record) = @_;
92     return;
93     my $dbh = C4::Context->dbh;
94     my $result = TransformMarcToKoha( $dbh, $record, '' );
95     my $sth;
96     my $query;
97     my $search;
98     my $type;
99     my ( $biblionumber, $title );
100
101     # search duplicate on ISBN, easy and fast..
102     #$search->{'avoidquerylog'}=1;
103     if ( $result->{isbn} ) {
104         $query = "isbn=$result->{isbn}";
105     }
106     else {
107         $result->{title} =~ s /\\//g;
108         $result->{title} =~ s /\"//g;
109         $result->{title} =~ s /\(//g;
110         $result->{title} =~ s /\)//g;
111         $query = "ti,ext=$result->{title}";
112     }
113     my ($possible_duplicate_record) =
114       C4::Biblio::getRecord( "biblioserver", $query, "usmarc" ); # FIXME :: hardcoded !
115     if ($possible_duplicate_record) {
116         my $marcrecord =
117           MARC::Record->new_from_usmarc($possible_duplicate_record);
118         my $result = TransformMarcToKoha( $dbh, $marcrecord, '' );
119         
120         # FIXME :: why 2 $biblionumber ?
121         return $result->{'biblionumber'}, $result->{'biblionumber'},
122           $result->{'title'}
123           if $result;
124     }
125 }
126
127 =head2 SimpleSearch
128
129 ($error,$results) = SimpleSearch($query,@servers);
130
131 this function performs a simple search on the catalog using zoom.
132
133 =over 2
134
135 =item C<input arg:>
136
137     * $query could be a simple keyword or a complete CCL query wich is depending on your ccl file.
138     * @servers is optionnal. default one is read on koha.xml
139
140 =item C<Output arg:>
141     * $error is a string which containt the description error if there is one. Else it's empty.
142     * \@results is an array of marc record.
143
144 =item C<usage in the script:>
145
146 =back
147
148 my ($error, $marcresults) = SimpleSearch($query);
149
150 if (defined $error) {
151     $template->param(query_error => $error);
152     warn "error: ".$error;
153     output_html_with_http_headers $input, $cookie, $template->output;
154     exit;
155 }
156
157 my $hits = scalar @$marcresults;
158 my @results;
159
160 for(my $i=0;$i<$hits;$i++) {
161     my %resultsloop;
162     my $marcrecord = MARC::File::USMARC::decode($marcresults->[$i]);
163     my $biblio = TransformMarcToKoha(C4::Context->dbh,$marcrecord,'');
164
165     #build the hash for the template.
166     $resultsloop{highlight}       = ($i % 2)?(1):(0);
167     $resultsloop{title}           = $biblio->{'title'};
168     $resultsloop{subtitle}        = $biblio->{'subtitle'};
169     $resultsloop{biblionumber}    = $biblio->{'biblionumber'};
170     $resultsloop{author}          = $biblio->{'author'};
171     $resultsloop{publishercode}   = $biblio->{'publishercode'};
172     $resultsloop{publicationyear} = $biblio->{'publicationyear'};
173
174     push @results, \%resultsloop;
175 }
176 $template->param(result=>\@results);
177
178 =cut
179
180 sub SimpleSearch {
181     my $query   = shift;
182     my @servers = @_;
183     my @results;
184     my @tmpresults;
185     my @zconns;
186     return ( "No query entered", undef ) unless $query;
187
188     #@servers = (C4::Context->config("biblioserver")) unless @servers;
189     @servers =
190       ("biblioserver") unless @servers
191       ;    # FIXME hardcoded value. See catalog/search.pl & opac-search.pl too.
192
193     # Connect & Search
194     for ( my $i = 0 ; $i < @servers ; $i++ ) {
195         $zconns[$i] = C4::Context->Zconn( $servers[$i], 1 );
196         $tmpresults[$i] =
197           $zconns[$i]
198           ->search( new ZOOM::Query::CCL2RPN( $query, $zconns[$i] ) );
199
200         # getting error message if one occured.
201         my $error =
202             $zconns[$i]->errmsg() . " ("
203           . $zconns[$i]->errcode() . ") "
204           . $zconns[$i]->addinfo() . " "
205           . $zconns[$i]->diagset();
206
207         return ( $error, undef ) if $zconns[$i]->errcode();
208     }
209     my $hits;
210     my $ev;
211     while ( ( my $i = ZOOM::event( \@zconns ) ) != 0 ) {
212         $ev = $zconns[ $i - 1 ]->last_event();
213         if ( $ev == ZOOM::Event::ZEND ) {
214             $hits = $tmpresults[ $i - 1 ]->size();
215         }
216         if ( $hits > 0 ) {
217             for ( my $j = 0 ; $j < $hits ; $j++ ) {
218                 my $record = $tmpresults[ $i - 1 ]->record($j)->raw();
219                 push @results, $record;
220             }
221         }
222     }
223     return ( undef, \@results );
224 }
225
226 # performs the search
227 sub getRecords {
228     my (
229         $koha_query,     $federated_query,  $sort_by_ref,
230         $servers_ref,    $results_per_page, $offset,
231         $expanded_facet, $branches,         $query_type,
232         $scan
233     ) = @_;
234
235     my @servers = @$servers_ref;
236     my @sort_by = @$sort_by_ref;
237
238     # create the zoom connection and query object
239     my $zconn;
240     my @zconns;
241     my @results;
242     my $results_hashref = ();
243
244     ### FACETED RESULTS
245     my $facets_counter = ();
246     my $facets_info    = ();
247     my $facets         = getFacets();
248
249     #### INITIALIZE SOME VARS USED CREATE THE FACETED RESULTS
250     my @facets_loop;    # stores the ref to array of hashes for template
251     for ( my $i = 0 ; $i < @servers ; $i++ ) {
252         $zconns[$i] = C4::Context->Zconn( $servers[$i], 1 );
253
254 # perform the search, create the results objects
255 # if this is a local search, use the $koha-query, if it's a federated one, use the federated-query
256         my $query_to_use;
257         if ( $servers[$i] =~ /biblioserver/ ) {
258             $query_to_use = $koha_query;
259         }
260         else {
261             $query_to_use = $federated_query;
262         }
263
264         #          warn "HERE : $query_type => $query_to_use";
265         # check if we've got a query_type defined
266         eval {
267             if ($query_type)
268             {
269                 if ( $query_type =~ /^ccl/ ) {
270                     $query_to_use =~
271                       s/\:/\=/g;    # change : to = last minute (FIXME)
272
273                     #                 warn "CCL : $query_to_use";
274                     $results[$i] =
275                       $zconns[$i]->search(
276                         new ZOOM::Query::CCL2RPN( $query_to_use, $zconns[$i] )
277                       );
278                 }
279                 elsif ( $query_type =~ /^cql/ ) {
280
281                     #                 warn "CQL : $query_to_use";
282                     $results[$i] =
283                       $zconns[$i]->search(
284                         new ZOOM::Query::CQL( $query_to_use, $zconns[$i] ) );
285                 }
286                 elsif ( $query_type =~ /^pqf/ ) {
287
288                     #                 warn "PQF : $query_to_use";
289                     $results[$i] =
290                       $zconns[$i]->search(
291                         new ZOOM::Query::PQF( $query_to_use, $zconns[$i] ) );
292                 }
293             }
294             else {
295                 if ($scan) {
296
297                     #                 warn "preparing to scan";
298                     $results[$i] =
299                       $zconns[$i]->scan(
300                         new ZOOM::Query::CCL2RPN( $query_to_use, $zconns[$i] )
301                       );
302                 }
303                 else {
304
305                     #             warn "LAST : $query_to_use";
306                     $results[$i] =
307                       $zconns[$i]->search(
308                         new ZOOM::Query::CCL2RPN( $query_to_use, $zconns[$i] )
309                       );
310                 }
311             }
312         };
313         if ($@) {
314             warn "prob with query  toto $query_to_use " . $@;
315         }
316
317         # concatenate the sort_by limits and pass them to the results object
318         my $sort_by;
319         foreach my $sort (@sort_by) {
320             $sort_by .= $sort . " ";    # used to be $sort,
321         }
322         $results[$i]->sort( "yaz", $sort_by ) if $sort_by;
323     }
324     while ( ( my $i = ZOOM::event( \@zconns ) ) != 0 ) {
325         my $ev = $zconns[ $i - 1 ]->last_event();
326         if ( $ev == ZOOM::Event::ZEND ) {
327             my $size = $results[ $i - 1 ]->size();
328             if ( $size > 0 ) {
329                 my $results_hash;
330                 #$results_hash->{'server'} = $servers[$i-1];
331                 # loop through the results
332                 $results_hash->{'hits'} = $size;
333                 my $times;
334                 if ( $offset + $results_per_page <= $size ) {
335                     $times = $offset + $results_per_page;
336                 }
337                 else {
338                     $times = $size;
339                 }
340                 for ( my $j = $offset ; $j < $times ; $j++ )
341                 {   #(($offset+$count<=$size) ? ($offset+$count):$size) ; $j++){
342                     my $records_hash;
343                     my $record;
344                     my $facet_record;
345                     ## This is just an index scan
346                     if ($scan) {
347                         my ( $term, $occ ) = $results[ $i - 1 ]->term($j);
348
349                  # here we create a minimal MARC record and hand it off to the
350                  # template just like a normal result ... perhaps not ideal, but
351                  # it works for now
352                         my $tmprecord = MARC::Record->new();
353                         $tmprecord->encoding('UTF-8');
354                         my $tmptitle;
355
356           # srote the minimal record in author/title (depending on MARC flavour)
357                         if ( C4::Context->preference("marcflavour") eq
358                             "UNIMARC" )
359                         {
360                             $tmptitle = MARC::Field->new(
361                                 '200', ' ', ' ',
362                                 a => $term,
363                                 f => $occ
364                             );
365                         }
366                         else {
367                             $tmptitle = MARC::Field->new(
368                                 '245', ' ', ' ',
369                                 a => $term,
370                                 b => $occ
371                             );
372                         }
373                         $tmprecord->append_fields($tmptitle);
374                         $results_hash->{'RECORDS'}[$j] =
375                           $tmprecord->as_usmarc();
376                     }
377                     else {
378                         $record = $results[ $i - 1 ]->record($j)->raw();
379
380                         #warn "RECORD $j:".$record;
381                         $results_hash->{'RECORDS'}[$j] =
382                           $record;    # making a reference to a hash
383                                       # Fill the facets while we're looping
384                         $facet_record = MARC::Record->new_from_usmarc($record);
385
386                         #warn $servers[$i-1].$facet_record->title();
387                         for ( my $k = 0 ; $k <= @$facets ; $k++ ) {
388                             if ( $facets->[$k] ) {
389                                 my @fields;
390                                 for my $tag ( @{ $facets->[$k]->{'tags'} } ) {
391                                     push @fields, $facet_record->field($tag);
392                                 }
393                                 for my $field (@fields) {
394                                     my @subfields = $field->subfields();
395                                     for my $subfield (@subfields) {
396                                         my ( $code, $data ) = @$subfield;
397                                         if ( $code eq
398                                             $facets->[$k]->{'subfield'} )
399                                         {
400                                             $facets_counter->{ $facets->[$k]
401                                                   ->{'link_value'} }->{$data}++;
402                                         }
403                                     }
404                                 }
405                                 $facets_info->{ $facets->[$k]->{'link_value'} }
406                                   ->{'label_value'} =
407                                   $facets->[$k]->{'label_value'};
408                                 $facets_info->{ $facets->[$k]->{'link_value'} }
409                                   ->{'expanded'} = $facets->[$k]->{'expanded'};
410                             }
411                         }
412                     }
413                 }
414                 $results_hashref->{ $servers[ $i - 1 ] } = $results_hash;
415             }
416
417             #print "connection ", $i-1, ": $size hits";
418             #print $results[$i-1]->record(0)->render() if $size > 0;
419             # BUILD FACETS
420             for my $link_value (
421                 sort { $facets_counter->{$b} <=> $facets_counter->{$a} }
422                 keys %$facets_counter
423               )
424             {
425                 my $expandable;
426                 my $number_of_facets;
427                 my @this_facets_array;
428                 for my $one_facet (
429                     sort {
430                         $facets_counter->{$link_value}
431                           ->{$b} <=> $facets_counter->{$link_value}->{$a}
432                     } keys %{ $facets_counter->{$link_value} }
433                   )
434                 {
435                     $number_of_facets++;
436                     if (   ( $number_of_facets < 6 )
437                         || ( $expanded_facet eq $link_value )
438                         || ( $facets_info->{$link_value}->{'expanded'} ) )
439                     {
440
441                        # sanitize the link value ), ( will cause errors with CCL
442                         my $facet_link_value = $one_facet;
443                         $facet_link_value =~ s/(\(|\))/ /g;
444
445                         # fix the length that will display in the label
446                         my $facet_label_value = $one_facet;
447                         $facet_label_value = substr( $one_facet, 0, 20 ) . "..."
448                           unless length($facet_label_value) <= 20;
449
450                        # well, if it's a branch, label by the name, not the code
451                         if ( $link_value =~ /branch/ ) {
452                             $facet_label_value =
453                               $branches->{$one_facet}->{'branchname'};
454                         }
455
456                  # but we're down with the whole label being in the link's title
457                         my $facet_title_value = $one_facet;
458
459                         push @this_facets_array,
460                           (
461                             {
462                                 facet_count =>
463                                   $facets_counter->{$link_value}->{$one_facet},
464                                 facet_label_value => $facet_label_value,
465                                 facet_title_value => $facet_title_value,
466                                 facet_link_value  => $facet_link_value,
467                                 type_link_value   => $link_value,
468                             },
469                           );
470                     }
471                 }
472                 unless ( $facets_info->{$link_value}->{'expanded'} ) {
473                     $expandable = 1
474                       if ( ( $number_of_facets > 6 )
475                         && ( $expanded_facet ne $link_value ) );
476                 }
477                 push @facets_loop,
478                   (
479                     {
480                         type_link_value => $link_value,
481                         type_id         => $link_value . "_id",
482                         type_label      =>
483                           $facets_info->{$link_value}->{'label_value'},
484                         facets     => \@this_facets_array,
485                         expandable => $expandable,
486                         expand     => $link_value,
487                     }
488                   );
489             }
490         }
491     }
492     return ( undef, $results_hashref, \@facets_loop );
493 }
494
495 # build the query itself
496 sub buildQuery {
497     my ( $query, $operators, $operands, $indexes, $limits, $sort_by ) = @_;
498
499     my @operators = @$operators if $operators;
500     my @indexes   = @$indexes   if $indexes;
501     my @operands  = @$operands  if $operands;
502     my @limits    = @$limits    if $limits;
503     my @sort_by   = @$sort_by   if $sort_by;
504
505     my $human_search_desc;      # a human-readable query
506     my $machine_search_desc;    #a machine-readable query
507         # FIXME: the locale should be set based on the syspref
508     my $stemmer = Lingua::Stem->new( -locale => 'EN-US' );
509
510 # FIXME: these should be stored in the db so the librarian can modify the behavior
511     $stemmer->add_exceptions(
512         {
513             'and' => 'and',
514             'or'  => 'or',
515             'not' => 'not',
516         }
517     );
518
519 # STEP I: determine if this is a form-based / simple query or if it's complex (if complex,
520 # we can't handle field weighting, stemming until a formal query parser is written
521 # I'll work on this soon -- JF
522 #if (!$query) { # form-based
523 # check if this is a known query language query, if it is, return immediately:
524     if ( $query =~ /^ccl=/ ) {
525         return ( undef, $', $', $', 'ccl' );
526     }
527     if ( $query =~ /^cql=/ ) {
528         return ( undef, $', $', $', 'cql' );
529     }
530     if ( $query =~ /^pqf=/ ) {
531         return ( undef, $', $', $', 'pqf' );
532     }
533     if ( $query =~ /(\(|\))/ ) {    # sorry, too complex
534         return ( undef, $query, $query, $query, 'ccl' );
535     }
536
537 # form-based queries are limited to non-nested a specific depth, so we can easily
538 # modify the incoming query operands and indexes to do stemming and field weighting
539 # Once we do so, we'll end up with a value in $query, just like if we had an
540 # incoming $query from the user
541     else {
542         $query = ""
543           ; # clear it out so we can populate properly with field-weighted stemmed query
544         my $previous_operand
545           ;    # a flag used to keep track if there was a previous query
546                # if there was, we can apply the current operator
547         for ( my $i = 0 ; $i <= @operands ; $i++ ) {
548             my $operand = $operands[$i];
549             my $index   = $indexes[$i];
550             my $stemmed_operand;
551             my $stemming      = C4::Context->parameters("Stemming")     || 0;
552             my $weight_fields = C4::Context->parameters("WeightFields") || 0;
553
554             if ( $operands[$i] ) {
555
556 # STEMMING FIXME: need to refine the field weighting so stemmed operands don't disrupt the query ranking
557                 if ($stemming) {
558                     my @words = split( / /, $operands[$i] );
559                     my $stems = $stemmer->stem(@words);
560                     foreach my $stem (@$stems) {
561                         $stemmed_operand .= "$stem";
562                         $stemmed_operand .= "?"
563                           unless ( $stem =~ /(and$|or$|not$)/ )
564                           || ( length($stem) < 3 );
565                         $stemmed_operand .= " ";
566
567                         #warn "STEM: $stemmed_operand";
568                     }
569
570                     #$operand = $stemmed_operand;
571                 }
572
573 # FIELD WEIGHTING - This is largely experimental stuff. What I'm committing works
574 # pretty well but will work much better when we have an actual query parser
575                 my $weighted_query;
576                 if ($weight_fields) {
577                     $weighted_query .=
578                       " rk=(";    # Specifies that we're applying rank
579                                   # keyword has different weight properties
580                     if ( ( $index =~ /kw/ ) || ( !$index ) )
581                     { # FIXME: do I need to add right-truncation in the case of stemming?
582                           # a simple way to find out if this query uses an index
583                         if ( $operand =~ /(\=|\:)/ ) {
584                             $weighted_query .= " $operand";
585                         }
586                         else {
587                             $weighted_query .=
588                               " Title-cover,ext,r1=\"$operand\""
589                               ;    # index label as exact
590                             $weighted_query .=
591                               " or ti,ext,r2=$operand";    # index as exact
592                              #$weighted_query .= " or ti,phr,r3=$operand";              # index as  phrase
593                              #$weighted_query .= " or any,ext,r4=$operand";         # index as exact
594                             $weighted_query .=
595                               " or kw,wrdl,r5=$operand";    # index as exact
596                             $weighted_query .= " or wrd,fuzzy,r9=$operand";
597                             $weighted_query .= " or wrd=$stemmed_operand"
598                               if $stemming;
599                         }
600                     }
601                     elsif ( $index =~ /au/ ) {
602                         $weighted_query .=
603                           " $index,ext,r1=$operand";    # index label as exact
604                          #$weighted_query .= " or (title-sort-az=0 or $index,startswithnt,st-word,r3=$operand #)";
605                         $weighted_query .=
606                           " or $index,phr,r3=$operand";    # index as phrase
607                         $weighted_query .= " or $index,rt,wrd,r3=$operand";
608                     }
609                     elsif ( $index =~ /ti/ ) {
610                         $weighted_query .=
611                           " Title-cover,ext,r1=$operand"; # index label as exact
612                         $weighted_query .= " or Title-series,ext,r2=$operand";
613
614                         #$weighted_query .= " or ti,ext,r2=$operand";
615                         #$weighted_query .= " or ti,phr,r3=$operand";
616                         #$weighted_query .= " or ti,wrd,r3=$operand";
617                         $weighted_query .=
618 " or (title-sort-az=0 or Title-cover,startswithnt,st-word,r3=$operand #)";
619                         $weighted_query .=
620 " or (title-sort-az=0 or Title-cover,phr,r6=$operand)";
621
622                         #$weighted_query .= " or Title-cover,wrd,r5=$operand";
623                         #$weighted_query .= " or ti,ext,r6=$operand";
624                         #$weighted_query .= " or ti,startswith,phr,r7=$operand";
625                         #$weighted_query .= " or ti,phr,r8=$operand";
626                         #$weighted_query .= " or ti,wrd,r9=$operand";
627
628    #$weighted_query .= " or ti,ext,r2=$operand";         # index as exact
629    #$weighted_query .= " or ti,phr,r3=$operand";              # index as  phrase
630    #$weighted_query .= " or any,ext,r4=$operand";         # index as exact
631    #$weighted_query .= " or kw,wrd,r5=$operand";         # index as exact
632                     }
633                     else {
634                         $weighted_query .=
635                           " $index,ext,r1=$operand";    # index label as exact
636                          #$weighted_query .= " or $index,ext,r2=$operand";            # index as exact
637                         $weighted_query .=
638                           " or $index,phr,r3=$operand";    # index as phrase
639                         $weighted_query .= " or $index,rt,wrd,r3=$operand";
640                         $weighted_query .=
641                           " or $index,wrd,r5=$operand"
642                           ;    # index as word right-truncated
643                         $weighted_query .= " or $index,wrd,fuzzy,r8=$operand";
644                     }
645                     $weighted_query .= ")";    # close rank specification
646                     $operand = $weighted_query;
647                 }
648
649                 # only add an operator if there is a previous operand
650                 if ($previous_operand) {
651                     if ( $operators[ $i - 1 ] ) {
652                         $query .= " $operators[$i-1] $index: $operand";
653                         if ( !$index ) {
654                             $human_search_desc .=
655                               "  $operators[$i-1] $operands[$i]";
656                         }
657                         else {
658                             $human_search_desc .=
659                               "  $operators[$i-1] $index: $operands[$i]";
660                         }
661                     }
662
663                     # the default operator is and
664                     else {
665                         $query             .= " and $index: $operand";
666                         $human_search_desc .= "  and $index: $operands[$i]";
667                     }
668                 }
669                 else {
670                     if ( !$index ) {
671                         $query             .= " $operand";
672                         $human_search_desc .= "  $operands[$i]";
673                     }
674                     else {
675                         $query             .= " $index: $operand";
676                         $human_search_desc .= "  $index: $operands[$i]";
677                     }
678                     $previous_operand = 1;
679                 }
680             }    #/if $operands
681         }    # /for
682     }
683
684     # add limits
685     my $limit_query;
686     my $limit_search_desc;
687     foreach my $limit (@limits) {
688
689         # FIXME: not quite right yet ... will work on this soon -- JF
690         my $type = $1 if $limit =~ m/([^:]+):([^:]*)/;
691         if ( $limit =~ /available/ ) {
692             $limit_query .=
693 " (($query and datedue=0000-00-00) or ($query and datedue=0000-00-00 not lost=1) or ($query and datedue=0000-00-00 not lost=2))";
694
695             #$limit_search_desc.=" and available";
696         }
697         elsif ( ($limit_query) && ( index( $limit_query, $type, 0 ) > 0 ) ) {
698             if ( $limit_query !~ /\(/ ) {
699                 $limit_query =
700                     substr( $limit_query, 0, index( $limit_query, $type, 0 ) )
701                   . "("
702                   . substr( $limit_query, index( $limit_query, $type, 0 ) )
703                   . " or $limit )"
704                   if $limit;
705                 $limit_search_desc =
706                   substr( $limit_search_desc, 0,
707                     index( $limit_search_desc, $type, 0 ) )
708                   . "("
709                   . substr( $limit_search_desc,
710                     index( $limit_search_desc, $type, 0 ) )
711                   . " or $limit )"
712                   if $limit;
713             }
714             else {
715                 chop $limit_query;
716                 chop $limit_search_desc;
717                 $limit_query       .= " or $limit )" if $limit;
718                 $limit_search_desc .= " or $limit )" if $limit;
719             }
720         }
721         elsif ( ($limit_query) && ( $limit =~ /mc/ ) ) {
722             $limit_query       .= " or $limit" if $limit;
723             $limit_search_desc .= " or $limit" if $limit;
724         }
725
726         # these are treated as AND
727         elsif ($limit_query) {
728            if ($limit =~ /branch/){
729                         $limit_query       .= " ) and ( $limit" if $limit;
730                         $limit_search_desc .= " ) and ( $limit" if $limit;
731                 }else{
732                         $limit_query       .= " or $limit" if $limit;
733                         $limit_search_desc .= " or $limit" if $limit;
734                 }
735         }
736
737         # otherwise, there is nothing but the limit
738         else {
739             $limit_query       .= "$limit" if $limit;
740             $limit_search_desc .= "$limit" if $limit;
741         }
742     }
743
744     # if there's also a query, we need to AND the limits to it
745     if ( ($limit_query) && ($query) ) {
746         $limit_query       = " and (" . $limit_query . ")";
747         $limit_search_desc = " and ($limit_search_desc)" if $limit_search_desc;
748
749     }
750     $query             .= $limit_query;
751     $human_search_desc .= $limit_search_desc;
752
753     # now normalize the strings
754     $query =~ s/  / /g;    # remove extra spaces
755     $query =~ s/^ //g;     # remove any beginning spaces
756     $query =~ s/:/=/g;     # causes probs for server
757     $query =~ s/==/=/g;    # remove double == from query
758
759     my $federated_query = $human_search_desc;
760     $federated_query =~ s/  / /g;
761     $federated_query =~ s/^ //g;
762     $federated_query =~ s/:/=/g;
763     my $federated_query_opensearch = $federated_query;
764
765 #     my $federated_query_RPN = new ZOOM::Query::CCL2RPN( $query , C4::Context->ZConn('biblioserver'));
766
767     $human_search_desc =~ s/  / /g;
768     $human_search_desc =~ s/^ //g;
769     my $koha_query = $query;
770
771     #warn "QUERY:".$koha_query;
772     #warn "SEARCHDESC:".$human_search_desc;
773     #warn "FEDERATED QUERY:".$federated_query;
774     return ( undef, $human_search_desc, $koha_query, $federated_query );
775 }
776
777 # IMO this subroutine is pretty messy still -- it's responsible for
778 # building the HTML output for the template
779 sub searchResults {
780     my ( $searchdesc, $hits, $results_per_page, $offset, @marcresults ) = @_;
781
782     my $dbh = C4::Context->dbh;
783     my $toggle;
784     my $even = 1;
785     my @newresults;
786     my $span_terms_hashref;
787     for my $span_term ( split( / /, $searchdesc ) ) {
788         $span_term =~ s/(.*=|\)|\(|\+|\.)//g;
789         $span_terms_hashref->{$span_term}++;
790     }
791
792     #Build brancnames hash
793     #find branchname
794     #get branch information.....
795     my %branches;
796     my $bsth =
797       $dbh->prepare("SELECT branchcode,branchname FROM branches")
798       ;    # FIXME : use C4::Koha::GetBranches
799     $bsth->execute();
800     while ( my $bdata = $bsth->fetchrow_hashref ) {
801         $branches{ $bdata->{'branchcode'} } = $bdata->{'branchname'};
802     }
803
804     #Build itemtype hash
805     #find itemtype & itemtype image
806     my %itemtypes;
807     $bsth =
808       $dbh->prepare("SELECT itemtype,description,imageurl,summary FROM itemtypes");
809     $bsth->execute();
810     while ( my $bdata = $bsth->fetchrow_hashref ) {
811         $itemtypes{ $bdata->{'itemtype'} }->{description} =
812           $bdata->{'description'};
813         $itemtypes{ $bdata->{'itemtype'} }->{imageurl} = $bdata->{'imageurl'};
814         $itemtypes{ $bdata->{'itemtype'} }->{summary} = $bdata->{'summary'};
815     }
816
817     #search item field code
818     my $sth =
819       $dbh->prepare(
820 "select tagfield from marc_subfield_structure where kohafield like 'items.itemnumber'"
821       );
822     $sth->execute;
823     my ($itemtag) = $sth->fetchrow;
824
825     ## find column names of items related to MARC
826     my $sth2 = $dbh->prepare("SHOW COLUMNS from items");
827     $sth2->execute;
828     my %subfieldstosearch;
829     while ( ( my $column ) = $sth2->fetchrow ) {
830         my ( $tagfield, $tagsubfield ) =
831           &GetMarcFromKohaField( $dbh, "items." . $column, "" );
832         $subfieldstosearch{$column} = $tagsubfield;
833     }
834     my $times;
835
836     if ( $hits && $offset + $results_per_page <= $hits ) {
837         $times = $offset + $results_per_page;
838     }
839     else {
840         $times = $hits;
841     }
842
843     for ( my $i = $offset ; $i <= $times - 1 ; $i++ ) {
844         my $marcrecord;
845         $marcrecord = MARC::File::USMARC::decode( $marcresults[$i] );
846
847         my $oldbiblio = TransformMarcToKoha( $dbh, $marcrecord, '' );
848
849         # add image url if there is one
850         if ( $itemtypes{ $oldbiblio->{itemtype} }->{imageurl} =~ /^http:/ ) {
851             $oldbiblio->{imageurl} =
852               $itemtypes{ $oldbiblio->{itemtype} }->{imageurl};
853             $oldbiblio->{description} =
854               $itemtypes{ $oldbiblio->{itemtype} }->{description};
855         }
856         else {
857             $oldbiblio->{imageurl} =
858               getitemtypeimagesrc() . "/"
859               . $itemtypes{ $oldbiblio->{itemtype} }->{imageurl}
860               if ( $itemtypes{ $oldbiblio->{itemtype} }->{imageurl} );
861             $oldbiblio->{description} =
862               $itemtypes{ $oldbiblio->{itemtype} }->{description};
863         }
864         #
865         # build summary if there is one (the summary is defined in itemtypes table
866         #
867         if ($itemtypes{ $oldbiblio->{itemtype} }->{summary}) {
868             my $summary = $itemtypes{ $oldbiblio->{itemtype} }->{summary};
869             my @fields = $marcrecord->fields();
870             foreach my $field (@fields) {
871                 my $tag = $field->tag();
872                 my $tagvalue = $field->as_string();
873                 $summary =~ s/\[(.?.?.?.?)$tag\*(.*?)]/$1$tagvalue$2\[$1$tag$2]/g;
874                 unless ($tag<10) {
875                     my @subf = $field->subfields;
876                     for my $i (0..$#subf) {
877                         my $subfieldcode = $subf[$i][0];
878                         my $subfieldvalue = $subf[$i][1];
879                         my $tagsubf = $tag.$subfieldcode;
880                         $summary =~ s/\[(.?.?.?.?)$tagsubf(.*?)]/$1$subfieldvalue$2\[$1$tagsubf$2]/g;
881                     }
882                 }
883             }
884             $summary =~ s/\[(.*?)]//g;
885             $summary =~ s/\n/<br>/g;
886             $oldbiblio->{summary} = $summary;
887         }
888         # add spans to search term in results
889         foreach my $term ( keys %$span_terms_hashref ) {
890
891             #warn "term: $term";
892             my $old_term = $term;
893             if ( length($term) > 3 ) {
894                 $term =~ s/(.*=|\)|\(|\+|\.|\?)//g;
895
896                 #FIXME: is there a better way to do this?
897                 $oldbiblio->{'title'} =~ s/$term/<span class=term>$&<\/span>/gi;
898                 $oldbiblio->{'subtitle'} =~
899                   s/$term/<span class=term>$&<\/span>/gi;
900
901                 $oldbiblio->{'author'} =~ s/$term/<span class=term>$&<\/span>/gi;
902                 $oldbiblio->{'publishercode'} =~ s/$term/<span class=term>$&<\/span>/gi;
903                 $oldbiblio->{'place'} =~ s/$term/<span class=term>$&<\/span>/gi;
904                 $oldbiblio->{'pages'} =~ s/$term/<span class=term>$&<\/span>/gi;
905                 $oldbiblio->{'notes'} =~ s/$term/<span class=term>$&<\/span>/gi;
906                 $oldbiblio->{'size'}  =~ s/$term/<span class=term>$&<\/span>/gi;
907             }
908         }
909
910         if ( $i % 2 ) {
911             $toggle = "#ffffcc";
912         }
913         else {
914             $toggle = "white";
915         }
916         $oldbiblio->{'toggle'} = $toggle;
917         my @fields = $marcrecord->field($itemtag);
918         my @items_loop;
919         my $items;
920         my $ordered_count     = 0;
921         my $onloan_count      = 0;
922         my $wthdrawn_count    = 0;
923         my $itemlost_count    = 0;
924         my $itembinding_count = 0;
925         my $norequests        = 1;
926
927         foreach my $field (@fields) {
928             my $item;
929             foreach my $code ( keys %subfieldstosearch ) {
930                 $item->{$code} = $field->subfield( $subfieldstosearch{$code} );
931             }
932             if ( $item->{wthdrawn} ) {
933                 $wthdrawn_count++;
934             }
935             elsif ( $item->{notforloan} == -1 ) {
936                 $ordered_count++;
937                 $norequests = 0;
938             }
939             elsif ( $item->{itemlost} ) {
940                 $itemlost_count++;
941             }
942             elsif ( $item->{binding} ) {
943                 $itembinding_count++;
944             }
945             elsif ( ( $item->{onloan} ) && ( $item->{onloan} != '0000-00-00' ) )
946             {
947                 $onloan_count++;
948                 $norequests = 0;
949             }
950             else {
951                 $norequests = 0;
952                 if ( $item->{'homebranch'} ) {
953                     $items->{ $item->{'homebranch'} }->{count}++;
954                 }
955
956                 # Last resort
957                 elsif ( $item->{'holdingbranch'} ) {
958                     $items->{ $item->{'homebranch'} }->{count}++;
959                 }
960                 $items->{ $item->{homebranch} }->{itemcallnumber} =
961                 $item->{itemcallnumber};
962                 $items->{ $item->{homebranch} }->{location} =
963                 $item->{location};
964             }
965         }    # notforloan, item level and biblioitem level
966         for my $key ( keys %$items ) {
967
968             #warn "key: $key";
969             my $this_item = {
970                 branchname     => $branches{$key},
971                 branchcode     => $key,
972                 count          => $items->{$key}->{count},
973                 itemcallnumber => $items->{$key}->{itemcallnumber},
974                 location => $items->{$key}->{location},
975             };
976             push @items_loop, $this_item;
977         }
978         $oldbiblio->{norequests}    = $norequests;
979         $oldbiblio->{items_loop}    = \@items_loop;
980         $oldbiblio->{onloancount}   = $onloan_count;
981         $oldbiblio->{wthdrawncount} = $wthdrawn_count;
982         $oldbiblio->{itemlostcount} = $itemlost_count;
983         $oldbiblio->{bindingcount}  = $itembinding_count;
984         $oldbiblio->{orderedcount}  = $ordered_count;
985
986 # FIXME
987 #  Ugh ... this is ugly, I'll re-write it better above then delete it
988 #     my $norequests = 1;
989 #     my $noitems    = 1;
990 #     if (@items) {
991 #         $noitems = 0;
992 #         foreach my $itm (@items) {
993 #             $norequests = 0 unless $itm->{'itemnotforloan'};
994 #         }
995 #     }
996 #     $oldbiblio->{'noitems'} = $noitems;
997 #     $oldbiblio->{'norequests'} = $norequests;
998 #     $oldbiblio->{'even'} = $even = not $even;
999 #     $oldbiblio->{'itemcount'} = $counts{'total'};
1000 #     my $totalitemcounts = 0;
1001 #     foreach my $key (keys %counts){
1002 #         if ($key ne 'total'){
1003 #             $totalitemcounts+= $counts{$key};
1004 #             $oldbiblio->{'locationhash'}->{$key}=$counts{$key};
1005 #         }
1006 #     }
1007 #     my ($locationtext, $locationtextonly, $notavailabletext) = ('','','');
1008 #     foreach (sort keys %{$oldbiblio->{'locationhash'}}) {
1009 #         if ($_ eq 'notavailable') {
1010 #             $notavailabletext="Not available";
1011 #             my $c=$oldbiblio->{'locationhash'}->{$_};
1012 #             $oldbiblio->{'not-available-p'}=$c;
1013 #         } else {
1014 #             $locationtext.="$_";
1015 #             my $c=$oldbiblio->{'locationhash'}->{$_};
1016 #             if ($_ eq 'Item Lost') {
1017 #                 $oldbiblio->{'lost-p'} = $c;
1018 #             } elsif ($_ eq 'Withdrawn') {
1019 #                 $oldbiblio->{'withdrawn-p'} = $c;
1020 #             } elsif ($_ eq 'On Loan') {
1021 #                 $oldbiblio->{'on-loan-p'} = $c;
1022 #             } else {
1023 #                 $locationtextonly.= $_;
1024 #                 $locationtextonly.= " ($c)<br/> " if $totalitemcounts > 1;
1025 #             }
1026 #             if ($totalitemcounts>1) {
1027 #                 $locationtext.=" ($c)<br/> ";
1028 #             }
1029 #         }
1030 #     }
1031 #     if ($notavailabletext) {
1032 #         $locationtext.= $notavailabletext;
1033 #     } else {
1034 #         $locationtext=~s/, $//;
1035 #     }
1036 #     $oldbiblio->{'location'} = $locationtext;
1037 #     $oldbiblio->{'location-only'} = $locationtextonly;
1038 #     $oldbiblio->{'use-location-flags-p'} = 1;
1039
1040         push( @newresults, $oldbiblio );
1041     }
1042     return @newresults;
1043 }
1044
1045 END { }    # module clean-up code here (global destructor)
1046
1047 1;
1048 __END__
1049
1050 =head1 AUTHOR
1051
1052 Koha Developement team <info@koha.org>
1053
1054 =cut