<%once>;
my $key = 'element_type';
my $pkg_name = get_package_name($key);
my $disp = get_disp_name($key);
# This coderef displays profile link labels.
my $prof_sub = sub {

    # Get the object ID.
    my $id = $_[0]->get_id;

    # Assume user can view the profile.
    my $edit = ['View', "/admin/profile/$key/$id", ''];

    # Change the label to "Edit" if they can edit it.
    $edit->[0] = 'Edit' if chk_authz($_[0], EDIT, 1);

    return [ $edit, [ 'Log', "/admin/events/$key/$id", ''] ];
};

# This coderef determines whether to include an Add link or not.
my $add_sub = sub {
    return chk_authz($pkg_name, CREATE, 1) ?
        ['Add', "/admin/profile/$key"] : undef;
};
</%once>
<%init>;
# Figure out where we are.
my ($section, $mode, undef, $id) = parse_uri($r->uri);
undef $id if $id && $id eq '';
my ($can_del, $sel_label, $sel_widg, $title, $context, $state_key, $exclude);
my ($constrain, $behavior) = ({}, 'expand');

# Set the title, selection label, and widget.
if (defined $id) {
    # defined $id means we're adding subelements to a profile
    my $et = $pkg_name->lookup({ id => $id });
    my %already_added = map { $_->get_id => 1 } $et->get_containers;
    $constrain->{top_level} = 0;
    $exclude = sub {
        return 1 unless chk_authz($_[0], READ, 1);
        return 1 if $already_added{$_[0]->get_id};
        return 0;
    };
    $state_key = 'subelement_type';
    $title     = 'Choose Subelements';
    my $name   = $et->get_name;
    $context   = "Admin | Profile | $disp | &quot;$name&quot; | Subelements";
    $sel_label = 'Add to Element Type';
    $sel_widg  = 'element_type|addElementType_cb'
} else {
    # undef $id means we're listing elements in the manager
    $exclude = sub { ! chk_authz($_[0], READ, 1) };
    $state_key = 'element_type';
    $title     = "$disp Manager";
    $context   = "Admin | Manager | $disp";
    $sel_label = 'Delete';
    $sel_widg  = 'listManager|deactivate_cb';
}

# This coderef determines whether the user can delete objects.
my $sel_sub = sub {
    if (chk_authz($_[0], EDIT, 1)) {
        # User can delete.
        $can_del = 1;
        return [$sel_label, $sel_widg];
    }
    return undef;
};
</%init>

<%perl>;
# Output the header.
$m->comp('/widgets/wrappers/sharky/header.mc',
         title => $title,
         context => $context);
# Output the search widget.

my $searchType = "triple";

$m->comp(
    '/widgets/search/search.mc',
   object    => $key,
   field     => 'name',
   state_key => $state_key,
   type      => $searchType,
);

# Output a form tag.
$m->out(qq{<form method="post" action="${ \$r->uri }" name="manager"});
if ($id) {
    $m->out(">\n");
} else {
    $m->out(qq{onsubmit="return confirmDeletions()">});
}

$m->comp('/widgets/profile/hidden.mc',
          name  => 'element_type_id',
          value => $id
) if ($id);

$m->comp('/widgets/wrappers/sharky/table_top.mc',
          caption => 'Existing %n',
          object  => $key
);

# Output the list of found objects.
$m->comp('/widgets/listManager/listManager.mc',
         behavior  => $behavior,
         object    => $key,
         state_key => $state_key,
         constrain => $constrain,
         profile   => ($id) ? undef : $prof_sub,
         fields    => [qw(name key_name biz_class_id)],
         exclude   => $exclude,
         addition  => defined $id ? undef : $add_sub,
         select    => $sel_sub,
         alter     => {
             biz_class_id => sub {
                 return 'Subelement' unless $_[1]->get_top_level;
                 return get_disp_name($_[0]);
             },
        },
);

$m->comp('/widgets/wrappers/sharky/table_bottom.mc');

# Output the form closing tag if necessary.
if ($id) {
    $m->comp('/widgets/buttons/submit.mc',
              disp      => 'Add Elements',
              widget    => 'element_type',
              cb        => 'doRedirect_cb',
              button    => 'add_elements_lgreen',
              value     => 1,
              useTable  => 0,
    );
} elsif ($can_del) {
    $m->comp('/widgets/buttons/submit.mc',
              disp      => 'Delete Checked',
              name      => 'action',
              button    => 'delete_checked_red',
              value     => "$sel_label Checked",
              useTable  => 0,
    );
}

$m->out('</form>');

# Output the footer.
$m->comp('/widgets/wrappers/sharky/footer.mc', param => \%ARGS);
</%perl>

<%doc>
###############################################################################

=head1 NAME

/admin/manager/dhandler - Admin tools type manager

=head1 VERSION

$LastChangedRevision$

=head1 DATE

$LastChangedDate$

=head1 SYNOPSIS

/admin/manager/element_type/index.html

=head1 DESCRIPTION

This manager manages element type objects.

</%doc>
