# --
# Copyright (C) 2006-2024 KIX Service Software GmbH, https://www.kixdesk.com
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file LICENSE-GPL3 for license information (GPL3). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --

package Kernel::System::ObjectSearch::Database::Ticket;

use strict;
use warnings;

use Kernel::System::VariableCheck qw(:all);

use base qw(
    Kernel::System::ObjectSearch::Database::CommonObjectType
);

our $ObjectManagerDisabled = 1;

=head1 NAME

Kernel::System::ObjectSearch::Database::Ticket - object type module for object search

=head1 SYNOPSIS

=head1 PUBLIC INTERFACE

=over 4

=cut

sub Init {
    my ( $Self, %Param ) = @_;

    # init join map as empty hash
    $Param{Flags}->{JoinMap} = {};

    # init flag join counter with 0
    $Param{Flags}->{ArticleFlagJoinCounter} = 0;
    $Param{Flags}->{TicketFlagJoinCounter} = 0;

    # init dynamic field join counter with 0
    $Param{Flags}->{DynamicFieldJoinCounter} = 0;

    # init translation join counter with 0
    $Param{Flags}->{TranslationJoinCounter} = 0;

    # init sla join counter with 0
    $Param{Flags}->{SLAJoinCounter} = 0;

    # init sla criterion join counter with 0
    $Param{Flags}->{SLACriterionJoinCounter} = 0;

    return 1;
}

sub GetBaseDef {
    my ( $Self, %Param ) = @_;

    return {
        Select  => ['st.id', 'st.tn'],
        From    => ['ticket st'],
        OrderBy => ['st.id ASC']
    };
}

sub GetPermissionDef {
    my ( $Self, %Param ) = @_;

    my $QueueIDs = $Kernel::OM->Get('Ticket')->BasePermissionRelevantObjectIDList(
        %Param,
        Types        => ['Base::Ticket'],
        UsageContext => $Param{UserType},
        Permission   => 'READ',
    );

    if ( IsArrayRefWithData( $QueueIDs ) ) {
        return {
            Where => [ 'st.queue_id IN (' . join( q{,}, @{ $QueueIDs } ) . q{)} ]
        };
    }
    elsif(
        !IsArrayRef( $QueueIDs )
        && $QueueIDs
    ) {
        return {};
    }
    else {
        return {
            Where => [ '0=1' ]
        };
    }
}

1;

=back

=head1 TERMS AND CONDITIONS

This software is part of the KIX project
(L<https://www.kixdesk.com/>).

This software comes with ABSOLUTELY NO WARRANTY. For details, see the enclosed file
LICENSE-GPL3 for license information (GPL3). If you did not receive this file, see

<https://www.gnu.org/licenses/gpl-3.0.txt>.

=cut