use C4::Search; replace use C4::SearchMarc;
[koha_gimpoz] / opac / opac-dictionary.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use C4::Output;
23 use C4::Interface::CGI::Output;
24 use C4::Auth;
25 use CGI;
26 use C4::Search;
27 use C4::AuthoritiesMarc;
28 use C4::Context;
29 use C4::Biblio;
30 use HTML::Template;
31
32 =head1 NAME
33
34 dictionnary.pl : script to search in biblio & authority an existing value
35
36 =head1 SYNOPSIS
37
38 useful when the user want to search a term before running a query. For example, to see if "computer" is used in the database
39
40 The parameter "marclist" tells which field is searched (title, author, subject, but could be anything else)
41
42 This script searches in both biblios & authority
43 * in biblio, the script search in all marc fields related to what the user is looking for (for example, if the dictionnary is used on "author", the script searches in biblio.author, but also in additional authors & any MARC field related to author (through the "seealso" MARC constraint)
44 * in authority, the script search everywhere. Thus, the accepted & rejected forms are found.
45
46 The script shows all results & the user can choose what he want, that is copied into search form.
47
48 =cut
49
50 my $input = new CGI;
51 my $field =$input->param('marclist');
52 #warn "field :$field";
53 my ($tablename, $kohafield)=split /./,$field;
54 #my $tablename=$input->param('tablename');
55 $tablename="biblio" unless ($tablename);
56 #my $kohafield = $input->param('kohafield');
57 my @search = $input->param('search');
58 # warn " ".$search[0];
59 my $index = $input->param('index');
60 # warn " index: ".$index;
61 my $op=$input->param('op');
62 if (($search[0]) and not ($op eq 'do_search')){
63         $op='do_search';
64 }
65 my $script_name = 'opac-dictionary.pl';
66 my $query;
67 my $type=$input->param('type');
68 #warn " ".$type;
69
70 my $dbh = C4::Context->dbh;
71 my ($template, $loggedinuser, $cookie);
72
73 my $env;
74
75 my $startfrom=$input->param('startfrom');
76 $startfrom=0 if(!defined $startfrom);
77 my $searchdesc;
78 my $resultsperpage;
79
80 #warn "Starting process";
81
82 if ($op eq "do_search") {
83         #
84         # searching in biblio
85         #
86         my $sth=$dbh->prepare("Select distinct tagfield,tagsubfield from marc_subfield_structure where kohafield = ?");
87         $sth->execute("$field");
88         my (@tags, @and_or, @operator, @excluding,@value);
89         
90         while ((my $tagfield,my $tagsubfield,my $liblibrarian) = $sth->fetchrow) {
91                 push @tags, $dbh->quote("$tagfield$tagsubfield");
92         }
93
94         $resultsperpage= $input->param('resultsperpage');
95         $resultsperpage = 19 if(!defined $resultsperpage);
96         my $orderby = $input->param('orderby');
97
98         findseealso($dbh,\@tags);
99
100         my @results, my $total;
101         my $strsth="select distinct subfieldvalue, count(marc_subfield_table.bibid) from marc_subfield_table,marc_word where marc_word.word like ? and marc_subfield_table.bibid=marc_word.bibid and marc_subfield_table.tagorder=marc_word.tagorder and marc_word.tagsubfield in ";
102         my $listtags="(";
103         foreach my $tag (@tags){
104                 $listtags .= $tag .",";
105         }
106         $listtags =~s/,$/)/;
107         $strsth .= $listtags." and marc_word.tagsubfield=concat(marc_subfield_table.tag,marc_subfield_table.subfieldcode) group by subfieldvalue ";
108 #       warn "search in biblio : ".$strsth;
109         my $value = uc($search[0]);
110         $value=~s/\*/%/g;
111         $value.= "%" if not($value=~m/%/);
112 #       warn " texte : ".$value;
113
114         $sth=$dbh->prepare($strsth);
115         $sth->execute($value);
116         my $total;
117         my @catresults;
118         while (my ($value,$ctresults)=$sth->fetchrow) {
119 #               warn "countresults : ".$ctresults;
120                 push @catresults,{value=> $value, 
121                                                   even=>($total-$startfrom*$resultsperpage)%2,
122                                                   count=>$ctresults
123                                                   } if (($total>=$startfrom*$resultsperpage) and ($total<($startfrom+1)*$resultsperpage));
124                 $total++;
125         }
126         
127
128         my $strsth="Select distinct authtypecode from marc_subfield_structure where (";
129         foreach my $listtags (@tags){
130                 my @taglist=split /,/,$listtags;
131                 foreach my $curtag (@taglist){
132                         $strsth.="(tagfield='".substr($curtag,1,3)."' AND tagsubfield='".substr($curtag,4,1)."') OR";
133                 }
134         }
135         
136         $strsth=~s/ OR$/)/;
137         my $strsth = $strsth." and authtypecode is not NULL";
138 #       warn $strsth;
139         my $sth=$dbh->prepare($strsth);
140         $sth->execute;
141         
142         #
143         # searching in authorities
144         #
145         my @authresults;
146         my $authnbresults;
147         while ((my $authtypecode) = $sth->fetchrow) {
148                 my ($curauthresults,$nbresults) = authoritysearch($dbh,[''],[''],[''],['contains'],
149                                                                                                                 \@search,$startfrom*$resultsperpage, $resultsperpage,$authtypecode);
150                 push @authresults, @$curauthresults;
151                 $authnbresults+=$nbresults;
152 #               warn "auth : $authtypecode nbauthresults : $nbresults";
153         }
154         
155         # 
156         # OK, filling the template with authorities & biblio entries found.
157         #
158         ($template, $loggedinuser, $cookie)
159                 = get_template_and_user({template_name => "opac-dictionary.tmpl",
160                                 query => $input,
161                                 type => 'opac',
162                                 authnotrequired => 1,
163                                 debug => 1,
164                                 });
165
166         # multi page display gestion
167         my $displaynext=0;
168         my $displayprev=$startfrom;
169         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ) {
170                 $displaynext = 1;
171         }
172
173         my @field_data = ();
174
175         for(my $i = 0 ; $i <= $#tags ; $i++) {
176                 push @field_data, { term => "marclist", val=>$tags[$i] };
177                 push @field_data, { term => "and_or", val=>$and_or[$i] };
178                 push @field_data, { term => "excluding", val=>$excluding[$i] };
179                 push @field_data, { term => "operator", val=>$operator[$i] };
180                 push @field_data, { term => "value", val=>$value[$i] };
181         }
182
183         my @numbers = ();
184
185         if ($total>$resultsperpage) {
186                 for (my $i=1; $i<$total/$resultsperpage+1; $i++) {
187                         if ($i<16) {
188                         my $highlight=0;
189                         ($startfrom==($i-1)) && ($highlight=1);
190                         push @numbers, { number => $i,
191                                         highlight => $highlight ,
192                                         searchdata=> \@field_data,
193                                         startfrom => ($i-1)};
194                         }
195         }
196         }
197
198         my $from = $startfrom*$resultsperpage+1;
199         my $to;
200
201         if($total < (($startfrom+1)*$resultsperpage))
202         {
203                 $to = $total;
204         } else {
205                 $to = (($startfrom+1)*$resultsperpage);
206         }
207         $template->param(anindex => $input->param('index'),
208         opaclayoutstylesheet => C4::Context->preference("opaclayoutstylesheet"),
209         opaccolorstylesheet => C4::Context->preference("opaccolorstylesheet"),
210         );
211         $template->param(result => \@results,
212                                          catresult=> \@catresults,
213                                                 search => $search[0],
214                                                 marclist =>$field,
215                                                 authresult => \@authresults,
216                                                 nbresults => $authnbresults,
217                                                 startfrom=> $startfrom,
218                                                 displaynext=> $displaynext,
219                                                 displayprev=> $displayprev,
220                                                 resultsperpage => $resultsperpage,
221                                                 startfromnext => $startfrom+1,
222                                                 startfromprev => $startfrom-1,
223                                                 searchdata=>\@field_data,
224                                                 total=>$total,
225                                                 from=>$from,
226                                                 to=>$to,
227                                                 numbers=>\@numbers,
228                                                 MARC_ON => C4::Context->preference("marc"),
229                                                 );
230
231  } else {
232         ($template, $loggedinuser, $cookie)
233                 = get_template_and_user({template_name => "opac-dictionary.tmpl",
234                                 query => $input,
235                                 type => 'opac',
236                                 authnotrequired => 1,
237                                 debug => 1,
238                                 });
239 #warn "type : $type";
240  
241  }
242 $template->param(search => $search[0],
243                                         marclist =>$field,
244                                         type=>$type,
245                                         anindex => $input->param('index'));
246
247 # Print the page
248 output_html_with_http_headers $input, $cookie, $template->output;
249
250 # Local Variables:
251 # tab-width: 4
252 # End: