bug 2505: enabled warnings in C4::NewsChannels
[koha.git] / C4 / ClassSource.pm
blob4a161ae6a26bd27c700e82819687f09dfb900c73
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 require Exporter;
22 use C4::Context;
23 use C4::Koha;
24 use C4::ClassSortRoutine;
26 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28 # set the version for version checking
29 $VERSION = 3.00;
31 =head1 NAME
33 C4::ClassSources - handle classification sources in Koha
35 =head1 SYNOPSIS
37 use C4::ClassSource;
39 =head1 DESCRIPTION
41 This module deals with manipulating classification
42 sources and sorting rules.
44 =head1 FUNCTIONS
46 =cut
49 @ISA = qw(Exporter);
50 @EXPORT = qw(
51 &GetClassSources
52 &AddClassSource
53 &GetClassSource
54 &ModClassSource
55 &DelClassSource
56 &GetClassSortRules
57 &AddClassSortRule
58 &GetClassSortRule
59 &ModClassSortRule
60 &DelClassSortRule
62 &GetSourcesForSortRule
63 &GetClassSort
67 =head2 GetClassSources
69 my $sources = GetClassSources();
71 Returns reference to hash of references to
72 the class sources, keyed on cn_source.
74 =head3 Example
76 my $sources = GetClassSources();
77 my @sources = ();
78 foreach my $cn_source (sort keys %$sources) {
79 my $source = $sources->{$cn_source};
80 push @sources,
82 code => $source->{'cn_source'},
83 description => $source->{'description'},
84 used => $source->{'used'},
85 sortrule => $source->{'class_sort_rule'}
89 =cut
91 sub GetClassSources {
93 my %class_sources = ();
94 my $dbh = C4::Context->dbh;
95 my $sth = $dbh->prepare_cached("SELECT * FROM `class_sources`");
96 $sth->execute();
97 while (my $source = $sth->fetchrow_hashref) {
98 $class_sources{ $source->{'cn_source'} } = $source;
100 $sth->finish();
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_cached("INSERT INTO `class_sources`
119 (`cn_source`, `description`, `used`, `class_sort_rule`)
120 VALUES (?, ?, ?, ?)");
121 $sth->execute($cn_source, $description, $used, $class_sort_rule);
122 $sth->finish();
126 =head2 GetClassSource
128 my $hashref = GetClassSource($cn_source);
130 Retrieves a class_sources row by cn_source.
132 =cut
134 sub GetClassSource {
136 my ($cn_source) = (@_);
137 my $dbh = C4::Context->dbh;
138 my $sth = $dbh->prepare_cached("SELECT * FROM `class_sources` WHERE cn_source = ?");
139 $sth->execute($cn_source);
140 my $row = $sth->fetchrow_hashref();
141 $sth->finish();
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_cached("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);
163 $sth->finish();
167 =head2 DelClassSource
169 DelClassSource($cn_source);
171 Deletes class_sources row.
173 =cut
175 sub DelClassSource {
177 my ($cn_source) = @_;
178 my $dbh = C4::Context->dbh;
179 my $sth = $dbh->prepare_cached("DELETE FROM `class_sources` WHERE `cn_source` = ?");
180 $sth->execute($cn_source);
181 $sth->finish();
185 =head2 GetClassSortRules
187 my $sort_rules = GetClassSortRules();
189 Returns reference to hash of references to
190 the class sorting rules, keyed on class_sort_rule
192 =head3 Example
194 my $sort_rules = GetClassSortRules();
195 my @sort_rules = ();
196 foreach my $sort_rule (sort keys %$sort_rules) {
197 my $sort_rule = $sort_rules->{$sort_rule};
198 push @sort_rules,
200 rule => $sort_rule->{'class_sort_rule'},
201 description => $sort_rule->{'description'},
202 sort_routine => $sort_rule->{'sort_routine'}
206 =cut
208 sub GetClassSortRules {
210 my %class_sort_rules = ();
211 my $dbh = C4::Context->dbh;
212 my $sth = $dbh->prepare_cached("SELECT * FROM `class_sort_rules`");
213 $sth->execute();
214 while (my $sort_rule = $sth->fetchrow_hashref) {
215 $class_sort_rules{ $sort_rule->{'class_sort_rule'} } = $sort_rule;
217 $sth->finish();
219 return \%class_sort_rules;
223 =head2 AddClassSortRule
225 AddClassSortRule($class_sort_rule, $description, $sort_routine);
227 Adds a class_sort_rules row.
229 =cut
231 sub AddClassSortRule {
233 my ($class_sort_rule, $description, $sort_routine) = @_;
234 my $dbh = C4::Context->dbh;
235 my $sth = $dbh->prepare_cached("INSERT INTO `class_sort_rules`
236 (`class_sort_rule`, `description`, `sort_routine`)
237 VALUES (?, ?, ?)");
238 $sth->execute($class_sort_rule, $description, $sort_routine);
239 $sth->finish();
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_cached("SELECT * FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
256 $sth->execute($class_sort_rule);
257 my $row = $sth->fetchrow_hashref();
258 $sth->finish();
259 return $row;
262 =head2 ModClassSortRule
264 ModClassSortRule($class_sort_rule, $description, $sort_routine);
266 Updates a class_sort_rules row.
268 =cut
270 sub ModClassSortRule {
272 my ($class_sort_rule, $description, $sort_routine) = @_;
273 my $dbh = C4::Context->dbh;
274 my $sth = $dbh->prepare_cached("UPDATE `class_sort_rules`
275 SET `description` = ?,
276 `sort_routine` = ?
277 WHERE `class_sort_rule` = ?");
278 $sth->execute($description, $sort_routine, $class_sort_rule);
279 $sth->finish();
283 =head2 DelClassSortRule
285 DelClassSortRule($class_sort_rule);
287 Deletes class_sort_rules row.
289 =cut
291 sub DelClassSortRule {
293 my ($class_sort_rule) = @_;
294 my $dbh = C4::Context->dbh;
295 my $sth = $dbh->prepare_cached("DELETE FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
296 $sth->execute($class_sort_rule);
297 $sth->finish();
301 =head2 GetSourcesForSortRule
303 my @source = GetSourcesForSortRule($class_sort_rule);
305 Retrieves an array class_source.cn_rule for each source
306 that uses the supplied $class_sort_rule.
308 =cut
310 sub GetSourcesForSortRule {
312 my ($class_sort_rule) = @_;
314 my $dbh = C4::Context->dbh;
315 my $sth = $dbh->prepare_cached("SELECT cn_source FROM class_sources WHERE class_sort_rule = ?");
316 $sth->execute($class_sort_rule);
317 my @sources = ();
318 while (my ($source) = $sth->fetchrow_array()) {
319 push @sources, $source;
321 $sth->finish();
322 return @sources;
326 =head2 GetClassSort
328 my $cn_sort = GetClassSort($cn_source, $cn_class, $cn_item);
330 Get the sort key corresponding to the classification part and item part
331 and the defined call number source.
333 =cut
335 sub GetClassSort {
337 my ($cn_source, $cn_class, $cn_item) = @_;
339 my $source_ref = GetClassSource($cn_source);
340 unless (defined $source_ref) {
341 $source_ref = GetClassSource(C4::Context->preference("DefaultClassificationSource"));
343 my $routine = "";
344 if (defined $source_ref) {
345 my $rule_ref = GetClassSortRule($source_ref->{'class_sort_rule'});
346 if (defined $rule_ref) {
347 $routine = $rule_ref->{'sort_routine'};
351 return GetClassSortKey($routine, $cn_class, $cn_item);
357 =head1 AUTHOR
359 Koha Developement team <info@koha.org>
361 =cut