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