contacts can be shared among customers / "duplicate contact emails", RT#27943
[freeside.git] / httemplate / edit / cust_main-contacts.html
1 <& elements/edit.html,
2      'name_singular'   => 'customer contacts', #yes, we're editing all of them
3      'table'           => 'cust_main',
4      'post_url'        => popurl(1). 'process/cust_main-contacts.html',
5      'no_pkey_display' => 1,
6      'labels'          => {
7                             'contactnum'  => ' ', #'Contact',
8                             #'locationnum' => '&nbsp;',
9                           },
10      'fields'          => [
11        { 'field'             => 'contactnum',
12          'type'              => 'contact',
13          'colspan'           => 6,
14          'm2m_method'        => 'cust_contact',
15          'm2m_dstcol'        => 'contactnum',   
16          'm2_label'          => ' ', #'Contact',
17          'm2_error_callback' => $m2_error_callback,
18        },
19      ],
20      #'new_callback'    => $new_callback,
21      #'edit_callback'   => $edit_callback,
22      #'error_callback'  => $error_callback,
23      'agent_virt'      => 1,
24      'menubar'         => [], #remove "view all" link
25
26      #XXX it would be nice if this could instead be after the error but before
27      # the table
28      'html_init'       => include('/elements/small_custview.html',
29                                     $custnum,
30                                     $conf->config('countrydefault') || 'US',
31                                     1, #no balance
32                                  ).
33                           '<BR>',
34 &>
35 <%init>
36
37 my $curuser = $FS::CurrentUser::CurrentUser;
38 my $conf = new FS::Conf;
39
40 my $custnum;
41 if ( $cgi->param('error') ) {
42   $custnum = scalar($cgi->param('custnum'));
43
44   die "access denied"
45     unless $curuser->access_right(($custnum ? 'Edit' : 'New'). ' customer'); #contacts?
46
47 } elsif ( $cgi->keywords ) { #editing
48   $custnum = ($cgi->keywords)[0];
49
50   die "access denied"
51     unless $curuser->access_right('Edit customer');
52
53 } else { #new customer
54
55   #this doesn't really work here, we're an edit only
56   die "guru meditation #32";
57
58   die "access denied"
59     unless $curuser->access_right('New customer');
60
61 }
62
63 #my $new_callback = sub {
64 #  my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
65 #};
66
67 #my $edit_callback = sub {
68 # my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
69 #};
70
71 #my $error_callback = sub {
72 #  my( $cgi, $prospect_main, $fields_listref, $opt_hashref ) = @_;
73 #};
74
75 my $m2_error_callback = sub {
76   my($cgi, $object) = @_;
77
78   #process_o2m fields in process/cust_main-contacts.html
79   my @fields = qw( first last title comment );
80   my @gfields = ( '', map "_$_", @fields );
81
82   map {
83         if ( /^contactnum(\d+)$/ ) {
84           my $num = $1;
85           if ( grep $cgi->param("contactnum$num$_"), @gfields ) {
86             my $x = new FS::contact {
87               'contactnum' => scalar($cgi->param("contactnum$num")),
88               map { $_ => scalar($cgi->param("contactnum${num}_$_")) } @fields,
89             };
90             $x;
91           } else {
92             ();
93           }
94         } else {
95           ();
96         }
97       }
98       $cgi->param;
99 };
100
101 </%init>