<%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
        . ((ALLOW_URIS_WITHOUT_CATEGORIES) ? qq{">\n} : qq{" onsubmit="return confirmURIFormats(this) && 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, burner => 1 }
);
if (defined $id || $no_edit) {
    $m->comp(
        '/widgets/profile/displayFormElement.mc',
        objref   => $oc,
        key      => 'burner_name',
        readOnly => 1,
    );
} else {
    $m->comp(
        '/widgets/profile/displayFormElement.mc',
        objref   => $oc,
        key      => 'burner',
        readOnly => 0,
    );
}
my @sites = site_list(EDIT);
if ($id || $no_edit) {
    $m->comp('/widgets/profile/hidden.mc', value => $oc->get_site_id, name => '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->comp('/widgets/profile/hidden.mc', value => ($site_id || $sites[0]->get_id), name => 'site_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
if (defined $id) {
    $button = undef;
    $m->out('<script type="text/javascript">
var selectOrderNames = new Array("include_pos")
</script>
    ');
    my $inc = $oc->get_includes;
    my $opts = [ map { [ $_ => $_ + 1 ] } (0..$#$inc) ];
    my %exclude = ( map { $_->get_id => 1 } @$inc, $oc );
    my @avail_ocs = grep { !$exclude{$_->get_id} } Bric::Biz::OutputChannel->list({
        active => 1,
        burner => $oc->get_burner,
    });

    if (@$inc || @avail_ocs) {
        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')"}
            );
        };

        # Add the include section
        $m->comp(
            '/widgets/wrappers/sharky/table_top.mc',
            caption => 'Template Includes',
            number => 2,
        );
        $m->comp('/widgets/listManager/listManager.mc',
                 object         => 'output_channel',
                 userSort       => 0,
                 def_sort_field => 'order',
                 objs           => $inc,
                 addition       => undef,
                 constrain      => { include_parent_id => $id },
                 field_titles   => { order => 'Order' },
                 field_values   => $ord_sub,
                 profile        => undef,
                 select         =>  ['Delete', 'include_oc_id_del'],
                 fields         => [
                     'name', (@sites > 1 ? ('site') : ()), qw(description order)
                 ],
             );
        if (@avail_ocs) {
            $m->print(qq{<div class="actions">\n});
            $m->comp(
                '/widgets/select_object/select_object.mc',
                object     => 'output_channel',
                field      => 'name',
                objs       => \@avail_ocs,
                no_persist => 1,
                name       => 'output_channel|include_oc_id_cb',
                default    => ['' => 'Add to Include'],
                js         => "onChange='submit()'",
                useTable   => 0,
            );
            $m->print("</div>\n");
        }
        $m->comp("/widgets/wrappers/sharky/table_bottom.mc");
    }
}

# 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>
