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