| 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 | 22 22 22 | 536 76 750 | use strict; | |||
| 21 | 22 22 22 | 156 101 984 | use warnings; | |||
| 22 | ||||||
| 23 | 22 22 22 | 155 107 8872 | 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 | 26 | 120 | my ($cn_class, $cn_item) = @_; | |||
| 60 | ||||||
| 61 | 26 | 125 | $cn_class = '' unless defined $cn_class; | |||
| 62 | 26 | 92 | $cn_item = '' unless defined $cn_item; | |||
| 63 | 26 | 140 | my $init = uc "$cn_class $cn_item"; | |||
| 64 | 26 | 82 | $init =~ s/^\s+//; | |||
| 65 | 26 | 121 | $init =~ s/\s+$//; | |||
| 66 | 26 | 50 | $init =~ s!/!!g; | |||
| 67 | 26 0 0 0 | 574 0 0 0 | $init =~ s/^([\p{IsAlpha}]+)/$1 /; | |||
| 68 | 26 | 175747 | my @tokens = split /\.|\s+/, $init; | |||
| 69 | 26 | 313 | my $digit_group_count = 0; | |||
| 70 | for (my $i = 0; $i <= $#tokens; $i++) { | |||||
| 71 | 51 | 806 | if ($tokens[$i] =~ /^\d+$/) { | |||
| 72 | 6 | 7 | $digit_group_count++; | |||
| 73 | 6 | 17 | if (2 == $digit_group_count) { | |||
| 74 | 3 | 13 | $tokens[$i] = sprintf("%-15.15s", $tokens[$i]); | |||
| 75 | 3 | 10 | $tokens[$i] =~ tr/ /0/; | |||
| 76 | } | |||||
| 77 | } | |||||
| 78 | 26 | 196 | } | |||
| 79 | # Pad the first digit_group if there was only one | |||||
| 80 | 26 | 202 | if (1 == $digit_group_count) { | |||
| 81 | 0 | 0 | $tokens[0] .= '_000000000000000' | |||
| 82 | } | |||||
| 83 | 26 | 222 | my $key = join("_", @tokens); | |||
| 84 | 26 | 592 | $key =~ s/[^\p{IsAlnum}_]//g; | |||
| 85 | ||||||
| 86 | 26 | 148641 | return $key; | |||
| 87 | ||||||
| 88 | } | |||||
| 89 | ||||||
| 90 | 1; | |||||
| 91 | ||||||
| 92 - 96 | =head1 AUTHOR Koha Development Team <http://koha-community.org/> =cut | |||||
| 97 | ||||||