File: | C4/ClassSortRoutine/Dewey.pm |
Coverage: | 87.5% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
1 | package 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 | 46 46 46 | 46113 297 1731 | use strict; | |||
21 | 46 46 46 | 413 296 2473 | use warnings; | |||
22 | ||||||
23 | 46 46 46 | 393 194 18202 | 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 | ||||||
58 | sub get_class_sort_key { | |||||
59 | 54 | 383002 | my ($cn_class, $cn_item) = @_; | |||
60 | ||||||
61 | 54 | 434 | $cn_class = '' unless defined $cn_class; | |||
62 | 54 | 385 | $cn_item = '' unless defined $cn_item; | |||
63 | 54 | 543 | my $init = uc "$cn_class $cn_item"; | |||
64 | 54 | 327 | $init =~ s/^\s+//; | |||
65 | 54 | 373 | $init =~ s/\s+$//; | |||
66 | 54 | 232 | $init =~ s!/!!g; | |||
67 | 54 0 0 0 | 1196 0 0 0 | $init =~ s/^([\p{IsAlpha}]+)/$1 /; | |||
68 | 54 | 325371 | my @tokens = split /\.|\s+/, $init; | |||
69 | 54 | 1039 | my $digit_group_count = 0; | |||
70 | for (my $i = 0; $i <= $#tokens; $i++) { | |||||
71 | 106 | 1809 | if ($tokens[$i] =~ /^\d+$/) { | |||
72 | 12 | 46 | $digit_group_count++; | |||
73 | 12 | 66 | if (2 == $digit_group_count) { | |||
74 | 6 | 33 | $tokens[$i] = sprintf("%-15.15s", $tokens[$i]); | |||
75 | 6 | 32 | $tokens[$i] =~ tr/ /0/; | |||
76 | } | |||||
77 | } | |||||
78 | 54 | 763 | } | |||
79 | # Pad the first digit_group if there was only one | |||||
80 | 54 | 756 | if (1 == $digit_group_count) { | |||
81 | 0 | 0 | $tokens[0] .= '_000000000000000' | |||
82 | } | |||||
83 | 54 | 683 | my $key = join("_", @tokens); | |||
84 | 54 | 1583 | $key =~ s/[^\p{IsAlnum}_]//g; | |||
85 | ||||||
86 | 54 | 222035 | return $key; | |||
87 | ||||||
88 | } | |||||
89 | ||||||
90 | 1; | |||||
91 | ||||||
92 - 96 | =head1 AUTHOR Koha Development Team <http://koha-community.org/> =cut | |||||
97 |