<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># --
# Kernel/Modules/AdminProcessManagementActivityDialog.pm - process management activity
# 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::AdminProcessManagementActivityDialog;

use strict;
use warnings;

use Kernel::System::JSON;
use Kernel::System::DynamicField;
use Kernel::System::ProcessManagement::DB::Process;
use Kernel::System::ProcessManagement::DB::Entity;
use Kernel::System::ProcessManagement::DB::Process;
use Kernel::System::ProcessManagement::DB::Activity;
use Kernel::System::ProcessManagement::DB::ActivityDialog;

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

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 MainObject EncodeObject)
        )
    {
        if ( !$Self-&gt;{$Needed} ) {
            $Self-&gt;{LayoutObject}-&gt;FatalError( Message =&gt; "Got no $Needed!" );
        }
    }

    # create additional objects
    $Self-&gt;{JSONObject}         = Kernel::System::JSON-&gt;new( %{$Self} );
    $Self-&gt;{DynamicFieldObject} = Kernel::System::DynamicField-&gt;new( %{$Self} );
    $Self-&gt;{ProcessObject}      = Kernel::System::ProcessManagement::DB::Process-&gt;new( %{$Self} );
    $Self-&gt;{EntityObject}       = Kernel::System::ProcessManagement::DB::Entity-&gt;new( %{$Self} );
    $Self-&gt;{ProcessObject}
        = Kernel::System::ProcessManagement::DB::Process-&gt;new( %{$Self} );
    $Self-&gt;{ActivityObject}
        = Kernel::System::ProcessManagement::DB::Activity-&gt;new( %{$Self} );
    $Self-&gt;{ActivityDialogObject}
        = Kernel::System::ProcessManagement::DB::ActivityDialog-&gt;new( %{$Self} );

    # create available Fields list
    $Self-&gt;{AvailableFields} = {
        Article     =&gt; 'Article',
        State       =&gt; 'StateID',
        Priority    =&gt; 'PriorityID',
        Lock        =&gt; 'LockID',
        Queue       =&gt; 'QueueID',
        CustomerID  =&gt; 'CustomerID',
        Owner       =&gt; 'OwnerID',
        PendingTime =&gt; 'PendingTime',
        Title       =&gt; 'Title',
    };

    # add service and SLA fields, if option is activated in sysconfig.
    if ( $Self-&gt;{ConfigObject}-&gt;Get('Ticket::Service') ) {
        $Self-&gt;{AvailableFields}-&gt;{Service} = 'ServiceID';
        $Self-&gt;{AvailableFields}-&gt;{SLA}     = 'SLAID';
    }

    # add ticket type field, if option is activated in sysconfig.
    if ( $Self-&gt;{ConfigObject}-&gt;Get('Ticket::Type') ) {
        $Self-&gt;{AvailableFields}-&gt;{Type} = 'TypeID';
    }

    # add responsible field, if option is activated in sysconfig.
    if ( $Self-&gt;{ConfigObject}-&gt;Get('Ticket::Responsible') ) {
        $Self-&gt;{AvailableFields}-&gt;{Responsible} = 'ResponsibleID';
    }

    my $DynamicFieldList = $Self-&gt;{DynamicFieldObject}-&gt;DynamicFieldList(
        ObjectType =&gt; [ 'Ticket', 'Article' ],
        ResultType =&gt; 'HASH',
    );

    DYNAMICFIELD:
    for my $DynamicFieldName ( values %{$DynamicFieldList} ) {

        next DYNAMICFIELD if !$DynamicFieldName;

        # do not show internal fields for process management
        next DYNAMICFIELD if $DynamicFieldName eq 'ProcessManagementProcessID';
        next DYNAMICFIELD if $DynamicFieldName eq 'ProcessManagementActivityID';

        $Self-&gt;{AvailableFields}-&gt;{"DynamicField_$DynamicFieldName"} = $DynamicFieldName;
    }

    return $Self;
}

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

    $Self-&gt;{Subaction} = $Self-&gt;{ParamObject}-&gt;GetParam( Param =&gt; 'Subaction' ) || '';

    my $ActivityDialogID = $Self-&gt;{ParamObject}-&gt;GetParam( Param =&gt; 'ID' )       || '';
    my $EntityID         = $Self-&gt;{ParamObject}-&gt;GetParam( Param =&gt; 'EntityID' ) || '';

    my %SessionData = $Self-&gt;{SessionObject}-&gt;GetSessionIDData(
        SessionID =&gt; $Self-&gt;{SessionID},
    );

    # convert JSON string to array
    $Self-&gt;{ScreensPath} = $Self-&gt;{JSONObject}-&gt;Decode(
        Data =&gt; $SessionData{ProcessManagementScreensPath}
    );

    # ------------------------------------------------------------ #
    # ActivityDialogNew
    # ------------------------------------------------------------ #
    if ( $Self-&gt;{Subaction} eq 'ActivityDialogNew' ) {

        return $Self-&gt;_ShowEdit(
            %Param,
            Action =&gt; 'New',
        );
    }

    # ------------------------------------------------------------ #
    # ActivityDialogNewAction
    # ------------------------------------------------------------ #
    elsif ( $Self-&gt;{Subaction} eq 'ActivityDialogNewAction' ) {

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

        # get Activity Dialog data
        my $ActivityDialogData;

        # get parameter from web browser
        my $GetParam = $Self-&gt;_GetParams();

        # set new confguration
        $ActivityDialogData-&gt;{Name}                       = $GetParam-&gt;{Name};
        $ActivityDialogData-&gt;{EntityID}                   = $GetParam-&gt;{EntityID};
        $ActivityDialogData-&gt;{Config}-&gt;{Interface}        = $GetParam-&gt;{Interface};
        $ActivityDialogData-&gt;{Config}-&gt;{DescriptionShort} = $GetParam-&gt;{DescriptionShort};
        $ActivityDialogData-&gt;{Config}-&gt;{DescriptionLong}  = $GetParam-&gt;{DescriptionLong};
        $ActivityDialogData-&gt;{Config}-&gt;{Permission}       = $GetParam-&gt;{Permission};
        $ActivityDialogData-&gt;{Config}-&gt;{RequiredLock}     = $GetParam-&gt;{RequiredLock} || 0;
        $ActivityDialogData-&gt;{Config}-&gt;{SubmitAdviceText} = $GetParam-&gt;{SubmitAdviceText};
        $ActivityDialogData-&gt;{Config}-&gt;{SubmitButtonText} = $GetParam-&gt;{SubmitButtonText};
        $ActivityDialogData-&gt;{Config}-&gt;{Fields}           = {};
        $ActivityDialogData-&gt;{Config}-&gt;{FieldOrder}       = [];

        if ( IsArrayRefWithData( $GetParam-&gt;{Fields} ) ) {

            FIELD:
            for my $FieldName ( @{ $GetParam-&gt;{Fields} } ) {
                next FIELD if !$FieldName;
                next FIELD if !$Self-&gt;{AvailableFields}-&gt;{$FieldName};

                # set fields hash
                $ActivityDialogData-&gt;{Config}-&gt;{Fields}-&gt;{$FieldName} = {};

                # set field order array
                push @{ $ActivityDialogData-&gt;{Config}-&gt;{FieldOrder} }, $FieldName;
            }
        }

        # add field detail config to fields
        if ( IsHashRefWithData( $GetParam-&gt;{FieldDetails} ) ) {
            FIELDDETAIL:
            for my $FieldDetail ( sort keys %{ $GetParam-&gt;{FieldDetails} } ) {
                next FIELDDETAIL if !$FieldDetail;
                next FIELDDETAIL if !$ActivityDialogData-&gt;{Config}-&gt;{Fields}-&gt;{$FieldDetail};

                $ActivityDialogData-&gt;{Config}-&gt;{Fields}-&gt;{$FieldDetail}
                    = $GetParam-&gt;{FieldDetails}-&gt;{$FieldDetail};
            }
        }

        # set correct Interface value
        my %Interfaces = (
            AgentInterface    =&gt; ['AgentInterface'],
            CustomerInterface =&gt; ['CustomerInterface'],
            BothInterfaces    =&gt; [ 'AgentInterface', 'CustomerInterface' ],
        );
        $ActivityDialogData-&gt;{Config}-&gt;{Interface}
            = $Interfaces{ $ActivityDialogData-&gt;{Config}-&gt;{Interface} };

        if ( !$ActivityDialogData-&gt;{Config}-&gt;{Interface} ) {
            $ActivityDialogData-&gt;{Config}-&gt;{Interface} = $Interfaces{Agent};
        }

        # check required parameters
        my %Error;
        if ( !$GetParam-&gt;{Name} ) {

            # add server error error class
            $Error{NameServerError}        = 'ServerError';
            $Error{NameServerErrorMessage} = 'This field is required';
        }

        if ( !$GetParam-&gt;{DescriptionShort} ) {

            # add server error error class
            $Error{DescriptionShortServerError} = 'ServerError';
            $Error{DecriptionShortErrorMessage} = 'This field is required';
        }

        # check if permission exists
        if ( defined $GetParam-&gt;{Permission} &amp;&amp; $GetParam-&gt;{Permission} ne '' ) {
            my $PermissionList = $Self-&gt;{ConfigObject}-&gt;Get('System::Permission');

            my %PermissionLookup = map { $_ =&gt; 1 } @{$PermissionList};

            if ( !$PermissionLookup{ $GetParam-&gt;{Permission} } )
            {

                # add server error error class
                $Error{PermissionServerError} = 'ServerError';
            }
        }

        # check if required lock exists
        if ( $GetParam-&gt;{RequiredLock} &amp;&amp; $GetParam-&gt;{RequiredLock} ne 1 ) {

            # add server error error class
            $Error{RequiredLockServerError} = 'ServerError';
        }

        # if there is an error return to edit screen
        if ( IsHashRefWithData( \%Error ) ) {
            return $Self-&gt;_ShowEdit(
                %Error,
                %Param,
                ActivityDialogData =&gt; $ActivityDialogData,
                Action             =&gt; 'New',
            );
        }

        # generate entity ID
        my $EntityID = $Self-&gt;{EntityObject}-&gt;EntityIDGenerate(
            EntityType =&gt; 'ActivityDialog',
            UserID     =&gt; $Self-&gt;{UserID},
        );

        # show error if can't generate a new EntityID
        if ( !$EntityID ) {
            return $Self-&gt;{LayoutObject}-&gt;ErrorScreen(
                Message =&gt; "There was an error generating a new EntityID for this ActivityDialog",
            );
        }

        # otherwise save configuration and return process screen
        my $ActivityDialogID = $Self-&gt;{ActivityDialogObject}-&gt;ActivityDialogAdd(
            Name     =&gt; $ActivityDialogData-&gt;{Name},
            EntityID =&gt; $EntityID,
            Config   =&gt; $ActivityDialogData-&gt;{Config},
            UserID   =&gt; $Self-&gt;{UserID},
        );

        # show error if can't create
        if ( !$ActivityDialogID ) {
            return $Self-&gt;{LayoutObject}-&gt;ErrorScreen(
                Message =&gt; "There was an error creating the ActivityDialog",
            );
        }

        # set entity sync state
        my $Success = $Self-&gt;{EntityObject}-&gt;EntitySyncStateSet(
            EntityType =&gt; 'ActivityDialog',
            EntityID   =&gt; $EntityID,
            SyncState  =&gt; 'not_sync',
            UserID     =&gt; $Self-&gt;{UserID},
        );

        # show error if can't set
        if ( !$Success ) {
            return $Self-&gt;{LayoutObject}-&gt;ErrorScreen(
                Message =&gt; "There was an error setting the entity sync status for ActivityDialog "
                    . "entity:$EntityID",
            );
        }

        # remove this screen from session screen path
        $Self-&gt;_PopSessionScreen( OnlyCurrent =&gt; 1 );

        my $Redirect = $Self-&gt;{ParamObject}-&gt;GetParam( Param =&gt; 'PopupRedirect' ) || '';

        # get latest config data to send it back to main window
        my $ActivityDialogConfig = $Self-&gt;_GetActivityDialogConfig(
            EntityID =&gt; $EntityID,
        );

        my $ConfigJSON = $Self-&gt;{LayoutObject}-&gt;JSONEncode( Data =&gt; $ActivityDialogConfig );

        # check if needed to open another window or if popup should go back
        if ( $Redirect &amp;&amp; $Redirect eq '1' ) {

            $Self-&gt;_PushSessionScreen(
                ID        =&gt; $ActivityDialogID,
                EntityID  =&gt; $ActivityDialogData-&gt;{EntityID},
                Subaction =&gt; 'ActivityDialogEdit'               # always use edit screen
            );

            my $RedirectField = $Self-&gt;{ParamObject}-&gt;GetParam( Param =&gt; 'PopupRedirectID' ) || '';

            # redirect to another popup window
            return $Self-&gt;_PopupResponse(
                Redirect =&gt; 1,
                Screen   =&gt; {
                    Action    =&gt; 'AdminProcessManagementField',
                    Subaction =&gt; 'FieldEdit',
                    Field     =&gt; $RedirectField,
                },
                ConfigJSON =&gt; $ConfigJSON,
            );
        }
        else {

            # remove last screen
            my $LastScreen = $Self-&gt;_PopSessionScreen();

            # check if needed to return to main screen or to be redirected to last screen
            if ( $LastScreen-&gt;{Action} eq 'AdminProcessManagement' ) {

                # close the popup
                return $Self-&gt;_PopupResponse(
                    ClosePopup =&gt; 1,
                    ConfigJSON =&gt; $ConfigJSON,
                );
            }
            else {

                # redirect to last screen
                return $Self-&gt;_PopupResponse(
                    Redirect   =&gt; 1,
                    Screen     =&gt; $LastScreen,
                    ConfigJSON =&gt; $ConfigJSON,
                );
            }
        }
    }

    # ------------------------------------------------------------ #
    # ActivityDialogEdit
    # ------------------------------------------------------------ #
    elsif ( $Self-&gt;{Subaction} eq 'ActivityDialogEdit' ) {

        # check for ActivityDialogID
        if ( !$ActivityDialogID ) {
            return $Self-&gt;{LayoutObject}-&gt;ErrorScreen(
                Message =&gt; "Need ActivityDialogID!",
            );
        }

        # remove this screen from session screen path
        $Self-&gt;_PopSessionScreen( OnlyCurrent =&gt; 1 );

        # get Activity Dialog data
        my $ActivityDialogData = $Self-&gt;{ActivityDialogObject}-&gt;ActivityDialogGet(
            ID     =&gt; $ActivityDialogID,
            UserID =&gt; $Self-&gt;{UserID},
        );

        # check for valid Activity Dialog data
        if ( !IsHashRefWithData($ActivityDialogData) ) {
            return $Self-&gt;{LayoutObject}-&gt;ErrorScreen(
                Message =&gt; "Could not get data for ActivityDialogID $ActivityDialogID",
            );
        }

        return $Self-&gt;_ShowEdit(
            %Param,
            ActivityDialogID   =&gt; $ActivityDialogID,
            ActivityDialogData =&gt; $ActivityDialogData,
            Action             =&gt; 'Edit',
        );
    }

    # ------------------------------------------------------------ #
    # ActvityDialogEditAction
    # ------------------------------------------------------------ #
    elsif ( $Self-&gt;{Subaction} eq 'ActivityDialogEditAction' ) {

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

        # get Activity Dialog Data
        my $ActivityDialogData;

        # get parameter from web browser
        my $GetParam = $Self-&gt;_GetParams();

        # set new confguration
        $ActivityDialogData-&gt;{Name}                       = $GetParam-&gt;{Name};
        $ActivityDialogData-&gt;{EntityID}                   = $GetParam-&gt;{EntityID};
        $ActivityDialogData-&gt;{Config}-&gt;{Interface}        = $GetParam-&gt;{Interface};
        $ActivityDialogData-&gt;{Config}-&gt;{DescriptionShort} = $GetParam-&gt;{DescriptionShort};
        $ActivityDialogData-&gt;{Config}-&gt;{DescriptionLong}  = $GetParam-&gt;{DescriptionLong};
        $ActivityDialogData-&gt;{Config}-&gt;{Permission}       = $GetParam-&gt;{Permission};
        $ActivityDialogData-&gt;{Config}-&gt;{RequiredLock}     = $GetParam-&gt;{RequiredLock} || 0;
        $ActivityDialogData-&gt;{Config}-&gt;{SubmitAdviceText} = $GetParam-&gt;{SubmitAdviceText};
        $ActivityDialogData-&gt;{Config}-&gt;{SubmitButtonText} = $GetParam-&gt;{SubmitButtonText};
        $ActivityDialogData-&gt;{Config}-&gt;{Fields}           = {};
        $ActivityDialogData-&gt;{Config}-&gt;{FieldOrder}       = [];

        if ( IsArrayRefWithData( $GetParam-&gt;{Fields} ) ) {

            FIELD:
            for my $FieldName ( @{ $GetParam-&gt;{Fields} } ) {
                next FIELD if !$FieldName;
                next FIELD if !$Self-&gt;{AvailableFields}-&gt;{$FieldName};

                # set fields hash
                $ActivityDialogData-&gt;{Config}-&gt;{Fields}-&gt;{$FieldName} = {};

                # set field order array
                push @{ $ActivityDialogData-&gt;{Config}-&gt;{FieldOrder} }, $FieldName;
            }
        }

        # add field detail config to fields
        if ( IsHashRefWithData( $GetParam-&gt;{FieldDetails} ) ) {
            FIELDDETAIL:
            for my $FieldDetail ( sort keys %{ $GetParam-&gt;{FieldDetails} } ) {
                next FIELDDETAIL if !$FieldDetail;
                next FIELDDETAIL if !$ActivityDialogData-&gt;{Config}-&gt;{Fields}-&gt;{$FieldDetail};

                $ActivityDialogData-&gt;{Config}-&gt;{Fields}-&gt;{$FieldDetail}
                    = $GetParam-&gt;{FieldDetails}-&gt;{$FieldDetail};
            }
        }

        # set default values for fields in case they don't have details
        for my $FieldName ( sort keys %{ $ActivityDialogData-&gt;{Config}-&gt;{Fields} } ) {
            if ( !IsHashRefWithData( $ActivityDialogData-&gt;{Config}-&gt;{Fields}-&gt;{$FieldName} ) ) {
                $ActivityDialogData-&gt;{Config}-&gt;{Fields}-&gt;{$FieldName}-&gt;{DescriptionShort}
                    = $FieldName;
            }
        }

        # set correct Interface value
        my %Interfaces = (
            AgentInterface    =&gt; ['AgentInterface'],
            CustomerInterface =&gt; ['CustomerInterface'],
            BothInterfaces    =&gt; [ 'AgentInterface', 'CustomerInterface' ],
        );
        $ActivityDialogData-&gt;{Config}-&gt;{Interface}
            = $Interfaces{ $ActivityDialogData-&gt;{Config}-&gt;{Interface} };

        if ( !$ActivityDialogData-&gt;{Config}-&gt;{Interface} ) {
            $ActivityDialogData-&gt;{Config}-&gt;{Interface} = $Interfaces{Agent};
        }

        # check required parameters
        my %Error;

        if ( !$GetParam-&gt;{Name} ) {

            # add server error error class
            $Error{NameServerError}        = 'ServerError';
            $Error{NameServerErrorMessage} = 'This field is required';
        }

        if ( !$GetParam-&gt;{DescriptionShort} ) {

            # add server error error class
            $Error{DescriptionShortServerError} = 'ServerError';
            $Error{DecriptionShortErrorMessage} = 'This field is required';
        }

        # check if permission exists
        if ( defined $GetParam-&gt;{Permission} &amp;&amp; $GetParam-&gt;{Permission} ne '' ) {

            my $PermissionList = $Self-&gt;{ConfigObject}-&gt;Get('System::Permission');

            my %PermissionLookup = map { $_ =&gt; 1 } @{$PermissionList};

            if ( !$PermissionLookup{ $GetParam-&gt;{Permission} } )
            {

                # add server error error class
                $Error{PermissionServerError} = 'ServerError';
            }
        }

        # check if required lock exists
        if ( $GetParam-&gt;{RequiredLock} &amp;&amp; $GetParam-&gt;{RequiredLock} ne 1 ) {

            # add server error error class
            $Error{RequiredLockServerError} = 'ServerError';
        }

        # if there is an error return to edit screen
        if ( IsHashRefWithData( \%Error ) ) {
            return $Self-&gt;_ShowEdit(
                %Error,
                %Param,
                ActivityDialogData =&gt; $ActivityDialogData,
                Action             =&gt; 'Edit',
            );
        }

        # otherwise save configuration and return to overview screen
        my $Success = $Self-&gt;{ActivityDialogObject}-&gt;ActivityDialogUpdate(
            ID       =&gt; $ActivityDialogID,
            Name     =&gt; $ActivityDialogData-&gt;{Name},
            EntityID =&gt; $ActivityDialogData-&gt;{EntityID},
            Config   =&gt; $ActivityDialogData-&gt;{Config},
            UserID   =&gt; $Self-&gt;{UserID},
        );

        # show error if can't update
        if ( !$Success ) {
            return $Self-&gt;{LayoutObject}-&gt;ErrorScreen(
                Message =&gt; "There was an error updating the ActivityDialog",
            );
        }

        # set entity sync state
        $Success = $Self-&gt;{EntityObject}-&gt;EntitySyncStateSet(
            EntityType =&gt; 'ActivityDialog',
            EntityID   =&gt; $ActivityDialogData-&gt;{EntityID},
            SyncState  =&gt; 'not_sync',
            UserID     =&gt; $Self-&gt;{UserID},
        );

        # show error if can't set
        if ( !$Success ) {
            return $Self-&gt;{LayoutObject}-&gt;ErrorScreen(
                Message =&gt; "There was an error setting the entity sync status for ActivityDialog "
                    . "entity:$ActivityDialogData-&gt;{EntityID}",
            );
        }

        # remove this screen from session screen path
        $Self-&gt;_PopSessionScreen( OnlyCurrent =&gt; 1 );

        my $Redirect = $Self-&gt;{ParamObject}-&gt;GetParam( Param =&gt; 'PopupRedirect' ) || '';

        # get latest config data to send it back to main window
        my $ActivityDialogConfig = $Self-&gt;_GetActivityDialogConfig(
            EntityID =&gt; $ActivityDialogData-&gt;{EntityID},
        );

        my $ConfigJSON = $Self-&gt;{LayoutObject}-&gt;JSONEncode( Data =&gt; $ActivityDialogConfig );

        # check if needed to open another window or if popup should go back
        if ( $Redirect &amp;&amp; $Redirect eq '1' ) {

            $Self-&gt;_PushSessionScreen(
                ID        =&gt; $ActivityDialogID,
                EntityID  =&gt; $ActivityDialogData-&gt;{EntityID},
                Subaction =&gt; 'ActivityDialogEdit'               # always use edit screen
            );

            my $RedirectField = $Self-&gt;{ParamObject}-&gt;GetParam( Param =&gt; 'PopupRedirectID' ) || '';

            # redirect to another popup window
            return $Self-&gt;_PopupResponse(
                Redirect =&gt; 1,
                Screen   =&gt; {
                    Action    =&gt; 'AdminProcessManagementField',
                    Subaction =&gt; 'FieldEdit',
                    Field     =&gt; $RedirectField,
                },
                ConfigJSON =&gt; $ConfigJSON,
            );
        }
        else {

            # remove last screen
            my $LastScreen = $Self-&gt;_PopSessionScreen();

            # check if needed to return to main screen or to be redirected to last screen
            if ( $LastScreen-&gt;{Action} eq 'AdminProcessManagement' ) {

                # close the popup
                return $Self-&gt;_PopupResponse(
                    ClosePopup =&gt; 1,
                    ConfigJSON =&gt; $ConfigJSON,
                );
            }
            else {

                # redirect to last screen
                return $Self-&gt;_PopupResponse(
                    Redirect   =&gt; 1,
                    Screen     =&gt; $LastScreen,
                    ConfigJSON =&gt; $ConfigJSON,
                );
            }
        }
    }

    # ------------------------------------------------------------ #
    # Close popup
    # ------------------------------------------------------------ #
    elsif ( $Self-&gt;{Subaction} eq 'ClosePopup' ) {

        # close the popup
        return $Self-&gt;_PopupResponse(
            ClosePopup =&gt; 1,
        );
    }

    # ------------------------------------------------------------ #
    # Error
    # ------------------------------------------------------------ #
    else {
        return $Self-&gt;{LayoutObject}-&gt;ErrorScreen(
            Message =&gt; "This subaction is not valid",
        );
    }
}

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

    # Get new ActivityDialog Config as JSON
    my $ProcessDump = $Self-&gt;{ProcessObject}-&gt;ProcessDump(
        ResultType =&gt; 'HASH',
        UserID     =&gt; $Self-&gt;{UserID},
    );

    my %ActivityDialogConfig;
    $ActivityDialogConfig{ActivityDialog} = ();
    $ActivityDialogConfig{ActivityDialog}-&gt;{ $Param{EntityID} }
        = $ProcessDump-&gt;{ActivityDialog}-&gt;{ $Param{EntityID} };

    return \%ActivityDialogConfig;
}

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

    # get Activity Dialog information
    my $ActivityDialogData = $Param{ActivityDialogData} || {};

    # check if last screen action is main screen
    if ( $Self-&gt;{ScreensPath}-&gt;[-1]-&gt;{Action} eq 'AdminProcessManagement' ) {

        # show close popup link
        $Self-&gt;{LayoutObject}-&gt;Block(
            Name =&gt; 'ClosePopup',
            Data =&gt; {},
        );
    }
    else {

        # show go back link
        $Self-&gt;{LayoutObject}-&gt;Block(
            Name =&gt; 'GoBack',
            Data =&gt; {
                Action    =&gt; $Self-&gt;{ScreensPath}-&gt;[-1]-&gt;{Action}    || '',
                Subaction =&gt; $Self-&gt;{ScreensPath}-&gt;[-1]-&gt;{Subaction} || '',
                ID        =&gt; $Self-&gt;{ScreensPath}-&gt;[-1]-&gt;{ID}        || '',
                EntityID  =&gt; $Self-&gt;{ScreensPath}-&gt;[-1]-&gt;{EntityID}  || '',
            },
        );
    }

    # localize available fields
    my %AvailableFields = %{ $Self-&gt;{AvailableFields} };

    if ( defined $Param{Action} &amp;&amp; $Param{Action} eq 'Edit' ) {

        # get used fields by the activity dialog
        my %AssignedFields;

        if ( IsHashRefWithData( $ActivityDialogData-&gt;{Config}-&gt;{Fields} ) ) {
            FIELD:
            for my $Field ( sort keys %{ $ActivityDialogData-&gt;{Config}-&gt;{Fields} } ) {
                next FIELD if !$Field;
                next FIELD if !$ActivityDialogData-&gt;{Config}-&gt;{Fields}-&gt;{$Field};

                $AssignedFields{$Field} = 1;
            }
        }

        # remove used fields from available list
        for my $Field ( sort keys %AssignedFields ) {
            delete $AvailableFields{$Field};
        }

        # sort by translated field names
        my %AvailableFieldsTranslated;
        for my $Field ( sort keys %AvailableFields ) {
            my $Translation = $Self-&gt;{LayoutObject}-&gt;{LanguageObject}-&gt;Get($Field);
            $AvailableFieldsTranslated{$Field} = $Translation;
        }

        # display available fields
        for my $Field (
            sort { $AvailableFieldsTranslated{$a} cmp $AvailableFieldsTranslated{$b} }
            keys %AvailableFieldsTranslated
            )
        {
            $Self-&gt;{LayoutObject}-&gt;Block(
                Name =&gt; 'AvailableFieldRow',
                Data =&gt; {
                    Field               =&gt; $Field,
                    FieldnameTranslated =&gt; $AvailableFieldsTranslated{$Field},
                },
            );
        }

        # display used fields
        ASSIGNEDFIELD:
        for my $Field ( @{ $ActivityDialogData-&gt;{Config}-&gt;{FieldOrder} } ) {
            next ASSIGNEDFIELD if !$AssignedFields{$Field};

            my $FieldConfig = $ActivityDialogData-&gt;{Config}-&gt;{Fields}-&gt;{$Field};

            my $FieldConfigJSON = $Self-&gt;{JSONObject}-&gt;Encode(
                Data =&gt; $FieldConfig,
            );

            $Self-&gt;{LayoutObject}-&gt;Block(
                Name =&gt; 'AssignedFieldRow',
                Data =&gt; {
                    Field       =&gt; $Field,
                    FieldConfig =&gt; $FieldConfigJSON,
                },
            );
        }

        # display other affected processes by editing this activity (if applicable)
        my $AffectedActivities = $Self-&gt;_CheckActivityDialogUsage(
            EntityID =&gt; $ActivityDialogData-&gt;{EntityID},
        );

        if ( @{$AffectedActivities} ) {

            $Self-&gt;{LayoutObject}-&gt;Block(
                Name =&gt; 'EditWarning',
                Data =&gt; {
                    ActivityList =&gt; join( ', ', @{$AffectedActivities} ),
                    }
            );
        }

        $Param{Title} = "Edit Activity Dialog \"$ActivityDialogData-&gt;{Name}\"";
    }
    else {

        # sort by translated field names
        my %AvailableFieldsTranslated;
        for my $Field ( sort keys %AvailableFields ) {
            my $Translation = $Self-&gt;{LayoutObject}-&gt;{LanguageObject}-&gt;Get($Field);
            $AvailableFieldsTranslated{$Field} = $Translation;
        }

        # display available fields
        for my $Field (
            sort { $AvailableFieldsTranslated{$a} cmp $AvailableFieldsTranslated{$b} }
            keys %AvailableFieldsTranslated
            )
        {
            $Self-&gt;{LayoutObject}-&gt;Block(
                Name =&gt; 'AvailableFieldRow',
                Data =&gt; {
                    Field               =&gt; $Field,
                    FieldnameTranslated =&gt; $AvailableFieldsTranslated{$Field},
                },
            );
        }

        $Param{Title} = 'Create New Activity Dialog';
    }

    # get interface infos
    if ( defined $ActivityDialogData-&gt;{Config}-&gt;{Interface} ) {
        my $InterfaceLength = scalar @{ $ActivityDialogData-&gt;{Config}-&gt;{Interface} };
        if ( $InterfaceLength == 2 ) {
            $ActivityDialogData-&gt;{Config}-&gt;{Interface} = 'BothInterfaces';
        }
        elsif ( $InterfaceLength == 1 ) {
            $ActivityDialogData-&gt;{Config}-&gt;{Interface}
                = $ActivityDialogData-&gt;{Config}-&gt;{Interface}-&gt;[0];
        }
        else {
            $ActivityDialogData-&gt;{Config}-&gt;{Interface} = 'AgentInterface';
        }
    }
    else {
        $ActivityDialogData-&gt;{Config}-&gt;{Interface} = 'AgentInterface';
    }

    # create interface selection
    $Param{InterfaceSelection} = $Self-&gt;{LayoutObject}-&gt;BuildSelection(
        Data =&gt; {
            AgentInterface    =&gt; 'Agent Interface',
            CustomerInterface =&gt; 'Customer Interface',
            BothInterfaces    =&gt; 'Agent and Customer Interface',
        },
        Name         =&gt; 'Interface',
        ID           =&gt; 'Interface',
        SelectedID   =&gt; $ActivityDialogData-&gt;{Config}-&gt;{Interface} || '',
        Sort         =&gt; 'AlphanumericKey',
        Translation  =&gt; 1,
        PossibleNone =&gt; 0,
    );

    # create permission selection
    $Param{PermissionSelection} = $Self-&gt;{LayoutObject}-&gt;BuildSelection(
        Data       =&gt; $Self-&gt;{ConfigObject}-&gt;Get('System::Permission') || ['rw'],
        Name       =&gt; 'Permission',
        ID         =&gt; 'Permission',
        SelectedID =&gt; $ActivityDialogData-&gt;{Config}-&gt;{Permission}      || '',
        Sort       =&gt; 'AlphanumericKey',
        Translation  =&gt; 1,
        PossibleNone =&gt; 1,
        Class        =&gt; $Param{PermissionServerError} || '',
    );

    # create "required lock" selection
    $Param{RequiredLockSelection} = $Self-&gt;{LayoutObject}-&gt;BuildSelection(
        Data =&gt; {
            0 =&gt; 'No',
            1 =&gt; 'Yes',
        },
        Name        =&gt; 'RequiredLock',
        ID          =&gt; 'RequiredLock',
        SelectedID  =&gt; $ActivityDialogData-&gt;{Config}-&gt;{RequiredLock} || 0,
        Sort        =&gt; 'AlphanumericKey',
        Translation =&gt; 1,
        Class       =&gt; $Param{RequiredLockServerError} || '',
    );

    # create Display selection
    $Param{DisplaySelection} = $Self-&gt;{LayoutObject}-&gt;BuildSelection(
        Data =&gt; {
            0 =&gt; 'Do not show Field',
            1 =&gt; 'Show Field',
            2 =&gt; 'Show Field As Mandatory',
        },
        Name        =&gt; 'Display',
        ID          =&gt; 'Display',
        Sort        =&gt; 'AlphanumericKey',
        Translation =&gt; 1,
    );

    # create ArticleType selection
    $Param{ArticleTypeSelection} = $Self-&gt;{LayoutObject}-&gt;BuildSelection(
        Data =&gt; [
            'note-internal',
            'note-external',
            'note-report',
            'phone',
            'fax',
            'sms',
            'webrequest',
        ],
        SelectedValue =&gt; 'note-internal',
        Name          =&gt; 'ArticleType',
        ID            =&gt; 'ArticleType',
        Sort          =&gt; 'Alphanumeric',
        Translation   =&gt; 1,
    );

    # extract parameters from config
    $Param{DescriptionShort} = $Param{ActivityDialogData}-&gt;{Config}-&gt;{DescriptionShort};
    $Param{DescriptionLong}  = $Param{ActivityDialogData}-&gt;{Config}-&gt;{DescriptionLong};
    $Param{SubmitAdviceText} = $Param{ActivityDialogData}-&gt;{Config}-&gt;{SubmitAdviceText};
    $Param{SubmitButtonText} = $Param{ActivityDialogData}-&gt;{Config}-&gt;{SubmitButtonText};

    my $Output = $Self-&gt;{LayoutObject}-&gt;Header(
        Value =&gt; $Param{Title},
        Type  =&gt; 'Small',
    );
    $Output .= $Self-&gt;{LayoutObject}-&gt;Output(
        TemplateFile =&gt; "AdminProcessManagementActivityDialog",
        Data         =&gt; {
            %Param,
            %{$ActivityDialogData},
        },
    );

    $Output .= $Self-&gt;{LayoutObject}-&gt;Footer();

    return $Output;
}

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

    my $GetParam;

    # get parameters from web browser
    for my $ParamName (
        qw( Name EntityID Interface DescriptionShort DescriptionLong Permission RequiredLock SubmitAdviceText
        SubmitButtonText )
        )
    {
        $GetParam-&gt;{$ParamName} = $Self-&gt;{ParamObject}-&gt;GetParam( Param =&gt; $ParamName ) || '';
    }

    my $Fields = $Self-&gt;{ParamObject}-&gt;GetParam( Param =&gt; 'Fields' ) || '';

    if ($Fields) {
        $GetParam-&gt;{Fields} = $Self-&gt;{JSONObject}-&gt;Decode(
            Data =&gt; $Fields,
        );
    }
    else {
        $GetParam-&gt;{Fields} = '';
    }

    my $FieldDetails = $Self-&gt;{ParamObject}-&gt;GetParam( Param =&gt; 'FieldDetails' ) || '';

    if ($FieldDetails) {
        $GetParam-&gt;{FieldDetails} = $Self-&gt;{JSONObject}-&gt;Decode(
            Data =&gt; $FieldDetails,
        );
    }
    else {
        $GetParam-&gt;{FieldDetails} = '';
    }

    return $GetParam;
}

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

    my $LastScreen;

    if ( defined $Param{OnlyCurrent} &amp;&amp; $Param{OnlyCurrent} == 1 ) {

        # check if last screen action is current screen action
        if ( $Self-&gt;{ScreensPath}-&gt;[-1]-&gt;{Action} eq $Self-&gt;{Action} ) {

            # remove last screen
            $LastScreen = pop @{ $Self-&gt;{ScreensPath} };
        }
    }
    else {

        # remove last screen
        $LastScreen = pop @{ $Self-&gt;{ScreensPath} };
    }

    # convert screens path to string (JSON)
    my $JSONScreensPath = $Self-&gt;{LayoutObject}-&gt;JSONEncode(
        Data =&gt; $Self-&gt;{ScreensPath},
    );

    # update session
    $Self-&gt;{SessionObject}-&gt;UpdateSessionID(
        SessionID =&gt; $Self-&gt;{SessionID},
        Key       =&gt; 'ProcessManagementScreensPath',
        Value     =&gt; $JSONScreensPath,
    );

    return $LastScreen;
}

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

    # add screen to the screen path
    push @{ $Self-&gt;{ScreensPath} }, {
        Action =&gt; $Self-&gt;{Action} || '',
        Subaction =&gt; $Param{Subaction},
        ID        =&gt; $Param{ID},
        EntityID  =&gt; $Param{EntityID},
    };

    # convert screens path to string (JSON)
    my $JSONScreensPath = $Self-&gt;{LayoutObject}-&gt;JSONEncode(
        Data =&gt; $Self-&gt;{ScreensPath},
    );

    # update session
    $Self-&gt;{SessionObject}-&gt;UpdateSessionID(
        SessionID =&gt; $Self-&gt;{SessionID},
        Key       =&gt; 'ProcessManagementScreensPath',
        Value     =&gt; $JSONScreensPath,
    );

    return 1;
}

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

    if ( $Param{Redirect} &amp;&amp; $Param{Redirect} eq 1 ) {
        $Self-&gt;{LayoutObject}-&gt;Block(
            Name =&gt; 'Redirect',
            Data =&gt; {
                ConfigJSON =&gt; $Param{ConfigJSON},
                %{ $Param{Screen} },
            },
        );
    }
    elsif ( $Param{ClosePopup} &amp;&amp; $Param{ClosePopup} eq 1 ) {
        $Self-&gt;{LayoutObject}-&gt;Block(
            Name =&gt; 'ClosePopup',
            Data =&gt; {
                ConfigJSON =&gt; $Param{ConfigJSON},
            },
        );
    }

    my $Output = $Self-&gt;{LayoutObject}-&gt;Header( Type =&gt; 'Small' );
    $Output .= $Self-&gt;{LayoutObject}-&gt;Output(
        TemplateFile =&gt; "AdminProcessManagementPopupResponse",
        Data         =&gt; {},
    );
    $Output .= $Self-&gt;{LayoutObject}-&gt;Footer( Type =&gt; 'Small' );

    return $Output;
}

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

    # get a list of parents with all the details
    my $List = $Self-&gt;{ActivityObject}-&gt;ActivityListGet(
        UserID =&gt; 1,
    );

    my @Usage;

    # search entity id in all parents
    PARENT:
    for my $ParentData ( @{$List} ) {
        next PARENT if !$ParentData;
        next PARENT if !$ParentData-&gt;{ActivityDialogs};
        ENTITY:
        for my $EntityID ( @{ $ParentData-&gt;{ActivityDialogs} } ) {
            if ( $EntityID eq $Param{EntityID} ) {
                push @Usage, $ParentData-&gt;{Name};
                last ENTITY;
            }
        }
    }

    return \@Usage;
}

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