<%perl>;
# Instantiate an object.
my $oc = $ARGS{obj} ? $ARGS{obj} : defined $id ? $class->lookup({ id => $id})
  : $class->new;
$id ||= $oc->get_id;

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

# Get the name for the breadcrumb trail.
my $crumb = $oc->get_name;
$crumb = $crumb ? "&quot;$crumb&quot;" : 'New';

# Assume we're going to need a "next" button.
my $button = 'next_dkgreen';

# Output the header and other necessities.
$m->comp('/widgets/wrappers/sharky/header.mc',
	 title => "$disp Profile",
	 context => "Admin | Profile | $disp | $crumb"
);
$m->out(qq{<form method="post" name="oc_profile" action="} . $r->uri
        . qq{" onSubmit="return confirmChanges(this)">\n});

# Output a hidden field with the ID, if we have one.
$m->comp('/widgets/profile/hidden.mc', value => $id, name => 'output_channel_id')
  if defined $id;

$m->comp("/widgets/wrappers/sharky/table_top.mc",
  	 caption => "Properties",
	 number  => 1);

# Dump out the fields for display.
$m->comp('/widgets/profile/dumpRemainingFields.mc',
         readOnly   => $no_edit,
	 objref     => $oc,
         fieldsUsed => {active => 1});
my @sites = site_list(EDIT);
if ($id || $no_edit) {
    $m->out('<input type="hidden" name="site_id" value="'.$oc->get_site_id.'">');
    $m->comp('/widgets/profile/displayFormElement.mc',
             objref   => $oc,
             key      => 'site',
             readOnly => 1) if @sites > 1;
} elsif ((my $site_id = $oc->get_site_id) || @sites == 1) {
    $m->out('<input type=hidden name="site_id" value="'.($site_id || $sites[0]->get_id).'">');
} else {
    # Output a list of sites to choose from.
    $m->comp('/widgets/select_object/select_object.mc',
	     readOnly  => ($no_edit || $id),
             object    => 'site',
             name      => 'site_id',
             field     => 'name',
             disp      => 'Site',
             constrain => { active => 1 },
             exclude   => sub { ! chk_authz($_[0], EDIT, 1) },
             selected  => scalar $c->get_user_cx(get_user_id),
             reset_key => $id
            );
}

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

# Add pre/post fields to the specialCharacters array
$m->out(q{<script language="javascript">
specialOCFields["pre_path"]  = "URI Prefix";
specialOCFields["post_path"] = "URI Sufix";
</script>
});

if (defined $id) {
    $m->out('<script>
var selectOrderNames = new Array("include_pos")
</script>
    ');
    my $inc = $oc->get_includes;
    my $opts = [ map { [ $_ => $_ + 1 ] } (0..$#$inc) ];
    my $i = 0;
    my $ord_sub = sub {
        return unless $_[1] eq 'order';
	# Output a hidden field for this included OC.
	$m->scomp('/widgets/profile/hidden.mc',
                            name => 'include_id',
			    value => $_[0]->get_id
        )
	# And ouput the position number.
	. $m->scomp('/widgets/profile/select.mc',
	         name => 'include_pos',
                 disp => '',
		 value => $i++,
		 options => $opts,
		 useTable => 0,
		 js => qq{onChange="reorder(this, 'oc_profile')"}
        );
    };

    $button = undef;
    # Add the include section
    $m->comp('/widgets/listManager/listManager.mc',
	     object => 'output_channel',
	     userSort => 0,
	     def_sort_field => 'order',
	     title => 'Template Includes',
	     objs => $inc,
	     addition => undef,
	     constrain => { include_parent_id => $id },
	     fields => ['name', (@sites > 1 ? ('site') : ()), qw(description order)],
	     field_titles => { order => 'Order' },
	     field_values => $ord_sub,
	     profile => undef,
	     select =>  ['Delete', 'include_oc_id_del'],
	     number => 2
    );


</%perl>
<table border=1 cellpadding=2 cellspacing=0 width=580 bordercolor="#cccc99" style="border-style:solid; border-color:#cccc99;">
<tr><td class="medHeader" style="border-style:solid; border-color:#cccc99;"><& '/widgets/select_object/select_object.mc',
    object => 'output_channel',
    field  => 'name',
    getter => @sites > 1 ? $oc_name_getter : undef,
    exclude => [ $id, map { $_->get_id } $oc->get_includes ],
    no_persist => 1,
    name   => 'output_channel|include_oc_id_cb',
    default => ['' => 'Add to Include'],
    constrain => { active => 1 },
    js => "onChange='submit()'",
    useTable => 0,
&></td></tr>
</table>
<br />
<%perl>;

}

# Get the group's membership listed
$m->comp("/widgets/grp_membership/grp_membership.mc",
         grp_class => 'Bric::Util::Grp::OutputChannel',
         obj => $oc,
         formName => 'oc_profile',
         no_edit => $no_edit,
         widget  => $type,
         num => 3
);

# Add the form buttons and other necessities.
$m->comp("/widgets/profile/formButtons.mc",
	 type => $type,
	 section => $widget,
         widget => $type,
	 val => $button,
	 no_del => $no_del,
	 no_save => $no_edit);
$m->out("</form>\n");
$m->comp('/widgets/wrappers/sharky/footer.mc', param => \%ARGS );
</%perl>

%#-- Once Section --#
<%once>;
my $class = "Bric::Biz::OutputChannel";
my $widget = 'profile';
my $type = 'output_channel';
my $disp = get_disp_name($type);
my $ng = Bric::Biz::OutputChannel->my_meths->{name}{get_meth};
my $sg = Bric::Biz::OutputChannel->my_meths->{site}{get_meth};
my $oc_name_getter = sub {
    my $o = shift;
    return $ng->($o) . ' (' . $sg->($o) . ')';
};
</%once>

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

=head1 NAME

=head1 VERSION

$LastChangedRevision$

=head1 DATE

$LastChangedDate$

=head1 SYNOPSIS

/admin/profile/container_type/dhandler - display output channel object profile.

=head1 DESCRIPTION

This element displays output channel object profiles for editing.

=cut

</%doc>
