File: SysConfig.pm

package info (click to toggle)
otrs2 6.0.32-6
  • links: PTS
  • area: non-free
  • in suites: bullseye
  • size: 197,336 kB
  • sloc: perl: 1,003,018; javascript: 75,060; xml: 70,883; php: 51,819; sql: 22,361; sh: 379; makefile: 51
file content (1038 lines) | stat: -rw-r--r-- 35,841 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --

package Kernel::Output::HTML::SysConfig;

use strict;
use warnings;

use Kernel::System::VariableCheck qw( :all );
use parent qw(Kernel::System::SysConfig::Base::Framework);

our @ObjectDependencies = (
    'Kernel::Config',
    'Kernel::Language',
    'Kernel::Output::HTML::Layout',
    'Kernel::System::SysConfig',
    'Kernel::System::Log',
    'Kernel::System::Main',
);

=head1 NAME

Kernel::Output::HTML::SysConfig - Manage HTML representation of SysConfig settings.

=head1 PUBLIC INTERFACE

=head2 new()

Create an object. Do not use it directly, instead use:

    use Kernel::System::ObjectManager;
    local $Kernel::OM = Kernel::System::ObjectManager->new();
    my $SysConfigHTMLObject = $Kernel::OM->Get('Kernel::Output::HTML::SysConfig');

=cut

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

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

    return $Self;
}

=head2 SettingRender()

Returns the specific HTML for the setting.

    my $HTMLStr = $SysConfigHTMLObject->SettingRender(
        Setting   => {
            Name             => 'Setting Name',
            XMLContentParsed => $XMLParsedToPerl,
            EffectiveValue   => "Product 6",        # or a complex structure
            DefaultValue     => "Product 5",        # or a complex structure
            IsAjax           => 1,                  # (optional) is ajax request. Default 0.
            # ...
        },
        RW     => 1,                                # (optional) Allow editing. Default 0.
        UserID => 1,                                # (required) UserID
    );

Returns:

    $HTMLStr = '<div class="Setting"><div class "Field"...</div></div>'        # or false in case of an error

=cut

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

    # Check needed stuff.
    for my $Needed (qw(Setting UserID)) {
        if ( !$Param{$Needed} ) {
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message  => "Need $Needed!",
            );
            return;
        }
    }
    if ( !IsHashRefWithData( $Param{Setting} ) ) {
        $Kernel::OM->Get('Kernel::System::Log')->Log(
            Priority => 'error',
            Message  => "Setting is invalid!",
        );
    }

    my %Setting = %{ $Param{Setting} };

    my $RW = $Param{RW};
    if ( $Setting{OverriddenFileName} ) {
        $RW = 0;
    }

    my $Result = $Self->_SettingRender(
        %Setting,
        Value                   => $Setting{XMLContentParsed}->{Value},
        RW                      => $RW,
        IsAjax                  => $Param{IsAjax},
        SkipEffectiveValueCheck => $Param{SkipEffectiveValueCheck},
        EffectiveValue          => $Setting{EffectiveValue},
        UserID                  => $Param{UserID},
    );

    my $HTML = <<"EOF";
                        <div class="Setting" data-change-time="$Param{Setting}->{ChangeTime}">
                            $Result
                        </div>
EOF

    if ($RW) {
        my $LanguageObject = $Kernel::OM->Get('Kernel::Language');

        my $SaveText   = $LanguageObject->Translate('Save this setting');
        my $CancelText = $LanguageObject->Translate('Cancel editing and unlock this setting');
        my $ResetText  = $LanguageObject->Translate('Reset this setting to its default value.');

        $HTML .= "
                                <div class='SettingUpdateBox'>
                                    <button class='CallForAction Update' aria-controls='fieldset$Param{Setting}->{DefaultID}' type='button' value='$SaveText' title='$SaveText'>
                                        <span><i class='fa fa-check'></i></span>
                                    </button>
                                    <button class='CallForAction Cancel' aria-controls='fieldset$Param{Setting}->{DefaultID}' type='button' value='$CancelText' title='$CancelText'>
                                        <span><i class='fa fa-times'></i></span>
                                    </button>
                                </div>\n";
    }

    return $HTML;
}

=head2 SettingAddItem()

Returns response that is sent when user adds new array/hash item.

    my %Result = $SysConfigHTMLObject->SettingAddItem(
        SettingStructure  => [],         # (required) array that contains structure
                                         #  where a new item should be inserted (can be empty)
        Setting           => {           # (required) Setting hash (from SettingGet())
            'DefaultID' => '8905',
            'DefaultValue' => [ 'Item 1', 'Item 2' ],
            'Description' => 'Simple array item(Min 1, Max 3).',
            'Name' => 'TestArray',
            ...
        },
        Key               => 'HashKey',  # (optional) hash key
        IDSuffix          => '_Array3,   # (optional) suffix that will be added to all input/select fields
                                         #    (it is used in the JS on Update, during EffectiveValue calculation)
        Value             => [           # (optional) Perl structure
            {
                'Array' => [
                    'Item' => [
                        {
                        'Content' => 'Item 1',
                        },
                        ...
                    ],
                ],
            },
        ],
        AddSettingContent => 0,          # (optional) if enabled, result will be inside of div with class "SettingContent"
        UserID            => 1,          # (required) UserID
    );

Returns:

    %Result = (
        'Item' => '<div class=\'SettingContent\'>
<input type=\'text\' id=\'TestArray_Array4\'
        value=\'Default value\' name=\'TestArray\' class=\' Entry\'/></div>',
    );

    or

    %Result = (
        'Error' => 'Error description',
    );

=cut

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

    for my $Needed (qw(SettingStructure Setting UserID)) {
        if ( !$Param{$Needed} ) {
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message  => "Need $Needed!",
            );
            return;
        }
    }
    $Param{Key} //= '';

    my @SettingStructure = @{ $Param{SettingStructure} };

    $Param{IDSuffix} //= '';
    my %Setting = %{ $Param{Setting} };

    my $DefaultItem;
    my %Result;

    my $Value = $Param{Value};

    if ( $Value->{Item} && $Value->{Item}->[0]->{ValueType} ) {
        my $ValueType = $Value->{Item}->[0]->{ValueType};

        my $Loaded = $Kernel::OM->Get('Kernel::System::Main')->Require(
            "Kernel::System::SysConfig::ValueType::$ValueType",
        );

        if ( !$Loaded ) {
            $Result{Error} = $Kernel::OM->Get('Kernel::Language')->Translate(
                "Unable to load %s!",
                "Kernel::System::SysConfig::ValueType::$ValueType"
            );
            return %Result;
        }

        my $BackendObject = $Kernel::OM->Get(
            "Kernel::System::SysConfig::ValueType::$ValueType",
        );

        $DefaultItem = $BackendObject->DefaultItemAdd();

    }
    elsif ( $Value->{Item} && $Value->{Item}->[0]->{ValueType} eq 'VacationDaysOneTime' ) {
        $DefaultItem = {
            Item => {
                Content => '',
            },
            ValueType => 'VacationDaysOneTime',
        };
    }
    elsif ( $Value->{Array} && $Value->{Array}->[0]->{DefaultItem} ) {
        $DefaultItem = $Value->{Array}->[0]->{DefaultItem}->[0];
    }
    elsif ( $Value->{Hash} && $Value->{Hash}->[0]->{DefaultItem} ) {
        $DefaultItem = $Value->{Hash}->[0]->{DefaultItem}->[0];
    }

    if ( !$DefaultItem && $Value->{Array} ) {
        my $DedicatedDefaultItem = $Value->{Array}->[0];

        my @Structure = split m{_Array|_Hash\#\#\#}smx, $Param{IDSuffix};
        shift @Structure;

        my $Index = 0;

        STRUCTURE:
        for my $StructureItem (@SettingStructure) {
            if ( $StructureItem eq 'Hash' ) {
                my $HashKey = $Structure[$Index];

                my ($Item) = grep { $HashKey eq $_->{Key} } @{ $DedicatedDefaultItem->{Item} };
                last STRUCTURE if !$Item;

                $DedicatedDefaultItem = $Item->{Hash}->[0];
            }
            else {
                my $ArrayIndex = $Structure[$Index];
                last STRUCTURE if !$DedicatedDefaultItem->{Item}->{$ArrayIndex};

                $DedicatedDefaultItem = $DedicatedDefaultItem->{Item}->[$ArrayIndex]->{Array}->[0];
            }

            $Index++;
        }

        if ( $DedicatedDefaultItem->{DefaultItem} ) {
            $DefaultItem      = $DedicatedDefaultItem->{DefaultItem}->[0];
            @SettingStructure = ();
        }
        else {

            # Simple item
            $DefaultItem = {
                Item => {
                    Content => '',
                },
            };
        }
    }
    elsif ( !$DefaultItem && $Value->{Hash} ) {

        my $DedicatedDefaultItem = $Value->{Hash}->[0];

        my @Structure = split m{_Array|_Hash\#\#\#}smx, $Param{IDSuffix};
        shift @Structure;

        my $Index = 0;

        STRUCTURE:
        for my $StructureItem (@SettingStructure) {

            # NOTE: Array elements must contain same structure.
            if ( $StructureItem eq 'Hash' ) {

                # Check original XML structure and search for the element with the same key.
                # If found, system will use this structure.

                my $HashKey = $Structure[$Index];

                my ($Item) = grep { $HashKey eq $_->{Key} } @{ $DedicatedDefaultItem->{Item} };
                last STRUCTURE if !$Item;

                $DedicatedDefaultItem = $Item->{Hash}->[0];
            }

            $Index++;
        }

        if ( $DedicatedDefaultItem->{DefaultItem} ) {
            $DefaultItem      = $DedicatedDefaultItem->{DefaultItem}->[0];
            @SettingStructure = ();
        }
        else {

            # Simple item
            $DefaultItem = {
                Item => {
                    Content => '',
                },
            };
        }
    }

    for my $StructureItem ( reverse @SettingStructure ) {
        if ( $StructureItem eq 'Array' ) {
            $DefaultItem = $DefaultItem->{Array}->[0]->{DefaultItem}->[0];

            if ( !$DefaultItem ) {
                $DefaultItem = {
                    Item => {
                        Content => '',
                    },
                };
            }
        }
        else {
            # Check if there is a defintion for this item in Default item.
            if ( $DefaultItem->{Hash} && $DefaultItem->{Hash}->[0]->{Item} ) {
                ($DefaultItem) = grep { $_->{Key} eq $Param{Key} } @{ $DefaultItem->{Hash}->[0]->{Item} };
            }

            # If it's not found and there is Default item defined, use first value.
            else {
                $DefaultItem = $DefaultItem->{Hash}->[0]->{DefaultItem}->[0];
            }

            # Fallback to the simple item.
            if ( !$DefaultItem ) {
                $DefaultItem = {
                    Item => {
                        Content => '',
                    },
                };
            }
        }
    }

    # Default fallback
    if ( !$DefaultItem ) {
        $DefaultItem = {
            Item => {
                Content => '',
            },
        };
    }

    if ( $DefaultItem->{Item} || $DefaultItem->{ValueType} ) {
        my $ValueType = $DefaultItem->{ValueType} || 'String';

        my $Loaded = $Kernel::OM->Get('Kernel::System::Main')->Require(
            "Kernel::System::SysConfig::ValueType::$ValueType",
        );

        if ( !$Loaded ) {
            $Result{Error} = $Kernel::OM->Get('Kernel::Language')->Translate(
                "Unable to load %s!",
                "Kernel::System::SysConfig::ValueType::$ValueType"
            );
            return %Result;
        }

        my $BackendObject = $Kernel::OM->Get(
            "Kernel::System::SysConfig::ValueType::$ValueType",
        );

        # Check if ValueType should add SettingContent.
        my $AddSettingContent = $BackendObject->AddSettingContent();

        if ($AddSettingContent) {
            $Result{Item} = "<div class='SettingContent'>\n";
        }

        $Result{Item} .= $BackendObject->AddItem(
            Name        => $Setting{Name},
            DefaultItem => $DefaultItem,
            IDSuffix    => $Param{IDSuffix},
            UserID      => $Param{UserID},
        );

        if ($AddSettingContent) {
            $Result{Item} .= '</div>';
        }
    }
    else {
        $Result{Item} //= '';

        if ( $DefaultItem->{Array} ) {
            pop @SettingStructure;
            $Param{SettingStructure} = \@SettingStructure;

            # new array item
            my %SubResult = $Self->SettingAddItem(
                %Param,
                Value             => $DefaultItem,
                IDSuffix          => $Param{IDSuffix} . '_Array1',
                AddSettingContent => 0,
                UserID            => $Param{UserID},
            );
            return %SubResult if $SubResult{Error};

            if ( $Param{AddSettingContent} ) {
                $Result{Item} .= "<div class=\"SettingContent\">\n";
            }
            $Result{Item} .= '<div class="Array">';
            $Result{Item} .= '<div class="ArrayItem">';
            $Result{Item} .= $SubResult{Item};

            $Result{Item} .= "</div>\n";
            $Result{Item} .= "</div>\n";
            if ( $Param{AddSettingContent} ) {
                $Result{Item} .= "</div>\n";
            }
        }
        elsif ( $DefaultItem->{Hash} ) {
            my $AddKey = $Kernel::OM->Get('Kernel::Language')->Translate("Add key");

            pop @SettingStructure;
            $Param{SettingStructure} = \@SettingStructure;

            # new hash item
            my %SubResult = $Self->SettingAddItem(
                %Param,
                Value             => $DefaultItem,
                IDSuffix          => $Param{IDSuffix} . "_Hash###$Param{Key}",
                AddSettingContent => 0,
                UserID            => $Param{UserID},
            );
            return %SubResult if $SubResult{Error};

            if ( $Param{AddSettingContent} ) {
                $Result{Item} .= "<div class=\"SettingContent\">\n";
            }

            $Result{Item} .= "
            <div class=\"Hash\">
                <div class=\"HashItem\">
                    <input type=\"text\" class=\"Key\" data-suffix=\"$Param{IDSuffix}_Hash###\">
                    <button class=\"AddKey\" value=\"$AddKey\" type=\"button\" title=\"$AddKey\">
                        <i class=\"fa fa-plus-circle\"></i>
                        <span class=\"InvisibleText\">$AddKey</span>
                    </button>
                </div>
            </div>";

            if ( $Param{AddSettingContent} ) {
                $Result{Item} .= "</div>\n";
            }
        }
    }

    return %Result;
}

=head1 PRIVATE INTERFACE

=head2 _SettingRender()

Recursive helper for SettingRender().

    my $HTMLStr = $SysConfigObject->_SettingRender(
        Name             => 'Setting Name',
        Value            => $XMLParsedToPerlValue,  # (required)
        EffectiveValue   => "Product 6",            # (required) or a complex structure
        DefaultValue     => "Product 5",            # (optional) or a complex structure
        ValueType        => "String",               # (optional)
        IsAjax           => 1,                      # (optional) Default 0.
        # ...
        RW => 1,                                    # (optional) Allow editing. Default 0.
        IsArray => 1,                               # (optional) Item is part of the array
        IsHash  => 1,                               # (optional) Item is part of the hash
        Key     => 'Key',                           # (optional) Hash key (if available)
        SkipEffectiveValueCheck => 1,               # (optional) If enabled, system will not perform effective value check.
                                                    #            Default: 1.
        UserID => 1,                                # (required) UserID
    );

Returns:

    $HTMLStr = '<div class "Field"...</div>'        # or false in case of an error

=cut

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

    for my $Needed (qw(Value EffectiveValue UserID)) {
        if ( !defined $Param{$Needed} ) {
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message  => "Need $Needed!",
            );

            return;
        }
    }
    $Param{IDSuffix}       //= '';
    $Param{EffectiveValue} //= '';

    my $Result = $Param{Result} || '';
    my %Objects;
    if ( $Param{Objects} ) {
        %Objects = %{ $Param{Objects} };
    }

    my $LanguageObject = $Kernel::OM->Get('Kernel::Language');

    # Make sure structure is correct.
    return $Result if !IsArrayRefWithData( $Param{Value} );
    return $Result if !IsHashRefWithData( $Param{Value}->[0] );

    if ( $Param{Value}->[0]->{Item} ) {

        # Make sure structure is correct.
        return $Result if !IsArrayRefWithData( $Param{Value}->[0]->{Item} );
        return $Result if !IsHashRefWithData( $Param{Value}->[0]->{Item}->[0] );

        # Set default ValueType.
        my $ValueType = "String";

        if ( $Param{Value}->[0]->{Item}->[0]->{ValueType} ) {
            $ValueType = $Param{Value}->[0]->{Item}->[0]->{ValueType};
        }

        if ( !$Objects{$ValueType} ) {

            # Load required class.
            my $Loaded = $Kernel::OM->Get('Kernel::System::Main')->Require(
                "Kernel::System::SysConfig::ValueType::$ValueType",
            );

            return $Result if !$Loaded;

            # Create object instance.
            $Objects{$ValueType} = $Kernel::OM->Get(
                "Kernel::System::SysConfig::ValueType::$ValueType",
            );
        }

        $Result = $Objects{$ValueType}->SettingRender(
            %Param,
            Item => $Param{Value}->[0]->{Item},
        );
    }

    elsif ( $Param{Value}->[0]->{Hash} ) {

        # Make sure structure is correct.
        return {} if !IsArrayRefWithData( $Param{Value}->[0]->{Hash} );
        return {} if ref $Param{Value}->[0]->{Hash}->[0] ne 'HASH';

        if ( !$Param{Value}->[0]->{Hash}->[0]->{Item} ) {
            $Param{Value}->[0]->{Hash}->[0]->{Item} = [];
            delete $Param{Value}->[0]->{Content};
        }

        return {} if ref $Param{Value}->[0]->{Hash}->[0]->{Item} ne 'ARRAY';

        my $ModifiedXMLParsed = $Self->SettingModifiedXMLContentParsedGet(
            ModifiedSetting => {
                EffectiveValue => $Param{EffectiveValue},
            },
            DefaultSetting => {
                XMLContentParsed => {
                    Value => $Param{Value},
                },
            },
        );
        my @Items;

        my $RemoveThisEntry = $LanguageObject->Translate("Remove this entry");
        $Result .= "<div class='Hash' ";
        if ( $ModifiedXMLParsed->[0]->{Hash}->[0]->{MinItems} ) {
            $Result .= "data-min-items='" . $ModifiedXMLParsed->[0]->{Hash}->[0]->{MinItems} . "' ";
        }

        if ( $ModifiedXMLParsed->[0]->{Hash}->[0]->{MaxItems} ) {
            $Result .= "data-max-items='" . $ModifiedXMLParsed->[0]->{Hash}->[0]->{MaxItems} . "' ";
        }
        $Result .= ">";

        my $Index = 0;

        ITEM:
        for my $Item ( @{ $ModifiedXMLParsed->[0]->{Hash}->[0]->{Item} } ) {
            next ITEM if !IsHashRefWithData($Item);

            $Index++;

            # Add attributes that are defined in the XML file to the corresponding ModifiedXMLParsed items.
            if ( $Param{Value}->[0]->{Hash}->[0]->{Item} ) {

                my ($HashItem) = grep { defined $_->{Key} && $_->{Key} eq $Item->{Key} }
                    @{ $Param{Value}->[0]->{Hash}->[0]->{Item} };

                if ($HashItem) {

                    ATTRIBUTE:
                    for my $Attribute ( sort keys %{$HashItem} ) {

                        # Do not override core attributes.
                        next ATTRIBUTE if grep { $Attribute eq $_ } qw(Content DefaultItem Hash Array Key SelectedID);

                        if ( $Attribute eq 'Item' ) {

                            if (
                                !$HashItem->{Item}->[0]->{ValueType}
                                || $HashItem->{Item}->[0]->{ValueType} ne 'Option'
                                )
                            {
                                # Skip Items that contain Options (they can't be modified).
                                next ATTRIBUTE;
                            }
                        }

                        $Item->{$Attribute} = $HashItem->{$Attribute};
                    }
                }
            }

            my $DefaultValueType;

            if ( $Param{Value}->[0]->{Hash}->[0]->{DefaultItem} ) {
                $DefaultValueType = $Param{Value}->[0]->{Hash}->[0]->{DefaultItem}->[0]->{ValueType};
            }

            if ( $Item->{Hash} || $Item->{Array} ) {

                # Complex structure - HoA or HoH

                my $Key = $Item->{Hash} ? 'Hash' : 'Array';

                $Result .= "<div class='HashItem'>\n";

                # Gather the value type from the default item, from a previous call
                #   or string as default.
                my $ValueType = $DefaultValueType
                    || $Param{ValueType}
                    || 'String';

                my $HTMLKey = '';

                # Check if complex structure has a key element.
                if ( defined $Item->{Key} ) {
                    $HTMLKey = $Kernel::OM->Get('Kernel::Output::HTML::Layout')->Ascii2Html(
                        Text => $Item->{Key},
                        Type => 'Normal',
                    );
                    $Result .= "
                        <input class=\"Key\" type=\"text\" name=\"$Param{Name}Key\" value=\"$HTMLKey\"/>";
                }
                $Result .= "<div class=\"SettingContent\">\n";

                my $IDSuffix = $Param{IDSuffix} . "_Hash###$HTMLKey";

                if ( $Param{Value}->[0]->{Hash}->[0]->{DefaultItem} ) {
                    if ( $Param{Value}->[0]->{Hash}->[0]->{DefaultItem}->[0]->{Array} ) {
                        my $DefaultItem
                            = $Param{Value}->[0]->{Hash}->[0]->{DefaultItem}->[0]->{Array}->[0]->{DefaultItem};

                        if ($DefaultItem) {

                            # Append default item parameters.
                            $Item->{Array}->[0] = {
                                %{ $Item->{Array}->[0] },
                                DefaultItem => $DefaultItem,
                            };
                        }
                    }
                    elsif ( $Param{Value}->[0]->{Hash}->[0]->{DefaultItem}->[0]->{Hash} ) {
                        my $DefaultItem
                            = $Param{Value}->[0]->{Hash}->[0]->{DefaultItem}->[0]->{Hash}->[0]->{DefaultItem};

                        if ($DefaultItem) {

                            # Append default item parameters.
                            $Item->{Hash}->[0] = {
                                %{ $Item->{Hash}->[0] },
                                DefaultItem => $DefaultItem,
                            };
                        }
                    }
                }

                # Start recursion.
                $Result .= $Self->_SettingRender(
                    %Param,
                    EffectiveValue => $Param{EffectiveValue}->{ $Item->{Key} },
                    Value          => [$Item],
                    Objects        => \%Objects,
                    ValueType      => $ValueType,
                    IDSuffix       => $IDSuffix,
                );
                $Result .= "</div>\n";

                if ( $Param{RW} ) {
                    $Result
                        .= "<button value=\"$RemoveThisEntry\" title=\"$RemoveThisEntry\" type=\"button\" class=\"RemoveButton\">
        <i class=\"fa fa-minus-circle\"></i>
        <span class=\"InvisibleText\">$RemoveThisEntry</span>
    </button>";
                }

                $Result .= "</div>\n";
            }
            else {
                my $HashItem = "<div class='HashItem'>\n";

                # Gather the Value type from parent, from item, from previous call
                #   or use string as default.
                my $ValueType = $Item->{ValueType}
                    || $DefaultValueType
                    || $Param{ValueType}
                    || 'String';

                # Output key element.
                $HashItem .= "<input class=\"Key\" type=\"text\" name=\"$Param{Name}Key\" value=\"$Item->{Key}\" ";
                if ( !$Param{RW} ) {
                    $HashItem .= "disabled=\"disabled\" ";
                }

                $HashItem .= "readonly=\"readonly\" ";
                $HashItem .= "/>\n";

                if ( !$Objects{$ValueType} ) {

                    # Make sure the ValueType backed is present and is syntactically correct.
                    my $Loaded = $Kernel::OM->Get('Kernel::System::Main')->Require(
                        "Kernel::System::SysConfig::ValueType::$ValueType",
                    );

                    return $Result if !$Loaded;

                    # Create object instance
                    $Objects{$ValueType} = $Kernel::OM->Get(
                        "Kernel::System::SysConfig::ValueType::$ValueType",
                    );
                }

                my $IDSuffix = $Param{IDSuffix} || '';
                $IDSuffix .= "_Hash###$Item->{Key}";

                my $ValueAttribute = $Objects{$ValueType}->ValueAttributeGet();

                # Output item.
                $HashItem .= $Objects{$ValueType}->SettingRender(
                    %Param,
                    Name           => $Param{Name},
                    EffectiveValue => $Item->{$ValueAttribute} // '',
                    Class          => 'Content',
                    Item           => [$Item],
                    IsAjax         => $Param{IsAjax},
                    IsHash         => 1,
                    Key            => $Item->{Key},
                    IDSuffix       => $IDSuffix,
                );

                if ( $Param{RW} ) {

                    # Check if item is removable
                    if ( $ValueType eq ( $DefaultValueType || 'String' ) ) {

                        $HashItem .= "
    <button value=\"$RemoveThisEntry\" title=\"$RemoveThisEntry\" type=\"button\" class=\"RemoveButton\">
        <i class=\"fa fa-minus-circle\"></i>
        <span class=\"InvisibleText\">$RemoveThisEntry</span>
    </button>";
                    }
                }
                $HashItem .= "</div>\n";

                $Result .= $HashItem;
            }
        }

        my $KeyTranslation     = $LanguageObject->Translate('Key');
        my $ContentTranslation = $LanguageObject->Translate('Content');

        my $AddNewEntry = $LanguageObject->Translate("Add new entry");
        $Result .= "
    <button data-suffix=\"$Param{IDSuffix}_Hash###\" value=\"$AddNewEntry\" title=\"$AddNewEntry\" type=\"button\" class=\"AddHashKey";
        if ( !$Param{RW} ) {
            $Result .= " Hidden";
        }
        $Result .= "\">
        <i class=\"fa fa-plus-circle\"></i>
        <span class=\"InvisibleText\">$AddNewEntry</span>
    </button>";

        $Result .= "</div>";
    }

    elsif ( $Param{Value}->[0]->{Array} ) {

        # Make sure structure is correct.
        return [] if !IsArrayRefWithData( $Param{Value}->[0]->{Array} );
        return [] if ref $Param{Value}->[0]->{Array}->[0] ne 'HASH';

        if ( !$Param{Value}->[0]->{Array}->[0]->{Item} ) {
            $Param{Value}->[0]->{Array}->[0]->{Item} = [];
        }

        return [] if ref $Param{Value}->[0]->{Array}->[0]->{Item} ne 'ARRAY';

        my $ModifiedXMLParsed = $Self->SettingModifiedXMLContentParsedGet(
            ModifiedSetting => {
                EffectiveValue => $Param{EffectiveValue},
            },
            DefaultSetting => {
                XMLContentParsed => {
                    Value => $Param{Value},
                },
            },
        );

        my @Items;

        my $RemoveThisEntry = $LanguageObject->Translate("Remove this entry");

        $Result .= "<div class='Array' ";
        if ( $ModifiedXMLParsed->[0]->{Array}->[0]->{MinItems} ) {
            $Result .= "data-min-items='" . $ModifiedXMLParsed->[0]->{Array}->[0]->{MinItems} . "' ";
        }

        if ( $ModifiedXMLParsed->[0]->{Array}->[0]->{MaxItems} ) {
            $Result .= "data-max-items='" . $ModifiedXMLParsed->[0]->{Array}->[0]->{MaxItems} . "' ";
        }
        $Result .= ">";

        my $Index = 0;

        ITEM:
        for my $Item ( @{ $ModifiedXMLParsed->[0]->{Array}->[0]->{Item} } ) {
            next ITEM if !IsHashRefWithData($Item);

            $Index++;

            # check attributes
            if ( $Param{Value}->[0]->{Array}->[0]->{Item} ) {

                ATTRIBUTE:
                for my $Attribute ( sort keys %{ $Param{Value}->[0]->{Array}->[0]->{Item}->[0] } ) {
                    next ATTRIBUTE if grep { $Attribute eq $_ } qw(Content Item DefaultItem Hash Array);

                    $Item->{$Attribute} = $Param{Value}->[0]->{Array}->[0]->{Item}->[0]->{$Attribute};
                }
            }

            my $DefaultValueType;
            if ( $Param{Value}->[0]->{Array}->[0]->{DefaultItem} ) {
                $DefaultValueType = $Param{Value}->[0]->{Array}->[0]->{DefaultItem}->[0]->{ValueType};
            }

            if ( $Item->{Hash} || $Item->{Array} ) {

                # Complex structure - AoA or AoH

                my $Key = $Item->{Hash} ? 'Hash' : 'Array';

                $Result .= "<div class='ArrayItem'>";

                my $IDSuffix = $Param{IDSuffix} || '';
                $IDSuffix .= "_Array$Index";

                if ( $Param{Value}->[0]->{Array}->[0]->{DefaultItem} ) {

                    if ( $Param{Value}->[0]->{Array}->[0]->{DefaultItem}->[0]->{Array} ) {
                        my $DefaultItem
                            = $Param{Value}->[0]->{Array}->[0]->{DefaultItem}->[0]->{Array}->[0]->{DefaultItem};

                        if ($DefaultItem) {

                            # Append default item parameters.
                            $Item->{Array}->[0] = {
                                %{ $Item->{Array}->[0] },
                                DefaultItem => $DefaultItem,
                            };
                        }
                    }
                    elsif ( $Param{Value}->[0]->{Array}->[0]->{DefaultItem}->[0]->{Hash} ) {
                        my $DefaultItem
                            = $Param{Value}->[0]->{Array}->[0]->{DefaultItem}->[0]->{Hash}->[0]->{DefaultItem};

                        if ($DefaultItem) {

                            # Append default item parameters.
                            $Item->{Hash}->[0] = {
                                %{ $Item->{Hash}->[0] },
                                DefaultItem => $DefaultItem,
                            };
                        }
                    }
                }

                # Start recursion.
                $Result .= $Self->_SettingRender(
                    %Param,
                    Value          => [$Item],
                    EffectiveValue => $Param{EffectiveValue}->[ $Index - 1 ],
                    IDSuffix       => $IDSuffix,
                );
                if ( $Param{RW} ) {
                    $Result .= "
    <button value=\"$RemoveThisEntry\" title=\"$RemoveThisEntry\" type=\"button\" class=\"RemoveButton\">
        <i class=\"fa fa-minus-circle\"></i>
        <span class=\"InvisibleText\">$RemoveThisEntry</span>
    </button>";
                }
                $Result .= "</div>";
            }
            else {

                # Gather the value type from the default item, from the item, from a previous call
                #   or string as default.
                my $ValueType = $DefaultValueType
                    || $Item->{ValueType}
                    || $Param{ValueType}
                    || 'String';

                if ( !$Objects{$ValueType} ) {

                    # Make sure the ValueType backed is present and is syntactically correct.
                    my $Loaded = $Kernel::OM->Get('Kernel::System::Main')->Require(
                        "Kernel::System::SysConfig::ValueType::$ValueType",
                    );

                    return $Result if !$Loaded;

                    # Create object instance
                    $Objects{$ValueType} = $Kernel::OM->Get(
                        "Kernel::System::SysConfig::ValueType::$ValueType",
                    );
                }

                my $RenderItem;

                if ( $Item->{Content} ) {
                    $RenderItem = [$Item];
                }
                else {
                    $RenderItem = $Item->{Item};
                }

                my $ValueAttribute = $Objects{$ValueType}->ValueAttributeGet();
                my $EffectiveValue = $RenderItem->[0]->{$ValueAttribute} || '';

                my $IDSuffix = $Param{IDSuffix} || '';
                $IDSuffix .= "_Array$Index";

                my $ArrayItem = "<div class='ArrayItem'>";
                $ArrayItem .= $Objects{$ValueType}->SettingRender(
                    %Param,
                    Name           => $Param{Name},
                    EffectiveValue => $EffectiveValue || '',
                    Class          => 'Entry',
                    Item           => $RenderItem,
                    IsAjax         => $Param{IsAjax},
                    IsArray        => 1,
                    IDSuffix       => $IDSuffix,
                );

                if ( $Param{RW} ) {

                    $ArrayItem .= "
    <button value=\"$RemoveThisEntry\" title=\"$RemoveThisEntry\" type=\"button\" class=\"RemoveButton\">
        <i class=\"fa fa-minus-circle\"></i>
        <span class=\"InvisibleText\">$RemoveThisEntry</span>
    </button>";
                }

                $ArrayItem .= "</div>\n";

                $Result .= $ArrayItem;
            }
        }

        my $Size = @{ $ModifiedXMLParsed->[0]->{Array}->[0]->{Item} } + 1;
        $Param{IDSuffix} //= '';

        my $AddNewEntry = $LanguageObject->Translate("Add new entry");

        $Result .= "
    <button value=\"$AddNewEntry\" title=\"$AddNewEntry\" type=\"button\" data-suffix=\"$Param{IDSuffix}_Array$Size\" class=\"AddArrayItem";
        if ( !$Param{RW} ) {
            $Result .= " Hidden";
        }
        $Result .= "\">
        <i class=\"fa fa-plus-circle\"></i>
        <span class=\"InvisibleText\">$AddNewEntry</span>
    </button>\n";

        $Result .= "</div>";
    }

    return $Result;
}

1;

=head1 TERMS AND CONDITIONS

This software is part of the OTRS project (L<https://otrs.org/>).

This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (GPL). If you
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.

=cut