Bug 21184: Replace C4::Items::GetBarcodeFromItemnumber calls
[koha.git] / C4 / ClassSource.pm
blob245f9589c64e8accf934b3345a7d2d4765b5450b
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 $exists = GetClassSource($cn_source);
116 if ($exists) {
117 return 0;
119 my $dbh = C4::Context->dbh;
120 my $sth = $dbh->prepare("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 return 1;
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("SELECT * FROM `class_sources` WHERE cn_source = ?");
140 $sth->execute($cn_source);
141 my $row = $sth->fetchrow_hashref();
142 return $row;
145 =head2 ModClassSource
147 ModClassSource($cn_source, $description, $used, $class_sort_rule);
149 Updates a class_sources row.
151 =cut
153 sub ModClassSource {
155 my ($cn_source, $description, $used, $class_sort_rule) = @_;
156 my $dbh = C4::Context->dbh;
157 my $sth = $dbh->prepare("UPDATE `class_sources`
158 SET `description` = ?,
159 `used` = ?,
160 `class_sort_rule` = ?
161 WHERE `cn_source` = ?");
162 $sth->execute($description, $used, $class_sort_rule, $cn_source);
166 =head2 DelClassSource
168 DelClassSource($cn_source);
170 Deletes class_sources row.
172 =cut
174 sub DelClassSource {
176 my ($cn_source) = @_;
177 my $dbh = C4::Context->dbh;
178 my $sth = $dbh->prepare("DELETE FROM `class_sources` WHERE `cn_source` = ?");
179 $sth->execute($cn_source);
183 =head2 GetClassSortRules
185 my $sort_rules = GetClassSortRules();
187 Returns reference to hash of references to
188 the class sorting rules, keyed on class_sort_rule
190 =head3 Example
192 my $sort_rules = GetClassSortRules();
193 my @sort_rules = ();
194 foreach my $sort_rule (sort keys %$sort_rules) {
195 my $sort_rule = $sort_rules->{$sort_rule};
196 push @sort_rules,
198 rule => $sort_rule->{'class_sort_rule'},
199 description => $sort_rule->{'description'},
200 sort_routine => $sort_rule->{'sort_routine'}
204 =cut
206 sub GetClassSortRules {
208 my %class_sort_rules = ();
209 my $dbh = C4::Context->dbh;
210 my $sth = $dbh->prepare("SELECT * FROM `class_sort_rules`");
211 $sth->execute();
212 while (my $sort_rule = $sth->fetchrow_hashref) {
213 $class_sort_rules{ $sort_rule->{'class_sort_rule'} } = $sort_rule;
216 return \%class_sort_rules;
220 =head2 AddClassSortRule
222 AddClassSortRule($class_sort_rule, $description, $sort_routine);
224 Adds a class_sort_rules row.
226 =cut
228 sub AddClassSortRule {
230 my ($class_sort_rule, $description, $sort_routine) = @_;
231 my $exists = GetClassSortRule($class_sort_rule);
232 if ($exists) {
233 return 0;
235 my $dbh = C4::Context->dbh;
236 my $sth = $dbh->prepare("INSERT INTO `class_sort_rules`
237 (`class_sort_rule`, `description`, `sort_routine`)
238 VALUES (?, ?, ?)");
239 $sth->execute($class_sort_rule, $description, $sort_routine);
240 return 1;
243 =head2 GetClassSortRule
245 my $hashref = GetClassSortRule($class_sort_rule);
247 Retrieves a class_sort_rules row by class_sort_rule.
249 =cut
251 sub GetClassSortRule {
253 my ($class_sort_rule) = (@_);
254 my $dbh = C4::Context->dbh;
255 my $sth = $dbh->prepare("SELECT * FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
256 $sth->execute($class_sort_rule);
257 my $row = $sth->fetchrow_hashref();
258 return $row;
261 =head2 ModClassSortRule
263 ModClassSortRule($class_sort_rule, $description, $sort_routine);
265 Updates a class_sort_rules row.
267 =cut
269 sub ModClassSortRule {
271 my ($class_sort_rule, $description, $sort_routine) = @_;
272 my $dbh = C4::Context->dbh;
273 my $sth = $dbh->prepare("UPDATE `class_sort_rules`
274 SET `description` = ?,
275 `sort_routine` = ?
276 WHERE `class_sort_rule` = ?");
277 $sth->execute($description, $sort_routine, $class_sort_rule);
281 =head2 DelClassSortRule
283 DelClassSortRule($class_sort_rule);
285 Deletes class_sort_rules row.
287 =cut
289 sub DelClassSortRule {
291 my ($class_sort_rule) = @_;
292 my $dbh = C4::Context->dbh;
293 my $sth = $dbh->prepare("DELETE FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
294 $sth->execute($class_sort_rule);
298 =head2 GetSourcesForSortRule
300 my @source = GetSourcesForSortRule($class_sort_rule);
302 Retrieves an array class_source.cn_rule for each source
303 that uses the supplied $class_sort_rule.
305 =cut
307 sub GetSourcesForSortRule {
309 my ($class_sort_rule) = @_;
311 my $dbh = C4::Context->dbh;
312 my $sth = $dbh->prepare("SELECT cn_source FROM class_sources WHERE class_sort_rule = ?");
313 $sth->execute($class_sort_rule);
314 my @sources = ();
315 while (my ($source) = $sth->fetchrow_array()) {
316 push @sources, $source;
318 return @sources;
322 =head2 GetClassSort
324 my $cn_sort = GetClassSort($cn_source, $cn_class, $cn_item);
326 Get the sort key corresponding to the classification part and item part
327 and the defined call number source.
329 =cut
331 sub GetClassSort {
333 my ($cn_source, $cn_class, $cn_item) = @_;
335 my $source_ref = GetClassSource($cn_source);
336 unless (defined $source_ref) {
337 $source_ref = GetClassSource(C4::Context->preference("DefaultClassificationSource"));
339 my $routine = "";
340 if (defined $source_ref) {
341 my $rule_ref = GetClassSortRule($source_ref->{'class_sort_rule'});
342 if (defined $rule_ref) {
343 $routine = $rule_ref->{'sort_routine'};
347 return GetClassSortKey($routine, $cn_class, $cn_item);
353 =head1 AUTHOR
355 Koha Development Team <http://koha-community.org/>
357 =cut