Bug 32817: Fix cataloguing/value_builder/dateaccessioned.pl
[koha.git] / cataloguing / value_builder / unimarc_field_146f.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_146f.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_146f.tt",
51         query => $cgi,
52         type => 'intranet',
53         flagsrequired => { editcatalogue => '*' },
54     });
55
56     my @category_optgroups = (
57         { label => __('Woodwinds'), values => Koha::CodeList::Unimarc::MediumOfPerformance->woodwinds() },
58         { label => __('Brass instruments'), values => Koha::CodeList::Unimarc::MediumOfPerformance->brass() },
59         { label => __('Strings, bowed'), values => Koha::CodeList::Unimarc::MediumOfPerformance->strings_bowed() },
60         { label => __('Strings, plucked'), values => Koha::CodeList::Unimarc::MediumOfPerformance->strings_plucked() },
61         { label => __('Keyboard'), values => Koha::CodeList::Unimarc::MediumOfPerformance->keyboard() },
62         { label => __('Percussion'), values => Koha::CodeList::Unimarc::MediumOfPerformance->percussion() },
63         { label => __('Electric / electronic instruments and devices'), values => Koha::CodeList::Unimarc::MediumOfPerformance->electronic() },
64         { label => __('Miscellaneous, other, unspecified instruments'), values => Koha::CodeList::Unimarc::MediumOfPerformance->misc() },
65     );
66
67     foreach my $optgroup (@category_optgroups) {
68         my $values = delete $optgroup->{values};
69         $optgroup->{options} = [
70             map {
71                 { value => $_, label => __( $values->{$_} ) }
72             } sort keys %$values
73         ];
74     }
75
76     my $tessitura_hash = Koha::CodeList::Unimarc::MediumOfPerformance->tessitura();
77     my @tessitura_options = map {
78         { value => $_, label => __p('tessitura', $tessitura_hash->{$_}) }
79     } sort keys %$tessitura_hash;
80
81     my $number_of_hands_or_keys_hash = Koha::CodeList::Unimarc::MediumOfPerformance->number_of_hands_or_keys();
82     my @number_of_hands_or_keys_options = map {
83         { value => $_, label => __p('music', $number_of_hands_or_keys_hash->{$_}) }
84     } sort keys %$number_of_hands_or_keys_hash;
85
86     my $other_hash = Koha::CodeList::Unimarc::MediumOfPerformance->other();
87     my @other_options = map {
88         { value => $_, label => __($other_hash->{$_}) }
89     } sort keys %$other_hash;
90
91     my $other2_hash = Koha::CodeList::Unimarc::MediumOfPerformance->other2();
92     my @other2_options = map {
93         { value => $_, label => __($other2_hash->{$_}) }
94     } sort keys %$other2_hash;
95
96     my $value = $cgi->param('value');
97     my $number = substr($value, 0, 2);
98     unless (Scalar::Util::looks_like_number($number)) {
99         $number = '';
100     }
101     my $category = substr($value, 2, 3);
102     my $tessitura = substr($value, 5, 1);
103     my $number_of_hands_or_keys = substr($value, 6, 1);
104     my $other = substr($value, 7, 1);
105     my $other2 = substr($value, 8, 1);
106
107     $template->param(
108         id => scalar $cgi->param('id'),
109         number => $number,
110         category => $category,
111         tessitura => $tessitura,
112         number_of_hands_or_keys => $number_of_hands_or_keys,
113         other => $other,
114         other2 => $other2,
115         category_optgroups => \@category_optgroups,
116         tessitura_options => \@tessitura_options,
117         number_of_hands_or_keys_options => \@number_of_hands_or_keys_options,
118         other_options => \@other_options,
119         other2_options => \@other2_options,
120     );
121     output_html_with_http_headers $cgi, $cookie, $template->output;
122 };
123
124 return { builder => $builder, launcher => $launcher };