%# 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 }}}
%# Shared read-only lifecycle viewer for a ticket or asset. The caller passes
%# the record as $Object and the object-type-specific status-change helper as
%# $UpdateURL. The cytoscape libraries are loaded by the surrounding portlet
%# widget, not here, so a status-change refresh of this component doesn't re-run
%# them.
% if ($Object && $Object->id && $lifecycle_config) {
<div class="lifecycle-viewer-portlet">
  <div class="lifecycle-viewer"
       id="lifecycle-viewer-<% $Object->id %>"
       data-config="<% JSON($lifecycle_config) %>"
       data-layout="<% JSON($lifecycle_layout) %>"
       data-lifecycle="<% $lifecycle_name %>"
       data-object-id="<% $Object->id %>"
       data-update-url="<% $UpdateURL %>"
       data-confirm-status-change="<% $confirm_status_change %>"
       data-descriptions="<% JSON(\%descriptions) %>"
       data-current="<% $Object->Status %>">
    <div class="lifecycle-viewer-graph border rounded"></div>
  </div>

  <div class="modal lifecycle-confirm-modal" id="lifecycle-confirm-<% $Object->id %>" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title"><&|/l&>Change status</&></h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="<&|/l&>Close</&>"></button>
        </div>
        <div class="modal-body">
% if ($is_asset) {
          <p><&|/l&>Change this asset's status to:</&> <strong class="lifecycle-target-status"></strong></p>
% } else {
          <p><&|/l&>Change this ticket's status to:</&> <strong class="lifecycle-target-status"></strong></p>
% }
          <div class="form-check">
            <input class="form-check-input lifecycle-confirm-skip" type="checkbox" value="1" id="lifecycle-confirm-skip-<% $Object->id %>">
            <label class="form-check-label" for="lifecycle-confirm-skip-<% $Object->id %>">
              <&|/l&>Don't show this when changing status via lifecycle</&>
            </label>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><&|/l&>Cancel</&></button>
          <button type="button" class="btn btn-primary lifecycle-confirm-ok"><&|/l&>Change status</&></button>
        </div>
      </div>
    </div>
  </div>
</div>
% }

<%INIT>
my $lifecycle_config;
my $lifecycle_layout = {};
my $lifecycle_name = '';
my %descriptions = ( statuses => {}, transitions => {} );
my $is_asset = ( $Object && $Object->isa('RT::Asset') ) ? 1 : 0;

my $confirm_status_change
    = RT->Config->Get('LifecycleStatusChangeConfirm', $session{'CurrentUser'}) ? 1 : 0;

if ( $Object && $Object->id ) {
    my $lifecycle = $Object->LifecycleObj;
    if ($lifecycle && $lifecycle->Name) {
        $lifecycle_name = $lifecycle->Name;
        $lifecycle_layout = RT::Lifecycle->LoadLifecycleLayout( LifecycleObj => $lifecycle );

        # Show only the statuses and transitions the current user can reach from
        # this object's current status; everything else is dropped.
        $lifecycle_config = $lifecycle->FilterAllowedTransitions( Object => $Object );

        # Show user-facing descriptions for tooltips
        for my $status ( map { @{ $lifecycle_config->{$_} || [] } } qw/initial active inactive/ ) {
            my $desc = $lifecycle->StatusMetadata($status)->{description};
            $descriptions{statuses}{ lc $status } = $desc if defined $desc && length $desc;
        }
        my $transitions = $lifecycle_config->{transitions} || {};
        for my $from ( keys %$transitions ) {
            next unless length $from;
            for my $to ( @{ $transitions->{$from} } ) {
                # The merged value applies any matching wildcard descriptions too.
                my $desc = $lifecycle->TransitionMetadata( from => $from, to => $to )->{description};
                $descriptions{transitions}{ lc "$from -> $to" } = $desc
                    if defined $desc && length $desc;
            }
        }

        # FilterAllowedTransitions returns a shallow copy, so deleting these
        # keeps notes (and any unreachable statuses' metadata) out of the page
        # without touching the shared config.
        delete $lifecycle_config->{status_metadata};
        delete $lifecycle_config->{transition_metadata};
    }
}
</%INIT>
<%ARGS>
$Object => undef
$UpdateURL => ''
</%ARGS>
