%# BEGIN LICENSE BLOCK
%# 
%# Copyright (c) 1996-2002 Jesse Vincent <jesse@bestpractical.com>
%# 
%# (Except where explictly superceded by other copyright notices)
%# 
%# This work is made available to you under the terms of Version 2 of
%# the GNU General Public License. A copy of that license should have
%# been provided with this software, but in any event can be snarfed
%# from www.gnu.org
%# 
%# This work is distributed in the hope that it will be useful, but
%# WITHOUT ANY WARRANTY; without even the implied warranty of
%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%# General Public License for more details.
%# 
%# 
%# Unless otherwise specified, all modifications, corrections or
%# extensions to this work which alter its source code become the
%# property of Best Practical Solutions, LLC when submitted for
%# inclusion in the work.
%# 
%# 
%# END LICENSE BLOCK
<& /Elements/TitleBoxStart, title => loc("Approval diagram"), color => "#999900" &>
<TABLE WIDTH=100%>
<%PERL>
my $level = 0;
my ($appv_id, $next_appv_id);

{
    my @top = grep { $_->{Id} =~ m/^\d+$/ } @{$session{Workflow}}
	if exists $session{Workflow};

    if (@top) {
	$next_appv_id = $top[-1]{Id};
	$next_appv_id =~ s/(\d+)$/$1+1/e;
    } else {
	$next_appv_id = 1;
    }
}
</%PERL>
<TR>
<TD>[ <&|/l&>Begin Approval</&> ]</TD>
<TD ALIGN=RIGHT>[ <A HREF="<%$URL%>?workflow=<%$Workflow%>&WFAction=create&Queue=<%$Queue%>&WFId=<%$next_appv_id%>"><&|/l&>Add Next State</&></A> ]</TD>
<TD></TD>
</TR>
<%PERL>
if ($WorkflowObj->Id and defined(my $content = $WorkflowObj->Content)) {
    my @flow = ();
    my ($Queue, $User) = ( RT::Queue->new($session{CurrentUser}),
                           RT::User->new($session{CurrentUser})
                         );
    foreach (sort split(/^ENDOFCONTENT\n*/m, $content)) {
        next unless /^===Create-Ticket: T(.*)$/m;
        my $stage = { Id => $1 };
        $stage->{Owner} = $1		if /^Owner: (.*)$/m;
        $stage->{Queue} = $1		if /^Queue: (.*)$/m;
        $stage->{AdminCc} = $1		if /^AdminCc: (.*)$/m;
        $stage->{Description} = $1	if /^Subject: (.*)$/m;
        $stage->{Type} = ucfirst($1)	if /^Type: (.*)$/m;
        $stage->{Content} = $1		if /^Content: (.*)$/ms;
        $User->Load( $stage->{Owner} );
        $Queue->Load( $stage->{Queue} );
        $stage->{Owner} = $User->Id;
        $stage->{Queue} = $Queue->Id;
        push @flow, $stage;
    }
    
    $session{Workflow} = [ @flow ];
}

if (defined $session{Workflow}) {
    foreach my $item (@{$session{Workflow}}) {
	$appv_id = $item->{Id};
	my $cur_level;
	{ my @a = $appv_id =~ m/\./g; $cur_level = scalar @a };

	my $indent = "&nbsp;" x (($cur_level + 1) * 2);

	my @children =
	    grep { $_->{Id} =~ m/^${appv_id}\.\d+$/ } @{$session{Workflow}};
	if (@children) {
	    $next_appv_id = $children[-1]{Id};
	    $next_appv_id =~ s/(\d+)$/$1+1/e;
	} else {
	    $next_appv_id = "$appv_id.1";
	}

	$level = $cur_level;
	my $User = RT::User->new($session{'CurrentUser'});
	$User->Load($item->{Owner});
	my $user_name = ($User->Name) ? (" - " . $User->Name) : '';

	my $Group = RT::Principal->new($session{'CurrentUser'});
	$Group->Load($item->{AdminCc});
	my $group_name = (eval{$Group->Object}) ? (" - " . $Group->Object->Name) : '';

	my $TicketQueue = RT::Queue->new($session{'CurrentUser'});
	$TicketQueue->Load($item->{Queue});
	my $queue_name = $TicketQueue->Name;
	if ($queue_name eq '___Approvals' and $item->{Type} =~ /approval|code/i) {
	    $queue_name = '';
	}
	else {
	    $queue_name = " - $queue_name";
	}

</%PERL>
<TR>
<TD><%$indent%>[ <A HREF="<%$URL%>?Workflow=<%$Workflow%>&WFAction=modify&Queue=<%$Queue%>&WFId=<%$appv_id%>"><% $item->{Id} %></A> - <% loc($item->{Type}) %><% $user_name . $group_name . $queue_name %> - <% $item->{Description} %> ]</TD>
<TD ALIGN=RIGHT>[ <A HREF="<%$URL%>?Workflow=<%$Workflow%>&WFAction=create&Queue=<%$Queue%>&WFId=<%$next_appv_id%>"><&|/l&>Add Next State</&></A> ]</TD>
<TD ALIGN=RIGHT>[ <A HREF="<%$URL%>?Workflow=<%$Workflow%>&WFAction=delete&Queue=<%$Queue%>&WFId=<%$appv_id%>"><&|/l&>Delete</&></A> ]</TD>
</TR>
%    }
%}
<TR>
<TD>[ <&|/l&>Finish Approval</&> ]</TD>
<TD></TD>
<TD></TD>
</TR>
<& /Elements/TitleBoxEnd &>
<& /Elements/TitleBoxStart, title => loc("Approval Details") . ($ARGShref->{WFId} && " #$ARGShref->{WFId}"), color => "#999900" &>

<TABLE>
<TR>
<TD ALIGN=RIGHT><&|/l&>Condition</&>:</TD>
<TD COLSPAN=3>
<% $ARGShref->{WFCondition} || ''%>
</TD><TD ALIGN=RIGHT>
<a href="/Search/Listing.html">[
<&|/l&>Edit Conditions</&>
]</a></TD></TR>
<TR>
<TD ROWSPAN=<% @{$session{Workflow}} ? 4 : 3%> ALIGN=RIGHT><&|/l&>Type</&>:</TD>
<TD>
<& .WFType, Default => $ARGShref->{WFType}, Type => (
    'Approval' # loc
) &><br>
</TD>
<TD ALIGN=LEFT><&|/l&>Owner</&>:
<& /Elements/SelectOwner, Name => 'WFOwner', Default => $ARGShref->{WFOwner} || undef &>
% my $CFs = $QueueObj->CustomFields;
% if ($CFs->Count) {
(<&|/l&>CustomField</&>):
<SELECT>
<OPTION>-</OPTION>
% while ( my $cf = $CFs->Next ) {
%     next unless $cf->Type =~ /User/;
<OPTION><% $cf->Name %></OPTION>
% }
</SELECT>
% }
</TD>
<TD ALIGN=RIGHT><&|/l&>AdminCc</&>:</TD>
<TD>
% $m->print(loc("[_1] [_2] of group [_3]", $depth_str, $role_str, $group_str));
</TD>
</TR>
<TR>
<TD>
<& .WFType, Default => $ARGShref->{WFType}, Type => (
    'Ticket' # loc
) &><br>
</TD>
<TD COLSPAN=3 ALIGN=LEFT><&|/l&>Queue</&>:
<& /Elements/SelectQueue, Name => 'WFQueue', Default => $ARGShref->{WFQueue} || undef &></TD>
</TR>
% if (@{$session{Workflow}}) {
<TR><TD>
<& .WFType, Default => $ARGShref->{WFType}, Type => (
    'Alias' # loc
) &>
</TD>
<TD COLSPAN=3 ALIGN=LEFT><&|/l&>Alias for</&>:
<% $alias_str %>
</TD></TR>
% }
<TR><TD COLSPAN=4>
<& .WFType, Default => $ARGShref->{WFType}, Type => (
    'Code' # loc
) &><br>
</TD></TR>
<TR>
<TD ALIGN=LEFT><&|/l&>Name</&>:</TD>
<TD COLSPAN=5>
<input name="Name" VALUE="<%$Name%>" SIZE=80><BR>
</TD></TR>
<TR><TD COLSPAN=5>
</TD></TR>
<TR>
<TD ALIGN=LEFT><&|/l&>Description</&>:</TD>
<TD COLSPAN=5>
<input name="Description" VALUE="<%$Description%>" SIZE=80><BR>
</TD></TR>
<TR><TD COLSPAN=5>
</TD></TR>
<TR>
<TD ALIGN=RIGHT VALIGN=TOP>
<&|/l&>Content</&>:<BR>
</TD>
<TD COLSPAN=5>
<TEXTAREA NAME=Content ROWS=5 COLS=80 WRAP=SOFT>
<%$Content%></TEXTAREA>
</TD>
</TABLE>
<& /Elements/TitleBoxEnd &>

<%def .WFType>
<INPUT NAME="WFType" TYPE='RADIO' VALUE="<% $Type %>" <% (($Default || 'Approval') eq $Type) && 'CHECKED' %>><% loc($Type) %>
<%args>
$Type
$Default
</%args>
</%def>
<%INIT>
my $depth_str = '<SELECT NAME="WFDepth">';
foreach my $d (0 .. $MaxDepth) {
    $depth_str .= "<OPTION VALUE='$d' ";
    $depth_str .= "SELECTED" if $d eq $ARGShref->{WFDepth};
    $depth_str .= ">";

    # this is almost like a joke. ;)
    $depth_str .= loc('Grand')  for 2 .. $d;
    $depth_str .= loc('Parent') if $d;
    $depth_str .= "</OPTION>";
}
$depth_str .= '</SELECT>';

my $role_str = '<SELECT NAME="WFRole">';
foreach my $r (
    'Members',            # loc
    'Admins',             # loc
) {
    $role_str .= "<OPTION VALUE='$r' ";
    $role_str .= "SELECTED" if $r eq $ARGShref->{WFRole};
    $role_str .= ">" . loc($r). "</OPTION>";
}
$role_str .= '</SELECT>';

my $Groups = RT::Groups->new($session{CurrentUser});
$Groups->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'UserDefined');

my $group_str = '<SELECT NAME="WFAdminCc">';
$group_str .= '<OPTION VALUE="">-</OPTION>';
$group_str .= '<OPTION VALUE="-1">'.loc("(requestor's group)").'</OPTION>';
while ( my $Group = $Groups->Next()) {
    $group_str .= "<OPTION VALUE='" . $Group->Id . "' ";
    $group_str .= "SELECTED" if $Group->Id == $ARGShref->{WFAdminCc};
    $group_str .= ">" . $Group->Name() . '</OPTION>';
}
$group_str .= '</SELECT>';

my $alias_str = '<SELECT NAME="WFAlias">';
$alias_str .= '<OPTION VALUE="">-</OPTION>';
foreach my $id (map $_->{Id}, @{$session{Workflow}}) {
    $alias_str .= "<OPTION VALUE='$id' ";
    $alias_str .= "SELECTED" if $id == $ARGShref->{WFAdminCc};
    $alias_str .= ">$id</OPTION>";
}

my $QueueObj = RT::Queue->new($session{CurrentUser});
$QueueObj->Load($Queue);
</%INIT>
<%ARGS>
$URL
$Name => undef
$Description => undef
$Content => undef
$Queue => undef
$Workflow => undef
$WorkflowObj => undef
$MaxDepth => 2
$ARGShref => {}
</%ARGS>
