%#-- Begin HTML --#
<& '/widgets/wrappers/sharky/header.mc',
  title => "$disp Profile",
    context => "Admin | Profile | $disp | $crumb",
  jsInit => (defined $id) ? "showForm('text')" : ""
&>
% if (ENABLE_HTMLAREA) {
% # HTMLAREA code here, only used if textarea is in the page with wysiwyg on.
<& /widgets/htmlarea/load.mc &>
% }
<form method="post" action="<% $r->uri %>" name="cont_profile" id="cont_profile" onSubmit="return confirmChanges('cont_profile')">
<%perl>;
if (defined $id) {
    $m->comp('/widgets/profile/hidden.mc',
             value => $id,
             name  => 'element_id');
} else {
    if (my $site_cx_id = $c->get_user_cx(get_user_id)) {
        $m->comp('/widgets/profile/hidden.mc',
                 value => $site_cx_id,
                 name  => 'formBuilder|add_site_id_cb');
    }
}

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/displayFormElement.mc',
         objref => $comp,
         readOnly => $no_edit || defined $id,
         key    => 'key_name');

$m->comp('/widgets/profile/dumpRemainingFields.mc',
	 objref => $comp, readOnly => $no_edit,
	 fieldsUsed => {
                        key_name => 1,
			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      => $lang->maketext('Type'),
	 selected  => $comp->get_type__id,
	 reset_key => $id,
	 localize  => 0
);

$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::Element',
         obj => $comp,
         formName => 'cont_profile',
         no_edit  => $no_edit,
         widget   => $type,
         no_cb    => 1,
         num      => ++$num
);


if (defined $id) {

    if (Bric::Biz::ATType->lookup({ id => $comp->get_type__id })->get_top_level) {
        my $site_sub = sub {
            my $siteid = $_[0]->get_id;
            if ($_[1] eq 'primary') {
                $m->scomp('/widgets/select_object/select_object.mc',
                          object => 'output_channel',
                          field  => 'name',
                          constrain => { site_id => $siteid, active => 1 },
                          no_persist => 1,
                          name   => "primary_oc_site_$siteid",
                          useTable => 0,
                          selected => $comp->get_primary_oc_id($siteid) || '',
                         );
            }

        };

        $m->comp('/widgets/listManager/listManager.mc',
                 object => 'site',
                 userSort => 0,
                 def_sort_field => 'name',
                 title => 'Sites',
                 objs => scalar $comp->get_sites,
                 addition => undef,
                 fields => [qw(name description primary)],
                 field_titles => { primary => 'Primary Output Channel'},
                 field_values => $site_sub,
                 profile => undef,
                 select => $sel_sub,
                 number => ++$num
                );
my %sites = map { $_->get_id => 1 } $comp->get_sites;
</%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 => 'site',
    field  => 'name',
    exclude => sub { $sites{$_[0]->get_id} || ! chk_authz($_[0], EDIT, 1) },
    no_persist => 1,
    name   => 'formBuilder|add_site_id_cb',
    default => ['' => 'Add Site'],
    constrain => { active => 1, output_channel_id => 'not null' },
    js => "onChange='submit()'",
    useTable => 0,
&></td></tr>
</table>
<br />
<%perl>;
        $m->out(qq{<img src="/media/images/spacer.gif" width="578" height="8" />});

        # Manage Output Channels if this is a top-level container.

        my $oc_sub = sub {
            my $ocid = $_[0]->get_id;
            if ($_[1] eq 'enabled') {
                return 'Yes'
                  if $comp->get_primary_oc_id($_[0]->get_site_id) == $ocid;
                $m->scomp('/widgets/profile/checkbox.mc',
                          name => 'enabled',
                          value => $ocid,
                          checked => $_[0]->is_enabled,
                          useTable => 0
                         );
        }
        };
        my %poc_ids = map { $comp->get_primary_oc_id($_->get_id) => 1 }
          $comp->get_sites;
        my $oc_sel_sub = sub {
            return if $poc_ids{$_[0]->get_id};
            return ['Delete', 'rem_oc'];
        };

        $m->comp('/widgets/listManager/listManager.mc',
                 object => 'output_channel',
                 userSort => 0,
                 def_sort_field => 'name',
                 title => 'Output Channels',
                 objs => scalar $comp->get_output_channels,
                 addition => undef,
                 fields => [qw(name description site enabled)],
                 field_titles => { enabled => 'Enabled' },
                 field_values => $oc_sub,
                 profile => undef,
                 select => $oc_sel_sub,
                 number => ++$num
                );
        my @available_ocs;
        foreach my $site ($comp->get_sites) {
            push @available_ocs, Bric::Biz::OutputChannel->list({
                site_id => $site->get_id,
                active  => 1
            });
        }
</%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',
    objs   => \@available_ocs,
    field  => 'name',
    exclude => [ map { $_->get_id } $comp->get_output_channels ],
    no_persist => 1,
    getter => $oc_name_getter,
    name   => 'formBuilder|add_oc_id_cb',
    default => ['' => 'Add Output Channel'],
    constrain => { active => 1 },
    js => "onChange='submit()'",
    useTable => 0,
&></td></tr>
</table>
<br />
<%perl>;
        $m->out(qq{<img src="/media/images/spacer.gif" width="578" height="8" />});
    }

    # display elements contained by this asset type
    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_sub'],
	     number         => ++$num,
	    );

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

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

        # display 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;
            if (@$all_data) {
                my $attrs = [];
                foreach my $dataObj (@$all_data) {
                    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_key_name;
                    $tmp->{id} = $dataObj->get_id;
                    push @$attrs, $tmp;
                }

                $num_fields = @$attrs;
                $m->comp("/widgets/profile/displayAttrs.mc",
                         form_name => 'cont_profile',
                         attr     => $attrs,
                         readOnly => $no_edit,
                         useEdit     => !$no_edit,
                         usePosition => !$no_edit,
                         useDelete   => !$no_edit);
            } else {
                $m->print(qq{<img src="/media/images/spacer.gif" height="18" width="5" />},
                $lang->maketext("No fields defined"));
            }

    # 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>
% if (ENABLE_HTMLAREA) {
% # HTMLAREA code here, only used if textarea is in the page with wysiwyg on.
<& /widgets/htmlarea/timeout.mc &>
% }
<& '/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!
my $sel_sub = sub {
    return unless chk_authz($_[0], EDIT, 1);
    return ['Delete', 'rem_site'];
};
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>

%#-- 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/element/dhandler - Container profile interface

=head1 VERSION

$LastChangedRevision$

=head1 DATE

$LastChangedDate$

=head1 DESCRIPTION

Displays the profile for containers.

</%doc>
