Fixing process for $0 subfield
[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 #               my @loop_data =();
104 #               my $tag;
105 #               my @loop_data =();
106 #               my @subfields_data;
107                 
108                 my $subfield_value_9=$bibid;
109                 my $subfield_value_0=$marcrecord->field('001')->data;
110                 my $subfield_value_a;
111                 if ($marcrecord->field('200')){
112                         $subfield_value_a=$marcrecord->field('200')->subfield("f");
113                 } elsif ($marcrecord->field('700')){
114                         $subfield_value_a=$marcrecord->field('700')->subfield("a");
115                 } elsif ($marcrecord->field('701')){
116                         $subfield_value_a=$marcrecord->field('701')->subfield("a");
117                 }
118                 my $subfield_value_c = $marcrecord->field('210')->subfield("d") if ($marcrecord->field('210'));
119                 my $subfield_value_d;
120                 if ($marcrecord->field('100')){
121                         my $publicationdate;
122                         warn "date de publication 1".substr($marcrecord->field('100')->subfield("a"),9,4)."   date de publication 2 ".substr($marcrecord->field('100')->subfield("a"),12,4);
123                         $publicationdate = substr($marcrecord->field('100')->subfield("a"),9,4);
124                         if (substr($marcrecord->field('100')->subfield("a"),12,4)>$publicationdate){
125                         $publicationdate=substr($marcrecord->field('100')->subfield("a"),12,4);
126                         }
127                         $subfield_value_d=$publicationdate;
128                 }
129                 my $subfield_value_e= $marcrecord->field('205')->subfield("a") if ($marcrecord->field('205'));
130                 
131                 my $subfield_value_h; 
132                 if (($marcrecord->field('200')) && ($marcrecord->field('200')->subfield("h"))){
133                         $subfield_value_h = $marcrecord->field('200')->subfield("h") ;
134                 } elsif (($marcrecord->field('225')) && ($marcrecord->field('225')->subfield("h"))) {
135                         $subfield_value_h = $marcrecord->field('225')->subfield("h") ;
136                 } elsif (($marcrecord->field('500')) && ($marcrecord->field('500')->subfield("h"))) {
137                         $subfield_value_h = $marcrecord->field('500')->subfield("h") ;
138                 }
139                 
140                 my $subfield_value_i;
141                 if (($marcrecord->field('200')) && ($marcrecord->field('200')->subfield("i"))){
142                         $subfield_value_i = $marcrecord->field('200')->subfield("i") ;
143                 } elsif (($marcrecord->field('225')) && ($marcrecord->field('225')->subfield("i"))) {
144                         $subfield_value_i = $marcrecord->field('225')->subfield("i") ;
145                 } elsif (($marcrecord->field('500')) && ($marcrecord->field('500')->subfield("i"))) {
146                         $subfield_value_i = $marcrecord->field('500')->subfield("i") ;
147                 }
148
149                 my $subfield_value_p = $marcrecord->field('215')->subfield("a") if ($marcrecord->field('215'));
150                 
151                 my $subfield_value_t;
152                 if (($marcrecord->field('200')) && ($marcrecord->field('200')->subfield("a"))){
153                         $subfield_value_t = $marcrecord->field('200')->subfield("a") ;
154                 } elsif (($marcrecord->field('225')) && ($marcrecord->field('225')->subfield("a"))) {
155                         $subfield_value_t = $marcrecord->field('225')->subfield("a") ;
156                 } elsif (($marcrecord->field('500')) && ($marcrecord->field('500')->subfield("a"))) {
157                         $subfield_value_t = $marcrecord->field('500')->subfield("a") ;
158                 }
159                 
160                 my $subfield_value_u = $marcrecord->field('856')->subfield("p") if ($marcrecord->field('856'));
161                 
162                 my $subfield_value_v;
163                 if (($marcrecord->field('225')) && ($marcrecord->field('225')->subfield("v"))){
164                         $subfield_value_v = $marcrecord->field('225')->subfield("v") ;
165                 } elsif (($marcrecord->field('200')) && ($marcrecord->field('200')->subfield("h"))) {
166                         $subfield_value_v = $marcrecord->field('200')->subfield("h") ;
167                 }
168                 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"))));
169                 my $subfield_value_y = $marcrecord->field('013')->subfield("a") if ($marcrecord->field('013'));
170                 if ($marcrecord->field('010')){
171                         my $subfield_value_y = $marcrecord->field('010')->subfield("a");
172                 }
173 #               my @subf;
174 #               #=(a,c,d,e,h,i,p,t,u,v,x,y,0,9);
175 #                       # loop through each subfield
176 #               for my $i (0..$#subf) {
177 #                       $subf[$i][0] = "@" unless $subf[$i][0];
178 #                       my %subfield_data;
179 #                       $subfield_data{marc_value}=$subf[$i][1];
180 #                       $subfield_data{marc_subfield}=$subf[$i][0];
181 #                       $subfield_data{marc_tag}="";#$field->tag();
182 #                       push(@subfields_data, \%subfield_data);
183 #               }
184 #               if ($#subfields_data>=0) {
185 #                       my %tag_data;
186 #                       $tag_data{tag}="";#$field->tag().' -'. "";
187 #                       $tag_data{subfield} = \@subfields_data;
188 #                       push (@loop_data, \%tag_data);
189 #               }
190 #               $template->param("0XX" =>\@loop_data);
191                 $template->param(fillinput => 1,
192                                                 index => $query->param('index')."",
193                                                 bibid=>$bibid?$bibid:"",
194                                                 subfield_value_9=>$subfield_value_9,
195                                                 subfield_value_0=>$subfield_value_0,
196                                                 subfield_value_a=>$subfield_value_a,
197                                                 subfield_value_c=>$subfield_value_c,
198                                                 subfield_value_d=>$subfield_value_d,
199                                                 subfield_value_e=>$subfield_value_e,
200                                                 subfield_value_h=>$subfield_value_h,
201                                                 subfield_value_i=>$subfield_value_i,
202                                                 subfield_value_p=>$subfield_value_p,
203                                                 subfield_value_t=>$subfield_value_t,
204                                                 subfield_value_u=>$subfield_value_u,
205                                                 subfield_value_v=>$subfield_value_v,
206                                                 subfield_value_x=>$subfield_value_x,
207                                                 subfield_value_y=>$subfield_value_y,
208                                                 );
209 ############################################################### 
210         }elsif ($op eq "do_search") {
211                 my @marclist = $query->param('marclist');
212                 my @and_or = $query->param('and_or');
213                 my @excluding = $query->param('excluding');
214                 my @operator = $query->param('operator');
215                 my @value = $query->param('value');
216         
217                 for (my $i=0;$i<=$#marclist;$i++) {
218                         if ($searchdesc) { # don't put the and_or on the 1st search term
219                                 $searchdesc .= $and_or[$i]." ".$excluding[$i]." ".($marclist[$i]?$marclist[$i]:"*")." ".$operator[$i]." ".$value[$i]." " if ($value[$i]);
220                         } else {
221                                 $searchdesc = $excluding[$i]." ".($marclist[$i]?$marclist[$i]:"*")." ".$operator[$i]." ".$value[$i]." " if ($value[$i]);
222                         }
223                 }
224                 $resultsperpage= $query->param('resultsperpage');
225                 $resultsperpage = 19 if(!defined $resultsperpage);
226                 my $orderby = $query->param('orderby');
227                 my $desc_or_asc = $query->param('desc_or_asc');
228         
229                 # builds tag and subfield arrays
230                 my @tags;
231                 foreach my $marc (@marclist) {
232                         if ($marc) {
233                                 my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,$marc,'');
234                                 if ($tag) {
235                                         push @tags,$dbh->quote("$tag$subfield");
236                                 } else {
237                                         push @tags, $dbh->quote(substr($marc,0,4));
238                                 }
239                         } else {
240                                 push @tags, "";
241                         }
242                 }
243                 
244                 my ($results,$total) = catalogsearch($dbh, \@tags,\@and_or,
245                                                                                         \@excluding, \@operator, \@value,
246                                                                                         $startfrom*$resultsperpage, $resultsperpage,$orderby, $desc_or_asc);
247 #               warn " biblio count : ".$total;
248                 
249                 ($template, $loggedinuser, $cookie)
250                         = get_template_and_user({template_name => "value_builder/unimarc_field_4XX.tmpl",
251                                         query => $query,
252                                         type => 'intranet',
253                                         authnotrequired => 1,
254                                         debug => 1,
255                                         });
256         
257                 # multi page display gestion
258                 my $displaynext=0;
259                 my $displayprev=$startfrom;
260                 if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
261                         $displaynext = 1;
262                 }
263         
264                 my @field_data = ();
265         
266         
267                 for(my $i = 0 ; $i <= $#marclist ; $i++)
268                 {
269                         push @field_data, { term => "marclist", val=>$marclist[$i] };
270                         push @field_data, { term => "and_or", val=>$and_or[$i] };
271                         push @field_data, { term => "excluding", val=>$excluding[$i] };
272                         push @field_data, { term => "operator", val=>$operator[$i] };
273                         push @field_data, { term => "value", val=>$value[$i] };
274                 }
275         
276                 my @numbers = ();
277         
278                 if ($total>$resultsperpage)
279                 {
280                         for (my $i=1; $i<$total/$resultsperpage+1; $i++)
281                         {
282                                 if ($i<16)
283                                 {
284                                         my $highlight=0;
285                                         ($startfrom==($i-1)) && ($highlight=1);
286                                         push @numbers, { number => $i,
287                                                 highlight => $highlight ,
288                                                 searchdata=> \@field_data,
289                                                 startfrom => ($i-1)};
290                                 }
291                         }
292                 }
293         
294                 my $from = $startfrom*$resultsperpage+1;
295                 my $to;
296         
297                 if($total < (($startfrom+1)*$resultsperpage))
298                 {
299                         $to = $total;
300                 } else {
301                         $to = (($startfrom+1)*$resultsperpage);
302                 }
303                 my $defaultview = 'BiblioDefaultView'.C4::Context->preference('BiblioDefaultView');
304                 $template->param(result => $results,
305                                                 index => $query->param('index')."",
306                                                                 startfrom=> $startfrom,
307                                                                 displaynext=> $displaynext,
308                                                                 displayprev=> $displayprev,
309                                                                 resultsperpage => $resultsperpage,
310                                                                 orderby => $orderby,
311                                                                 startfromnext => $startfrom+1,
312                                                                 startfromprev => $startfrom-1,
313                                                                 searchdata=>\@field_data,
314                                                                 total=>$total,
315                                                                 from=>$from,
316                                                                 to=>$to,
317                                                                 numbers=>\@numbers,
318                                                                 searchdesc=> $searchdesc,
319                                                                 $defaultview => 1,
320                                                                 Search =>0
321                                                                 );
322         
323         } else {
324                 ($template, $loggedinuser, $cookie)
325                         = get_template_and_user({template_name => "value_builder/unimarc_field_4XX.tmpl",
326                                                 query => $query,
327                                                 type => "intranet",
328                                                 authnotrequired => 1,
329                                         });
330                 
331                 
332                 my $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
333                 $sth->execute;
334                 my  @itemtype;
335                 my %itemtypes;
336                 push @itemtype, "";
337                 $itemtypes{''} = "";
338                 while (my ($value,$lib) = $sth->fetchrow_array) {
339                         push @itemtype, $value;
340                         $itemtypes{$value}=$lib;
341                 }
342                 
343                 my $CGIitemtype=CGI::scrolling_list( -name     => 'value',
344                                         -values   => \@itemtype,
345                                         -labels   => \%itemtypes,
346                                         -size     => 1,
347                                         -multiple => 0 );
348                 $sth->finish;
349                 
350                 my @branchloop;
351                 my @select_branch;
352                 my %select_branches;
353                 my $branches=getbranches;
354                 push @select_branch, "";
355                 $select_branches{''} = "";
356                 foreach my $thisbranch (keys %$branches){
357                         push @select_branch, $branches->{$thisbranch}->{'branchcode'};
358                         $select_branches{$branches->{$thisbranch}->{'branchcode'}} = $branches->{$thisbranch}->{'branchname'};
359                 }
360                 my $CGIbranch=CGI::scrolling_list( -name     => 'value',
361                                         -values   => \@select_branch,
362                                         -labels   => \%select_branches,
363                                         -size     => 1,
364                                         -multiple => 0 );
365                 $sth->finish;
366                 
367                 my $req = $dbh->prepare("select distinctrow left(publishercode,45) from biblioitems order by publishercode");
368                 $req->execute;
369                 my @select;
370                 push @select,"";
371                 while (my ($value) =$req->fetchrow) {
372                         push @select, $value;
373                 }
374                 my $CGIpublisher=CGI::scrolling_list( -name     => 'value',
375                                         -id => 'publisher',
376                                         -values   => \@select,
377                                         -size     => 1,
378                                         -multiple => 0 );
379                 
380 #               my $sth=$dbh->prepare("select description,itemtype from itemtypes order by description");
381 #               $sth->execute;
382 #               while (my ($description,$itemtype) = $sth->fetchrow) {
383 #                       $classlist.="<option value=\"$itemtype\">$description</option>\n";
384 #               }
385 #               $sth->finish;
386                                 
387                 $template->param(#classlist => $classlist,
388                                                 CGIitemtype => $CGIitemtype,
389                                                 CGIbranch => $CGIbranch,
390                                                 CGIPublisher => $CGIpublisher,
391                                                 index=>$query->param('index'),
392                                                 Search =>1,
393                 );
394         }       
395         output_html_with_http_headers $query, $cookie, $template->output ;
396 }
397
398 1;