+
+
+=head2 prepare_adv_search_types
+
+ my $type = C4::Context->preference("AdvancedSearchTypes") || "itemtypes";
+ my @advanced_search_types = prepare_adv_search_types($type);
+
+Different types can be searched for in the advanced search. This takes the
+system preference that defines these types and parses it into an arrayref for
+the template.
+
+"itemtypes" is handled specially, as itemtypes aren't an authorised value.
+It also accounts for the "item-level_itypes" system preference.
+
+=cut
+
+sub prepare_adv_search_types {
+ my ($types) = @_;
+
+ my @advanced_search_types = split( /\|/, $types );
+
+ # the index parameter is different for item-level itemtypes
+ my $itype_or_itemtype =
+ ( C4::Context->preference("item-level_itypes") ) ? 'itype' : 'itemtype';
+ my $itemtypes = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
+
+ my ( $cnt, @result );
+ foreach my $advanced_srch_type (@advanced_search_types) {
+ $advanced_srch_type =~ s/^\s*//;
+ $advanced_srch_type =~ s/\s*$//;
+ if ( $advanced_srch_type eq 'itemtypes' ) {
+
+ # itemtype is a special case, since it's not defined in authorized values
+ my @itypesloop;
+ foreach my $thisitemtype (
+ sort {
+ $itemtypes->{$a}->{'translated_description'}
+ cmp $itemtypes->{$b}->{'translated_description'}
+ } keys %$itemtypes
+ )
+ {
+ my %row = (
+ number => $cnt++,
+ ccl => "$itype_or_itemtype,phr",
+ code => $thisitemtype,
+ description => $itemtypes->{$thisitemtype}->{'translated_description'},
+ imageurl => getitemtypeimagelocation(
+ 'intranet', $itemtypes->{$thisitemtype}->{'imageurl'}
+ ),
+ );
+ push @itypesloop, \%row;
+ }
+ my %search_code = (
+ advanced_search_type => $advanced_srch_type,
+ code_loop => \@itypesloop
+ );
+ push @result, \%search_code;
+ }
+ else {
+ # covers all the other cases: non-itemtype authorized values
+ my $advsearchtypes = GetAuthorisedValues($advanced_srch_type);
+ my @authvalueloop;
+ for my $thisitemtype (@$advsearchtypes) {
+ my %row = (
+ number => $cnt++,
+ ccl => $advanced_srch_type,
+ code => $thisitemtype->{authorised_value},
+ description => $thisitemtype->{'lib'},
+ imageurl => getitemtypeimagelocation(
+ 'intranet', $thisitemtype->{'imageurl'}
+ ),
+ );
+ push @authvalueloop, \%row;
+ }
+ my %search_code = (
+ advanced_search_type => $advanced_srch_type,
+ code_loop => \@authvalueloop
+ );
+ push @result, \%search_code;
+ }
+ }
+ return \@result;
+}