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

=head1 NAME

/workflow/active/dhandler - Show active assets.

=head1 VERSION

$LastChangedRevision$

=head1 DATE

$LastChangedDate$

=head1 SYNOPSIS

=head1 DESCRIPTION

=cut

</%doc>
<%init>;
my ($type, $work_id) = split('/', $m->dhandler_arg);
my ($fields, $obj_type, $previewer);
if ($type eq 'templates') {
    $fields = [qw(file_name version priority desk)];
    $obj_type = 'formatting';
} else {
    if (USE_THUMBNAILS && $type eq 'media') {
        $fields = [qw(thumb title version priority desk)];
    } else {
        $fields = [qw(title version priority desk)];
    }
    $obj_type = $type;
    $previewer = sub {
        $m->comp('/widgets/profile/preview_link.mc',
                 type  => $type,
                 title => '<span class="l10n">' . shift() . '</span>',
                 doc   => shift)
    };
}

# Grab the workflow and start desk permissions.
my $wf = Bric::Biz::Workflow->lookup({ id => $work_id });
my $desk_chk = { map { $_->[0] => [chk_authz(0, READ, 1, @{$_->[1]}),
                                   chk_authz(0, EDIT, 1, @{$_->[1]}) ] }
                 map { [$_->get_id => [$_->get_asset_grp, $_->get_grp_ids ]] }
                 $wf->allowed_desks
               };
$wf = $wf->get_name;
</%init>

<& '/widgets/wrappers/sharky/header.mc',
    title => "Active $pl_name->{$obj_type}",
    no_hist => 1,
    context => "Workflow | &quot;$wf&quot; | 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->($desk_chk),
     addition     => undef,
     select       => $select->($type, $work_id, $desk_chk),
     profile      => $profile->($type),
     alter        => {priority => sub { $pri[$_[0]-1] },
                      title    => $previewer },
     field_titles => { desk    => $lang->maketext('Desk'),
                       version => $lang->maketext('V.'),
                       thumb   => $lang->maketext('Thumb'),
                     },
     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/<% $lang_key %>/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/<% $lang_key %>/return_dgreen.gif" border="0" name="return" value="Return" vspace=2 /></a>

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

  </form>
</tr>
</table>
<& /widgets/wrappers/sharky/footer.mc &>
<%cleanup>;
# Clear out the cache of user names.
%users = ();
</%cleanup>
<%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, $desk_chk) = @_;
    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 (defined $u) {
            # Show the name of the person who has the asset checked out.
            return $users{$u} ||= Bric::Biz::Person::User->lookup({
              id => $u
            })->format_name;
        } elsif ($desk_chk->{$o->get_desk_id}[0] && chk_authz($o, EDIT, 1)) {
            # Allow access if they have permission and have READ access to the
            # desk the asset is on.
            return ['Checkout', $widget.'|checkout_cb'];
        }
        return;
    }
};

my $exclude = sub {
    my $desk_chk = shift;
    return sub {
        my $o = shift;
        return 1 unless $desk_chk->{$o->get_desk_id}[0];
        return ! chk_authz($o, 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 'thumb') {
        # If we get a thumbnail URI, we have an image. Otherwise, simply
        # return a space to fill the column properly because we aren't an
        # image object but USE_THUMBNAILS is on.
        my $thumb_uri = $o->thumbnail_uri or return '&nbsp;';
        return qq{ <img src="$thumb_uri" />};
    }
    return;
};
</%once>



