<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># --
# Kernel/Modules/AdminAutoResponse.pm - provides admin std response module
# Copyright (C) 2001-2014 OTRS AG, http://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

package Kernel::Modules::AdminAutoResponse;

use strict;
use warnings;

use Kernel::System::AutoResponse;
use Kernel::System::SystemAddress;
use Kernel::System::Valid;
use Kernel::System::HTMLUtils;

sub new {
    my ( $Type, %Param ) = @_;

    # allocate new hash for object
    my $Self = {%Param};
    bless( $Self, $Type );

    # check all needed objects
    for my $Needed (qw(ParamObject DBObject LayoutObject ConfigObject LogObject)) {
        if ( !$Self-&gt;{$Needed} ) {
            $Self-&gt;{LayoutObject}-&gt;FatalError( Message =&gt; "Got no $Needed!" );
        }
    }
    $Self-&gt;{AutoResponseObject}  = Kernel::System::AutoResponse-&gt;new(%Param);
    $Self-&gt;{SystemAddressObject} = Kernel::System::SystemAddress-&gt;new(%Param);
    $Self-&gt;{ValidObject}         = Kernel::System::Valid-&gt;new(%Param);
    $Self-&gt;{HTMLUtilsObject}     = Kernel::System::HTMLUtils-&gt;new(%Param);

    return $Self;
}

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

    # ------------------------------------------------------------ #
    # change
    # ------------------------------------------------------------ #
    if ( $Self-&gt;{Subaction} eq 'Change' ) {
        my $ID = $Self-&gt;{ParamObject}-&gt;GetParam( Param =&gt; 'ID' ) || '';
        my %Data = $Self-&gt;{AutoResponseObject}-&gt;AutoResponseGet( ID =&gt; $ID, );

        my $Output = $Self-&gt;{LayoutObject}-&gt;Header();
        $Output .= $Self-&gt;{LayoutObject}-&gt;NavigationBar();
        $Self-&gt;_Edit(
            Action =&gt; 'Change',
            %Data,
        );
        $Output .= $Self-&gt;{LayoutObject}-&gt;Output(
            TemplateFile =&gt; 'AdminAutoResponse',
            Data         =&gt; \%Param,
        );
        $Output .= $Self-&gt;{LayoutObject}-&gt;Footer();
        return $Output;
    }

    # ------------------------------------------------------------ #
    # change action
    # ------------------------------------------------------------ #
    elsif ( $Self-&gt;{Subaction} eq 'ChangeAction' ) {

        # challenge token check for write action
        $Self-&gt;{LayoutObject}-&gt;ChallengeTokenCheck();

        my ( %GetParam, %Errors );
        for my $Parameter (qw(ID Name Comment ValidID Response Subject TypeID AddressID)) {
            $GetParam{$Parameter} = $Self-&gt;{ParamObject}-&gt;GetParam( Param =&gt; $Parameter ) || '';
        }

        # get composed content type
        $GetParam{ContentType} = 'text/plain';
        if ( $Self-&gt;{LayoutObject}-&gt;{BrowserRichText} ) {
            $GetParam{ContentType} = 'text/html';
        }

        # get charset
        $GetParam{Charset} = $Self-&gt;{LayoutObject}-&gt;{UserCharset};

        # check needed data
        for my $Needed (qw(Name ValidID AddressID TypeID Subject)) {
            if ( !$GetParam{$Needed} ) {
                $Errors{ $Needed . 'Invalid' } = 'ServerError';
            }
        }

        # if no errors occurred
        if ( !%Errors ) {

            # update group
            if (
                $Self-&gt;{AutoResponseObject}-&gt;AutoResponseUpdate(
                    %GetParam, UserID =&gt; $Self-&gt;{UserID}
                )
                )
            {
                $Self-&gt;_Overview();
                my $Output = $Self-&gt;{LayoutObject}-&gt;Header();
                $Output .= $Self-&gt;{LayoutObject}-&gt;NavigationBar();
                $Output .= $Self-&gt;{LayoutObject}-&gt;Notify( Info =&gt; 'Response updated!' );
                $Output .= $Self-&gt;{LayoutObject}-&gt;Output(
                    TemplateFile =&gt; 'AdminAutoResponse',
                    Data         =&gt; \%Param,
                );
                $Output .= $Self-&gt;{LayoutObject}-&gt;Footer();
                return $Output;
            }
        }

        # something has gone wrong
        my $Output = $Self-&gt;{LayoutObject}-&gt;Header();
        $Output .= $Self-&gt;{LayoutObject}-&gt;NavigationBar();
        $Output .= $Self-&gt;{LayoutObject}-&gt;Notify( Priority =&gt; 'Error' );
        $Self-&gt;_Edit(
            Action =&gt; 'Change',
            Errors =&gt; \%Errors,
            %GetParam,
        );
        $Output .= $Self-&gt;{LayoutObject}-&gt;Output(
            TemplateFile =&gt; 'AdminAutoResponse',
            Data         =&gt; \%Param,
        );
        $Output .= $Self-&gt;{LayoutObject}-&gt;Footer();
        return $Output;
    }

    # ------------------------------------------------------------ #
    # add
    # ------------------------------------------------------------ #
    elsif ( $Self-&gt;{Subaction} eq 'Add' ) {
        my %GetParam;
        $GetParam{Name} = $Self-&gt;{ParamObject}-&gt;GetParam( Param =&gt; 'Name' );
        my $Output = $Self-&gt;{LayoutObject}-&gt;Header();
        $Output .= $Self-&gt;{LayoutObject}-&gt;NavigationBar();
        $Self-&gt;_Edit(
            Action =&gt; 'Add',
            %GetParam,
        );
        $Output .= $Self-&gt;{LayoutObject}-&gt;Output(
            TemplateFile =&gt; 'AdminAutoResponse',
            Data         =&gt; \%Param,
        );
        $Output .= $Self-&gt;{LayoutObject}-&gt;Footer();
        return $Output;
    }

    # ------------------------------------------------------------ #
    # add action
    # ------------------------------------------------------------ #
    elsif ( $Self-&gt;{Subaction} eq 'AddAction' ) {

        # challenge token check for write action
        $Self-&gt;{LayoutObject}-&gt;ChallengeTokenCheck();

        my ( %GetParam, %Errors );
        for my $Parameter (qw(ID Name Comment ValidID Response Subject TypeID AddressID)) {
            $GetParam{$Parameter} = $Self-&gt;{ParamObject}-&gt;GetParam( Param =&gt; $Parameter ) || '';
        }

        # get composed content type
        $GetParam{ContentType} = 'text/plain';
        if ( $Self-&gt;{LayoutObject}-&gt;{BrowserRichText} ) {
            $GetParam{ContentType} = 'text/html';
        }

        # get charset
        $GetParam{Charset} = $Self-&gt;{LayoutObject}-&gt;{UserCharset};

        # check needed data
        for my $Needed (qw(Name ValidID AddressID TypeID Subject)) {
            if ( !$GetParam{$Needed} ) {
                $Errors{ $Needed . 'Invalid' } = 'ServerError';
            }
        }

        # if no errors occurred
        if ( !%Errors ) {

            # add state
            my $AutoResponseID = $Self-&gt;{AutoResponseObject}-&gt;AutoResponseAdd(
                %GetParam,
                UserID =&gt; $Self-&gt;{UserID}
            );
            if ($AutoResponseID) {
                $Self-&gt;_Overview();
                my $Output = $Self-&gt;{LayoutObject}-&gt;Header();
                $Output .= $Self-&gt;{LayoutObject}-&gt;NavigationBar();
                $Output .= $Self-&gt;{LayoutObject}-&gt;Notify( Info =&gt; 'Response added!' );
                $Output .= $Self-&gt;{LayoutObject}-&gt;Output(
                    TemplateFile =&gt; 'AdminAutoResponse',
                    Data         =&gt; \%Param,
                );
                $Output .= $Self-&gt;{LayoutObject}-&gt;Footer();
                return $Output;
            }
        }

        # something has gone wrong
        my $Output = $Self-&gt;{LayoutObject}-&gt;Header();
        $Output .= $Self-&gt;{LayoutObject}-&gt;NavigationBar();
        $Output .= $Self-&gt;{LayoutObject}-&gt;Notify( Priority =&gt; 'Error' );
        $Self-&gt;_Edit(
            Action =&gt; 'Add',
            Errors =&gt; \%Errors,
            %GetParam,
        );
        $Output .= $Self-&gt;{LayoutObject}-&gt;Output(
            TemplateFile =&gt; 'AdminAutoResponse',
            Data         =&gt; \%Param,
        );
        $Output .= $Self-&gt;{LayoutObject}-&gt;Footer();
        return $Output;
    }

    # ------------------------------------------------------------
    # overview
    # ------------------------------------------------------------
    else {
        $Self-&gt;_Overview();
        my $Output = $Self-&gt;{LayoutObject}-&gt;Header();
        $Output .= $Self-&gt;{LayoutObject}-&gt;NavigationBar();
        $Output .= $Self-&gt;{LayoutObject}-&gt;Output(
            TemplateFile =&gt; 'AdminAutoResponse',
            Data         =&gt; \%Param,
        );
        $Output .= $Self-&gt;{LayoutObject}-&gt;Footer();
        return $Output;
    }

}

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

    $Self-&gt;{LayoutObject}-&gt;Block(
        Name =&gt; 'Overview',
        Data =&gt; \%Param,
    );

    $Self-&gt;{LayoutObject}-&gt;Block( Name =&gt; 'ActionList' );
    $Self-&gt;{LayoutObject}-&gt;Block( Name =&gt; 'ActionOverview' );

    # get valid list
    my %ValidList        = $Self-&gt;{ValidObject}-&gt;ValidList();
    my %ValidListReverse = reverse %ValidList;

    $Param{ValidOption} = $Self-&gt;{LayoutObject}-&gt;BuildSelection(
        Data       =&gt; \%ValidList,
        Name       =&gt; 'ValidID',
        SelectedID =&gt; $Param{ValidID} || $ValidListReverse{valid},
        Class      =&gt; 'Validate_Required ' . ( $Param{Errors}-&gt;{'ValidIDInvalid'} || '' ),
    );

    $Param{AutoResponseOption} = $Self-&gt;{LayoutObject}-&gt;BuildSelection(
        Data       =&gt; { $Self-&gt;{AutoResponseObject}-&gt;AutoResponseList(), },
        Name       =&gt; 'ID',
        Max        =&gt; 75,
        Multiple   =&gt; 1,
        SelectedID =&gt; $Param{ID},
    );

    $Param{TypeOption} = $Self-&gt;{LayoutObject}-&gt;BuildSelection(
        Data       =&gt; { $Self-&gt;{AutoResponseObject}-&gt;AutoResponseTypeList(), },
        Name       =&gt; 'TypeID',
        SelectedID =&gt; $Param{TypeID},
        Class      =&gt; 'Validate_Required ' . ( $Param{Errors}-&gt;{'TypeIDInvalid'} || '' ),
    );

    $Param{SystemAddressOption} = $Self-&gt;{LayoutObject}-&gt;BuildSelection(
        Data =&gt; { $Self-&gt;{SystemAddressObject}-&gt;SystemAddressList( Valid =&gt; 1 ), },
        Name =&gt; 'AddressID',
        SelectedID  =&gt; $Param{AddressID},
        Translation =&gt; 0,
        Class       =&gt; 'Validate_Required ' . ( $Param{Errors}-&gt;{'AddressIDInvalid'} || '' ),
    );

    # shows header
    if ( $Param{Action} eq 'Change' ) {
        $Self-&gt;{LayoutObject}-&gt;Block( Name =&gt; 'HeaderEdit' );
    }
    else {
        $Self-&gt;{LayoutObject}-&gt;Block( Name =&gt; 'HeaderAdd' );
    }

    # add rich text editor
    if ( $Self-&gt;{LayoutObject}-&gt;{BrowserRichText} ) {

        # reformat from plain to html
        if ( $Param{ContentType} &amp;&amp; $Param{ContentType} =~ /text\/plain/i ) {
            $Param{Response} = $Self-&gt;{HTMLUtilsObject}-&gt;ToHTML(
                String =&gt; $Param{Response},
            );
        }
    }
    else {

        # reformat from html to plain
        if ( $Param{ContentType} &amp;&amp; $Param{ContentType} =~ /text\/html/i ) {
            $Param{Response} = $Self-&gt;{HTMLUtilsObject}-&gt;ToAscii(
                String =&gt; $Param{Response},
            );
        }
    }

    $Self-&gt;{LayoutObject}-&gt;Block(
        Name =&gt; 'OverviewUpdate',
        Data =&gt; {
            %Param,
            %{ $Param{Errors} },
        },
    );

    if ( $Self-&gt;{LayoutObject}-&gt;{BrowserRichText} ) {
        $Self-&gt;{LayoutObject}-&gt;Block(
            Name =&gt; 'RichText',
            Data =&gt; \%Param,
        );
    }

    return 1;
}

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

    $Self-&gt;{LayoutObject}-&gt;Block(
        Name =&gt; 'Overview',
        Data =&gt; \%Param,
    );

    $Self-&gt;{LayoutObject}-&gt;Block( Name =&gt; 'ActionList' );
    $Self-&gt;{LayoutObject}-&gt;Block( Name =&gt; 'ActionAdd' );

    $Self-&gt;{LayoutObject}-&gt;Block(
        Name =&gt; 'OverviewResult',
        Data =&gt; \%Param,
    );
    my %List = $Self-&gt;{AutoResponseObject}-&gt;AutoResponseList(
        UserID =&gt; 1,
        Valid  =&gt; 0,
    );

    # if there are any results, they are shown
    if (%List) {

        # get valid list
        my %ValidList = $Self-&gt;{ValidObject}-&gt;ValidList();
        for my $ID ( sort { $List{$a} cmp $List{$b} } keys %List ) {

            my %Data = $Self-&gt;{AutoResponseObject}-&gt;AutoResponseGet( ID =&gt; $ID, );
            $Self-&gt;{LayoutObject}-&gt;Block(
                Name =&gt; 'OverviewResultRow',
                Data =&gt; {
                    Valid =&gt; $ValidList{ $Data{ValidID} },
                    %Data,
                    Attachments =&gt; int rand 5,
                },
            );
        }
    }

    # otherwise a no data message is displayed
    else {
        $Self-&gt;{LayoutObject}-&gt;Block(
            Name =&gt; 'NoDataFoundMsg',
            Data =&gt; {},
        );
    }
    return 1;
}

1;
</pre></body></html>