1 package FS::TicketSystem;
4 use vars qw( $conf $system $AUTOLOAD );
6 use FS::UID qw( dbh driver_name );
7 use FS::Record qw( dbdef );
9 FS::UID->install_callback( sub {
11 $system = $conf->config('ticket_system');
20 my $conf = new FS::Conf;
21 die "FS::TicketSystem::$AUTOLOAD called, but no ticket system configured\n"
24 eval "use FS::TicketSystem::$system;";
34 WillResolve => { type => 'timestamp', null => 1, default => '', },
37 Required => { type => 'integer', default => 0, null => 0 },
42 my $system = FS::Conf->new->config('ticket_system');
43 return if !defined($system) || $system ne 'RT_Internal';
44 my ($class, %opts) = @_;
48 my $case = driver_name eq 'mysql' ? sub {@_} : sub {map lc, @_};
49 foreach my $tablename (keys %columns) {
50 my $table = dbdef->table(&$case($tablename));
53 "$tablename table does not exist. Your RT installation is incomplete.\n";
56 foreach my $colname (keys %{ $columns{$tablename} }) {
57 if ( !$table->column(&$case($colname)) ) {
58 my $col = new DBIx::DBSchema::Column {
60 name => &$case($colname),
61 %{ $columns{$tablename}->{$colname} }
63 $col->table_obj($table);
64 push @sql, $col->sql_add_column($dbh);
70 warn "Upgrading RT schema:\n";
71 foreach my $statement (@sql) {
73 $dbh->do( $statement )
74 or die "Error: ". $dbh->errstr. "\n executing: $statement";
80 return if !defined($system) || $system ne 'RT_Internal';
81 my ($class, %opts) = @_;
83 # go ahead and use the RT API for this
85 FS::TicketSystem->init;
86 my $session = FS::TicketSystem->session();
87 # bypass RT ACLs--we're going to do lots of things
88 my $CurrentUser = $RT::SystemUser;
91 my $User = RT::User->new($CurrentUser);
92 $User->Load('%%%SELFSERVICE_USER%%%');
93 if (!defined($User->Id)) {
94 my ($val, $msg) = $User->Create(
95 'Name' => '%%%SELFSERVICE_USER%%%',
96 'Gecos' => '%%%SELFSERVICE_USER%%%',
98 # any other fields needed?
102 my $Principal = $User->PrincipalObj; # can this ever fail?
103 my @rights = ( qw(ShowTicket SeeQueue ModifyTicket ReplyToTicket
104 CreateTicket SeeCustomField) );
106 next if $Principal->HasRight( 'Right' => $_, Object => $RT::System );
107 my ($val, $msg) = $Principal->GrantRight(
109 'Object' => $RT::System,
114 # EscalateQueue custom field and friends
115 my $CF = RT::CustomField->new($CurrentUser);
116 $CF->Load('EscalateQueue');
117 if (!defined($CF->Id)) {
118 my ($val, $msg) = $CF->Create(
119 'Name' => 'EscalateQueue',
122 'LookupType' => 'RT::Queue',
123 'Description' => 'Escalate to Queue',
124 'ValuesClass' => 'RT::CustomFieldValues::Queues', #magic!
127 my $OCF = RT::ObjectCustomField->new($CurrentUser);
128 ($val, $msg) = $OCF->Create(
129 'CustomField' => $CF->Id,
135 # Load from RT data file
136 our (@Groups, @Users, @ACL, @Queues, @ScripActions, @ScripConditions,
137 @Templates, @CustomFields, @Scrips, @Attributes, @Initial, @Final);
138 my $datafile = '%%%RT_PATH%%%/etc/initialdata';
139 eval { require $datafile };
141 warn "Couldn't load RT data from '$datafile': $@\n(skipping)\n";
145 # Cache existing ScripCondition, ScripAction, and Template IDs
146 my $search = RT::ScripConditions->new($CurrentUser);
148 my %condition = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
150 $search = RT::ScripActions->new($CurrentUser);
152 my %action = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
154 $search = RT::Templates->new($CurrentUser);
156 my %template = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
159 my $ScripCondition = RT::ScripCondition->new($CurrentUser);
160 foreach my $sc (@ScripConditions) {
161 # $sc: Name, Description, ApplicableTransTypes, ExecModule, Argument
162 next if exists( $condition{ lc($sc->{Name}) } );
163 my ($val, $msg) = $ScripCondition->Create( %$sc );
165 $condition{ lc($ScripCondition->Name) } = $ScripCondition->Id;
169 my $ScripAction = RT::ScripAction->new($CurrentUser);
170 foreach my $sa (@ScripActions) {
171 # $sa: Name, Description, ExecModule, Argument
172 next if exists( $action{ lc($sa->{Name}) } );
173 my ($val, $msg) = $ScripAction->Create( %$sa );
175 $action{ lc($ScripAction->Name) } = $ScripAction->Id;
179 my $Template = RT::Template->new($CurrentUser);
180 foreach my $t (@Templates) {
181 # $t: Queue, Name, Description, Content
182 next if exists( $template{ lc($t->{Name}) } );
183 my ($val, $msg) = $Template->Create( %$t );
185 $template{ lc($Template->Name) } = $Template->Id;
189 my $Scrip = RT::Scrip->new($CurrentUser);
190 foreach my $s ( @Scrips ) {
191 my $desc = $s->{'Description'};
192 my ($c, $a, $t) = map lc,
193 @{ $s }{'ScripCondition', 'ScripAction', 'Template'};
194 if ( !$condition{$c} ) {
195 warn "ScripCondition '$c' not found.\n";
198 if ( !$action{$a} ) {
199 warn "ScripAction '$a' not found.\n";
202 if ( !$template{$t} ) {
203 warn "Template '$t' not found.\n";
207 ScripCondition => $condition{$c},
208 ScripAction => $action{$a},
209 Template => $template{$t},
212 $Scrip->LoadByCols(%param);
213 if (!defined($Scrip->Id)) {
214 my ($val, $msg) = $Scrip->Create(%param, Description => $desc);