Sorting resullts
[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 $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                 push @and_or, "";
79                 push @operator, "contains";
80                 push @excluding, "";
81                 push @value, @search ;
82         }
83
84         $resultsperpage= $input->param('resultsperpage');
85         $resultsperpage = 19 if(!defined $resultsperpage);
86         my $orderby = $input->param('orderby');
87
88         findseealso($dbh,\@tags);
89 #       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
90
91
92         my ($results,$total) = catalogsearch($dbh,\@tags ,\@and_or,
93                                                                                 \@excluding, \@operator,  \@value,
94                                                                                 $startfrom*$resultsperpage, $resultsperpage,$orderby);
95         my %seen = ();
96
97         foreach my $item (@$results) {
98                 my $display;
99                 $display="author" if ($field=~/author/);
100                 $display="title" if ($field=~/title/);
101                 $display="subject" if ($field=~/subject/);
102                 $display="publishercode" if ($field=~/publisher/);
103             $seen{$item->{$display}}++;
104         }
105         my @catresults;
106         foreach my $name (sort keys %seen){
107                 push @catresults, { value => $name , count => $seen{$name}}
108         }
109
110         my $strsth="Select distinct authtypecode from marc_subfield_structure where ";
111         my $strtagfields="tagfield in (";
112         my $strtagsubfields=" and tagsubfield in (";
113         foreach my $listtags (@tags){
114                 my @taglist=split /,/,$listtags;
115                 foreach my $curtag (@taglist){
116                         $strtagfields=$strtagfields."'".substr($curtag,1,3)."',";
117                         $strtagsubfields=$strtagsubfields."'".substr($curtag,4,1)."',";
118                 }
119         }
120         $strtagfields=~s/,$/)/;
121         $strtagsubfields=~s/,$/)/;
122         my $strsth = $strsth.$strtagfields.$strtagsubfields." and authtypecode is not NULL";
123         warn $strsth;
124         my $sth=$dbh->prepare($strsth);
125         $sth->execute;
126         
127         my @authresults;
128         my $authnbresults;
129         while ((my $authtypecode) = $sth->fetchrow) {
130                 my ($curauthresults,$nbresults) = authoritysearch($dbh,[''],[''],[''],['contains'],
131                                                                                                                 \@search,$startfrom*$resultsperpage, $resultsperpage,$authtypecode);
132                 push @authresults, @$curauthresults;
133                 $authnbresults+=$nbresults;
134 #               warn "auth : $authtypecode nbauthresults : $nbresults";
135         }
136         
137         ($template, $loggedinuser, $cookie)
138                 = get_template_and_user({template_name => "search.marc/dictionary.tmpl",
139                                 query => $input,
140                                 type => $type,
141                                 authnotrequired => 0,
142                                 flagsrequired => {borrowers => 1},
143                                 flagsrequired => {catalogue => 1},
144                                 debug => 1,
145                                 });
146
147         # multi page display gestion
148         my $displaynext=0;
149         my $displayprev=$startfrom;
150         if(($total - (($startfrom+1)*($resultsperpage))) > 0 ) {
151                 $displaynext = 1;
152         }
153
154         my @field_data = ();
155
156         for(my $i = 0 ; $i <= $#tags ; $i++) {
157                 push @field_data, { term => "marclist", val=>$tags[$i] };
158                 push @field_data, { term => "and_or", val=>$and_or[$i] };
159                 push @field_data, { term => "excluding", val=>$excluding[$i] };
160                 push @field_data, { term => "operator", val=>$operator[$i] };
161                 push @field_data, { term => "value", val=>$value[$i] };
162         }
163
164         my @numbers = ();
165
166         if ($total>$resultsperpage) {
167                 for (my $i=1; $i<$total/$resultsperpage+1; $i++) {
168                         if ($i<16) {
169                         my $highlight=0;
170                         ($startfrom==($i-1)) && ($highlight=1);
171                         push @numbers, { number => $i,
172                                         highlight => $highlight ,
173                                         searchdata=> \@field_data,
174                                         startfrom => ($i-1)};
175                         }
176         }
177         }
178
179         my $from = $startfrom*$resultsperpage+1;
180         my $to;
181
182         if($total < (($startfrom+1)*$resultsperpage))
183         {
184                 $to = $total;
185         } else {
186                 $to = (($startfrom+1)*$resultsperpage);
187         }
188         $template->param(result => $results,
189                                          catresult=> \@catresults,
190                                                 search => $search[0],
191                                                 marclist =>$field,
192                                                 authresult => \@authresults,
193                                                 nbresults => $authnbresults,
194                                                 startfrom=> $startfrom,
195                                                 displaynext=> $displaynext,
196                                                 displayprev=> $displayprev,
197                                                 resultsperpage => $resultsperpage,
198                                                 startfromnext => $startfrom+1,
199                                                 startfromprev => $startfrom-1,
200                                                 searchdata=>\@field_data,
201                                                 total=>$total,
202                                                 from=>$from,
203                                                 to=>$to,
204                                                 numbers=>\@numbers,
205                                                 MARC_ON => C4::Context->preference("marc"),
206                                                 );
207
208  } else {
209         ($template, $loggedinuser, $cookie)
210                 = get_template_and_user({template_name => "search.marc/dictionary.tmpl",
211                                 query => $input,
212                                 type => $type,
213                                 authnotrequired => 0,
214                                 flagsrequired => {catalogue => 1},
215                                 debug => 1,
216                                 search => $search[0],
217                                 marclist =>$field,
218                                 });
219 #warn "type : $type";
220  
221  }
222 $template->param(search => $search[0],
223                                         marclist =>$field,
224                                         type=>$type);
225
226 # Print the page
227 output_html_with_http_headers $input, $cookie, $template->output;
228
229 # Local Variables:
230 # tab-width: 4
231 # End: