%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2026 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>
# inline add message uses this regardless of setting for SimplifiedRecipients
$m->abort
    unless RT->Config->Get('SimplifiedRecipients', $session{'CurrentUser'})
        || $ARGS{InlineAddMessage};

my $TicketObj = RT::Ticket->new($session{CurrentUser});
$TicketObj->Load($ARGS{id});
$ARGS{UpdateType} ||= $ARGS{DefaultUpdateType};
$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";
        ProcessTicketOwnerUpdate(ARGSRef => \%ARGS, TicketObj => $TicketObj );
        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="form-check">
  <input class="form-check-input" type="checkbox" id="TxnSendMailToAll-Simplified" name="TxnSendMailToAll" value="1">
  <label class="form-check-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}};
%   # inline add message has different styling
%   if ( $ARGS{InlineAddMessage} ) {
<div class="row mt-2">
  <div class="col-1 d-flex align-items-center justify-content-end">
    <span><% $type %>:</span>
  </div>
  <div class="col-11">
    <div class="row">
%     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}++;
%         my $id = 'TxnSendMailTo-' . $addr->address . '-' . $recips{ $addr->address };
      <div class="col-auto px-1">
        <div class="inline-add-message-recipients editable input-group mb-1 rounded d-flex align-items-center">
          <span class="input-group-text m-0 p-1">
            <label class="form-check-label" for="<% $id %>">
              &lt;<% $addr->address %>&gt;
            </label>
          </span>
          <div class="input-group-text m-0 p-1">
            <input type="checkbox" name="TxnSendMailTo" <% $checked ? 'checked="checked"' : '' |n%> value="<%$addr->address%>" id="<% $id %>" class="form-check-input" />
          </div>
        </div>
      </div>
%     }
    </div>
  </div>
</div>
%   } else {
<ul class="list-group list-group-compact">
  <li class="list-group-item">
    <div class="row mt-2">
      <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="form-check">
          <input type="checkbox" name="TxnSendMailTo" <% $checked ? 'checked="checked"' : '' |n%> value="<%$addr->address%>" id="TxnSendMailTo-<% $addr->address %>-<% $recips{$addr->address} %>" class="form-check-input" />
          <label class="form-check-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 );
% # never show the uncheck boxes message for inline add message
% if ( ( keys %recips ) || ( ! $ARGS{InlineAddMessage} ) ) {
<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>
% }
% else {
% # only show the no recipients message for inline add message
<p>
<&|/l, RT->Config->Get('WebPath')."/Ticket/ModifyPeople.html?id=".$TicketObj->Id,
&>No recipients. Add users to ticket roles on the <a href="[_1]">People page</a> to send updates.</&>
</p>
% }

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