%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
%#                                          <sales@bestpractical.com>
%#
%# (Except where explicitly superseded by other copyright notices)
%#
%#
%# LICENSE:
%#
%# 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.
%#
%# You should have received a copy of the GNU General Public License
%# along with this program; if not, write to the Free Software
%# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
%# 02110-1301 or visit their web page on the internet at
%# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
%#
%#
%# CONTRIBUTION SUBMISSION POLICY:
%#
%# (The following paragraph is not intended to limit the rights granted
%# to you to modify and distribute this software under the terms of
%# the GNU General Public License and is only of importance to you if
%# you choose to contribute your changes and enhancements to the
%# community by submitting them to Best Practical Solutions, LLC.)
%#
%# By intentionally submitting any modifications, corrections or
%# derivatives to this work, or any other work intended for use with
%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
%# you are the copyright holder for those contributions and you grant
%# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
%# royalty-free, perpetual, license to use, copy, create derivative
%# works based on those contributions, and sublicense and distribute
%# those contributions and any derivatives thereof.
%#
%# END BPS TAGGED BLOCK }}}
<%args>
$Ticket => undef
$id => undef
$ShowCompleted => 0
$Edit => 0
</%args>
<%init>

$Ticket = LoadTicket($id) if ($id);
my $resolve_status = $Ticket->QueueObj->Lifecycle->ReminderStatusOnResolve;

my $count_reminders = RT::Reminders->new($session{'CurrentUser'});
$count_reminders->Ticket($Ticket->id);
my $count_tickets = $count_reminders->Collection;
if (!$ShowCompleted) {
    # XXX: don't break encapsulation if we can avoid it
    $count_tickets->FromSQL(q{Type = "reminder" AND RefersTo = "} .  $Ticket->id . qq{" AND Status != "$resolve_status" });
}
my $has_reminders = $count_tickets->Count;

# We've made changes, let's reload our search
my $reminder_collection = $count_reminders->Collection;
my $visible = 0;
</%init>
<input type="hidden" class="hidden" name="id" value="<% $Ticket->id %>" />
<input type="hidden" class="hidden" name="update-reminders" value="1" />
% if ($has_reminders) {
<table border="0" cellpadding="1" cellspacing="0" class="collection-as-table"<% $Edit ? ' style="width: auto;"' : '' |n %>>
<tr>
% if ( $Edit ) {
<th class="collection-as-table" colspan="5"><&|/l&>Reminders</&></th>
% } else {
<th class="collection-as-table"></th>
<th class="collection-as-table"><&|/l&>Reminder</&></th>
<th class="collection-as-table"><&|/l&>Due</&></th>
<th class="collection-as-table"><&|/l&>Owner</&></th>
% }
</tr>
% my $i = 0;
% while ( my $reminder = $reminder_collection->Next ) {
% $i++;
% if ( $reminder->Status eq $resolve_status && !$ShowCompleted ) {
<tr class="hidden"><td><input type="hidden" class="hidden" name="Complete-Reminder-<% $reminder->id %>" value="1" /></td></tr>
% $i++;
% } elsif ($Edit) {
<& SELF:EditEntry, Reminder => $reminder, Ticket => $Ticket, Index => $i &>
% $visible++;
% } else {
<& SELF:ShowEntry, Reminder => $reminder, Ticket => $Ticket, Index => $i &>
% $visible++;
% }
% }
</table>
% if ( $visible > 0 ) {
<i><&|/l&>(Check box to complete)</&></i><br /><br />
% }
% } else {

%# we must always include resolved reminders due to the browser
%# checkbox-with-false-value issue
% while ( my $reminder = $reminder_collection->Next ) {
% if ( $reminder->Status eq $resolve_status && !$ShowCompleted ) {
<input type="hidden" class="hidden" name="Complete-Reminder-<% $reminder->id %>" value="1" />
% }
% }
% }

% if (lc $Ticket->Status ne "deleted") {
<&|/l&>New reminder:</&>
<& SELF:NewReminder, Ticket => $Ticket &>
% }
% return(lc $Ticket->Status ne "deleted" or $visible);
<%method NewReminder>
<%args>
$Ticket
</%args>
<table>
<tr>
<td class="label"><&|/l&>Subject</&>:</td>
<td class="entry" colspan="3"><input type="text" size="50" name="NewReminder-Subject" id="NewReminder-Subject" /></td>
</tr>
<tr>
<td class="label"><&|/l&>Owner</&>:</td>
<td class="entry"><& /Elements/SelectOwner, Name => 'NewReminder-Owner', QueueObj => $Ticket->QueueObj, Default=>$session{'CurrentUser'}->id, DefaultValue => 0 &></td>
</tr>
<tr>
<td class="label"><&|/l&>Due</&>:</td>
<td class="entry"><& /Elements/SelectDate, Name => "NewReminder-Due", Default => "" &></td>
</tr>
</table>
</%method>
<%method EditEntry>
<%args>
$Reminder
$Ticket
$Index
</%args>
<tr class="<% $Index%2 ? 'oddline' : 'evenline' %>">
<td class="entry"><input type="checkbox" value="1" name="Complete-Reminder-<% $Reminder->id %>" <% $Reminder->Status eq $Reminder->QueueObj->Lifecycle->ReminderStatusOnResolve ? 'checked="checked"' : '' |n %> /></td>
<td class="label"><&|/l&>Subject</&>:</td>
<td class="entry" colspan="3"><input type="text" size="50" name="Reminder-Subject-<% $Reminder->id %>" value="<% $Reminder->Subject %>" /></td>
</tr>
<tr class="<% $Index%2 ? 'oddline' : 'evenline' %>">
<td class="entry">&nbsp;</td>
<td class="label"><&|/l&>Owner</&>:</td>
<td class="entry"><& /Elements/SelectOwner, Name => 'Reminder-Owner-'.$Reminder->id, QueueObj => $Ticket->QueueObj, Default => $Reminder->Owner, DefaultValue => 0  &></td>
<td class="label"><&|/l&>Due</&>:</td>
<td class="entry"><& /Elements/SelectDate, Name => 'Reminder-Due-'.$Reminder->id &> (<% $Reminder->DueObj->AsString %>)</td>
</tr>
</%method>
<%method ShowEntry>
<%args>
$Reminder
$Ticket
$Index
</%args>
% my $dueobj = $Reminder->DueObj;
% my $overdue = $dueobj->Unix > 0 && $dueobj->Diff < 0 ? 1 : 0;
<tr class="<% $Index%2 ? 'oddline' : 'evenline' %>">
<td class="collection-as-table"><input type="checkbox" value="1" name="Complete-Reminder-<% $Reminder->id %>" <% $Reminder->Status eq $Reminder->QueueObj->Lifecycle->ReminderStatusOnResolve ? 'checked="checked"' : '' |n %> /></td>
<td class="collection-as-table"><% $Reminder->Subject %></td>
<td class="collection-as-table"><% $overdue ? '<span class="overdue">' : '' |n %><% $dueobj->AgeAsString || loc('Not set') %><% $overdue ? '</span>' : '' |n %></td>
<td class="collection-as-table"><& /Elements/ShowUser, User => $Reminder->OwnerObj &></td>
</tr>
</%method>
