-
Notifications
You must be signed in to change notification settings - Fork 0
/
marc21_field_001_autocontrolnumber.pl
executable file
·66 lines (51 loc) · 1.56 KB
/
marc21_field_001_autocontrolnumber.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/perl
# Copyright 2023 HKS3
# https://github.com/HKS3/Koha-ValueBuilder-AutoControlNumber
use Modern::Perl;
use C4::Context;
my $builder = sub {
my ( $params ) = @_;
my $dbh = C4::Context->dbh;
my $seq = 'AUTO_CONTROL_NUMBER_SEQUENCE';
my $nextval_sth = $dbh->prepare("SELECT NEXTVAL($seq)");
my $extract_sth = $dbh->prepare(q{SELECT biblionumber FROM biblio_metadata WHERE ExtractValue(metadata,'//controlfield[@tag="001"]') = ?});
my $controlnumber_exists = sub {
my $cn = shift;
$extract_sth->execute($cn);
my $biblionumber = $extract_sth->fetchrow();
warn "controlnumber $cn already in use by biblionumber: $biblionumber"
if defined $biblionumber;
return defined $biblionumber;
};
my $get_controlnumber = sub {
my $cn;
do {
$nextval_sth->execute();
$cn = $nextval_sth->fetchrow();
if (!defined $cn) {
die "there was an error while fetching nextval from sequence '$seq'";
}
} while ( $controlnumber_exists->($cn) );
return $cn;
};
my $build_js = sub {
my ($id, $cn) = @_;
my $js = <<"EOJS";
<script type="text/javascript">
//<![CDATA[
\$(document).ready(function() {
if(!document.getElementById('$id').value){
document.getElementById('$id').value = '$cn';
}
});
//]]>
</script>
EOJS
return $js;
};
my $id = $params->{id};
my $cn = $get_controlnumber->();
my $js = $build_js->($id, $cn);
return $js;
};
return { builder => $builder };