add uid to mass duplicate checking on export changes, fix bug in new export editing...
[freeside.git] / httemplate / edit / part_svc.cgi
1 <%
2 my $part_svc;
3 my $clone = '';
4 my $error = '';
5 if ( $cgi->param('magic') eq 'process' ) {
6
7   my $svcpart = $cgi->param('svcpart');
8   my $old = qsearchs('part_svc', { 'svcpart' => $svcpart }) if $svcpart;
9   
10   $cgi->param( 'svc_acct__usergroup',
11                join(',', $cgi->param('svc_acct__usergroup') ) );
12   
13   my $new = new FS::part_svc ( {
14     map {
15       $_, scalar($cgi->param($_));
16   #  } qw(svcpart svc svcdb)
17     } ( fields('part_svc'),
18         map { my $svcdb = $_;
19               my @fields = fields($svcdb);
20               push @fields, 'usergroup' if $svcdb eq 'svc_acct'; #kludge
21               map { ( $svcdb.'__'.$_, $svcdb.'__'.$_.'_flag' )  } @fields;
22             } grep defined( $FS::Record::dbdef->table($_) ),
23                    qw( svc_acct svc_domain svc_forward svc_www svc_broadband )
24       )
25   } );
26   
27   my %exportnums =
28     map { $_->exportnum => ( $cgi->param('exportnum'.$_->exportnum) || '') }
29         qsearch('part_export', {} );
30
31   if ( $svcpart ) {
32     $error = $new->replace($old, '1.3-COMPAT', [ 'usergroup' ], \%exportnums );
33   } else {
34     $error = $new->insert( [ 'usergroup' ], \%exportnums );
35     $svcpart = $new->getfield('svcpart');
36   }
37
38   unless ( $error ) { #no error, redirect
39     #print $cgi->redirect(popurl(3)."browse/part_svc.cgi");
40     print $cgi->redirect("${p}browse/part_svc.cgi");
41     myexit;
42   }
43
44   $part_svc = $new; #??
45   #$part_svc = new FS::part_svc ( {
46   #  map { $_, scalar($cgi->param($_)) } fields('part_svc')
47   #} );
48
49 } elsif ( $cgi->param('clone') && $cgi->param('clone') =~ /^(\d+)$/ ) {#clone
50   #$cgi->param('clone') =~ /^(\d+)$/ or die "malformed query: $query";
51   $part_svc = qsearchs('part_svc', { 'svcpart'=>$1 } )
52     or die "unknown svcpart: $1";
53   $clone = $part_svc->svcpart;
54   $part_svc->svcpart('');
55 } elsif ( $cgi->keywords ) { #edit
56   my($query) = $cgi->keywords;
57   $query =~ /^(\d+)$/ or die "malformed query: $query";
58   $part_svc=qsearchs('part_svc', { 'svcpart'=>$1 } )
59     or die "unknown svcpart: $1";
60 } else { #adding
61   $part_svc = new FS::part_svc {};
62 }
63
64 my $action = $part_svc->svcpart ? 'Edit' : 'Add';
65 my $hashref = $part_svc->hashref;
66 #   my $p_svcdb = $part_svc->svcdb || 'svc_acct';
67
68
69            #" onLoad=\"visualize()\""
70 %>
71 <!-- mason kludge -->
72 <%= header("$action Service Definition",
73            menubar( 'Main Menu'         => $p,
74                     'View all service definitions' => "${p}browse/part_svc.cgi"
75                   ),
76            )
77 %>
78
79 <% if ( $error ) { %>
80 <FONT SIZE="+1" COLOR="#ff0000">Error: <%= $error %></FONT>
81 <% } %>
82
83 <FORM NAME="dummy">
84
85       Service Part #<%= $part_svc->svcpart ? $part_svc->svcpart : "(NEW)" %>
86 <BR><BR>
87 Service  <INPUT TYPE="text" NAME="svc" VALUE="<%= $hashref->{svc} %>"><BR>
88 Disable new orders <INPUT TYPE="checkbox" NAME="disabled" VALUE="Y"<%= $hashref->{disabled} eq 'Y' ? ' CHECKED' : '' %>><BR>
89 <INPUT TYPE="hidden" NAME="magic" VALUE="process">
90 <INPUT TYPE="hidden" NAME="svcpart" VALUE="<%= $hashref->{svcpart} %>">
91 <BR>
92 Services are items you offer to your customers.
93 <UL><LI>svc_acct - Shell accounts, POP mailboxes, SLIP/PPP and ISDN accounts
94     <LI>svc_domain - Domains
95     <LI>svc_acct_sm - <B>deprecated</B> (use svc_forward for new installations) Virtual domain mail aliasing.
96     <LI>svc_forward - mail forwarding
97     <LI>svc_www - Virtual domain website
98 <!--   <LI>svc_charge - One-time charges (Partially unimplemented)
99        <LI>svc_wo - Work orders (Partially unimplemented)
100 -->
101 </UL>
102 For the selected table, you can give fields default or fixed (unchangable)
103 values.  For example, a SLIP/PPP account may have a default (or perhaps fixed)
104 <B>slipip</B> of <B>0.0.0.0</B>, while a POP mailbox will probably have a fixed
105 blank <B>slipip</B> as well as a fixed shell something like <B>/bin/true</B> or
106 <B>/usr/bin/passwd</B>.
107 <BR><BR>
108
109 <%
110 #these might belong somewhere else for other user interfaces 
111 #pry need to eventually create stuff that's shared amount UIs
112 my $conf = new FS::Conf;
113 my %defs = (
114   'svc_acct' => {
115     'dir'       => 'Home directory',
116     'uid'       => 'UID (set to fixed and blank for dial-only)',
117     'slipip'    => 'IP address (Set to fixed and blank to disable dialin, or, set a value to be exported to RADIUS Framed-IP-Address.  Use the special value <code>0e0</code> [zero e zero] to enable export to RADIUS without a Framed-IP-Address.)',
118 #    'popnum'    => qq!<A HREF="$p/browse/svc_acct_pop.cgi/">POP number</A>!,
119     'popnum'    => {
120                      desc => 'Access number',
121                      type => 'select',
122                      select_table => 'svc_acct_pop',
123                      select_key   => 'popnum',
124                      select_label => 'city',
125                    },
126     'username'  => {
127                       desc => 'Username',
128                       type => 'disabled',
129                    },
130     'quota'     => '',
131     '_password' => 'Password',
132     'gid'       => 'GID (when blank, defaults to UID)',
133     'shell'     => {
134                      desc =>'Shell (all service definitions should have a default or fixed shell that is present in the <b>shells</b> configuration file)',
135                      type =>'select',
136                      select_list => [ $conf->config('shells') ],
137                    },
138     'finger'    => 'GECOS',
139     'domsvc'    => {
140                      desc =>'svcnum from svc_domain',
141                      type =>'select',
142                      select_table => 'svc_domain',
143                      select_key   => 'svcnum',
144                      select_label => 'domain',
145                    },
146     'usergroup' => {
147                      desc =>'ICRADIUS/FreeRADIUS groups',
148                      type =>'radius_usergroup_selector',
149                    },
150   },
151   'svc_domain' => {
152     'domain'    => 'Domain',
153   },
154   'svc_acct_sm' => {
155     'domuser'   => 'domuser@virtualdomain.com',
156     'domuid'    => 'UID where domuser@virtualdomain.com mail is forwarded',
157     'domsvc'    => 'svcnum from svc_domain for virtualdomain.com',
158   },
159   'svc_forward' => {
160     'srcsvc'    => 'service from which mail is to be forwarded',
161     'dstsvc'    => 'service to which mail is to be forwarded',
162     'dst'       => 'someone@another.domain.com to use when dstsvc is 0',
163   },
164   'svc_charge' => {
165     'amount'    => 'amount',
166   },
167   'svc_wo' => {
168     'worker'    => 'Worker',
169     '_date'      => 'Date',
170   },
171   'svc_www' => {
172     #'recnum' => '',
173     #'usersvc' => '',
174   },
175 );
176
177   my @dbs = $hashref->{svcdb}
178              ? ( $hashref->{svcdb} )
179              : qw( svc_acct svc_domain svc_acct_sm svc_forward svc_www );
180
181   tie my %svcdb, 'Tie::IxHash', map { $_=>$_ } @dbs;
182   my $widget = new HTML::Widgets::SelectLayers(
183     #'selected_layer' => $p_svcdb,
184     'selected_layer' => $hashref->{svcdb} || 'svc_acct',
185     'options'        => \%svcdb,
186     'form_name'      => 'dummy',
187     #'form_action'    => 'process/part_svc.cgi',
188     'form_action'    => 'part_svc.cgi', #self
189     'form_text'      => [ qw( magic svc svcpart ) ],
190     'form_checkbox'  => [ 'disabled' ],
191     'layer_callback' => sub {
192       my $layer = shift;
193       my $html = qq!<INPUT TYPE="hidden" NAME="svcdb" VALUE="$layer">!;
194
195       my $columns = 3;
196       my $count = 0;
197       my @part_export =
198         map { qsearch( 'part_export', {exporttype => $_ } ) }
199           keys %{FS::part_export::export_info($layer)};
200       $html .= '<BR><BR>'. table().
201                table(). "<TR><TH COLSPAN=$columns>Exports</TH></TR><TR>";
202       foreach my $part_export ( @part_export ) {
203         $html .= '<TD><INPUT TYPE="checkbox"'.
204                  ' NAME="exportnum'. $part_export->exportnum. '"  VALUE="1" ';
205         $html .= 'CHECKED'
206           if ( $clone || $part_svc->svcpart ) #null svcpart search causing error
207               && qsearchs( 'export_svc', {
208                                    exportnum => $part_export->exportnum,
209                                    svcpart   => $clone || $part_svc->svcpart });
210         $html .= '>'. $part_export->exportnum. ': '. $part_export->exporttype.
211                  ' to '. $part_export->machine. '</TD>';
212         $count++;
213         $html .= '</TR><TR>' unless $count % $columns;
214       }
215       $html .= '</TR></TABLE><BR><BR>';
216
217       $html .=  table(). "<TH>Field</TH><TH COLSPAN=2>Modifier</TH>";
218       #yucky kludge
219       my @fields = defined( $FS::Record::dbdef->table($layer) )
220                       ? grep { $_ ne 'svcnum' } fields($layer)
221                       : ();
222       push @fields, 'usergroup' if $layer eq 'svc_acct'; #kludge
223       $part_svc->svcpart($clone) if $clone; #haha, undone below
224       foreach my $field (@fields) {
225         my $part_svc_column = $part_svc->part_svc_column($field);
226         my $value = $error
227                       ? $cgi->param("${layer}__${field}")
228                       : $part_svc_column->columnvalue;
229         my $flag = $error
230                      ? $cgi->param("${layer}__${field}_flag")
231                      : $part_svc_column->columnflag;
232         my $def = $defs{$layer}{$field};
233         my $desc = ref($def) ? $def->{desc} : $def;
234         
235         $html .= "<TR><TD>$field";
236         $html .= "- <FONT SIZE=-1>$desc</FONT>" if $desc;
237         $html .=  "</TD>";
238         $flag = '' if ref($def) && $def->{type} eq 'disabled';
239         $html .=
240           qq!<TD><INPUT TYPE="radio" NAME="${layer}__${field}_flag" VALUE=""!.
241           ' CHECKED'x($flag eq ''). ">Off</TD>".
242           '<TD>';
243         unless ( ref($def) && $def->{type} eq 'disabled' ) {
244           $html .= 
245             qq!<INPUT TYPE="radio" NAME="${layer}__${field}_flag" VALUE="D"!.
246             ' CHECKED'x($flag eq 'D'). ">Default ".
247             qq!<INPUT TYPE="radio" NAME="${layer}__${field}_flag" VALUE="F"!.
248             ' CHECKED'x($flag eq 'F'). ">Fixed ".
249             '<BR>';
250         }
251         if ( ref($def) ) {
252           if ( $def->{type} eq 'select' ) {
253             $html .= qq!<SELECT NAME="${layer}__${field}">!;
254             $html .= '<OPTION> </OPTION>' unless $value;
255             if ( $def->{select_table} ) {
256               foreach my $record ( qsearch( $def->{select_table}, {} ) ) {
257                 my $rvalue = $record->getfield($def->{select_key});
258                 $html .= qq!<OPTION VALUE="$rvalue"!.
259                          ( $rvalue==$value ? ' SELECTED>' : '>' ).
260                          $record->getfield($def->{select_label}). '</OPTION>';
261               } #next $record
262             } else { # select_list
263               foreach my $item ( @{$def->{select_list}} ) {
264                 $html .= qq!<OPTION VALUE="$item"!.
265                          ( $item eq $value ? ' SELECTED>' : '>' ).
266                          $item. '</OPTION>';
267               } #next $item
268             } #endif
269             $html .= '</SELECT>';
270           } elsif ( $def->{type} eq 'radius_usergroup_selector' ) {
271             $html .= FS::svc_acct::radius_usergroup_selector(
272               [ split(',', $value) ], "${layer}__${field}" );
273           } elsif ( $def->{type} eq 'disabled' ) {
274             $html .=
275               qq!<INPUT TYPE="hidden" NAME="${layer}__${field}" VALUE="">!;
276           } else {
277             $html .= '<font color="#ff0000">unknown type'. $def->{type};
278           }
279         } else {
280           $html .=
281             qq!<INPUT TYPE="text" NAME="${layer}__${field}" VALUE="$value">!;
282         }
283         $html .= "</TD></TR>\n";
284       }
285       $part_svc->svcpart('') if $clone; #undone
286       $html .= "</TABLE>";
287
288       $html .= '<BR><INPUT TYPE="submit" VALUE="'.
289                ($hashref->{svcpart} ? 'Apply changes' : 'Add service'). '">';
290
291       $html;
292
293     },
294   );
295
296 %>
297 Table <%= $widget->html %>
298   </BODY>
299 </HTML>
300