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