Bug 23820: Fix Koha/Club/Hold.t
[koha.git] / C4 / ClassSource.pm
blob8c133bce704dccde3ec144884bb75462cb6de754
1 package C4::ClassSource;
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 C4::Context;
25 use C4::ClassSortRoutine;
27 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
30 =head1 NAME
32 C4::ClassSources - handle classification sources in Koha
34 =head1 SYNOPSIS
36 use C4::ClassSource;
38 =head1 DESCRIPTION
40 This module deals with manipulating classification
41 sources and sorting rules.
43 =head1 FUNCTIONS
45 =cut
48 @ISA = qw(Exporter);
49 @EXPORT = qw(
50 &GetClassSources
51 &GetClassSource
52 &GetClassSortRule
54 &GetClassSort
58 =head2 GetClassSources
60 my $sources = GetClassSources();
62 Returns reference to hash of references to
63 the class sources, keyed on cn_source.
65 =head3 Example
67 my $sources = GetClassSources();
68 my @sources = ();
69 foreach my $cn_source (sort keys %$sources) {
70 my $source = $sources->{$cn_source};
71 push @sources,
73 code => $source->{'cn_source'},
74 description => $source->{'description'},
75 used => $source->{'used'},
76 sortrule => $source->{'class_sort_rule'}
80 =cut
82 sub GetClassSources {
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;
92 return \%class_sources;
96 =head2 GetClassSource
98 my $hashref = GetClassSource($cn_source);
100 Retrieves a class_sources row by cn_source.
102 =cut
104 sub GetClassSource {
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;
114 =head2 GetClassSortRule
116 my $hashref = GetClassSortRule($class_sort_rule);
118 Retrieves a class_sort_rules row by class_sort_rule.
120 =cut
122 sub GetClassSortRule {
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;
132 =head2 GetClassSort
134 my $cn_sort = GetClassSort($cn_source, $cn_class, $cn_item);
136 Get the sort key corresponding to the classification part and item part
137 and the defined call number source.
139 =cut
141 sub GetClassSort {
143 my ($cn_source, $cn_class, $cn_item) = @_;
145 my $source_ref = GetClassSource($cn_source);
146 unless (defined $source_ref) {
147 $source_ref = GetClassSource(C4::Context->preference("DefaultClassificationSource"));
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'};
157 return GetClassSortKey($routine, $cn_class, $cn_item);
163 =head1 AUTHOR
165 Koha Development Team <http://koha-community.org/>
167 =cut