<%once>;
my $key = 'element';
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);
my ($constrain, $behavior) = ({}, 'expand');

if (exists $ARGS{elem_type}) {
    if ($ARGS{'search|substr_cb'}) {
        # Delete elem_type if we're showing search results.
        delete $ARGS{elem_type};
    } elsif ($ARGS{elem_type}) {
        # Determine list contraint.
        $constrain = { type__id => $ARGS{elem_type} };
        $behavior = 'narrow';
    }
}

# Set the title, selection label, and widget.
if (defined $id) {
    $constrain->{top_level} = 0;
    $title = 'Choose Subelements';
    my $name = $pkg_name->lookup({ id => $id })->get_name;
    $context = "Admin | Profile | $disp | &quot;$name&quot; | Subelements";
    $sel_label = 'Add to Element';
    $sel_widg  = 'element|addElement_cb'
} else {
    $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 $groupList;
my $searchType = "triple";

$groupList = $m->scomp('/widgets/select_object/select_object.mc',
		       object => 'element_type',
		       default => ['', 'All Elements'],
		       indent  => 150,
		       field => 'name',
		       name => 'elem_type',
		       disp => 'Or Pick a Type',
		       reset_key => $ARGS{elem_type},
		       selected => $ARGS{elem_type},
		       js => qq{onChange="location.href='} . $r->uri .
                             "?elem_type=' + " .
                             qq{escape(this.options[this.selectedIndex].value)"}
		      );

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

# 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->out(qq {<input type="hidden" name="element_id" value=$id>\n} ) if ($id);

# Output the list of found objects.
$m->comp('/widgets/listManager/listManager.mc',
	 behavior  => $behavior,
	 object    => $key,
         constrain => $constrain,
	 profile   => ($id) ? undef : $prof_sub,
	 title     => 'Existing %n',
	 fields    => [qw(name description type_name)],
         exclude   => sub { ! chk_authz($_[0], READ, 1) },
	 addition  => defined $id ? undef : $add_sub,
	 select    => $sel_sub,
);

# Output the form closing tag if necessary.
if ($id) {
    $m->out(qq{<br /><input type="image" src="/media/images/$lang_key/add_elements_lgreen.gif" border=0 name="element|doRedirect_cb" value="1" />\n</form>});
} elsif ($can_del) {
    $m->out(qq{<br /><input type="image" src="/media/images/$lang_key/delete_checked_red.gif" border=0 name="action"}
	    . qq{value="$sel_label Checked" />\n</form>});
} else  {
    $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/index.html

=head1 DESCRIPTION

This manager manages element objects.

</%doc>
