File Coverage

File:C4/ClassSortRoutine/Dewey.pm
Coverage:95.8%

linestmtbrancondsubtimecode
1package C4::ClassSortRoutine::Dewey;
2
3# Copyright (C) 2007 LibLime
4#
5# This file is part of Koha.
6#
7# Koha is free software; you can redistribute it and/or modify it under the
8# terms of the GNU General Public License as published by the Free Software
9# Foundation; either version 2 of the License, or (at your option) any later
10# version.
11#
12# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License along
17# with Koha; if not, write to the Free Software Foundation, Inc.,
18# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
24
24
24
20371
157
887
use strict;
21
24
24
24
256
146
1523
use warnings;
22
23
24
24
24
199
115
9260
use vars qw($VERSION);
24
25# set the version for version checking
26$VERSION = 3.00;
27
28 - 56
=head1 NAME 

C4::ClassSortRoutine::Dewey - generic call number sorting key routine

=head1 SYNOPSIS

use C4::ClassSortRoutine;

my $cn_sort = GetClassSortKey('Dewey', $cn_class, $cn_item);

=head1 FUNCTIONS

=head2 get_class_sort_key

  my $cn_sort = C4::ClassSortRoutine::Dewey::Dewey($cn_class, $cn_item);

Generates sorting key using the following rules:

* Concatenates class and item part.
* Converts to uppercase.
* Removes leading and trailing whitespace and '/'
* Separates alphabetic prefix from the rest of the call number
* Splits into tokens on whitespaces and periods.
* Leaves first digit group as is.
* Converts second digit group to 15-digit long group, padded on right with zeroes.
* Converts each run of whitespace to an underscore.
* Removes any remaining non-alphabetical, non-numeric, non-underscore characters.

=cut
57
58sub get_class_sort_key {
59
28
184311
    my ($cn_class, $cn_item) = @_;
60
61
28
168
    $cn_class = '' unless defined $cn_class;
62
28
139
    $cn_item = '' unless defined $cn_item;
63
28
157
    my $init = uc "$cn_class $cn_item";
64
28
116
    $init =~ s/^\s+//;
65
28
208
    $init =~ s/\s+$//;
66
28
100
    $init =~ s!/!!g;
67
2
2
2
28
384
15
63
608
    $init =~ s/^([\p{IsAlpha}]+)/$1 /;
68
28
304283
    my @tokens = split /\.|\s+/, $init;
69
28
342
    my $digit_group_count = 0;
70    for (my $i = 0; $i <= $#tokens; $i++) {
71
55
735
        if ($tokens[$i] =~ /^\d+$/) {
72
6
32
            $digit_group_count++;
73
6
31
            if (2 == $digit_group_count) {
74
3
18
                $tokens[$i] = sprintf("%-15.15s", $tokens[$i]);
75
3
18
                $tokens[$i] =~ tr/ /0/;
76            }
77        }
78
28
255
    }
79    # Pad the first digit_group if there was only one
80
28
257
    if (1 == $digit_group_count) {
81
0
0
        $tokens[0] .= '_000000000000000'
82    }
83
28
265
    my $key = join("_", @tokens);
84
28
767
    $key =~ s/[^\p{IsAlnum}_]//g;
85
86
28
174201
    return $key;
87
88}
89
901;
91
92 - 96
=head1 AUTHOR

Koha Development Team <http://koha-community.org/>

=cut
97