fix 'Can't call method svcdb without a package or object reference' when ordering...
[freeside.git] / FS / FS / TicketSystem.pm
1 package FS::TicketSystem;
2
3 use strict;
4 use vars qw( $conf $system $AUTOLOAD );
5 use FS::Conf;
6 use FS::UID qw( dbh driver_name );
7 use FS::Record qw( dbdef );
8
9 FS::UID->install_callback( sub { 
10   $conf = new FS::Conf;
11   $system = $conf->config('ticket_system');
12 } );
13
14 sub AUTOLOAD {
15   my $self = shift;
16
17   my($sub)=$AUTOLOAD;
18   $sub =~ s/.*://;
19
20   my $conf = new FS::Conf;
21   die "FS::TicketSystem::$AUTOLOAD called, but no ticket system configured\n"
22     unless $system;
23
24   eval "use FS::TicketSystem::$system;";
25   die $@ if $@;
26
27   $self .= "::$system";
28   $self->$sub(@_);
29 }
30
31 # Our schema changes
32 my %columns = (
33   Tickets => {
34     WillResolve => { type => 'timestamp', null => 1, default => '', },
35   },
36   CustomFields => {
37     Required => { type => 'integer', default => 0, null => 0 },
38   },
39 );
40
41 sub _upgrade_schema {
42   my $system = FS::Conf->new->config('ticket_system');
43   return if !defined($system) || $system ne 'RT_Internal';
44   my ($class, %opts) = @_;
45
46   my $dbh = dbh;
47   my @sql;
48   my $case = driver_name eq 'mysql' ? sub {@_} : sub {map lc, @_};
49   foreach my $tablename (keys %columns) {
50     my $table = dbdef->table(&$case($tablename));
51     if ( !$table ) {
52       warn 
53       "$tablename table does not exist.  Your RT installation is incomplete.\n";
54       next;
55     }
56     foreach my $colname (keys %{ $columns{$tablename} }) {
57       if ( !$table->column(&$case($colname)) ) {
58         my $col = new DBIx::DBSchema::Column {
59             table_obj => $table,
60             name => &$case($colname),
61             %{ $columns{$tablename}->{$colname} }
62           };
63         $col->table_obj($table);
64         push @sql, $col->sql_add_column($dbh);
65       }
66     } #foreach $colname
67   } #foreach $tablename
68
69   return if !@sql;
70   warn "Upgrading RT schema:\n";
71   foreach my $statement (@sql) {
72     warn "$statement\n";
73     $dbh->do( $statement )
74       or die "Error: ". $dbh->errstr. "\n executing: $statement";
75   }
76   return;
77 }
78
79 sub _upgrade_data {
80   return if !defined($system) || $system ne 'RT_Internal';
81   my ($class, %opts) = @_;
82
83   # go ahead and use the RT API for this
84   
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;
89
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(
96         'Name' => $username,
97         'Gecos' => $username,
98         'Privileged' => 1,
99         # any other fields needed?
100       );
101       die $msg if !$val;
102     }
103     my $Principal = $User->PrincipalObj; # can this ever fail?
104     my @rights = ( qw(ShowTicket SeeQueue ModifyTicket ReplyToTicket 
105                       CreateTicket SeeCustomField) );
106     foreach (@rights) {
107       next if $Principal->HasRight( 'Right' => $_, Object => $RT::System );
108       my ($val, $msg) = $Principal->GrantRight(
109         'Right' => $_,
110         'Object' => $RT::System,
111       );
112       die $msg if !$val;
113     }
114   } #foreach $username
115
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',
122       'Type' => 'Select',
123       'MaxValues' => 1,
124       'LookupType' => 'RT::Queue',
125       'Description' => 'Escalate to Queue',
126       'ValuesClass' => 'RT::CustomFieldValues::Queues', #magic!
127     );
128     die $msg if !$val;
129     my $OCF = RT::ObjectCustomField->new($CurrentUser);
130     ($val, $msg) = $OCF->Create(
131       'CustomField' => $CF->Id,
132       'ObjectId' => 0,
133     );
134     die $msg if !$val;
135   }
136
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 };
142   if ( $@ ) {
143     warn "Couldn't load RT data from '$datafile': $@\n(skipping)\n";
144     return;
145   }
146
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.
150   my $cachify = sub {
151     my ($class, $hash) = @_;
152     my $search = $class->new($CurrentUser);
153     $search->UnLimit;
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;
158       }
159       else {
160         push @$ids, $item->Id;
161       }
162     }
163   };
164
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
171
172   # ScripConditions
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 );
178     die $msg if !$val;
179     $condition{ lc($ScripCondition->Name) } = [ $ScripCondition->Id ];
180   }
181
182   # ScripActions
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 );
188     die $msg if !$val;
189     $action{ lc($ScripAction->Name) } = [ $ScripAction->Id ];
190   }
191
192   # Templates
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 );
198     die $msg if !$val;
199     $template{ lc($Template->Name) } = [ $Template->Id ];
200   }
201
202   # Scrips
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;
213     }
214     else {
215       $scrip{$c}{$a}{$t} = $item->id;
216     }
217   }
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";
227       next;
228     }
229     if ( !exists($action{$a}) ) {
230       warn "ScripAction '$a' not found.\n";
231       next;
232     }
233     if ( !exists($template{$t}) ) {
234       warn "Template '$t' not found.\n";
235       next;
236     }
237     my %new_param = (
238       ScripCondition => $condition{$c}->[0],
239       ScripAction => $action{$a}->[0],
240       Template => $template{$t}->[0],
241       Queue => 0,
242       Description => $desc,
243     );
244     warn "Creating scrip: $c $a [$t]\n";
245     my ($val, $msg) = $Scrip->Create(%new_param);
246     die $msg if !$val;
247   } #foreach (@Scrips)
248
249   return;
250 }
251
252 1;