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