%#-- Begin HTML --#
<& '/widgets/wrappers/sharky/header.mc',
    title => "$disp Profile",
    context => "Admin | Profile | $disp | $crumb"
&>
<& '/javascripts/profile/formValidator.js', id => $id &>
<form method="post" action="<% $r->uri %>" name="cat_profile" onSubmit="return confirmChanges(this)">
<%perl>;
my $reset;
if (defined $id) {
    $m->comp('/widgets/profile/hidden.mc', value => $id, name => 'category_id');
    $reset = $id;
} else {
    $reset = $ARGS{reset} || time;
    $m->comp('/widgets/profile/hidden.mc', value => $reset, name => 'reset');
}

# Output the crucial fields.
$m->comp("/widgets/wrappers/sharky/table_top.mc",
  	 caption => "Properties", number  => 1);
$m->comp('/widgets/profile/dumpRemainingFields.mc', objref => $cat,
	 readOnly => $no_edit,
	 fieldsUsed => { uri => 1, ad_string => 1,
			 ad_string2 => 1, directory => 1 });

if (defined $id) {
    # It's an existing category. Just show them the URI.
    $m->comp('/widgets/profile/displayFormElement.mc', objref => $cat,
	     key => 'uri', readOnly => 1);
} else {
    # Output a field for the directory.
    $m->comp('/widgets/profile/displayFormElement.mc', objref => $cat,
	     key => 'directory');

    # Output a list of parent categories to choose from.
    # Remove any that are children of the category itself.
    my $p_id = $cat->get_parent;
    $p_id &&= $p_id->get_id;

    $m->comp('/widgets/select_object/select_object.mc', object => 'category',
	     name => 'parent_id', field => 'uri', disp => 'Parent Category',
	     selected => ( defined($p_id) ? $p_id : exists $ARGS{parent_id} ? $ARGS{parent_id} : 'none'),
	     sort_field => 'uri', reset_key => $reset, req => 1, exclude => [map($_->get_id, $cat->children), defined($id) ? $id : ()]);
}
$m->comp("/widgets/wrappers/sharky/table_bottom.mc");

# Output the keywords.
$m->comp("/widgets/wrappers/sharky/table_top.mc",
  	 caption => "Keywords", number  => 2);
my $kws = $cat->keywords;
$m->comp('/widgets/add_more/add_more.mc', type => 'keyword', param => \%ARGS,
	 fields => [qw(keyword)], name => 'keyword', deleteLabelOnly => 1,
	 reset_key => $reset, objs => $kws, incr => 4, no_edit => [qw(keyword)],
	 num => @$kws >= 4 ? @$kws + 2 : 4, read_only => $no_edit,
	 formName => 'cat_profile');
$m->comp("/widgets/wrappers/sharky/table_bottom.mc");

# Output the ad strings.
$m->comp("/widgets/wrappers/sharky/table_top.mc",
  	 caption => "Ad Strings", number  => 3);
$m->comp('/widgets/profile/dumpRemainingFields.mc', objref => $cat,
	 readOnly => $no_edit,
	 fieldsUsed => { uri => 1, name => 1, description => 1,
			 directory => 1 });
$m->comp("/widgets/wrappers/sharky/table_bottom.mc");

# Add a checkbox to cascade delete.
unless ($no_edit || $no_del) {
    $m->comp('/widgets/profile/checkbox.mc', name => 'delete_cascade',
	     label_after => 1, value => 1,
	     disp => "<span class=burgandyLabel>Delete this Category and All its Subcategories</span>");
    $m->out("<br />\n");
}
# Add the buttons.
$m->comp('/widgets/profile/formButtons.mc', type => $type, section => $section,
	 no_del => $no_del, no_save => $no_edit);

</%perl>
</form>
<& '/widgets/wrappers/sharky/footer.mc', param => \%ARGS &>
%#-- End HTML --#


%#-- Once Section --#
<%once>;
my $class = 'Bric::Biz::Category';
my $section = 'admin';
my $type = 'category';
my $widget = 'profile';
my $disp = get_disp_name($type);
my $root_id = $class->ROOT_CATEGORY_ID;
</%once>

%#-- Args Section --#
<%args>
$id => undef
</%args>

%#-- Init Section --#
<%init>;
$id ||= $ARGS{category_id} unless defined $id;
# Instantiate an object.
my $cat = $ARGS{obj} ? $ARGS{obj} : defined $id ? $class->lookup({ id => $id})
  : $class->new;
$id = $cat->get_id unless defined $id;

# Check authorization.
chk_authz($cat, $id ? READ : CREATE);
my $no_edit = !chk_authz($cat, ($id ? EDIT : CREATE), 1);
my $no_del = ! defined $id || $no_edit || $id == $root_id;

# Roll in any changes to the category object if we're just adding keywords.
if ($ARGS{'add_more|keyword|add_cb'}) {
    foreach my $meth ($cat->my_meths(1)) {
	$meth->{set_meth}->($cat, @{$meth->{set_args}}, $ARGS{$meth->{name}})
	  if defined $meth->{set_meth};
    }
}

# Get the name for the breadcrumb trail.
my $crumb = $cat->get_name;
$crumb = $crumb ? "&quot;$crumb&quot;" : 'New';
</%init>

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

=head1 NAME

/admin/profile/category/dhandler - Interface for managing categories.

=head1 VERSION

$Revision: 1.7 $

=head1 DATE

$Date: 2002/07/02 22:49:18 $

=head1 DESCRIPTION

This element handles the display for editing categories.

</%doc>
