Adding plugin for Unimarc_field-4XX.pl
[koha.git] / value_builder / unimarc_field_4XX.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 require Exporter;
22 use CGI;
23 use HTML::Template;
24 use C4::Interface::CGI::Output;
25 use C4::Context;
26 use C4::Search;
27 use C4::Auth;
28 use C4::Output;
29 use C4::Database;
30 use C4::Biblio;
31 use C4::SearchMarc;
32 use C4::Acquisition;
33 use C4::Koha;
34
35 sub plugin_parameters {
36 my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
37 return "";
38 }
39
40 sub plugin_javascript {
41 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
42 my $function_name= "4XX".(int(rand(100000))+1);
43 my $res="
44 <script>
45 function Focus$function_name(subfield_managed) {
46 return 1;
47 }
48
49 function Blur$function_name(subfield_managed) {
50         return 1;
51 }
52
53 function Clic$function_name(i) {
54         defaultvalue=document.f.field_value[i].value;
55         newin=window.open(\"../plugin_launcher.pl?plugin_name=unimarc_field_4XX.pl&index=\"+i+\"&result=\"+defaultvalue,\"unimarc field 4\"+i+\"\",'width=700,height=700,toolbar=false,scrollbars=yes');
56
57 }
58 </script>
59 ";
60
61 return ($function_name,$res);
62 }
63
64 sub plugin {
65         my ($input) = @_;
66         my %env;
67         
68         
69         my $dbh=C4::Context->dbh;
70         my $query = new CGI;
71         my $op = $query->param('op');
72         my $type=$query->param('type');
73         warn "operation  ".$op;
74         my $startfrom=$query->param('startfrom');
75         $startfrom=0 if(!defined $startfrom);
76         my ($template, $loggedinuser, $cookie);
77         my $resultsperpage;
78         my $searchdesc;
79         
80         if ($op eq "do_search") {
81                 my @marclist = $query->param('marclist');
82                 my @and_or = $query->param('and_or');
83                 my @excluding = $query->param('excluding');
84                 my @operator = $query->param('operator');
85                 my @value = $query->param('value');
86         
87                 for (my $i=0;$i<=$#marclist;$i++) {
88                         if ($searchdesc) { # don't put the and_or on the 1st search term
89                                 $searchdesc .= $and_or[$i]." ".$excluding[$i]." ".($marclist[$i]?$marclist[$i]:"*")." ".$operator[$i]." ".$value[$i]." " if ($value[$i]);
90                         } else {
91                                 $searchdesc = $excluding[$i]." ".($marclist[$i]?$marclist[$i]:"*")." ".$operator[$i]." ".$value[$i]." " if ($value[$i]);
92                         }
93                 }
94                 $resultsperpage= $query->param('resultsperpage');
95                 $resultsperpage = 19 if(!defined $resultsperpage);
96                 my $orderby = $query->param('orderby');
97                 my $desc_or_asc = $query->param('desc_or_asc');
98         
99                 # builds tag and subfield arrays
100                 my @tags;
101                 foreach my $marc (@marclist) {
102                         if ($marc) {
103                                 my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,$marc,'');
104                                 if ($tag) {
105                                         push @tags,$dbh->quote("$tag$subfield");
106                                 } else {
107                                         push @tags, $dbh->quote(substr($marc,0,4));
108                                 }
109                         } else {
110                                 push @tags, "";
111                         }
112                 }
113                 
114                 my ($results,$total) = catalogsearch($dbh, \@tags,\@and_or,
115                                                                                         \@excluding, \@operator, \@value,
116                                                                                         $startfrom*$resultsperpage, $resultsperpage,$orderby, $desc_or_asc);
117                 warn " biblio count : ".$total;
118                 warn " biblio count : ".$total;
119                 
120                 ($template, $loggedinuser, $cookie)
121                         = get_template_and_user({template_name => "value_builder/unimarc_field_4XX.tmpl",
122                                         query => $query,
123                                         type => 'intranet',
124                                         authnotrequired => 1,
125                                         debug => 1,
126                                         });
127         
128                 # multi page display gestion
129                 my $displaynext=0;
130                 my $displayprev=$startfrom;
131                 if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
132                         $displaynext = 1;
133                 }
134         
135                 my @field_data = ();
136         
137         
138                 for(my $i = 0 ; $i <= $#marclist ; $i++)
139                 {
140                         push @field_data, { term => "marclist", val=>$marclist[$i] };
141                         push @field_data, { term => "and_or", val=>$and_or[$i] };
142                         push @field_data, { term => "excluding", val=>$excluding[$i] };
143                         push @field_data, { term => "operator", val=>$operator[$i] };
144                         push @field_data, { term => "value", val=>$value[$i] };
145                 }
146         
147                 my @numbers = ();
148         
149                 if ($total>$resultsperpage)
150                 {
151                         for (my $i=1; $i<$total/$resultsperpage+1; $i++)
152                         {
153                                 if ($i<16)
154                                 {
155                                         my $highlight=0;
156                                         ($startfrom==($i-1)) && ($highlight=1);
157                                         push @numbers, { number => $i,
158                                                 highlight => $highlight ,
159                                                 searchdata=> \@field_data,
160                                                 startfrom => ($i-1)};
161                                 }
162                         }
163                 }
164         
165                 my $from = $startfrom*$resultsperpage+1;
166                 my $to;
167         
168                 if($total < (($startfrom+1)*$resultsperpage))
169                 {
170                         $to = $total;
171                 } else {
172                         $to = (($startfrom+1)*$resultsperpage);
173                 }
174                 my $defaultview = 'BiblioDefaultView'.C4::Context->preference('BiblioDefaultView');
175                 $template->param(result => $results,
176                                                 index => $query->param('index')."",
177                                                                 startfrom=> $startfrom,
178                                                                 displaynext=> $displaynext,
179                                                                 displayprev=> $displayprev,
180                                                                 resultsperpage => $resultsperpage,
181                                                                 orderby => $orderby,
182                                                                 startfromnext => $startfrom+1,
183                                                                 startfromprev => $startfrom-1,
184                                                                 searchdata=>\@field_data,
185                                                                 total=>$total,
186                                                                 from=>$from,
187                                                                 to=>$to,
188                                                                 numbers=>\@numbers,
189                                                                 searchdesc=> $searchdesc,
190                                                                 $defaultview => 1,
191                                                                 Search =>0
192                                                                 );
193         
194         } else {
195                 ($template, $loggedinuser, $cookie)
196                         = get_template_and_user({template_name => "value_builder/unimarc_field_4XX.tmpl",
197                                                 query => $query,
198                                                 type => "intranet",
199                                                 authnotrequired => 1,
200                                         });
201                 
202                 
203                 my $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
204                 $sth->execute;
205                 my  @itemtype;
206                 my %itemtypes;
207                 push @itemtype, "";
208                 $itemtypes{''} = "";
209                 while (my ($value,$lib) = $sth->fetchrow_array) {
210                         push @itemtype, $value;
211                         $itemtypes{$value}=$lib;
212                 }
213                 
214                 my $CGIitemtype=CGI::scrolling_list( -name     => 'value',
215                                         -values   => \@itemtype,
216                                         -labels   => \%itemtypes,
217                                         -size     => 1,
218                                         -multiple => 0 );
219                 $sth->finish;
220                 
221                 my @branchloop;
222                 my @select_branch;
223                 my %select_branches;
224                 my $branches=getbranches;
225                 push @select_branch, "";
226                 $select_branches{''} = "";
227                 foreach my $thisbranch (keys %$branches){
228                         push @select_branch, $branches->{$thisbranch}->{'branchcode'};
229                         $select_branches{$branches->{$thisbranch}->{'branchcode'}} = $branches->{$thisbranch}->{'branchname'};
230                 }
231                 my $CGIbranch=CGI::scrolling_list( -name     => 'value',
232                                         -values   => \@select_branch,
233                                         -labels   => \%select_branches,
234                                         -size     => 1,
235                                         -multiple => 0 );
236                 $sth->finish;
237                 
238                 my $req = $dbh->prepare("select distinctrow left(publishercode,45) from biblioitems order by publishercode");
239                 $req->execute;
240                 my @select;
241                 push @select,"";
242                 while (my ($value) =$req->fetchrow) {
243                         push @select, $value;
244                 }
245                 my $CGIpublisher=CGI::scrolling_list( -name     => 'value',
246                                         -id => 'publisher',
247                                         -values   => \@select,
248                                         -size     => 1,
249                                         -multiple => 0 );
250                 
251 #               my $sth=$dbh->prepare("select description,itemtype from itemtypes order by description");
252 #               $sth->execute;
253 #               while (my ($description,$itemtype) = $sth->fetchrow) {
254 #                       $classlist.="<option value=\"$itemtype\">$description</option>\n";
255 #               }
256 #               $sth->finish;
257                                 
258                 $template->param(#classlist => $classlist,
259                                                 CGIitemtype => $CGIitemtype,
260                                                 CGIbranch => $CGIbranch,
261                                                 CGIPublisher => $CGIpublisher,
262                                                 Search =>1,
263                 );
264         }       
265         output_html_with_http_headers $query, $cookie, $template->output ;
266 }
267
268 1;