Bug 6679: (follow-up) fix 9 perlcritic violations in C4/TmplTokenType.pm
[koha.git] / C4 / ClassSortRoutine / Dewey.pm
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
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.
11 #
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.
16 #
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>.
19
20 use strict;
21 use warnings;
22
23 use vars qw($VERSION);
24
25 # set the version for version checking
26 $VERSION = 3.07.00.049;
27
28 =head1 NAME 
29
30 C4::ClassSortRoutine::Dewey - generic call number sorting key routine
31
32 =head1 SYNOPSIS
33
34 use C4::ClassSortRoutine;
35
36 my $cn_sort = GetClassSortKey('Dewey', $cn_class, $cn_item);
37
38 =head1 FUNCTIONS
39
40 =head2 get_class_sort_key
41
42   my $cn_sort = C4::ClassSortRoutine::Dewey::Dewey($cn_class, $cn_item);
43
44 Generates sorting key using the following rules:
45
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.
55
56 =cut
57
58 sub get_class_sort_key {
59     my ($cn_class, $cn_item) = @_;
60
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!/!!g;
68     $init =~ s/^([\p{IsAlpha}]+)/$1 /;
69     my @tokens = split /\.|\s+/, $init;
70     my $digit_group_count = 0;
71     my $first_digit_group_idx;
72     for (my $i = 0; $i <= $#tokens; $i++) {
73         if ($tokens[$i] =~ /^\d+$/) {
74             $digit_group_count++;
75             if (1 == $digit_group_count) {
76                 $first_digit_group_idx = $i;
77             }
78             if (2 == $digit_group_count) {
79                if ($i - $first_digit_group_idx == 1) {
80                     $tokens[$i] = sprintf("%-15.15s", $tokens[$i]);
81                     $tokens[$i] =~ tr/ /0/;
82                 } else {
83                     $tokens[$first_digit_group_idx] .= '_000000000000000'
84                 }
85             }
86         }
87     }
88     # Pad the first digit_group if there was only one
89     if (1 == $digit_group_count) {
90         $tokens[$first_digit_group_idx] .= '_000000000000000'
91     }
92     my $key = join("_", @tokens);
93     $key =~ s/[^\p{IsAlnum}_]//g;
94
95     return $key;
96
97 }
98
99 1;
100
101 =head1 AUTHOR
102
103 Koha Development Team <http://koha-community.org/>
104
105 =cut
106