[Koha-patches] [PATCH] Bug 10892 : Make facets customizable - step 1 store configuration in YAML block

Mathieu Saby mathieu.saby at univ-rennes2.fr
Tue Sep 17 18:08:42 CEST 2013


This is the first of a group of patches whose goal is to make facets configurable.
This patch stores facet configuration in a YAML block, within Koha.pm file.
Next steps will be to use an external YAML file.

No functional changes are expected, except for UNIMARC :
I took the occasion for fixing location facets, which were broken (wrongly build on 995$c instead of 995$e).

To test :
1. make some searches, use facets
2. apply
3. make the same searches, use facets the same way. No differences should be visible
4. in a UNIMARC Koha, check location facets are visible after applying the patch
---
 C4/Koha.pm   |  271 ++++++++++++++++++++++++++++++++++------------------------
 C4/Search.pm |   34 ++++++--
 2 files changed, 184 insertions(+), 121 deletions(-)

diff --git a/C4/Koha.pm b/C4/Koha.pm
index c5b32a6..b67ff05 100644
--- a/C4/Koha.pm
+++ b/C4/Koha.pm
@@ -29,6 +29,7 @@ use Koha::DateUtils qw(dt_from_string);
 use Memoize;
 use DateTime::Format::MySQL;
 use autouse 'Data::Dumper' => qw(Dumper);
+use YAML::Syck;
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $DEBUG);
 
@@ -674,121 +675,167 @@ sub getallthemes {
     return @themes;
 }
 
+sub get_facets_from_yaml {
+    my $marcflavor = shift;
+    my $YAML_conf;
+    if ($marcflavor eq "UNIMARC") {
+        $YAML_conf = <<END;
+topics:
+  idx: 'su-to'
+  label: 'Topics'
+  tags:
+    - 600ab
+    - 601ab
+    - 602ab
+    - 604at
+    - 605a
+    - 606ax
+    - 610a
+  sep: ' - '
+  order: 1
+places:
+  idx: 'su-geo'
+  label: 'Places'
+  tags:
+    - 607a
+  sep: ' - '
+  order: 2
+titles:
+  idx: 'su-ut'
+  label: 'Titles'
+  tags:
+    - 500a
+    - 501a
+    - 503a
+  sep: ' - '
+  order: 3
+authors:
+  idx: 'au'
+  label: 'Authors'
+  tags:
+    - 700ab
+    - 701ab
+    - 702ab
+  order: 4
+series:
+  idx: 'se'
+  label: 'Series'
+  tags:
+    - 225a
+  sep: ', '
+  order: 5
+branches:
+  idx: 'branch'
+  label: 'Libraries'
+  tags:
+    - 995b
+  order: 6
+location:
+  idx: 'location'
+  label: 'Location'
+  tags:
+    - 995e
+  order: 7
+
+END
+    } else {
+        $YAML_conf = <<END;
+topics:
+  idx: 'su-to'
+  label: 'Topics'
+  tags:
+    - 650a
+  sep: ' - '
+  order: 1
+places:
+  idx: 'su-geo'
+  label: 'Places'
+  tags:
+    - 651a
+  sep: ' - '
+  order: 2
+titles:
+  idx: 'su-ut'
+  label: 'Titles'
+  tags:
+    - 630a
+  sep: ' - '
+  order: 3
+authors:
+  idx: 'au'
+  label: 'Authors'
+  tags:
+    - 100a
+    - 110a
+    - 700a
+  order: 4
+peopleandorganizations:
+  idx: 'su-na'
+  label: 'People and Organizations'
+  tags:
+    - 600a
+    - 610a
+    - 611a
+series:
+  idx: 'se'
+  label: 'Series'
+  tags:
+    - 440a
+    - 490a
+  sep: ', '
+  order: 5
+branches:
+  idx: 'branch'
+  label: 'Libraries'
+  tags:
+    - 995b
+  order: 6
+  sort: alpha_desc
+location:
+  idx: 'location'
+  label: 'Location'
+  tags:
+    - 995c
+  order: 7
+itemtypes:
+  idx: 'itype'
+  label: 'ItemTypes'
+  tags:
+    - 952y
+    - 942c
+  order: 8
+  sep: ' - '
+
+END
+    }
+return YAML::Load($YAML_conf);
+}
+
 sub getFacets {
-    my $facets;
-    if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) {
-        $facets = [
-            {
-                idx   => 'su-to',
-                label => 'Topics',
-                tags  => [ qw/ 600ab 601ab 602a 604at 605a 606ax 610a / ],
-                sep   => ' - ',
-            },
-            {
-                idx   => 'su-geo',
-                label => 'Places',
-                tags  => [ qw/ 607a / ],
-                sep   => ' - ',
-            },
-            {
-                idx   => 'su-ut',
-                label => 'Titles',
-                tags  => [ qw/ 500a 501a 503a / ],
-                sep   => ', ',
-            },
-            {
-                idx   => 'au',
-                label => 'Authors',
-                tags  => [ qw/ 700ab 701ab 702ab / ],
-                sep   => C4::Context->preference("UNIMARCAuthorsFacetsSeparator"),
-            },
-            {
-                idx   => 'se',
-                label => 'Series',
-                tags  => [ qw/ 225a / ],
-                sep   => ', ',
-            },
-            ];
-
-            my $library_facet;
-            unless ( C4::Context->preference("singleBranchMode") || GetBranchesCount() == 1 ) {
-                $library_facet = {
-                    idx  => 'branch',
-                    label => 'Libraries',
-                    tags        => [ qw/ 995b / ],
-                };
-            } else {
-                $library_facet = {
-                    idx  => 'location',
-                    label => 'Location',
-                    tags        => [ qw/ 995c / ],
-                };
-            }
-            push( @$facets, $library_facet );
+    my $all_facets;
+    my $marcflavor = C4::Context->preference("marcflavour");
+    $all_facets = get_facets_from_yaml ($marcflavor);
+# gets facets from YAML data
+# return a reference to a hash with following values of each facet
+# {facet_n}-> {idx => index_n                   string
+#              label => label_n                  string
+#              tags => [ fieldandsubfields       string : MARC tag code followed by MARC subtags codes
+#                        fieldandsubfields
+#                        fieldandsubfields ]
+#              order => order                    digit, or undef (don't show the facet). For the moment, only used to hide facet if undef.
+#              sep => separator}                 string
+
+# Some values are special and must be modified:
+# author
+    if ($marcflavor eq "UNIMARC") {
+	$all_facets->{authors}->{sep} = C4::Context->preference("UNIMARCAuthorsFacetsSeparator");
     }
-    else {
-        $facets = [
-            {
-                idx   => 'su-to',
-                label => 'Topics',
-                tags  => [ qw/ 650a / ],
-                sep   => '--',
-            },
-            #        {
-            #        idx   => 'su-na',
-            #        label => 'People and Organizations',
-            #        tags  => [ qw/ 600a 610a 611a / ],
-            #        sep   => 'a',
-            #        },
-            {
-                idx   => 'su-geo',
-                label => 'Places',
-                tags  => [ qw/ 651a / ],
-                sep   => '--',
-            },
-            {
-                idx   => 'su-ut',
-                label => 'Titles',
-                tags  => [ qw/ 630a / ],
-                sep   => '--',
-            },
-            {
-                idx   => 'au',
-                label => 'Authors',
-                tags  => [ qw/ 100a 110a 700a / ],
-                sep   => ', ',
-            },
-            {
-                idx   => 'se',
-                label => 'Series',
-                tags  => [ qw/ 440a 490a / ],
-                sep   => ', ',
-            },
-            {
-                idx   => 'itype',
-                label => 'ItemTypes',
-                tags  => [ qw/ 952y 942c / ],
-                sep   => ', ',
-            },
-            ];
-
-            my $library_facet;
-            unless ( C4::Context->preference("singleBranchMode") || GetBranchesCount() == 1 ) {
-                $library_facet = {
-                    idx  => 'branch',
-                    label => 'Libraries',
-                    tags        => [ qw / 952b / ],
-                };
-            } else {
-                $library_facet = {
-                    idx => 'location',
-                    label => 'Location',
-                    tags => [ qw / 952c / ],
-                };
-            }
-            push( @$facets, $library_facet );
+    unless ( C4::Context->preference("singleBranchMode") || GetBranchesCount() == 1 ) {
+	undef $all_facets->{location}->{order};
+    } else {
+        undef $all_facets->{branches}->{order};
     }
-    return $facets;
+    return $all_facets;
 }
 
 =head2 get_infos_of
diff --git a/C4/Search.pm b/C4/Search.pm
index 0453b91..2b79b2a 100644
--- a/C4/Search.pm
+++ b/C4/Search.pm
@@ -340,6 +340,16 @@ sub getRecords {
     my $facets_counter = ();
     my $facets_info    = ();
     my $facets         = getFacets();
+# result : reference to a hash with the following informations for each facet:
+# facet_n => {idx => index_n                   string
+#            label => label_n                  string
+#            tags => [ fieldandsubfields       string : MARC tag code followed by MARC subtags codes
+#                      fieldandsubfields
+#                      fieldandsubfields ]
+#            order => 0                        digit : if 0 or void, don't show the facet
+#            sort => type_of_string            string : 4 values : alpha_asc, alpha_desc, occur_asc, occur_desc
+#            sep => separator}                 string
+
     my $facets_maxrecs = C4::Context->preference('maxRecordsForFacets')||20;
 
     my @facets_loop;    # stores the ref to array of hashes for template facets loop
@@ -501,13 +511,14 @@ sub getRecords {
 
                     my $jmax =
                       $size > $facets_maxrecs ? $facets_maxrecs : $size;
-                    for my $facet (@$facets) {
+# only display facets with {order} value undef
+                    my @active_facets =  grep {defined $facets->{$_}->{order}} keys %{ $facets };
+                    foreach my $facet_key (@active_facets) {
                         for ( my $j = 0 ; $j < $jmax ; $j++ ) {
                             my $render_record =
                               $results[ $i - 1 ]->record($j)->render();
                             my @used_datas = ();
-                            foreach my $tag ( @{ $facet->{tags} } ) {
-
+                            foreach my $tag ( @{ $facets->{$facet_key}->{tags}} ) {
                                 # avoid first line
                                 my $tag_num = substr( $tag, 0, 3 );
                                 my $letters = substr( $tag, 3 );
@@ -529,19 +540,19 @@ sub getRecords {
                                             push @values, $value;
                                         }
                                     }
-                                    my $data = join( $facet->{sep}, @values );
+                                    my $data = join( $facets->{$facet_key}-> {sep}, @values );
                                     unless ( $data ~~ @used_datas ) {
-                                        $facets_counter->{ $facet->{idx} }
+                                        $facets_counter->{ $facets->{$facet_key}-> {idx} }
                                           ->{$data}++;
                                         push @used_datas, $data;
                                     }
                                 }    # fields
                             }    # field codes
                         }    # records
-                        $facets_info->{ $facet->{idx} }->{label_value} =
-                          $facet->{label};
-                        $facets_info->{ $facet->{idx} }->{expanded} =
-                          $facet->{expanded};
+                        $facets_info->{ $facets->{$facet_key}-> {idx} }->{label_value} =
+                          $facets->{$facet_key}-> {label};
+                        $facets_info->{ $facets->{$facet_key}-> {idx} }->{expanded} =
+                          $facets->{$facet_key}-> {expanded};
                     }    # facets
                 }
 
@@ -621,9 +632,14 @@ sub getRecords {
 
                # also, if it's a location code, use the name instead of the code
                                 if ( $link_value =~ /location/ ) {
+                                    warn $link_value ;
+                                    warn $one_facet;
+                                    warn $opac;
+                                    warn $facet_label_value;
                                     $facet_label_value =
                                       GetKohaAuthorisedValueLib( 'LOC',
                                         $one_facet, $opac );
+                                      warn $facet_label_value;
                                 }
 
                 # but we're down with the whole label being in the link's title.
-- 
1.7.9.5


-- 
Mathieu Saby
Service d'Informatique Documentaire
Service Commun de la Documentation
Université Rennes 2
Téléphone : 02 99 14 12 65
Courriel : mathieu.saby at univ-rennes2.fr



More information about the Koha-patches mailing list