Bug 12598: Fix POD
[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
24
25 =head1 NAME 
26
27 C4::ClassSortRoutine::Dewey - generic call number sorting key routine
28
29 =head1 SYNOPSIS
30
31 use C4::ClassSortRoutine;
32
33 my $cn_sort = GetClassSortKey('Dewey', $cn_class, $cn_item);
34
35 =head1 FUNCTIONS
36
37 =head2 get_class_sort_key
38
39   my $cn_sort = C4::ClassSortRoutine::Dewey::Dewey($cn_class, $cn_item);
40
41 Generates sorting key using the following rules:
42
43 * Concatenates class and item part.
44 * Converts to uppercase.
45 * Removes leading and trailing whitespace and '/'
46 * Separates alphabetic prefix from the rest of the call number
47 * Splits into tokens on whitespaces and periods.
48 * Leaves first digit group as is.
49 * Converts second digit group to 15-digit long group, padded on right with zeroes.
50 * Converts each run of whitespace to an underscore.
51 * Removes any remaining non-alphabetical, non-numeric, non-underscore characters.
52
53 =cut
54
55 sub get_class_sort_key {
56     my ($cn_class, $cn_item) = @_;
57
58     $cn_class = '' unless defined $cn_class;
59     $cn_item  = '' unless defined $cn_item;
60     my $init = uc "$cn_class $cn_item";
61     $init =~ s/^\s+//;
62     $init =~ s/\s+$//;
63     $init =~ s/\// /g;
64     $init =~ s!/!!g;
65     $init =~ s/^([\p{IsAlpha}]+)/$1 /;
66     my @tokens = split /\.|\s+/, $init;
67     my $digit_group_count = 0;
68     my $first_digit_group_idx;
69     for (my $i = 0; $i <= $#tokens; $i++) {
70         if ($tokens[$i] =~ /^\d+$/) {
71             $digit_group_count++;
72             if (1 == $digit_group_count) {
73                 $first_digit_group_idx = $i;
74             }
75             if (2 == $digit_group_count) {
76                if ($i - $first_digit_group_idx == 1) {
77                     $tokens[$i] = sprintf("%-15.15s", $tokens[$i]);
78                     $tokens[$i] =~ tr/ /0/;
79                 } else {
80                     $tokens[$first_digit_group_idx] .= '_000000000000000'
81                 }
82             }
83         }
84     }
85     # Pad the first digit_group if there was only one
86     if (1 == $digit_group_count) {
87         $tokens[$first_digit_group_idx] .= '_000000000000000'
88     }
89     my $key = join("_", @tokens);
90     $key =~ s/[^\p{IsAlnum}_]//g;
91
92     return $key;
93
94 }
95
96 1;
97
98 =head1 AUTHOR
99
100 Koha Development Team <http://koha-community.org/>
101
102 =cut
103