jsTree dynamically updated

English! place to talk about development, programming and coding
Post Reply
Giulio Soleni
Znuny wizard
Posts: 392
Joined: 30 Dec 2010, 14:35
Znuny Version: 6.0.x and 5.0.x
Real Name: Giulio Soleni
Company: IKS srl

jsTree dynamically updated

Post by Giulio Soleni »

Hi,
I've customized AgentTicketPhone.pm module to let the list of queues selectable as destination of a New Phone Ticket, not only related to agent permissions but also on the previous selected customer permissions (adding just a call to GroupMemberList function).
I am pretty sure that the perl code is good, since I added several debugging points where I log the information directly on OTRS log, and the results are as expected... anyhow here is the code (I edited only the definition of _GetTos sub from #### START EDIT line to #### END EDIT)

Code: Select all

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

    # get config object
    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');

    # check own selection
    my %NewTos;
    if ( $ConfigObject->Get('Ticket::Frontend::NewQueueOwnSelection') ) {
        %NewTos = %{ $ConfigObject->Get('Ticket::Frontend::NewQueueOwnSelection') };
    }
    else {

        # SelectionType Queue or SystemAddress?
        my %Tos;

#### START EDIT

        if ( $ConfigObject->Get('Ticket::Frontend::NewQueueSelectionType') eq 'Queue' ) {
            %Tos = $Kernel::OM->Get('Kernel::System::Ticket')->MoveList(
                %Param,
                Type    => 'create',
                Action  => $Self->{Action},
                QueueID => $Self->{QueueID},
                UserID  => $Self->{UserID},

            );

            # START DEBUG
            foreach my $TosKey (keys %Tos) {
                $Kernel::OM->Get('Kernel::System::Log')->Log(
                    Priority => 'error',
                    Message  => ">>>>> RETRIEVED QUEUE LIST $TosKey $Tos{$TosKey}",
                );
            }
            # END DEBUG

        }
        else {
            %Tos = $Kernel::OM->Get('Kernel::System::DB')->GetTableData(
                Table => 'system_address',
                What  => 'queue_id, id',
                Valid => 1,
                Clamp => 1,
            );
        }

        # get create permission queues
        my %UserGroups = $Kernel::OM->Get('Kernel::System::Group')->PermissionUserGet(
            UserID => $Self->{UserID},
            Type   => 'create',
        );

        # START DEBUG
        foreach my $UserGroupsKey (keys %UserGroups) {
             $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message  => ">>>>> RETRIEVED AGENT GRANTS $UserGroupsKey $UserGroups{$UserGroupsKey}",
             );
        }
        # END DEBUG

        my $CustomerID = $Param{CustomerUserID};
        my %CustomerGroups;
        if ($Param{CustomerUserID} ne '') {

            # START DEBUG
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message  => ">>>>> CUSTOMERGROUP UPDATED FOR $Param{CustomerUserID}",
            );
			# END DEBUG

            %CustomerGroups = $Kernel::OM->Get('Kernel::System::CustomerGroup')->GroupMemberList(
                UserID => $CustomerID,
                Type   => 'rw',
                Result => 'HASH',
            );
        }
        else {
            %CustomerGroups = ();
            # START DEBUG
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message  => ">>>>> CUSTOMERGROUP NULL",
            );
			# END DEBUG
        }
        # START DEBUG
        foreach my $CustomerGroupsKey (keys %CustomerGroups) {
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message  => ">>>>> RETRIEVED CUSTOMER GRANTS $CustomerGroupsKey $CustomerGroups{$CustomerGroupsKey}",
            );
        }
        # END DEBUG

        foreach my $UserGroupsKey (keys %UserGroups) {
             if (!$CustomerGroups{$UserGroupsKey}) {
                 delete $UserGroups{$UserGroupsKey};
             }
        }

        # START DEBUG
        foreach my $UserGroupsKey (keys %UserGroups) {
             $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message  => ">>>>> AGENT GRANTS UPDATED $UserGroupsKey $UserGroups{$UserGroupsKey}",
             );
        }
        # END DEBUG

#### END EDIT

        # build selection string
        QUEUEID:
        for my $QueueID ( sort keys %Tos ) {
            my %QueueData = $Kernel::OM->Get('Kernel::System::Queue')->QueueGet( ID => $QueueID );

            # permission check, can we create new tickets in queue
            next QUEUEID if !$UserGroups{ $QueueData{GroupID} };

            my $String = $ConfigObject->Get('Ticket::Frontend::NewQueueSelectionString')
                || '<Realname> <<Email>> - Queue: <Queue>';
            $String =~ s/<Queue>/$QueueData{Name}/g;
            $String =~ s/<QueueComment>/$QueueData{Comment}/g;
            if ( $ConfigObject->Get('Ticket::Frontend::NewQueueSelectionType') ne 'Queue' )
            {
                my %SystemAddressData = $Kernel::OM->Get('Kernel::System::SystemAddress')->SystemAddressGet(
                    ID => $Tos{$QueueID},
                );
                $String =~ s/<Realname>/$SystemAddressData{Realname}/g;
                $String =~ s/<Email>/$SystemAddressData{Name}/g;
            }
            $NewTos{$QueueID} = $String;
        }
    }

    # add empty selection
    $NewTos{''} = '-';
    return \%NewTos;
}
On the original code, the destination queue list was defined once and for all, as soon as the "New Phone Ticket" page was loaded (since the agent that created the ticket could not be changed, and therefore the list of available destination queues could not change as well).
With my change, the list of available queues is updated as soon as I select a new customer (for whom the ticket is created).

My problem is that the jsTree menu of target queues on "New Phone Ticket" form seems not to get correctly updated as soon as I switch from a customer that has only permissions on first level queues, to a customer that have permissions on sub-queues.

In other words if I select a customer that has only permissions on Queue1 and Queue2 the queue menu is correctly populated, while if I switch to a customer that has permission on Queue1::SubQueue1-1 the destination queue menu is not updated and gets stuck with a "Loading ..." label.

I hope I explained my issue ...

The problem could be fixed if I'd use the list view instead of the tree view, but I would like to keep the tree view with all its nice searching feature and all...
I think that maybe some editing should be done on Core.UI.InputFields.js file but I am not well acquainted with jsTree and javascript in general ... so could you help me fixing this problem?

Thank you
Giulio
OTRS 6.0.x on CentOS 7.x with MariaDB 10.2.x database connected to an Active Directory for Agents and Customers.
ITSM and FAQ modules installed.
Post Reply