Dictionary feature implemented
[koha.git] / search.marc / 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::SearchMarc;
28 use C4::AuthoritiesMarc;
29 use C4::Context;
30 use C4::Biblio;
31 use HTML::Template;
32
33 my $input = new CGI;
34 my $field =$input->param('marclist');
35 #warn "field :$field";
36 my ($tablename, $kohafield)=split /./,$field;
37 #my $tablename=$input->param('tablename');
38 $tablename="biblio" unless ($tablename);
39 #my $kohafield = $input->param('kohafield');
40 my @search = $input->param('search');
41 warn " ".$search[0];
42 my $index = $input->param('index');
43 warn " index: ".$index;
44 my $op=$input->param('op');
45 if (($search[0]) and not ($op eq 'do_search')){
46         $op='do_search';
47 }
48 my $script_name = 'search.marc/dictionary.pl';
49 my $query;
50 my $type=$input->param('type');
51 #warn " ".$type;
52
53 my $dbh = C4::Context->dbh;
54 my ($template, $loggedinuser, $cookie);
55
56 my $env;
57
58 my $startfrom=$input->param('startfrom');
59 $startfrom=0 if(!defined $startfrom);
60 my $searchdesc;
61 my $resultsperpage;
62
63 #warn "Starting process";
64
65 if ($op eq "do_search") {
66         ($template, $loggedinuser, $cookie)
67                         = get_template_and_user({template_name => "search.marc/dictionary.tmpl",
68                                         query => $input,
69                                         type => $type,
70                                         authnotrequired => 0,
71                                         flagsrequired => {catalogue => 1},
72                                         debug => 1,
73                                         });
74         my $sth=$dbh->prepare("Select distinct tagfield,tagsubfield from marc_subfield_structure where kohafield = ?");
75         $sth->execute("$field");
76         my (@tags, @and_or, @operator, @excluding,@value);
77         
78         while ((my $tagfield,my $tagsubfield,my $liblibrarian) = $sth->fetchrow) {
79                 push @tags, $dbh->quote("$tagfield$tagsubfield");
80         }
81
82         $resultsperpage= $input->param('resultsperpage');
83         $resultsperpage = 19 if(!defined $resultsperpage);
84         my $orderby = $input->param('orderby');
85
86         findseealso($dbh,\@tags);
87 #       select distinct m1.bibid from biblio,biblioitems,marc_biblio,marc_word as m1 where biblio.biblionumber=marc_biblio.biblionumber and biblio.biblionumber=biblioitems.biblionumber and m1.bibid=marc_biblio.bibid and (m1.word  like 'Paul' and m1.tagsubfield in ('200f','710a','711a','712a','701a','702a','700a')) order by biblio.title
88
89
90         my @results, my $total;
91 #       my ($results,$total) = catalogsearch($dbh,\@tags ,\@and_or,
92 #                                                                               \@excluding, \@operator,  \@value,
93 #                                                                               $startfrom*$resultsperpage, $resultsperpage,$orderby);
94         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_word.tagsubfield in ";
95         my $listtags="(";
96         foreach my $tag (@tags){
97                 $listtags .= $tag .",";
98         }
99         $listtags =~s/,$/)/;
100         $strsth .= $listtags." and marc_word.tagsubfield=marc_subfield_table.tag+marc_subfield_table.subfieldcode group by subfieldvalue ";
101         warn "".$strsth;
102         my $value = uc($search[0]);
103         $value=~s/\*/%/g;
104         $value.= "%" if not($value=~m/%/);
105         warn " texte : ".$value;
106
107         $sth=$dbh->prepare($strsth);
108         $sth->execute($value);
109         my $total;
110         my @catresults;
111         while (my ($value,$ctresults)=$sth->fetchrow) {
112                 warn "countresults : ".$ctresults;
113                 push @catresults,{value=> $value, 
114                                                   even=>($total-$startfrom*$resultsperpage)%2,
115                                                   count=>$ctresults
116                                                   } if (($total>=$startfrom*$resultsperpage) and ($total<($startfrom+1)*$resultsperpage));
117                 $total++;
118         }
119         
120
121         my $strsth="Select distinct authtypecode from marc_subfield_structure where (";
122         foreach my $listtags (@tags){
123                 my @taglist=split /,/,$listtags;
124                 foreach my $curtag (@taglist){
125                         $strsth.="(tagfield='".substr($curtag,1,3)."' AND tagsubfield='".substr($curtag,4,1)."') OR";
126                 }
127         }
128         
129         $strsth=~s/ OR$/)/;
130         my $strsth = $strsth." and authtypecode is not NULL";
131         warn $strsth;
132         my $sth=$dbh->prepare($strsth);
133         $sth->execute;
134         
135         my @authresults;
136         my $authnbresults;
137         while ((my $authtypecode) = $sth->fetchrow) {
138                 my ($curauthresults,$nbresults) = authoritysearch($dbh,[''],[''],[''],['contains'],
139                                                                                                                 \@search,$startfrom*$resultsperpage, $resultsperpage,$authtypecode);
140                 push @authresults, @$curauthresults;
141                 $authnbresults+=$nbresults;
142 #               warn "auth : $authtypecode nbauthresults : $nbresults";
143         }
144         
145         ($template, $loggedinuser, $cookie)
146                 = get_template_and_user({template_name => "search.marc/dictionary.tmpl",
147                                 query => $input,
148                                 type => $type,
149                                 authnotrequired => 0,
150                                 flagsrequired => {borrowers => 1},
151                                 flagsrequired => {catalogue => 1},
152                                 debug => 1,
153                                 });
154
155         # multi page display gestion
156         my $displaynext=0;
157         my $displayprev=$startfrom;
158         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ) {
159                 $displaynext = 1;
160         }
161
162         my @field_data = ();
163
164         for(my $i = 0 ; $i <= $#tags ; $i++) {
165                 push @field_data, { term => "marclist", val=>$tags[$i] };
166                 push @field_data, { term => "and_or", val=>$and_or[$i] };
167                 push @field_data, { term => "excluding", val=>$excluding[$i] };
168                 push @field_data, { term => "operator", val=>$operator[$i] };
169                 push @field_data, { term => "value", val=>$value[$i] };
170         }
171
172         my @numbers = ();
173
174         if ($total>$resultsperpage) {
175                 for (my $i=1; $i<$total/$resultsperpage+1; $i++) {
176                         if ($i<16) {
177                         my $highlight=0;
178                         ($startfrom==($i-1)) && ($highlight=1);
179                         push @numbers, { number => $i,
180                                         highlight => $highlight ,
181                                         searchdata=> \@field_data,
182                                         startfrom => ($i-1)};
183                         }
184         }
185         }
186
187         my $from = $startfrom*$resultsperpage+1;
188         my $to;
189
190         if($total < (($startfrom+1)*$resultsperpage))
191         {
192                 $to = $total;
193         } else {
194                 $to = (($startfrom+1)*$resultsperpage);
195         }
196         $template->param(anindex => $input->param('index'));
197         $template->param(result => \@results,
198                                          catresult=> \@catresults,
199                                                 search => $search[0],
200                                                 marclist =>$field,
201                                                 authresult => \@authresults,
202                                                 nbresults => $authnbresults,
203                                                 startfrom=> $startfrom,
204                                                 displaynext=> $displaynext,
205                                                 displayprev=> $displayprev,
206                                                 resultsperpage => $resultsperpage,
207                                                 startfromnext => $startfrom+1,
208                                                 startfromprev => $startfrom-1,
209                                                 searchdata=>\@field_data,
210                                                 total=>$total,
211                                                 from=>$from,
212                                                 to=>$to,
213                                                 numbers=>\@numbers,
214                                                 MARC_ON => C4::Context->preference("marc"),
215                                                 );
216
217  } else {
218         ($template, $loggedinuser, $cookie)
219                 = get_template_and_user({template_name => "search.marc/dictionary.tmpl",
220                                 query => $input,
221                                 type => $type,
222                                 authnotrequired => 0,
223                                 flagsrequired => {catalogue => 1},
224                                 debug => 1,
225                                 });
226 #warn "type : $type";
227  
228  }
229 $template->param(search => $search[0],
230                                         marclist =>$field,
231                                         type=>$type,
232                                         anindex => $input->param('index'));
233
234 # Print the page
235 output_html_with_http_headers $input, $cookie, $template->output;
236
237 # Local Variables:
238 # tab-width: 4
239 # End: