Bug 23049: Add CHECK constraint to accountlines
[koha.git] / C4 / ClassSortRoutine.pm
blobd5fd8a092efb687827d0b5a2404b19ba1ed0b174
1 package C4::ClassSortRoutine;
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 require Exporter;
24 use Class::Factory::Util;
25 use C4::Context;
27 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
30 =head1 NAME
32 C4::ClassSortRoutine - base object for creation of classification sorting
33 key generation routines
35 =head1 SYNOPSIS
37 use C4::ClassSortRoutine;
39 =head1 FUNCTIONS
41 =cut
43 @ISA = qw(Exporter);
44 @EXPORT = qw(
45 &GetSortRoutineNames
46 &GetClassSortKey
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;
61 } else {
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.
75 =cut
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.
89 =cut
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;
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
110 letter characters.
112 =cut
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;
124 =head1 AUTHOR
126 Koha Development Team <http://koha-community.org/>
128 =cut