bug 2505: enable warnings in call number normalization modules
[koha.git] / C4 / ClassSortRoutine.pm
1 package C4::ClassSortRoutine;
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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use warnings;
22
23 require Exporter;
24 use Class::Factory::Util;
25 use C4::Context;
26 use C4::Koha;
27
28 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
29
30 # set the version for version checking
31 $VERSION = 3.00;
32
33 =head1 NAME 
34
35 C4::ClassSortRoutine - base object for creation of classification sorting
36                        key generation routines
37
38 =head1 SYNOPSIS
39
40 use C4::ClassSortRoutine;
41
42 =head1 FUNCTIONS
43
44 =cut
45
46 @ISA    = qw(Exporter);
47 @EXPORT = qw(
48    &GetSortRoutineNames
49    &GetClassSortKey
50 );
51
52 # intialization code
53 my %loaded_routines = ();
54 my @sort_routines = GetSortRoutineNames();
55 foreach my $sort_routine (@sort_routines) {
56     if (eval "require C4::ClassSortRoutine::$sort_routine") {
57         my $ref;
58         eval "\$ref = \\\&C4::ClassSortRoutine::${sort_routine}::get_class_sort_key";
59         if (eval "\$ref->(\"a\", \"b\")") {
60             $loaded_routines{$sort_routine} = $ref;
61         } else {
62             $loaded_routines{$sort_routine} = \&_get_class_sort_key;
63         }
64     } else {
65         $loaded_routines{$sort_routine} = \&_get_class_sort_key;
66     }
67 }
68
69 =head2 GetSortRoutineNames
70
71   my @routines = GetSortRoutineNames();
72
73 Get names of all modules under C4::ClassSortRoutine::*.  Adding
74 a new classification sorting routine can therefore be done 
75 simply by writing a new submodule under C4::ClassSortRoutine and
76 placing it in the C4/ClassSortRoutine directory.
77
78 =cut
79
80 sub GetSortRoutineNames {
81     return C4::ClassSortRoutine->subclasses();
82 }
83
84 =head2  GetClassSortKey
85
86   my $cn_sort = GetClassSortKey($sort_routine, $cn_class, $cn_item);
87
88 Generates classification sorting key.  If $sort_routine does not point
89 to a valid submodule in C4::ClassSortRoutine, default to a basic
90 normalization routine.
91
92 =cut
93
94 sub GetClassSortKey {
95     my ($sort_routine, $cn_class, $cn_item) = @_;
96     unless (exists $loaded_routines{$sort_routine}) {
97         warn "attempting to use non-existent class sorting routine $sort_routine\n";
98         $loaded_routines{$sort_routine} = \&_get_class_sort_key;
99     }
100     my $key = $loaded_routines{$sort_routine}->($cn_class, $cn_item);
101     # FIXME -- hardcoded length for cn_sort
102     # should replace with some way of getting column widths from
103     # the DB schema -- since doing this should ideally be
104     # independent of the DBMS, deferring for the moment.
105     return substr($key, 0, 30);
106 }
107
108 =head2 _get_class_sort_key 
109
110 Basic sorting function.  Concatenates classification part 
111 and item, converts to uppercase, changes each run of
112 whitespace to '_', and removes any non-digit, non-latin
113 letter characters.
114
115 =cut
116
117 sub _get_class_sort_key {
118     my ($cn_class, $cn_item) = @_;
119     my $key = uc "$cn_class $cn_item";
120     $key =~ s/\s+/_/;
121     $key =~ s/[^A-Z_0-9]//g;
122     return $key;
123 }
124
125 1;
126
127 =head1 AUTHOR
128
129 Koha Developement team <info@koha.org>
130
131 =cut
132