Bug 23140: Fix typo in branchcode parameters for print slip
[koha.git] / C4 / ClassSource.pm
1 package C4::ClassSource;
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 C4::Context;
25 use C4::ClassSortRoutine;
26
27 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28
29
30 =head1 NAME
31
32 C4::ClassSources - handle classification sources in Koha
33
34 =head1 SYNOPSIS
35
36 use C4::ClassSource;
37
38 =head1 DESCRIPTION
39
40 This module deals with manipulating classification
41 sources and sorting rules.
42
43 =head1 FUNCTIONS
44
45 =cut
46
47
48 @ISA    = qw(Exporter);
49 @EXPORT = qw(
50     &GetClassSources
51     &GetClassSource
52     &GetClassSortRule
53
54     &GetClassSort
55
56 );
57
58 =head2 GetClassSources
59
60   my $sources = GetClassSources();
61
62   Returns reference to hash of references to
63   the class sources, keyed on cn_source.
64
65 =head3 Example
66
67 my $sources = GetClassSources();
68 my @sources = ();
69 foreach my $cn_source (sort keys %$sources) {
70     my $source = $sources->{$cn_source};
71     push @sources, 
72       {  
73         code        => $source->{'cn_source'},
74         description => $source->{'description'},
75         used => $source->{'used'},
76         sortrule    => $source->{'class_sort_rule'}
77       } 
78 }
79
80 =cut
81
82 sub GetClassSources {
83
84     my %class_sources = ();
85     my $dbh = C4::Context->dbh;
86     my $sth = $dbh->prepare("SELECT * FROM `class_sources`");
87     $sth->execute();
88     while (my $source = $sth->fetchrow_hashref) {
89         $class_sources{ $source->{'cn_source'} } = $source;
90     }
91
92     return \%class_sources;
93
94 }
95
96 =head2 GetClassSource
97
98   my $hashref = GetClassSource($cn_source);
99
100   Retrieves a class_sources row by cn_source.
101
102 =cut
103
104 sub GetClassSource {
105
106     my ($cn_source) = (@_);
107     my $dbh = C4::Context->dbh;
108     my $sth = $dbh->prepare("SELECT * FROM `class_sources` WHERE cn_source = ?");
109     $sth->execute($cn_source);
110     my $row = $sth->fetchrow_hashref();
111     return $row;
112 }
113
114 =head2 GetClassSortRule
115
116   my $hashref = GetClassSortRule($class_sort_rule);
117
118   Retrieves a class_sort_rules row by class_sort_rule.
119
120 =cut
121
122 sub GetClassSortRule {
123
124     my ($class_sort_rule) = (@_);
125     my $dbh = C4::Context->dbh;
126     my $sth = $dbh->prepare("SELECT * FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
127     $sth->execute($class_sort_rule);
128     my $row = $sth->fetchrow_hashref();
129     return $row;
130 }
131
132 =head2 GetClassSort
133
134   my $cn_sort = GetClassSort($cn_source, $cn_class, $cn_item);
135
136 Get the sort key corresponding to the classification part and item part
137 and the defined call number source.
138
139 =cut
140
141 sub GetClassSort {
142
143     my ($cn_source, $cn_class, $cn_item) = @_;
144
145     my $source_ref = GetClassSource($cn_source);
146     unless (defined $source_ref) {
147         $source_ref = GetClassSource(C4::Context->preference("DefaultClassificationSource"));
148     }
149     my $routine = "";
150     if (defined $source_ref) {
151         my $rule_ref = GetClassSortRule($source_ref->{'class_sort_rule'});
152         if (defined $rule_ref) {
153             $routine = $rule_ref->{'sort_routine'};
154         }
155     } 
156
157     return GetClassSortKey($routine, $cn_class, $cn_item);
158
159 }
160
161 1;
162
163 =head1 AUTHOR
164
165 Koha Development Team <http://koha-community.org/>
166
167 =cut