+ my $checked='';
+ if ($accessflags->{$flag}) {
+ $checked= 1;
+ }
+
+ my %row = ( bit => $bit,
+ flag => $flag,
+ checked => $checked,
+ flagdesc => $flagdesc );
+
+ my @sub_perm_loop = ();
+ my $expand_parent = 0;
+ if ($checked) {
+ if (exists $all_perms->{$flag}) {
+ $expand_parent = 1;
+ foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
+ push @sub_perm_loop, {
+ id => "${flag}_$sub_perm",
+ perm => "$flag:$sub_perm",
+ code => $sub_perm,
+ description => $all_perms->{$flag}->{$sub_perm},
+ checked => 1
+ };
+ }
+ }
+ } else {
+ if (exists $user_perms->{$flag}) {
+ $expand_parent = 1;
+ # put selected ones first
+ foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
+ push @sub_perm_loop, {
+ id => "${flag}_$sub_perm",
+ perm => "$flag:$sub_perm",
+ code => $sub_perm,
+ description => $all_perms->{$flag}->{$sub_perm},
+ checked => 1
+ };
+ }
+ }
+ # then ones not selected
+ if (exists $all_perms->{$flag}) {
+ foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
+ push @sub_perm_loop, {
+ id => "${flag}_$sub_perm",
+ perm => "$flag:$sub_perm",
+ code => $sub_perm,
+ description => $all_perms->{$flag}->{$sub_perm},
+ checked => 0
+ } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
+ }
+ }
+ }
+ $row{expand} = $expand_parent;
+ if ($#sub_perm_loop > -1) {
+ $row{sub_perm_loop} = \@sub_perm_loop;
+ }
+ push @loop, \%row;
+ }
+
+ if ( $bor->{'category_type'} eq 'C') {
+ my ( $catcodes, $labels ) = GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
+ my $cnt = scalar(@$catcodes);
+ $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
+ $template->param( 'catcode' => $catcodes->[0]) if $cnt == 1;