%#-- Begin HTML --#
<& '/widgets/wrappers/sharky/header.mc',
  title => "$disp Profile",
    context => "Admin | Profile | $disp | $crumb",
  jsInit => (defined $id) ? "showForm('text')" : ""
&>

<& '/javascripts/profile/formValidator.js', id => $id &>

<form method="post" action="<% $r->uri %>" name="cont_profile" onSubmit="return confirmChanges(this)">

<%perl>;

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

my ($num_fields, $meta, $all, $num);

# Output the primary fields.
$m->comp("/widgets/wrappers/sharky/table_top.mc",
  	 caption => "Properties",
	 number  => ++$num);

$m->comp('/widgets/profile/dumpRemainingFields.mc',
	 objref => $comp, readOnly => $no_edit,
	 fieldsUsed => {
			active => 1,
			type_name => 1
		       }
	);

# Output a list of object types to choose from.
$m->comp('/widgets/select_object/select_object.mc',
	 readOnly  => ($no_edit || $id),
	 object    => 'element_type',
         name      => 'element_type_id',
	 field     => 'name',
	 disp      => 'Type',
	 selected  => $comp->get_type__id,
	 reset_key => $id
);

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

# Manage Output Channels if this is a top-level container.
if (Bric::Biz::ATType->lookup({ id => $comp->get_type__id })->get_top_level) {

    my ($left, $right, $opt) = ([], [], []);

    # Assemble the list of output channels for this instance.
    foreach my $grp ( $id ? @{$comp->get_output_channels} : () ) {
	push @$right, { value =>  $grp->get_id, description => $grp->get_name };
    }

    # Assemble a list of all output channels.
    foreach my $oc ( Bric::Biz::OutputChannel->list({ active => 1 }) ) {
	push @$left, { value =>  $oc->get_id, description => $oc->get_name };
	push @$opt, [ $oc->get_id, $oc->get_name ];
    }

    # Load up the double-list manager.
    $m->comp("/widgets/wrappers/sharky/table_top.mc",
  	     caption => "Output Channels",
	     number  => ++$num
	    );

    $m->comp( "/widgets/doubleListManager/doubleListManager.mc",
	      readOnly     => $no_edit,
	      leftOpts     => $left,
	      rightOpts    => $right,
	      formName     => 'cont_profile',
	      leftName     => 'rem_oc',
	      rightName    => 'add_oc',
	      leftCaption  => $no_edit ? undef : "Available Output Channels",
              showLeftList => !$no_edit || 0,
	      rightCaption => 'Current Output Channels',

	    );

    # Now load a select widget so a primary output channel can be selected.
    $m->comp('/widgets/profile/select.mc',
	     disp => 'Primary Output Channel',
	     name => 'primary_oc_id',
	     options => $opt,
	     req => 1,
	     readOnly => $no_edit,
	     value => $comp->get_primary_oc_id
	    );

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

}


# display elements contained by this asset type
if (defined $id) {

    my $i;
    my $label = $no_edit ? 'View' : 'Edit';
    # Output the actions.
    my $containers = $comp->get_containers;

    $m->comp('/widgets/listManager/listManager.mc',
	     object         => 'element',
	     objs           => $containers,
	     behavior       => 'expand',
	     title          => "Existing Subelements",
	     addition       => undef,
	     fields         => [qw(name type_name description)],
	     profile        => undef,
	     select         => $no_del ? undef : ['Delete', 'element|delete_cb'],
	     number         => ++$num,
	    );

    $m->comp('/widgets/profile/imageSubmit.mc',
	     formName => "cont_profile",
	     callback => "formBuilder|addElement_cb",
	     image    => "add_sub_element_lgreen",
	     vspace   => 5
	    );

    $m->out(qq{<img src="/media/images/spacer.gif" width=578 height=8 />});
}


# dislay existing attributes for this asset type
if (defined $id) {

    $m->comp("/widgets/wrappers/sharky/table_top.mc",
	     caption => "Custom Fields",
	     number  => ++$num);

    # mangle the attrs and meta to match the existing display code.
    my $all_data = $comp->get_data;
    my $attrs = [];
    foreach my $dataObj (sort { $a->get_place <=> $b->get_place } @$all_data) {
	my $attr = $dataObj->get_name;
	my $metas = $dataObj->get_meta('html_info');
	my $tmp;
	foreach my $meta (keys %$metas) {
	    $tmp->{meta}{$meta} =  { value => $metas->{$meta} };
	}
	$tmp->{value} = $metas->{value};
	$tmp->{name} = $dataObj->get_name;
	push @$attrs, $tmp;
    }

    $num_fields = @$attrs;
    $m->comp("/widgets/profile/displayAttrs.mc", form_name => 'cont_profile',
	     attr => $attrs, readOnly => $no_edit);

    # Close the table.
    $m->comp("/widgets/wrappers/sharky/table_bottom.mc");
}

#########################

if (!$no_edit && defined $id) {
    # Output the formBuilder.
    $m->comp('/widgets/formBuilder/formBuilder.mc',
	     numFields => 0,
	     widget    => 1,
	     stay      => 1,
	     formName  => 'cont_profile',
	     numFields => $num_fields,
	     num       => ++$num,
	     caption   => 'Add New Field',
	     useRequired   => 1,
	     useQuantifier => 1
	    );
} else {
    # Add the buttons.
    $m->out( qq {<input type="hidden" name="isNew" value="1">\n});
    $m->comp('/widgets/profile/formButtons.mc',
	     type => $type,
	     val => 'next_dkgreen',
	     section => $section,
	     no_del => 1,
	     no_save => $no_edit,
	     widget  => "formBuilder"
	    );
}

</%perl>
</form>

<& '/widgets/wrappers/sharky/footer.mc', param => \%ARGS &>

%#-- End HTML --#


%#-- Once Section --#
<%once>;
my $class = 'Bric::Biz::AssetType';
my $type = 'element';
my $section = 'admin';
my $disp = get_disp_name('element'); # HACK!
</%once>

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

%#-- Init Section --#
<%init>;
# Do any redirects first.
do_queued_redirect();

$id = $ARGS{element_id} unless defined $id;
# Instantiate an object.
my $comp = $ARGS{obj} ? $ARGS{obj} : defined $id ? $class->lookup({ id => $id})
  : $class->new;

$id ||= $comp->get_id;

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

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

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

=head1 NAME

/admin/profile/contrib_type/dhandler - Container profile interface

=head1 VERSION

$Revision: 1.9 $

=head1 DATE

$Date: 2002/07/19 09:46:07 $

=head1 DESCRIPTION

Diplays the profile for containers.

</%doc>
