Bug 7753: serials-home displays library name instead of code
[koha.git] / C4 / ClassSource.pm
blob5d76497cf26871f7e8ca29c6d18ff9157afafc33
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use strict;
21 use warnings;
23 require Exporter;
24 use C4::Context;
25 use C4::ClassSortRoutine;
27 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
29 # set the version for version checking
30 $VERSION = 3.00;
32 =head1 NAME
34 C4::ClassSources - handle classification sources in Koha
36 =head1 SYNOPSIS
38 use C4::ClassSource;
40 =head1 DESCRIPTION
42 This module deals with manipulating classification
43 sources and sorting rules.
45 =head1 FUNCTIONS
47 =cut
50 @ISA = qw(Exporter);
51 @EXPORT = qw(
52 &GetClassSources
53 &AddClassSource
54 &GetClassSource
55 &ModClassSource
56 &DelClassSource
57 &GetClassSortRules
58 &AddClassSortRule
59 &GetClassSortRule
60 &ModClassSortRule
61 &DelClassSortRule
63 &GetSourcesForSortRule
64 &GetClassSort
68 =head2 GetClassSources
70 my $sources = GetClassSources();
72 Returns reference to hash of references to
73 the class sources, keyed on cn_source.
75 =head3 Example
77 my $sources = GetClassSources();
78 my @sources = ();
79 foreach my $cn_source (sort keys %$sources) {
80 my $source = $sources->{$cn_source};
81 push @sources,
83 code => $source->{'cn_source'},
84 description => $source->{'description'},
85 used => $source->{'used'},
86 sortrule => $source->{'class_sort_rule'}
90 =cut
92 sub GetClassSources {
94 my %class_sources = ();
95 my $dbh = C4::Context->dbh;
96 my $sth = $dbh->prepare_cached("SELECT * FROM `class_sources`");
97 $sth->execute();
98 while (my $source = $sth->fetchrow_hashref) {
99 $class_sources{ $source->{'cn_source'} } = $source;
101 $sth->finish();
103 return \%class_sources;
107 =head2 AddClassSource
109 AddClassSource($cn_source, $description, $used, $class_sort_rule);
111 Adds a class_sources row.
113 =cut
115 sub AddClassSource {
117 my ($cn_source, $description, $used, $class_sort_rule) = @_;
118 my $dbh = C4::Context->dbh;
119 my $sth = $dbh->prepare_cached("INSERT INTO `class_sources`
120 (`cn_source`, `description`, `used`, `class_sort_rule`)
121 VALUES (?, ?, ?, ?)");
122 $sth->execute($cn_source, $description, $used, $class_sort_rule);
123 $sth->finish();
127 =head2 GetClassSource
129 my $hashref = GetClassSource($cn_source);
131 Retrieves a class_sources row by cn_source.
133 =cut
135 sub GetClassSource {
137 my ($cn_source) = (@_);
138 my $dbh = C4::Context->dbh;
139 my $sth = $dbh->prepare_cached("SELECT * FROM `class_sources` WHERE cn_source = ?");
140 $sth->execute($cn_source);
141 my $row = $sth->fetchrow_hashref();
142 $sth->finish();
143 return $row;
146 =head2 ModClassSource
148 ModClassSource($cn_source, $description, $used, $class_sort_rule);
150 Updates a class_sources row.
152 =cut
154 sub ModClassSource {
156 my ($cn_source, $description, $used, $class_sort_rule) = @_;
157 my $dbh = C4::Context->dbh;
158 my $sth = $dbh->prepare_cached("UPDATE `class_sources`
159 SET `description` = ?,
160 `used` = ?,
161 `class_sort_rule` = ?
162 WHERE `cn_source` = ?");
163 $sth->execute($description, $used, $class_sort_rule, $cn_source);
164 $sth->finish();
168 =head2 DelClassSource
170 DelClassSource($cn_source);
172 Deletes class_sources row.
174 =cut
176 sub DelClassSource {
178 my ($cn_source) = @_;
179 my $dbh = C4::Context->dbh;
180 my $sth = $dbh->prepare_cached("DELETE FROM `class_sources` WHERE `cn_source` = ?");
181 $sth->execute($cn_source);
182 $sth->finish();
186 =head2 GetClassSortRules
188 my $sort_rules = GetClassSortRules();
190 Returns reference to hash of references to
191 the class sorting rules, keyed on class_sort_rule
193 =head3 Example
195 my $sort_rules = GetClassSortRules();
196 my @sort_rules = ();
197 foreach my $sort_rule (sort keys %$sort_rules) {
198 my $sort_rule = $sort_rules->{$sort_rule};
199 push @sort_rules,
201 rule => $sort_rule->{'class_sort_rule'},
202 description => $sort_rule->{'description'},
203 sort_routine => $sort_rule->{'sort_routine'}
207 =cut
209 sub GetClassSortRules {
211 my %class_sort_rules = ();
212 my $dbh = C4::Context->dbh;
213 my $sth = $dbh->prepare_cached("SELECT * FROM `class_sort_rules`");
214 $sth->execute();
215 while (my $sort_rule = $sth->fetchrow_hashref) {
216 $class_sort_rules{ $sort_rule->{'class_sort_rule'} } = $sort_rule;
218 $sth->finish();
220 return \%class_sort_rules;
224 =head2 AddClassSortRule
226 AddClassSortRule($class_sort_rule, $description, $sort_routine);
228 Adds a class_sort_rules row.
230 =cut
232 sub AddClassSortRule {
234 my ($class_sort_rule, $description, $sort_routine) = @_;
235 my $dbh = C4::Context->dbh;
236 my $sth = $dbh->prepare_cached("INSERT INTO `class_sort_rules`
237 (`class_sort_rule`, `description`, `sort_routine`)
238 VALUES (?, ?, ?)");
239 $sth->execute($class_sort_rule, $description, $sort_routine);
240 $sth->finish();
244 =head2 GetClassSortRule
246 my $hashref = GetClassSortRule($class_sort_rule);
248 Retrieves a class_sort_rules row by class_sort_rule.
250 =cut
252 sub GetClassSortRule {
254 my ($class_sort_rule) = (@_);
255 my $dbh = C4::Context->dbh;
256 my $sth = $dbh->prepare_cached("SELECT * FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
257 $sth->execute($class_sort_rule);
258 my $row = $sth->fetchrow_hashref();
259 $sth->finish();
260 return $row;
263 =head2 ModClassSortRule
265 ModClassSortRule($class_sort_rule, $description, $sort_routine);
267 Updates a class_sort_rules row.
269 =cut
271 sub ModClassSortRule {
273 my ($class_sort_rule, $description, $sort_routine) = @_;
274 my $dbh = C4::Context->dbh;
275 my $sth = $dbh->prepare_cached("UPDATE `class_sort_rules`
276 SET `description` = ?,
277 `sort_routine` = ?
278 WHERE `class_sort_rule` = ?");
279 $sth->execute($description, $sort_routine, $class_sort_rule);
280 $sth->finish();
284 =head2 DelClassSortRule
286 DelClassSortRule($class_sort_rule);
288 Deletes class_sort_rules row.
290 =cut
292 sub DelClassSortRule {
294 my ($class_sort_rule) = @_;
295 my $dbh = C4::Context->dbh;
296 my $sth = $dbh->prepare_cached("DELETE FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
297 $sth->execute($class_sort_rule);
298 $sth->finish();
302 =head2 GetSourcesForSortRule
304 my @source = GetSourcesForSortRule($class_sort_rule);
306 Retrieves an array class_source.cn_rule for each source
307 that uses the supplied $class_sort_rule.
309 =cut
311 sub GetSourcesForSortRule {
313 my ($class_sort_rule) = @_;
315 my $dbh = C4::Context->dbh;
316 my $sth = $dbh->prepare_cached("SELECT cn_source FROM class_sources WHERE class_sort_rule = ?");
317 $sth->execute($class_sort_rule);
318 my @sources = ();
319 while (my ($source) = $sth->fetchrow_array()) {
320 push @sources, $source;
322 $sth->finish();
323 return @sources;
327 =head2 GetClassSort
329 my $cn_sort = GetClassSort($cn_source, $cn_class, $cn_item);
331 Get the sort key corresponding to the classification part and item part
332 and the defined call number source.
334 =cut
336 sub GetClassSort {
338 my ($cn_source, $cn_class, $cn_item) = @_;
340 my $source_ref = GetClassSource($cn_source);
341 unless (defined $source_ref) {
342 $source_ref = GetClassSource(C4::Context->preference("DefaultClassificationSource"));
344 my $routine = "";
345 if (defined $source_ref) {
346 my $rule_ref = GetClassSortRule($source_ref->{'class_sort_rule'});
347 if (defined $rule_ref) {
348 $routine = $rule_ref->{'sort_routine'};
352 return GetClassSortKey($routine, $cn_class, $cn_item);
358 =head1 AUTHOR
360 Koha Development Team <http://koha-community.org/>
362 =cut