call number work part 2 -- added framework for call number filing routines
authorGalen Charlton <galen.charlton@liblime.com>
Mon, 22 Oct 2007 02:23:26 +0000 (21:23 -0500)
committerJoshua Ferraro <jmf@liblime.com>
Mon, 22 Oct 2007 03:11:37 +0000 (22:11 -0500)
Signed-off-by: Chris Cormack <crc@liblime.com>
Signed-off-by: Joshua Ferraro <jmf@liblime.com>
C4/ClassSortRoutine.pm [new file with mode: 0644]
C4/ClassSortRoutine/Dewey.pm [new file with mode: 0644]
C4/ClassSortRoutine/Generic.pm [new file with mode: 0644]
C4/ClassSortRoutine/LCC.pm [new file with mode: 0644]
admin/classsources.pl
koha-tmpl/intranet-tmpl/prog/en/modules/admin/classsources.tmpl

diff --git a/C4/ClassSortRoutine.pm b/C4/ClassSortRoutine.pm
new file mode 100644 (file)
index 0000000..b4fa7a4
--- /dev/null
@@ -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 <info@koha.org>
+
+=cut
+
diff --git a/C4/ClassSortRoutine/Dewey.pm b/C4/ClassSortRoutine/Dewey.pm
new file mode 100644 (file)
index 0000000..a105d65
--- /dev/null
@@ -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 <info@koha.org>
+
+=cut
+
diff --git a/C4/ClassSortRoutine/Generic.pm b/C4/ClassSortRoutine/Generic.pm
new file mode 100644 (file)
index 0000000..ded0239
--- /dev/null
@@ -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 <info@koha.org>
+
+=cut
+
diff --git a/C4/ClassSortRoutine/LCC.pm b/C4/ClassSortRoutine/LCC.pm
new file mode 100644 (file)
index 0000000..6ef7589
--- /dev/null
@@ -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 <info@koha.org>
+
+=cut
+
index 3e4bfe8..d60f03c 100755 (executable)
@@ -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 {
index b3fc5fc..adeaca9 100644 (file)
@@ -189,8 +189,15 @@ function CheckRuleForm(f) {
                   value="<!-- TMPL_VAR name="description" escape="HTML" -->" />
        </li>
        <li><span class="label">Filing Routine</span>
-           <input type="text" id="sort_routine" name="sort_routine" size="30" maxlength="30" 
-                  value="<!-- TMPL_VAR name="sort_routine" escape="HTML" -->" />
+           <select id="sort_routine" name="sort_routine">
+           <!-- TMPL_LOOP name="routines_dropdown" -->
+             <!-- TMPL_IF name="selected" -->
+             <option value="<!-- TMPL_VAR name="routine" -->" selected="selected"><!-- TMPL_VAR name="routine" --></option>
+             <!-- TMPL_ELSE -->
+             <option value="<!-- TMPL_VAR name="routine" -->"><!-- TMPL_VAR name="routine" --></option>
+             <!-- /TMPL_IF -->
+           <!-- /TMPL_LOOP -->
+           </select>
        </li>
     </ol>
   </fieldset>