Bug 15200 - t/Creators.t fails when using build-git-snapshot
[koha.git] / C4 / ClassSource.pm
blobbb7807471b059f3887628e9feb732a48d2418fe1
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 &AddClassSource
52 &GetClassSource
53 &ModClassSource
54 &DelClassSource
55 &GetClassSortRules
56 &AddClassSortRule
57 &GetClassSortRule
58 &ModClassSortRule
59 &DelClassSortRule
61 &GetSourcesForSortRule
62 &GetClassSort
66 =head2 GetClassSources
68 my $sources = GetClassSources();
70 Returns reference to hash of references to
71 the class sources, keyed on cn_source.
73 =head3 Example
75 my $sources = GetClassSources();
76 my @sources = ();
77 foreach my $cn_source (sort keys %$sources) {
78 my $source = $sources->{$cn_source};
79 push @sources,
81 code => $source->{'cn_source'},
82 description => $source->{'description'},
83 used => $source->{'used'},
84 sortrule => $source->{'class_sort_rule'}
88 =cut
90 sub GetClassSources {
92 my %class_sources = ();
93 my $dbh = C4::Context->dbh;
94 my $sth = $dbh->prepare("SELECT * FROM `class_sources`");
95 $sth->execute();
96 while (my $source = $sth->fetchrow_hashref) {
97 $class_sources{ $source->{'cn_source'} } = $source;
100 return \%class_sources;
104 =head2 AddClassSource
106 AddClassSource($cn_source, $description, $used, $class_sort_rule);
108 Adds a class_sources row.
110 =cut
112 sub AddClassSource {
114 my ($cn_source, $description, $used, $class_sort_rule) = @_;
115 my $dbh = C4::Context->dbh;
116 my $sth = $dbh->prepare("INSERT INTO `class_sources`
117 (`cn_source`, `description`, `used`, `class_sort_rule`)
118 VALUES (?, ?, ?, ?)");
119 $sth->execute($cn_source, $description, $used, $class_sort_rule);
123 =head2 GetClassSource
125 my $hashref = GetClassSource($cn_source);
127 Retrieves a class_sources row by cn_source.
129 =cut
131 sub GetClassSource {
133 my ($cn_source) = (@_);
134 my $dbh = C4::Context->dbh;
135 my $sth = $dbh->prepare("SELECT * FROM `class_sources` WHERE cn_source = ?");
136 $sth->execute($cn_source);
137 my $row = $sth->fetchrow_hashref();
138 return $row;
141 =head2 ModClassSource
143 ModClassSource($cn_source, $description, $used, $class_sort_rule);
145 Updates a class_sources row.
147 =cut
149 sub ModClassSource {
151 my ($cn_source, $description, $used, $class_sort_rule) = @_;
152 my $dbh = C4::Context->dbh;
153 my $sth = $dbh->prepare("UPDATE `class_sources`
154 SET `description` = ?,
155 `used` = ?,
156 `class_sort_rule` = ?
157 WHERE `cn_source` = ?");
158 $sth->execute($description, $used, $class_sort_rule, $cn_source);
162 =head2 DelClassSource
164 DelClassSource($cn_source);
166 Deletes class_sources row.
168 =cut
170 sub DelClassSource {
172 my ($cn_source) = @_;
173 my $dbh = C4::Context->dbh;
174 my $sth = $dbh->prepare("DELETE FROM `class_sources` WHERE `cn_source` = ?");
175 $sth->execute($cn_source);
179 =head2 GetClassSortRules
181 my $sort_rules = GetClassSortRules();
183 Returns reference to hash of references to
184 the class sorting rules, keyed on class_sort_rule
186 =head3 Example
188 my $sort_rules = GetClassSortRules();
189 my @sort_rules = ();
190 foreach my $sort_rule (sort keys %$sort_rules) {
191 my $sort_rule = $sort_rules->{$sort_rule};
192 push @sort_rules,
194 rule => $sort_rule->{'class_sort_rule'},
195 description => $sort_rule->{'description'},
196 sort_routine => $sort_rule->{'sort_routine'}
200 =cut
202 sub GetClassSortRules {
204 my %class_sort_rules = ();
205 my $dbh = C4::Context->dbh;
206 my $sth = $dbh->prepare("SELECT * FROM `class_sort_rules`");
207 $sth->execute();
208 while (my $sort_rule = $sth->fetchrow_hashref) {
209 $class_sort_rules{ $sort_rule->{'class_sort_rule'} } = $sort_rule;
212 return \%class_sort_rules;
216 =head2 AddClassSortRule
218 AddClassSortRule($class_sort_rule, $description, $sort_routine);
220 Adds a class_sort_rules row.
222 =cut
224 sub AddClassSortRule {
226 my ($class_sort_rule, $description, $sort_routine) = @_;
227 my $dbh = C4::Context->dbh;
228 my $sth = $dbh->prepare("INSERT INTO `class_sort_rules`
229 (`class_sort_rule`, `description`, `sort_routine`)
230 VALUES (?, ?, ?)");
231 $sth->execute($class_sort_rule, $description, $sort_routine);
235 =head2 GetClassSortRule
237 my $hashref = GetClassSortRule($class_sort_rule);
239 Retrieves a class_sort_rules row by class_sort_rule.
241 =cut
243 sub GetClassSortRule {
245 my ($class_sort_rule) = (@_);
246 my $dbh = C4::Context->dbh;
247 my $sth = $dbh->prepare("SELECT * FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
248 $sth->execute($class_sort_rule);
249 my $row = $sth->fetchrow_hashref();
250 return $row;
253 =head2 ModClassSortRule
255 ModClassSortRule($class_sort_rule, $description, $sort_routine);
257 Updates a class_sort_rules row.
259 =cut
261 sub ModClassSortRule {
263 my ($class_sort_rule, $description, $sort_routine) = @_;
264 my $dbh = C4::Context->dbh;
265 my $sth = $dbh->prepare("UPDATE `class_sort_rules`
266 SET `description` = ?,
267 `sort_routine` = ?
268 WHERE `class_sort_rule` = ?");
269 $sth->execute($description, $sort_routine, $class_sort_rule);
273 =head2 DelClassSortRule
275 DelClassSortRule($class_sort_rule);
277 Deletes class_sort_rules row.
279 =cut
281 sub DelClassSortRule {
283 my ($class_sort_rule) = @_;
284 my $dbh = C4::Context->dbh;
285 my $sth = $dbh->prepare("DELETE FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
286 $sth->execute($class_sort_rule);
290 =head2 GetSourcesForSortRule
292 my @source = GetSourcesForSortRule($class_sort_rule);
294 Retrieves an array class_source.cn_rule for each source
295 that uses the supplied $class_sort_rule.
297 =cut
299 sub GetSourcesForSortRule {
301 my ($class_sort_rule) = @_;
303 my $dbh = C4::Context->dbh;
304 my $sth = $dbh->prepare("SELECT cn_source FROM class_sources WHERE class_sort_rule = ?");
305 $sth->execute($class_sort_rule);
306 my @sources = ();
307 while (my ($source) = $sth->fetchrow_array()) {
308 push @sources, $source;
310 return @sources;
314 =head2 GetClassSort
316 my $cn_sort = GetClassSort($cn_source, $cn_class, $cn_item);
318 Get the sort key corresponding to the classification part and item part
319 and the defined call number source.
321 =cut
323 sub GetClassSort {
325 my ($cn_source, $cn_class, $cn_item) = @_;
327 my $source_ref = GetClassSource($cn_source);
328 unless (defined $source_ref) {
329 $source_ref = GetClassSource(C4::Context->preference("DefaultClassificationSource"));
331 my $routine = "";
332 if (defined $source_ref) {
333 my $rule_ref = GetClassSortRule($source_ref->{'class_sort_rule'});
334 if (defined $rule_ref) {
335 $routine = $rule_ref->{'sort_routine'};
339 return GetClassSortKey($routine, $cn_class, $cn_item);
345 =head1 AUTHOR
347 Koha Development Team <http://koha-community.org/>
349 =cut