Update release notes for 19.05.02 release
[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
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 require Exporter;
24 use Class::Factory::Util;
25 use C4::Context;
26
27 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28
29
30 =head1 NAME 
31
32 C4::ClassSortRoutine - base object for creation of classification sorting
33                        key generation routines
34
35 =head1 SYNOPSIS
36
37 use C4::ClassSortRoutine;
38
39 =head1 FUNCTIONS
40
41 =cut
42
43 @ISA    = qw(Exporter);
44 @EXPORT = qw(
45    &GetSortRoutineNames
46    &GetClassSortKey
47 );
48
49 # initialization code
50 my %loaded_routines = ();
51 my @sort_routines = GetSortRoutineNames();
52 foreach my $sort_routine (@sort_routines) {
53     if (eval "require C4::ClassSortRoutine::$sort_routine") {
54         my $ref;
55         eval "\$ref = \\\&C4::ClassSortRoutine::${sort_routine}::get_class_sort_key";
56         if (eval "\$ref->(\"a\", \"b\")") {
57             $loaded_routines{$sort_routine} = $ref;
58         } else {
59             $loaded_routines{$sort_routine} = \&_get_class_sort_key;
60         }
61     } else {
62         $loaded_routines{$sort_routine} = \&_get_class_sort_key;
63     }
64 }
65
66 =head2 GetSortRoutineNames
67
68   my @routines = GetSortRoutineNames();
69
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.
74
75 =cut
76
77 sub GetSortRoutineNames {
78     return C4::ClassSortRoutine->subclasses();
79 }
80
81 =head2  GetClassSortKey
82
83   my $cn_sort = GetClassSortKey($sort_routine, $cn_class, $cn_item);
84
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.
88
89 =cut
90
91 sub GetClassSortKey {
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;
96     }
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);
103 }
104
105 =head2 _get_class_sort_key 
106
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
110 letter characters.
111
112 =cut
113
114 sub _get_class_sort_key {
115     my ($cn_class, $cn_item) = @_;
116     my $key = uc "$cn_class $cn_item";
117     $key =~ s/\s+/_/;
118     $key =~ s/[^A-Z_0-9]//g;
119     return $key;
120 }
121
122 1;
123
124 =head1 AUTHOR
125
126 Koha Development Team <http://koha-community.org/>
127
128 =cut
129