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