From a2c245c77ddb92b884c237e3392b2a309bf913ff Mon Sep 17 00:00:00 2001 From: Galen Charlton Date: Sun, 21 Oct 2007 21:23:26 -0500 Subject: [PATCH] call number work part 2 -- added framework for call number filing routines Signed-off-by: Chris Cormack Signed-off-by: Joshua Ferraro --- C4/ClassSortRoutine.pm | 130 ++++++++++++++++++ C4/ClassSortRoutine/Dewey.pm | 90 ++++++++++++ C4/ClassSortRoutine/Generic.pm | 72 ++++++++++ C4/ClassSortRoutine/LCC.pm | 75 ++++++++++ admin/classsources.pl | 21 +++ .../prog/en/modules/admin/classsources.tmpl | 11 +- 6 files changed, 397 insertions(+), 2 deletions(-) create mode 100644 C4/ClassSortRoutine.pm create mode 100644 C4/ClassSortRoutine/Dewey.pm create mode 100644 C4/ClassSortRoutine/Generic.pm create mode 100644 C4/ClassSortRoutine/LCC.pm diff --git a/C4/ClassSortRoutine.pm b/C4/ClassSortRoutine.pm new file mode 100644 index 0000000000..b4fa7a4285 --- /dev/null +++ b/C4/ClassSortRoutine.pm @@ -0,0 +1,130 @@ +package C4::ClassSortRoutine; + +# Copyright (C) 2007 LibLime +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, +# Suite 330, Boston, MA 02111-1307 USA + +use strict; +require Exporter; +use Class::Factory::Util; +use C4::Context; +use C4::Koha; + +use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); + +# set the version for version checking +$VERSION = 3.00; + +=head1 NAME + +C4::ClassSortRoutine - base object for creation of classification sorting + key generation routines + +=head1 SYNOPSIS + +use C4::ClassSortRoutine; + +=head1 FUNCTIONS + +=cut + +@ISA = qw(Exporter); +@EXPORT = qw( + &GetSortRoutineNames + &GetClassSortKey +); + +# intialization code +my %loaded_routines = (); +my @sort_routines = GetSortRoutineNames(); +foreach my $sort_routine (@sort_routines) { + if (eval "require C4::ClassSortRoutine::$sort_routine") { + my $ref; + eval "\$ref = \\\&C4::ClassSortRoutine::${sort_routine}::get_class_sort_key"; + if (eval "\$ref->(\"a\", \"b\")") { + $loaded_routines{$sort_routine} = $ref; + } else { + $loaded_routines{$sort_routine} = \&_get_class_sort_key; + } + } else { + $loaded_routines{$sort_routine} = \&_get_class_sort_key; + } +} + +=head2 GetSortRoutineNames + + my @routines = GetSortRoutineNames(); + +Get names of all modules under C4::ClassSortRoutine::*. Adding +a new classification sorting routine can therefore be done +simply by writing a new submodule under C4::ClassSortRoutine and +placing it in the C4/ClassSortRoutine directory. + +=cut + +sub GetSortRoutineNames { + return C4::ClassSortRoutine->subclasses(); +} + +=head2 GetClassSortKey + + my $cn_sort = GetClassSortKey($sort_routine, $cn_class, $cn_item); + +Generates classification sorting key. If $sort_routine does not point +to a valid submodule in C4::ClassSortRoutine, default to a basic +normalization routine. + +=cut + +sub GetClassSortKey { + my ($sort_routine, $cn_class, $cn_item) = @_; + unless (exists $loaded_routines{$sort_routine}) { + warn "attempting to use non-existent class sorting routine $sort_routine\n"; + $loaded_routines{$sort_routine} = \&_get_class_sort_key; + } + my $key = $loaded_routines{$sort_routine}->($cn_class, $cn_item); + # FIXME -- hardcoded length for cn_sort + # should replace with some way of getting column widths from + # the DB schema -- since doing this should ideally be + # independent of the DBMS, deferring for the moment. + return substr($key, 0, 30); +} + +=head2 _get_class_sort_key + +Basic sorting function. Concatenates classification part +and item, converts to uppercase, changes each run of +whitespace to '_', and removes any non-digit, non-latin +letter characters. + +=cut + +sub _get_class_sort_key { + my ($cn_class, $cn_item) = @_; + my $key = uc "$cn_class $cn_item"; + $key =~ s/\s+/_/; + $key =~ s/[^A-Z_0-9]//g; + return $key; +} + +1; + +=head1 AUTHOR + +Koha Developement team + +=cut + diff --git a/C4/ClassSortRoutine/Dewey.pm b/C4/ClassSortRoutine/Dewey.pm new file mode 100644 index 0000000000..a105d656a7 --- /dev/null +++ b/C4/ClassSortRoutine/Dewey.pm @@ -0,0 +1,90 @@ +package C4::ClassSortRoutine::Dewey; + +# Copyright (C) 2007 LibLime +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, +# Suite 330, Boston, MA 02111-1307 USA + +use strict; +require Exporter; + +use vars qw($VERSION); + +# set the version for version checking +$VERSION = 3.00; + +=head1 NAME + +C4::ClassSortRoutine::Dewey - generic call number sorting key routine + +=head1 SYNOPSIS + +use C4::ClassSortRoutine; + +my $cn_sort = GetClassSortKey('Dewey', $cn_class, $cn_item); + +=head1 FUNCTIONS + +=head2 get_class_sort_key + + my $cn_sort = C4::ClassSortRoutine::Dewey::Dewey($cn_class, $cn_item); + +Generates sorting key using the following rules: + +* Concatenates class and item part. +* Converts to uppercase. +* Removes leading and trailing whitespace +* Separates alphabetic prefix from the rest of the call number +* Splits into tokens on whitespaces and periods. +* Leaves first digit group as is. +* Converts second digit group to 15-digit long group, padded on right with zeroes. +* Converts each run of whitespace to an underscore. +* Removes any remaining non-alphabetical, non-numeric, non-underscore characters. + +=cut + +sub get_class_sort_key { + my ($cn_class, $cn_item) = @_; + + my $init = uc "$cn_class $cn_item"; + $init =~ s/^\s+//; + $init =~ s/\s+$//; + $init =~ s/^([\p{IsAlpha}]+)/$1 /; + my @tokens = split /\.|\s+/, $init; + my $digit_group_count = 0; + for (my $i = 0; $i <= $#tokens; $i++) { + if ($tokens[$i] =~ /^\d+$/) { + $digit_group_count++; + if (2 == $digit_group_count) { + $tokens[$i] = sprintf("%-15.15s", $tokens[$i]); + $tokens[$i] =~ tr/ /0/; + } + } + } + my $key = join("_", @tokens); + $key =~ s/[^\p{IsAlnum}_]//g; + + return $key; + +} + +1; + +=head1 AUTHOR + +Koha Developement team + +=cut + diff --git a/C4/ClassSortRoutine/Generic.pm b/C4/ClassSortRoutine/Generic.pm new file mode 100644 index 0000000000..ded0239c7b --- /dev/null +++ b/C4/ClassSortRoutine/Generic.pm @@ -0,0 +1,72 @@ +package C4::ClassSortRoutine::Generic; + +# Copyright (C) 2007 LibLime +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, +# Suite 330, Boston, MA 02111-1307 USA + +use strict; +require Exporter; + +use vars qw($VERSION); + +# set the version for version checking +$VERSION = 3.00; + +=head1 NAME + +C4::ClassSortRoutine::Generic - generic call number sorting key routine + +=head1 SYNOPSIS + +use C4::ClassSortRoutine; + +my $cn_sort = GetClassSortKey('Generic', $cn_class, $cn_item); + +=head1 FUNCTIONS + +=head2 get_class_sort_key + + my $cn_sort = C4::ClassSortRoutine::Generic::Generic($cn_class, $cn_item); + +Generates sorting key using the following rules: + +* Concatenates class and item part. +* Removes leading and trailing whitespace. +* Converts each run of whitespace to an underscore. +* Converts to upper-case and removes non-alphabetical, non-numeric, non-underscore characters. + +=cut + +sub get_class_sort_key { + my ($cn_class, $cn_item) = @_; + + my $key = uc "$cn_class $cn_item"; + $key =~ s/^\s+//; + $key =~ s/\s+$//; + $key =~ s/\s+/_/g; + $key =~ s/[^\p{IsAlnum}_]//g; + return $key; + +} + +1; + +=head1 AUTHOR + +Koha Developement team + +=cut + diff --git a/C4/ClassSortRoutine/LCC.pm b/C4/ClassSortRoutine/LCC.pm new file mode 100644 index 0000000000..6ef758927d --- /dev/null +++ b/C4/ClassSortRoutine/LCC.pm @@ -0,0 +1,75 @@ +package C4::ClassSortRoutine::LCC; + +# Copyright (C) 2007 LibLime +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, +# Suite 330, Boston, MA 02111-1307 USA + +use strict; +require Exporter; + +use vars qw($VERSION); + +# set the version for version checking +$VERSION = 3.00; + +=head1 NAME + +C4::ClassSortRoutine::LCC - generic call number sorting key routine + +=head1 SYNOPSIS + +use C4::ClassSortRoutine; + +my $cn_sort = GetClassSortKey('LCC', $cn_class, $cn_item); + +=head1 FUNCTIONS + +=head2 get_class_sort_key + + my $cn_sort = C4::ClassSortRoutine::LCC::LCC($cn_class, $cn_item); + +Generates sorting key for LC call numbers. + +=cut + +sub get_class_sort_key { + my ($cn_class, $cn_item) = @_; + + my $key = uc "$cn_class $cn_item"; + $key =~ s/^\s+//; + $key =~ s/\s+$//; + $key =~ s/^[^\p{IsAlnum}\s.]//g; + $key =~ s/^([A-Z]+)/$1 /; + $key =~ s/(\.[A-Z])/ $1/g; + # handle first digit group + $key =~ s/(\d+)/sprintf("%-05.5d", $1)/xe; + $key =~ s/\s+/_/g; + $key =~ s/\./_/g; + $key =~ s/__/_/g; + $key =~ s/[^\p{IsAlnum}_]//g; + + return $key; + +} + +1; + +=head1 AUTHOR + +Koha Developement team + +=cut + diff --git a/admin/classsources.pl b/admin/classsources.pl index 3e4bfe8220..d60f03ca8a 100755 --- a/admin/classsources.pl +++ b/admin/classsources.pl @@ -25,6 +25,7 @@ use C4::Context; use C4::Output; use C4::Koha; use C4::ClassSource; +use C4::ClassSortRoutine; my $script_name = "/cgi-bin/koha/admin/classsources.pl"; @@ -186,6 +187,7 @@ sub add_class_sort_rule_form { sort_rule_form => 1, confirm_op => "add_sort_rule_confirmed" ); + get_class_sort_routines($template, ""); } sub add_class_sort_rule { @@ -231,6 +233,25 @@ sub edit_class_sort_rule_form { sort_routine => $rule->{'sort_routine'} ); + get_class_sort_routines($template, $rule->{'sort_routine'}); + +} + +sub get_class_sort_routines { + my ($template, $current_routine) = @_; + + my @sort_routines = GetSortRoutineNames(); + my @sort_form = (); + + foreach my $sort_routine (sort @sort_routines) { + push @sort_form, + { + routine => $sort_routine, + selected => $sort_routine eq $current_routine ? 1 : 0 + } + } + $template->param(routines_dropdown => \@sort_form); + } sub edit_class_sort_rule { diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/classsources.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/classsources.tmpl index b3fc5fcb72..adeaca9e3d 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/classsources.tmpl +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/classsources.tmpl @@ -189,8 +189,15 @@ function CheckRuleForm(f) { value="" />
  • Filing Routine - " /> +
  • -- 2.39.5