fix date formatting in overdue_notices.pl
[koha.git] / catalogue / dictionary.pl
blob6a767f39020e6ebb93324127afd7697ad95bc176
1 #!/usr/bin/perl
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
11 # version.
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
21 use strict;
22 use C4::Output;
23 use C4::Auth;
24 use CGI;
25 use C4::Search;
26 use C4::AuthoritiesMarc;
27 use C4::Context;
28 use C4::Biblio;
31 =head1 NAME
33 dictionnary.pl : script to search in biblio & authority an existing value
35 =head1 SYNOPSIS
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
39 The parameter "marclist" tells which field is searched (title, author, subject, but could be anything else)
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.
45 The script shows all results & the user can choose what he want, that is copied into search form.
47 =cut
49 my $input = new CGI;
50 my $field =$input->param('marclist');
51 #warn "field :$field";
52 my ($tablename, $kohafield)=split /./,$field;
53 #my $tablename=$input->param('tablename');
54 $tablename="biblio" unless ($tablename);
55 #my $kohafield = $input->param('kohafield');
56 my @search = $input->param('search');
57 # warn " ".$search[0];
58 my $index = $input->param('index');
59 # warn " index: ".$index;
60 my $op=$input->param('op');
61 if (($search[0]) and not ($op eq 'do_search')){
62 $op='do_search';
64 my $script_name = 'catalogue/dictionary.pl';
65 my $query;
66 my $type=$input->param('type');
67 #warn " ".$type;
69 my $dbh = C4::Context->dbh;
70 my ($template, $loggedinuser, $cookie);
72 my $startfrom=$input->param('startfrom');
73 $startfrom=0 if(!defined $startfrom);
74 my $searchdesc;
75 my $resultsperpage;
77 #warn "Starting process";
79 if ($op eq "do_search") {
81 # searching in biblio
83 my $sth=$dbh->prepare("Select distinct tagfield,tagsubfield from marc_subfield_structure where kohafield = ?");
84 $sth->execute("$field");
85 my (@tags, @and_or, @operator, @excluding,@value);
87 while ((my $tagfield,my $tagsubfield,my $liblibrarian) = $sth->fetchrow) {
88 push @tags, $dbh->quote("$tagfield$tagsubfield");
91 $resultsperpage= $input->param('resultsperpage');
92 $resultsperpage = 19 if(!defined $resultsperpage);
93 my $orderby = $input->param('orderby');
95 findseealso($dbh,\@tags);
97 my @results, my $total;
98 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 ";
99 my $listtags="(";
100 foreach my $tag (@tags){
101 $listtags .= $tag .",";
103 $listtags =~s/,$/)/;
104 $strsth .= $listtags." and marc_word.tagsubfield=concat(marc_subfield_table.tag,marc_subfield_table.subfieldcode) group by subfieldvalue ";
105 # warn "search in biblio : ".$strsth;
106 my $value = uc($search[0]);
107 $value=~s/\*/%/g;
108 $value.= "%" if not($value=~m/%/);
109 # warn " texte : ".$value;
111 $sth=$dbh->prepare($strsth);
112 $sth->execute($value);
113 $total=0;
114 my @catresults;
115 my $javalue;
116 while (my ($value,$ctresults)=$sth->fetchrow) {
117 # This $javalue is used for the javascript selectentry function (javalue for javascript value !)
118 $javalue = $value;
119 $javalue =~s/'/\\'/g;
121 push @catresults,{value=> $value,
122 javalue=> $javalue,
123 even=>($total-$startfrom*$resultsperpage)%2,
124 count=>$ctresults
125 } if (($total>=$startfrom*$resultsperpage) and ($total<($startfrom+1)*$resultsperpage));
126 $total++;
130 $strsth="Select distinct authtypecode from marc_subfield_structure where (";
131 foreach my $listtags (@tags){
132 my @taglist=split /,/,$listtags;
133 foreach my $curtag (@taglist){
134 $curtag =~s/\s+//;
135 $strsth.="(tagfield='".substr($curtag,1,3)."' AND tagsubfield='".substr($curtag,4,1)."') OR";
139 $strsth=~s/ OR$/)/;
140 $strsth .= " and authtypecode is not NULL";
141 # warn $strsth;
142 $sth=$dbh->prepare($strsth);
143 $sth->execute;
146 # searching in authorities
148 my @authresults;
149 my $authnbresults;
150 while ((my $authtypecode) = $sth->fetchrow) {
151 my ($curauthresults,$nbresults) = SearchAuthorities([''],[''],[''],['contains'],
152 \@search,$startfrom*$resultsperpage, $resultsperpage,$authtypecode);
153 if (defined(@$curauthresults)) {
154 for (my $i = 0; $i < @$curauthresults ;$i++) {
155 @$curauthresults[$i]->{jamainentry} = @$curauthresults[$i]->{mainentry};
156 @$curauthresults[$i]->{jamainentry} =~ s/'/\\'/g;
159 push @authresults, @$curauthresults;
160 $authnbresults+=$nbresults;
161 # warn "auth : $authtypecode nbauthresults : $nbresults";
165 # OK, filling the template with authorities & biblio entries found.
167 ($template, $loggedinuser, $cookie)
168 = get_template_and_user({template_name => "catalogue/dictionary.tmpl",
169 query => $input,
170 type => $type,
171 authnotrequired => 0,
172 flagsrequired => {catalogue => 1},
173 debug => 1,
176 # multi page display gestion
177 my $displaynext=0;
178 my $displayprev=$startfrom;
179 if(($total - (($startfrom+1)*($resultsperpage))) > 0 ) {
180 $displaynext = 1;
183 my @field_data = ();
185 for(my $i = 0 ; $i <= $#tags ; $i++) {
186 push @field_data, { term => "marclist", val=>$tags[$i] };
187 push @field_data, { term => "and_or", val=>$and_or[$i] };
188 push @field_data, { term => "excluding", val=>$excluding[$i] };
189 push @field_data, { term => "operator", val=>$operator[$i] };
190 push @field_data, { term => "value", val=>$value[$i] };
193 my @numbers = ();
195 if ($total>$resultsperpage) {
196 for (my $i=1; $i<$total/$resultsperpage+1; $i++) {
197 if ($i<16) {
198 my $highlight=0;
199 ($startfrom==($i-1)) && ($highlight=1);
200 push @numbers, { number => $i,
201 highlight => $highlight ,
202 searchdata=> \@field_data,
203 startfrom => ($i-1)};
208 my $from = $startfrom*$resultsperpage+1;
209 my $to;
211 if($total < (($startfrom+1)*$resultsperpage))
213 $to = $total;
214 } else {
215 $to = (($startfrom+1)*$resultsperpage);
217 $template->param(anindex => $input->param('index'));
218 $template->param(result => \@results,
219 catresult=> \@catresults,
220 search => $search[0],
221 marclist =>$field,
222 authresult => \@authresults,
223 nbresults => $authnbresults,
224 startfrom=> $startfrom,
225 displaynext=> $displaynext,
226 displayprev=> $displayprev,
227 resultsperpage => $resultsperpage,
228 startfromnext => $startfrom+1,
229 startfromprev => $startfrom-1,
230 searchdata=>\@field_data,
231 total=>$total,
232 from=>$from,
233 to=>$to,
234 numbers=>\@numbers,
235 MARC_ON => C4::Context->preference("marc"),
238 } else {
239 ($template, $loggedinuser, $cookie)
240 = get_template_and_user({template_name => "catalogue/dictionary.tmpl",
241 query => $input,
242 type => $type,
243 authnotrequired => 0,
244 flagsrequired => {catalogue => 1},
245 debug => 1,
247 #warn "type : $type";
250 $template->param(search => $search[0],
251 marclist =>$field,
252 type=>$type,
253 anindex => $input->param('index'),
256 # Print the page
257 output_html_with_http_headers $input, $cookie, $template->output;
259 # Local Variables:
260 # tab-width: 4
261 # End: