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