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