Bug 9978: Replace license header with the correct license (GPLv3+)
[koha-equinox.git] / cataloguing / value_builder / marc21_field_008_authorities.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use C4::Auth;
24 use CGI qw ( -utf8 );
25 use C4::Context;
26
27 use C4::Search;
28 use C4::Output;
29
30 use constant FIXLEN_DATA_ELTS => '|| aca||aabn           | a|a     d';
31 use constant PREF_008 => 'MARCAuthorityControlField008';
32
33 =head1 DESCRIPTION
34
35 plugin_parameters : other parameters added when the plugin is called by the dopop function
36
37 =cut
38
39 # find today's date
40 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
41
42 $year +=1900; $mon +=1;
43 my $dateentered = substr($year,2,2).sprintf ("%0.2d", $mon).sprintf ("%0.2d",$mday);
44 my $defaultval = Field008();
45
46 sub plugin_javascript {
47     my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
48     my $function_name= $field_number;
49     my $res="
50 <script type=\"text/javascript\">
51 //<![CDATA[
52
53 function Focus$function_name(subfield_managed) {
54     if (!document.getElementById(\"$field_number\").value) {
55     var authtype=document.forms['f'].elements['authtypecode'].value;
56     var fieldval='$dateentered$defaultval';
57     if(authtype && (authtype == 'TOPIC_TERM' || authtype == 'GENRE/FORM' || authtype == 'CHRON_TERM')) {
58       fieldval= fieldval.substr(0,14)+'b'+fieldval.substr(15);
59     }
60         document.getElementById(\"$field_number\").value=fieldval;
61     }
62     return 1;
63 }
64
65 function Clic$function_name(i) {
66     var authtype=document.forms['f'].elements['authtypecode'].value;
67     defaultvalue=document.getElementById(\"$field_number\").value;
68     newin=window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=marc21_field_008_authorities.pl&index=$field_number&result=\"+defaultvalue+\"&authtypecode=\"+authtype,\"tag_editor\",'width=1000,height=600,toolbar=false,scrollbars=yes');
69
70 }
71 //]]>
72 </script>
73 ";
74
75     return ($function_name,$res);
76 }
77 sub plugin {
78     my ($input) = @_;
79     my $index= $input->param('index');
80     my $result= $input->param('result');
81     my $authtype= $input->param('authtypecode')||'';
82     substr($defaultval,14-6,1)='b' if $authtype=~ /TOPIC_TERM|GENRE.FORM|CHRON_TERM/;
83
84     my $dbh = C4::Context->dbh;
85
86     my ($template, $loggedinuser, $cookie)
87     = get_template_and_user({template_name => "cataloguing/value_builder/marc21_field_008_authorities.tt",
88                  query => $input,
89                  type => "intranet",
90                  authnotrequired => 0,
91                  flagsrequired => {editcatalogue => '*'},
92                  debug => 1,
93                  });
94     $result = "$dateentered$defaultval" unless $result;
95     my @f;
96     for(0,6..17,28,29,31..33,38,39) {
97         $f[$_]=substr($result,$_,$_==0?6:1);
98     }
99     $template->param(index => $index);
100
101     $f[0]= $dateentered if !$f[0] || $f[0]=~/\s/;
102     $template->param(f1 => $f[0]);
103
104     for(6..17,28,29,31..33,38,39) {
105         $template->param(
106             "f$_" => $f[$_],
107             "f$_".($f[$_] eq '|'? 'pipe': $f[$_]) => $f[$_],
108         );
109     }
110     output_html_with_http_headers $input, $cookie, $template->output;
111 }
112
113 sub Field008 {
114   my $pref= C4::Context->preference(PREF_008);
115   if(!$pref) {
116     return FIXLEN_DATA_ELTS;
117   }
118   elsif(length($pref)<34) {
119     warn "marc21_field_008_authorities.pl: Syspref ".PREF_008." should be 34 characters long ";
120     return FIXLEN_DATA_ELTS;
121   }
122   return substr($pref,0,34);  #ignore remainder
123 }