From 585cb1dc0dd9f38c6a82bd5fb35ed3fa4f606e8b Mon Sep 17 00:00:00 2001
From: Fridolyn SOMERS <fridolyn.somers@biblibre.com>
Date: Thu, 6 Dec 2012 13:56:26 +0100
Subject: [PATCH] Bug 9223: Multiple values of AdvancedSearchTypes in
 suggestions

---
 C4/Koha.pm |   87 +++++++++++++++++++++++++++++-------------------------------
 1 file changed, 42 insertions(+), 45 deletions(-)

diff --git a/C4/Koha.pm b/C4/Koha.pm
index 3892468..16e7953 100644
--- a/C4/Koha.pm
+++ b/C4/Koha.pm
@@ -25,6 +25,7 @@ use strict;
 
 use C4::Context;
 use C4::Branch qw(GetBranchesCount);
+use C4::ItemType;
 use Memoize;
 use DateTime;
 use DateTime::Format::MySQL;
@@ -130,33 +131,23 @@ Returns a string with the name of the itemtype.
 
 =cut
 
-sub GetSupportName{
-	my ($codestring)=@_;
-	return if (! $codestring); 
-	my $resultstring;
-	my $advanced_search_types = C4::Context->preference("AdvancedSearchTypes");
-	if (!$advanced_search_types or $advanced_search_types eq 'itemtypes') {  
-		my $query = qq|
-			SELECT description
-			FROM   itemtypes
-			WHERE itemtype=?
-			order by description
-		|;
-		my $sth = C4::Context->dbh->prepare($query);
-		$sth->execute($codestring);
-		($resultstring)=$sth->fetchrow;
-		return $resultstring;
-	} else {
-        my $sth =
-            C4::Context->dbh->prepare(
-                    "SELECT lib FROM authorised_values WHERE category = ? AND authorised_value = ?"
-                    );
-        $sth->execute( $advanced_search_types, $codestring );
-        my $data = $sth->fetchrow_hashref;
-        return $$data{'lib'};
-	}
-
+sub GetSupportName {
+    my $codestring = shift;
+    return '' unless $codestring;
+    my $advanced_search_types = C4::Context->preference("AdvancedSearchTypes") || 'itemtypes';
+    for my $support_type ( split /\|/, $advanced_search_types ) {
+        if ( $support_type eq 'itemtypes' ) {
+            my $itemtype = getitemtypeinfo($codestring);
+            return $itemtype->{'description'} if $itemtype;
+        }
+        else {
+            my $lib = GetKohaAuthorisedValueLib( $support_type, $codestring );
+            return $lib if $lib;
+        }
+    }
+    return '';
 }
+
 =head2 GetSupportList
 
   $itemtypes = &GetSupportList();
@@ -185,23 +176,29 @@ build a HTML select with the following code :
 
 =cut
 
-sub GetSupportList{
-	my $advanced_search_types = C4::Context->preference("AdvancedSearchTypes");
-	if (!$advanced_search_types or $advanced_search_types eq 'itemtypes') {  
-		my $query = qq|
-			SELECT *
-			FROM   itemtypes
-			order by description
-		|;
-		my $sth = C4::Context->dbh->prepare($query);
-		$sth->execute;
-		return $sth->fetchall_arrayref({});
-	} else {
-		my $advsearchtypes = GetAuthorisedValues($advanced_search_types);
-		my @results= map {{itemtype=>$$_{authorised_value},description=>$$_{lib},imageurl=>$$_{imageurl}}} @$advsearchtypes;
-		return \@results;
-	}
+sub GetSupportList {
+    my $advanced_search_types = C4::Context->preference("AdvancedSearchTypes") || 'itemtypes';
+    my @all_supports;
+    for my $support_type ( split /\|/, $advanced_search_types ) {
+        my @supports;
+        if ( $support_type eq 'itemtypes' ) {
+            @supports = C4::ItemType->all;
+        }
+        else {
+            my $authorised_values = GetAuthorisedValues($support_type);
+            @supports = map {
+                {
+                    itemtype    => $_->{authorised_value},
+                    description => $_->{lib},
+                    imageurl    => $_->{imageurl}
+                }
+            } @$authorised_values;
+        }
+        push @all_supports, @supports;
+    }
+    return \@all_supports;
 }
+
 =head2 GetItemTypes
 
   $itemtypes = &GetItemTypes();
@@ -437,9 +434,9 @@ sub getitemtypeinfo {
     my $sth        = $dbh->prepare("select * from itemtypes where itemtype=?");
     $sth->execute($itemtype);
     my $res = $sth->fetchrow_hashref;
-
-    $res->{imageurl} = getitemtypeimagelocation( 'intranet', $res->{imageurl} );
-
+    if ( $res ) {
+        $res->{imageurl} = getitemtypeimagelocation( 'intranet', $res->{imageurl} );
+    }
     return $res;
 }
 
-- 
1.7.9.5