Bug 7683: Relabel "acquired date" and "removed date"
[koha.git] / C4 / ClassSortRoutine / Dewey.pm
blob89262c33bfe9c640e76e40d3d733aacfc02f1d97
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
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>.
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!/!!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;
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'
88 # Pad the first digit_group if there was only one
89 if (1 == $digit_group_count) {
90 $tokens[$first_digit_group_idx] .= '_000000000000000'
92 my $key = join("_", @tokens);
93 $key =~ s/[^\p{IsAlnum}_]//g;
95 return $key;
101 =head1 AUTHOR
103 Koha Development Team <http://koha-community.org/>
105 =cut