TicketWizard.pm
35.4 KB
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
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
# --
# Kernel/System/TicketWizard.pm - core module - SeTIC - UFSC - http://setic.ufsc.br/
# Rodrigo Gonçalves - rodrigo.g@ufsc.br
#
# Version 01/12/2015 - Support for OTRS 4.0.3
# Version 2017-06-20 - Code refactoring
# Version 2018-01-18 - Support for OTRS 6
#
# --
# 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::System::TicketWizard;
use strict;
use warnings;
use utf8;
use Data::Dumper;
use URI::Escape;
use Encode qw( encode_utf8 decode_utf8 );
use JSON qw(encode_json decode_json);
our @ObjectDependencies = (
"Kernel::System::Web::Request",
"Kernel::System::DB",
"KerneL::System::Log",
"KerneL::System::Ticket",
"KerneL::System::Type",
"Kernel::System::YAM",
"Kernel::Config",
"Kernel::System::DynamicField",
"Kernel::System::Service",
"Kernel::System::ServiceForm",
"Kernel::System::SystemAddress" );
sub new {
my ( $Type, %Param ) = @_;
# allocate new hash for object
my $Self = {%Param};
bless( $Self, $Type );
return $Self;
}
=head
Returns fields structure for new ticket form wizard public
=cut
sub GetBasicFieldsOptionsPublic {
my ( $Self, %Param ) = @_;
return $Kernel::OM->Get("Kernel::Config")->Get("Ticket::Frontend::Customer::NewTicketWizard")->{BasicFormPublic};
}
=head
Returns fields schema for new ticket wizard public
=cut
sub GetBasicFieldsSchemaPublic {
my ( $Self, %Param ) = @_;
return $Kernel::OM->Get("Kernel::Config")->Get("Ticket::Frontend::Customer::NewTicketWizard")->{BasicSchemaPublic};
}
=head
Returns fields structure for new ticket form wizard
=cut
sub GetBasicFieldsOptions {
my ( $Self, %Param ) = @_;
return $Kernel::OM->Get("Kernel::Config")->Get("Ticket::Frontend::Customer::NewTicketWizard")->{BasicForm};
}
=head
Returns fields schema for new ticket wizard
=cut
sub GetBasicFieldsSchema {
my ( $Self, %Param ) = @_;
return $Kernel::OM->Get("Kernel::Config")->Get("Ticket::Frontend::Customer::NewTicketWizard")->{BasicSchema};
}
=head
Replaces OTRS system field values (service and type)
=cut
sub ReplaceOTRSValues {
my ( $Self, %Param ) = @_;
my $schema = $Param{Schema};
my $options = $Param{Options};
my ( $serviceNames, $serviceIDs ) = $Self->GetOTRSServicesData();
$schema =~ s/OTRS_service_values/$serviceIDs/g;
$options =~ s/OTRS_service_labels/$serviceNames/g;
my ( $typeNames, $typeIDs ) = $Self->GetOTRSTypesData(UserID => $Param{UserID});
$schema =~ s/OTRS_type_values/$typeIDs/g;
$options =~ s/OTRS_type_labels/$typeNames/g;
return ( $schema, $options );
}
=head
Get OTRS services info (service and type)
=cut
sub GetOTRSServicesData {
my ( $Self, %Param ) = @_;
my $services = $Kernel::OM->Get("Kernel::System::Service");
my %serviceList = $services->ServiceList( Valid => 1, UserID => $Kernel::OM->Get("Kernel::Config")->Get('CustomerPanelUserID') );
my $serviceNames = "";
my $serviceIDs = "";
foreach ( sort { ( $serviceList{$a} cmp $serviceList{$b} ) } keys %serviceList ) {
$serviceNames .= "\"$serviceList{$_}\",";
$serviceIDs .= "$_,";
}
$serviceNames = substr($serviceNames, 0, -1);
$serviceIDs = substr($serviceIDs, 0, -1);
return ( $serviceNames, $serviceIDs );
}
=head
Get OTRS ticket types
=cut
sub GetOTRSTypesData {
my ( $Self, %Param ) = @_;
my $ticketObject = $Kernel::OM->Get("Kernel::System::Ticket");
my %typeList = $ticketObject->TicketTypeList( UserID => $Param{UserID} );
my $typeNames = "";
my $typeIDs = "";
foreach ( sort { ( $typeList{$a} cmp $typeList{$b} ) } keys %typeList ) {
if (index($typeList{$_}, '::') == -1) {
$typeNames .= "\"$typeList{$_}\",";
$typeIDs .= "$_,";
}
}
$typeNames = substr($typeNames, 0, -1);
$typeIDs = substr($typeIDs, 0, -1);
return ( $typeNames, $typeIDs );
}
=head
Replaces OTRS dynamic fields
=cut
sub ReplaceOTRSDynamicFields {
my ( $Self, %Param ) = @_;
my $schema = $Param{Schema};
my $options = $Param{Options};
my $dfields = $Kernel::OM->Get("Kernel::System::DynamicField");
my @dfieldList = @{ $dfields->DynamicFieldListGet( Valid => 1 ) };
foreach (@dfieldList) {
my $campo = $_;
my $name = $campo->{Name};
my $config = $campo->{Config};
my $type = $campo->{FieldType};
my $idFieldValues = "DF_" . $name . "_values";
my $idFieldLabels = "DF_" . $name . "_labels";
if ( $type eq "Dropdown" ) {
my ( $dfValues, $dfLabels ) = $Self->GetDynamicFieldValues( ConfigDF => $config );
$schema =~ s/$idFieldValues/$dfValues/g;
$options =~ s/$idFieldLabels/$dfLabels/g;
}
}
return ( $schema, $options );
}
=head
Get OTRS dyanmic field values
=cut
sub GetDynamicFieldValues {
my ( $Self, %Param ) = @_;
my $config = $Param{ConfigDF};
my $values = $config->{PossibleValues};
my $dfValues = "";
my $dfLabels = "";
foreach ( sort { ( $values->{$a} cmp $values->{$b} ) } keys %{$values} ) {
$dfValues .= "\"$_\",";
$dfLabels .= "\"$values->{$_}\",";
}
$dfValues = substr($dfValues, 0, -1);
$dfLabels = substr($dfLabels, 0, -1);
return ( $dfValues, $dfLabels );
}
=head
Builds service choosing combo
=cut
sub BuildServices {
my ( $Self, %Param ) = @_;
my %Data = ();
my $ParamObject = $Kernel::OM->Get("Kernel::System::Web::Request");
my $QueueServiceObject = $Kernel::OM->Get("Kernel::System::QueueService");
my $ConfigObject = $Kernel::OM->Get("Kernel::Config");
my $ServiceObject = $Kernel::OM->Get("Kernel::System::Service");
my $RestrictedObject = $Kernel::OM->Get("Kernel::System::RestrictedService");
my $Public = $Param{Public};
# Build service chooser
my %Services = ();
if ( !$ParamObject->GetParam( Param => "QueueID" ) ) {
%Services = $ServiceObject->ServiceList( UserID => $ConfigObject->Get('CustomerPanelUserID'), );
}
else {
%Services =
$QueueServiceObject->GetServiceList( QueueID => $ParamObject->GetParam( Param => "QueueID" ), );
}
my @ServicesCombo = ();
my @restrictedServices = $RestrictedObject->GetRestrictedServices();
for my $serviceID ( keys %Services ) {
if ( grep { index( $Services{$_}, $Services{$serviceID} . "::" ) >= 0 } ( keys %Services ) ) {
if ((! $Public) || (! ( $serviceID ~~ @restrictedServices ))) {
my %serv = ();
$serv{Value} = $Services{$serviceID};
$serv{Key} = $serviceID;
$serv{Disabled} = 1;
push @ServicesCombo, \%serv;
}
}
else {
if ((! $Public) || (! ( $serviceID ~~ @restrictedServices ))) {
my %serv = ();
$serv{Value} = $Services{$serviceID};
$serv{Key} = $serviceID;
push @ServicesCombo, \%serv;
}
}
}
@ServicesCombo = sort { $a->{Value} . "::" cmp $b->{Value} . "::" } @ServicesCombo;
my $retorno = "";
my $servicePreDefined = $ParamObject->GetParam( Param => "ServiceID" );
$retorno = "<select data-width=\"auto\" id=\"ServiceID\" data-live-search-normalize=\"true\" class=\"selectpicker\" data-live-search=\"true\">\n<option value=\"\">-</option>";
if ($servicePreDefined) {
$Self->Debug("Service specified: $servicePreDefined");
} else {
$Self->Debug("Service not specified");
}
my $inOpt = 0;
for my $service (@ServicesCombo) {
if ($service->{Disabled}) {
if ($inOpt) {
$retorno = $retorno . "</optgroup>\n";
}
$inOpt = 1;
$retorno = $retorno . "<optgroup label='" . $service->{Value} . "'>\n";
} else {
if ($servicePreDefined && $servicePreDefined eq $service->{Key}) {
$retorno = $retorno . "<option selected value='" . $service->{Key} . "'>" . $Self->LastPart($service->{Value}) . "</option>\n";
} else {
$retorno = $retorno . "<option value='" . $service->{Key} . "'>" . $Self->LastPart($service->{Value}) . "</option>\n";
}
}
}
if ($inOpt) {
$retorno = $retorno . "</optgroup>\n";
}
$retorno = $retorno . "</select>\n";
return $retorno;
}
=head
Returns service name without the parent services names
=cut
sub LastPart {
my $Self = shift;
my $queue = shift;
my @parts = split( "::", $queue );
return $parts[-1];
}
=head
Handles jQueryFileUpload component in AlpacaJS
=cut
sub UploadFile {
my ( $Self, %Param ) = @_;
my $ParamObject = $Kernel::OM->Get("Kernel::System::Web::Request");
if ($ParamObject->GetParam(Param => "UploadFileCmd") eq "UploadFile") {
$Self->Debug("Upload file command received");
return $Self->UploadFileReceive();
} elsif ($ParamObject->GetParam(Param => "UploadFileCmd") eq "DeleteFile") {
$Self->Debug("Delete uploaded file command received");
return $Self->UploadFileDelete();
} else {
$Self->Debug("Unknown file command received");
return 1;
}
}
=head
Handles jQueryFileUpload component in AlpacaJS
=cut
sub UploadFileDelete() {
my ( $Self, %Param ) = @_;
my %Data = ();
my $ParamObject = $Kernel::OM->Get("Kernel::System::Web::Request");
my $WebUploadCacheObject = $Kernel::OM->Get('Kernel::System::Web::UploadCache');
my $LayoutObject = $Kernel::OM->Get("Kernel::Output::HTML::Layout");
my $fileName = $ParamObject->GetParam(Param => "Filename");
my $formId = $ParamObject->GetParam(Param => "FormID");
$Self->Debug("FileUpload: Delete file $fileName from form $formId");
my @filesUploaded = $WebUploadCacheObject->FormIDGetAllFilesData(FormID => $formId);
my $fileDeleted = 0;
for my $uploadedFile (@filesUploaded) {
if ($uploadedFile->{Filename} eq $fileName) {
$WebUploadCacheObject->FormIDRemoveFile(FormID => $formId, FileID => $uploadedFile->{FileID});
$Self->Debug("FileUpload: Deleted file " . $fileName);
$fileDeleted = 1;
}
}
if ($fileDeleted) {
return $LayoutObject->Attachment(
ContentType => 'application/json; charset=' . $LayoutObject->{Charset},
Content => "{ $fileName: true}",
Type => 'inline',
NoCache => 1,
);
} else {
return 1;
}
}
=head
Deletes all attachments from the form
=cut
sub ClearCache() {
my ( $Self, %Param ) = @_;
my %Data = ();
my $ParamObject = $Kernel::OM->Get("Kernel::System::Web::Request");
my $WebUploadCacheObject = $Kernel::OM->Get('Kernel::System::Web::UploadCache');
my $LayoutObject = $Kernel::OM->Get("Kernel::Output::HTML::Layout");
my $formId = $Param{FormID};
$Self->Debug("Clearing cache for form " . $formId);
my @filesUploaded = $WebUploadCacheObject->FormIDGetAllFilesData(FormID => $formId);
my $fileDeleted = 0;
for my $uploadedFile (@filesUploaded) {
$WebUploadCacheObject->FormIDRemoveFile(FormID => $formId, FileID => 1);
$Self->Debug("FileUpload: Deleted file " . $uploadedFile->{Filename});
}
}
=head
Handles jQueryFileUPload component in AlpacaJS
=cut
sub UploadFileReceive() {
my ( $Self, %Param ) = @_;
my %Data = ();
my $ParamObject = $Kernel::OM->Get("Kernel::System::Web::Request");
my $WebUploadCacheObject = $Kernel::OM->Get('Kernel::System::Web::UploadCache');
my $LayoutObject = $Kernel::OM->Get("Kernel::Output::HTML::Layout");
my $LanguageObject = $Kernel::OM->Get("Kernel::Language");
my %tmpFile = $ParamObject->GetUploadAll(Param => "attachment_files");
my $formId = $ParamObject->GetParam( Param => "FormID" );
if (! $tmpFile{Filename} || ! $formId) {
my $msgError = $LanguageObject->Translate("Invalid file!");
return $LayoutObject->Attachment(
ContentType => 'application/json; charset=' . $LayoutObject->{Charset},
Content => "{ \"error\": \"$msgError\", \"files\": [] }",
Type => 'inline',
NoCache => 1,
);
}
my $clearName = $tmpFile{Filename};
$clearName =~ tr/0-9a-zA-Z\.\-/\_/c;
$tmpFile{Filename} = $clearName;
$Self->Debug("FileUpload: Adding file to cache. FormID: " . $formId . " \t Filename: " . $tmpFile{Filename} . " \t ContentType: " . $tmpFile{ContentType} );
$WebUploadCacheObject->FormIDAddFile(
FormID => $formId,
Filename => $tmpFile{Filename},
Content => $tmpFile{Content},
ContentType => $tmpFile{ContentType}
);
my %retorno = ();
my @filesReturned = ();
my %dados = ();
$dados{name} = $tmpFile{Filename};
$dados{size} = length($tmpFile{Content});
$dados{deleteUrl} = "/otrs/public.pl?Action=NewTicketWizardPublic;UploadFileCmd=DeleteFile;Filename=" . uri_escape($tmpFile{Filename}) . ";FormID=" . $formId;
push @filesReturned, \%dados;
$retorno{"files"} = \@filesReturned;
return $LayoutObject->Attachment(
ContentType => 'application/json; charset=' . $LayoutObject->{Charset},
Content => encode_json(\%retorno),
Type => 'inline',
NoCache => 1,
);
}
=head
Returns the custom script defined for a service,
if it exists or just a comment-blank line if it doesn't
=cut
sub GetFormCustomProps {
my ( $Self, %Param ) = @_;
my %serviceForm;
my $ServiceFormObject = $Kernel::OM->Get("Kernel::System::ServiceForm");
my $LayoutObject = $Kernel::OM->Get("Kernel::Output::HTML::Layout");
my $ParamObject = $Kernel::OM->Get("Kernel::System::Web::Request");
my $QueueID = $ParamObject->GetParam( Param => "QueueID" );
if ( $Param{ServiceID} ) {
%serviceForm = $ServiceFormObject->GetServiceFormForQueue( ServiceID => $Param{ServiceID}, QueueID => $QueueID );
if (! $serviceForm{ServiceID}) {
%serviceForm = $ServiceFormObject->GetServiceForm( ServiceID => $Param{ServiceID} );
}
}
return $LayoutObject->Attachment(
ContentType => 'text/plain; charset=' . $LayoutObject->{Charset},
Content => $serviceForm{CustomProps} || "/**/",
Type => 'inline',
NoCache => 1,
);
}
=head
Returns the JSON form structure for a given service, if one is defined
=cut
sub GetFormJSON {
my ( $Self, %Param ) = @_;
my %serviceForm;
my $ServiceFormObject = $Kernel::OM->Get("Kernel::System::ServiceForm");
my $LayoutObject = $Kernel::OM->Get("Kernel::Output::HTML::Layout");
my $ParamObject = $Kernel::OM->Get("Kernel::System::Web::Request");
my $QueueID = $ParamObject->GetParam(Param => "QueueID");
my $FormID = $ParamObject->GetParam(Param => "FormID");
if ( $Param{ServiceID} ) {
%serviceForm = $ServiceFormObject->GetServiceFormForQueue( ServiceID => $Param{ServiceID}, QueueID => $QueueID );
if (! $serviceForm{ServiceID}) {
%serviceForm = $ServiceFormObject->GetServiceForm( ServiceID => $Param{ServiceID} );
}
}
$Self->ClearCache(FormID => $FormID);
my ( $schema, $fields, $introduction ) = $Self->GetForm( Public => $Param{Public}, ServiceForm => \%serviceForm, QueueID => $QueueID );
return $LayoutObject->Attachment(
ContentType => 'application/json; charset=' . $LayoutObject->{Charset},
Content => "[{" . $schema . "}, {" . $fields . "}, \"" . $introduction . "\"]",
Type => 'inline',
NoCache => 1,
);
}
=head
Allows using a field in the form to select a target queue.
If a queue was defined in request parameter, uses it.
If there is a prefix defined for the queues to automatically forward tickets,
and also a field was defined as handling the target queue, try to locate one.
If none is found, uses the default queue.
For example:
QueueDefault = Primeiro Nivel
QueuePrefix = Primeiro Nivel
QueueField = Unidade
AvailableQueues = Primeiro Nivel CCB, Primeiro Nivel CCE, Primeiro Nivel
QueueDefault = Primeiro Nivel
if QueueField value is CCB, then selects queue Primeiro Nivel CCB
if QueueField value is CCJ (no queue by that suffix), then selects queue Primeiro Nivel
if QueueField value is CCE, then selects queue Primeiro Nivel CCE
=cut
sub GetQueueID() {
my ( $Self, %Param ) = @_;
my $ParamObject = $Kernel::OM->Get("Kernel::System::Web::Request");
my $QueueObject = $Kernel::OM->Get("Kernel::System::Queue");
my $ConfigObject = $Kernel::OM->Get("Kernel::Config");
# Check if queueID was sent
my $QueueID = $ParamObject->GetParam(Param => "QueueID");
if ($QueueID) {
$Self->Debug("Sent queue: $QueueID");
my %Queue = $QueueObject->QueueGet(ID => $QueueID);
return ($Queue{"Name"}, $QueueID);
}
# Gets default Queue
my $ConfigTicket = $ConfigObject->Get("Ticket::Frontend::CustomerTicketMessage");
my $ConfigTicketWizard = $ConfigObject->Get("Ticket::Frontend::Customer::NewTicketWizard");
my $QueueDefault = $ConfigTicket->{"QueueDefault"};
# Checks if there is a field for the queue
my $QueuePrefix = $ConfigTicketWizard->{"QueuePrefix"};
my $QueueField = $ConfigTicketWizard->{"QueueField"};
my %QueueListID = $QueueObject->QueueList( Valid => 1 );
my %QueueList = reverse $QueueObject->QueueList( Valid => 1 );
$QueueID = $QueueList{$QueueDefault};
my $Queue = $QueueListID{$QueueID};
if ( $QueuePrefix && $QueueField ) {
my $QueueSelected = $ParamObject->GetParam( Param => $QueueField );
my $QueueName = "$QueuePrefix $QueueSelected";
if ( $QueueList{$QueueName} ) {
$QueueID = $QueueList{$QueueName};
$Queue = $QueueName;
}
}
return ( $Queue, $QueueID );
}
=head
Returns the form code for a given service, if one is defined
=cut
sub GetForm {
my ( $Self, %Param ) = @_;
my $TicketWizard = $Kernel::OM->Get("Kernel::System::TicketWizard");
my $ConfigObject = $Kernel::OM->Get("Kernel::Config");
my $schema;
my $fields;
$Self->{ConfigNTW} = $ConfigObject->Get("Ticket::Frontend::Customer::NewTicketWizard");
$Self->{DefaultUserID} = $Self->{ConfigNTW}->{"DefaultUserID"};
$Self->{DefaultCustomerID} = $Self->{ConfigNTW}->{"DefaultCustomerID"};
$Self->{DefaultUserDomain} = $Self->{ConfigNTW}->{"DefaultUserDomain"};
if ($Param{Public}) {
$Self->Debug("Getting public form schema");
$schema = $TicketWizard->GetBasicFieldsSchemaPublic();
$fields = $TicketWizard->GetBasicFieldsOptionsPublic();
} else {
$Self->Debug("Getting authenticated form schema");
$schema = $TicketWizard->GetBasicFieldsSchema();
$fields = $TicketWizard->GetBasicFieldsOptions();
}
my $introduction;
( $schema, $fields ) = $TicketWizard->ReplaceOTRSValues(
Schema => $schema,
Options => $fields,
UserID => $Self->{DefaultUserID}
);
if ( !$Param{ServiceForm} ) {
$schema =~ s/CF_SCHEMA//g;
$fields =~ s/CF_FORM//g;
$introduction = "";
# Include dynamic fields in replace command
( $schema, $fields ) = $TicketWizard->ReplaceOTRSDynamicFields( Schema => $schema, Options => $fields );
}
else {
my %serviceForm = %{ $Param{ServiceForm} };
$introduction = $serviceForm{Introduction};
if ( !$Param{ServiceForm}{Schema} ) {
$schema =~ s/CF_SCHEMA//g;
$fields =~ s/CF_FORM//g;
}
else {
my $schemaForm = "," . $serviceForm{Schema};
my $fieldsForm = "," . $serviceForm{Form};
$schema =~ s/CF_SCHEMA/$schemaForm/g;
$fields =~ s/CF_FORM/$fieldsForm/g;
}
# Include dynamic fields in replace command
( $schema, $fields ) = $TicketWizard->ReplaceOTRSDynamicFields( Schema => $schema, Options => $fields );
# Set fixed field values, if defined
if ($serviceForm{FixedValues}) {
($schema, $fields) = $Self->AdjustFixedFields(Schema => $schema, Fields => $fields, FixedValues => $serviceForm{FixedValues});
}
}
if ($Param{"QueueID"}) {
$schema = $schema . ",\n\"QueueID\": {\n\"type\": \"string\",\n\"default\": \"" . $Param{"QueueID"} . "\"}";
$fields = $fields . ",\n\"QueueID\": {\n\"type\": \"hidden\"\n}";
}
return ( $schema, $fields, $introduction );
}
=head
If some fields have fixed values in service definition, sets them
=cut
sub AdjustFixedFields {
my ( $Self, %Param ) = @_;
my $schema = $Param{Schema};
my $form = $Param{Fields};
my $fixedValues = $Param{FixedValues};
my @values = split(/\n/, $fixedValues);
for my $value (@values) {
$value =~ s/\r//g;
my @data = split('=', $value);
my $fieldKey = $data[0];
my $fixedValue = $data[1];
$Self->Debug("Adjusting fixed field { $fieldKey } with value [ $fixedValue ]");
my $key = '("' . $fieldKey . '"[\ ]?:[\ ]?{[^}]+})';
my $replace = '"' . $fieldKey . '"' . ': {"type": "string", "default": "' . $fixedValue . '"}';
$Self->Debug("Adjusting fixed fields in schema: \n " . $schema);
$schema =~ s/$key/$replace/;
$Self->Debug("Adjusting fixed fields in form: \n " . $form);
$form = "{" . $form . "}";
$form = encode_utf8($form);
my $formStruct = decode_json($form);
my $hiddenStruct = {};
$hiddenStruct->{"type"} = "hidden";
$formStruct->{$fieldKey} = $hiddenStruct;
print STDERR Dumper($formStruct);
$form = encode_json($formStruct);
$form = decode_utf8($form);
substr($form, 0, 1, "");
substr($form, -1, 1, "");
}
return ($schema, $form);
}
=head
Creates a new ticket, given the input data and sent attachments/images
=cut
sub CreateTicket {
my ( $Self, %Param ) = @_;
my %Data = ();
my $TicketObject = $Kernel::OM->Get("Kernel::System::Ticket");
my $ConfigObject = $Kernel::OM->Get("Kernel::Config");
my $ParamObject = $Kernel::OM->Get("Kernel::System::Web::Request");
my $BackendObject = $Kernel::OM->Get("Kernel::System::DynamicField::Backend");
my $DynamicFieldObject = $Kernel::OM->Get("Kernel::System::DynamicField");
my $CustomerUserObject = $Kernel::OM->Get("Kernel::System::CustomerUser");
my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
my $TicketWizard = $Kernel::OM->Get("Kernel::System::TicketWizard");
my $LanguageObject = $Kernel::OM->Get("Kernel::Language");
$Self->{ConfigNTW} = $ConfigObject->Get("Ticket::Frontend::Customer::NewTicketWizard");
$Self->{DefaultUserID} = $Self->{ConfigNTW}->{"DefaultUserID"};
$Self->{DefaultCustomerID} = $Self->{ConfigNTW}->{"DefaultCustomerID"};
$Self->{DefaultUserDomain} = $Self->{ConfigNTW}->{"DefaultUserDomain"};
# Check if queue was pre-defined
my $queueFixed = 0;
if ($Data{QueueID}) {
$queueFixed = 1;
}
# Get queue name and id using rules, if no queue was predefined
my ( $Queue, $QueueID ) = $TicketWizard->GetQueueID(%Param);
$Data{QueueID} = $QueueID;
$LayoutObject->AddJSData(
Key => 'NewTicketWizard.QueueID',
Value => $Data{QueueID},
);
# Module config
$Self->{Config} = $ConfigObject->Get("Ticket::Frontend::CustomerTicketMessage");
# Ticket creation
my $customerID;
my $customerUser;
if ($Param{Public}) {
$customerID = $Self->GetUserIDFromEmail( $ParamObject->GetParam( Param => "email" ) );
$customerUser = $Self->GetUserIDFromEmail( $ParamObject->GetParam( Param => "email" ) );
} else {
$customerID = $Param{CustomerID};
$customerUser = $Param{CustomerUser};
}
$Self->Debug("Creating ticket");
my $TicketID = $TicketObject->TicketCreate(
Title => $ParamObject->GetParam( Param => "subject" ),
QueueID => $QueueID,
Priority => $Self->{Config}->{PriorityDefault},
Lock => 'unlock',
State => 'new',
ServiceID => $ParamObject->GetParam( Param => "service" ),
TypeID =>$ParamObject->GetParam( Param => "type" ),
CustomerID => $customerID,
CustomerUser => $customerUser,
OwnerID => $Self->{DefaultUserID},
UserID => $Self->{DefaultUserID},
);
$Self->Debug("Created ticket ID $TicketID");
my $MimeType = 'text/html';
my %serviceFieldsValues = ();
my $serviceFields = "";
my @serviceFieldsKeys = ();
# Service Fields
$Self->Debug("Retrieving service form values");
for ( $ParamObject->GetParamNames() ) {
if (( substr( $_, 0, 3 ) eq "SF_" ) && ( substr( $_, 0, 5 ) ne "SF_T_" )) {
if ($ParamObject->GetArray( Param => $_ )) {
my @paramVals = $ParamObject->GetArray( Param => $_ );
for my $paramValue (@paramVals) {
if (($paramValue) && (!($paramValue eq "-"))) {
if (! $serviceFieldsValues{$_}) {
$serviceFieldsValues{$_} = $paramValue;
push @serviceFieldsKeys, $_;
} else {
$serviceFieldsValues{$_} .= ", " . $paramValue;
}
}
}
} else {
my $paramValue = $ParamObject->GetParam( Param => $_ );
if (($paramValue) && (!($paramValue eq "-"))) {
if (! $serviceFieldsValues{$_}) {
$serviceFieldsValues{$_} = $paramValue;
push @serviceFieldsKeys, $_;
} else {
$serviceFieldsValues{$_} .= ", " . $paramValue;
}
}
}
}
}
for (@serviceFieldsKeys) {
$serviceFields .= "<B>" . $Self->breakWords(Text => substr( $_, 3 )) . ": </B>" . $serviceFieldsValues{$_} . "<BR/>";
}
if (! $Param{Public}) {
$serviceFields .= "<B>" . $LanguageObject->Translate("Authenticated user") . ": </B>" . $customerUser . "<BR/>";
}
$serviceFields .= "<B>IP: </B>" . $ENV{'REMOTE_ADDR'} . "<BR/>";
$serviceFields .= "<BR/><BR/>";
# Dynamic Fields
my $userIDDF;
if ($Param{Public}) {
$userIDDF = $Self->{DefaultUserID};
} else {
$userIDDF = $ConfigObject->Get('CustomerPanelUserID');
}
$Self->Debug("Retrieving dynamic fields values");
for ( $ParamObject->GetParamNames() ) {
if ( substr( $_, 0, 3 ) eq "DF_" ) {
my $Success = $BackendObject->ValueSet(
DynamicFieldConfig => $DynamicFieldObject->DynamicFieldGet( Name => substr( $_, 3 ) ),
ObjectID => $TicketID,
Value => $ParamObject->GetParam( Param => $_ ),
UserID => $userIDDF,
);
}
}
# Service Fields - Table fields
$Self->Debug("Retrieving service fields - table fields");
my $lastTab = "";
my %keysFound = ();
my $newTab = 1;
for ( $ParamObject->GetParamNames() ) {
if ( substr( $_, 0, 5 ) eq "SF_T_" ) {
my $paramName = $_;
my @parts = split('_', $_);
my $currTab = $parts[2];
my $currKey = $parts[4];
if ($currTab ne $lastTab) {
if ($lastTab ne "") {
$serviceFields .= "</TABLE><BR/><BR/>";
}
$serviceFields .= "<TABLE style='border-collapse: collapse; width: 700px;' border='1px'>";
$lastTab = $currTab;
$newTab = 1;
} else {
$newTab = 0;
}
## new line due to repetition or new table
if ($keysFound{$currKey} || ($newTab) ) {
%keysFound = ();
$serviceFields .= "<TR><TD COLSPAN='2' style='text-align: center; font-size: large'><B>" . $Self->breakWords(Text => $currTab) . "</B></TD></TR>";
}
$keysFound{$currKey} = 1;
$serviceFields .= "<TR><TD style='width: 100px;'><B>" . $Self->breakWords(Text => $currKey) . "</B></TD><TD>" . $ParamObject->GetParam( Param => $paramName ) . "</TD></TR>";
}
}
if ($lastTab ne "") {
$serviceFields .= "</TABLE><BR/><BR/>";
}
my $body = $ParamObject->GetParam( Param => "description" );
# Images
$Self->Debug("Loading images from cache");
my $WebUploadCacheObject = $Kernel::OM->Get('Kernel::System::Web::UploadCache');
my $formId = $ParamObject->GetParam(Param => "FormID");
my @filesUploaded = $WebUploadCacheObject->FormIDGetAllFilesData(FormID => $formId);
for my $uploadedFile (@filesUploaded) {
my $ContentID = $uploadedFile->{ContentID};
if ($ContentID && ( $uploadedFile->{ContentType} =~ /image/i ) && ( $uploadedFile->{Disposition} eq 'inline' )) {
$Self->Debug("NewTicketWizard: replacing image URL in body with attachment");
$Self->Debug("NewTicketWizard: Body: $body");
my $ContentIDHTMLQuote = $LayoutObject->Ascii2Html(Text => $ContentID,);
# workaround for link encode of rich text editor, see bug#5053
my $ContentIDLinkEncode = $LayoutObject->LinkEncode($ContentID);
$body =~ s/(ContentID=)$ContentIDLinkEncode/$1$ContentID/g;
$Self->Debug("NewTicketWizard: new body: $body");
}
}
# Create article
$Self->Debug("Creating article");
my $FullName;
my $Email;
if ($Param{Public}) {
$FullName = $ParamObject->GetParam( Param => "name" );
$Email = $ParamObject->GetParam( Param => "email" );
} else {
$FullName = $CustomerUserObject->CustomerName( UserLogin => $Param{UserLogin});
$Email = $Param{UserEmail};
}
my $From = "\"$FullName\" <$Email>";
my $ArticleBackendObject = $Kernel::OM->Get('Kernel::System::Ticket::Article')->BackendForChannel(ChannelName => 'Email');
my $ArticleID = $ArticleBackendObject->ArticleCreate(
TicketID => $TicketID,
IsVisibleForCustomer => 1,
ArticleType => $Self->{Config}->{ArticleType},
SenderType => $Self->{Config}->{SenderType},
From => $From,
To => $Queue,
Subject => $ParamObject->GetParam( Param => "subject" ),
Body => $LayoutObject->RichTextDocumentComplete(String => $serviceFields . $body),
MimeType => $MimeType,
Charset => $LayoutObject->{UserCharset},
UserID => $userIDDF,
HistoryType => $Self->{Config}->{HistoryType},
HistoryComment => $Self->{Config}->{HistoryComment} || '%%',
AutoResponseType => ( $ConfigObject->Get('AutoResponseForWebTickets') )
? 'auto reply'
: '',
OrigHeader => {
From => $From,
To => $Queue,
Subject => $ParamObject->GetParam( Param => "subject" ),
Body => $LayoutObject->RichTextDocumentComplete(String => $serviceFields . $body),,
},
Queue => $Queue,
);
# Attachments
$Self->Debug("Including attachments");
for my $uploadedFile (@filesUploaded) {
$ArticleBackendObject->ArticleWriteAttachment(
Filename => $uploadedFile->{Filename},
Content => $uploadedFile->{Content},
ContentType => $uploadedFile->{ContentType},
ArticleID => $ArticleID,
ContentID => $uploadedFile->{ContentID},
UserID => $userIDDF,
);
}
$Data{TicketNumber} = $TicketObject->TicketNumberLookup( TicketID => $TicketID );
$Self->{ConfigNTW} = $ConfigObject->Get("Ticket::Frontend::Customer::NewTicketWizard");
my $idCustomTemplate = "NTW" . $Self->{ConfigNTW}->{"CustomTemplate"};
# build output
if ($Param{Public}) {
$Data{PublicMode} = "Public";
$Data{Module} = "public";
} else {
$Data{Module} = "customer";
}
if (! $queueFixed) {
delete $Data{QueueID};
}
my $Output = $LayoutObject->CustomerHeader(Type => $idCustomTemplate, Title => "Ticket created" );
$Output .= $LayoutObject->Output(
Data => \%Data,
TemplateFile => 'NewTicketWizardTicketCreated',
);
$Output .= $LayoutObject->CustomerFooter(Type => $idCustomTemplate);
return $Output;
}
=head
If public interface was used, try to guess the user from the informed e-mail
=cut
sub GetUserIDFromEmail {
my $Self = shift;
my $email = shift;
my $result = $Self->{DefaultCustomerID};
my $domainList = $Self->{DefaultUserDomain};
my $CustomerUserObject = $Kernel::OM->Get("Kernel::System::CustomerUser");
if ($domainList) {
my @domains = split( ",", $domainList );
for my $domain (@domains) {
my @parts = split( "@", $email );
if ( $parts[1] eq $domain ) {
my $id = $parts[0];
my %List = $CustomerUserObject->CustomerSearch(
UserLogin => $id,
Valid => 1, # not required, default 1
);
if ( keys %List ) {
$result = $id;
return $result;
}
}
}
}
return $result;
}
=head
Default action to open a new form (frontend method share by public and customer frontend modules)
=cut
sub OpenForm {
my ( $Self, %Param ) = @_;
my %Data = ();
if ($Param{Public}) {
$Self->Debug("Opening public form");
} else {
$Self->Debug("Opening authenticated form");
}
my $LayoutObject = $Kernel::OM->Get('Kernel::Output::HTML::Layout');
my $TicketObject = $Kernel::OM->Get("Kernel::System::Ticket");
my $ConfigObject = $Kernel::OM->Get("Kernel::Config");
my $ParamObject = $Kernel::OM->Get("Kernel::System::Web::Request");
my $BackendObject = $Kernel::OM->Get("Kernel::System::DynamicField::Backend");
my $DynamicFieldObject = $Kernel::OM->Get("Kernel::System::DynamicField");
my $CustomerUserObject = $Kernel::OM->Get("Kernel::System::CustomerUser");
my $TicketWizard = $Kernel::OM->Get("Kernel::System::TicketWizard");
my $QueueObject = $Kernel::OM->Get("Kernel::System::Queue");
my $QueueServiceObject = $Kernel::OM->Get("Kernel::System::QueueService");
my $ServiceObject = $Kernel::OM->Get("Kernel::System::Service");
my $MaintenanceObject = $Kernel::OM->Get("Kernel::System::Maintenance");
my $DateUtilsObject = $Kernel::OM->Get("Kernel::System::DateUtils");
my $LanguageObject = $Kernel::OM->Get("Kernel::Language");
if ($Param{Public}) {
$Data{AuthenticateURL} = $ConfigObject->Get('Customer::AuthModule::CAS::CASUrl')
. "/login?service="
. "https://"
. $ENV{SERVER_NAME}
. $ENV{SCRIPT_NAME}
. "?Action=NewTicketWizard";
}
# Check if queue parameter was received
if ($ParamObject->GetParam(Param => "QueueID")) {
$Data{QueueID} = $ParamObject->GetParam(Param => "QueueID");
$LayoutObject->AddJSData(
Key => 'NewTicketWizard.QueueID',
Value => $Data{QueueID},
);
$Self->Debug("Specific queue defined => " . $Data{QueueID});
} else {
# Check if must show queue - if so, redirect user
my $_ConfigTicketWizard = $ConfigObject->Get("Ticket::Frontend::Customer::NewTicketWizard");
my $_ShowQueue = $_ConfigTicketWizard->{"ShowQueue"};
if (! $_ShowQueue) {
$Self->Debug("Queue selection must be shown - redirection to QueuePanelPublic action");
return $LayoutObject->Redirect( OP => "Action=QueuesPanelPublic" );
}
}
my ( $schema, $fields ) = $TicketWizard->GetForm(Public => $Param{Public});
$Data{SchemaForm} = $schema;
$Data{FieldsForm} = $fields;
# Common info
$Self->{ConfigNTW} = $ConfigObject->Get("Ticket::Frontend::Customer::NewTicketWizard");
my $idCustomTemplate = "NTW" . $Self->{ConfigNTW}->{"CustomTemplate"};
my $msgChooseService;
if ($Param{Public}) {
$msgChooseService = $Self->{ConfigNTW}->{"MessageChooseServicePublic"};
} else {
$msgChooseService = $Self->{ConfigNTW}->{"MessageChooseService"};
}
if ($ParamObject->GetParam( Param => "QueueID" ) ) {
my %queueData = $QueueObject->QueueGet(ID => $ParamObject->GetParam( Param => "QueueID" ));
my $queueName = $queueData{Name};
$msgChooseService =~ s/PLACE\_NAME/$queueName/g;
}
$Data{MsgChooseService} = $msgChooseService;
$Self->{DefaultUserID} = $Self->{ConfigNTW}->{"DefaultUserID"};
$Self->{DefaultCustomerID} = $Self->{ConfigNTW}->{"DefaultCustomerID"};
$Self->{DefaultUserDomain} = $Self->{ConfigNTW}->{"DefaultUserDomain"};
# Build service chooser
$Data{ServiceStrg} = $TicketWizard->BuildServices(ServiceID => $ParamObject->GetParam( Param => "ServiceID" ), Public => $Param{Public});
# Build output
my $Output = $LayoutObject->CustomerHeader(Type => $idCustomTemplate, Title => $LanguageObject->Translate("New ticket wizard") );
# Load current maintenances
my @maintenances = $MaintenanceObject->ListMaintenances(Type => "current");
if (@maintenances) {
$LayoutObject->Block(
Name => 'MaintenancesPanel',
);
for my $maintenanceHash (@maintenances) {
my %maintenance = %{$maintenanceHash};
$LayoutObject->Block(
Name => 'MaintenanceLine',
Data => {
Message => $maintenance{'Description'},
StartDate => $DateUtilsObject->BrazilianDate(Timestamp => $DateUtilsObject->FromSQL(StringDate => $maintenance{StartDate})),
});
}
}
if ($Param{Public}) {
$Self->Debug("Public view - unauthenticated message enabled");
$LayoutObject->Block(
Name => 'UnauthenticatedMessage',
);
$Self->Debug("Public view - public toolbar enabled");
$LayoutObject->Block(
Name => 'ToolBarPublic',
);
} else {
$Self->Debug("Customer view - customer toolbar enabled");
$LayoutObject->Block(
Name => 'ToolBarCustomer',
);
}
# Prepare cache for imagens and attachments
my $WebUploadCacheObject = $Kernel::OM->Get('Kernel::System::Web::UploadCache');
$Data{FormID} = $WebUploadCacheObject->FormIDCreate();
$LayoutObject->AddJSData(
Key => 'NewTicketWizard.FormID',
Value => $Data{FormID},
);
if ($Param{Public}) {
$Data{PublicMode} = "Public";
}
$Output .= $LayoutObject->Output(
Data => \%Data,
TemplateFile => 'NewTicketWizard',
);
$Output .= $LayoutObject->CustomerFooter(Type => $idCustomTemplate);
return $Output;
}
=head
Separate service-field label names, to make it human-readable (CapitalLikeLabels -> Capital like labels)
=cut
sub breakWords {
my ( $Self, %Param ) = @_;
my $return = $Param{Text};
$return =~ s/(.)([A-Z][^A-Z])/$1 $2/g;
$return =~ s/([\w']+)/\u\L$1/g;
return $return;
}
sub Debug {
my $Self = shift;
my $msg = shift;
$Kernel::OM->Get("Kernel::System::Log")->Log( Priority => 'debug', Message => $msg );
}
1;