% #-- Begin HTML --#
<& '/widgets/wrappers/sharky/header.mc',
   title => "$disp Profile",
   context => "Admin | Profile | $disp | $crumb"
 &>
<form method="post" action="<% $r->uri %>" name="wf_profile" onsubmit="return confirmChanges(this)">
<%perl>;
$m->comp('/widgets/profile/hidden.mc', value => $id, name => 'workflow_id')
  if defined $id;

# Output the key properties.
$m->comp("/widgets/wrappers/sharky/table_top.mc", 
          caption => "Properties",
          number  => 1,
);
$m->comp('/widgets/profile/dumpRemainingFields.mc', 
          objref => $wf,
          readOnly => $no_edit, 
          fieldsUsed => { active => 1, type => 1, site => 1 },
);
# Output the Workflow type.
if (defined $id) {
    $m->comp('/widgets/profile/displayFormElement.mc', readOnly => 1,
              vals  => { disp => 'Type',
                         value => WORKFLOW_TYPE_MAP->{$wf->get_type},
                         props => { type => 'text' }
                       }
    );
} else {
    # It's a new workflow. Output a list of parent categories to choose from.
    $m->comp('/widgets/profile/displayFormElement.mc', 
              objref => $wf,
              key => 'type',
    );
    $blabel = 'next_dkgreen';
}

my @sites = site_list(EDIT);
if ($id || $no_edit) {
    $m->comp('/widgets/profile/hidden.mc',
              name  => 'site_id',
              value => $wf->get_site_id,
    );
    $m->comp('/widgets/profile/displayFormElement.mc',
             objref   => $wf,
             key      => 'site',
             readOnly => 1) if @sites > 1;
} elsif ((my $site_id = $wf->get_site_id) || @sites == 1) {
    $m->comp('/widgets/profile/hidden.mc',
              name    => 'site_id',
              value   => ($site_id || $sites[0]->get_id),
    );
} else {
    # Output a list of object types 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 },
             objs      => \@sites,
             selected  => $ARGS{site_id} || scalar $c->get_user_cx(get_user_id),
             reset_key => $id
            );
}


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


# Output the desks.
if (defined $id) {
    # Grab a list of the desks.
    my $desks = $wf->allowed_desks;
    $start_id = $wf->get_head_desk_id;
    
    $m->comp('/widgets/wrappers/sharky/table_top.mc', 
             caption => '%n',
             object  => 'desk',
             number  => 2 );
    $m->comp('/widgets/listManager/listManager.mc',
             object         => 'desk',
             userSort       => 0,
             addition       => $no_edit ? undef : ['Add', "/admin/profile/desk"],
             objs           => $desks,
             fields         => [qw(name description publish start)],
             field_titles   => { start => 'Start Desk' },
             field_values   => $start_sub,
             alter          => { publish => $yes_no },
             profile        => $prof_sub,
             select         =>  $no_del || @$desks < 2 ? undef : ['Remove', 'remove_desk'],
    );
    $m->comp('/widgets/wrappers/sharky/table_bottom.mc');
} elsif ($new_desk) {
    # Allow 'em to create a brand new desk.
    $m->comp("/widgets/wrappers/sharky/table_top.mc",
             caption => "Start Desk",
             number => 2);
    $m->comp('/widgets/profile/text.mc',
             name => 'new_desk_name',
             disp => 'Start Desk',
             req  => 1
    );
    $m->comp("/widgets/wrappers/sharky/table_bottom.mc");
} else {
    # Output a select list to choose a start desk from.
    $m->comp("/widgets/wrappers/sharky/table_top.mc",
             caption => "Start Desk",
             number => 2);
    $m->comp('/widgets/select_object/select_object.mc',
             object     => 'desk',
             no_persist => 1,
             name       => 'first_desk',
             field      => 'name',
             disp       => 'Start Desk',
             useTable   => 1);
    $m->out(qq {<table width=578 cellpadding=2 cellspacing=0 border=0><tr><td width=140>&nbsp;</td><td width=438>});

    $m->comp('/widgets/profile/imageSubmit.mc',
             formName => "wf_profile",
             callback => "new_desk",
             alt      => 'New Desk',
             image    => "new_desk_red",
             js       => qq{onClick="requiredFields='';return customSubmit('wf_profile', 'new_desk', 1)"}
    );

    $m->out( qq{</td></tr></table>\n});

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

$m->out("<br />\n");

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


$m->out("<br />\n");
$m->comp('/widgets/profile/formButtons.mc',
         type    => $type,
         section => $section,
         no_del  => $no_del,
         no_save => $no_edit,
         val     => $blabel,
         formName => 'wf_profile',
         widget  => $type,
);
($no_edit, $no_del) = ();
</%perl>
</form>
<& '/widgets/wrappers/sharky/footer.mc', param => \%ARGS &>

%#-- End HTML --#


%#-- Once Section --#
<%once>;
my $class = 'Bric::Biz::Workflow';
my $section = 'admin';
my $type = 'workflow';
my $disp = get_disp_name($type);
my $rad = [{ key => 'start_desk', disp => 'Start Desk', props => { type => 'single_rad'} },
           { key => 'publish', disp => 'Publish Desk', props => { type => 'checkbox' } } ];
my ($no_edit, $no_del);

my $prof_sub = sub {
    # Get the object ID.
    my $id = $_[0]->get_id;
    # Assume user can view the profile.
    my $edit = [($no_edit ? 'View' : 'Edit'), "/admin/profile/desk/$id", ''];
    return [ $edit, [ 'Log', "/admin/events/desk/$id", ''] ];
};

my $yes_no = sub { $_[0] ? 'Yes' : 'No'};

my $start_id;

my $start_sub = sub {
    return unless $_[1] eq 'start';
    my $did = $_[0]->get_id;
    $m->scomp('/widgets/profile/radio.mc',
              name => 'start',
              value => $did,
              checked => $did == $start_id
    );
};

</%once>

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

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

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

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

# If they're creating a new start desk, set the basic properties.
if ($new_desk) {
    $wf->set_name($ARGS{name});
    $wf->set_description($ARGS{description});
    $wf->set_type($ARGS{type});
}
</%init>

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

=head1 NAME

/admin/profile/workflow/dhandler - Interface for managing workflows.

=head1 VERSION

$LastChangedRevision$

=head1 DATE

$LastChangedDate$

=head1 DESCRIPTION

This element handles the display for editing workflows.

</%doc>
