|
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 to_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_b = subfield_a.closest(\".sortable_subfield\").querySelector(\"li[id^=subfield083b] input[id^=tag_083_subfield_b_]\"); |
| 59 |
const subfield_c = subfield_a.closest(\".sortable_subfield\").querySelector(\"li[id^=subfield083c] input[id^=tag_083_subfield_c_]\"); |
| 60 |
const subfield_1 = subfield_a.closest(\".sortable_subfield\").querySelector(\"li[id^=subfield0831] input[id^=tag_083_subfield_1_]\"); |
| 61 |
|
| 62 |
subfield_c.style.transition = \"background-color 1000ms linear\"; |
| 63 |
subfield_1.style.transition = \"background-color 1000ms linear\"; |
| 64 |
|
| 65 |
if ( ! subfield_a.value ) { |
| 66 |
updateContent( subfield_1, \"No DDC number provided\" ); |
| 67 |
return 1; |
| 68 |
} else if ( ! ddcregex.test(subfield_a.value) ) { |
| 69 |
updateContent( subfield_1, \"Invalid DDC number provided in subfield \$a\" ); |
| 70 |
return 1; |
| 71 |
} else if ( subfield_b.value && ! ddcregex.test(subfield_b.value) ) { |
| 72 |
updateContent( subfield_1, \"Invalid DDC number provided in subfield \$b\" ); |
| 73 |
return 1; |
| 74 |
} |
| 75 |
|
| 76 |
if ( subfield_a.value && subfield_b.value ) { |
| 77 |
// It's a DDC range, separate the two numbers with a hyphen |
| 78 |
ddc_request = subfield_a.value + '-' + subfield_b.value; |
| 79 |
} else if ( subfield_a.value && ! subfield_b.value ) { |
| 80 |
ddc_request = subfield_a.value; |
| 81 |
} |
| 82 |
|
| 83 |
const url = \"../cataloguing/plugin_launcher.pl?plugin_name=marc21_field_083a_authorities.pl&ddc=\" + ddc_request; |
| 84 |
var request = \$.get(url); |
| 85 |
|
| 86 |
request.done( function(response) { |
| 87 |
const failregex = />Access denied/; |
| 88 |
if ( failregex.test(response) ) { |
| 89 |
updateContent( subfield_1, \"Access denied, you need at least the \\\"catalogue\\\" and \\\"editauthorities\\\" user permissions\" ); |
| 90 |
return 1; |
| 91 |
} else { |
| 92 |
const json = JSON.parse(response)[0]; |
| 93 |
if ( json.info ) { |
| 94 |
updateContent( subfield_1, json.info ); |
| 95 |
return 1; |
| 96 |
} else { |
| 97 |
updateContent( subfield_1, json.ld_url ); |
| 98 |
updateContent( subfield_c, json.ld_descr ); |
| 99 |
} |
| 100 |
} |
| 101 |
return 0; |
| 102 |
}); |
| 103 |
} |
| 104 |
|
| 105 |
// Credit to: https://stackoverflow.com/a/62468495 |
| 106 |
function updateContent( element, content ) { |
| 107 |
// update content |
| 108 |
element.value = content; |
| 109 |
// change the background color (will trigger the CSS configured transition) |
| 110 |
element.style.backgroundColor = \"yellow\"; |
| 111 |
// change the background color back (with a delay equal to transition length) |
| 112 |
setTimeout(() => { element.style.backgroundColor = \"white\"; }, 2000); |
| 113 |
} |
| 114 |
</script> |
| 115 |
"; |
| 116 |
return $res; |
| 117 |
}; |
| 118 |
|
| 119 |
my $launcher = sub { |
| 120 |
my ($params) = @_; |
| 121 |
my $input = $params->{cgi}; |
| 122 |
my $ddc_number = $input->param('ddc'); |
| 123 |
|
| 124 |
my ( $template, $loggedinuser, $cookie ) = get_template_and_user( |
| 125 |
{ |
| 126 |
template_name => "cataloguing/value_builder/ajax.tt", |
| 127 |
query => $input, |
| 128 |
type => "intranet", |
| 129 |
flagsrequired => { editauthorities => 1 }, |
| 130 |
} |
| 131 |
); |
| 132 |
|
| 133 |
my @ret; |
| 134 |
my $msg; |
| 135 |
my $info = undef; |
| 136 |
my $enabled = C4::Context->preference('OCLCDeweyLinkedDataAPI') ? 1 : 0; |
| 137 |
my $language = C4::Context->preference('OCLCDeweyLinkedDataLanguage'); |
| 138 |
|
| 139 |
if ( not $enabled ) { |
| 140 |
$info = 'Please enable the OCLCDeweyLinkedDataAPI System Preference in order to use this Framework Plugin'; |
| 141 |
} |
| 142 |
|
| 143 |
my $access_token; |
| 144 |
if ( not $info ) { |
| 145 |
|
| 146 |
my $scope = 'deweyLinkedData'; |
| 147 |
|
| 148 |
( $access_token, $msg ) = Koha::ExternalContent::OCLC->get_access_token( |
| 149 |
{ |
| 150 |
scope => $scope, |
| 151 |
} |
| 152 |
); |
| 153 |
if ( not $access_token ) { |
| 154 |
$info = "Cannot obtain API token ($msg)"; |
| 155 |
} |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
# Trim leading 'T' (uppercase 't') from DDC number (or range) before looking it up |
| 160 |
my $ddc_lookup = $ddc_number =~ s/^T//r; |
| 161 |
|
| 162 |
my $ddc_uri; |
| 163 |
if ( not $info ) { |
| 164 |
|
| 165 |
( $ddc_uri, $msg ) = Koha::ExternalContent::OCLC->get_ddc_uri( |
| 166 |
{ |
| 167 |
access_token => $access_token, |
| 168 |
ddc_number => $ddc_lookup |
| 169 |
} |
| 170 |
); |
| 171 |
if ( not $ddc_uri and $msg eq 'Unauthorized' ) { |
| 172 |
$info = 'The API access token has expired'; |
| 173 |
} elsif ( $ddc_uri eq '' ) { |
| 174 |
$info = "No Dewey Linked Data URI found for DDC '$ddc_number'"; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
my $ddc_url; |
| 179 |
if ( not $info ) { |
| 180 |
|
| 181 |
( $ddc_url, $msg ) = Koha::ExternalContent::OCLC->get_ddc_url( |
| 182 |
{ |
| 183 |
access_token => $access_token, |
| 184 |
ddc_number => $ddc_lookup |
| 185 |
} |
| 186 |
); |
| 187 |
if ( not $ddc_url and $msg eq 'Unauthorized' ) { |
| 188 |
$info = 'The API access token has expired'; |
| 189 |
} elsif ( $ddc_url eq '' ) { |
| 190 |
$info = "No Dewey Linked Data URL found for DDC '$ddc_number'"; |
| 191 |
} |
| 192 |
|
| 193 |
} |
| 194 |
|
| 195 |
my $ddc_descr; |
| 196 |
if ( not $info ) { |
| 197 |
|
| 198 |
( $ddc_descr, $msg ) = Koha::ExternalContent::OCLC->get_ddc_description( |
| 199 |
{ |
| 200 |
access_token => $access_token, |
| 201 |
ddc_uri => $ddc_uri, |
| 202 |
language => $language, |
| 203 |
} |
| 204 |
); |
| 205 |
if ( not $ddc_descr and $msg eq 'Unauthorized' ) { |
| 206 |
$info = 'The API access token has expired'; |
| 207 |
} elsif ( not defined $ddc_descr ) { |
| 208 |
$info = "No Dewey Linked Data Description found for DDC URI '$ddc_uri'"; |
| 209 |
} |
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
push @ret, { |
| 214 |
info => $info, |
| 215 |
ld_url => $ddc_url, |
| 216 |
ld_descr => $ddc_descr, |
| 217 |
}; |
| 218 |
output_html_with_http_headers $input, $cookie, to_json( \@ret, { utf8 => 1 } ), 'json'; |
| 219 |
}; |
| 220 |
|
| 221 |
return { builder => $builder, launcher => $launcher }; |