pagination did not work anymore : we stepped only 1 record by 1 record. fixes the...
[koha.git] / catalogue / 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::Auth;
24 use CGI;
25 use C4::Search;
26 use C4::AuthoritiesMarc;
27 use C4::Context;
28 use C4::Biblio;
29
30
31 =head1 NAME
32
33 dictionnary.pl : script to search in biblio & authority an existing value
34
35 =head1 SYNOPSIS
36
37 useful when the user want to search a term before running a query. For example, to see if "computer" is used in the database
38
39 The parameter "marclist" tells which field is searched (title, author, subject, but could be anything else)
40
41 This script searches in both biblios & authority
42 * 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)
43 * in authority, the script search everywhere. Thus, the accepted & rejected forms are found.
44
45 The script shows all results & the user can choose what he want, that is copied into search form.
46
47 =cut
48
49 my $input = new CGI;
50 my $field =$input->param('marclist');
51 #warn "field :$field";
52 my ($tablename, $kohafield)=split /./,$field;
53 #my $tablename=$input->param('tablename');
54 $tablename="biblio" unless ($tablename);
55 #my $kohafield = $input->param('kohafield');
56 my @search = $input->param('search');
57 # warn " ".$search[0];
58 my $index = $input->param('index');
59 # warn " index: ".$index;
60 my $op=$input->param('op');
61 if (($search[0]) and not ($op eq 'do_search')){
62         $op='do_search';
63 }
64 my $script_name = 'catalogue/dictionary.pl';
65 my $query;
66 my $type=$input->param('type');
67 #warn " ".$type;
68
69 my $dbh = C4::Context->dbh;
70 my ($template, $loggedinuser, $cookie);
71
72 my $startfrom=$input->param('startfrom');
73 $startfrom=0 if(!defined $startfrom);
74 my $searchdesc;
75 my $resultsperpage;
76
77 #warn "Starting process";
78
79 if ($op eq "do_search") {
80         #
81         # searching in biblio
82         #
83         my $sth=$dbh->prepare("Select distinct tagfield,tagsubfield from marc_subfield_structure where kohafield = ?");
84         $sth->execute("$field");
85         my (@tags, @and_or, @operator, @excluding,@value);
86         
87         while ((my $tagfield,my $tagsubfield,my $liblibrarian) = $sth->fetchrow) {
88                 push @tags, $dbh->quote("$tagfield$tagsubfield");
89         }
90
91         $resultsperpage= $input->param('resultsperpage');
92         $resultsperpage = 19 if(!defined $resultsperpage);
93         my $orderby = $input->param('orderby');
94
95         findseealso($dbh,\@tags);
96
97         my @results, my $total;
98         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 ";
99         my $listtags="(";
100         foreach my $tag (@tags){
101                 $listtags .= $tag .",";
102         }
103         $listtags =~s/,$/)/;
104         $strsth .= $listtags." and marc_word.tagsubfield=concat(marc_subfield_table.tag,marc_subfield_table.subfieldcode) group by subfieldvalue ";
105 #       warn "search in biblio : ".$strsth;
106         my $value = uc($search[0]);
107         $value=~s/\*/%/g;
108         $value.= "%" if not($value=~m/%/);
109 #       warn " texte : ".$value;
110
111         $sth=$dbh->prepare($strsth);
112         $sth->execute($value);
113         my $total;
114         my @catresults;
115         my $javalue;
116         while (my ($value,$ctresults)=$sth->fetchrow) {
117                 # This $javalue is used for the javascript selectentry function (javalue for javascript value !)
118                 $javalue = $value;
119                 $javalue =~s/'/\\'/g;
120
121                 push @catresults,{value=> $value, 
122                                                   javalue=> $javalue,
123                                                   even=>($total-$startfrom*$resultsperpage)%2,
124                                                   count=>$ctresults
125                                                   } if (($total>=$startfrom*$resultsperpage) and ($total<($startfrom+1)*$resultsperpage));
126                 $total++;
127         }
128         
129
130         my $strsth="Select distinct authtypecode from marc_subfield_structure where (";
131         foreach my $listtags (@tags){
132                 my @taglist=split /,/,$listtags;
133                 foreach my $curtag (@taglist){
134                         $curtag =~s/\s+//;
135                         $strsth.="(tagfield='".substr($curtag,1,3)."' AND tagsubfield='".substr($curtag,4,1)."') OR";
136                 }
137         }
138         
139         $strsth=~s/ OR$/)/;
140         my $strsth = $strsth." and authtypecode is not NULL";
141 #       warn $strsth;
142         my $sth=$dbh->prepare($strsth);
143         $sth->execute;
144         
145         #
146         # searching in authorities
147         #
148         my @authresults;
149         my $authnbresults;
150         while ((my $authtypecode) = $sth->fetchrow) {
151                 my ($curauthresults,$nbresults) = SearchAuthorities([''],[''],[''],['contains'],
152                                                                                                                 \@search,$startfrom*$resultsperpage, $resultsperpage,$authtypecode);
153                 if (defined(@$curauthresults)) {
154                         for (my $i = 0; $i < @$curauthresults ;$i++) {
155                                 @$curauthresults[$i]->{jamainentry} = @$curauthresults[$i]->{mainentry};
156                                 @$curauthresults[$i]->{jamainentry} =~ s/'/\\'/g;
157                         }
158                 }
159                 push @authresults, @$curauthresults;
160                 $authnbresults+=$nbresults;
161 #               warn "auth : $authtypecode nbauthresults : $nbresults";
162         }
163         
164         # 
165         # OK, filling the template with authorities & biblio entries found.
166         #
167         ($template, $loggedinuser, $cookie)
168                 = get_template_and_user({template_name => "catalogue/dictionary.tmpl",
169                                 query => $input,
170                                 type => $type,
171                                 authnotrequired => 0,
172                                 flagsrequired => {catalogue => 1},
173                                 debug => 1,
174                                 });
175
176         # multi page display gestion
177         my $displaynext=0;
178         my $displayprev=$startfrom;
179         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ) {
180                 $displaynext = 1;
181         }
182
183         my @field_data = ();
184
185         for(my $i = 0 ; $i <= $#tags ; $i++) {
186                 push @field_data, { term => "marclist", val=>$tags[$i] };
187                 push @field_data, { term => "and_or", val=>$and_or[$i] };
188                 push @field_data, { term => "excluding", val=>$excluding[$i] };
189                 push @field_data, { term => "operator", val=>$operator[$i] };
190                 push @field_data, { term => "value", val=>$value[$i] };
191         }
192
193         my @numbers = ();
194
195         if ($total>$resultsperpage) {
196                 for (my $i=1; $i<$total/$resultsperpage+1; $i++) {
197                         if ($i<16) {
198                         my $highlight=0;
199                         ($startfrom==($i-1)) && ($highlight=1);
200                         push @numbers, { number => $i,
201                                         highlight => $highlight ,
202                                         searchdata=> \@field_data,
203                                         startfrom => ($i-1)};
204                         }
205         }
206         }
207
208         my $from = $startfrom*$resultsperpage+1;
209         my $to;
210
211         if($total < (($startfrom+1)*$resultsperpage))
212         {
213                 $to = $total;
214         } else {
215                 $to = (($startfrom+1)*$resultsperpage);
216         }
217         $template->param(anindex => $input->param('index'));
218         $template->param(result => \@results,
219                                          catresult=> \@catresults,
220                                                 search => $search[0],
221                                                 marclist =>$field,
222                                                 authresult => \@authresults,
223                                                 nbresults => $authnbresults,
224                                                 startfrom=> $startfrom,
225                                                 displaynext=> $displaynext,
226                                                 displayprev=> $displayprev,
227                                                 resultsperpage => $resultsperpage,
228                                                 startfromnext => $startfrom+1,
229                                                 startfromprev => $startfrom-1,
230                                                 searchdata=>\@field_data,
231                                                 total=>$total,
232                                                 from=>$from,
233                                                 to=>$to,
234                                                 numbers=>\@numbers,
235                                                 MARC_ON => C4::Context->preference("marc"),
236                                                 );
237
238  } else {
239         ($template, $loggedinuser, $cookie)
240                 = get_template_and_user({template_name => "catalogue/dictionary.tmpl",
241                                 query => $input,
242                                 type => $type,
243                                 authnotrequired => 0,
244                                 flagsrequired => {catalogue => 1},
245                                 debug => 1,
246                                 });
247 #warn "type : $type";
248  
249  }
250 $template->param(search => $search[0],
251                 marclist =>$field,
252                 type=>$type,
253                 anindex => $input->param('index'),
254                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
255                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
256                 IntranetNav => C4::Context->preference("IntranetNav"),
257                 );
258
259 # Print the page
260 output_html_with_http_headers $input, $cookie, $template->output;
261
262 # Local Variables:
263 # tab-width: 4
264 # End: