start of big MARC21 authorities work
[koha.git] / opac / opac-dictionary.pl
blob985986cf36bec62800686a61f0f9ece57106a1c1
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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.
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.
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
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;
30 =head1 NAME
32 dictionnary.pl : script to search in biblio & authority an existing value
34 =head1 SYNOPSIS
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
38 The parameter "marclist" tells which field is searched (title, author, subject, but could be anything else)
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.
44 The script shows all results & the user can choose what he want, that is copied into search form.
46 =cut
48 my $input = new CGI;
49 my $field = $input->param('marclist');
51 #warn "field :$field";
52 my ( $tablename, $kohafield ) = split /./, $field;
54 #my $tablename=$input->param('tablename');
55 $tablename = "biblio" unless ($tablename);
57 #my $kohafield = $input->param('kohafield');
58 my @search = $input->param('search');
60 # warn " ".$search[0];
61 my $index = $input->param('index');
63 # warn " index: ".$index;
64 my $op = $input->param('op');
65 if ( ( $search[0] ) and not( $op eq 'do_search' ) ) {
66 $op = 'do_search';
68 my $script_name = 'opac-dictionary.pl';
69 my $query;
70 my $type = $input->param('type');
72 #warn " ".$type;
74 my $dbh = C4::Context->dbh;
75 my ( $template, $loggedinuser, $cookie );
77 my $startfrom = $input->param('startfrom');
78 $startfrom = 0 if ( !defined $startfrom );
79 my $searchdesc;
80 my $resultsperpage;
82 #warn "Starting process";
84 if ( $op eq "do_search" ) {
87 # searching in biblio
89 my $sth =
90 $dbh->prepare(
91 "Select distinct tagfield,tagsubfield from marc_subfield_structure where kohafield = ?"
93 $sth->execute("$field");
94 my ( @tags, @and_or, @operator, @excluding, @value );
96 while ( ( my $tagfield, my $tagsubfield, my $liblibrarian ) =
97 $sth->fetchrow )
99 push @tags, $dbh->quote("$tagfield$tagsubfield");
102 $resultsperpage = $input->param('resultsperpage');
103 $resultsperpage = 19 if ( !defined $resultsperpage );
104 my $orderby = $input->param('orderby');
106 findseealso( $dbh, \@tags );
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 . ",";
115 $listtags =~ s/,$/)/;
116 $strsth .= $listtags
117 . " and marc_word.tagsubfield=concat(marc_subfield_table.tag,marc_subfield_table.subfieldcode) group by subfieldvalue ";
119 # warn "search in biblio : ".$strsth;
120 my $value = uc( $search[0] );
121 $value =~ s/\*/%/g;
122 $value .= "%" if not( $value =~ m/%/ );
124 # warn " texte : ".$value;
126 $sth = $dbh->prepare($strsth);
127 $sth->execute($value);
128 my @catresults;
129 while ( my ( $value, $ctresults ) = $sth->fetchrow ) {
131 # warn "countresults : ".$ctresults;
132 push @catresults,
134 value => $value,
135 even => ( $total - $startfrom * $resultsperpage ) % 2,
136 count => $ctresults
138 if ( ( $total >= $startfrom * $resultsperpage )
139 and ( $total < ( $startfrom + 1 ) * $resultsperpage ) );
140 $total++;
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";
156 $strsth =~ s/ OR$/)/;
157 $strsth = $strsth . " and authtypecode is not NULL";
159 # warn $strsth;
160 $sth = $dbh->prepare($strsth);
161 $sth->execute;
164 # searching in authorities
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;
176 # warn "auth : $authtypecode nbauthresults : $nbresults";
180 # OK, filling the template with authorities & biblio entries found.
182 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
184 template_name => "opac-dictionary.tmpl",
185 query => $input,
186 type => 'opac',
187 authnotrequired => 1,
188 debug => 1,
192 # multi page display gestion
193 my $displaynext = 0;
194 my $displayprev = $startfrom;
195 if ( ( $total - ( ( $startfrom + 1 ) * ($resultsperpage) ) ) > 0 ) {
196 $displaynext = 1;
199 my @field_data = ();
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] };
209 my @numbers = ();
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,
218 number => $i,
219 highlight => $highlight,
220 searchdata => \@field_data,
221 startfrom => ( $i - 1 )
227 my $from = $startfrom * $resultsperpage + 1;
228 my $to;
230 if ( $total < ( ( $startfrom + 1 ) * $resultsperpage ) ) {
231 $to = $total;
233 else {
234 $to = ( ( $startfrom + 1 ) * $resultsperpage );
236 $template->param(
237 anindex => $input->param('index'),
238 opaclayoutstylesheet => C4::Context->preference("opaclayoutstylesheet"),
239 opaccolorstylesheet => C4::Context->preference("opaccolorstylesheet"),
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"),
263 else {
264 ( $template, $loggedinuser, $cookie ) = get_template_and_user(
266 template_name => "opac-dictionary.tmpl",
267 query => $input,
268 type => 'opac',
269 authnotrequired => 1,
270 debug => 1,
274 #warn "type : $type";
277 $template->param(
278 search => $search[0],
279 marclist => $field,
280 type => $type,
281 anindex => $input->param('index')
284 # Print the page
285 output_html_with_http_headers $input, $cookie, $template->output;
287 # Local Variables:
288 # tab-width: 4
289 # End: