1 package FS::TicketSystem;
4 use vars qw( $conf $system $AUTOLOAD );
6 use FS::UID qw( dbh driver_name );
8 FS::UID->install_callback( sub {
10 $system = $conf->config('ticket_system');
19 my $conf = new FS::Conf;
20 die "FS::TicketSystem::$AUTOLOAD called, but no ticket system configured\n"
23 eval "use FS::TicketSystem::$system;";
31 return if !defined($system) || $system ne 'RT_Internal';
32 my ($class, %opts) = @_;
34 # go ahead and use the RT API for this
36 FS::TicketSystem->init;
37 my $session = FS::TicketSystem->session();
38 # bypass RT ACLs--we're going to do lots of things
39 my $CurrentUser = $RT::SystemUser;
42 my $User = RT::User->new($CurrentUser);
43 $User->Load('%%%SELFSERVICE_USER%%%');
44 if (!defined($User->Id)) {
45 my ($val, $msg) = $User->Create(
46 'Name' => '%%%SELFSERVICE_USER%%%',
47 'Gecos' => '%%%SELFSERVICE_USER%%%',
49 # any other fields needed?
53 my $Principal = $User->PrincipalObj; # can this ever fail?
54 my @rights = ( qw(ShowTicket SeeQueue ModifyTicket ReplyToTicket
55 CreateTicket SeeCustomField) );
57 next if $Principal->HasRight( 'Right' => $_, Object => $RT::System );
58 my ($val, $msg) = $Principal->GrantRight(
60 'Object' => $RT::System,
65 # EscalateQueue custom field and friends
66 my $CF = RT::CustomField->new($CurrentUser);
67 $CF->Load('EscalateQueue');
68 if (!defined($CF->Id)) {
69 my ($val, $msg) = $CF->Create(
70 'Name' => 'EscalateQueue',
73 'LookupType' => 'RT::Queue',
74 'Description' => 'Escalate to Queue',
75 'ValuesClass' => 'RT::CustomFieldValues::Queues', #magic!
78 my $OCF = RT::ObjectCustomField->new($CurrentUser);
79 ($val, $msg) = $OCF->Create(
80 'CustomField' => $CF->Id,
86 # Load from RT data file
87 our (@Groups, @Users, @ACL, @Queues, @ScripActions, @ScripConditions,
88 @Templates, @CustomFields, @Scrips, @Attributes, @Initial, @Final);
89 my $datafile = '%%%RT_PATH%%%/etc/initialdata';
90 eval { require $datafile };
92 warn "Couldn't load RT data from '$datafile': $@\n(skipping)\n";
96 # Cache existing ScripCondition, ScripAction, and Template IDs
97 my $search = RT::ScripConditions->new($CurrentUser);
99 my %condition = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
101 $search = RT::ScripActions->new($CurrentUser);
103 my %action = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
105 $search = RT::Templates->new($CurrentUser);
107 my %template = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
110 my $ScripCondition = RT::ScripCondition->new($CurrentUser);
111 foreach my $sc (@ScripConditions) {
112 # $sc: Name, Description, ApplicableTransTypes, ExecModule, Argument
113 next if exists( $condition{ lc($sc->{Name}) } );
114 my ($val, $msg) = $ScripCondition->Create( %$sc );
116 $condition{ lc($ScripCondition->Name) } = $ScripCondition->Id;
120 my $ScripAction = RT::ScripAction->new($CurrentUser);
121 foreach my $sa (@ScripActions) {
122 # $sa: Name, Description, ExecModule, Argument
123 next if exists( $action{ lc($sa->{Name}) } );
124 my ($val, $msg) = $ScripAction->Create( %$sa );
126 $action{ lc($ScripAction->Name) } = $ScripAction->Id;
130 my $Template = RT::Template->new($CurrentUser);
131 foreach my $t (@Templates) {
132 # $t: Queue, Name, Description, Content
133 next if exists( $template{ lc($t->{Name}) } );
134 my ($val, $msg) = $Template->Create( %$t );
136 $template{ lc($Template->Name) } = $Template->Id;
140 my $Scrip = RT::Scrip->new($CurrentUser);
141 foreach my $s ( @Scrips ) {
142 my $desc = $s->{'Description'};
143 my ($c, $a, $t) = map lc,
144 @{ $s }{'ScripCondition', 'ScripAction', 'Template'};
145 if ( !$condition{$c} ) {
146 warn "ScripCondition '$c' not found.\n";
149 if ( !$action{$a} ) {
150 warn "ScripAction '$a' not found.\n";
153 if ( !$template{$t} ) {
154 warn "Template '$t' not found.\n";
158 ScripCondition => $condition{$c},
159 ScripAction => $action{$a},
160 Template => $template{$t},
163 $Scrip->LoadByCols(%param);
164 if (!defined($Scrip->Id)) {
165 my ($val, $msg) = $Scrip->Create(%param, Description => $desc);