%# BEGIN BPS TAGGED BLOCK {{{
%# 
%# COPYRIGHT:
%# 
%# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
%#                                          <jesse@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 $request_args = $m->request_args();

my $reminder_collection = $Ticket->Reminders->Collection;

if ( $request_args->{'update-reminders'} ) {
    while ( my $reminder = $reminder_collection->Next ) {
        if (   $reminder->Status ne 'resolved' && $request_args->{ 'Complete-Reminder-' . $reminder->id } ) {
            $Ticket->Reminders->Resolve($reminder);
        }
        elsif ( $reminder->Status eq 'resolved' && !$request_args->{ 'Complete-Reminder-' . $reminder->id } ) {
            $Ticket->Reminders->Open($reminder);
        }

        if ( exists( $request_args->{ 'Reminder-Subject-' . $reminder->id } ) && ( $reminder->Subject ne $request_args->{ 'Reminder-Subject-' . $reminder->id } )) {
            $reminder->SetSubject( $request_args->{ 'Reminder-Subject-' . $reminder->id } ) ;
        }

        if ( exists( $request_args->{ 'Reminder-Owner-' . $reminder->id } ) && ( $reminder->Owner != $request_args->{ 'Reminder-Owner-' . $reminder->id } )) {
            $reminder->SetOwner( $request_args->{ 'Reminder-Owner-' . $reminder->id } , "Force" ) ;
        }

        if ( exists( $request_args->{ 'Reminder-Due-' . $reminder->id } ) && ( $reminder->DueObj->Date ne $request_args->{ 'Reminder-Due-' . $reminder->id } )) {
            $reminder->SetDue( $request_args->{ 'Reminder-Due-' . $reminder->id } ) ;
        }
    }
}

if ( $request_args->{'NewReminder-Subject'} ) {
    my $due_obj = RT::Date->new( $session{'CurrentUser'} );
    my $date    = Time::ParseDate::parsedate(
        $request_args->{'NewReminder-Due'},
        UK            => RT->Config->Get('DateDayBeforeMonth'),
        PREFER_PAST   => 0,
        PREFER_FUTURE => 1
    );
    $due_obj->Set( Value => $date, Format => 'unix' );
    my ( $add_id, $msg, $txnid ) = $Ticket->Reminders->Add(

        Subject => $request_args->{'NewReminder-Subject'},
        Owner   => $request_args->{'NewReminder-Owner'},
        Due     => $due_obj->ISO
    );
}

# We've made changes, let's reload our search

$reminder_collection = $Ticket->Reminders->Collection;
</%init>
<input type="hidden" class="hidden" name="id" value="<% $Ticket->id %>" />
<input type="hidden" class="hidden" name="update-reminders" value="1" />
<div>
% while (my $reminder = $reminder_collection->Next) {
%   if ($reminder->Status eq 'resolved' && !$ShowCompleted) {
<input type="hidden" class="hidden" name="Complete-Reminder-<% $reminder->id %>" value="1" />
%   } elsif ($Edit) {
<& SELF:EditEntry, Reminder => $reminder, Ticket => $Ticket &>
%   } else {
<& SELF:ShowEntry, Reminder => $reminder, Ticket => $Ticket &>
%   }
% }
% if ($reminder_collection->Count) {
<i><&|/l&>(Check box to delete)</&></i><br /><br />
% }
</div>
<div>
<&|/l&>New reminder:</&>
<& SELF:NewReminder, Ticket => $Ticket &>
<%method NewReminder>
<%args>
$Ticket
</%args>
<table>
<tr class="input-row">
<td class="label"><label class="horizontal" for="NewReminder-Subject" ><&|/l&>Subject</&>:</label></td>
<td class="value">
<input type="text" size="15" name="NewReminder-Subject" id="NewReminder-Subject" />
</td>
</tr>
<tr class="input-row">
<td class="label">
<label class="horizontal" for="NewReminder-Owner" ><&|/l&>Owner</&>:</label></td><td class="value">
<& /Elements/SelectOwner, Name => 'NewReminder-Owner', QueueObj => $Ticket->QueueObj, Default=>$session{'CurrentUser'}->id, DefaultValue => 0 &>
</td>
</tr>
<tr class="input-row">
<td class="label"><label class="horizontal" for="NewReminder-Due" ><&|/l&>Due</&>:</label></td>
<td class="value">
<& /Elements/SelectDate, Name => "NewReminder-Due", Default => "" &>
</td>
</tr>
</table>
</%method>
<%method EditEntry>
<%args>
$Reminder
$Ticket
</%args>
<input
    type="checkbox" 
    name="Complete-Reminder-<%$Reminder->id%>" 
    <% $Reminder->Status eq 'resolved' ? 'checked="checked"' : '' %> 
/> 
    <input type="text" size="15" name="Reminder-Subject-<% $Reminder->id %>" value="<%$Reminder->Subject%>" /> &bull; 
    <& /Elements/SelectOwner, Name => 'Reminder-Owner-'.$Reminder->id, Queue => $Ticket->QueueObj, Default => $Reminder->Owner, DefaultValue => 0  &>
    <& /Elements/SelectDate, Name => 'Reminder-Due-'.$Reminder->id, Default => $Reminder->DueObj->Date &>
    (<%$Reminder->DueObj->Unix>0  ? $Reminder->DueObj->AgeAsString : '' %>)<br />
</%method>
<%method ShowEntry>
<%args>
$Reminder
$Ticket
</%args>
<input
    type="checkbox" 
    name="Complete-Reminder-<%$Reminder->id%>" 
    <% $Reminder->Status eq 'resolved' ? 'checked="checked"' : '' %> 
/> 
    <%$Reminder->Subject%> &bull; 
    <%$Reminder->OwnerObj->Name%>
    <%$Reminder->DueObj->Unix>0  ? "&bull; ". $Reminder->DueObj->AgeAsString : '' |n%><br />
</%method>
