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;
90 # selfservice and cron users
91 foreach my $username ('%%%SELFSERVICE_USER%%%', 'fs_daily') {
92 my $User = RT::User->new($CurrentUser);
93 $User->Load($username);
94 if (!defined($User->Id)) {
95 my ($val, $msg) = $User->Create(
99 # any other fields needed?
103 my $Principal = $User->PrincipalObj; # can this ever fail?
104 my @rights = ( qw(ShowTicket SeeQueue ModifyTicket ReplyToTicket
105 CreateTicket SeeCustomField) );
107 next if $Principal->HasRight( 'Right' => $_, Object => $RT::System );
108 my ($val, $msg) = $Principal->GrantRight(
110 'Object' => $RT::System,
116 # EscalateQueue custom field and friends
117 my $CF = RT::CustomField->new($CurrentUser);
118 $CF->Load('EscalateQueue');
119 if (!defined($CF->Id)) {
120 my ($val, $msg) = $CF->Create(
121 'Name' => 'EscalateQueue',
124 'LookupType' => 'RT::Queue',
125 'Description' => 'Escalate to Queue',
126 'ValuesClass' => 'RT::CustomFieldValues::Queues', #magic!
129 my $OCF = RT::ObjectCustomField->new($CurrentUser);
130 ($val, $msg) = $OCF->Create(
131 'CustomField' => $CF->Id,
137 # Load from RT data file
138 our (@Groups, @Users, @ACL, @Queues, @ScripActions, @ScripConditions,
139 @Templates, @CustomFields, @Scrips, @Attributes, @Initial, @Final,
141 my $datafile = '%%%RT_PATH%%%/etc/initialdata';
142 eval { require $datafile };
144 warn "Couldn't load RT data from '$datafile': $@\n(skipping)\n";
148 # Cache existing ScripCondition, ScripAction, and Template IDs.
149 # Complicated because we don't want to just step on multiple IDs
150 # with the same name.
152 my ($class, $hash) = @_;
153 my $search = $class->new($CurrentUser);
155 while ( my $item = $search->Next ) {
156 my $ids = $hash->{lc($item->Name)} ||= [];
157 if ( $item->Creator == 1 ) { # RT::SystemUser
158 unshift @$ids, $item->Id;
161 push @$ids, $item->Id;
166 my (%condition, %action, %template);
167 &$cachify('RT::ScripConditions', \%condition);
168 &$cachify('RT::ScripActions', \%action);
169 &$cachify('RT::Templates', \%template);
170 # $condition{name} = [ ids... ]
171 # with the id of the system-created object first, if there is one
174 my $ScripCondition = RT::ScripCondition->new($CurrentUser);
175 foreach my $sc (@ScripConditions) {
176 # $sc: Name, Description, ApplicableTransTypes, ExecModule, Argument
177 next if exists( $condition{ lc($sc->{Name}) } );
178 my ($val, $msg) = $ScripCondition->Create( %$sc );
180 $condition{ lc($ScripCondition->Name) } = [ $ScripCondition->Id ];
184 my $ScripAction = RT::ScripAction->new($CurrentUser);
185 foreach my $sa (@ScripActions) {
186 # $sa: Name, Description, ExecModule, Argument
187 next if exists( $action{ lc($sa->{Name}) } );
188 my ($val, $msg) = $ScripAction->Create( %$sa );
190 $action{ lc($ScripAction->Name) } = [ $ScripAction->Id ];
194 my $Template = RT::Template->new($CurrentUser);
195 foreach my $t (@Templates) {
196 # $t: Queue, Name, Description, Content
197 next if exists( $template{ lc($t->{Name}) } );
198 my ($val, $msg) = $Template->Create( %$t );
200 $template{ lc($Template->Name) } = [ $Template->Id ];
204 my %scrip; # $scrips{condition}{action}{template} = id
205 my $search = RT::Scrips->new($CurrentUser);
206 $search->Limit(FIELD => 'Queue', VALUE => 0);
207 while (my $item = $search->Next) {
208 my ($c, $a, $t) = map {lc $item->$_->Name}
209 ('ScripConditionObj', 'ScripActionObj', 'TemplateObj');
210 if ( exists $scrip{$c}{$a}{$t} and $item->Creator == 1 ) {
211 warn "Deleting duplicate scrip $c $a [$t]\n";
212 my ($val, $msg) = $item->Delete;
213 warn "error deleting scrip: $msg\n" if !$val;
215 elsif ( exists $Delete_Scrips{$c}{$a}{$t} and $item->Creator == 1 ) {
216 warn "Deleting obsolete scrip $c $a [$t]\n";
217 my ($val, $msg) = $item->Delete;
218 warn "error deleting scrip: $msg\n" if !$val;
221 $scrip{$c}{$a}{$t} = $item->id;
224 my $Scrip = RT::Scrip->new($CurrentUser);
225 foreach my $s ( @Scrips ) {
226 my $desc = $s->{'Description'};
227 my ($c, $a, $t) = map lc,
228 @{ $s }{'ScripCondition', 'ScripAction', 'Template'};
229 # skip existing scrips
230 next if ( exists($scrip{$c}{$a}{$t}) );
231 if ( !exists($condition{$c}) ) {
232 warn "ScripCondition '$c' not found.\n";
235 if ( !exists($action{$a}) ) {
236 warn "ScripAction '$a' not found.\n";
239 if ( !exists($template{$t}) ) {
240 warn "Template '$t' not found.\n";
244 ScripCondition => $condition{$c}->[0],
245 ScripAction => $action{$a}->[0],
246 Template => $template{$t}->[0],
248 Description => $desc,
250 warn "Creating scrip: $c $a [$t]\n";
251 my ($val, $msg) = $Scrip->Create(%new_param);