RT# 75817 - fixed issue with contact edit on edit customer page
[freeside.git] / httemplate / edit / cust_main-contacts.html
1 <SCRIPT>
2   function checkPasswordValidation(fieldid)  {
3     var validationResult = document.getElementById(fieldid+'_result').innerHTML;
4     if (validationResult.match(/Password valid!/)) {
5       return true;
6     }
7     else {
8       return false;
9     }
10   }
11 </SCRIPT>
12
13 <& '/elements/validate_password_js.html', &>
14
15 <& elements/edit.html,
16      'name_singular'    => 'customer contacts', #yes, we're editing all of them
17      'table'            => 'cust_main',
18      'post_url'         => popurl(1). 'process/cust_main-contacts.html',
19      'no_pkey_display'  => 1,
20      'submit_id'        => 'submit',
21      'labels'           => { 'custnum'     => ' ', #XXX supress this line entirely, its being redundant
22                              'contactnum'  => ' ', #'Contact',
23                              #'locationnum' => '&nbsp;',
24                            },
25      'fields'           => [
26        { 'field'             => 'contactnum',
27          'type'              => 'contact',
28          'colspan'           => 6,
29          'custnum'           => $custnum, 
30          'm2m_method'        => 'cust_contact',
31          'm2m_dstcol'        => 'contactnum',   
32          'm2_label'          => ' ', #'Contact',
33          'm2_error_callback' => $m2_error_callback,
34        },
35      ],
36      #'new_callback'    => $new_callback,
37      #'edit_callback'   => $edit_callback,
38      #'error_callback'  => $error_callback,
39      'agent_virt'      => 1,
40      'menubar'          => [], #remove "view all" link
41
42      #XXX it would be nice if this could instead be after the error but before
43      # the table
44      'html_init'        => include('/elements/small_custview.html',
45                                      $custnum,
46                                      $conf->config('countrydefault') || 'US',
47                                      1, #no balance
48                                   ),
49 &>
50 <%init>
51
52 my $curuser = $FS::CurrentUser::CurrentUser;
53 my $conf = new FS::Conf;
54
55 if ( $cgi->param('redirect') ) {
56   my $session = $cgi->param('redirect');
57   my $pref = $curuser->option("redirect$session");
58   die "unknown redirect session $session\n" unless length($pref);
59   $cgi = new CGI($pref);
60 }
61
62 my $custnum;
63 if ( $cgi->param('error') ) {
64   $custnum = scalar($cgi->param('custnum'));
65
66   die "access denied"
67     unless $curuser->access_right(($custnum ? 'Edit' : 'New'). ' customer'); #contacts?
68
69 } elsif ( $cgi->keywords ) { #editing
70   $custnum = ($cgi->keywords)[0];
71
72   die "access denied"
73     unless $curuser->access_right('Edit customer');
74
75 } else { #new customer
76
77   #this doesn't really work here, we're an edit only
78   die "guru meditation #32";
79
80   die "access denied"
81     unless $curuser->access_right('New customer');
82
83 }
84
85 #my $new_callback = sub {
86 #  my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
87 #};
88
89 #my $edit_callback = sub {
90 # my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
91 #};
92
93 #my $error_callback = sub {
94 #  my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
95 #};
96
97 my $m2_error_callback = sub {
98   my($cgi, $object) = @_;
99
100   #process_o2m fields in process/cust_main-contacts.html
101   my @fields = qw( first last title comment );
102   my @gfields = ( '', map "_$_", @fields );
103
104   map {
105         if ( /^contactnum(\d+)$/ ) {
106           my $num = $1;
107           if ( grep $cgi->param("contactnum$num$_"), @gfields ) {
108             my $x = new FS::contact {
109               'contactnum' => scalar($cgi->param("contactnum$num")),
110               map { $_ => scalar($cgi->param("contactnum${num}_$_")) } @fields,
111             };
112             $x;
113           } else {
114             ();
115           }
116         } else {
117           ();
118         }
119       }
120       $cgi->param;
121 };
122
123 </%init>