%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2023 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 }}}
<%init>
$m->abort unless RT->Config->Get('SimplifiedRecipients', $session{'CurrentUser'});

my $TicketObj = RT::Ticket->new($session{CurrentUser});
$TicketObj->Load($ARGS{id});
$m->abort unless $TicketObj->id && $ARGS{UpdateType};

if ( $ARGS{UpdateType} eq 'private' ) {
    $m->abort
      unless $TicketObj->CurrentUserHasRight( 'CommentOnTicket' ) || $TicketObj->CurrentUserHasRight( 'ModifyTicket' );
}
else {
    $m->abort
      unless $TicketObj->CurrentUserHasRight( 'ReplyToTicket' ) || $TicketObj->CurrentUserHasRight( 'ModifyTicket' );
}

my @dryrun = $TicketObj->DryRun(
    sub {
        local $ARGS{UpdateContent} ||= "Content";
        ProcessUpdateMessage(ARGSRef  => \%ARGS, TicketObj => $TicketObj );
        ProcessTicketWatchers(ARGSRef => \%ARGS, TicketObj => $TicketObj );
        ProcessTicketBasics(  ARGSRef => \%ARGS, TicketObj => $TicketObj );
        ProcessTicketLinks(   ARGSRef => \%ARGS, TicketObj => $TicketObj );
        ProcessTicketDates(   ARGSRef => \%ARGS, TicketObj => $TicketObj );
        ProcessObjectCustomFieldUpdates(ARGSRef => \%ARGS, Object => $TicketObj );
        ProcessTicketReminders( ARGSRef => \%ARGS, TicketObj => $TicketObj );
    }
);
$m->abort unless @dryrun;

my %headers = (To => {}, Cc => {}, Bcc => {});
my %no_squelch = (To => {}, Cc => {}, Bcc => {});
my @scrips = map {@{$_->Scrips->Prepared}} @dryrun;
if (@scrips) {
    for my $scrip (grep $_->ActionObj->Action->isa('RT::Action::SendEmail'), @scrips) {
        my $action = $scrip->ActionObj->Action;
        for my $type (qw(To Cc Bcc)) {
            for my $addr ($action->$type()) {
                if (grep {$addr->address eq $_} @{$action->{NoSquelch}{$type} || []}) {
                    $no_squelch{$type}{$addr->address} = $addr;
                } else {
                    $headers{$type}{$addr->address} = $addr;
                }
            }
        }
    }
}
my %recips;
my %squelched = ProcessTransactionSquelching( \%ARGS );
my $squelched_config = !( RT->Config->Get('SquelchedRecipients', $session{'CurrentUser'}) );
my %submitted;
$submitted{$_} = 1 for split /,/, $ARGS{TxnRecipients};
</%init>
%   if ( scalar(map { keys %{$headers{$_}} } qw(To Cc Bcc)) ) {

<div class="custom-control custom-checkbox">
  <input class="custom-control-input" type="checkbox" id="TxnSendMailToAll-Simplified" name="TxnSendMailToAll" value="1">
  <label class="custom-control-label" for="TxnSendMailToAll-Simplified"><b><% loc('All recipients') %></b></label>
</div>
% }
% for my $type (qw(To Cc Bcc)) {
%     next unless keys %{$headers{$type}} or keys %{$no_squelch{$type}};
<ul class="list-group list-group-compact">
  <li class="list-group-item">
    <div class="form-row">
      <div class="col-auto">
        <% $type %>:
      </div>
      <div class="col-auto">
%     for my $addr (sort {$a->address cmp $b->address} values %{$headers{$type}}) {
%         my $checked = $submitted{$addr->address} ? not $squelched{$addr->address} : $squelched_config;
%         $m->callback(CallbackName => 'BeforeAddress', Ticket => $TicketObj, Address => $addr, Type => $type, Checked => \$checked);
%         $recips{$addr->address}++;
        <div class="custom-control custom-checkbox">
          <input type="checkbox" name="TxnSendMailTo" <% $checked ? 'checked="checked"' : '' |n%> value="<%$addr->address%>" id="TxnSendMailTo-<% $addr->address %>-<% $recips{$addr->address} %>" class="custom-control-input" />
          <label class="custom-control-label" for="TxnSendMailTo-<% $addr->address %>-<% $recips{$addr->address} %>"><& /Elements/ShowUser, Address => $addr &></label>
        </div>
%         $m->callback(CallbackName => 'AfterAddress', Ticket => $TicketObj, Address => $addr, Type => $type);
%     }
%     for my $addr (sort {$a->address cmp $b->address} values %{$no_squelch{$type}}) {
<div class="col-auto">
<label><& /Elements/ShowUser, Address => $addr &></label>
%         if ( $type eq 'Cc' ) {
(<&|/l&>explicit one-time Cc</&>)
%         } else {
(<&|/l&>explicit one-time Bcc</&>)
%         }
</div>
%     }
      </div>
    </li>
  </ul>
% }
% $m->callback( CallbackName => 'AfterRecipients', TicketObj => $TicketObj );
<p>
<&|/l, RT->Config->Get('WebPath')."/Ticket/ModifyPeople.html?id=".$TicketObj->Id,
&>Uncheck boxes to disable notifications to the listed recipients <b>for this transaction only</b>; persistent squelching is managed on the <a href="[_1]">People page</a>.</&>
</p>

% if (!$TicketObj->CurrentUserHasRight('ShowOutgoingEmail') || $ARGS{'RenderTxnRecipientsInput'}) {
    <input type="hidden" name="TxnRecipients" value="<% join ",",sort keys %recips %>" />
% }
% $m->abort();
