Bug 2576: Added OPACFinesTab Syspref
[koha.git] / C4 / ClassSource.pm
blobee468acaa7f7cd51b610f7a9cfd33867dd26cf41
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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
20 use strict;
21 use warnings;
23 require Exporter;
24 use C4::Context;
25 use C4::Koha;
26 use C4::ClassSortRoutine;
28 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
30 # set the version for version checking
31 $VERSION = 3.00;
33 =head1 NAME
35 C4::ClassSources - handle classification sources in Koha
37 =head1 SYNOPSIS
39 use C4::ClassSource;
41 =head1 DESCRIPTION
43 This module deals with manipulating classification
44 sources and sorting rules.
46 =head1 FUNCTIONS
48 =cut
51 @ISA = qw(Exporter);
52 @EXPORT = qw(
53 &GetClassSources
54 &AddClassSource
55 &GetClassSource
56 &ModClassSource
57 &DelClassSource
58 &GetClassSortRules
59 &AddClassSortRule
60 &GetClassSortRule
61 &ModClassSortRule
62 &DelClassSortRule
64 &GetSourcesForSortRule
65 &GetClassSort
69 =head2 GetClassSources
71 my $sources = GetClassSources();
73 Returns reference to hash of references to
74 the class sources, keyed on cn_source.
76 =head3 Example
78 my $sources = GetClassSources();
79 my @sources = ();
80 foreach my $cn_source (sort keys %$sources) {
81 my $source = $sources->{$cn_source};
82 push @sources,
84 code => $source->{'cn_source'},
85 description => $source->{'description'},
86 used => $source->{'used'},
87 sortrule => $source->{'class_sort_rule'}
91 =cut
93 sub GetClassSources {
95 my %class_sources = ();
96 my $dbh = C4::Context->dbh;
97 my $sth = $dbh->prepare_cached("SELECT * FROM `class_sources`");
98 $sth->execute();
99 while (my $source = $sth->fetchrow_hashref) {
100 $class_sources{ $source->{'cn_source'} } = $source;
102 $sth->finish();
104 return \%class_sources;
108 =head2 AddClassSource
110 AddClassSource($cn_source, $description, $used, $class_sort_rule);
112 Adds a class_sources row.
114 =cut
116 sub AddClassSource {
118 my ($cn_source, $description, $used, $class_sort_rule) = @_;
119 my $dbh = C4::Context->dbh;
120 my $sth = $dbh->prepare_cached("INSERT INTO `class_sources`
121 (`cn_source`, `description`, `used`, `class_sort_rule`)
122 VALUES (?, ?, ?, ?)");
123 $sth->execute($cn_source, $description, $used, $class_sort_rule);
124 $sth->finish();
128 =head2 GetClassSource
130 my $hashref = GetClassSource($cn_source);
132 Retrieves a class_sources row by cn_source.
134 =cut
136 sub GetClassSource {
138 my ($cn_source) = (@_);
139 my $dbh = C4::Context->dbh;
140 my $sth = $dbh->prepare_cached("SELECT * FROM `class_sources` WHERE cn_source = ?");
141 $sth->execute($cn_source);
142 my $row = $sth->fetchrow_hashref();
143 $sth->finish();
144 return $row;
147 =head2 ModClassSource
149 ModClassSource($cn_source, $description, $used, $class_sort_rule);
151 Updates a class_sources row.
153 =cut
155 sub ModClassSource {
157 my ($cn_source, $description, $used, $class_sort_rule) = @_;
158 my $dbh = C4::Context->dbh;
159 my $sth = $dbh->prepare_cached("UPDATE `class_sources`
160 SET `description` = ?,
161 `used` = ?,
162 `class_sort_rule` = ?
163 WHERE `cn_source` = ?");
164 $sth->execute($description, $used, $class_sort_rule, $cn_source);
165 $sth->finish();
169 =head2 DelClassSource
171 DelClassSource($cn_source);
173 Deletes class_sources row.
175 =cut
177 sub DelClassSource {
179 my ($cn_source) = @_;
180 my $dbh = C4::Context->dbh;
181 my $sth = $dbh->prepare_cached("DELETE FROM `class_sources` WHERE `cn_source` = ?");
182 $sth->execute($cn_source);
183 $sth->finish();
187 =head2 GetClassSortRules
189 my $sort_rules = GetClassSortRules();
191 Returns reference to hash of references to
192 the class sorting rules, keyed on class_sort_rule
194 =head3 Example
196 my $sort_rules = GetClassSortRules();
197 my @sort_rules = ();
198 foreach my $sort_rule (sort keys %$sort_rules) {
199 my $sort_rule = $sort_rules->{$sort_rule};
200 push @sort_rules,
202 rule => $sort_rule->{'class_sort_rule'},
203 description => $sort_rule->{'description'},
204 sort_routine => $sort_rule->{'sort_routine'}
208 =cut
210 sub GetClassSortRules {
212 my %class_sort_rules = ();
213 my $dbh = C4::Context->dbh;
214 my $sth = $dbh->prepare_cached("SELECT * FROM `class_sort_rules`");
215 $sth->execute();
216 while (my $sort_rule = $sth->fetchrow_hashref) {
217 $class_sort_rules{ $sort_rule->{'class_sort_rule'} } = $sort_rule;
219 $sth->finish();
221 return \%class_sort_rules;
225 =head2 AddClassSortRule
227 AddClassSortRule($class_sort_rule, $description, $sort_routine);
229 Adds a class_sort_rules row.
231 =cut
233 sub AddClassSortRule {
235 my ($class_sort_rule, $description, $sort_routine) = @_;
236 my $dbh = C4::Context->dbh;
237 my $sth = $dbh->prepare_cached("INSERT INTO `class_sort_rules`
238 (`class_sort_rule`, `description`, `sort_routine`)
239 VALUES (?, ?, ?)");
240 $sth->execute($class_sort_rule, $description, $sort_routine);
241 $sth->finish();
245 =head2 GetClassSortRule
247 my $hashref = GetClassSortRule($class_sort_rule);
249 Retrieves a class_sort_rules row by class_sort_rule.
251 =cut
253 sub GetClassSortRule {
255 my ($class_sort_rule) = (@_);
256 my $dbh = C4::Context->dbh;
257 my $sth = $dbh->prepare_cached("SELECT * FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
258 $sth->execute($class_sort_rule);
259 my $row = $sth->fetchrow_hashref();
260 $sth->finish();
261 return $row;
264 =head2 ModClassSortRule
266 ModClassSortRule($class_sort_rule, $description, $sort_routine);
268 Updates a class_sort_rules row.
270 =cut
272 sub ModClassSortRule {
274 my ($class_sort_rule, $description, $sort_routine) = @_;
275 my $dbh = C4::Context->dbh;
276 my $sth = $dbh->prepare_cached("UPDATE `class_sort_rules`
277 SET `description` = ?,
278 `sort_routine` = ?
279 WHERE `class_sort_rule` = ?");
280 $sth->execute($description, $sort_routine, $class_sort_rule);
281 $sth->finish();
285 =head2 DelClassSortRule
287 DelClassSortRule($class_sort_rule);
289 Deletes class_sort_rules row.
291 =cut
293 sub DelClassSortRule {
295 my ($class_sort_rule) = @_;
296 my $dbh = C4::Context->dbh;
297 my $sth = $dbh->prepare_cached("DELETE FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
298 $sth->execute($class_sort_rule);
299 $sth->finish();
303 =head2 GetSourcesForSortRule
305 my @source = GetSourcesForSortRule($class_sort_rule);
307 Retrieves an array class_source.cn_rule for each source
308 that uses the supplied $class_sort_rule.
310 =cut
312 sub GetSourcesForSortRule {
314 my ($class_sort_rule) = @_;
316 my $dbh = C4::Context->dbh;
317 my $sth = $dbh->prepare_cached("SELECT cn_source FROM class_sources WHERE class_sort_rule = ?");
318 $sth->execute($class_sort_rule);
319 my @sources = ();
320 while (my ($source) = $sth->fetchrow_array()) {
321 push @sources, $source;
323 $sth->finish();
324 return @sources;
328 =head2 GetClassSort
330 my $cn_sort = GetClassSort($cn_source, $cn_class, $cn_item);
332 Get the sort key corresponding to the classification part and item part
333 and the defined call number source.
335 =cut
337 sub GetClassSort {
339 my ($cn_source, $cn_class, $cn_item) = @_;
341 my $source_ref = GetClassSource($cn_source);
342 unless (defined $source_ref) {
343 $source_ref = GetClassSource(C4::Context->preference("DefaultClassificationSource"));
345 my $routine = "";
346 if (defined $source_ref) {
347 my $rule_ref = GetClassSortRule($source_ref->{'class_sort_rule'});
348 if (defined $rule_ref) {
349 $routine = $rule_ref->{'sort_routine'};
353 return GetClassSortKey($routine, $cn_class, $cn_item);
359 =head1 AUTHOR
361 Koha Developement team <info@koha.org>
363 =cut