1 package C4::ClassSortRoutine;
3 # Copyright (C) 2007 LibLime
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>.
23 use Class::Factory::Util;
26 our (@ISA, @EXPORT_OK);
38 C4::ClassSortRoutine - base object for creation of classification sorting
39 key generation routines
43 use C4::ClassSortRoutine;
50 my %loaded_routines = ();
51 my @sort_routines = GetSortRoutineNames();
52 foreach my $sort_routine (@sort_routines) {
53 if (eval "require C4::ClassSortRoutine::$sort_routine") {
55 $ref = \&{"C4::ClassSortRoutine::${sort_routine}::get_class_sort_key"};
56 if (eval { $ref->("a", "b") }) {
57 $loaded_routines{$sort_routine} = $ref;
59 $loaded_routines{$sort_routine} = \&_get_class_sort_key;
62 $loaded_routines{$sort_routine} = \&_get_class_sort_key;
66 =head2 GetSortRoutineNames
68 my @routines = GetSortRoutineNames();
70 Get names of all modules under C4::ClassSortRoutine::*. Adding
71 a new classification sorting routine can therefore be done
72 simply by writing a new submodule under C4::ClassSortRoutine and
73 placing it in the C4/ClassSortRoutine directory.
77 sub GetSortRoutineNames {
78 return C4::ClassSortRoutine->subclasses();
81 =head2 GetClassSortKey
83 my $cn_sort = GetClassSortKey($sort_routine, $cn_class, $cn_item);
85 Generates classification sorting key. If $sort_routine does not point
86 to a valid submodule in C4::ClassSortRoutine, default to a basic
87 normalization routine.
92 my ($sort_routine, $cn_class, $cn_item) = @_;
93 unless (exists $loaded_routines{$sort_routine}) {
94 warn "attempting to use non-existent class sorting routine $sort_routine\n";
95 $loaded_routines{$sort_routine} = \&_get_class_sort_key;
97 my $key = $loaded_routines{$sort_routine}->($cn_class, $cn_item);
98 # FIXME -- hardcoded length for cn_sort
99 # should replace with some way of getting column widths from
100 # the DB schema -- since doing this should ideally be
101 # independent of the DBMS, deferring for the moment.
102 return substr($key, 0, 255);
105 =head2 _get_class_sort_key
107 Basic sorting function. Concatenates classification part
108 and item, converts to uppercase, changes each run of
109 whitespace to '_', and removes any non-digit, non-latin
114 sub _get_class_sort_key {
115 my ($cn_class, $cn_item) = @_;
116 my $key = uc "$cn_class $cn_item";
118 $key =~ s/[^A-Z_0-9]//g;
126 Koha Development Team <http://koha-community.org/>