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

=head1 NAME

/workflow/active/dhandler - Show active assets.

=head1 VERSION

$Revision: 1.6 $

=head1 DATE

$Date: 2002/01/31 00:15:11 $

=head1 SYNOPSIS

=head1 DESCRIPTION

=cut

</%doc>

<%args>
</%args>

<%init>
my ($type, $work_id) = split('/', $m->dhandler_arg);
my $fields   = [qw(id version priority name uri desk)];
my $obj_type = $type eq 'templates' ? 'formatting' : $type;
# Grab the workflow from the cache.
my $wf = $c->get('__WORKFLOWS__');
for (@$wf) { $wf = "&quot;$_->{name}&quot;" and last if $_->{id} == $work_id }
</%init>

<& '/widgets/wrappers/sharky/header.mc',
    title => "Active $pl_name->{$obj_type}",
    no_hist => 1,
    context => "Workflow | $wf | Active $pl_name->{$obj_type}"
&>

<form method="post" action="<% $r->uri %>" name="theForm">

  <& '/widgets/listManager/listManager.mc',
     object       => $obj_type,
     title        => 'Active %n',
     fields       => $fields,
     sortBy       => 'priority',
     exclude      => $exclude,
     addition     => undef,
     select       => $select->($type, $work_id),
     profile      => $profile->($type),
     alter        => {'priority' => sub { $pri[$_[0]-1] }},
     field_titles => {'desk' => 'Desk'},
     field_values => $values,
     constrain    => {'workflow__id' => $work_id},
  &>
<br />
<table width=200 cellpadding=0 cellspacing=0>
<tr>
  <td width=80>

<table border=0 cellpadding=0 cellspacing=0>
<tr>
  <td colspan=2>

  </td>
</tr>
<tr>
   <td align=left>
<input type="image" src="/media/images/checkout_red.gif" border=0 name="checkout" value="checkout_red" />
   </td>
   <td align="right">
<a href="#" onClick="window.location.href='/workflow/profile/workspace/'; return false;"><img src="/media/images/return_dgreen.gif" border=0 name="return" value="Return" vspace=2 /></a>

  </td>
</tr>
</table>

  </form>
</tr>
</table>



<& /widgets/wrappers/sharky/footer.mc &>
%# Clear out the cache of user names.
% %users = ();

<%once>
my $pl_name = { map { $_ => get_class_info($_)->get_plural_name }
		qw(story media formatting) };

my @pri = ('High', 'Medium High', 'Normal', 'Medium Low', 'Low');
my %users;

my $profile = sub {
    my ($type) = @_;

    # Return an anonymous sub with closure on $type
    return sub {
	my $o = shift;
	my $u = $o->get_user__id;
	my $id = $o->get_id;
	if (defined $u && $u == get_user_id && chk_authz($o, EDIT, 1)) {
	    return ['Edit', "/workflow/profile/$type/$id?checkout=1&return=active", ''];
	} else {
	    return ['View', "/workflow/profile/$type/$id?return=active", ''];
	}
    }
};

my $select = sub {
    my ($type, $work_id) = @_;
    my $widget = $type eq 'templates' ? 'tmpl_prof' : $type.'_prof';

    # Return an anonymous sub with closure on $type
    return sub {
        my $o = shift;
        my $u = $o->get_user__id;
        if ((not defined $u) && chk_authz($o, EDIT, 1)) {
	    if ($o->get_workflow_id) {
	        return ['Checkout', $widget.'|checkout_cb'];
	    } else {
	        return ['Checkout', $widget.'|recall_cb', $o->get_id.'|'.$work_id];
	    }
        }
        return;
    }
};

my $exclude = sub { ! chk_authz($_[0], READ, 1) };

my $values = sub {
    my ($o, $f) = @_;
    if ($f eq 'desk') {
        my $d = $o->get_current_desk;
        my $w_id = $o->get_workflow_id;
        my $d_id = $d->get_id;
        my $name = $d->get_name;

        return qq{<a href="/workflow/profile/desk/$w_id/$d_id/" class=redLink>$name</a>};
    } elsif ($f eq 'status') {
        my $u_id = $o->get_user__id;
	my $pkg = ref $o;
	my $widget;

	if ($pkg eq 'Bric::Biz::Asset::Business::Story') {
	    $widget = 'story_prof';
	} elsif ($pkg eq 'Bric::Biz::Asset::Business::Media') {
	    $widget = 'media_prof';
	} else {
	    $widget = 'tmpl_prof';
	}

        if (defined $u_id) {
            return $users{$u_id} ||= Bric::Biz::Person::User->lookup({'id' => $u_id})->format_name;
        } else {
            my $uri  = $r->uri;
            my $a_id = $o->get_id;
            return qq{<a href="$uri?$widget|checkout_cb=$a_id">Checkout</a>};
        }
    }
    return;
};
</%once>



