%# 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 }}}
<%ARGS>
$UserField      => 'Name'
$UserOp         => '='
$UserString     => undef
$PrivilegedOnly => undef

$GroupField     => 'Name'
$GroupOp        => '='
$GroupString    => undef

$Recipients => undef
$IsFirstSubscription => undef
</%ARGS>

<%INIT>
my ($recipients_users, $recipients_groups);
my ($Users, $Groups);

if ($Recipients) {
    $recipients_users = $Recipients->{Users};
    $recipients_groups = $Recipients->{Groups};
}

if ($UserString) {
    $Users = RT::Users->new($session{'CurrentUser'});
    $Users->Limit(FIELD => $UserField, VALUE => $UserString, OPERATOR => $UserOp, CASESENSITIVE => 0);
    $Users->LimitToPrivileged if $PrivilegedOnly;

    my $excluded_users = [ $session{'CurrentUser'}->id ];
    push @$excluded_users, @{ $recipients_users || [] };
    $Users->Limit(FIELD => 'id', VALUE => $excluded_users, OPERATOR => 'NOT IN');
}

if ($GroupString) {
    $Groups = RT::Groups->new($session{'CurrentUser'});
    $Groups->LimitToUserDefinedGroups;
    $Groups->Limit(FIELD => $GroupField, VALUE => $GroupString, OPERATOR => $GroupOp, CASESENSITIVE => 0);
    $Groups->Limit(FIELD => 'id', VALUE => $recipients_groups, OPERATOR => 'NOT IN') if @{ $recipients_groups || [] };
}
</%INIT>

<div class="row">
  <div class="col-xl-6">
    <&|/l&>Find people whose</&><br />

    <div class="row mt-2">
% if ($UserString) {
      <& /Elements/SelectUsers, UserString => $UserString, UserField => $UserField, UserOp => $UserOp &>
% } else {
      <& /Elements/SelectUsers &>
% }
      <div class="col-auto">
        <input type="submit" class="btn btn-primary" name="OnlySearchForPeople" value="<&|/l&>Go!</&>" />
      </div>
    </div>

    <br />
    <&|/l&>Find groups whose</&><br />

    <div class="row mt-2">
% if ($GroupString) {
      <& /Elements/SelectGroups, GroupString => $GroupString, GroupField => $GroupField, GroupOp => $GroupOp &>
% } else {
      <& /Elements/SelectGroups &>
% }
      <div class="col-auto">
          <input type="submit" class="btn btn-primary" name="OnlySearchForGroup" value="<&|/l&>Go!</&>" />
      </div>
    </div>

    <br />
    <&|/l&>Add New Recipients</&>:<br />

% if ( $Users && $Users->Count ) {
    <div class="row mt-2">
      <div class="col-3">
        <&|/l&>User</&>
      </div>
    </div>
% while ( my $u = $Users->Next ) {
    <div class="row mt-2">
      <div class="col-3">
        <input type="hidden" name="Dashboard-Subscription-Users-<%$u->id%>" value="0" />
        <div class="form-check">
          <input type="checkbox" id="Dashboard-Subscription-Users-<%$u->id%>" name="Dashboard-Subscription-Users-<%$u->id%>" class="form-check-input" value="1">
          <label class="form-check-label" for="Dashboard-Subscription-Users-<%$u->id%>"><& '/Elements/ShowUser', User => $u, style=>'verbose' &></label>
        </div>
      </div>
    </div>
% }
% }

% if ( $Groups && $Groups->Count ) {
    <div class="row mt-2">
      <div class="col-3">
        <&|/l&>Group</&>
      </div>
    </div>
% while ( my $g = $Groups->Next ) {
    <div class="row mt-2">
      <div class="col-3">
        <input type="hidden" name="Dashboard-Subscription-Groups-<%$g->id%>" value="0" />
        <div class="form-check">
          <input type="checkbox" id="Dashboard-Subscription-Groups-<%$g->id%>" name="Dashboard-Subscription-Groups-<%$g->id%>" class="form-check-input" value="1">
          <label class="form-check-label" for="Dashboard-Subscription-Groups-<%$g->id%>"><%$g->Name%> (<%$g->Description%>)</label>
        </div>
      </div>
    </div>
% }
% }

    <div class="row mt-2">
      <div class="col-3">
        <&|/l&>Email</&>
      </div>
    </div>

% for my $i (1 .. 3) {
    <div class="row mt-2">
      <div class="col-3">
        <& /Elements/EmailInput, Name => 'Dashboard-Subscription-Email-' . $i, Size => '20' &>
      </div>
    </div>
% }
  </div>

  <div class="current-recipients col-xl-6">

    <div class="row mt-2">
      <div class="col-12">
        <&|/l&>Current Recipients</&>:
      </div>
    </div>

    <ul class="list-group list-group-compact">
% my $current_user_id = $session{CurrentUser}->id;
% my $current_user_subscribed = $recipients_users && grep { $_ == $current_user_id } @$recipients_users;
      <li class="list-group-item">
        <input type="hidden" name="Dashboard-Subscription-Users-<%$current_user_id%>" value="0" />
        <div class="form-check">
          <input type="checkbox" id="Dashboard-Subscription-Users-<%$current_user_id%>" name="Dashboard-Subscription-Users-<%$current_user_id%>" class="form-check-input" value="1" <% $current_user_subscribed || $IsFirstSubscription ? 'checked' : '' %>>
          <label class="form-check-label" for="Dashboard-Subscription-Users-<%$current_user_id%>">
% if ( $session{CurrentUser}->HasRight( Right => 'AdminUsers', Object => $RT::System ) &&
%      $session{CurrentUser}->HasRight( Right => 'ShowConfigTab', Object =>$RT::System ) ) {
            <a href="<% RT->Config->Get('WebPath') %>/Admin/Users/Modify.html?id=<% $current_user_id %>">
              <& /Elements/ShowUser, User => $session{CurrentUser}->UserObj &>
            </a>
% } else {
            <& /Elements/ShowUser, User => $session{CurrentUser}->UserObj &>
% }
            <& /Elements/ShowUserEmailFrequency, User => $session{CurrentUser}->UserObj &>
          </label>
        </div>
      </li>

% for my $user_id (@{ $recipients_users || [] }) {
% next if $user_id == $session{'CurrentUser'}->id; # already listed current user
% my $user = RT::User->new( $session{'CurrentUser'} );
% $user->Load($user_id);
% next unless $user->id and !$user->Disabled;
      <li class="list-group-item">
        <input type="hidden" name="Dashboard-Subscription-Users-<%$user_id%>" value="0" />
        <div class="form-check">
          <input type="checkbox" id="Dashboard-Subscription-Users-<%$user_id%>" name="Dashboard-Subscription-Users-<%$user_id%>" class="form-check-input" value="1" checked="checked">
          <label class="form-check-label" for="Dashboard-Subscription-Users-<%$user_id%>">
% if ( $session{CurrentUser}->HasRight( Right => 'AdminUsers', Object => $RT::System ) &&
%      $session{CurrentUser}->HasRight( Right => 'ShowConfigTab', Object =>$RT::System ) ) {
            <a href="<% RT->Config->Get('WebPath') %>/Admin/Users/Modify.html?id=<% $user_id %>">
              <& /Elements/ShowUser, User => $user &>
            </a>
% } else {
            <& /Elements/ShowUser, User => $user &>
% }
            <& /Elements/ShowUserEmailFrequency, User => $user &>
          </label>
        </div>
      </li>
% }

% for my $group_id (@{ $recipients_groups || [] }) {
% my $group = RT::Group->new( $session{'CurrentUser'} );
% $group->Load($group_id);
% next unless $group->id and !$group->Disabled;
      <li class="list-group-item">
        <input type="hidden" name="Dashboard-Subscription-Groups-<%$group_id%>" value="0" />
        <div class="form-check">
          <input type="checkbox" id="Dashboard-Subscription-Groups-<%$group_id%>" name="Dashboard-Subscription-Groups-<%$group_id%>" class="form-check-input" value="1" checked="checked">
          <label class="form-check-label" for="Dashboard-Subscription-Groups-<%$group_id%>">
% if ( $session{CurrentUser}->HasRight( Right => 'AdminGroup', Object => $RT::System ) &&
%      $session{CurrentUser}->HasRight( Right => 'ShowConfigTab', Object =>$RT::System ) ) {
            <a href="<% RT->Config->Get('WebPath') %>/Admin/Groups/Modify.html?id=<% $group_id %>">
              <% $group->Name %>
            </a>
% } else {
            <% $group->Name %>
% }
          </label>
        </div>
      </li>
% }
    </ul>
  </div>
</div>
