View | Details | Raw Unified | Return to bug 40309
Collapse All | Expand All

(-)a/cataloguing/value_builder/marc21_field_082a_authorities.pl (-1 / +161 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Copyright (C) 2025 Dataly Tech
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 C4::Auth    qw( check_cookie_auth get_template_and_user );
23
use C4::Context qw( preference );
24
use C4::Output  qw( output_html_with_http_headers );
25
26
use Koha::Caches;
27
use Koha::ExternalContent::OCLC;
28
29
use CGI  qw( -utf8 );
30
use JSON qw( from_json );
31
use LWP::UserAgent;
32
33
use YAML;
34
35
my $input = CGI->new;
36
my ($auth_status) =
37
    check_cookie_auth( $input->cookie('CGISESSID'), { catalogue => 1 } );
38
if ( $auth_status ne "ok" ) {
39
    print $input->header( -type => 'text/plain', -status => '403 Forbidden' );
40
    exit 0;
41
}
42
43
my $builder = sub {
44
    my ($params) = @_;
45
46
    my $function_name = $params->{id};
47
48
    my $ddcregex = '^((T([1-6])--([0-9]+)))$|^([0-9]{3})$|^(([0-9]{3})\.([0-9]+))$';
49
50
    my $res = "
51
<script>
52
    function Click$function_name(event) {
53
        event.preventDefault();
54
55
        const ddcregex = /$ddcregex/;
56
57
        const subfield_a = document.getElementById(event.data.id);
58
        const subfield_1 = subfield_a.closest(\".sortable_subfield\").querySelector(\"li[id^=subfield0821] input[id^=tag_082_subfield_1_]\");
59
60
        subfield_1.style.transition = \"background-color 1000ms linear\";
61
62
        if ( ! subfield_a.value ) {
63
            updateContent( subfield_1, \"No DDC number provided\" );
64
            return 1;
65
        } else if ( ! ddcregex.test(subfield_a.value) ) {
66
            updateContent( subfield_1, \"Invalid DDC number provided in subfield \$a\" );
67
            return 1;
68
        }
69
70
        const url = \"../cataloguing/plugin_launcher.pl?plugin_name=marc21_field_082a_authorities.pl&ddc=\" + subfield_a.value;
71
        var request = \$.get(url);
72
73
        request.done( function(response) {
74
            const failregex = />Access denied/;
75
            if ( failregex.test(response) ) {
76
                updateContent( subfield_1, \"Access denied, you need at least the \\\"catalogue\\\" and \\\"editauthorities\\\" user permissions\" );
77
                return 1;
78
            } else {
79
                updateContent( subfield_1, response );
80
            }
81
            return 0;
82
        });
83
    }
84
85
    // Credit to: https://stackoverflow.com/a/62468495
86
    function updateContent( element, content ) {
87
        // update content
88
        element.value = content;
89
        // change the background color (will trigger the CSS configured transition)
90
        element.style.backgroundColor = \"yellow\";
91
        // change the background color back (with a delay equal to transition length)
92
        setTimeout(() => { element.style.backgroundColor = \"white\"; }, 2000);
93
    }
94
</script>
95
";
96
    return $res;
97
};
98
99
my $launcher = sub {
100
    my ($params)   = @_;
101
    my $input      = $params->{cgi};
102
    my $ddc_number = $input->param('ddc');
103
104
    my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
105
        {
106
            template_name => "cataloguing/value_builder/ajax.tt",
107
            query         => $input,
108
            type          => "intranet",
109
            flagsrequired => { editauthorities => 1 },
110
        }
111
    );
112
113
    my $ret;
114
    my $msg;
115
116
    my $enabled = C4::Context->preference('OCLCDeweyLinkedDataAPI') ? 1 : 0;
117
    if ( not $enabled ) {
118
        $ret = 'Please enable the OCLCDeweyLinkedDataAPI System Preference in order to use this Framework Plugin';
119
    }
120
121
    my $access_token;
122
    if ( not $ret ) {
123
        my $scope = 'deweyLinkedData';
124
125
        ( $access_token, $msg ) = Koha::ExternalContent::OCLC->get_access_token(
126
            {
127
                scope => $scope,
128
            }
129
        );
130
        if ( not $access_token ) {
131
            $ret = "Cannot obtain API token ($msg)";
132
        }
133
    }
134
135
    if ( not $ret ) {
136
137
        # Trim leading 'T' (uppercase 't') from DDC number before looking it up
138
        my $ddc_lookup = $ddc_number =~ s/^T//r;
139
140
        my $ddc_url;
141
        ( $ddc_url, $msg ) = Koha::ExternalContent::OCLC->get_ddc_url(
142
            {
143
                access_token => $access_token,
144
                ddc_number   => $ddc_lookup
145
            }
146
        );
147
        if ( not $ddc_url and $msg eq 'Unauthorized' ) {
148
            $ret = 'The API access token has expired';
149
        } elsif ( $ddc_url eq '' ) {
150
            $ret = "No Dewey Linked Data URL found for DDC '$ddc_number'";
151
        } else {
152
            $ret = $ddc_url;
153
        }
154
    }
155
156
    $template->param( return => $ret );
157
    output_html_with_http_headers $input, $cookie, $template->output;
158
159
};
160
161
return { builder => $builder, launcher => $launcher };

Return to bug 40309