Bug 16011: $VERSION - Remove the $VERSION init
[koha.git] / C4 / ClassSource.pm
blob5f8cfe3fec3561301b375aac3fe9dc62d32f338b
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);
29 # set the version for version checking
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("SELECT * FROM `class_sources`");
96 $sth->execute();
97 while (my $source = $sth->fetchrow_hashref) {
98 $class_sources{ $source->{'cn_source'} } = $source;
101 return \%class_sources;
105 =head2 AddClassSource
107 AddClassSource($cn_source, $description, $used, $class_sort_rule);
109 Adds a class_sources row.
111 =cut
113 sub AddClassSource {
115 my ($cn_source, $description, $used, $class_sort_rule) = @_;
116 my $dbh = C4::Context->dbh;
117 my $sth = $dbh->prepare("INSERT INTO `class_sources`
118 (`cn_source`, `description`, `used`, `class_sort_rule`)
119 VALUES (?, ?, ?, ?)");
120 $sth->execute($cn_source, $description, $used, $class_sort_rule);
124 =head2 GetClassSource
126 my $hashref = GetClassSource($cn_source);
128 Retrieves a class_sources row by cn_source.
130 =cut
132 sub GetClassSource {
134 my ($cn_source) = (@_);
135 my $dbh = C4::Context->dbh;
136 my $sth = $dbh->prepare("SELECT * FROM `class_sources` WHERE cn_source = ?");
137 $sth->execute($cn_source);
138 my $row = $sth->fetchrow_hashref();
139 return $row;
142 =head2 ModClassSource
144 ModClassSource($cn_source, $description, $used, $class_sort_rule);
146 Updates a class_sources row.
148 =cut
150 sub ModClassSource {
152 my ($cn_source, $description, $used, $class_sort_rule) = @_;
153 my $dbh = C4::Context->dbh;
154 my $sth = $dbh->prepare("UPDATE `class_sources`
155 SET `description` = ?,
156 `used` = ?,
157 `class_sort_rule` = ?
158 WHERE `cn_source` = ?");
159 $sth->execute($description, $used, $class_sort_rule, $cn_source);
163 =head2 DelClassSource
165 DelClassSource($cn_source);
167 Deletes class_sources row.
169 =cut
171 sub DelClassSource {
173 my ($cn_source) = @_;
174 my $dbh = C4::Context->dbh;
175 my $sth = $dbh->prepare("DELETE FROM `class_sources` WHERE `cn_source` = ?");
176 $sth->execute($cn_source);
180 =head2 GetClassSortRules
182 my $sort_rules = GetClassSortRules();
184 Returns reference to hash of references to
185 the class sorting rules, keyed on class_sort_rule
187 =head3 Example
189 my $sort_rules = GetClassSortRules();
190 my @sort_rules = ();
191 foreach my $sort_rule (sort keys %$sort_rules) {
192 my $sort_rule = $sort_rules->{$sort_rule};
193 push @sort_rules,
195 rule => $sort_rule->{'class_sort_rule'},
196 description => $sort_rule->{'description'},
197 sort_routine => $sort_rule->{'sort_routine'}
201 =cut
203 sub GetClassSortRules {
205 my %class_sort_rules = ();
206 my $dbh = C4::Context->dbh;
207 my $sth = $dbh->prepare("SELECT * FROM `class_sort_rules`");
208 $sth->execute();
209 while (my $sort_rule = $sth->fetchrow_hashref) {
210 $class_sort_rules{ $sort_rule->{'class_sort_rule'} } = $sort_rule;
213 return \%class_sort_rules;
217 =head2 AddClassSortRule
219 AddClassSortRule($class_sort_rule, $description, $sort_routine);
221 Adds a class_sort_rules row.
223 =cut
225 sub AddClassSortRule {
227 my ($class_sort_rule, $description, $sort_routine) = @_;
228 my $dbh = C4::Context->dbh;
229 my $sth = $dbh->prepare("INSERT INTO `class_sort_rules`
230 (`class_sort_rule`, `description`, `sort_routine`)
231 VALUES (?, ?, ?)");
232 $sth->execute($class_sort_rule, $description, $sort_routine);
236 =head2 GetClassSortRule
238 my $hashref = GetClassSortRule($class_sort_rule);
240 Retrieves a class_sort_rules row by class_sort_rule.
242 =cut
244 sub GetClassSortRule {
246 my ($class_sort_rule) = (@_);
247 my $dbh = C4::Context->dbh;
248 my $sth = $dbh->prepare("SELECT * FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
249 $sth->execute($class_sort_rule);
250 my $row = $sth->fetchrow_hashref();
251 return $row;
254 =head2 ModClassSortRule
256 ModClassSortRule($class_sort_rule, $description, $sort_routine);
258 Updates a class_sort_rules row.
260 =cut
262 sub ModClassSortRule {
264 my ($class_sort_rule, $description, $sort_routine) = @_;
265 my $dbh = C4::Context->dbh;
266 my $sth = $dbh->prepare("UPDATE `class_sort_rules`
267 SET `description` = ?,
268 `sort_routine` = ?
269 WHERE `class_sort_rule` = ?");
270 $sth->execute($description, $sort_routine, $class_sort_rule);
274 =head2 DelClassSortRule
276 DelClassSortRule($class_sort_rule);
278 Deletes class_sort_rules row.
280 =cut
282 sub DelClassSortRule {
284 my ($class_sort_rule) = @_;
285 my $dbh = C4::Context->dbh;
286 my $sth = $dbh->prepare("DELETE FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
287 $sth->execute($class_sort_rule);
291 =head2 GetSourcesForSortRule
293 my @source = GetSourcesForSortRule($class_sort_rule);
295 Retrieves an array class_source.cn_rule for each source
296 that uses the supplied $class_sort_rule.
298 =cut
300 sub GetSourcesForSortRule {
302 my ($class_sort_rule) = @_;
304 my $dbh = C4::Context->dbh;
305 my $sth = $dbh->prepare("SELECT cn_source FROM class_sources WHERE class_sort_rule = ?");
306 $sth->execute($class_sort_rule);
307 my @sources = ();
308 while (my ($source) = $sth->fetchrow_array()) {
309 push @sources, $source;
311 return @sources;
315 =head2 GetClassSort
317 my $cn_sort = GetClassSort($cn_source, $cn_class, $cn_item);
319 Get the sort key corresponding to the classification part and item part
320 and the defined call number source.
322 =cut
324 sub GetClassSort {
326 my ($cn_source, $cn_class, $cn_item) = @_;
328 my $source_ref = GetClassSource($cn_source);
329 unless (defined $source_ref) {
330 $source_ref = GetClassSource(C4::Context->preference("DefaultClassificationSource"));
332 my $routine = "";
333 if (defined $source_ref) {
334 my $rule_ref = GetClassSortRule($source_ref->{'class_sort_rule'});
335 if (defined $rule_ref) {
336 $routine = $rule_ref->{'sort_routine'};
340 return GetClassSortKey($routine, $cn_class, $cn_item);
346 =head1 AUTHOR
348 Koha Development Team <http://koha-community.org/>
350 =cut