This commit was generated by cvs2svn to compensate for changes in r4407,
[freeside.git] / FS / FS / UI / Web.pm
1 package FS::UI::Web;
2
3 use vars qw($DEBUG);
4 use FS::Conf;
5 use FS::Record qw(dbdef);
6
7 #use vars qw(@ISA);
8 #use FS::UI
9 #@ISA = qw( FS::UI );
10
11 use Date::Parse;
12 sub parse_beginning_ending {
13   my($cgi) = @_;
14
15   my $beginning = 0;
16   if ( $cgi->param('begin') =~ /^(\d+)$/ ) {
17     $beginning = $1;
18   } elsif ( $cgi->param('beginning') =~ /^([ 0-9\-\/]{1,64})$/ ) {
19     $beginning = str2time($1) || 0;
20   }
21
22   my $ending = 4294967295; #2^32-1
23   if ( $cgi->param('end') =~ /^(\d+)$/ ) {
24     $ending = $1 - 1;
25   } elsif ( $cgi->param('ending') =~ /^([ 0-9\-\/]{1,64})$/ ) {
26     #probably need an option to turn off the + 86399
27     $ending = str2time($1) + 86399;
28   }
29
30   ( $beginning, $ending );
31 }
32
33 ###
34 # cust_main report methods
35 ###
36
37 =item cust_header
38
39 Returns an array of customer information headers according to the
40 B<cust-fields> configuration setting.
41
42 =cut
43
44 use vars qw( @cust_fields );
45
46 sub cust_sql_fields {
47   my @fields = qw( last first company );
48   push @fields, map "ship_$_", @fields
49     if dbdef->table('cust_main')->column('ship_last');
50   map "cust_main.$_", @fields;
51 }
52
53 sub cust_header {
54
55   warn "FS::svc_Common::cust_header called"
56     if $DEBUG;
57
58   my $conf = new FS::Conf;
59
60   my %header2method = (
61     'Customer'           => 'name',
62     'Cust#'              => 'custnum',
63     'Name'               => 'contact',
64     'Company'            => 'company',
65     '(bill) Customer'    => 'name',
66     '(service) Customer' => 'ship_name',
67     '(bill) Name'        => 'contact',
68     '(service) Name'     => 'ship_contact',
69     '(bill) Company'     => 'company',
70     '(service) Company'  => 'ship_company',
71   );
72
73   my @cust_header;
74   if (    $conf->exists('cust-fields')
75        && $conf->config('cust-fields') =~ /^([\w \|\#\(\)]+):/
76      )
77   {
78     warn "  found cust-fields configuration value"
79       if $DEBUG;
80
81     my $cust_fields = $1;
82      @cust_header = split(/ \| /, $cust_fields);
83      @cust_fields = map { $header2method{$_} } @cust_header;
84   } else { 
85     warn "  no cust-fields configuration value found; using default 'Customer'"
86       if $DEBUG;
87     @cust_header = ( 'Customer' );
88     @cust_fields = ( 'name' );
89   }
90
91   #my $svc_x = shift;
92   @cust_header;
93 }
94
95 =item cust_fields
96
97 Given a svc_ object that contains fields from cust_main (say, from a
98 JOINed search.  See httemplate/search/svc_* for examples), returns an array
99 of customer information according to the <B>cust-fields</B> configuration
100 setting, or "(unlinked)" if this service is not linked to a customer.
101
102 =cut
103
104 sub cust_fields {
105   my $svc_x = shift;
106   warn "FS::svc_Common::cust_fields called for $svc_x ".
107        "(cust_fields: @cust_fields)"
108     if $DEBUG > 1;
109
110   cust_header() unless @cust_fields;
111
112   my $seen_unlinked = 0;
113   map { 
114     if ( $svc_x->custnum ) {
115       warn "  $svc_x -> $_"
116         if $DEBUG > 1;
117       $svc_x->$_(@_);
118     } else {
119       warn "  ($svc_x unlinked)"
120         if $DEBUG > 1;
121       $seen_unlinked++ ? '' : '(unlinked)';
122     }
123   } @cust_fields;
124 }
125
126 ###
127 # begin JSRPC code...
128 ###
129
130 package FS::UI::Web::JSRPC;
131
132 use strict;
133 use vars qw(@ISA $DEBUG);
134 use Storable qw(nfreeze);
135 use MIME::Base64;
136 #use JavaScript::RPC::Server::CGI;
137 use FS::UID;
138 use FS::Record qw(qsearchs);
139 use FS::queue;
140
141 #@ISA = qw( JavaScript::RPC::Server::CGI );
142 $DEBUG = 0;
143
144 sub new {
145         my $class = shift;
146         my $self  = {
147                 env => {},
148                 job => shift,
149                 cgi => shift,
150         };
151
152         bless $self, $class;
153
154         return $self;
155 }
156
157 sub process {
158
159   my $self = shift;
160
161   my $cgi = $self->{'cgi'};
162
163   # XXX this should parse JSON foo and build a proper data structure
164   my @args = $cgi->param('arg');
165
166   my $sub = $cgi->param('sub'); #????
167
168   warn "FS::UI::Web::JSRPC::process:\n".
169        "  cgi=$cgi\n".
170        "  sub=$sub\n".
171        "  args=".join(', ',@args)."\n"
172     if $DEBUG;
173
174   if ( $sub eq 'start_job' ) {
175
176     $self->start_job(@args);
177
178   } elsif ( $sub eq 'job_status' ) {
179
180     $self->job_status(@args);
181
182   }
183
184 }
185
186 sub start_job {
187   my $self = shift;
188
189   warn "FS::UI::Web::start_job: ". join(', ', @_) if $DEBUG;
190 #  my %param = @_;
191   my %param = ();
192   while ( @_ ) {
193     my( $field, $value ) = splice(@_, 0, 2);
194     unless ( exists( $param{$field} ) ) {
195       $param{$field} = $value;
196     } elsif ( ! ref($param{$field}) ) {
197       $param{$field} = [ $param{$field}, $value ];
198     } else {
199       push @{$param{$field}}, $value;
200     }
201   }
202   warn "FS::UI::Web::start_job\n".
203        join('', map {
204                       if ( ref($param{$_}) ) {
205                         "  $_ => [ ". join(', ', @{$param{$_}}). " ]\n";
206                       } else {
207                         "  $_ => $param{$_}\n";
208                       }
209                     } keys %param )
210     if $DEBUG;
211
212   #first get the CGI params shipped off to a job ASAP so an id can be returned
213   #to the caller
214   
215   my $job = new FS::queue { 'job' => $self->{'job'} };
216   
217   #too slow to insert all the cgi params as individual args..,?
218   #my $error = $queue->insert('_JOB', $cgi->Vars);
219   
220   #warn 'froze string of size '. length(nfreeze(\%param)). " for job args\n"
221   #  if $DEBUG;
222
223   my $error = $job->insert( '_JOB', encode_base64(nfreeze(\%param)) );
224
225   if ( $error ) {
226     $error;  #this doesn't seem to be handled well,
227              # will trigger "illegal jobnum" below?
228              # (should never be an error inserting the job, though, only thing
229              #  would be Pg f%*kage)
230   } else {
231     $job->jobnum;
232   }
233   
234 }
235
236 sub job_status {
237   my( $self, $jobnum ) = @_; #$url ???
238
239   sleep 1; # XXX could use something better...
240
241   my $job;
242   if ( $jobnum =~ /^(\d+)$/ ) {
243     $job = qsearchs('queue', { 'jobnum' => $jobnum } );
244   } else {
245     die "FS::UI::Web::job_status: illegal jobnum $jobnum\n";
246   }
247
248   my @return;
249   if ( $job && $job->status ne 'failed' ) {
250     @return = ( 'progress', $job->statustext );
251   } elsif ( !$job ) { #handle job gone case : job sucessful
252                       # so close popup, redirect parent window...
253     @return = ( 'complete' );
254   } else {
255     @return = ( 'error', $job ? $job->statustext : $jobnum );
256   }
257
258   #join("\n",@return);
259
260   #XXX should use JSON!
261   @return = map {
262     s/\\/\\\\/g;
263     s/\n/\\n/g;
264     s/"/\"/g;
265     $_
266   } @return;
267   
268   '[ '. join(', ', map { qq("$_") } @return). " ]\n";
269
270 }
271
272 #sub get_new_query {
273 #  FS::UID::cgi();
274 #}
275
276 1;
277