1 package C4::ClassSortRoutine::Dewey;
3 # Copyright (C) 2007 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
28 C4::ClassSortRoutine::Dewey - generic call number sorting key routine
32 use C4::ClassSortRoutine;
34 my $cn_sort = GetClassSortKey('Dewey', $cn_class, $cn_item);
38 =head2 get_class_sort_key
40 my $cn_sort = C4::ClassSortRoutine::Dewey::Dewey($cn_class, $cn_item);
42 Generates sorting key using the following rules:
44 * Concatenates class and item part.
45 * Converts to uppercase.
46 * Removes leading and trailing whitespace and '/'
47 * Separates alphabetic prefix from the rest of the call number
48 * Splits into tokens on whitespaces and periods.
49 * Leaves first digit group as is.
50 * Converts second digit group to 15-digit long group, padded on right with zeroes.
51 * Converts each run of whitespace to an underscore.
52 * Removes any remaining non-alphabetical, non-numeric, non-underscore characters.
56 sub get_class_sort_key {
57 my ($cn_class, $cn_item) = @_;
59 $cn_class = '' unless defined $cn_class;
60 $cn_item = '' unless defined $cn_item;
61 my $init = uc "$cn_class $cn_item";
66 $init =~ s/^([\p{IsAlpha}]+)/$1 /;
67 my @tokens = split /\.|\s+/, $init;
68 my $digit_group_count = 0;
69 my $first_digit_group_idx;
70 for (my $i = 0; $i <= $#tokens; $i++) {
71 if ($tokens[$i] =~ /^\d+$/) {
73 if (1 == $digit_group_count) {
74 $first_digit_group_idx = $i;
76 if (2 == $digit_group_count) {
77 if ($i - $first_digit_group_idx == 1) {
78 $tokens[$i] = sprintf("%-15.15s", $tokens[$i]);
79 $tokens[$i] =~ tr/ /0/;
81 $tokens[$first_digit_group_idx] .= '_000000000000000'
86 # Pad the first digit_group if there was only one
87 if (1 == $digit_group_count) {
88 $tokens[$first_digit_group_idx] .= '_000000000000000'
90 my $key = join("_", @tokens);
91 $key =~ s/[^\p{IsAlnum}_]//g;
101 Koha Development Team <http://koha-community.org/>