File Coverage

File:C4/ClassSource.pm
Coverage:17.1%

linestmtbrancondsubtimecode
1package C4::ClassSource;
2
3# Copyright (C) 2007 LibLime
4#
5# This file is part of Koha.
6#
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.
11#
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.
15#
16# You should have received a copy of the GNU General Public License along
17# with Koha; if not, write to the Free Software Foundation, Inc.,
18# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
42
42
42
43386
182
1463
use strict;
21
42
42
42
363
242
2202
use warnings;
22
23require Exporter;
24
42
42
42
636
189
722
use C4::Context;
25
42
42
42
8492
134
5216
use C4::ClassSortRoutine;
26
27
42
42
42
275
96
39179
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28
29# set the version for version checking
30$VERSION = 3.00;
31
32 - 47
=head1 NAME

C4::ClassSources - handle classification sources in Koha

=head1 SYNOPSIS

use C4::ClassSource;

=head1 DESCRIPTION

This module deals with manipulating classification
sources and sorting rules.

=head1 FUNCTIONS

=cut
48
49
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
62
63    &GetSourcesForSortRule
64    &GetClassSort
65
66);
67
68 - 90
=head2 GetClassSources

  my $sources = GetClassSources();

  Returns reference to hash of references to
  the class sources, keyed on cn_source.

=head3 Example

my $sources = GetClassSources();
my @sources = ();
foreach my $cn_source (sort keys %$sources) {
    my $source = $sources->{$cn_source};
    push @sources, 
      {  
        code        => $source->{'cn_source'},
        description => $source->{'description'},
        used => $source->{'used'},
        sortrule    => $source->{'class_sort_rule'}
      } 
}

=cut
91
92sub GetClassSources {
93
94
0
    my %class_sources = ();
95
0
    my $dbh = C4::Context->dbh;
96
0
    my $sth = $dbh->prepare_cached("SELECT * FROM `class_sources`");
97
0
    $sth->execute();
98
0
    while (my $source = $sth->fetchrow_hashref) {
99
0
        $class_sources{ $source->{'cn_source'} } = $source;
100    }
101
0
    $sth->finish();
102
103
0
    return \%class_sources;
104
105}
106
107 - 113
=head2 AddClassSource

  AddClassSource($cn_source, $description, $used, $class_sort_rule);

  Adds a class_sources row.

=cut
114
115sub AddClassSource {
116
117
0
    my ($cn_source, $description, $used, $class_sort_rule) = @_;
118
0
    my $dbh = C4::Context->dbh;
119
0
    my $sth = $dbh->prepare_cached("INSERT INTO `class_sources`
120                                           (`cn_source`, `description`, `used`, `class_sort_rule`)
121                                           VALUES (?, ?, ?, ?)");
122
0
    $sth->execute($cn_source, $description, $used, $class_sort_rule);
123
0
    $sth->finish();
124
125}
126
127 - 133
=head2 GetClassSource

  my $hashref = GetClassSource($cn_source);

  Retrieves a class_sources row by cn_source.

=cut
134
135sub GetClassSource {
136
137
0
    my ($cn_source) = (@_);
138
0
    my $dbh = C4::Context->dbh;
139
0
    my $sth = $dbh->prepare_cached("SELECT * FROM `class_sources` WHERE cn_source = ?");
140
0
    $sth->execute($cn_source);
141
0
    my $row = $sth->fetchrow_hashref();
142
0
    $sth->finish();
143
0
    return $row;
144}
145
146 - 152
=head2 ModClassSource 

  ModClassSource($cn_source, $description, $used, $class_sort_rule);

  Updates a class_sources row.

=cut
153
154sub ModClassSource {
155
156
0
    my ($cn_source, $description, $used, $class_sort_rule) = @_;
157
0
    my $dbh = C4::Context->dbh;
158
0
    my $sth = $dbh->prepare_cached("UPDATE `class_sources`
159                                    SET `description` = ?,
160                                         `used` = ?,
161                                         `class_sort_rule` = ?
162                                    WHERE `cn_source` = ?");
163
0
    $sth->execute($description, $used, $class_sort_rule, $cn_source);
164
0
    $sth->finish();
165
166}
167
168 - 174
=head2 DelClassSource 

  DelClassSource($cn_source);

  Deletes class_sources row.

=cut
175
176sub DelClassSource {
177
178
0
    my ($cn_source) = @_;
179
0
    my $dbh = C4::Context->dbh;
180
0
    my $sth = $dbh->prepare_cached("DELETE FROM `class_sources` WHERE `cn_source` = ?");
181
0
    $sth->execute($cn_source);
182
0
    $sth->finish();
183
184}
185
186 - 207
=head2 GetClassSortRules

  my $sort_rules = GetClassSortRules();

Returns reference to hash of references to
the class sorting rules, keyed on class_sort_rule

=head3 Example

  my $sort_rules = GetClassSortRules();
  my @sort_rules = ();
  foreach my $sort_rule (sort keys %$sort_rules) {
      my $sort_rule = $sort_rules->{$sort_rule};
      push @sort_rules,
          {
          rule        => $sort_rule->{'class_sort_rule'},
          description => $sort_rule->{'description'},
          sort_routine    => $sort_rule->{'sort_routine'}
      }
   }

=cut
208
209sub GetClassSortRules {
210
211
0
    my %class_sort_rules = ();
212
0
    my $dbh = C4::Context->dbh;
213
0
    my $sth = $dbh->prepare_cached("SELECT * FROM `class_sort_rules`");
214
0
    $sth->execute();
215
0
    while (my $sort_rule = $sth->fetchrow_hashref) {
216
0
        $class_sort_rules{ $sort_rule->{'class_sort_rule'} } = $sort_rule;
217    }
218
0
    $sth->finish();
219
220
0
    return \%class_sort_rules;
221
222}
223
224 - 230
=head2 AddClassSortRule

  AddClassSortRule($class_sort_rule, $description, $sort_routine);

  Adds a class_sort_rules row.

=cut
231
232sub AddClassSortRule {
233
234
0
    my ($class_sort_rule, $description, $sort_routine) = @_;
235
0
    my $dbh = C4::Context->dbh;
236
0
    my $sth = $dbh->prepare_cached("INSERT INTO `class_sort_rules`
237                                           (`class_sort_rule`, `description`, `sort_routine`)
238                                           VALUES (?, ?, ?)");
239
0
    $sth->execute($class_sort_rule, $description, $sort_routine);
240
0
    $sth->finish();
241
242}
243
244 - 250
=head2 GetClassSortRule

  my $hashref = GetClassSortRule($class_sort_rule);

  Retrieves a class_sort_rules row by class_sort_rule.

=cut
251
252sub GetClassSortRule {
253
254
0
    my ($class_sort_rule) = (@_);
255
0
    my $dbh = C4::Context->dbh;
256
0
    my $sth = $dbh->prepare_cached("SELECT * FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
257
0
    $sth->execute($class_sort_rule);
258
0
    my $row = $sth->fetchrow_hashref();
259
0
    $sth->finish();
260
0
    return $row;
261}
262
263 - 269
=head2 ModClassSortRule 

  ModClassSortRule($class_sort_rule, $description, $sort_routine);

  Updates a class_sort_rules row.

=cut
270
271sub ModClassSortRule {
272
273
0
    my ($class_sort_rule, $description, $sort_routine) = @_;
274
0
    my $dbh = C4::Context->dbh;
275
0
    my $sth = $dbh->prepare_cached("UPDATE `class_sort_rules`
276                                    SET `description` = ?,
277                                         `sort_routine` = ?
278                                    WHERE `class_sort_rule` = ?");
279
0
    $sth->execute($description, $sort_routine, $class_sort_rule);
280
0
    $sth->finish();
281
282}
283
284 - 290
=head2 DelClassSortRule 

  DelClassSortRule($class_sort_rule);

  Deletes class_sort_rules row.

=cut
291
292sub DelClassSortRule {
293
294
0
    my ($class_sort_rule) = @_;
295
0
    my $dbh = C4::Context->dbh;
296
0
    my $sth = $dbh->prepare_cached("DELETE FROM `class_sort_rules` WHERE `class_sort_rule` = ?");
297
0
    $sth->execute($class_sort_rule);
298
0
    $sth->finish();
299
300}
301
302 - 309
=head2 GetSourcesForSortRule

  my @source = GetSourcesForSortRule($class_sort_rule);

  Retrieves an array class_source.cn_rule for each source
  that uses the supplied $class_sort_rule.

=cut
310
311sub GetSourcesForSortRule {
312
313
0
    my ($class_sort_rule) = @_;
314
315
0
    my $dbh = C4::Context->dbh;
316
0
    my $sth = $dbh->prepare_cached("SELECT cn_source FROM class_sources WHERE class_sort_rule = ?");
317
0
    $sth->execute($class_sort_rule);
318
0
    my @sources = ();
319
0
    while (my ($source) = $sth->fetchrow_array()) {
320
0
        push @sources, $source;
321    }
322
0
    $sth->finish();
323
0
    return @sources;
324
325}
326
327 - 334
=head2 GetClassSort

  my $cn_sort = GetClassSort($cn_source, $cn_class, $cn_item);

Get the sort key corresponding to the classification part and item part
and the defined call number source.

=cut
335
336sub GetClassSort {
337
338
0
    my ($cn_source, $cn_class, $cn_item) = @_;
339
340
0
    my $source_ref = GetClassSource($cn_source);
341
0
    unless (defined $source_ref) {
342
0
        $source_ref = GetClassSource(C4::Context->preference("DefaultClassificationSource"));
343    }
344
0
    my $routine = "";
345
0
    if (defined $source_ref) {
346
0
        my $rule_ref = GetClassSortRule($source_ref->{'class_sort_rule'});
347
0
        if (defined $rule_ref) {
348
0
            $routine = $rule_ref->{'sort_routine'};
349        }
350    }
351
352
0
    return GetClassSortKey($routine, $cn_class, $cn_item);
353
354}
355
3561;
357
358 - 362
=head1 AUTHOR

Koha Development Team <http://koha-community.org/>

=cut