Bug 27461: Move hardcoded value to module
[koha.git] / Koha / Util / FrameworkPlugin.pm
1 package Koha::Util::FrameworkPlugin;
2
3 # Module contains subroutines used in the framework plugins
4 #
5 # Copyright 2014 Koha Development Team
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 #
22
23 use Modern::Perl;
24
25 use constant DEFAULT_008_POS_6_39 => 'b        |||||||| |||| 00| 0 eng d';
26
27 our ( @ISA, @EXPORT_OK );
28 BEGIN {
29     require Exporter;
30     @ISA = qw( Exporter );
31     @EXPORT_OK = qw( wrapper date_entered biblio_008 );
32 }
33
34 =head1 NAME
35
36 Koha::Util::FrameworkPlugin - utility class with routines for framework plugins
37
38 =head1 FUNCTIONS
39
40 =head2 wrapper
41
42     wrapper returns a text for strings containing spaces, pipe chars, ...
43     The wrapper subroutine is used in several UNIMARC plugins.
44
45 =cut
46
47 sub wrapper {
48     my ( $str ) = @_;
49     return "space" if $str eq " ";
50     return "dblspace" if $str eq "  ";
51     return "pipe" if $str eq "|";
52     return "dblpipe" if $str eq "||";
53     return $str;
54 }
55
56 =head2 date_entered
57
58     date_entered returns date in yymmdd format as needed by MARC21 field 008
59
60 =cut
61
62 sub date_entered {
63     # find today's date
64     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
65     $year +=1900;
66     $mon +=1;
67     return substr($year,2,2).sprintf ("%0.2d", $mon).sprintf ("%0.2d",$mday);
68 }
69
70 =head2 biblio_008
71
72     Returns a default value for MARC21 field 008 for biblio records.
73     Depends on prefs DefaultCountryField008, DefaultLanguageField008.
74
75 =cut
76
77 sub biblio_008 {
78     my $result = date_entered() . DEFAULT_008_POS_6_39;
79     if( C4::Context->preference('DefaultCountryField008') ) {
80         substr( $result, 15, 3 ) = pack( "A3", C4::Context->preference('DefaultCountryField008') );
81     }
82     if( C4::Context->preference('DefaultLanguageField008') ) {
83         substr( $result, 35, 3 ) = pack( "A3", C4::Context->preference('DefaultLanguageField008' ) );
84     }
85     return $result;
86 }
87
88 1;