|
Line 0
Link Here
|
|
|
1 |
package Koha::Plugin::Test::PID::Orgcode; |
| 2 |
|
| 3 |
use Modern::Perl; |
| 4 |
use JSON; |
| 5 |
use MARC::Record; |
| 6 |
|
| 7 |
use C4::Context; |
| 8 |
|
| 9 |
use parent qw/Koha::Plugins::Base/; |
| 10 |
|
| 11 |
our $VERSION = 1.00; |
| 12 |
our $metadata = { name => 'Test::PID::Orgcode', version => $VERSION }; |
| 13 |
|
| 14 |
sub new { |
| 15 |
my ($class, $params) = @_; |
| 16 |
$params->{metadata} = $metadata; |
| 17 |
return $class->SUPER::new($params); |
| 18 |
} |
| 19 |
sub install { |
| 20 |
C4::Context->dbh->do( "INSERT IGNORE INTO plugin_methods (plugin_class, plugin_method) VALUES (?,?)", undef, __PACKAGE__, 'pid_get_hook' ); |
| 21 |
C4::Context->dbh->do( "INSERT IGNORE INTO plugin_methods (plugin_class, plugin_method) VALUES (?,?)", undef, __PACKAGE__, 'pid_insert_hook' ); |
| 22 |
1; |
| 23 |
} |
| 24 |
sub uninstall { |
| 25 |
C4::Context->dbh->do( "DELETE FROM plugin_data WHERE plugin_class LIKE ?", undef, __PACKAGE__ ); |
| 26 |
C4::Context->dbh->do( "DELETE FROM plugin_methods WHERE plugin_class LIKE ?", undef, __PACKAGE__ ); |
| 27 |
1; |
| 28 |
} |
| 29 |
|
| 30 |
sub pid_get_hook { # We should return true if we want get to proceed |
| 31 |
my ( $self, $params ) = @_; |
| 32 |
my $category = $params->{category} // q{}; |
| 33 |
my $record = $params->{record} // q{}; |
| 34 |
|
| 35 |
return 1 if $category eq 'item'; |
| 36 |
return 1 if C4::Context->preference('PID_Field') !~ /\Q$category\E[:=]035\$a/; # Assume 035a |
| 37 |
my $orgcode = C4::Context->preference('MARCOrgCode'); # override via $ENV ? |
| 38 |
# Walk thru 035s and temporarily 'remove' the URLs without orgcode |
| 39 |
# This will force get to return a PID with orgcode if present |
| 40 |
foreach my $fld ( $record->field('035') ) { |
| 41 |
$record->delete_fields( $fld ) if |
| 42 |
!$fld->subfield('a') or $fld->subfield('a') !~ /^\(\Q$orgcode\E\)/; |
| 43 |
} |
| 44 |
return 1; |
| 45 |
} |
| 46 |
|
| 47 |
sub pid_insert_hook { |
| 48 |
my ( $self, $params ) = @_; |
| 49 |
my $category = $params->{category} // q{}; |
| 50 |
my $inserted = $params->{inserted} // q{}; |
| 51 |
my $pid = $params->{pid} // q{}; |
| 52 |
my $record = $params->{record} or return; # something wrong |
| 53 |
|
| 54 |
return 1 if $category eq 'item'; |
| 55 |
return 1 if C4::Context->preference('PID_Field') !~ /\Q$category\E[:=]035\$a/; # Assume 035a |
| 56 |
return 1 if !$inserted; # Nothing was touched |
| 57 |
|
| 58 |
# Adjust first (new) field and look for second field with orgcode |
| 59 |
my $orgcode = C4::Context->preference('MARCOrgCode'); # override via $ENV ? |
| 60 |
my ( $first, $first_fld ) = ( 1, undef ); |
| 61 |
foreach my $fld ( $record->field('035') ) { |
| 62 |
if( $first ) { |
| 63 |
return if $fld->subfield('a') ne $pid; # unexpected |
| 64 |
$fld->update( a => "($orgcode)$pid" ); |
| 65 |
$first = 0; |
| 66 |
$first_fld = $fld; |
| 67 |
next; |
| 68 |
} |
| 69 |
next if !$fld->subfield('a') or $fld->subfield('a') !~ /^\(\Q$orgcode\E\)/; |
| 70 |
# If there is a field, move older $a to $z and insert new $a |
| 71 |
$fld->add_subfields( z => $fld->subfield('a') ); |
| 72 |
$fld->update( a => "($orgcode)$pid" ); |
| 73 |
$record->delete_fields( $first_fld ); |
| 74 |
last; |
| 75 |
} |
| 76 |
return 1; |
| 77 |
} |
| 78 |
|
| 79 |
1; |