removing all useless %env / $env
[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::Interface::CGI::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
52 #warn "field :$field";
53 my ( $tablename, $kohafield ) = split /./, $field;
54
55 #my $tablename=$input->param('tablename');
56 $tablename = "biblio" unless ($tablename);
57
58 #my $kohafield = $input->param('kohafield');
59 my @search = $input->param('search');
60
61 # warn " ".$search[0];
62 my $index = $input->param('index');
63
64 # warn " index: ".$index;
65 my $op = $input->param('op');
66 if ( ( $search[0] ) and not( $op eq 'do_search' ) ) {
67     $op = 'do_search';
68 }
69 my $script_name = 'opac-dictionary.pl';
70 my $query;
71 my $type = $input->param('type');
72
73 #warn " ".$type;
74
75 my $dbh = C4::Context->dbh;
76 my ( $template, $loggedinuser, $cookie );
77
78 my $startfrom = $input->param('startfrom');
79 $startfrom = 0 if ( !defined $startfrom );
80 my $searchdesc;
81 my $resultsperpage;
82
83 #warn "Starting process";
84
85 if ( $op eq "do_search" ) {
86
87     #
88     # searching in biblio
89     #
90     my $sth =
91       $dbh->prepare(
92 "Select distinct tagfield,tagsubfield from marc_subfield_structure where kohafield = ?"
93       );
94     $sth->execute("$field");
95     my ( @tags, @and_or, @operator, @excluding, @value );
96
97     while ( ( my $tagfield, my $tagsubfield, my $liblibrarian ) =
98         $sth->fetchrow )
99     {
100         push @tags, $dbh->quote("$tagfield$tagsubfield");
101     }
102
103     $resultsperpage = $input->param('resultsperpage');
104     $resultsperpage = 19 if ( !defined $resultsperpage );
105     my $orderby = $input->param('orderby');
106
107     findseealso( $dbh, \@tags );
108
109     my @results, my $total;
110     my $strsth =
111 "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 ";
112     my $listtags = "(";
113     foreach my $tag (@tags) {
114         $listtags .= $tag . ",";
115     }
116     $listtags =~ s/,$/)/;
117     $strsth .= $listtags
118       . " and marc_word.tagsubfield=concat(marc_subfield_table.tag,marc_subfield_table.subfieldcode) group by subfieldvalue ";
119
120     #     warn "search in biblio : ".$strsth;
121     my $value = uc( $search[0] );
122     $value =~ s/\*/%/g;
123     $value .= "%" if not( $value =~ m/%/ );
124
125     #     warn " texte : ".$value;
126
127     $sth = $dbh->prepare($strsth);
128     $sth->execute($value);
129     my @catresults;
130     while ( my ( $value, $ctresults ) = $sth->fetchrow ) {
131
132         #         warn "countresults : ".$ctresults;
133         push @catresults,
134           {
135             value => $value,
136             even  => ( $total - $startfrom * $resultsperpage ) % 2,
137             count => $ctresults
138           }
139           if (  ( $total >= $startfrom * $resultsperpage )
140             and ( $total < ( $startfrom + 1 ) * $resultsperpage ) );
141         $total++;
142     }
143
144     $strsth =
145       "Select distinct authtypecode from marc_subfield_structure where (";
146     foreach my $listtags (@tags) {
147         my @taglist = split /,/, $listtags;
148         foreach my $curtag (@taglist) {
149             $strsth .=
150                 "(tagfield='"
151               . substr( $curtag, 1, 3 )
152               . "' AND tagsubfield='"
153               . substr( $curtag, 4, 1 ) . "') OR";
154         }
155     }
156
157     $strsth =~ s/ OR$/)/;
158     $strsth = $strsth . " and authtypecode is not NULL";
159
160     #     warn $strsth;
161     $sth = $dbh->prepare($strsth);
162     $sth->execute;
163
164     #
165     # searching in authorities
166     #
167     my @authresults;
168     my $authnbresults;
169     while ( ( my $authtypecode ) = $sth->fetchrow ) {
170         my ( $curauthresults, $nbresults ) =
171           authoritysearch( $dbh, [''], [''], [''], ['contains'], \@search,
172             $startfrom * $resultsperpage,
173             $resultsperpage, $authtypecode );
174         push @authresults, @$curauthresults;
175         $authnbresults += $nbresults;
176
177         #        warn "auth : $authtypecode nbauthresults : $nbresults";
178     }
179
180     #
181     # OK, filling the template with authorities & biblio entries found.
182     #
183     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
184         {
185             template_name   => "opac-dictionary.tmpl",
186             query           => $input,
187             type            => 'opac',
188             authnotrequired => 1,
189             debug           => 1,
190         }
191     );
192
193     # multi page display gestion
194     my $displaynext = 0;
195     my $displayprev = $startfrom;
196     if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
197         $displaynext = 1;
198     }
199
200     my @field_data = ();
201
202     for ( my $i = 0 ; $i <= $#tags ; $i++ ) {
203         push @field_data, { term => "marclist",  val => $tags[$i] };
204         push @field_data, { term => "and_or",    val => $and_or[$i] };
205         push @field_data, { term => "excluding", val => $excluding[$i] };
206         push @field_data, { term => "operator",  val => $operator[$i] };
207         push @field_data, { term => "value",     val => $value[$i] };
208     }
209
210     my @numbers = ();
211
212     if ( $total > $resultsperpage ) {
213         for ( my $i = 1 ; $i < $total / $resultsperpage + 1 ; $i++ ) {
214             if ( $i < 16 ) {
215                 my $highlight = 0;
216                 ( $startfrom == ( $i - 1 ) ) && ( $highlight = 1 );
217                 push @numbers,
218                   {
219                     number     => $i,
220                     highlight  => $highlight,
221                     searchdata => \@field_data,
222                     startfrom  => ( $i - 1 )
223                   };
224             }
225         }
226     }
227
228     my $from = $startfrom * $resultsperpage + 1;
229     my $to;
230
231     if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
232         $to = $total;
233     }
234     else {
235         $to = ( ( $startfrom + 1 ) * $resultsperpage );
236     }
237     $template->param(
238         anindex              => $input->param('index'),
239         opaclayoutstylesheet => C4::Context->preference("opaclayoutstylesheet"),
240         opaccolorstylesheet  => C4::Context->preference("opaccolorstylesheet"),
241     );
242     $template->param(
243         result         => \@results,
244         catresult      => \@catresults,
245         search         => $search[0],
246         marclist       => $field,
247         authresult     => \@authresults,
248         nbresults      => $authnbresults,
249         startfrom      => $startfrom,
250         displaynext    => $displaynext,
251         displayprev    => $displayprev,
252         resultsperpage => $resultsperpage,
253         startfromnext  => $startfrom + 1,
254         startfromprev  => $startfrom - 1,
255         searchdata     => \@field_data,
256         total          => $total,
257         from           => $from,
258         to             => $to,
259         numbers        => \@numbers,
260         MARC_ON        => C4::Context->preference("marc"),
261     );
262
263 }
264 else {
265     ( $template, $loggedinuser, $cookie ) = get_template_and_user(
266         {
267             template_name   => "opac-dictionary.tmpl",
268             query           => $input,
269             type            => 'opac',
270             authnotrequired => 1,
271             debug           => 1,
272         }
273     );
274
275     #warn "type : $type";
276
277 }
278 $template->param(
279     search   => $search[0],
280     marclist => $field,
281     type     => $type,
282     anindex  => $input->param('index')
283 );
284
285 # Print the page
286 output_html_with_http_headers $input, $cookie, $template->output;
287
288 # Local Variables:
289 # tab-width: 4
290 # End: