Bug 7380: Refactor the patron search box header
authorJonathan Druart <jonathan.druart@biblibre.com>
Tue, 15 Jul 2014 10:44:23 +0000 (12:44 +0200)
committerTomas Cohen Arazi <tomascohen@gmail.com>
Tue, 19 May 2015 13:10:31 +0000 (10:10 -0300)
A lot of page uses the patron search box header include (git grep
patron-search.inc for the list) and the branch and category lists are
not generated for all of them.

For instance, on a patron detail page, there is a [+] link, but nothing
append on clicking on it.

This patch add a new template plugin "Categories" to get all patron
categories from the templates.

Test plan:
1/ Verify the [+] is working on all patron page (if that works for 2,
that should work for all).
2/ Launch a search from the header box and verify the library and
category dropdown lists select the correct value.
3/ Launch a search on the patron search page (patron home) and verify
the search is working correctly on selecting a category and/or a library

Followed test plan. Works as expected.
Signed-off-by: Marc VĂ©ron <veron@veron.ch>
Signed-off-by: Nicole Engard <nengard@bywatersolutions.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
Koha/Template/Plugin/Branches.pm
Koha/Template/Plugin/Categories.pm [new file with mode: 0644]
koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc
koha-tmpl/intranet-tmpl/prog/en/modules/members/member.tt
members/member.pl

index f197e99..880b2c4 100644 (file)
@@ -1,6 +1,7 @@
 package Koha::Template::Plugin::Branches;
 
 # Copyright ByWater Solutions 2012
+# Copyright BibLibre 2014
 
 # This file is part of Koha.
 #
@@ -53,4 +54,33 @@ sub GetURL {
     return $b->{branchurl};
 }
 
+sub all {
+    my ( $self, $params ) = @_;
+    my $selected = $params->{selected};
+    my $dbh = C4::Context->dbh;
+    my @params;
+    my $query = q|
+        SELECT branchcode, branchname
+        FROM branches
+    |;
+    if (    C4::Branch::onlymine
+        and C4::Context->userenv
+        and C4::Context->userenv->{branch} )
+    {
+        $query .= q| WHERE branchcode = ? |;
+        push @params, C4::Context->userenv->{branch};
+    }
+    $query .= q| ORDER BY branchname|;
+    my $branches = $dbh->selectall_arrayref( $query, { Slice => {} }, @params );
+
+    if ( $selected ) {
+        for my $branch ( @$branches ) {
+            if ( $branch->{branchcode} eq $selected ) {
+                $branch->{selected} = 1;
+            }
+        }
+    }
+    return $branches;
+}
+
 1;
diff --git a/Koha/Template/Plugin/Categories.pm b/Koha/Template/Plugin/Categories.pm
new file mode 100644 (file)
index 0000000..86630ca
--- /dev/null
@@ -0,0 +1,63 @@
+package Koha::Template::Plugin::Categories;
+
+# Copyright 2013-2014 BibLibre
+#
+# 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 3 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, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Template::Plugin;
+use base qw( Template::Plugin );
+
+use C4::Category;
+
+sub all {
+    my ( $self, $params ) = @_;
+    my $selected = $params->{selected};
+
+    my @categories = C4::Category->all;
+    if ( $selected ) {
+        for my $category ( @categories ) {
+            if ( $category->{categorycode} eq $selected ) {
+                $category->{selected} = 1;
+            }
+        }
+    }
+    return @categories;
+}
+
+1;
+
+=head1 NAME
+
+Koha::Template::Plugin::Categories - TT Plugin for categories
+
+=head1 SYNOPSIS
+
+[% USE Categories %]
+
+[% Categories.all() %]
+
+=head1 ROUTINES
+
+=head2 all
+
+In a template, you can get the all categories with
+the following TT code: [% Categories.all() %]
+
+=head1 AUTHOR
+
+Jonathan Druart <jonathan.druart@biblibre.com>
+
+=cut
index 7739cc3..7723e3a 100644 (file)
@@ -1,4 +1,6 @@
 [% USE Koha %]
+[% USE Branches %]
+[% USE Categories %]
 <div class="gradient">
 <h1 id="logo"><a href="/cgi-bin/koha/mainpage.pl">[% LibraryName %]</a></h1><!-- Begin Patrons Resident Search Box -->
 <div id="header_search">
             </select>
         </p>
 
+        <p>
+            <label for="branchcode">Library: </label>
+            [% SET branches = Branches.all( selected => branchcode ) %]
+            <select name="branchcode" id="branchcode">
+                [% IF branches.size != 1 %]
+                  <option value="">Any</option>
+                [% END %]
+                [% FOREACH b IN branches %]
+                    [% IF b.selected %]
+                        <option value="[% b.branchcode %]" selected="selected">[% b.branchname %]</option>
+                    [% ELSE %]
+                        <option value="[% b.branchcode %]">[% b.branchname %]</option>
+                    [% END %]
+                [% END %]
+            </select>
+        </p>
 
-        [% IF ( branchloop ) %]
-        <p> <label for="branchcode">Library: </label>
-        <select name="branchcode" id="branchcode">
-            [% IF branchloop.size != 1 %]
-              <option value="">Any</option>
-            [% END %]
-            [% FOREACH branchloo IN branchloop %]
-            [% IF ( branchloo.selected ) %]
-            <option value="[% branchloo.value %]" selected="selected">[% branchloo.branchname %]</option>[% ELSE %]
-            <option value="[% branchloo.value %]">[% branchloo.branchname %]</option>[% END %]
-          [% END %]</select>
-        [% END %]
-        [% IF ( categories ) %]
-            <p>
-            <label for="categorycode">Category: </label><select name="categorycode" id="categorycode">
-            <option value="">Any</option>[% FOREACH categorie IN categories %]
-            [% IF ( categorie.selected ) %]
-            <option value="[% categorie.categorycode %]" selected="selected">[% categorie.description %]</option>[% ELSE %]
-            <option value="[% categorie.categorycode %]">[% categorie.description %]</option>[% END %]
-          [% END %]</select>
-            </p>
-        [% END %]
+        <p>
+            <label for="categorycode">Category: </label>
+            [% SET categories = Categories.all( selected => categorycode ) %]
+            <select name="categorycode" id="categorycode">
+                <option value="">Any</option>
+                [% FOREACH categorie IN categories %]
+                    [% IF ( categorie.selected ) %]
+                        <option value="[% categorie.categorycode %]" selected="selected">[% categorie.description %]</option>
+                    [% ELSE %]
+                        <option value="[% categorie.categorycode %]">[% categorie.description %]</option>
+                    [% END %]
+                [% END %]
+            </select>
+        </p>
     </div>
     </form>
 </div>
index 7ecf271..cd2608b 100644 (file)
@@ -1,5 +1,7 @@
 [% USE Koha %]
 [% USE ColumnsSettings %]
+[% USE Branches %]
+[% USE Categories %]
 [% INCLUDE 'doc-head-open.inc' %]
 <title>Koha &rsaquo; Patrons [% IF ( searching ) %]&rsaquo; Search results[% END %]</title>
 [% INCLUDE 'doc-head-close.inc' %]
@@ -452,24 +454,26 @@ function filterByFirstLetterSurname(letter) {
             </li>
             <li>
               <label for="categorycode_filter">Category:</label>
+              [% SET categories = Categories.all( selected => categorycode ) %]
               <select id="categorycode_filter">
                 <option value="">Any</option>
                 [% FOREACH cat IN categories %]
                   [% IF cat.selected %]
-                    <option selected="selected" value="[% cat.categorycode %]">[% cat.description | html_entity %]</option>
+                    <option selected="selected" value="[% cat.categorycode %]">[% cat.description %]</option>
                   [% ELSE %]
-                    <option value="[% cat.categorycode %]">[% cat.description | html_entity %]</option>
+                    <option value="[% cat.categorycode %]">[% cat.description %]</option>
                   [% END %]
                 [% END %]
               </select>
             </li>
             <li>
               <label for="branchcode_filter">Library:</label>
+              [% SET branches = Branches.all( selected => branchcode ) %]
               <select id="branchcode_filter">
-                [% IF branchloop.size != 1 %]
+                [% IF branches.size != 1 %]
                   <option value="">Any</option>
                 [% END %]
-                [% FOREACH b IN branchloop %]
+                [% FOREACH b IN branches %]
                   [% IF b.selected %]
                     <option selected="selected" value="[% b.branchcode %]">[% b.branchname %]</option>
                   [% ELSE %]
index a93e79a..71b1f40 100755 (executable)
@@ -75,37 +75,6 @@ if ( $searchfieldstype eq "dateofbirth" ) {
     $searchmember = output_pref({dt => dt_from_string($searchmember), dateformat => 'iso', dateonly => 1});
 }
 
-my $branches = C4::Branch::GetBranches;
-my @branches_loop;
-if ( C4::Branch::onlymine ) {
-    my $userenv = C4::Context->userenv;
-    my $branch = C4::Branch::GetBranchDetail( $userenv->{'branch'} );
-    push @branches_loop, {
-        value => $branch->{branchcode},
-        branchcode => $branch->{branchcode},
-        branchname => $branch->{branchname},
-        selected => 1
-    }
-} else {
-    foreach ( sort { lc($branches->{$a}->{branchname}) cmp lc($branches->{$b}->{branchname}) } keys %$branches ) {
-        my $selected = 0;
-        $selected = 1 if($patron->{branchcode} and $patron->{branchcode} eq $_);
-        push @branches_loop, {
-            value => $_,
-            branchcode => $_,
-            branchname => $branches->{$_}->{branchname},
-            selected => $selected
-        };
-    }
-}
-
-my @categories = C4::Category->all;
-if ( $patron->{categorycode} ) {
-    foreach my $category ( grep { $_->{categorycode} eq $patron->{categorycode} } @categories ) {
-        $category->{selected} = 1;
-    }
-}
-
 $template->param( 'alphabet' => C4::Context->preference('alphabet') || join ' ', 'A' .. 'Z' );
 
 my $view = $input->request_method() eq "GET" ? "show_form" : "show_results";
@@ -113,8 +82,6 @@ my $view = $input->request_method() eq "GET" ? "show_form" : "show_results";
 $template->param(
     patron_lists => [ GetPatronLists() ],
     searchmember        => $searchmember,
-    branchloop          => \@branches_loop,
-    categories          => \@categories,
     branchcode          => $patron->{branchcode},
     categorycode        => $patron->{categorycode},
     searchtype          => $input->param('searchtype') || 'start_with',