Bug 11299: Add a button to the biblio edition page to automatically add authority...
[koha.git] / cataloguing / value_builder / marc21_field_008_authorities.pl
1 #!/usr/bin/perl
2
3 # Converted to new plugin style (Bug 13437)
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 use Modern::Perl;
23 use C4::Auth;
24 use CGI qw ( -utf8 );
25 use C4::Context;
26
27 use C4::Search;
28 use C4::Output;
29 use Koha::Util::FrameworkPlugin qw|date_entered|;
30
31 use constant FIXLEN_DATA_ELTS => '|| aca||aabn           | a|a     d';
32 use constant PREF_008 => 'MARCAuthorityControlField008';
33
34 my $builder = sub {
35     my ( $params ) = @_;
36     my $function_name = $params->{id};
37     my $dateentered = date_entered();
38     my $defaultval = substr( C4::Context->preference(PREF_008) || FIXLEN_DATA_ELTS, 0, 34 );
39     my $res="
40 <script>
41
42 function Focus$function_name(event) {
43     if (!document.getElementById(event.data.id).value) {
44     var authtype=document.forms['f'].elements['authtypecode'].value;
45     var fieldval='$dateentered$defaultval';
46     if(authtype && (authtype == 'TOPIC_TERM' || authtype == 'GENRE/FORM' || authtype == 'CHRON_TERM')) {
47       fieldval= fieldval.substr(0,14)+'b'+fieldval.substr(15);
48     }
49         document.getElementById(event.data.id).value=fieldval;
50     }
51     return 1;
52 }
53
54 function Click$function_name(event) {
55     var authtype=document.forms['f'].elements['authtypecode'].value;
56     defaultvalue=document.getElementById(event.data.id).value;
57     newin=window.open(\"../cataloguing/plugin_launcher.pl?plugin_name=marc21_field_008_authorities.pl&index=\"+ event.data.id +\"&result=\"+defaultvalue+\"&authtypecode=\"+authtype,\"tag_editor\",'width=1000,height=600,toolbar=false,scrollbars=yes');
58
59 }
60 </script>
61 ";
62
63     return $res;
64 };
65
66 my $launcher = sub {
67     my ( $params ) = @_;
68     my $input = $params->{cgi};
69     my $index= $input->param('index');
70     my $result= $input->param('result');
71     my $authtype= $input->param('authtypecode')||'';
72
73     my $defaultval = substr( C4::Context->preference(PREF_008) || FIXLEN_DATA_ELTS, 0, 34 );
74     substr($defaultval,14-6,1)='b' if $authtype=~ /TOPIC_TERM|GENRE.FORM|CHRON_TERM/;
75
76     my $dbh = C4::Context->dbh;
77
78     my ($template, $loggedinuser, $cookie)
79     = get_template_and_user({template_name => "cataloguing/value_builder/marc21_field_008_authorities.tt",
80                  query => $input,
81                  type => "intranet",
82                  flagsrequired => {editcatalogue => '*'},
83                  debug => 1,
84                  });
85     my $dateentered = date_entered();
86     $result = "$dateentered$defaultval" unless $result;
87     my @f;
88     for(0,6..17,28,29,31..33,38,39) {
89         $f[$_]=substr($result,$_,$_==0?6:1);
90     }
91     $template->param(index => $index);
92
93     $f[0]= $dateentered if !$f[0] || $f[0]=~/\s/;
94     $template->param(f1 => $f[0]);
95
96     for(6..17,28,29,31..33,38,39) {
97         $template->param(
98             "f$_" => $f[$_],
99             "f$_".($f[$_] eq '|'? 'pipe': $f[$_]) => $f[$_],
100         );
101     }
102     output_html_with_http_headers $input, $cookie, $template->output;
103 };
104
105 return { builder => $builder, launcher => $launcher };