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) );
56 next if $Principal->HasRight( 'Right' => $_, Object => $RT::System );
57 my ($val, $msg) = $Principal->GrantRight(
59 'Object' => $RT::System,
64 # EscalateQueue custom field and friends
65 my $CF = RT::CustomField->new($CurrentUser);
66 $CF->Load('EscalateQueue');
67 if (!defined($CF->Id)) {
68 my ($val, $msg) = $CF->Create(
69 'Name' => 'EscalateQueue',
72 'LookupType' => 'RT::Queue',
73 'Description' => 'Escalate to Queue',
74 'ValuesClass' => 'RT::CustomFieldValues::Queues', #magic!
77 my $OCF = RT::ObjectCustomField->new($CurrentUser);
78 ($val, $msg) = $OCF->Create(
79 'CustomField' => $CF->Id,
85 # Load from RT data file
86 our (@Groups, @Users, @ACL, @Queues, @ScripActions, @ScripConditions,
87 @Templates, @CustomFields, @Scrips, @Attributes, @Initial, @Final);
88 my $datafile = '%%%RT_PATH%%%/etc/initialdata';
89 eval { require $datafile };
91 warn "Couldn't load RT data from '$datafile': $@\n(skipping)\n";
95 # Cache existing ScripCondition, ScripAction, and Template IDs
96 my $search = RT::ScripConditions->new($CurrentUser);
98 my %condition = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
100 $search = RT::ScripActions->new($CurrentUser);
102 my %action = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
104 $search = RT::Templates->new($CurrentUser);
106 my %template = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
109 my $ScripCondition = RT::ScripCondition->new($CurrentUser);
110 foreach my $sc (@ScripConditions) {
111 # $sc: Name, Description, ApplicableTransTypes, ExecModule, Argument
112 next if exists( $condition{ lc($sc->{Name}) } );
113 my ($val, $msg) = $ScripCondition->Create( %$sc );
115 $condition{ lc($ScripCondition->Name) } = $ScripCondition->Id;
119 my $ScripAction = RT::ScripAction->new($CurrentUser);
120 foreach my $sa (@ScripActions) {
121 # $sa: Name, Description, ExecModule, Argument
122 next if exists( $action{ lc($sa->{Name}) } );
123 my ($val, $msg) = $ScripAction->Create( %$sa );
125 $action{ lc($ScripAction->Name) } = $ScripAction->Id;
129 my $Template = RT::Template->new($CurrentUser);
130 foreach my $t (@Templates) {
131 # $t: Queue, Name, Description, Content
132 next if exists( $template{ lc($t->{Name}) } );
133 my ($val, $msg) = $Template->Create( %$t );
135 $template{ lc($Template->Name) } = $Template->Id;
139 my $Scrip = RT::Scrip->new($CurrentUser);
140 foreach my $s ( @Scrips ) {
141 my $desc = $s->{'Description'};
142 my ($c, $a, $t) = map lc,
143 @{ $s }{'ScripCondition', 'ScripAction', 'Template'};
144 if ( !$condition{$c} ) {
145 warn "ScripCondition '$c' not found.\n";
148 if ( !$action{$a} ) {
149 warn "ScripAction '$a' not found.\n";
152 if ( !$template{$t} ) {
153 warn "Template '$t' not found.\n";
157 ScripCondition => $condition{$c},
158 ScripAction => $action{$a},
159 Template => $template{$t},
162 $Scrip->LoadByCols(%param);
163 if (!defined($Scrip->Id)) {
164 my ($val, $msg) = $Scrip->Create(%param, Description => $desc);