Bug 32817: Fix cataloguing/value_builder/dateaccessioned.pl
[koha.git] / cataloguing / value_builder / unimarc_field_146d.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Scalar::Util;
21
22 use C4::Auth qw( get_template_and_user );
23 use C4::Output qw( output_html_with_http_headers );
24
25 use Koha::I18N;
26 use Koha::CodeList::Unimarc::MediumOfPerformance;
27
28 my $builder = sub {
29     my $params = shift;
30     my $id = $params->{id};
31
32     return qq|
33 <script>
34 function Click$id (event) {
35     event.preventDefault();
36     const value = document.getElementById(event.data.id).value;
37     const url = new URL('/cgi-bin/koha/cataloguing/plugin_launcher.pl', location);
38     url.searchParams.set('plugin_name', 'unimarc_field_146d.pl');
39     url.searchParams.set('id', event.data.id);
40     url.searchParams.set('value', value);
41     window.open(url.toString(), 'tag_editor', 'width=700,height=700,toolbar=false,scrollbars=yes');
42 }
43 </script>|;
44 };
45
46 my $launcher = sub {
47     my $params = shift;
48     my $cgi = $params->{cgi};
49     my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
50         template_name => "cataloguing/value_builder/unimarc_field_146d.tt",
51         query => $cgi,
52         type => 'intranet',
53         flagsrequired => { editcatalogue => '*' },
54     });
55
56     my @category_optgroups = (
57         { label => __('Choruses'), values => Koha::CodeList::Unimarc::MediumOfPerformance->choruses() },
58         { label => __('Orchestras, ensembles'), values => Koha::CodeList::Unimarc::MediumOfPerformance->orchestras() },
59     );
60
61     foreach my $optgroup (@category_optgroups) {
62         my $values = delete $optgroup->{values};
63         $optgroup->{options} = [
64             map {
65                 { value => $_, label => __( $values->{$_} ) }
66             } sort keys %$values
67         ];
68     }
69
70     my $other_hash = Koha::CodeList::Unimarc::MediumOfPerformance->other();
71     my @other_options = map {
72         { value => $_, label => __($other_hash->{$_}) }
73     } sort keys %$other_hash;
74
75     my $other2_hash = Koha::CodeList::Unimarc::MediumOfPerformance->other2();
76     my @other2_options = map {
77         { value => $_, label => __($other2_hash->{$_}) }
78     } sort keys %$other2_hash;
79
80     my $value = $cgi->param('value');
81     my $number = substr($value, 0, 2);
82     unless (Scalar::Util::looks_like_number($number)) {
83         $number = '';
84     }
85     my $category = substr($value, 2, 3);
86     my $number_of_real_parts = substr($value, 5, 2);
87     unless (Scalar::Util::looks_like_number($number_of_real_parts)) {
88         $number_of_real_parts = '';
89     }
90     my $other = substr($value, 7, 1);
91     my $other2 = substr($value, 8, 1);
92
93     $template->param(
94         id => scalar $cgi->param('id'),
95         number => $number,
96         category => $category,
97         number_of_real_parts => $number_of_real_parts,
98         other => $other,
99         other2 => $other2,
100         category_optgroups => \@category_optgroups,
101         other_options => \@other_options,
102         other2_options => \@other2_options,
103     );
104     output_html_with_http_headers $cgi, $cookie, $template->output;
105 };
106
107 return { builder => $builder, launcher => $launcher };