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);
140 my $datafile = '%%%RT_PATH%%%/etc/initialdata';
141 eval { require $datafile };
143 warn "Couldn't load RT data from '$datafile': $@\n(skipping)\n";
147 # Cache existing ScripCondition, ScripAction, and Template IDs.
148 # Complicated because we don't want to just step on multiple IDs
149 # with the same name.
151 my ($class, $hash) = @_;
152 my $search = $class->new($CurrentUser);
154 while ( my $item = $search->Next ) {
155 my $ids = $hash->{lc($item->Name)} ||= [];
156 if ( $item->Creator == 1 ) { # RT::SystemUser
157 unshift @$ids, $item->Id;
160 push @$ids, $item->Id;
165 my (%condition, %action, %template);
166 &$cachify('RT::ScripConditions', \%condition);
167 &$cachify('RT::ScripActions', \%action);
168 &$cachify('RT::Templates', \%template);
169 # $condition{name} = [ ids... ]
170 # with the id of the system-created object first, if there is one
173 my $ScripCondition = RT::ScripCondition->new($CurrentUser);
174 foreach my $sc (@ScripConditions) {
175 # $sc: Name, Description, ApplicableTransTypes, ExecModule, Argument
176 next if exists( $condition{ lc($sc->{Name}) } );
177 my ($val, $msg) = $ScripCondition->Create( %$sc );
179 $condition{ lc($ScripCondition->Name) } = [ $ScripCondition->Id ];
183 my $ScripAction = RT::ScripAction->new($CurrentUser);
184 foreach my $sa (@ScripActions) {
185 # $sa: Name, Description, ExecModule, Argument
186 next if exists( $action{ lc($sa->{Name}) } );
187 my ($val, $msg) = $ScripAction->Create( %$sa );
189 $action{ lc($ScripAction->Name) } = [ $ScripAction->Id ];
193 my $Template = RT::Template->new($CurrentUser);
194 foreach my $t (@Templates) {
195 # $t: Queue, Name, Description, Content
196 next if exists( $template{ lc($t->{Name}) } );
197 my ($val, $msg) = $Template->Create( %$t );
199 $template{ lc($Template->Name) } = [ $Template->Id ];
203 my %scrip; # $scrips{condition}{action}{template} = id
204 my $search = RT::Scrips->new($CurrentUser);
205 $search->Limit(FIELD => 'Queue', VALUE => 0);
206 while (my $item = $search->Next) {
207 my ($c, $a, $t) = map {lc $item->$_->Name}
208 ('ScripConditionObj', 'ScripActionObj', 'TemplateObj');
209 if ( exists $scrip{$c}{$a}{$t} and $item->Creator == 1 ) {
210 warn "Deleting duplicate scrip $c $a [$t]\n";
211 my ($val, $msg) = $item->Delete;
212 warn "error deleting scrip: $msg\n" if !$val;
215 $scrip{$c}{$a}{$t} = $item->id;
218 my $Scrip = RT::Scrip->new($CurrentUser);
219 foreach my $s ( @Scrips ) {
220 my $desc = $s->{'Description'};
221 my ($c, $a, $t) = map lc,
222 @{ $s }{'ScripCondition', 'ScripAction', 'Template'};
223 # skip existing scrips
224 next if ( exists($scrip{$c}{$a}{$t}) );
225 if ( !exists($condition{$c}) ) {
226 warn "ScripCondition '$c' not found.\n";
229 if ( !exists($action{$a}) ) {
230 warn "ScripAction '$a' not found.\n";
233 if ( !exists($template{$t}) ) {
234 warn "Template '$t' not found.\n";
238 ScripCondition => $condition{$c}->[0],
239 ScripAction => $action{$a}->[0],
240 Template => $template{$t}->[0],
242 Description => $desc,
244 warn "Creating scrip: $c $a [$t]\n";
245 my ($val, $msg) = $Scrip->Create(%new_param);