88baf0764264e9ec59c629f4ab42b60f07827c69
[freeside.git] / FS / FS / ClientAPI / MasonComponent.pm
1 package FS::ClientAPI::MasonComponent;
2
3 use strict;
4 use vars qw( $cache $DEBUG $me );
5 use subs qw( _cache );
6 use FS::Mason qw( mason_interps );
7 use FS::Conf;
8 use FS::ClientAPI_SessionCache;
9 use FS::Record qw( qsearch qsearchs );
10 use FS::cust_main;
11 use FS::part_pkg;
12
13 $DEBUG = 0;
14 $me = '[FS::ClientAPI::MasonComponent]';
15
16 my %allowed_comps = map { $_=>1 } qw(
17   /elements/select-did.html
18   /misc/areacodes.cgi
19   /misc/exchanges.cgi
20   /misc/phonenums.cgi
21   /misc/states.cgi
22   /misc/counties.cgi
23   /misc/svc_acct-domains.cgi
24   /misc/part_svc-columns.cgi
25 );
26
27 my %session_comps = map { $_=>1 } qw(
28   /elements/location.html
29   /edit/cust_main/first_pkg/select-part_pkg.html
30 );
31
32 my %session_callbacks = (
33
34   '/elements/location.html' => sub {
35     my( $custnum, $argsref ) = @_;
36     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
37       or return "unknown custnum $custnum";
38     my %args = @$argsref;
39     $args{object} = $cust_main;
40     @$argsref = ( %args );
41     return ''; #no error
42   },
43
44   '/edit/cust_main/first_pkg/select-part_pkg.html' => sub {
45     my( $custnum, $argsref ) = @_;
46     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
47       or return "unknown custnum $custnum";
48
49     my $pkgpart = $cust_main->agent->pkgpart_hashref;
50
51     #false laziness w/ edit/cust_main/first_pkg.html
52     my @first_svc = ( 'svc_acct', 'svc_phone' );
53
54     my @part_pkg =
55       grep { $_->svcpart(\@first_svc)
56              && ( $pkgpart->{ $_->pkgpart } 
57                   || ( $_->agentnum && $_->agentnum == $cust_main->agentnum )
58                 )
59            }
60       qsearch( 'part_pkg', { 'disabled' => '' }, '', 'ORDER BY pkg' ); # case?
61
62     my %args = @$argsref;
63     $args{part_pkg} = \@part_pkg;
64     @$argsref = ( %args );
65     return ''; #no error
66
67   },
68
69 );
70
71 my $outbuf;
72 my( $fs_interp, $rt_interp ) = mason_interps('standalone', 'outbuf'=>\$outbuf);
73
74 sub mason_comp {
75   my $packet = shift;
76
77   warn "$me mason_comp called on $packet\n" if $DEBUG;
78
79   my $comp = $packet->{'comp'};
80   unless ( $allowed_comps{$comp} || $session_comps{$comp} ) {
81     return { 'error' => 'Illegal component' };
82   }
83
84   my @args = $packet->{'args'} ? @{ $packet->{'args'} } : ();
85
86   if ( $session_comps{$comp} ) {
87
88     my $session = _cache->get($packet->{'session_id'})
89       or return ( 'error' => "Can't resume session" ); #better error message
90     my $custnum = $session->{'custnum'};
91
92     my $error = &{ $session_callbacks{$comp} }( $custnum, \@args );
93     return { 'error' => $error } if $error;
94
95   }
96
97   my $conf = new FS::Conf;
98   $FS::Mason::Request::FSURL = $conf->config('selfservice_server-base_url');
99   $FS::Mason::Request::QUERY_STRING = $packet->{'query_string'} || '';
100
101   $outbuf = '';
102   $fs_interp->exec($comp, @args); #only FS for now alas...
103
104   #errors? (turn off in-line error reporting?)
105
106   return { 'output' => $outbuf };
107
108 }
109
110 #hmm
111 sub _cache {
112   $cache ||= new FS::ClientAPI_SessionCache( {
113                'namespace' => 'FS::ClientAPI::MyAccount',
114              } );
115 }
116
117 1;