Bug 12177 - Remove HTML from authorities.pl
[koha.git] / C4 / ClassSortRoutine / Dewey.pm
blob25480ec082da9fc8dd05147994f96a45ebdb8c64
1 package C4::ClassSortRoutine::Dewey;
3 # Copyright (C) 2007 LibLime
4 #
5 # This file is part of Koha.
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.
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.
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.
20 use strict;
21 use warnings;
23 use vars qw($VERSION);
25 # set the version for version checking
26 $VERSION = 3.07.00.049;
28 =head1 NAME
30 C4::ClassSortRoutine::Dewey - generic call number sorting key routine
32 =head1 SYNOPSIS
34 use C4::ClassSortRoutine;
36 my $cn_sort = GetClassSortKey('Dewey', $cn_class, $cn_item);
38 =head1 FUNCTIONS
40 =head2 get_class_sort_key
42 my $cn_sort = C4::ClassSortRoutine::Dewey::Dewey($cn_class, $cn_item);
44 Generates sorting key using the following rules:
46 * Concatenates class and item part.
47 * Converts to uppercase.
48 * Removes leading and trailing whitespace and '/'
49 * Separates alphabetic prefix from the rest of the call number
50 * Splits into tokens on whitespaces and periods.
51 * Leaves first digit group as is.
52 * Converts second digit group to 15-digit long group, padded on right with zeroes.
53 * Converts each run of whitespace to an underscore.
54 * Removes any remaining non-alphabetical, non-numeric, non-underscore characters.
56 =cut
58 sub get_class_sort_key {
59 my ($cn_class, $cn_item) = @_;
61 $cn_class = '' unless defined $cn_class;
62 $cn_item = '' unless defined $cn_item;
63 my $init = uc "$cn_class $cn_item";
64 $init =~ s/^\s+//;
65 $init =~ s/\s+$//;
66 $init =~ s!/!!g;
67 $init =~ s/^([\p{IsAlpha}]+)/$1 /;
68 my @tokens = split /\.|\s+/, $init;
69 my $digit_group_count = 0;
70 my $first_digit_group_idx;
71 for (my $i = 0; $i <= $#tokens; $i++) {
72 if ($tokens[$i] =~ /^\d+$/) {
73 $digit_group_count++;
74 if (1 == $digit_group_count) {
75 $first_digit_group_idx = $i;
77 if (2 == $digit_group_count) {
78 $tokens[$i] = sprintf("%-15.15s", $tokens[$i]);
79 $tokens[$i] =~ tr/ /0/;
83 # Pad the first digit_group if there was only one
84 if (1 == $digit_group_count) {
85 $tokens[$first_digit_group_idx] .= '_000000000000000'
87 my $key = join("_", @tokens);
88 $key =~ s/[^\p{IsAlnum}_]//g;
90 return $key;
96 =head1 AUTHOR
98 Koha Development Team <http://koha-community.org/>
100 =cut