Plugin for field 123i
[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::SimpleMarc;
32 use C4::SearchMarc;
33 use C4::Acquisition;
34 use C4::Koha;
35 use MARC::Record;
36
37 sub plugin_parameters {
38 my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
39 return "";
40 }
41
42 sub plugin_javascript {
43 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
44 my $function_name= "4XX".(int(rand(100000))+1);
45 my $res="
46 <script>
47 function Focus$function_name(subfield_managed) {
48 return 1;
49 }
50
51 function Blur$function_name(subfield_managed) {
52         return 1;
53 }
54
55 function Clic$function_name(i) {
56         defaultvalue=document.f.field_value[i].value;
57         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');
58
59 }
60 </script>
61 ";
62
63 return ($function_name,$res);
64 }
65
66 sub plugin {
67         my ($input) = @_;
68         my %env;
69         
70         
71         my $dbh=C4::Context->dbh;
72         my $query = new CGI;
73         my $op = $query->param('op');
74         my $type=$query->param('type');
75         warn "operation  ".$op;
76         my $startfrom=$query->param('startfrom');
77         $startfrom=0 if(!defined $startfrom);
78         my ($template, $loggedinuser, $cookie);
79         my $resultsperpage;
80         my $searchdesc;
81         
82         if ($op eq "fillinput"){
83                 my $bibnum = $query->param('bibnum');
84                 my $index = $query->param('index');
85                 my $marcrecord;
86 # open template
87                 ($template, $loggedinuser, $cookie)= get_template_and_user({template_name => "value_builder/unimarc_field_4XX.tmpl",
88                              query => $query,
89                              type => "intranet",
90                              authnotrequired => 0,
91                              flagsrequired => {catalogue => 1},
92                              debug => 1,
93                             });
94                 #get bibid
95                 my $bibid;
96                 my $req= $dbh->prepare("SELECT distinctrow bibid,biblionumber FROM `marc_biblio` WHERE biblionumber= ?");
97                 $req->execute($bibnum);
98                 ($bibid,$bibnum) = $req->fetchrow;
99                 #warn "bibid :".$bibid;
100                 #get marc record
101                 $marcrecord = MARCgetbiblio($dbh,$bibid);
102 #               warn "record : ".$marcrecord->as_formatted;
103                 
104                 my $subfield_value_9=$bibid;
105                 my $subfield_value_0;
106                 $subfield_value_0=$marcrecord->field('001')->data if $marcrecord->field('001');
107                 my $subfield_value_a;
108                 if ($marcrecord->field('200')){
109                         $subfield_value_a=$marcrecord->field('200')->subfield("f");
110                 } elsif ($marcrecord->field('700')){
111                         $subfield_value_a=$marcrecord->field('700')->subfield("a");
112                 } elsif ($marcrecord->field('701')){
113                         $subfield_value_a=$marcrecord->field('701')->subfield("a");
114                 }
115                 my $subfield_value_c = $marcrecord->field('210')->subfield("a") if ($marcrecord->field('210'));
116                 my $subfield_value_d = $marcrecord->field('210')->subfield("d") if ($marcrecord->field('210'));
117                 
118                 my $subfield_value_e= $marcrecord->field('205')->subfield("a") if ($marcrecord->field('205'));
119                 
120                 my $subfield_value_h; 
121                 if (($marcrecord->field('200')) && ($marcrecord->field('200')->subfield("h"))){
122                         $subfield_value_h = $marcrecord->field('200')->subfield("h") ;
123                 } elsif (($marcrecord->field('225')) && ($marcrecord->field('225')->subfield("h"))) {
124                         $subfield_value_h = $marcrecord->field('225')->subfield("h") ;
125                 } elsif (($marcrecord->field('500')) && ($marcrecord->field('500')->subfield("h"))) {
126                         $subfield_value_h = $marcrecord->field('500')->subfield("h") ;
127                 }
128                 
129                 my $subfield_value_i;
130                 if (($marcrecord->field('200')) && ($marcrecord->field('200')->subfield("i"))){
131                         $subfield_value_i = $marcrecord->field('200')->subfield("i") ;
132                 } elsif (($marcrecord->field('225')) && ($marcrecord->field('225')->subfield("i"))) {
133                         $subfield_value_i = $marcrecord->field('225')->subfield("i") ;
134                 } elsif (($marcrecord->field('500')) && ($marcrecord->field('500')->subfield("i"))) {
135                         $subfield_value_i = $marcrecord->field('500')->subfield("i") ;
136                 }
137
138                 my $subfield_value_p = $marcrecord->field('215')->subfield("a") if ($marcrecord->field('215'));
139                 
140                 my $subfield_value_t;
141                 if (($marcrecord->field('200')) && ($marcrecord->field('200')->subfield("a"))){
142                         $subfield_value_t = $marcrecord->field('200')->subfield("a") ;
143                 } elsif (($marcrecord->field('225')) && ($marcrecord->field('225')->subfield("a"))) {
144                         $subfield_value_t = $marcrecord->field('225')->subfield("a") ;
145                 } elsif (($marcrecord->field('500')) && ($marcrecord->field('500')->subfield("a"))) {
146                         $subfield_value_t = $marcrecord->field('500')->subfield("a") ;
147                 }
148                 
149                 my $subfield_value_u = $marcrecord->field('856')->subfield("u") if ($marcrecord->field('856'));
150                 
151                 my $subfield_value_v;
152                 if (($marcrecord->field('225')) && ($marcrecord->field('225')->subfield("v"))){
153                         $subfield_value_v = $marcrecord->field('225')->subfield("v") ;
154                 } elsif (($marcrecord->field('200')) && ($marcrecord->field('200')->subfield("h"))) {
155                         $subfield_value_v = $marcrecord->field('200')->subfield("h") ;
156                 }
157                 my $subfield_value_x = $marcrecord->field('011')->subfield("a") if ($marcrecord->field('011') and not (($marcrecord->field('011')->subfield("y")) or ($marcrecord->field('011')->subfield("z"))));
158                 my $subfield_value_y = $marcrecord->field('013')->subfield("a") if ($marcrecord->field('013'));
159                 if ($marcrecord->field('010')){
160                         $subfield_value_y = $marcrecord->field('010')->subfield("a");
161                 }
162                 $template->param(fillinput => 1,
163                                                 index => $query->param('index')."",
164                                                 bibid=>$bibid?$bibid:"",
165                                                 subfield_value_9=>$subfield_value_9,
166                                                 subfield_value_0=>$subfield_value_0,
167                                                 subfield_value_a=>$subfield_value_a,
168                                                 subfield_value_c=>$subfield_value_c,
169                                                 subfield_value_d=>$subfield_value_d,
170                                                 subfield_value_e=>$subfield_value_e,
171                                                 subfield_value_h=>$subfield_value_h,
172                                                 subfield_value_i=>$subfield_value_i,
173                                                 subfield_value_p=>$subfield_value_p,
174                                                 subfield_value_t=>$subfield_value_t,
175                                                 subfield_value_u=>$subfield_value_u,
176                                                 subfield_value_v=>$subfield_value_v,
177                                                 subfield_value_x=>$subfield_value_x,
178                                                 subfield_value_y=>$subfield_value_y,
179                                                 );
180 ############################################################### 
181         }elsif ($op eq "do_search") {
182                 my @marclist = $query->param('marclist');
183                 my @and_or = $query->param('and_or');
184                 my @excluding = $query->param('excluding');
185                 my @operator = $query->param('operator');
186                 my @value = $query->param('value');
187         
188                 for (my $i=0;$i<=$#marclist;$i++) {
189                         if ($searchdesc) { # don't put the and_or on the 1st search term
190                                 $searchdesc .= $and_or[$i]." ".$excluding[$i]." ".($marclist[$i]?$marclist[$i]:"*")." ".$operator[$i]." ".$value[$i]." " if ($value[$i]);
191                         } else {
192                                 $searchdesc = $excluding[$i]." ".($marclist[$i]?$marclist[$i]:"*")." ".$operator[$i]." ".$value[$i]." " if ($value[$i]);
193                         }
194                 }
195                 $resultsperpage= $query->param('resultsperpage');
196                 $resultsperpage = 19 if(!defined $resultsperpage);
197                 my $orderby = $query->param('orderby');
198                 my $desc_or_asc = $query->param('desc_or_asc');
199         
200                 # builds tag and subfield arrays
201                 my @tags;
202                 foreach my $marc (@marclist) {
203                         if ($marc) {
204                                 my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,$marc,'');
205                                 if ($tag) {
206                                         push @tags,$dbh->quote("$tag$subfield");
207                                 } else {
208                                         push @tags, $dbh->quote(substr($marc,0,4));
209                                 }
210                         } else {
211                                 push @tags, "";
212                         }
213                 }
214                 
215                 my ($results,$total) = catalogsearch($dbh, \@tags,\@and_or,
216                                                                                         \@excluding, \@operator, \@value,
217                                                                                         $startfrom*$resultsperpage, $resultsperpage,$orderby, $desc_or_asc);
218 #               warn " biblio count : ".$total;
219                 
220                 ($template, $loggedinuser, $cookie)
221                         = get_template_and_user({template_name => "value_builder/unimarc_field_4XX.tmpl",
222                                         query => $query,
223                                         type => 'intranet',
224                                         authnotrequired => 1,
225                                         debug => 1,
226                                         });
227         
228                 # multi page display gestion
229                 my $displaynext=0;
230                 my $displayprev=$startfrom;
231                 if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
232                         $displaynext = 1;
233                 }
234         
235                 my @field_data = ();
236         
237         
238                 for(my $i = 0 ; $i <= $#marclist ; $i++)
239                 {
240                         push @field_data, { term => "marclist", val=>$marclist[$i] };
241                         push @field_data, { term => "and_or", val=>$and_or[$i] };
242                         push @field_data, { term => "excluding", val=>$excluding[$i] };
243                         push @field_data, { term => "operator", val=>$operator[$i] };
244                         push @field_data, { term => "value", val=>$value[$i] };
245                 }
246         
247                 my @numbers = ();
248         
249                 if ($total>$resultsperpage)
250                 {
251                         for (my $i=1; $i<$total/$resultsperpage+1; $i++)
252                         {
253                                 if ($i<16)
254                                 {
255                                         my $highlight=0;
256                                         ($startfrom==($i-1)) && ($highlight=1);
257                                         push @numbers, { number => $i,
258                                                 highlight => $highlight ,
259                                                 searchdata=> \@field_data,
260                                                 startfrom => ($i-1)};
261                                 }
262                         }
263                 }
264         
265                 my $from = $startfrom*$resultsperpage+1;
266                 my $to;
267         
268                 if($total < (($startfrom+1)*$resultsperpage))
269                 {
270                         $to = $total;
271                 } else {
272                         $to = (($startfrom+1)*$resultsperpage);
273                 }
274                 my $defaultview = 'BiblioDefaultView'.C4::Context->preference('BiblioDefaultView');
275                 $template->param(result => $results,
276                                                 index => $query->param('index')."",
277                                                                 startfrom=> $startfrom,
278                                                                 displaynext=> $displaynext,
279                                                                 displayprev=> $displayprev,
280                                                                 resultsperpage => $resultsperpage,
281                                                                 orderby => $orderby,
282                                                                 startfromnext => $startfrom+1,
283                                                                 startfromprev => $startfrom-1,
284                                                                 searchdata=>\@field_data,
285                                                                 total=>$total,
286                                                                 from=>$from,
287                                                                 to=>$to,
288                                                                 numbers=>\@numbers,
289                                                                 searchdesc=> $searchdesc,
290                                                                 $defaultview => 1,
291                                                                 Search =>0
292                                                                 );
293         
294         } else {
295                 ($template, $loggedinuser, $cookie)
296                         = get_template_and_user({template_name => "value_builder/unimarc_field_4XX.tmpl",
297                                                 query => $query,
298                                                 type => "intranet",
299                                                 authnotrequired => 1,
300                                         });
301                 
302                 
303                 my $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
304                 $sth->execute;
305                 my  @itemtype;
306                 my %itemtypes;
307                 push @itemtype, "";
308                 $itemtypes{''} = "";
309                 while (my ($value,$lib) = $sth->fetchrow_array) {
310                         push @itemtype, $value;
311                         $itemtypes{$value}=$lib;
312                 }
313                 
314                 my $CGIitemtype=CGI::scrolling_list( -name     => 'value',
315                                         -values   => \@itemtype,
316                                         -labels   => \%itemtypes,
317                                         -size     => 1,
318                                         -multiple => 0 );
319                 $sth->finish;
320                 
321                 my @branchloop;
322                 my @select_branch;
323                 my %select_branches;
324                 my $branches=getbranches;
325                 push @select_branch, "";
326                 $select_branches{''} = "";
327                 foreach my $thisbranch (keys %$branches){
328                         push @select_branch, $branches->{$thisbranch}->{'branchcode'};
329                         $select_branches{$branches->{$thisbranch}->{'branchcode'}} = $branches->{$thisbranch}->{'branchname'};
330                 }
331                 my $CGIbranch=CGI::scrolling_list( -name     => 'value',
332                                         -values   => \@select_branch,
333                                         -labels   => \%select_branches,
334                                         -size     => 1,
335                                         -multiple => 0 );
336                 $sth->finish;
337                 
338                 my $req = $dbh->prepare("select distinctrow left(publishercode,45) from biblioitems order by publishercode");
339                 $req->execute;
340                 my @select;
341                 push @select,"";
342                 while (my ($value) =$req->fetchrow) {
343                         push @select, $value;
344                 }
345                 my $CGIpublisher=CGI::scrolling_list( -name     => 'value',
346                                         -id => 'publisher',
347                                         -values   => \@select,
348                                         -size     => 1,
349                                         -multiple => 0 );
350                 
351 #               my $sth=$dbh->prepare("select description,itemtype from itemtypes order by description");
352 #               $sth->execute;
353 #               while (my ($description,$itemtype) = $sth->fetchrow) {
354 #                       $classlist.="<option value=\"$itemtype\">$description</option>\n";
355 #               }
356 #               $sth->finish;
357                                 
358                 $template->param(#classlist => $classlist,
359                                                 CGIitemtype => $CGIitemtype,
360                                                 CGIbranch => $CGIbranch,
361                                                 CGIPublisher => $CGIpublisher,
362                                                 index=>$query->param('index'),
363                                                 Search =>1,
364                 );
365         }       
366         output_html_with_http_headers $query, $cookie, $template->output ;
367 }
368
369 1;