1 package C4
::ClassSource
;
3 # Copyright (C) 2007 LibLime
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>.
25 use C4
::ClassSortRoutine
;
27 use vars
qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
32 C4::ClassSources - handle classification sources in Koha
40 This module deals with manipulating classification
41 sources and sorting rules.
61 &GetSourcesForSortRule
66 =head2 GetClassSources
68 my $sources = GetClassSources();
70 Returns reference to hash of references to
71 the class sources, keyed on cn_source.
75 my $sources = GetClassSources();
77 foreach my $cn_source (sort keys %$sources) {
78 my $source = $sources->{$cn_source};
81 code => $source->{'cn_source'},
82 description => $source->{'description'},
83 used => $source->{'used'},
84 sortrule => $source->{'class_sort_rule'}
92 my %class_sources = ();
93 my $dbh = C4
::Context
->dbh;
94 my $sth = $dbh->prepare("SELECT * FROM `class_sources`");
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.
114 my ($cn_source, $description, $used, $class_sort_rule) = @_;
115 my $exists = GetClassSource
($cn_source);
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);
127 =head2 GetClassSource
129 my $hashref = GetClassSource($cn_source);
131 Retrieves a class_sources row by cn_source.
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();
145 =head2 ModClassSource
147 ModClassSource($cn_source, $description, $used, $class_sort_rule);
149 Updates a class_sources row.
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` = ?,
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.
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
192 my $sort_rules = GetClassSortRules();
194 foreach my $sort_rule (sort keys %$sort_rules) {
195 my $sort_rule = $sort_rules->{$sort_rule};
198 rule => $sort_rule->{'class_sort_rule'},
199 description => $sort_rule->{'description'},
200 sort_routine => $sort_rule->{'sort_routine'}
206 sub GetClassSortRules
{
208 my %class_sort_rules = ();
209 my $dbh = C4
::Context
->dbh;
210 my $sth = $dbh->prepare("SELECT * FROM `class_sort_rules`");
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.
228 sub AddClassSortRule
{
230 my ($class_sort_rule, $description, $sort_routine) = @_;
231 my $exists = GetClassSortRule
($class_sort_rule);
235 my $dbh = C4
::Context
->dbh;
236 my $sth = $dbh->prepare("INSERT INTO `class_sort_rules`
237 (`class_sort_rule`, `description`, `sort_routine`)
239 $sth->execute($class_sort_rule, $description, $sort_routine);
243 =head2 GetClassSortRule
245 my $hashref = GetClassSortRule($class_sort_rule);
247 Retrieves a class_sort_rules row by class_sort_rule.
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();
261 =head2 ModClassSortRule
263 ModClassSortRule($class_sort_rule, $description, $sort_routine);
265 Updates a class_sort_rules row.
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` = ?,
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.
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.
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);
315 while (my ($source) = $sth->fetchrow_array()) {
316 push @sources, $source;
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.
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"));
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);
355 Koha Development Team <http://koha-community.org/>