Bug 14522: (QA followup) tidy tests
[koha.git] / C4 / ClassSource.pm
blob0a8d65b9bb8b49e6ecc710c77ace78b5517957f3
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($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
29 # set the version for version checking
30 $VERSION = 3.07.00.049;
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("SELECT * FROM `class_sources`");
97 $sth->execute();
98 while (my $source = $sth->fetchrow_hashref) {
99 $class_sources{ $source->{'cn_source'} } = $source;
102 return \%class_sources;
106 =head2 AddClassSource
108 AddClassSource($cn_source, $description, $used, $class_sort_rule);
110 Adds a class_sources row.
112 =cut
114 sub AddClassSource {
116 my ($cn_source, $description, $used, $class_sort_rule) = @_;
117 my $dbh = C4::Context->dbh;
118 my $sth = $dbh->prepare("INSERT INTO `class_sources`
119 (`cn_source`, `description`, `used`, `class_sort_rule`)
120 VALUES (?, ?, ?, ?)");
121 $sth->execute($cn_source, $description, $used, $class_sort_rule);
125 =head2 GetClassSource
127 my $hashref = GetClassSource($cn_source);
129 Retrieves a class_sources row by cn_source.
131 =cut
133 sub GetClassSource {
135 my ($cn_source) = (@_);
136 my $dbh = C4::Context->dbh;
137 my $sth = $dbh->prepare("SELECT * FROM `class_sources` WHERE cn_source = ?");
138 $sth->execute($cn_source);
139 my $row = $sth->fetchrow_hashref();
140 return $row;
143 =head2 ModClassSource
145 ModClassSource($cn_source, $description, $used, $class_sort_rule);
147 Updates a class_sources row.
149 =cut
151 sub ModClassSource {
153 my ($cn_source, $description, $used, $class_sort_rule) = @_;
154 my $dbh = C4::Context->dbh;
155 my $sth = $dbh->prepare("UPDATE `class_sources`
156 SET `description` = ?,
157 `used` = ?,
158 `class_sort_rule` = ?
159 WHERE `cn_source` = ?");
160 $sth->execute($description, $used, $class_sort_rule, $cn_source);
164 =head2 DelClassSource
166 DelClassSource($cn_source);
168 Deletes class_sources row.
170 =cut
172 sub DelClassSource {
174 my ($cn_source) = @_;
175 my $dbh = C4::Context->dbh;
176 my $sth = $dbh->prepare("DELETE FROM `class_sources` WHERE `cn_source` = ?");
177 $sth->execute($cn_source);
181 =head2 GetClassSortRules
183 my $sort_rules = GetClassSortRules();
185 Returns reference to hash of references to
186 the class sorting rules, keyed on class_sort_rule
188 =head3 Example
190 my $sort_rules = GetClassSortRules();
191 my @sort_rules = ();
192 foreach my $sort_rule (sort keys %$sort_rules) {
193 my $sort_rule = $sort_rules->{$sort_rule};
194 push @sort_rules,
196 rule => $sort_rule->{'class_sort_rule'},
197 description => $sort_rule->{'description'},
198 sort_routine => $sort_rule->{'sort_routine'}
202 =cut
204 sub GetClassSortRules {
206 my %class_sort_rules = ();
207 my $dbh = C4::Context->dbh;
208 my $sth = $dbh->prepare("SELECT * FROM `class_sort_rules`");
209 $sth->execute();
210 while (my $sort_rule = $sth->fetchrow_hashref) {
211 $class_sort_rules{ $sort_rule->{'class_sort_rule'} } = $sort_rule;
214 return \%class_sort_rules;
218 =head2 AddClassSortRule
220 AddClassSortRule($class_sort_rule, $description, $sort_routine);
222 Adds a class_sort_rules row.
224 =cut
226 sub AddClassSortRule {
228 my ($class_sort_rule, $description, $sort_routine) = @_;
229 my $dbh = C4::Context->dbh;
230 my $sth = $dbh->prepare("INSERT INTO `class_sort_rules`
231 (`class_sort_rule`, `description`, `sort_routine`)
232 VALUES (?, ?, ?)");
233 $sth->execute($class_sort_rule, $description, $sort_routine);
237 =head2 GetClassSortRule
239 my $hashref = GetClassSortRule($class_sort_rule);
241 Retrieves a class_sort_rules row by class_sort_rule.
243 =cut
245 sub GetClassSortRule {
247 my ($class_sort_rule) = (@_);
248 my $dbh = C4::Context->dbh;
249 my $sth = $dbh->prepare("SELECT * FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
250 $sth->execute($class_sort_rule);
251 my $row = $sth->fetchrow_hashref();
252 return $row;
255 =head2 ModClassSortRule
257 ModClassSortRule($class_sort_rule, $description, $sort_routine);
259 Updates a class_sort_rules row.
261 =cut
263 sub ModClassSortRule {
265 my ($class_sort_rule, $description, $sort_routine) = @_;
266 my $dbh = C4::Context->dbh;
267 my $sth = $dbh->prepare("UPDATE `class_sort_rules`
268 SET `description` = ?,
269 `sort_routine` = ?
270 WHERE `class_sort_rule` = ?");
271 $sth->execute($description, $sort_routine, $class_sort_rule);
275 =head2 DelClassSortRule
277 DelClassSortRule($class_sort_rule);
279 Deletes class_sort_rules row.
281 =cut
283 sub DelClassSortRule {
285 my ($class_sort_rule) = @_;
286 my $dbh = C4::Context->dbh;
287 my $sth = $dbh->prepare("DELETE FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
288 $sth->execute($class_sort_rule);
292 =head2 GetSourcesForSortRule
294 my @source = GetSourcesForSortRule($class_sort_rule);
296 Retrieves an array class_source.cn_rule for each source
297 that uses the supplied $class_sort_rule.
299 =cut
301 sub GetSourcesForSortRule {
303 my ($class_sort_rule) = @_;
305 my $dbh = C4::Context->dbh;
306 my $sth = $dbh->prepare("SELECT cn_source FROM class_sources WHERE class_sort_rule = ?");
307 $sth->execute($class_sort_rule);
308 my @sources = ();
309 while (my ($source) = $sth->fetchrow_array()) {
310 push @sources, $source;
312 return @sources;
316 =head2 GetClassSort
318 my $cn_sort = GetClassSort($cn_source, $cn_class, $cn_item);
320 Get the sort key corresponding to the classification part and item part
321 and the defined call number source.
323 =cut
325 sub GetClassSort {
327 my ($cn_source, $cn_class, $cn_item) = @_;
329 my $source_ref = GetClassSource($cn_source);
330 unless (defined $source_ref) {
331 $source_ref = GetClassSource(C4::Context->preference("DefaultClassificationSource"));
333 my $routine = "";
334 if (defined $source_ref) {
335 my $rule_ref = GetClassSortRule($source_ref->{'class_sort_rule'});
336 if (defined $rule_ref) {
337 $routine = $rule_ref->{'sort_routine'};
341 return GetClassSortKey($routine, $cn_class, $cn_item);
347 =head1 AUTHOR
349 Koha Development Team <http://koha-community.org/>
351 =cut