4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
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
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.
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
23 use C4::Interface::CGI::Output;
28 use C4::AuthoritiesMarc;
35 dictionnary.pl : script to search in biblio & authority an existing value
39 useful when the user want to search a term before running a query. For example, to see if "computer" is used in the database
41 The parameter "marclist" tells which field is searched (title, author, subject, but could be anything else)
43 This script searches in both biblios & authority
44 * 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)
45 * in authority, the script search everywhere. Thus, the accepted & rejected forms are found.
47 The script shows all results & the user can choose what he want, that is copied into search form.
52 my $field =$input->param('marclist');
53 #warn "field :$field";
54 my ($tablename, $kohafield)=split /./,$field;
55 #my $tablename=$input->param('tablename');
56 $tablename="biblio" unless ($tablename);
57 #my $kohafield = $input->param('kohafield');
58 my @search = $input->param('search');
59 # warn " ".$search[0];
60 my $index = $input->param('index');
61 # warn " index: ".$index;
62 my $op=$input->param('op');
63 if (($search[0]) and not ($op eq 'do_search')){
66 my $script_name = 'search.marc/dictionary.pl';
68 my $type=$input->param('type');
71 my $dbh = C4::Context->dbh;
72 my ($template, $loggedinuser, $cookie);
76 my $startfrom=$input->param('startfrom');
77 $startfrom=0 if(!defined $startfrom);
81 #warn "Starting process";
83 if ($op eq "do_search") {
87 my $sth=$dbh->prepare("Select distinct tagfield,tagsubfield from marc_subfield_structure where kohafield = ?");
88 $sth->execute("$field");
89 my (@tags, @and_or, @operator, @excluding,@value);
91 while ((my $tagfield,my $tagsubfield,my $liblibrarian) = $sth->fetchrow) {
92 push @tags, $dbh->quote("$tagfield$tagsubfield");
95 $resultsperpage= $input->param('resultsperpage');
96 $resultsperpage = 19 if(!defined $resultsperpage);
97 my $orderby = $input->param('orderby');
99 findseealso($dbh,\@tags);
101 my @results, my $total;
102 my $strsth="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 ";
104 foreach my $tag (@tags){
105 $listtags .= $tag .",";
108 $strsth .= $listtags." and marc_word.tagsubfield=concat(marc_subfield_table.tag,marc_subfield_table.subfieldcode) group by subfieldvalue ";
109 # warn "search in biblio : ".$strsth;
110 my $value = uc($search[0]);
112 $value.= "%" if not($value=~m/%/);
113 # warn " texte : ".$value;
115 $sth=$dbh->prepare($strsth);
116 $sth->execute($value);
120 while (my ($value,$ctresults)=$sth->fetchrow) {
121 # This $javalue is used for the javascript selectentry function (javalue for javascript value !)
123 $javalue =~s/'/\\'/g;
125 push @catresults,{value=> $value,
127 even=>($total-$startfrom*$resultsperpage)%2,
129 } if (($total>=$startfrom*$resultsperpage) and ($total<($startfrom+1)*$resultsperpage));
134 my $strsth="Select distinct authtypecode from marc_subfield_structure where (";
135 foreach my $listtags (@tags){
136 my @taglist=split /,/,$listtags;
137 foreach my $curtag (@taglist){
139 $strsth.="(tagfield='".substr($curtag,1,3)."' AND tagsubfield='".substr($curtag,4,1)."') OR";
144 my $strsth = $strsth." and authtypecode is not NULL";
146 my $sth=$dbh->prepare($strsth);
150 # searching in authorities
154 while ((my $authtypecode) = $sth->fetchrow) {
155 my ($curauthresults,$nbresults) = authoritysearch($dbh,[''],[''],[''],['contains'],
156 \@search,$startfrom*$resultsperpage, $resultsperpage,$authtypecode);
157 if (defined(@$curauthresults)) {
158 for (my $i = 0; $i < @$curauthresults ;$i++) {
159 @$curauthresults[$i]->{jamainentry} = @$curauthresults[$i]->{mainentry};
160 @$curauthresults[$i]->{jamainentry} =~ s/'/\\'/g;
163 push @authresults, @$curauthresults;
164 $authnbresults+=$nbresults;
165 # warn "auth : $authtypecode nbauthresults : $nbresults";
169 # OK, filling the template with authorities & biblio entries found.
171 ($template, $loggedinuser, $cookie)
172 = get_template_and_user({template_name => "search.marc/dictionary.tmpl",
175 authnotrequired => 0,
176 flagsrequired => {catalogue => 1},
180 # multi page display gestion
182 my $displayprev=$startfrom;
183 if(($total - (($startfrom+1)*($resultsperpage))) > 0 ) {
189 for(my $i = 0 ; $i <= $#tags ; $i++) {
190 push @field_data, { term => "marclist", val=>$tags[$i] };
191 push @field_data, { term => "and_or", val=>$and_or[$i] };
192 push @field_data, { term => "excluding", val=>$excluding[$i] };
193 push @field_data, { term => "operator", val=>$operator[$i] };
194 push @field_data, { term => "value", val=>$value[$i] };
199 if ($total>$resultsperpage) {
200 for (my $i=1; $i<$total/$resultsperpage+1; $i++) {
203 ($startfrom==($i-1)) && ($highlight=1);
204 push @numbers, { number => $i,
205 highlight => $highlight ,
206 searchdata=> \@field_data,
207 startfrom => ($i-1)};
212 my $from = $startfrom*$resultsperpage+1;
215 if($total < (($startfrom+1)*$resultsperpage))
219 $to = (($startfrom+1)*$resultsperpage);
221 $template->param(anindex => $input->param('index'));
222 $template->param(result => \@results,
223 catresult=> \@catresults,
224 search => $search[0],
226 authresult => \@authresults,
227 nbresults => $authnbresults,
228 startfrom=> $startfrom,
229 displaynext=> $displaynext,
230 displayprev=> $displayprev,
231 resultsperpage => $resultsperpage,
232 startfromnext => $startfrom+1,
233 startfromprev => $startfrom-1,
234 searchdata=>\@field_data,
239 MARC_ON => C4::Context->preference("marc"),
243 ($template, $loggedinuser, $cookie)
244 = get_template_and_user({template_name => "search.marc/dictionary.tmpl",
247 authnotrequired => 0,
248 flagsrequired => {catalogue => 1},
251 #warn "type : $type";
254 $template->param(search => $search[0],
257 anindex => $input->param('index'));
260 output_html_with_http_headers $input, $cookie, $template->output;