Bug 11944: use CGI( -utf8 ) everywhere
[koha.git] / cataloguing / value_builder / unimarc_field_100_authorities.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2013 Vitor Fernandes
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Koha::Util::FrameworkPlugin qw(wrapper);
23 use C4::Auth;
24 use CGI qw ( -utf8 );
25 use C4::Context;
26 use C4::Output;
27
28
29 =head1 FUNCTIONS
30
31 =head2 plugin_parameters
32
33 Other parameters added when the plugin is called by the dopop function
34
35 =cut
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 $res           = "
45         <script type='text/javascript'>
46             function Focus$field_number() {
47                 return 1;
48             }
49
50             function Blur$field_number() {
51                 return 1;
52             }
53
54             function Clic$field_number(i) {
55                 var defaultvalue;
56                 try {
57                     defaultvalue = document.getElementById(i).value;
58                 } catch(e) {
59                     alert('error when getting '+i);
60                     return;
61                 }
62                 window.open(\"/cgi-bin/koha/cataloguing/plugin_launcher.pl?plugin_name=unimarc_field_100_authorities.pl&index=\"+i+\"&result=\"+defaultvalue,\"unimarc_field_100\",'width=1000,height=600,toolbar=false,scrollbars=yes');
63             }
64         </script>
65 ";
66
67     return ( $field_number, $res );
68 }
69
70
71 sub plugin {
72     my ($input) = @_;
73     my $index  = $input->param('index');
74     my $result = $input->param('result');
75
76     my $dbh = C4::Context->dbh;
77
78     my $defaultlanguage = C4::Context->preference("UNIMARCField100Language");
79     $defaultlanguage = "fre" if (!$defaultlanguage || length($defaultlanguage) != 3);
80
81     my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
82         {
83             template_name => "cataloguing/value_builder/unimarc_field_100_authorities.tt",
84             query         => $input,
85             type          => "intranet",
86             authnotrequired => 0,
87             flagsrequired   => { editcatalogue => '*' },
88             debug           => 1,
89         }
90     );
91     $result = "        a".$defaultlanguage."y50      ba0" unless $result;
92     my $f1 = substr( $result, 0, 8 );
93     if ( $f1 eq '        ' ) {
94         my @today = Date::Calc::Today();
95         $f1 = $today[0] . sprintf('%02s',$today[1]) . sprintf('%02s',$today[2]);
96     }
97
98     my $f2  = substr( $result, 8,  1 ); $f2  = wrapper( $f2 ) if $f2;
99     my $f3  = substr( $result, 9,  3 );
100     my $f4  = substr( $result, 12, 1 ); $f4  = wrapper( $f4 ) if $f4;
101     my $f5  = substr( $result, 13, 2 ); $f5  = wrapper( $f5 ) if $f5;
102     my $f6  = substr( $result, 15, 2 ); $f6  = wrapper( $f6 ) if $f6;
103     my $f7  = substr( $result, 17, 4 ); $f7  = wrapper( $f7 ) if $f7;
104     my $f8  = substr( $result, 21, 2 ); $f8  = wrapper( $f8 ) if $f8;
105     my $f9  = substr( $result, 23, 1 ); $f9  = wrapper( $f9 ) if $f9;
106
107     $template->param(
108         index     => $index,
109         f1        => $f1,
110         "f2$f2"   => 1,
111         f3        => $f3,
112         "f4$f4"   => 1,
113         "f5$f5"   => 1,
114         "f6$f6"   => 1,
115         f7        => $f7,
116         "f8$f8"   => 1,
117         "f9$f9"   => 1,
118     );
119     output_html_with_http_headers $input, $cookie, $template->output;
120 }
121
122 1;