-
Notifications
You must be signed in to change notification settings - Fork 257
/
ClassSource.pm
179 lines (132 loc) · 4.58 KB
/
ClassSource.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package C4::ClassSource;
# Copyright 2022 Koha Development Team
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use C4::Context;
use C4::ClassSortRoutine qw( GetClassSortKey );
use Koha::Cache::Memory::Lite;
our (@ISA, @EXPORT_OK);
BEGIN {
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(
GetClassSources
GetClassSource
GetClassSortRule
GetClassSort
);
}
=head1 NAME
C4::ClassSources - handle classification sources in Koha
=head1 SYNOPSIS
use C4::ClassSource;
=head1 DESCRIPTION
This module deals with manipulating classification
sources and sorting rules.
=head1 FUNCTIONS
=cut
=head2 GetClassSources
my $sources = GetClassSources();
Returns reference to hash of references to
the class sources, keyed on cn_source.
=head3 Example
my $sources = GetClassSources();
my @sources = ();
foreach my $cn_source (sort keys %$sources) {
my $source = $sources->{$cn_source};
push @sources,
{
code => $source->{'cn_source'},
description => $source->{'description'},
used => $source->{'used'},
sortrule => $source->{'class_sort_rule'}
}
}
=cut
sub GetClassSources {
my $memory_cache = Koha::Cache::Memory::Lite->get_instance();
my $class_sources = $memory_cache->get_from_cache( "GetClassSource:All" );;
unless( $class_sources ){
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare("SELECT * FROM `class_sources`");
$sth->execute();
while (my $source = $sth->fetchrow_hashref) {
$class_sources->{ $source->{'cn_source'} } = $source;
}
$memory_cache->set_in_cache( "GetClassSource:All", $class_sources );
}
return $class_sources;
}
=head2 GetClassSource
my $hashref = GetClassSource($cn_source);
Retrieves a class_sources row by cn_source.
=cut
sub GetClassSource {
my ($cn_source) = (@_);
return unless $cn_source;
my $memory_cache = Koha::Cache::Memory::Lite->get_instance();
my $class_source = $memory_cache->get_from_cache( "GetClassSource:" . $cn_source );
unless( $class_source ){
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare("SELECT * FROM `class_sources` WHERE cn_source = ?");
$sth->execute($cn_source);
$class_source = $sth->fetchrow_hashref();
$memory_cache->set_in_cache( "GetClassSource:" . $cn_source, $class_source );
}
return $class_source;
}
=head2 GetClassSortRule
my $hashref = GetClassSortRule($class_sort_rule);
Retrieves a class_sort_rules row by class_sort_rule.
=cut
sub GetClassSortRule {
my ($class_sort_rule) = (@_);
return unless $class_sort_rule;
my $memory_cache = Koha::Cache::Memory::Lite->get_instance();
my $class_sort_rules = $memory_cache->get_from_cache( "GetClassSortRule:" . $class_sort_rule );
unless( $class_sort_rules ){
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare("SELECT * FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
$sth->execute($class_sort_rule);
$class_sort_rules = $sth->fetchrow_hashref();
$memory_cache->set_in_cache( "GetClassSortRule:" . $class_sort_rule, $class_sort_rules );
}
return $class_sort_rules;
}
=head2 GetClassSort
my $cn_sort = GetClassSort($cn_source, $cn_class, $cn_item);
Get the sort key corresponding to the classification part and item part
and the defined call number source.
=cut
sub GetClassSort {
my ($cn_source, $cn_class, $cn_item) = @_;
my $source_ref = GetClassSource($cn_source);
unless (defined $source_ref) {
$source_ref = GetClassSource(C4::Context->preference("DefaultClassificationSource"));
}
my $routine = "";
if (defined $source_ref) {
my $rule_ref = GetClassSortRule($source_ref->{'class_sort_rule'});
if (defined $rule_ref) {
$routine = $rule_ref->{'sort_routine'};
}
}
return GetClassSortKey($routine, $cn_class, $cn_item);
}
1;
=head1 AUTHOR
Koha Development Team <http://koha-community.org/>
=cut