-my @typeloop;
-my $no_categories = 1;
-my $no_add;
-foreach my $category_type (qw(C A S P I X)) {
- my $patron_categories = Koha::Patron::Categories->search_with_library_limits({ category_type => $category_type }, {order_by => ['categorycode']});
- $no_categories = 0 if $patron_categories->count > 0;
-
- my @categoryloop;
- while ( my $patron_category = $patron_categories->next ) {
- push @categoryloop,
- { 'categorycode' => $patron_category->categorycode,
- 'categoryname' => $patron_category->description,
- 'effective_min_password_length' => $patron_category->effective_min_password_length,
- 'effective_require_strong_password' => $patron_category->effective_require_strong_password,
- 'categorycodeselected' =>
- ( defined($categorycode) && $patron_category->categorycode eq $categorycode ),
- };
- }
- my %typehash;
- $typehash{'typename'} = $category_type;
- my $typedescription = "typename_" . $typehash{'typename'};
- $typehash{'categoryloop'} = \@categoryloop;
- push @typeloop,
- { 'typename' => $category_type,
- $typedescription => 1,
- 'categoryloop' => \@categoryloop
- };
+my $patron_categories = Koha::Patron::Categories->search_with_library_limits(
+ {
+ category_type => [qw(C A S P I X)],
+ ( $guarantor_id ? ( can_be_guarantee => 1 ) : () )
+ },
+ { order_by => ['categorycode'] }
+);
+my $no_categories = ! $patron_categories->count;
+my $categories = {};
+my @patron_categories = $patron_categories->as_list;
+# When adding a guarantor we don't have a category yet, and only want to choose from the eligible categories
+unless ( !$category || $patron_categories->find( $category->id ) ){
+ $template->param( limited_category => 1 );
+ push @patron_categories, $category;
+}
+foreach my $patron_category ( @patron_categories ) {
+ push @{ $categories->{ $patron_category->category_type } }, $patron_category;